blob: 1634a83abe820565fea6f2dfae2a42041eaaa8da [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
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "android/soong/android"
22)
23
24type BinaryLinkerProperties struct {
25 // compile executable with -static
26 Static_executable *bool `android:"arch_variant"`
27
28 // set the name of the output
29 Stem string `android:"arch_variant"`
30
31 // append to the name of the output
32 Suffix string `android:"arch_variant"`
33
34 // if set, add an extra objcopy --prefix-symbols= step
35 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070036
37 // if set, install a symlink to the preferred architecture
38 Symlink_preferred_arch bool
Colin Cross522e3732016-09-07 13:14:06 -070039
Colin Cross9b09f242016-12-07 13:37:42 -080040 // install symlinks to the binary. Symlink names will have the suffix and the binary
41 // extension (if any) appended
42 Symlinks []string `android:"arch_variant"`
43
Colin Cross522e3732016-09-07 13:14:06 -070044 DynamicLinker string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070045}
46
47func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070048 android.RegisterModuleType("cc_binary", binaryFactory)
49 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070050}
51
52// Module factory for binaries
53func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070054 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070055 return module.Init()
56}
57
58// Module factory for host binaries
59func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070060 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070061 return module.Init()
62}
63
64//
65// Executables
66//
67
Colin Crossb916a382016-07-29 17:28:03 -070068type binaryDecorator struct {
69 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070070 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 stripper
72
73 Properties BinaryLinkerProperties
74
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070075 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080076
77 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
78 symlinks []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070079}
80
Colin Crossb916a382016-07-29 17:28:03 -070081var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070082
Colin Crossb916a382016-07-29 17:28:03 -070083func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070084 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 &binary.Properties,
86 &binary.stripper.StripProperties)
87
88}
89
Colin Crossb916a382016-07-29 17:28:03 -070090func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070091 stem := ctx.baseModuleName()
Colin Cross4d9c2d12016-07-29 12:48:20 -070092 if binary.Properties.Stem != "" {
93 stem = binary.Properties.Stem
94 }
95
96 return stem + binary.Properties.Suffix
97}
98
Colin Crossb916a382016-07-29 17:28:03 -070099func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700100 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800101 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 if !Bool(binary.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800103 if !ctx.sdk() && !ctx.vndk() {
Colin Crossb916a382016-07-29 17:28:03 -0700104 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 deps.CrtBegin = "crtbegin_static"
106 } else {
107 deps.CrtBegin = "crtbegin_dynamic"
108 }
109 deps.CrtEnd = "crtend_android"
110 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800111 // TODO(danalbert): Add generation of crt objects.
112 // For `sdk_version: "current"`, we don't actually have a
113 // freshly generated set of CRT objects. Use the last stable
114 // version.
115 version := ctx.sdkVersion()
116 if version == "current" {
117 version = ctx.AConfig().PlatformSdkVersion()
118 }
119
Colin Crossb916a382016-07-29 17:28:03 -0700120 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800121 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700123 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800124 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800126 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 }
Dan Albertebedf672016-11-08 15:06:22 -0800128 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 }
130 }
131 }
132
Colin Crossb916a382016-07-29 17:28:03 -0700133 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 if inList("libc++_static", deps.StaticLibs) {
135 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
136 }
137 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
138 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
139 // move them to the beginning of deps.LateStaticLibs
140 var groupLibs []string
141 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
142 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
143 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
144 }
145 }
146
Colin Crossb916a382016-07-29 17:28:03 -0700147 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
149 "from static libs or set static_executable: true")
150 }
151 return deps
152}
153
Colin Crossb916a382016-07-29 17:28:03 -0700154func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 return true
156}
157
Colin Crossb916a382016-07-29 17:28:03 -0700158func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700160 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700161 baseLinker: NewBaseLinker(),
162 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 }
Colin Crossb916a382016-07-29 17:28:03 -0700164 module.compiler = NewBaseCompiler()
165 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700166 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700167 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168}
169
Colin Crossb916a382016-07-29 17:28:03 -0700170func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700171 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172
Dan Willemsen2e47b342016-11-17 01:02:25 -0800173 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 if ctx.Os() == android.Linux {
175 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700176 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 }
178 } else {
179 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700180 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 }
182 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183}
184
Colin Crossb916a382016-07-29 17:28:03 -0700185func (binary *binaryDecorator) static() bool {
186 return Bool(binary.Properties.Static_executable)
187}
188
189func (binary *binaryDecorator) staticBinary() bool {
190 return binary.static()
191}
192
193func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700194 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195
Colin Crossb916a382016-07-29 17:28:03 -0700196 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 flags.LdFlags = append(flags.LdFlags, "-pie")
198 if ctx.Os() == android.Windows {
199 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
200 }
201 }
202
203 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
204 // all code is position independent, and then those warnings get promoted to
205 // errors.
206 if ctx.Os() != android.Windows {
207 flags.CFlags = append(flags.CFlags, "-fpie")
208 }
209
Dan Willemsen2e47b342016-11-17 01:02:25 -0800210 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700211 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 // Clang driver needs -static to create static executable.
213 // However, bionic/linker uses -shared to overwrite.
214 // Linker for x86 targets does not allow coexistance of -static and -shared,
215 // so we add -static only if -shared is not used.
216 if !inList("-shared", flags.LdFlags) {
217 flags.LdFlags = append(flags.LdFlags, "-static")
218 }
219
220 flags.LdFlags = append(flags.LdFlags,
221 "-nostdlib",
222 "-Bstatic",
223 "-Wl,--gc-sections",
224 )
225
226 } else {
227 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700228 if binary.Properties.DynamicLinker != "" {
229 flags.DynamicLinker = binary.Properties.DynamicLinker
230 } else {
231 flags.DynamicLinker = "/system/bin/linker"
232 if flags.Toolchain.Is64Bit() {
233 flags.DynamicLinker += "64"
234 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 }
236 }
237
238 flags.LdFlags = append(flags.LdFlags,
239 "-pie",
240 "-nostdlib",
241 "-Bdynamic",
242 "-Wl,--gc-sections",
243 "-Wl,-z,nocopyreloc",
244 )
245 }
246 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700247 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 flags.LdFlags = append(flags.LdFlags, "-static")
249 }
250 if ctx.Darwin() {
251 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
252 }
253 }
254
255 return flags
256}
257
Colin Crossb916a382016-07-29 17:28:03 -0700258func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700259 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260
261 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
262 outputFile := android.PathForModuleOut(ctx, fileName)
263 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264
265 var linkerDeps android.Paths
266
267 sharedLibs := deps.SharedLibs
268 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
269
270 if flags.DynamicLinker != "" {
271 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
272 }
273
274 builderFlags := flagsToBuilderFlags(flags)
275
276 if binary.stripper.needsStrip(ctx) {
277 strippedOutputFile := outputFile
278 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
279 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
280 }
281
282 if binary.Properties.Prefix_symbols != "" {
283 afterPrefixSymbols := outputFile
284 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
285 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
286 flagsToBuilderFlags(flags), afterPrefixSymbols)
287 }
288
Colin Cross26c34ed2016-09-30 17:10:16 -0700289 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
290 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700291 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700292
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700293 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
295 builderFlags, outputFile)
296
297 return ret
298}
299
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700300func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
301 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800302 for _, symlink := range binary.Properties.Symlinks {
303 binary.symlinks = append(binary.symlinks,
304 symlink+binary.Properties.Suffix+binary.baseInstaller.path.Ext())
305 }
306
307 if binary.Properties.Symlink_preferred_arch {
308 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
309 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
310 }
311 if ctx.TargetPrimary() {
312 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
313 }
314 }
315
316 for _, symlink := range binary.symlinks {
317 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
318 }
319
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700320 if ctx.Os().Class == android.Host {
321 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
322 }
323}
324
325func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
326 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327}