blob: da29412f49678f22d57bf52b7f2746dfe11b42e1 [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() {
Paul Duffin2ee69792020-01-16 12:14:42 +000059 RegisterBinaryBuildComponents(android.InitRegistrationContext)
60}
61
62func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
63 ctx.RegisterModuleType("cc_binary", BinaryFactory)
64 ctx.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070065}
66
Patrice Arrudac249c712019-03-19 17:00:29 -070067// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090068func BinaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070069 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070070 return module.Init()
71}
72
Patrice Arrudac249c712019-03-19 17:00:29 -070073// cc_binary_host produces a binary that is runnable on a host.
Colin Cross36242852017-06-23 15:06:31 -070074func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070075 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 return module.Init()
77}
78
79//
80// Executables
81//
82
Colin Crossb916a382016-07-29 17:28:03 -070083type binaryDecorator struct {
84 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070085 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020086 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070087
88 Properties BinaryLinkerProperties
89
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070090 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080091
Colin Crossb60190a2018-09-04 16:28:17 -070092 // Location of the linked, unstripped binary
93 unstrippedOutputFile android.Path
94
Colin Cross9b09f242016-12-07 13:37:42 -080095 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
96 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080097
Colin Crossa6159012020-11-12 12:14:36 -080098 // If the module has symlink_preferred_arch set, the name of the symlink to the
99 // binary for the preferred arch.
100 preferredArchSymlink string
101
Dan Willemsen581341d2017-02-09 16:16:31 -0800102 // Output archive of gcno coverage information
103 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800104
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000105 // Location of the files that should be copied to dist dir when requested
106 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900107
108 post_install_cmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109}
110
Colin Crossb916a382016-07-29 17:28:03 -0700111var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112
Colin Crossb916a382016-07-29 17:28:03 -0700113func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700114 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 &binary.Properties,
116 &binary.stripper.StripProperties)
117
118}
119
Roland Levillain9efb8c72019-06-05 13:31:31 +0100120func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700121 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800122 if String(binary.Properties.Stem) != "" {
123 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 }
125
Roland Levillain9efb8c72019-06-05 13:31:31 +0100126 return stem
127}
128
129func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
130 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
Colin Cross37047f12016-12-13 17:06:13 -0800133func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700134 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800135 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 if !Bool(binary.baseLinker.Properties.Nocrt) {
Dan Albert92fe7402020-07-15 13:33:30 -0700137 if binary.static() {
138 deps.CrtBegin = "crtbegin_static"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 } else {
Dan Albert92fe7402020-07-15 13:33:30 -0700140 deps.CrtBegin = "crtbegin_dynamic"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 }
Dan Albert92fe7402020-07-15 13:33:30 -0700142 deps.CrtEnd = "crtend_android"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 }
144
Colin Crossb916a382016-07-29 17:28:03 -0700145 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800146 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700147 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
149 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
150 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
151 // move them to the beginning of deps.LateStaticLibs
152 var groupLibs []string
153 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
154 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
155 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
156 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700157
158 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700159 deps.DynamicLinker = "linker"
160 deps.LinkerFlagsFile = "host_bionic_linker_flags"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700161 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163
Colin Crossb916a382016-07-29 17:28:03 -0700164 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
166 "from static libs or set static_executable: true")
167 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800168
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 return deps
170}
171
Colin Crossb916a382016-07-29 17:28:03 -0700172func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 return true
174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700178 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700179 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700180 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 }
Colin Crossb916a382016-07-29 17:28:03 -0700182 module.compiler = NewBaseCompiler()
183 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700184 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000185
186 // Allow module to be added as member of an sdk/module_exports.
187 module.sdkMemberTypes = []android.SdkMemberType{
188 ccBinarySdkMemberType,
189 }
Colin Crossb916a382016-07-29 17:28:03 -0700190 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191}
192
Colin Crossb916a382016-07-29 17:28:03 -0700193func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700194 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195
Dan Willemsen2e47b342016-11-17 01:02:25 -0800196 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 if ctx.Os() == android.Linux {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700198 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800199 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800201 } else if !ctx.Fuchsia() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700203 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 }
205 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206}
207
Colin Crossb916a382016-07-29 17:28:03 -0700208func (binary *binaryDecorator) static() bool {
209 return Bool(binary.Properties.Static_executable)
210}
211
212func (binary *binaryDecorator) staticBinary() bool {
213 return binary.static()
214}
215
Inseob Kim7f283f42020-06-01 21:53:49 +0900216func (binary *binaryDecorator) binary() bool {
217 return true
218}
219
Colin Crossb916a382016-07-29 17:28:03 -0700220func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700221 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Colin Cross446c6662018-09-14 16:00:16 -0700223 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800224 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800225 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 }
227 }
228
229 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
230 // all code is position independent, and then those warnings get promoted to
231 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700232 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800233 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 }
235
Dan Willemsen2e47b342016-11-17 01:02:25 -0800236 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700237 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 // Clang driver needs -static to create static executable.
239 // However, bionic/linker uses -shared to overwrite.
240 // Linker for x86 targets does not allow coexistance of -static and -shared,
241 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800242 if !inList("-shared", flags.Local.LdFlags) {
243 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 }
245
Colin Cross4af21ed2019-11-04 09:37:55 -0800246 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 "-nostdlib",
248 "-Bstatic",
249 "-Wl,--gc-sections",
250 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 } else {
252 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700253 if binary.Properties.DynamicLinker != "" {
254 flags.DynamicLinker = binary.Properties.DynamicLinker
255 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700256 switch ctx.Os() {
257 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700258 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900259 flags.DynamicLinker = "/system/bin/bootstrap/linker"
260 } else {
261 flags.DynamicLinker = "/system/bin/linker"
262 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700263 if flags.Toolchain.Is64Bit() {
264 flags.DynamicLinker += "64"
265 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700266 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700267 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700268 default:
269 ctx.ModuleErrorf("unknown dynamic linker")
270 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700271 }
272
273 if ctx.Os() == android.LinuxBionic {
274 // Use the dlwrap entry point, but keep _start around so
275 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800276 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700277 "-Wl,--entry=__dlwrap__start",
278 "-Wl,--undefined=_start",
279 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 }
281 }
282
Colin Cross4af21ed2019-11-04 09:37:55 -0800283 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 "-pie",
285 "-nostdlib",
286 "-Bdynamic",
287 "-Wl,--gc-sections",
288 "-Wl,-z,nocopyreloc",
289 )
290 }
291 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700292 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800293 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 }
295 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800296 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 }
298 }
299
300 return flags
301}
302
Colin Crossb916a382016-07-29 17:28:03 -0700303func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700304 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305
306 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
307 outputFile := android.PathForModuleOut(ctx, fileName)
308 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309
310 var linkerDeps android.Paths
311
Dan Willemsena0790e32018-10-12 00:24:23 -0700312 if deps.LinkerFlagsFile.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800313 flags.Local.LdFlags = append(flags.Local.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")")
Dan Willemsena0790e32018-10-12 00:24:23 -0700314 linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path())
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700315 }
316
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800318 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Dan Willemsena0790e32018-10-12 00:24:23 -0700319 } else if ctx.toolchain().Bionic() && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800320 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 }
322
323 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200324 stripFlags := flagsToStripFlags(flags)
325 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800326 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200327 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800328 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329 strippedOutputFile := outputFile
330 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200331 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 }
333
Colin Crossb60190a2018-09-04 16:28:17 -0700334 binary.unstrippedOutputFile = outputFile
335
Nan Zhang0007d812017-11-07 10:57:05 -0800336 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 afterPrefixSymbols := outputFile
338 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800339 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200340 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341 }
342
Colin Crossd7227f92019-09-05 14:26:33 -0700343 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
344
Dan Willemsen569edc52018-11-19 09:33:29 -0800345 if Bool(binary.baseLinker.Properties.Use_version_lib) {
346 if ctx.Host() {
347 versionedOutputFile := outputFile
348 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
349 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
350 } else {
351 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000352 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800353
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200354 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800355 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000356 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200357 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800358 }
359
360 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
361 }
Colin Cross86803cf2018-02-15 14:12:26 -0800362 }
363
Dan Willemsena0790e32018-10-12 00:24:23 -0700364 if ctx.Os() == android.LinuxBionic && !binary.static() {
365 injectedOutputFile := outputFile
366 outputFile = android.PathForModuleOut(ctx, "prelinker", fileName)
367
368 if !deps.DynamicLinker.Valid() {
369 panic("Non-static host bionic modules must have a dynamic linker")
370 }
371
372 binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile)
373 }
374
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800375 var sharedLibs android.Paths
376 // Ignore shared libs for static executables.
377 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900378 sharedLibs = deps.EarlySharedLibs
379 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800380 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900381 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800382 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
383 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
384 }
385
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700386 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700387 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700388
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700389 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Josh Gao75a50a22019-06-07 17:58:59 -0700391 builderFlags, outputFile, nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700392
Dan Willemsen581341d2017-02-09 16:16:31 -0800393 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
394 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Oliver Nguyenc7434142019-04-24 14:22:25 -0700395 binary.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800396
Alex Light3d673592019-01-18 14:37:31 -0800397 // Need to determine symlinks early since some targets (ie APEX) need this
398 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800399 for _, symlink := range binary.Properties.Symlinks {
400 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800401 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800402 }
403
Nan Zhang0007d812017-11-07 10:57:05 -0800404 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100405 if String(binary.Properties.Suffix) == "" {
406 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800407 }
408 if ctx.TargetPrimary() {
Colin Crossa6159012020-11-12 12:14:36 -0800409 symlinkName := binary.getStemWithoutSuffix(ctx)
410 binary.symlinks = append(binary.symlinks, symlinkName)
411 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800412 }
413 }
414
Alex Light3d673592019-01-18 14:37:31 -0800415 return ret
416}
417
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900418func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
419 return binary.unstrippedOutputFile
420}
421
Alex Light3d673592019-01-18 14:37:31 -0800422func (binary *binaryDecorator) symlinkList() []string {
423 return binary.symlinks
424}
425
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700426func (binary *binaryDecorator) nativeCoverage() bool {
427 return true
428}
429
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900430func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
431 return binary.coverageOutputFile
432}
433
Jiyong Parkf1194352019-02-25 11:05:47 +0900434// /system/bin/linker -> /apex/com.android.runtime/bin/linker
435func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
436 dir := binary.baseInstaller.installDir(ctx)
437 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
438 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
439
440 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
441 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
442
443 for _, symlink := range binary.symlinks {
444 ctx.InstallAbsoluteSymlink(dir, symlink, target)
445 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target))
446 }
447}
448
Alex Light3d673592019-01-18 14:37:31 -0800449func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900450 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
451 // The original path becomes a symlink to the corresponding file in the
452 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700453 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700454 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700455 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
456 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700457
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 Crossa6159012020-11-12 12:14:36 -0800464
465 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800466 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800467 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
468 binary.baseInstaller.path)
469 if symlink == binary.preferredArchSymlink {
470 // If this is the preferred arch symlink, save the installed path for use as the
471 // tool path.
472 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
473 }
Colin Cross9b09f242016-12-07 13:37:42 -0800474 }
475
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700476 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800477 // If the binary is multilib with a symlink to the preferred architecture, use the
478 // symlink instead of the binary because that's the more "canonical" name.
479 if preferredArchSymlinkPath.Valid() {
480 binary.toolPath = preferredArchSymlinkPath
481 } else {
482 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
483 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700484 }
485}
486
487func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
488 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700489}
Dan Willemsena0790e32018-10-12 00:24:23 -0700490
491func init() {
492 pctx.HostBinToolVariable("hostBionicSymbolsInjectCmd", "host_bionic_inject")
493}
494
495var injectHostBionicSymbols = pctx.AndroidStaticRule("injectHostBionicSymbols",
496 blueprint.RuleParams{
497 Command: "$hostBionicSymbolsInjectCmd -i $in -l $linker -o $out",
498 CommandDeps: []string{"$hostBionicSymbolsInjectCmd"},
499 }, "linker")
500
501func (binary *binaryDecorator) injectHostBionicLinkerSymbols(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
502 ctx.Build(pctx, android.BuildParams{
503 Rule: injectHostBionicSymbols,
504 Description: "inject host bionic symbols",
505 Input: in,
506 Implicit: linker,
507 Output: out,
508 Args: map[string]string{
509 "linker": linker.String(),
510 },
511 })
512}