blob: 6a5ecdee9ae8ce0871792ef1e6f08de47582dbee [file] [log] [blame]
Stephen Craneba090d12017-05-09 15:44:35 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "github.com/google/blueprint"
19
20 "android/soong/android"
21)
22
23// LTO (link-time optimization) allows the compiler to optimize and generate
24// code for the entire module at link time, rather than per-compilation
25// unit. LTO is required for Clang CFI and other whole-program optimization
26// techniques. LTO also allows cross-compilation unit optimizations that should
27// result in faster and smaller code, at the expense of additional compilation
28// time.
29//
30// To properly build a module with LTO, the module and all recursive static
31// dependencies should be compiled with -flto which directs the compiler to emit
32// bitcode rather than native object files. These bitcode files are then passed
33// by the linker to the LLVM plugin for compilation at link time. Static
34// dependencies not built as bitcode will still function correctly but cannot be
35// optimized at link time and may not be compatible with features that require
36// LTO, such as CFI.
37//
38// This file adds support to soong to automatically propogate LTO options to a
39// new variant of all static dependencies for each module with LTO enabled.
40
41type LTOProperties struct {
42 // Lto must violate capitialization style for acronyms so that it can be
43 // referred to in blueprint files as "lto"
Yi Kong244bf072017-08-29 11:10:09 +080044 Lto struct {
45 Full *bool `android:"arch_variant"`
46 Thin *bool `android:"arch_variant"`
47 } `android:"arch_variant"`
48 LTODep bool `blueprint:"mutated"`
Stephen Craneba090d12017-05-09 15:44:35 -070049}
50
51type lto struct {
52 Properties LTOProperties
53}
54
55func (lto *lto) props() []interface{} {
56 return []interface{}{&lto.Properties}
57}
58
59func (lto *lto) begin(ctx BaseModuleContext) {
60}
61
62func (lto *lto) deps(ctx BaseModuleContext, deps Deps) Deps {
63 return deps
64}
65
66func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong244bf072017-08-29 11:10:09 +080067 if lto.LTO() {
68 var ltoFlag string
69 if Bool(lto.Properties.Lto.Thin) {
70 ltoFlag = "-flto=thin"
71 } else {
72 ltoFlag = "-flto"
73 }
74
75 flags.CFlags = append(flags.CFlags, ltoFlag)
76 flags.LdFlags = append(flags.LdFlags, ltoFlag)
Stephen Cranef5b9b952017-06-30 19:03:36 -070077 if ctx.Device() {
78 // Work around bug in Clang that doesn't pass correct emulated
79 // TLS option to target
80 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-emulated-tls")
81 }
Stephen Craneba090d12017-05-09 15:44:35 -070082 flags.ArFlags = append(flags.ArFlags, " --plugin ${config.LLVMGoldPlugin}")
83 }
84 return flags
85}
86
87// Can be called with a null receiver
88func (lto *lto) LTO() bool {
89 if lto == nil {
90 return false
91 }
92
Yi Kong244bf072017-08-29 11:10:09 +080093 full := Bool(lto.Properties.Lto.Full)
94 thin := Bool(lto.Properties.Lto.Thin)
95 return full || thin
Stephen Craneba090d12017-05-09 15:44:35 -070096}
97
98// Propagate lto requirements down from binaries
99func ltoDepsMutator(mctx android.TopDownMutatorContext) {
100 if c, ok := mctx.Module().(*Module); ok && c.lto.LTO() {
Yi Kong244bf072017-08-29 11:10:09 +0800101 full := Bool(c.lto.Properties.Lto.Full)
102 thin := Bool(c.lto.Properties.Lto.Thin)
103 if full && thin {
104 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
105 }
106
Stephen Craneba090d12017-05-09 15:44:35 -0700107 mctx.VisitDepsDepthFirst(func(m blueprint.Module) {
108 tag := mctx.OtherModuleDependencyTag(m)
109 switch tag {
110 case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag, objDepTag, reuseObjTag:
111 if cc, ok := m.(*Module); ok && cc.lto != nil {
112 cc.lto.Properties.LTODep = true
113 }
114 }
115 })
116 }
117}
118
119// Create lto variants for modules that need them
120func ltoMutator(mctx android.BottomUpMutatorContext) {
121 if c, ok := mctx.Module().(*Module); ok && c.lto != nil {
122 if c.lto.LTO() {
123 mctx.SetDependencyVariation("lto")
124 } else if c.lto.Properties.LTODep {
125 modules := mctx.CreateVariations("", "lto")
Yi Kong244bf072017-08-29 11:10:09 +0800126 modules[0].(*Module).lto.Properties.Lto.Full = boolPtr(false)
127 modules[0].(*Module).lto.Properties.Lto.Thin = boolPtr(false)
Stephen Craneba090d12017-05-09 15:44:35 -0700128 modules[0].(*Module).lto.Properties.LTODep = false
129 modules[1].(*Module).lto.Properties.LTODep = false
130 modules[1].(*Module).Properties.PreventInstall = true
131 modules[1].(*Module).Properties.HideFromMake = true
132 }
133 c.lto.Properties.LTODep = false
134 }
135}