blob: 4757fc7e839b1fc5a8db368926ba9a9243b02920 [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 (
Stephen Craneba090d12017-05-09 15:44:35 -070018 "android/soong/android"
19)
20
21// LTO (link-time optimization) allows the compiler to optimize and generate
22// code for the entire module at link time, rather than per-compilation
23// unit. LTO is required for Clang CFI and other whole-program optimization
24// techniques. LTO also allows cross-compilation unit optimizations that should
25// result in faster and smaller code, at the expense of additional compilation
26// time.
27//
28// To properly build a module with LTO, the module and all recursive static
29// dependencies should be compiled with -flto which directs the compiler to emit
30// bitcode rather than native object files. These bitcode files are then passed
31// by the linker to the LLVM plugin for compilation at link time. Static
32// dependencies not built as bitcode will still function correctly but cannot be
33// optimized at link time and may not be compatible with features that require
34// LTO, such as CFI.
35//
36// This file adds support to soong to automatically propogate LTO options to a
37// new variant of all static dependencies for each module with LTO enabled.
38
39type LTOProperties struct {
40 // Lto must violate capitialization style for acronyms so that it can be
41 // referred to in blueprint files as "lto"
Yi Kong244bf072017-08-29 11:10:09 +080042 Lto struct {
Stephen Crane10cd1872017-09-27 17:01:15 -070043 Never *bool `android:"arch_variant"`
44 Full *bool `android:"arch_variant"`
45 Thin *bool `android:"arch_variant"`
Yi Kong244bf072017-08-29 11:10:09 +080046 } `android:"arch_variant"`
Stephen Crane10cd1872017-09-27 17:01:15 -070047
48 // Dep properties indicate that this module needs to be built with LTO
49 // since it is an object dependency of an LTO module.
50 FullDep bool `blueprint:"mutated"`
51 ThinDep bool `blueprint:"mutated"`
Stephen Craneba090d12017-05-09 15:44:35 -070052}
53
54type lto struct {
55 Properties LTOProperties
56}
57
58func (lto *lto) props() []interface{} {
59 return []interface{}{&lto.Properties}
60}
61
62func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080063 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
64 lto.Properties.Lto.Never = boolPtr(true)
65 }
Stephen Craneba090d12017-05-09 15:44:35 -070066}
67
68func (lto *lto) deps(ctx BaseModuleContext, deps Deps) Deps {
69 return deps
70}
71
72func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong244bf072017-08-29 11:10:09 +080073 if lto.LTO() {
74 var ltoFlag string
75 if Bool(lto.Properties.Lto.Thin) {
76 ltoFlag = "-flto=thin"
77 } else {
78 ltoFlag = "-flto"
79 }
80
81 flags.CFlags = append(flags.CFlags, ltoFlag)
82 flags.LdFlags = append(flags.LdFlags, ltoFlag)
Stephen Cranef5b9b952017-06-30 19:03:36 -070083 if ctx.Device() {
84 // Work around bug in Clang that doesn't pass correct emulated
Ryan Prichard3ed1f702018-02-05 23:45:16 -080085 // TLS option to target. See b/72706604 or
86 // https://github.com/android-ndk/ndk/issues/498.
Stephen Cranef5b9b952017-06-30 19:03:36 -070087 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-emulated-tls")
88 }
Zhizhou Yang51be6322018-02-08 18:32:11 -080089 flags.ArGoldPlugin = true
Stephen Craneba090d12017-05-09 15:44:35 -070090 }
91 return flags
92}
93
94// Can be called with a null receiver
95func (lto *lto) LTO() bool {
Stephen Crane10cd1872017-09-27 17:01:15 -070096 if lto == nil || lto.Disabled() {
Stephen Craneba090d12017-05-09 15:44:35 -070097 return false
98 }
99
Yi Kong244bf072017-08-29 11:10:09 +0800100 full := Bool(lto.Properties.Lto.Full)
101 thin := Bool(lto.Properties.Lto.Thin)
102 return full || thin
Stephen Craneba090d12017-05-09 15:44:35 -0700103}
104
Stephen Crane10cd1872017-09-27 17:01:15 -0700105// Is lto.never explicitly set to true?
106func (lto *lto) Disabled() bool {
107 return lto.Properties.Lto.Never != nil && *lto.Properties.Lto.Never
108}
109
Stephen Craneba090d12017-05-09 15:44:35 -0700110// Propagate lto requirements down from binaries
111func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700112 if m, ok := mctx.Module().(*Module); ok && m.lto.LTO() {
113 full := Bool(m.lto.Properties.Lto.Full)
114 thin := Bool(m.lto.Properties.Lto.Thin)
Yi Kong244bf072017-08-29 11:10:09 +0800115 if full && thin {
116 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
117 }
118
Stephen Crane10cd1872017-09-27 17:01:15 -0700119 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
120 tag := mctx.OtherModuleDependencyTag(dep)
Stephen Craneba090d12017-05-09 15:44:35 -0700121 switch tag {
122 case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag, objDepTag, reuseObjTag:
Stephen Crane10cd1872017-09-27 17:01:15 -0700123 if dep, ok := dep.(*Module); ok && dep.lto != nil &&
124 !dep.lto.Disabled() {
125 if full && !Bool(dep.lto.Properties.Lto.Full) {
126 dep.lto.Properties.FullDep = true
127 }
128 if thin && !Bool(dep.lto.Properties.Lto.Thin) {
129 dep.lto.Properties.ThinDep = true
130 }
Stephen Craneba090d12017-05-09 15:44:35 -0700131 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700132
133 // Recursively walk static dependencies
134 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700135 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700136
137 // Do not recurse down non-static dependencies
138 return false
Stephen Craneba090d12017-05-09 15:44:35 -0700139 })
140 }
141}
142
143// Create lto variants for modules that need them
144func ltoMutator(mctx android.BottomUpMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700145 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
146 // Create variations for LTO types required as static
147 // dependencies
148 variationNames := []string{""}
149 if m.lto.Properties.FullDep && !Bool(m.lto.Properties.Lto.Full) {
150 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700151 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700152 if m.lto.Properties.ThinDep && !Bool(m.lto.Properties.Lto.Thin) {
153 variationNames = append(variationNames, "lto-thin")
154 }
155
156 // Use correct dependencies if LTO property is explicitly set
157 // (mutually exclusive)
158 if Bool(m.lto.Properties.Lto.Full) {
159 mctx.SetDependencyVariation("lto-full")
160 }
161 if Bool(m.lto.Properties.Lto.Thin) {
162 mctx.SetDependencyVariation("lto-thin")
163 }
164
165 if len(variationNames) > 1 {
166 modules := mctx.CreateVariations(variationNames...)
167 for i, name := range variationNames {
168 variation := modules[i].(*Module)
169 // Default module which will be
170 // installed. Variation set above according to
171 // explicit LTO properties
172 if name == "" {
173 continue
174 }
175
176 // LTO properties for dependencies
177 if name == "lto-full" {
178 variation.lto.Properties.Lto.Full = boolPtr(true)
179 variation.lto.Properties.Lto.Thin = boolPtr(false)
180 }
181 if name == "lto-thin" {
182 variation.lto.Properties.Lto.Full = boolPtr(false)
183 variation.lto.Properties.Lto.Thin = boolPtr(true)
184 }
185 variation.Properties.PreventInstall = true
186 variation.Properties.HideFromMake = true
187 variation.lto.Properties.FullDep = false
188 variation.lto.Properties.ThinDep = false
189 }
190 }
Stephen Craneba090d12017-05-09 15:44:35 -0700191 }
192}