blob: 763d2b9251f98507907f34443b47bd525a29521f [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
Chris Parsonse0f2ab32020-11-20 17:27:25 -050045 // override the dynamic linker
Colin Cross522e3732016-09-07 13:14:06 -070046 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070047
48 // Names of modules to be overridden. Listed modules can only be other binaries
49 // (in Make or Soong).
50 // This does not completely prevent installation of the overridden binaries, but if both
51 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
52 // from PRODUCT_PACKAGES.
53 Overrides []string
Colin Crossd7227f92019-09-05 14:26:33 -070054
55 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
56 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070057}
58
59func init() {
Paul Duffin2ee69792020-01-16 12:14:42 +000060 RegisterBinaryBuildComponents(android.InitRegistrationContext)
61}
62
63func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("cc_binary", BinaryFactory)
65 ctx.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070066}
67
Patrice Arrudac249c712019-03-19 17:00:29 -070068// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090069func BinaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070070 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 return module.Init()
72}
73
Patrice Arrudac249c712019-03-19 17:00:29 -070074// cc_binary_host produces a binary that is runnable on a host.
Colin Cross36242852017-06-23 15:06:31 -070075func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070076 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 return module.Init()
78}
79
80//
81// Executables
82//
83
Chris Parsonse0f2ab32020-11-20 17:27:25 -050084// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070085type binaryDecorator struct {
86 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070087 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020088 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070089
90 Properties BinaryLinkerProperties
91
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070092 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080093
Colin Crossb60190a2018-09-04 16:28:17 -070094 // Location of the linked, unstripped binary
95 unstrippedOutputFile android.Path
96
Colin Cross9b09f242016-12-07 13:37:42 -080097 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
98 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080099
Colin Crossa6159012020-11-12 12:14:36 -0800100 // If the module has symlink_preferred_arch set, the name of the symlink to the
101 // binary for the preferred arch.
102 preferredArchSymlink string
103
Dan Willemsen581341d2017-02-09 16:16:31 -0800104 // Output archive of gcno coverage information
105 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800106
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000107 // Location of the files that should be copied to dist dir when requested
108 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900109
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500110 // Action command lines to run directly after the binary is installed. For example,
111 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800112 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113}
114
Colin Crossb916a382016-07-29 17:28:03 -0700115var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500117// linkerProps returns the list of individual properties objects relevant
118// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700119func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700120 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 &binary.Properties,
122 &binary.stripper.StripProperties)
123
124}
125
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500126// getStemWithoutSuffix returns the main section of the name to use for the symlink of
127// the main output file of this binary module. This may be derived from the module name
128// or other property overrides.
129// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100130func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700131 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800132 if String(binary.Properties.Stem) != "" {
133 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 }
135
Roland Levillain9efb8c72019-06-05 13:31:31 +0100136 return stem
137}
138
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500139// getStem returns the full name to use for the symlink of the main output file of this binary
140// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100141func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
142 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143}
144
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500145// linkerDeps augments and returns the given `deps` to contain dependencies on
146// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800147func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700148 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Crossd1a28132021-06-21 17:34:47 -0700149 if !Bool(binary.baseLinker.Properties.Nocrt) {
150 if binary.static() {
151 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
152 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
153 } else {
154 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
155 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 }
Colin Crossd1a28132021-06-21 17:34:47 -0700157 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158
Colin Crossd1a28132021-06-21 17:34:47 -0700159 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700160 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800161 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700162 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 }
164 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
165 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
166 // move them to the beginning of deps.LateStaticLibs
167 var groupLibs []string
168 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
169 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
170 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
171 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700172
173 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700174 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700175 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 }
177
Liz Kammer356f7d42021-01-26 09:18:53 -0500178 if !binary.static() && inList("libc", deps.StaticLibs) && !ctx.BazelConversionMode() {
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
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500186// NewBinary builds and returns a new Module corresponding to a C++ binary.
187// Individual module implementations which comprise a C++ binary should call this function,
188// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700189func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700191 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700192 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700193 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 }
Colin Crossb916a382016-07-29 17:28:03 -0700195 module.compiler = NewBaseCompiler()
196 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700197 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000198
199 // Allow module to be added as member of an sdk/module_exports.
200 module.sdkMemberTypes = []android.SdkMemberType{
201 ccBinarySdkMemberType,
202 }
Colin Crossb916a382016-07-29 17:28:03 -0700203 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204}
205
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500206// linkerInit initializes dynamic properties of the linker (such as runpath) based
207// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700208func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700209 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
Dan Willemsen2e47b342016-11-17 01:02:25 -0800211 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 if ctx.Os() == android.Linux {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500213 // Unless explicitly specified otherwise, host static binaries are built with -static
214 // if HostStaticBinaries is true for the product configuration.
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700215 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800216 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 }
Colin Crosscb0ac952021-07-20 13:17:15 -0700218 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700220 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 }
222 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223}
224
Colin Crossb916a382016-07-29 17:28:03 -0700225func (binary *binaryDecorator) static() bool {
226 return Bool(binary.Properties.Static_executable)
227}
228
229func (binary *binaryDecorator) staticBinary() bool {
230 return binary.static()
231}
232
Inseob Kim7f283f42020-06-01 21:53:49 +0900233func (binary *binaryDecorator) binary() bool {
234 return true
235}
236
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500237// linkerFlags returns a Flags object containing linker flags that are defined
238// by this binary, or that are implied by attributes of this binary. These flags are
239// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700240func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700241 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500243 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700244 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800245 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800246 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 }
248 }
249
250 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
251 // all code is position independent, and then those warnings get promoted to
252 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700253 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800254 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
256
Dan Willemsen2e47b342016-11-17 01:02:25 -0800257 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700258 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 // Clang driver needs -static to create static executable.
260 // However, bionic/linker uses -shared to overwrite.
261 // Linker for x86 targets does not allow coexistance of -static and -shared,
262 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800263 if !inList("-shared", flags.Local.LdFlags) {
264 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265 }
266
Colin Cross4af21ed2019-11-04 09:37:55 -0800267 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 "-nostdlib",
269 "-Bstatic",
270 "-Wl,--gc-sections",
271 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500272 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700274 if binary.Properties.DynamicLinker != "" {
275 flags.DynamicLinker = binary.Properties.DynamicLinker
276 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700277 switch ctx.Os() {
278 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700279 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900280 flags.DynamicLinker = "/system/bin/bootstrap/linker"
281 } else {
282 flags.DynamicLinker = "/system/bin/linker"
283 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700284 if flags.Toolchain.Is64Bit() {
285 flags.DynamicLinker += "64"
286 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700287 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700288 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700289 default:
290 ctx.ModuleErrorf("unknown dynamic linker")
291 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700292 }
293
294 if ctx.Os() == android.LinuxBionic {
295 // Use the dlwrap entry point, but keep _start around so
296 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800297 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700298 "-Wl,--entry=__dlwrap__start",
299 "-Wl,--undefined=_start",
300 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 }
302 }
303
Colin Cross4af21ed2019-11-04 09:37:55 -0800304 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 "-pie",
306 "-nostdlib",
307 "-Bdynamic",
308 "-Wl,--gc-sections",
309 "-Wl,-z,nocopyreloc",
310 )
311 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500312 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700313 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800314 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 }
316 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800317 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 }
319 }
320
321 return flags
322}
323
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500324// link registers actions to link this binary, and sets various fields
325// on this binary to reflect information that should be exported up the build
326// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700327func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700328 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329
330 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
331 outputFile := android.PathForModuleOut(ctx, fileName)
332 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700333
334 var linkerDeps android.Paths
335
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800337 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000338 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800339 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 }
341
342 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200343 stripFlags := flagsToStripFlags(flags)
344 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800345 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200346 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800347 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 strippedOutputFile := outputFile
349 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200350 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351 }
352
Colin Crossb60190a2018-09-04 16:28:17 -0700353 binary.unstrippedOutputFile = outputFile
354
Nan Zhang0007d812017-11-07 10:57:05 -0800355 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700356 afterPrefixSymbols := outputFile
357 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500358 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200359 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 }
361
Colin Crossd7227f92019-09-05 14:26:33 -0700362 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
363
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500364 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800365 if Bool(binary.baseLinker.Properties.Use_version_lib) {
366 if ctx.Host() {
367 versionedOutputFile := outputFile
368 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
369 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
370 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500371 // When dist'ing a library or binary that has use_version_lib set, always
372 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800373 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000374 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800375
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200376 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800377 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000378 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200379 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800380 }
381
382 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
383 }
Colin Cross86803cf2018-02-15 14:12:26 -0800384 }
385
Colin Crossf04eb992021-06-11 15:22:41 -0700386 var validations android.WritablePaths
387
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500388 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700389 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700390 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700391
392 if !deps.DynamicLinker.Valid() {
393 panic("Non-static host bionic modules must have a dynamic linker")
394 }
395
Colin Crossf04eb992021-06-11 15:22:41 -0700396 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
397 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700398 }
399
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800400 var sharedLibs android.Paths
401 // Ignore shared libs for static executables.
402 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900403 sharedLibs = deps.EarlySharedLibs
404 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800405 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900406 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800407 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
408 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
409 }
410
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700411 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700412 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700413
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500414 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500415 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700416 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700417 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700418
Dan Willemsen581341d2017-02-09 16:16:31 -0800419 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
420 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500421 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800422
Alex Light3d673592019-01-18 14:37:31 -0800423 // Need to determine symlinks early since some targets (ie APEX) need this
424 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800425 for _, symlink := range binary.Properties.Symlinks {
426 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800427 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800428 }
429
Nan Zhang0007d812017-11-07 10:57:05 -0800430 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100431 if String(binary.Properties.Suffix) == "" {
432 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800433 }
434 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500435 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800436 symlinkName := binary.getStemWithoutSuffix(ctx)
437 binary.symlinks = append(binary.symlinks, symlinkName)
438 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800439 }
440 }
441
Alex Light3d673592019-01-18 14:37:31 -0800442 return ret
443}
444
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900445func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
446 return binary.unstrippedOutputFile
447}
448
Alex Light3d673592019-01-18 14:37:31 -0800449func (binary *binaryDecorator) symlinkList() []string {
450 return binary.symlinks
451}
452
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700453func (binary *binaryDecorator) nativeCoverage() bool {
454 return true
455}
456
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900457func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
458 return binary.coverageOutputFile
459}
460
Jiyong Parkf1194352019-02-25 11:05:47 +0900461// /system/bin/linker -> /apex/com.android.runtime/bin/linker
462func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
463 dir := binary.baseInstaller.installDir(ctx)
464 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
465 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
466
467 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800468 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900469
470 for _, symlink := range binary.symlinks {
471 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800472 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900473 }
474}
475
Alex Light3d673592019-01-18 14:37:31 -0800476func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900477 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
478 // The original path becomes a symlink to the corresponding file in the
479 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700480 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700481 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700482 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
483 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700484
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700485 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900486 binary.installSymlinkToRuntimeApex(ctx, file)
487 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900488 binary.baseInstaller.subDir = "bootstrap"
489 }
Alex Light3d673592019-01-18 14:37:31 -0800490 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800491
492 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800493 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800494 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
495 binary.baseInstaller.path)
496 if symlink == binary.preferredArchSymlink {
497 // If this is the preferred arch symlink, save the installed path for use as the
498 // tool path.
499 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
500 }
Colin Cross9b09f242016-12-07 13:37:42 -0800501 }
502
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700503 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800504 // If the binary is multilib with a symlink to the preferred architecture, use the
505 // symlink instead of the binary because that's the more "canonical" name.
506 if preferredArchSymlinkPath.Valid() {
507 binary.toolPath = preferredArchSymlinkPath
508 } else {
509 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
510 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700511 }
512}
513
514func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
515 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700516}
Dan Willemsena0790e32018-10-12 00:24:23 -0700517
518func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700519 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700520}
521
Colin Crossf04eb992021-06-11 15:22:41 -0700522var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700523 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700524 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
525 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700526 }, "linker")
527
Colin Crossf04eb992021-06-11 15:22:41 -0700528func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700529 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700530 Rule: verifyHostBionic,
531 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700532 Input: in,
533 Implicit: linker,
534 Output: out,
535 Args: map[string]string{
536 "linker": linker.String(),
537 },
538 })
539}