blob: c5017c1dd4671a03d1c869a1f33ca306141710cb [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"
Liz Kammer12615db2021-09-28 09:19:17 -040021 "github.com/google/blueprint/proptools"
Dan Willemsena0790e32018-10-12 00:24:23 -070022
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
Liz Kammer2b8004b2021-10-04 13:55:44 -040024 "android/soong/bazel"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
27type BinaryLinkerProperties struct {
28 // compile executable with -static
29 Static_executable *bool `android:"arch_variant"`
30
31 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080032 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070033
34 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080035 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
37 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080038 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070039
40 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010041 Symlink_preferred_arch *bool `android:"arch_variant"`
Colin Cross522e3732016-09-07 13:14:06 -070042
Colin Cross9b09f242016-12-07 13:37:42 -080043 // install symlinks to the binary. Symlink names will have the suffix and the binary
44 // extension (if any) appended
45 Symlinks []string `android:"arch_variant"`
46
Chris Parsonse0f2ab32020-11-20 17:27:25 -050047 // override the dynamic linker
Colin Cross522e3732016-09-07 13:14:06 -070048 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070049
50 // Names of modules to be overridden. Listed modules can only be other binaries
51 // (in Make or Soong).
52 // This does not completely prevent installation of the overridden binaries, but if both
53 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
54 // from PRODUCT_PACKAGES.
55 Overrides []string
Colin Crossd7227f92019-09-05 14:26:33 -070056
57 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
58 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070059}
60
61func init() {
Paul Duffin2ee69792020-01-16 12:14:42 +000062 RegisterBinaryBuildComponents(android.InitRegistrationContext)
63}
64
65func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
66 ctx.RegisterModuleType("cc_binary", BinaryFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040067 ctx.RegisterModuleType("cc_binary_host", BinaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070068}
69
Patrice Arrudac249c712019-03-19 17:00:29 -070070// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090071func BinaryFactory() android.Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040072 module, _ := newBinary(android.HostAndDeviceSupported, true)
Yu Liu7f3605f2022-02-08 14:15:12 -080073 module.bazelHandler = &ccBinaryBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 return module.Init()
75}
76
Patrice Arrudac249c712019-03-19 17:00:29 -070077// cc_binary_host produces a binary that is runnable on a host.
Liz Kammer2b8004b2021-10-04 13:55:44 -040078func BinaryHostFactory() android.Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040079 module, _ := newBinary(android.HostSupported, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 return module.Init()
81}
82
83//
84// Executables
85//
86
Chris Parsonse0f2ab32020-11-20 17:27:25 -050087// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070088type binaryDecorator struct {
89 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070090 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020091 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070092
93 Properties BinaryLinkerProperties
94
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070095 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080096
Colin Crossb60190a2018-09-04 16:28:17 -070097 // Location of the linked, unstripped binary
98 unstrippedOutputFile android.Path
99
Colin Cross9b09f242016-12-07 13:37:42 -0800100 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
101 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -0800102
Colin Crossa6159012020-11-12 12:14:36 -0800103 // If the module has symlink_preferred_arch set, the name of the symlink to the
104 // binary for the preferred arch.
105 preferredArchSymlink string
106
Dan Willemsen581341d2017-02-09 16:16:31 -0800107 // Output archive of gcno coverage information
108 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800109
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000110 // Location of the files that should be copied to dist dir when requested
111 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900112
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500113 // Action command lines to run directly after the binary is installed. For example,
114 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800115 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116}
117
Colin Crossb916a382016-07-29 17:28:03 -0700118var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500120// linkerProps returns the list of individual properties objects relevant
121// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700122func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700123 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 &binary.Properties,
125 &binary.stripper.StripProperties)
126
127}
128
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500129// getStemWithoutSuffix returns the main section of the name to use for the symlink of
130// the main output file of this binary module. This may be derived from the module name
131// or other property overrides.
132// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100133func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700134 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800135 if String(binary.Properties.Stem) != "" {
136 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137 }
138
Roland Levillain9efb8c72019-06-05 13:31:31 +0100139 return stem
140}
141
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500142// getStem returns the full name to use for the symlink of the main output file of this binary
143// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100144func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
145 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146}
147
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500148// linkerDeps augments and returns the given `deps` to contain dependencies on
149// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800150func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700151 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Crossd1a28132021-06-21 17:34:47 -0700152 if !Bool(binary.baseLinker.Properties.Nocrt) {
153 if binary.static() {
154 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
155 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
156 } else {
157 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
158 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 }
Colin Crossd1a28132021-06-21 17:34:47 -0700160 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161
Colin Cross450744e2021-08-18 13:52:09 -0700162 if binary.static() {
163 deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
164 }
165
Colin Crossd1a28132021-06-21 17:34:47 -0700166 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700167 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800168 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700169 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 }
171 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
172 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
173 // move them to the beginning of deps.LateStaticLibs
174 var groupLibs []string
175 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
176 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
177 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
178 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700179
180 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700181 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700182 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183 }
184
Liz Kammer356f7d42021-01-26 09:18:53 -0500185 if !binary.static() && inList("libc", deps.StaticLibs) && !ctx.BazelConversionMode() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
187 "from static libs or set static_executable: true")
188 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800189
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 return deps
191}
192
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500193// NewBinary builds and returns a new Module corresponding to a C++ binary.
194// Individual module implementations which comprise a C++ binary should call this function,
195// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700196func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400197 return newBinary(hod, true)
198}
199
200func newBinary(hod android.HostOrDeviceSupported, bazelable bool) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700202 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700203 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700204 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 }
Colin Crossb916a382016-07-29 17:28:03 -0700206 module.compiler = NewBaseCompiler()
207 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700208 module.installer = binary
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400209 module.bazelable = bazelable
Paul Duffin25ce04b2020-01-16 11:47:25 +0000210
211 // Allow module to be added as member of an sdk/module_exports.
212 module.sdkMemberTypes = []android.SdkMemberType{
213 ccBinarySdkMemberType,
214 }
Colin Crossb916a382016-07-29 17:28:03 -0700215 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216}
217
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500218// linkerInit initializes dynamic properties of the linker (such as runpath) based
219// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700220func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700221 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Colin Cross7e2092a2022-03-08 13:15:58 -0800223 if ctx.Os().Linux() && ctx.Host() {
224 // Unless explicitly specified otherwise, host static binaries are built with -static
225 // if HostStaticBinaries is true for the product configuration.
226 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
227 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 }
229 }
Colin Cross7e2092a2022-03-08 13:15:58 -0800230
231 if ctx.Darwin() || ctx.Windows() {
232 // Static executables are not supported on Darwin or Windows
233 binary.Properties.Static_executable = nil
234 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235}
236
Colin Crossb916a382016-07-29 17:28:03 -0700237func (binary *binaryDecorator) static() bool {
238 return Bool(binary.Properties.Static_executable)
239}
240
241func (binary *binaryDecorator) staticBinary() bool {
242 return binary.static()
243}
244
Inseob Kim7f283f42020-06-01 21:53:49 +0900245func (binary *binaryDecorator) binary() bool {
246 return true
247}
248
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500249// linkerFlags returns a Flags object containing linker flags that are defined
250// by this binary, or that are implied by attributes of this binary. These flags are
251// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700252func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700253 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500255 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700256 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800257 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800258 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 }
260 }
261
262 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
263 // all code is position independent, and then those warnings get promoted to
264 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700265 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800266 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 }
268
Dan Willemsen2e47b342016-11-17 01:02:25 -0800269 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700270 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 // Clang driver needs -static to create static executable.
272 // However, bionic/linker uses -shared to overwrite.
273 // Linker for x86 targets does not allow coexistance of -static and -shared,
274 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800275 if !inList("-shared", flags.Local.LdFlags) {
276 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 }
278
Colin Cross4af21ed2019-11-04 09:37:55 -0800279 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 "-nostdlib",
281 "-Bstatic",
282 "-Wl,--gc-sections",
283 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500284 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700286 if binary.Properties.DynamicLinker != "" {
287 flags.DynamicLinker = binary.Properties.DynamicLinker
288 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700289 switch ctx.Os() {
290 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700291 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900292 flags.DynamicLinker = "/system/bin/bootstrap/linker"
293 } else {
294 flags.DynamicLinker = "/system/bin/linker"
295 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700296 if flags.Toolchain.Is64Bit() {
297 flags.DynamicLinker += "64"
298 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700299 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700300 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700301 default:
302 ctx.ModuleErrorf("unknown dynamic linker")
303 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700304 }
305
306 if ctx.Os() == android.LinuxBionic {
307 // Use the dlwrap entry point, but keep _start around so
308 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800309 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700310 "-Wl,--entry=__dlwrap__start",
311 "-Wl,--undefined=_start",
312 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 }
314 }
315
Colin Cross4af21ed2019-11-04 09:37:55 -0800316 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317 "-pie",
318 "-nostdlib",
319 "-Bdynamic",
320 "-Wl,--gc-sections",
321 "-Wl,-z,nocopyreloc",
322 )
323 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500324 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700325 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800326 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327 }
328 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800329 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 }
331 }
332
333 return flags
334}
335
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500336// link registers actions to link this binary, and sets various fields
337// on this binary to reflect information that should be exported up the build
338// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700339func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700340 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341
342 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
343 outputFile := android.PathForModuleOut(ctx, fileName)
344 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345
346 var linkerDeps android.Paths
347
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800349 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000350 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800351 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 }
353
Dan Willemsen47450072021-10-19 20:24:49 -0700354 if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
355 fatOutputFile := outputFile
356 outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
357 transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
358 }
359
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200361 stripFlags := flagsToStripFlags(flags)
362 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800363 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200364 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800365 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 strippedOutputFile := outputFile
367 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200368 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 }
370
Colin Crossb60190a2018-09-04 16:28:17 -0700371 binary.unstrippedOutputFile = outputFile
372
Nan Zhang0007d812017-11-07 10:57:05 -0800373 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374 afterPrefixSymbols := outputFile
375 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500376 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200377 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 }
379
Colin Crossd7227f92019-09-05 14:26:33 -0700380 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
381
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500382 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800383 if Bool(binary.baseLinker.Properties.Use_version_lib) {
384 if ctx.Host() {
385 versionedOutputFile := outputFile
386 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
387 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
388 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500389 // When dist'ing a library or binary that has use_version_lib set, always
390 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800391 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000392 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800393
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200394 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800395 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000396 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200397 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800398 }
399
400 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
401 }
Colin Cross86803cf2018-02-15 14:12:26 -0800402 }
403
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700404 var validations android.Paths
Colin Crossf04eb992021-06-11 15:22:41 -0700405
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500406 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700407 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700408 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700409
410 if !deps.DynamicLinker.Valid() {
411 panic("Non-static host bionic modules must have a dynamic linker")
412 }
413
Colin Crossf04eb992021-06-11 15:22:41 -0700414 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
415 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700416 }
417
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800418 var sharedLibs android.Paths
419 // Ignore shared libs for static executables.
420 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900421 sharedLibs = deps.EarlySharedLibs
422 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800423 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900424 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800425 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
426 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -0800427 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800428 }
429
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800430 validations = append(validations, objs.tidyDepFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700431 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700432
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500433 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500434 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700436 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700437
Dan Willemsen581341d2017-02-09 16:16:31 -0800438 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
439 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500440 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800441
Alex Light3d673592019-01-18 14:37:31 -0800442 // Need to determine symlinks early since some targets (ie APEX) need this
443 // information but will not call 'install'
Inseob Kim4d945ee2022-02-24 10:29:18 +0900444 binary.setSymlinkList(ctx)
445
446 return ret
447}
448
449func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
450 return binary.unstrippedOutputFile
451}
452
453func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) {
Colin Cross9b09f242016-12-07 13:37:42 -0800454 for _, symlink := range binary.Properties.Symlinks {
455 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800456 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800457 }
458
Nan Zhang0007d812017-11-07 10:57:05 -0800459 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100460 if String(binary.Properties.Suffix) == "" {
461 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800462 }
463 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500464 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800465 symlinkName := binary.getStemWithoutSuffix(ctx)
466 binary.symlinks = append(binary.symlinks, symlinkName)
467 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800468 }
469 }
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900470}
471
Alex Light3d673592019-01-18 14:37:31 -0800472func (binary *binaryDecorator) symlinkList() []string {
473 return binary.symlinks
474}
475
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700476func (binary *binaryDecorator) nativeCoverage() bool {
477 return true
478}
479
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900480func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
481 return binary.coverageOutputFile
482}
483
Jiyong Parkf1194352019-02-25 11:05:47 +0900484// /system/bin/linker -> /apex/com.android.runtime/bin/linker
485func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
486 dir := binary.baseInstaller.installDir(ctx)
487 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
488 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
489
490 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800491 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900492
493 for _, symlink := range binary.symlinks {
494 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800495 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900496 }
497}
498
Alex Light3d673592019-01-18 14:37:31 -0800499func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900500 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
501 // The original path becomes a symlink to the corresponding file in the
502 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700503 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700504 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700505 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
506 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700507
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700508 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900509 binary.installSymlinkToRuntimeApex(ctx, file)
510 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900511 binary.baseInstaller.subDir = "bootstrap"
512 }
Alex Light3d673592019-01-18 14:37:31 -0800513 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800514
515 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800516 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800517 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
518 binary.baseInstaller.path)
519 if symlink == binary.preferredArchSymlink {
520 // If this is the preferred arch symlink, save the installed path for use as the
521 // tool path.
522 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
523 }
Colin Cross9b09f242016-12-07 13:37:42 -0800524 }
525
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700526 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800527 // If the binary is multilib with a symlink to the preferred architecture, use the
528 // symlink instead of the binary because that's the more "canonical" name.
529 if preferredArchSymlinkPath.Valid() {
530 binary.toolPath = preferredArchSymlinkPath
531 } else {
532 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
533 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700534 }
535}
536
537func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
538 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539}
Dan Willemsena0790e32018-10-12 00:24:23 -0700540
541func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700542 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700543}
544
Colin Crossf04eb992021-06-11 15:22:41 -0700545var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700546 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700547 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
548 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700549 }, "linker")
550
Colin Crossf04eb992021-06-11 15:22:41 -0700551func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700552 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700553 Rule: verifyHostBionic,
554 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700555 Input: in,
556 Implicit: linker,
557 Output: out,
558 Args: map[string]string{
559 "linker": linker.String(),
560 },
561 })
562}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400563
Yu Liu7f3605f2022-02-08 14:15:12 -0800564type ccBinaryBazelHandler struct {
565 android.BazelHandler
566
567 module *Module
568}
569
570func (handler *ccBinaryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
571 bazelCtx := ctx.Config().BazelContext
572 filePaths, ok := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
573 if ok {
574 if len(filePaths) != 1 {
575 ctx.ModuleErrorf("expected exactly one output file for '%s', but got %s", label, filePaths)
576 return false
577 }
578 outputFilePath := android.PathForBazelOut(ctx, filePaths[0])
579 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
580 // TODO(b/220164721): We need to decide if we should return the stripped as the unstripped.
581 handler.module.linker.(*binaryDecorator).unstrippedOutputFile = outputFilePath
582 }
583 return ok
584}
585
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400586func binaryBp2build(ctx android.TopDownMutatorContext, m *Module, typ string) {
Liz Kammere6583482021-10-19 13:56:10 -0400587 baseAttrs := bp2BuildParseBaseProps(ctx, m)
Liz Kammer12615db2021-09-28 09:19:17 -0400588 binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m)
589
590 if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) {
591 baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency)
592 } else {
593 baseAttrs.implementationDeps.Add(baseAttrs.protoDependency)
594 }
Liz Kammer2b8004b2021-10-04 13:55:44 -0400595
596 attrs := &binaryAttributes{
Liz Kammer12615db2021-09-28 09:19:17 -0400597 binaryLinkerAttrs: binaryLinkerAttrs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400598
Liz Kammere6583482021-10-19 13:56:10 -0400599 Srcs: baseAttrs.srcs,
600 Srcs_c: baseAttrs.cSrcs,
601 Srcs_as: baseAttrs.asSrcs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400602
Liz Kammere6583482021-10-19 13:56:10 -0400603 Copts: baseAttrs.copts,
604 Cppflags: baseAttrs.cppFlags,
605 Conlyflags: baseAttrs.conlyFlags,
606 Asflags: baseAttrs.asFlags,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400607
Liz Kammere6583482021-10-19 13:56:10 -0400608 Deps: baseAttrs.implementationDeps,
609 Dynamic_deps: baseAttrs.implementationDynamicDeps,
610 Whole_archive_deps: baseAttrs.wholeArchiveDeps,
611 System_deps: baseAttrs.systemDynamicDeps,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400612
Liz Kammere6583482021-10-19 13:56:10 -0400613 Local_includes: baseAttrs.localIncludes,
614 Absolute_includes: baseAttrs.absoluteIncludes,
615 Linkopts: baseAttrs.linkopts,
616 Link_crt: baseAttrs.linkCrt,
617 Use_libcrt: baseAttrs.useLibcrt,
Yu Liua79c9462022-03-22 16:35:22 -0700618 Use_version_lib: baseAttrs.useVersionLib,
Liz Kammere6583482021-10-19 13:56:10 -0400619 Rtti: baseAttrs.rtti,
620 Stl: baseAttrs.stl,
621 Cpp_std: baseAttrs.cppStd,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400622
Liz Kammere6583482021-10-19 13:56:10 -0400623 Additional_linker_inputs: baseAttrs.additionalLinkerInputs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400624
625 Strip: stripAttributes{
Liz Kammere6583482021-10-19 13:56:10 -0400626 Keep_symbols: baseAttrs.stripKeepSymbols,
627 Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame,
628 Keep_symbols_list: baseAttrs.stripKeepSymbolsList,
629 All: baseAttrs.stripAll,
630 None: baseAttrs.stripNone,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400631 },
632
Chris Parsons58852a02021-12-09 18:10:18 -0500633 Features: baseAttrs.features,
Yu Liufc603162022-03-01 15:44:08 -0800634
635 sdkAttributes: bp2BuildParseSdkAttributes(m),
Liz Kammer2b8004b2021-10-04 13:55:44 -0400636 }
637
Sam Delmerico75539d62022-01-31 14:37:29 +0000638 ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400639 Rule_class: "cc_binary",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500640 Bzl_load_location: "//build/bazel/rules/cc:cc_binary.bzl",
Liz Kammer2b8004b2021-10-04 13:55:44 -0400641 },
642 android.CommonAttributes{Name: m.Name()},
Sam Delmerico75539d62022-01-31 14:37:29 +0000643 attrs)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400644}
645
646// binaryAttributes contains Bazel attributes corresponding to a cc binary
647type binaryAttributes struct {
648 binaryLinkerAttrs
649 Srcs bazel.LabelListAttribute
650 Srcs_c bazel.LabelListAttribute
651 Srcs_as bazel.LabelListAttribute
652
653 Copts bazel.StringListAttribute
654 Cppflags bazel.StringListAttribute
655 Conlyflags bazel.StringListAttribute
656 Asflags bazel.StringListAttribute
657
658 Deps bazel.LabelListAttribute
659 Dynamic_deps bazel.LabelListAttribute
660 Whole_archive_deps bazel.LabelListAttribute
661 System_deps bazel.LabelListAttribute
662
663 Local_includes bazel.StringListAttribute
664 Absolute_includes bazel.StringListAttribute
665
666 Linkopts bazel.StringListAttribute
667 Additional_linker_inputs bazel.LabelListAttribute
668
Yu Liua79c9462022-03-22 16:35:22 -0700669 Link_crt bazel.BoolAttribute
670 Use_libcrt bazel.BoolAttribute
671 Use_version_lib bazel.BoolAttribute
Liz Kammer2b8004b2021-10-04 13:55:44 -0400672
673 Rtti bazel.BoolAttribute
674 Stl *string
675 Cpp_std *string
676
677 Strip stripAttributes
678
679 Features bazel.StringListAttribute
Yu Liufc603162022-03-01 15:44:08 -0800680
681 sdkAttributes
Liz Kammer2b8004b2021-10-04 13:55:44 -0400682}