blob: b27142cf7f3eb88571466ae6877c2620390c4618 [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 (
Jiyong Parkf1194352019-02-25 11:05:47 +090018 "path/filepath"
19
Dan Willemsena0790e32018-10-12 00:24:23 -070020 "github.com/google/blueprint"
21
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "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
Nan Zhang0007d812017-11-07 10:57:05 -080030 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070031
32 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080033 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
35 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080036 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070037
38 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010039 Symlink_preferred_arch *bool `android:"arch_variant"`
Colin Cross522e3732016-09-07 13:14:06 -070040
Colin Cross9b09f242016-12-07 13:37:42 -080041 // install symlinks to the binary. Symlink names will have the suffix and the binary
42 // extension (if any) appended
43 Symlinks []string `android:"arch_variant"`
44
Colin Cross522e3732016-09-07 13:14:06 -070045 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070046
47 // Names of modules to be overridden. Listed modules can only be other binaries
48 // (in Make or Soong).
49 // This does not completely prevent installation of the overridden binaries, but if both
50 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
51 // from PRODUCT_PACKAGES.
52 Overrides []string
Colin Crossd7227f92019-09-05 14:26:33 -070053
54 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
55 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070056}
57
58func init() {
Jiyong Park16e91a02018-12-20 18:18:08 +090059 android.RegisterModuleType("cc_binary", BinaryFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070060 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070061}
62
Patrice Arrudac249c712019-03-19 17:00:29 -070063// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090064func BinaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070065 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 return module.Init()
67}
68
Patrice Arrudac249c712019-03-19 17:00:29 -070069// cc_binary_host produces a binary that is runnable on a host.
Colin Cross36242852017-06-23 15:06:31 -070070func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070071 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070072 return module.Init()
73}
74
75//
76// Executables
77//
78
Colin Crossb916a382016-07-29 17:28:03 -070079type binaryDecorator struct {
80 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070081 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070082 stripper
83
84 Properties BinaryLinkerProperties
85
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070086 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080087
Colin Crossb60190a2018-09-04 16:28:17 -070088 // Location of the linked, unstripped binary
89 unstrippedOutputFile android.Path
90
Colin Cross9b09f242016-12-07 13:37:42 -080091 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
92 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080093
94 // Output archive of gcno coverage information
95 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -080096
97 // Location of the file that should be copied to dist dir when requested
98 distFile android.OptionalPath
Jiyong Parkf1194352019-02-25 11:05:47 +090099
100 post_install_cmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101}
102
Colin Crossb916a382016-07-29 17:28:03 -0700103var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104
Colin Crossb916a382016-07-29 17:28:03 -0700105func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700106 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 &binary.Properties,
108 &binary.stripper.StripProperties)
109
110}
111
Roland Levillain9efb8c72019-06-05 13:31:31 +0100112func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700113 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800114 if String(binary.Properties.Stem) != "" {
115 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 }
117
Roland Levillain9efb8c72019-06-05 13:31:31 +0100118 return stem
119}
120
121func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
122 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123}
124
Colin Cross37047f12016-12-13 17:06:13 -0800125func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700126 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800127 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 if !Bool(binary.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700129 if !ctx.useSdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700130 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 deps.CrtBegin = "crtbegin_static"
132 } else {
133 deps.CrtBegin = "crtbegin_dynamic"
134 }
135 deps.CrtEnd = "crtend_android"
136 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800137 // TODO(danalbert): Add generation of crt objects.
138 // For `sdk_version: "current"`, we don't actually have a
139 // freshly generated set of CRT objects. Use the last stable
140 // version.
141 version := ctx.sdkVersion()
142 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700143 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800144 }
145
Colin Crossb916a382016-07-29 17:28:03 -0700146 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800147 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700149 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800150 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800152 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 }
Dan Albertebedf672016-11-08 15:06:22 -0800154 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
156 }
157 }
158
Colin Crossb916a382016-07-29 17:28:03 -0700159 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800160 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700161 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
164 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
165 // move them to the beginning of deps.LateStaticLibs
166 var groupLibs []string
167 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
168 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
169 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
170 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700171
172 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700173 deps.DynamicLinker = "linker"
174 deps.LinkerFlagsFile = "host_bionic_linker_flags"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700175 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 }
177
Colin Crossb916a382016-07-29 17:28:03 -0700178 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
180 "from static libs or set static_executable: true")
181 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800182
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183 return deps
184}
185
Colin Crossb916a382016-07-29 17:28:03 -0700186func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 return true
188}
189
Colin Crossb916a382016-07-29 17:28:03 -0700190func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700192 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700193 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700194 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 }
Colin Crossb916a382016-07-29 17:28:03 -0700196 module.compiler = NewBaseCompiler()
197 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700198 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700199 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200}
201
Colin Crossb916a382016-07-29 17:28:03 -0700202func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700203 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204
Dan Willemsen2e47b342016-11-17 01:02:25 -0800205 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 if ctx.Os() == android.Linux {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700207 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800208 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800210 } else if !ctx.Fuchsia() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700212 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 }
214 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215}
216
Colin Crossb916a382016-07-29 17:28:03 -0700217func (binary *binaryDecorator) static() bool {
218 return Bool(binary.Properties.Static_executable)
219}
220
221func (binary *binaryDecorator) staticBinary() bool {
222 return binary.static()
223}
224
225func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700226 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
Colin Cross446c6662018-09-14 16:00:16 -0700228 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800229 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross7a108bc2017-01-30 22:44:19 -0800230 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 }
232 }
233
234 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
235 // all code is position independent, and then those warnings get promoted to
236 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700237 if !ctx.Windows() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700238 flags.CFlags = append(flags.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 }
240
Dan Willemsen2e47b342016-11-17 01:02:25 -0800241 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700242 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 // Clang driver needs -static to create static executable.
244 // However, bionic/linker uses -shared to overwrite.
245 // Linker for x86 targets does not allow coexistance of -static and -shared,
246 // so we add -static only if -shared is not used.
247 if !inList("-shared", flags.LdFlags) {
248 flags.LdFlags = append(flags.LdFlags, "-static")
249 }
250
251 flags.LdFlags = append(flags.LdFlags,
252 "-nostdlib",
253 "-Bstatic",
254 "-Wl,--gc-sections",
255 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 } else {
257 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700258 if binary.Properties.DynamicLinker != "" {
259 flags.DynamicLinker = binary.Properties.DynamicLinker
260 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700261 switch ctx.Os() {
262 case android.Android:
Jiyong Parkf1194352019-02-25 11:05:47 +0900263 if ctx.bootstrap() && !ctx.inRecovery() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900264 flags.DynamicLinker = "/system/bin/bootstrap/linker"
265 } else {
266 flags.DynamicLinker = "/system/bin/linker"
267 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700268 if flags.Toolchain.Is64Bit() {
269 flags.DynamicLinker += "64"
270 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700271 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700272 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700273 default:
274 ctx.ModuleErrorf("unknown dynamic linker")
275 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700276 }
277
278 if ctx.Os() == android.LinuxBionic {
279 // Use the dlwrap entry point, but keep _start around so
280 // that it can be used by host_bionic_inject
281 flags.LdFlags = append(flags.LdFlags,
282 "-Wl,--entry=__dlwrap__start",
283 "-Wl,--undefined=_start",
284 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 }
286 }
287
288 flags.LdFlags = append(flags.LdFlags,
289 "-pie",
290 "-nostdlib",
291 "-Bdynamic",
292 "-Wl,--gc-sections",
293 "-Wl,-z,nocopyreloc",
294 )
295 }
296 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700297 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 flags.LdFlags = append(flags.LdFlags, "-static")
299 }
300 if ctx.Darwin() {
301 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
302 }
303 }
304
305 return flags
306}
307
Colin Crossb916a382016-07-29 17:28:03 -0700308func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700309 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310
311 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
312 outputFile := android.PathForModuleOut(ctx, fileName)
313 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314
315 var linkerDeps android.Paths
316
Dan Willemsena0790e32018-10-12 00:24:23 -0700317 if deps.LinkerFlagsFile.Valid() {
318 flags.LdFlags = append(flags.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")")
319 linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path())
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700320 }
321
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 if flags.DynamicLinker != "" {
Dan Willemsena0790e32018-10-12 00:24:23 -0700323 flags.LdFlags = append(flags.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
324 } else if ctx.toolchain().Bionic() && !binary.static() {
325 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700326 }
327
328 builderFlags := flagsToBuilderFlags(flags)
329
330 if binary.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800331 if ctx.Darwin() {
332 builderFlags.stripUseGnuStrip = true
333 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 strippedOutputFile := outputFile
335 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700336 binary.stripper.stripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, builderFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 }
338
Colin Crossb60190a2018-09-04 16:28:17 -0700339 binary.unstrippedOutputFile = outputFile
340
Nan Zhang0007d812017-11-07 10:57:05 -0800341 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 afterPrefixSymbols := outputFile
343 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800344 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 flagsToBuilderFlags(flags), afterPrefixSymbols)
346 }
347
Colin Crossd7227f92019-09-05 14:26:33 -0700348 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
349
Dan Willemsen569edc52018-11-19 09:33:29 -0800350 if Bool(binary.baseLinker.Properties.Use_version_lib) {
351 if ctx.Host() {
352 versionedOutputFile := outputFile
353 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
354 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
355 } else {
356 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
357 binary.distFile = android.OptionalPathForPath(versionedOutputFile)
358
359 if binary.stripper.needsStrip(ctx) {
360 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
361 binary.distFile = android.OptionalPathForPath(out)
Ryan Prichardf979d732019-05-30 20:53:29 -0700362 binary.stripper.stripExecutableOrSharedLib(ctx, versionedOutputFile, out, builderFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800363 }
364
365 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
366 }
Colin Cross86803cf2018-02-15 14:12:26 -0800367 }
368
Dan Willemsena0790e32018-10-12 00:24:23 -0700369 if ctx.Os() == android.LinuxBionic && !binary.static() {
370 injectedOutputFile := outputFile
371 outputFile = android.PathForModuleOut(ctx, "prelinker", fileName)
372
373 if !deps.DynamicLinker.Valid() {
374 panic("Non-static host bionic modules must have a dynamic linker")
375 }
376
377 binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile)
378 }
379
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800380 var sharedLibs android.Paths
381 // Ignore shared libs for static executables.
382 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900383 sharedLibs = deps.EarlySharedLibs
384 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800385 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900386 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800387 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
388 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
389 }
390
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700391 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700392 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700393
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700394 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700395 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Josh Gao75a50a22019-06-07 17:58:59 -0700396 builderFlags, outputFile, nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397
Dan Willemsen581341d2017-02-09 16:16:31 -0800398 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
399 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Oliver Nguyenc7434142019-04-24 14:22:25 -0700400 binary.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800401
Alex Light3d673592019-01-18 14:37:31 -0800402 // Need to determine symlinks early since some targets (ie APEX) need this
403 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800404 for _, symlink := range binary.Properties.Symlinks {
405 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800406 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800407 }
408
Nan Zhang0007d812017-11-07 10:57:05 -0800409 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100410 if String(binary.Properties.Suffix) == "" {
411 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800412 }
413 if ctx.TargetPrimary() {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100414 binary.symlinks = append(binary.symlinks, binary.getStemWithoutSuffix(ctx))
Colin Cross9b09f242016-12-07 13:37:42 -0800415 }
416 }
417
Alex Light3d673592019-01-18 14:37:31 -0800418 return ret
419}
420
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900421func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
422 return binary.unstrippedOutputFile
423}
424
Alex Light3d673592019-01-18 14:37:31 -0800425func (binary *binaryDecorator) symlinkList() []string {
426 return binary.symlinks
427}
428
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700429func (binary *binaryDecorator) nativeCoverage() bool {
430 return true
431}
432
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900433func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
434 return binary.coverageOutputFile
435}
436
Jiyong Parkf1194352019-02-25 11:05:47 +0900437// /system/bin/linker -> /apex/com.android.runtime/bin/linker
438func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
439 dir := binary.baseInstaller.installDir(ctx)
440 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
441 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
442
443 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
444 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
445
446 for _, symlink := range binary.symlinks {
447 ctx.InstallAbsoluteSymlink(dir, symlink, target)
448 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target))
449 }
450}
451
Alex Light3d673592019-01-18 14:37:31 -0800452func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900453 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
454 // The original path becomes a symlink to the corresponding file in the
455 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700456 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Martin Stjernholm279de572019-09-10 23:18:20 +0100457 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !translatedArch && ctx.apexName() == "" && !ctx.inRecovery() {
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700458 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900459 binary.installSymlinkToRuntimeApex(ctx, file)
460 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900461 binary.baseInstaller.subDir = "bootstrap"
462 }
Alex Light3d673592019-01-18 14:37:31 -0800463 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800464 for _, symlink := range binary.symlinks {
465 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
466 }
467
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700468 if ctx.Os().Class == android.Host {
469 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
470 }
471}
472
473func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
474 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700475}
Dan Willemsena0790e32018-10-12 00:24:23 -0700476
477func init() {
478 pctx.HostBinToolVariable("hostBionicSymbolsInjectCmd", "host_bionic_inject")
479}
480
481var injectHostBionicSymbols = pctx.AndroidStaticRule("injectHostBionicSymbols",
482 blueprint.RuleParams{
483 Command: "$hostBionicSymbolsInjectCmd -i $in -l $linker -o $out",
484 CommandDeps: []string{"$hostBionicSymbolsInjectCmd"},
485 }, "linker")
486
487func (binary *binaryDecorator) injectHostBionicLinkerSymbols(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
488 ctx.Build(pctx, android.BuildParams{
489 Rule: injectHostBionicSymbols,
490 Description: "inject host bionic symbols",
491 Input: in,
492 Implicit: linker,
493 Output: out,
494 Args: map[string]string{
495 "linker": linker.String(),
496 },
497 })
498}