blob: 7aa8e20cc8197129b912912189edf5ff7f87bd54 [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
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "android/soong/android"
Colin Cross8ff10582023-12-07 13:10:56 -080021 "github.com/google/blueprint"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022)
23
24type BinaryLinkerProperties struct {
25 // compile executable with -static
26 Static_executable *bool `android:"arch_variant"`
27
28 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080029 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
31 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080032 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070033
34 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080035 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070036
37 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010038 Symlink_preferred_arch *bool `android:"arch_variant"`
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
Chris Parsonse0f2ab32020-11-20 17:27:25 -050044 // override the dynamic linker
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)
Liz Kammer2b8004b2021-10-04 13:55:44 -040064 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 Cross8ff10582023-12-07 13:10:56 -080069 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.
Liz Kammer2b8004b2021-10-04 13:55:44 -040074func BinaryHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -080075 module, _ := newBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 return module.Init()
77}
78
79//
80// Executables
81//
82
Chris Parsonse0f2ab32020-11-20 17:27:25 -050083// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070084type binaryDecorator struct {
85 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070086 *baseInstaller
Thiébaud Weksteend4587452020-08-19 14:53:01 +020087 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070088
89 Properties BinaryLinkerProperties
90
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070091 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080092
Colin Crossb60190a2018-09-04 16:28:17 -070093 // Location of the linked, unstripped binary
94 unstrippedOutputFile android.Path
95
Colin Cross9b09f242016-12-07 13:37:42 -080096 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
97 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080098
Colin Crossa6159012020-11-12 12:14:36 -080099 // If the module has symlink_preferred_arch set, the name of the symlink to the
100 // binary for the preferred arch.
101 preferredArchSymlink string
102
Dan Willemsen581341d2017-02-09 16:16:31 -0800103 // Output archive of gcno coverage information
104 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800105
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000106 // Location of the files that should be copied to dist dir when requested
107 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900108
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500109 // Action command lines to run directly after the binary is installed. For example,
110 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800111 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112}
113
Colin Crossb916a382016-07-29 17:28:03 -0700114var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500116// linkerProps returns the list of individual properties objects relevant
117// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700118func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700119 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 &binary.Properties,
121 &binary.stripper.StripProperties)
122
123}
124
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500125// getStemWithoutSuffix returns the main section of the name to use for the symlink of
126// the main output file of this binary module. This may be derived from the module name
127// or other property overrides.
128// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100129func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700130 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800131 if String(binary.Properties.Stem) != "" {
132 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 }
134
Roland Levillain9efb8c72019-06-05 13:31:31 +0100135 return stem
136}
137
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500138// getStem returns the full name to use for the symlink of the main output file of this binary
139// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100140func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
141 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142}
143
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500144// linkerDeps augments and returns the given `deps` to contain dependencies on
145// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800146func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700147 deps = binary.baseLinker.linkerDeps(ctx, deps)
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000148 if binary.baseLinker.Properties.crt() {
Colin Crossd1a28132021-06-21 17:34:47 -0700149 if binary.static() {
150 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
151 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
152 } else {
153 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
154 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
Colin Crossd1a28132021-06-21 17:34:47 -0700156 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157
Colin Cross450744e2021-08-18 13:52:09 -0700158 if binary.static() {
159 deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
160 }
161
Colin Crossd1a28132021-06-21 17:34:47 -0700162 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700163 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800164 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700165 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 }
167 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
168 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
169 // move them to the beginning of deps.LateStaticLibs
170 var groupLibs []string
171 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
172 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
173 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
174 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700175
176 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700177 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700178 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 }
180
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400181 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
183 "from static libs or set static_executable: true")
184 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800185
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 return deps
187}
188
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500189// NewBinary builds and returns a new Module corresponding to a C++ binary.
190// Individual module implementations which comprise a C++ binary should call this function,
191// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700192func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800193 return newBinary(hod)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400194}
195
Colin Cross8ff10582023-12-07 13:10:56 -0800196func newBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700198 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700199 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700200 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 }
Colin Crossb916a382016-07-29 17:28:03 -0700202 module.compiler = NewBaseCompiler()
203 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700204 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000205
206 // Allow module to be added as member of an sdk/module_exports.
207 module.sdkMemberTypes = []android.SdkMemberType{
208 ccBinarySdkMemberType,
209 }
Colin Crossb916a382016-07-29 17:28:03 -0700210 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211}
212
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500213// linkerInit initializes dynamic properties of the linker (such as runpath) based
214// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700215func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700216 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
Colin Cross7e2092a2022-03-08 13:15:58 -0800218 if ctx.Os().Linux() && ctx.Host() {
219 // Unless explicitly specified otherwise, host static binaries are built with -static
220 // if HostStaticBinaries is true for the product configuration.
221 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
222 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 }
224 }
Colin Cross7e2092a2022-03-08 13:15:58 -0800225
226 if ctx.Darwin() || ctx.Windows() {
227 // Static executables are not supported on Darwin or Windows
228 binary.Properties.Static_executable = nil
229 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230}
231
Colin Crossb916a382016-07-29 17:28:03 -0700232func (binary *binaryDecorator) static() bool {
233 return Bool(binary.Properties.Static_executable)
234}
235
236func (binary *binaryDecorator) staticBinary() bool {
237 return binary.static()
238}
239
Inseob Kim7f283f42020-06-01 21:53:49 +0900240func (binary *binaryDecorator) binary() bool {
241 return true
242}
243
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500244// linkerFlags returns a Flags object containing linker flags that are defined
245// by this binary, or that are implied by attributes of this binary. These flags are
246// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700247func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700248 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500250 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700251 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800252 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800253 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 }
255 }
256
257 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
258 // all code is position independent, and then those warnings get promoted to
259 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700260 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800261 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262 }
263
Dan Willemsen2e47b342016-11-17 01:02:25 -0800264 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700265 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 // Clang driver needs -static to create static executable.
267 // However, bionic/linker uses -shared to overwrite.
268 // Linker for x86 targets does not allow coexistance of -static and -shared,
269 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800270 if !inList("-shared", flags.Local.LdFlags) {
271 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 }
273
Colin Cross4af21ed2019-11-04 09:37:55 -0800274 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 "-nostdlib",
276 "-Bstatic",
277 "-Wl,--gc-sections",
278 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500279 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700281 if binary.Properties.DynamicLinker != "" {
282 flags.DynamicLinker = binary.Properties.DynamicLinker
283 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700284 switch ctx.Os() {
285 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700286 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900287 flags.DynamicLinker = "/system/bin/bootstrap/linker"
288 } else {
289 flags.DynamicLinker = "/system/bin/linker"
290 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700291 if flags.Toolchain.Is64Bit() {
292 flags.DynamicLinker += "64"
293 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700294 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700295 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700296 default:
297 ctx.ModuleErrorf("unknown dynamic linker")
298 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700299 }
300
301 if ctx.Os() == android.LinuxBionic {
302 // Use the dlwrap entry point, but keep _start around so
303 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800304 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700305 "-Wl,--entry=__dlwrap__start",
306 "-Wl,--undefined=_start",
307 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 }
309 }
310
Colin Cross4af21ed2019-11-04 09:37:55 -0800311 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 "-pie",
313 "-nostdlib",
314 "-Bdynamic",
315 "-Wl,--gc-sections",
316 "-Wl,-z,nocopyreloc",
317 )
318 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500319 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700320 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800321 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 }
323 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800324 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 }
326 }
327
328 return flags
329}
330
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500331// link registers actions to link this binary, and sets various fields
332// on this binary to reflect information that should be exported up the build
333// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700334func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700335 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336
337 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
338 outputFile := android.PathForModuleOut(ctx, fileName)
339 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340
341 var linkerDeps android.Paths
342
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800344 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000345 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800346 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700347 }
348
Dan Willemsen47450072021-10-19 20:24:49 -0700349 if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
350 fatOutputFile := outputFile
351 outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
352 transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
353 }
354
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355 builderFlags := flagsToBuilderFlags(flags)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200356 stripFlags := flagsToStripFlags(flags)
357 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800358 if ctx.Darwin() {
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200359 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800360 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 strippedOutputFile := outputFile
362 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200363 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700364 }
365
Colin Crossb60190a2018-09-04 16:28:17 -0700366 binary.unstrippedOutputFile = outputFile
367
Nan Zhang0007d812017-11-07 10:57:05 -0800368 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 afterPrefixSymbols := outputFile
370 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500371 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200372 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700373 }
374
Colin Crossd7227f92019-09-05 14:26:33 -0700375 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
376
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500377 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800378 if Bool(binary.baseLinker.Properties.Use_version_lib) {
379 if ctx.Host() {
380 versionedOutputFile := outputFile
381 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
382 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
383 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500384 // When dist'ing a library or binary that has use_version_lib set, always
385 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800386 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000387 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800388
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200389 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800390 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000391 binary.distFiles = android.MakeDefaultDistFiles(out)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200392 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800393 }
394
395 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
396 }
Colin Cross86803cf2018-02-15 14:12:26 -0800397 }
398
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700399 var validations android.Paths
Colin Crossf04eb992021-06-11 15:22:41 -0700400
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500401 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700402 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700403 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700404
405 if !deps.DynamicLinker.Valid() {
406 panic("Non-static host bionic modules must have a dynamic linker")
407 }
408
Colin Crossf04eb992021-06-11 15:22:41 -0700409 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
410 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700411 }
412
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800413 var sharedLibs android.Paths
414 // Ignore shared libs for static executables.
415 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900416 sharedLibs = deps.EarlySharedLibs
417 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800418 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900419 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800420 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
421 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -0800422 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800423 }
424
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800425 validations = append(validations, objs.tidyDepFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700426 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700427
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500428 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500429 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700430 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700431 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700432
Dan Willemsen581341d2017-02-09 16:16:31 -0800433 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
434 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500435 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800436
Alex Light3d673592019-01-18 14:37:31 -0800437 // Need to determine symlinks early since some targets (ie APEX) need this
438 // information but will not call 'install'
Inseob Kim4d945ee2022-02-24 10:29:18 +0900439 binary.setSymlinkList(ctx)
440
441 return ret
442}
443
444func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
445 return binary.unstrippedOutputFile
446}
447
Wei Li5f5d2712023-12-11 15:40:29 -0800448func (binary *binaryDecorator) strippedAllOutputFilePath() android.Path {
449 panic("Not implemented.")
450}
451
Inseob Kim4d945ee2022-02-24 10:29:18 +0900452func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) {
Colin Cross9b09f242016-12-07 13:37:42 -0800453 for _, symlink := range binary.Properties.Symlinks {
454 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800455 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800456 }
457
Nan Zhang0007d812017-11-07 10:57:05 -0800458 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100459 if String(binary.Properties.Suffix) == "" {
460 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800461 }
462 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500463 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800464 symlinkName := binary.getStemWithoutSuffix(ctx)
465 binary.symlinks = append(binary.symlinks, symlinkName)
466 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800467 }
468 }
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900469}
470
Alex Light3d673592019-01-18 14:37:31 -0800471func (binary *binaryDecorator) symlinkList() []string {
472 return binary.symlinks
473}
474
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700475func (binary *binaryDecorator) nativeCoverage() bool {
476 return true
477}
478
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900479func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
480 return binary.coverageOutputFile
481}
482
Jiyong Parkf1194352019-02-25 11:05:47 +0900483// /system/bin/linker -> /apex/com.android.runtime/bin/linker
484func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
485 dir := binary.baseInstaller.installDir(ctx)
486 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
487 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
488
489 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800490 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900491
492 for _, symlink := range binary.symlinks {
493 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800494 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900495 }
496}
497
Alex Light3d673592019-01-18 14:37:31 -0800498func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900499 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
500 // The original path becomes a symlink to the corresponding file in the
501 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700502 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700503 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700504 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
505 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700506
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700507 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900508 binary.installSymlinkToRuntimeApex(ctx, file)
509 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900510 binary.baseInstaller.subDir = "bootstrap"
511 }
胡泊6fe07a52023-06-14 07:38:44 +0000512 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800513
514 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800515 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800516 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
517 binary.baseInstaller.path)
518 if symlink == binary.preferredArchSymlink {
519 // If this is the preferred arch symlink, save the installed path for use as the
520 // tool path.
521 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
522 }
Colin Cross9b09f242016-12-07 13:37:42 -0800523 }
524
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700525 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800526 // If the binary is multilib with a symlink to the preferred architecture, use the
527 // symlink instead of the binary because that's the more "canonical" name.
528 if preferredArchSymlinkPath.Valid() {
529 binary.toolPath = preferredArchSymlinkPath
530 } else {
531 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
532 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700533 }
534}
535
536func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
537 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700538}
Dan Willemsena0790e32018-10-12 00:24:23 -0700539
Inseob Kima1888ce2022-10-04 14:42:02 +0900540func (binary *binaryDecorator) overriddenModules() []string {
541 return binary.Properties.Overrides
542}
543
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800544func (binary *binaryDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
545 moduleInfoJSON.Class = []string{"EXECUTABLES"}
546 binary.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON)
547}
548
Inseob Kima1888ce2022-10-04 14:42:02 +0900549var _ overridable = (*binaryDecorator)(nil)
550
Dan Willemsena0790e32018-10-12 00:24:23 -0700551func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700552 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700553}
554
Colin Crossf04eb992021-06-11 15:22:41 -0700555var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700556 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700557 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
558 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700559 }, "linker")
560
Colin Crossf04eb992021-06-11 15:22:41 -0700561func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700562 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700563 Rule: verifyHostBionic,
564 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700565 Input: in,
566 Implicit: linker,
567 Output: out,
568 Args: map[string]string{
569 "linker": linker.String(),
570 },
571 })
572}