blob: 38fc938f76d069537fbd1ffda2f593a415c40956 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 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"
Colin Crossb916a382016-07-29 17:28:03 -070019 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020
21 "android/soong"
22 "android/soong/android"
23)
24
25type BinaryLinkerProperties struct {
26 // compile executable with -static
27 Static_executable *bool `android:"arch_variant"`
28
29 // set the name of the output
30 Stem string `android:"arch_variant"`
31
32 // append to the name of the output
33 Suffix string `android:"arch_variant"`
34
35 // if set, add an extra objcopy --prefix-symbols= step
36 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070037
38 // if set, install a symlink to the preferred architecture
39 Symlink_preferred_arch bool
Colin Cross4d9c2d12016-07-29 12:48:20 -070040}
41
42func init() {
43 soong.RegisterModuleType("cc_binary", binaryFactory)
44 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
45}
46
47// Module factory for binaries
48func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070049 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 return module.Init()
51}
52
53// Module factory for host binaries
54func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070055 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070056 return module.Init()
57}
58
59//
60// Executables
61//
62
Colin Crossb916a382016-07-29 17:28:03 -070063type binaryDecorator struct {
64 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070065 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 stripper
67
68 Properties BinaryLinkerProperties
69
70 hostToolPath android.OptionalPath
71}
72
Colin Crossb916a382016-07-29 17:28:03 -070073var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070074
Colin Crossb916a382016-07-29 17:28:03 -070075func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070076 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 &binary.Properties,
78 &binary.stripper.StripProperties)
79
80}
81
Colin Crossb916a382016-07-29 17:28:03 -070082func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 stem := ctx.ModuleName()
84 if binary.Properties.Stem != "" {
85 stem = binary.Properties.Stem
86 }
87
88 return stem + binary.Properties.Suffix
89}
90
Colin Crossb916a382016-07-29 17:28:03 -070091func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070092 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 if ctx.Device() {
94 if !Bool(binary.baseLinker.Properties.Nocrt) {
95 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070096 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 deps.CrtBegin = "crtbegin_static"
98 } else {
99 deps.CrtBegin = "crtbegin_dynamic"
100 }
101 deps.CrtEnd = "crtend_android"
102 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700103 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
105 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700106 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
108 } else {
109 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
110 }
111 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
112 }
113 }
114 }
115
Colin Crossb916a382016-07-29 17:28:03 -0700116 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 if inList("libc++_static", deps.StaticLibs) {
118 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
119 }
120 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
121 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
122 // move them to the beginning of deps.LateStaticLibs
123 var groupLibs []string
124 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
125 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
126 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
127 }
128 }
129
Colin Crossb916a382016-07-29 17:28:03 -0700130 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
132 "from static libs or set static_executable: true")
133 }
134 return deps
135}
136
Colin Crossb916a382016-07-29 17:28:03 -0700137func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 return true
139}
140
Colin Crossb916a382016-07-29 17:28:03 -0700141func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700143 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700144 baseLinker: NewBaseLinker(),
145 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146 }
Colin Crossb916a382016-07-29 17:28:03 -0700147 module.compiler = NewBaseCompiler()
148 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700149 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700150 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151}
152
Colin Crossb916a382016-07-29 17:28:03 -0700153func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700154 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 if ctx.Host() {
157 if ctx.Os() == android.Linux {
158 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700159 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 }
161 } else {
162 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700163 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 }
165 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700166
167 if binary.Properties.Symlink_preferred_arch {
168 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
169 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
170 }
171 var prefer bool
172 if ctx.Host() {
173 prefer = ctx.AConfig().HostPrefer32BitExecutables()
174 } else {
175 prefer = ctx.AConfig().DevicePrefer32BitExecutables()
176 }
177 if ctx.PrimaryArch() != prefer {
178 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
179 ctx.ModuleName())
180 }
181 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184func (binary *binaryDecorator) static() bool {
185 return Bool(binary.Properties.Static_executable)
186}
187
188func (binary *binaryDecorator) staticBinary() bool {
189 return binary.static()
190}
191
192func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700193 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194
Colin Crossb916a382016-07-29 17:28:03 -0700195 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 flags.LdFlags = append(flags.LdFlags, "-pie")
197 if ctx.Os() == android.Windows {
198 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
199 }
200 }
201
202 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
203 // all code is position independent, and then those warnings get promoted to
204 // errors.
205 if ctx.Os() != android.Windows {
206 flags.CFlags = append(flags.CFlags, "-fpie")
207 }
208
209 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700210 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 // Clang driver needs -static to create static executable.
212 // However, bionic/linker uses -shared to overwrite.
213 // Linker for x86 targets does not allow coexistance of -static and -shared,
214 // so we add -static only if -shared is not used.
215 if !inList("-shared", flags.LdFlags) {
216 flags.LdFlags = append(flags.LdFlags, "-static")
217 }
218
219 flags.LdFlags = append(flags.LdFlags,
220 "-nostdlib",
221 "-Bstatic",
222 "-Wl,--gc-sections",
223 )
224
225 } else {
226 if flags.DynamicLinker == "" {
227 flags.DynamicLinker = "/system/bin/linker"
228 if flags.Toolchain.Is64Bit() {
229 flags.DynamicLinker += "64"
230 }
231 }
232
233 flags.LdFlags = append(flags.LdFlags,
234 "-pie",
235 "-nostdlib",
236 "-Bdynamic",
237 "-Wl,--gc-sections",
238 "-Wl,-z,nocopyreloc",
239 )
240 }
241 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700242 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 flags.LdFlags = append(flags.LdFlags, "-static")
244 }
245 if ctx.Darwin() {
246 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
247 }
248 }
249
250 return flags
251}
252
Colin Crossb916a382016-07-29 17:28:03 -0700253func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
255
256 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
257 outputFile := android.PathForModuleOut(ctx, fileName)
258 ret := outputFile
259 if ctx.Os().Class == android.Host {
260 binary.hostToolPath = android.OptionalPathForPath(outputFile)
261 }
262
263 var linkerDeps android.Paths
264
265 sharedLibs := deps.SharedLibs
266 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
267
268 if flags.DynamicLinker != "" {
269 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
270 }
271
272 builderFlags := flagsToBuilderFlags(flags)
273
274 if binary.stripper.needsStrip(ctx) {
275 strippedOutputFile := outputFile
276 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
277 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
278 }
279
280 if binary.Properties.Prefix_symbols != "" {
281 afterPrefixSymbols := outputFile
282 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
283 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
284 flagsToBuilderFlags(flags), afterPrefixSymbols)
285 }
286
287 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
288 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
289 builderFlags, outputFile)
290
291 return ret
292}
293
Colin Crossb916a382016-07-29 17:28:03 -0700294func (binary *binaryDecorator) HostToolPath() android.OptionalPath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 return binary.hostToolPath
296}