blob: 849aafaaa5625bdfd8d129928d341ff893b0e090 [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
Chris Parsonsf874e462022-05-10 13:50:12 -040020 "android/soong/bazel/cquery"
Dan Willemsena0790e32018-10-12 00:24:23 -070021 "github.com/google/blueprint"
Liz Kammer12615db2021-09-28 09:19:17 -040022 "github.com/google/blueprint/proptools"
Dan Willemsena0790e32018-10-12 00:24:23 -070023
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Liz Kammer2b8004b2021-10-04 13:55:44 -040025 "android/soong/bazel"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
28type BinaryLinkerProperties struct {
29 // compile executable with -static
30 Static_executable *bool `android:"arch_variant"`
31
32 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080033 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
35 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080036 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070037
38 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080039 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070040
41 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010042 Symlink_preferred_arch *bool `android:"arch_variant"`
Colin Cross522e3732016-09-07 13:14:06 -070043
Colin Cross9b09f242016-12-07 13:37:42 -080044 // install symlinks to the binary. Symlink names will have the suffix and the binary
45 // extension (if any) appended
46 Symlinks []string `android:"arch_variant"`
47
Chris Parsonse0f2ab32020-11-20 17:27:25 -050048 // override the dynamic linker
Colin Cross522e3732016-09-07 13:14:06 -070049 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070050
51 // Names of modules to be overridden. Listed modules can only be other binaries
52 // (in Make or Soong).
53 // This does not completely prevent installation of the overridden binaries, but if both
54 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
55 // from PRODUCT_PACKAGES.
56 Overrides []string
Colin Crossd7227f92019-09-05 14:26:33 -070057
58 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
59 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070060}
61
62func init() {
Paul Duffin2ee69792020-01-16 12:14:42 +000063 RegisterBinaryBuildComponents(android.InitRegistrationContext)
64}
65
66func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
67 ctx.RegisterModuleType("cc_binary", BinaryFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040068 ctx.RegisterModuleType("cc_binary_host", BinaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070069}
70
Patrice Arrudac249c712019-03-19 17:00:29 -070071// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090072func BinaryFactory() android.Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040073 module, _ := newBinary(android.HostAndDeviceSupported, true)
Yu Liu7f3605f2022-02-08 14:15:12 -080074 module.bazelHandler = &ccBinaryBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 return module.Init()
76}
77
Patrice Arrudac249c712019-03-19 17:00:29 -070078// cc_binary_host produces a binary that is runnable on a host.
Liz Kammer2b8004b2021-10-04 13:55:44 -040079func BinaryHostFactory() android.Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040080 module, _ := newBinary(android.HostSupported, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 return module.Init()
82}
83
84//
85// Executables
86//
87
Chris Parsonse0f2ab32020-11-20 17:27:25 -050088// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070089type binaryDecorator struct {
90 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070091 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020092 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070093
94 Properties BinaryLinkerProperties
95
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070096 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080097
Colin Crossb60190a2018-09-04 16:28:17 -070098 // Location of the linked, unstripped binary
99 unstrippedOutputFile android.Path
100
Colin Cross9b09f242016-12-07 13:37:42 -0800101 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
102 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -0800103
Colin Crossa6159012020-11-12 12:14:36 -0800104 // If the module has symlink_preferred_arch set, the name of the symlink to the
105 // binary for the preferred arch.
106 preferredArchSymlink string
107
Dan Willemsen581341d2017-02-09 16:16:31 -0800108 // Output archive of gcno coverage information
109 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800110
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000111 // Location of the files that should be copied to dist dir when requested
112 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900113
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500114 // Action command lines to run directly after the binary is installed. For example,
115 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800116 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117}
118
Colin Crossb916a382016-07-29 17:28:03 -0700119var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500121// linkerProps returns the list of individual properties objects relevant
122// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700123func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700124 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 &binary.Properties,
126 &binary.stripper.StripProperties)
127
128}
129
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500130// getStemWithoutSuffix returns the main section of the name to use for the symlink of
131// the main output file of this binary module. This may be derived from the module name
132// or other property overrides.
133// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100134func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700135 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800136 if String(binary.Properties.Stem) != "" {
137 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 }
139
Roland Levillain9efb8c72019-06-05 13:31:31 +0100140 return stem
141}
142
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500143// getStem returns the full name to use for the symlink of the main output file of this binary
144// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100145func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
146 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147}
148
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500149// linkerDeps augments and returns the given `deps` to contain dependencies on
150// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800151func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700152 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Crossd1a28132021-06-21 17:34:47 -0700153 if !Bool(binary.baseLinker.Properties.Nocrt) {
154 if binary.static() {
155 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
156 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
157 } else {
158 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
159 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 }
Colin Crossd1a28132021-06-21 17:34:47 -0700161 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162
Colin Cross450744e2021-08-18 13:52:09 -0700163 if binary.static() {
164 deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
165 }
166
Colin Crossd1a28132021-06-21 17:34:47 -0700167 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700168 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800169 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700170 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 }
172 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
173 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
174 // move them to the beginning of deps.LateStaticLibs
175 var groupLibs []string
176 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
177 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
178 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
179 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700180
181 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700182 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700183 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 }
185
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400186 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
188 "from static libs or set static_executable: true")
189 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800190
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 return deps
192}
193
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500194// NewBinary builds and returns a new Module corresponding to a C++ binary.
195// Individual module implementations which comprise a C++ binary should call this function,
196// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700197func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400198 return newBinary(hod, true)
199}
200
201func newBinary(hod android.HostOrDeviceSupported, bazelable bool) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700203 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700204 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700205 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 }
Colin Crossb916a382016-07-29 17:28:03 -0700207 module.compiler = NewBaseCompiler()
208 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700209 module.installer = binary
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400210 module.bazelable = bazelable
Paul Duffin25ce04b2020-01-16 11:47:25 +0000211
212 // Allow module to be added as member of an sdk/module_exports.
213 module.sdkMemberTypes = []android.SdkMemberType{
214 ccBinarySdkMemberType,
215 }
Colin Crossb916a382016-07-29 17:28:03 -0700216 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217}
218
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500219// linkerInit initializes dynamic properties of the linker (such as runpath) based
220// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700221func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700222 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223
Colin Cross7e2092a2022-03-08 13:15:58 -0800224 if ctx.Os().Linux() && ctx.Host() {
225 // Unless explicitly specified otherwise, host static binaries are built with -static
226 // if HostStaticBinaries is true for the product configuration.
227 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
228 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 }
230 }
Colin Cross7e2092a2022-03-08 13:15:58 -0800231
232 if ctx.Darwin() || ctx.Windows() {
233 // Static executables are not supported on Darwin or Windows
234 binary.Properties.Static_executable = nil
235 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236}
237
Colin Crossb916a382016-07-29 17:28:03 -0700238func (binary *binaryDecorator) static() bool {
239 return Bool(binary.Properties.Static_executable)
240}
241
242func (binary *binaryDecorator) staticBinary() bool {
243 return binary.static()
244}
245
Inseob Kim7f283f42020-06-01 21:53:49 +0900246func (binary *binaryDecorator) binary() bool {
247 return true
248}
249
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500250// linkerFlags returns a Flags object containing linker flags that are defined
251// by this binary, or that are implied by attributes of this binary. These flags are
252// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700253func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700254 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500256 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700257 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800258 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800259 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 }
261 }
262
263 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
264 // all code is position independent, and then those warnings get promoted to
265 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700266 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800267 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 }
269
Dan Willemsen2e47b342016-11-17 01:02:25 -0800270 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700271 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 // Clang driver needs -static to create static executable.
273 // However, bionic/linker uses -shared to overwrite.
274 // Linker for x86 targets does not allow coexistance of -static and -shared,
275 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800276 if !inList("-shared", flags.Local.LdFlags) {
277 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 }
279
Colin Cross4af21ed2019-11-04 09:37:55 -0800280 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 "-nostdlib",
282 "-Bstatic",
283 "-Wl,--gc-sections",
284 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500285 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700287 if binary.Properties.DynamicLinker != "" {
288 flags.DynamicLinker = binary.Properties.DynamicLinker
289 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700290 switch ctx.Os() {
291 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700292 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900293 flags.DynamicLinker = "/system/bin/bootstrap/linker"
294 } else {
295 flags.DynamicLinker = "/system/bin/linker"
296 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700297 if flags.Toolchain.Is64Bit() {
298 flags.DynamicLinker += "64"
299 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700300 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700301 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700302 default:
303 ctx.ModuleErrorf("unknown dynamic linker")
304 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700305 }
306
307 if ctx.Os() == android.LinuxBionic {
308 // Use the dlwrap entry point, but keep _start around so
309 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800310 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700311 "-Wl,--entry=__dlwrap__start",
312 "-Wl,--undefined=_start",
313 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 }
315 }
316
Colin Cross4af21ed2019-11-04 09:37:55 -0800317 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 "-pie",
319 "-nostdlib",
320 "-Bdynamic",
321 "-Wl,--gc-sections",
322 "-Wl,-z,nocopyreloc",
323 )
324 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500325 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700326 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800327 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 }
329 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800330 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 }
332 }
333
334 return flags
335}
336
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500337// link registers actions to link this binary, and sets various fields
338// on this binary to reflect information that should be exported up the build
339// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700340func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700341 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342
343 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
344 outputFile := android.PathForModuleOut(ctx, fileName)
345 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346
347 var linkerDeps android.Paths
348
Colin Cross4d9c2d12016-07-29 12:48:20 -0700349 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800350 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000351 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800352 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 }
354
Dan Willemsen47450072021-10-19 20:24:49 -0700355 if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
356 fatOutputFile := outputFile
357 outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
358 transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
359 }
360
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200362 stripFlags := flagsToStripFlags(flags)
363 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800364 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200365 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800366 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367 strippedOutputFile := outputFile
368 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200369 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370 }
371
Colin Crossb60190a2018-09-04 16:28:17 -0700372 binary.unstrippedOutputFile = outputFile
373
Nan Zhang0007d812017-11-07 10:57:05 -0800374 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700375 afterPrefixSymbols := outputFile
376 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500377 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200378 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379 }
380
Colin Crossd7227f92019-09-05 14:26:33 -0700381 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
382
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500383 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800384 if Bool(binary.baseLinker.Properties.Use_version_lib) {
385 if ctx.Host() {
386 versionedOutputFile := outputFile
387 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
388 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
389 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500390 // When dist'ing a library or binary that has use_version_lib set, always
391 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800392 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000393 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800394
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200395 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800396 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000397 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200398 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800399 }
400
401 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
402 }
Colin Cross86803cf2018-02-15 14:12:26 -0800403 }
404
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700405 var validations android.Paths
Colin Crossf04eb992021-06-11 15:22:41 -0700406
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500407 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700408 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700409 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700410
411 if !deps.DynamicLinker.Valid() {
412 panic("Non-static host bionic modules must have a dynamic linker")
413 }
414
Colin Crossf04eb992021-06-11 15:22:41 -0700415 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
416 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700417 }
418
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800419 var sharedLibs android.Paths
420 // Ignore shared libs for static executables.
421 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900422 sharedLibs = deps.EarlySharedLibs
423 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800424 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900425 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800426 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
427 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -0800428 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800429 }
430
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800431 validations = append(validations, objs.tidyDepFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700432 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700433
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500434 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500435 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700436 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700437 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700438
Dan Willemsen581341d2017-02-09 16:16:31 -0800439 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
440 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500441 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800442
Alex Light3d673592019-01-18 14:37:31 -0800443 // Need to determine symlinks early since some targets (ie APEX) need this
444 // information but will not call 'install'
Inseob Kim4d945ee2022-02-24 10:29:18 +0900445 binary.setSymlinkList(ctx)
446
447 return ret
448}
449
450func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
451 return binary.unstrippedOutputFile
452}
453
454func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) {
Colin Cross9b09f242016-12-07 13:37:42 -0800455 for _, symlink := range binary.Properties.Symlinks {
456 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800457 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800458 }
459
Nan Zhang0007d812017-11-07 10:57:05 -0800460 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100461 if String(binary.Properties.Suffix) == "" {
462 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800463 }
464 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500465 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800466 symlinkName := binary.getStemWithoutSuffix(ctx)
467 binary.symlinks = append(binary.symlinks, symlinkName)
468 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800469 }
470 }
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900471}
472
Alex Light3d673592019-01-18 14:37:31 -0800473func (binary *binaryDecorator) symlinkList() []string {
474 return binary.symlinks
475}
476
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700477func (binary *binaryDecorator) nativeCoverage() bool {
478 return true
479}
480
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900481func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
482 return binary.coverageOutputFile
483}
484
Jiyong Parkf1194352019-02-25 11:05:47 +0900485// /system/bin/linker -> /apex/com.android.runtime/bin/linker
486func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
487 dir := binary.baseInstaller.installDir(ctx)
488 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
489 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
490
491 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800492 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900493
494 for _, symlink := range binary.symlinks {
495 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800496 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900497 }
498}
499
Alex Light3d673592019-01-18 14:37:31 -0800500func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900501 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
502 // The original path becomes a symlink to the corresponding file in the
503 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700504 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700505 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700506 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
507 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700508
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700509 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900510 binary.installSymlinkToRuntimeApex(ctx, file)
511 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900512 binary.baseInstaller.subDir = "bootstrap"
513 }
Alex Light3d673592019-01-18 14:37:31 -0800514 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800515
516 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800517 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800518 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
519 binary.baseInstaller.path)
520 if symlink == binary.preferredArchSymlink {
521 // If this is the preferred arch symlink, save the installed path for use as the
522 // tool path.
523 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
524 }
Colin Cross9b09f242016-12-07 13:37:42 -0800525 }
526
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700527 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800528 // If the binary is multilib with a symlink to the preferred architecture, use the
529 // symlink instead of the binary because that's the more "canonical" name.
530 if preferredArchSymlinkPath.Valid() {
531 binary.toolPath = preferredArchSymlinkPath
532 } else {
533 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
534 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700535 }
536}
537
538func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
539 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540}
Dan Willemsena0790e32018-10-12 00:24:23 -0700541
542func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700543 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700544}
545
Colin Crossf04eb992021-06-11 15:22:41 -0700546var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700547 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700548 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
549 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700550 }, "linker")
551
Colin Crossf04eb992021-06-11 15:22:41 -0700552func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700553 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700554 Rule: verifyHostBionic,
555 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700556 Input: in,
557 Implicit: linker,
558 Output: out,
559 Args: map[string]string{
560 "linker": linker.String(),
561 },
562 })
563}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400564
Yu Liu7f3605f2022-02-08 14:15:12 -0800565type ccBinaryBazelHandler struct {
Yu Liu7f3605f2022-02-08 14:15:12 -0800566 module *Module
567}
568
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400569var _ BazelHandler = (*ccBinaryBazelHandler)(nil)
570
Chris Parsonsf874e462022-05-10 13:50:12 -0400571func (handler *ccBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Yu Liu7f3605f2022-02-08 14:15:12 -0800572 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400573 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
574}
575
576func (handler *ccBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
577 bazelCtx := ctx.Config().BazelContext
578 filePaths, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
579 if err != nil {
580 ctx.ModuleErrorf(err.Error())
581 return
Yu Liu7f3605f2022-02-08 14:15:12 -0800582 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400583
584 if len(filePaths) != 1 {
585 ctx.ModuleErrorf("expected exactly one output file for '%s', but got %s", label, filePaths)
586 return
587 }
588 outputFilePath := android.PathForBazelOut(ctx, filePaths[0])
589 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
590 // TODO(b/220164721): We need to decide if we should return the stripped as the unstripped.
591 handler.module.linker.(*binaryDecorator).unstrippedOutputFile = outputFilePath
Yu Liu7f3605f2022-02-08 14:15:12 -0800592}
593
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400594func binaryBp2build(ctx android.TopDownMutatorContext, m *Module, typ string) {
Liz Kammere6583482021-10-19 13:56:10 -0400595 baseAttrs := bp2BuildParseBaseProps(ctx, m)
Liz Kammer12615db2021-09-28 09:19:17 -0400596 binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m)
597
598 if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) {
599 baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency)
600 } else {
601 baseAttrs.implementationDeps.Add(baseAttrs.protoDependency)
602 }
Liz Kammer2b8004b2021-10-04 13:55:44 -0400603
604 attrs := &binaryAttributes{
Liz Kammer12615db2021-09-28 09:19:17 -0400605 binaryLinkerAttrs: binaryLinkerAttrs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400606
Liz Kammere6583482021-10-19 13:56:10 -0400607 Srcs: baseAttrs.srcs,
608 Srcs_c: baseAttrs.cSrcs,
609 Srcs_as: baseAttrs.asSrcs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400610
Liz Kammere6583482021-10-19 13:56:10 -0400611 Copts: baseAttrs.copts,
612 Cppflags: baseAttrs.cppFlags,
613 Conlyflags: baseAttrs.conlyFlags,
614 Asflags: baseAttrs.asFlags,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400615
Liz Kammere6583482021-10-19 13:56:10 -0400616 Deps: baseAttrs.implementationDeps,
617 Dynamic_deps: baseAttrs.implementationDynamicDeps,
618 Whole_archive_deps: baseAttrs.wholeArchiveDeps,
619 System_deps: baseAttrs.systemDynamicDeps,
Cole Faust6b29f592022-08-09 09:50:56 -0700620 Runtime_deps: baseAttrs.runtimeDeps,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400621
Liz Kammere6583482021-10-19 13:56:10 -0400622 Local_includes: baseAttrs.localIncludes,
623 Absolute_includes: baseAttrs.absoluteIncludes,
624 Linkopts: baseAttrs.linkopts,
625 Link_crt: baseAttrs.linkCrt,
626 Use_libcrt: baseAttrs.useLibcrt,
Yu Liua79c9462022-03-22 16:35:22 -0700627 Use_version_lib: baseAttrs.useVersionLib,
Liz Kammere6583482021-10-19 13:56:10 -0400628 Rtti: baseAttrs.rtti,
629 Stl: baseAttrs.stl,
630 Cpp_std: baseAttrs.cppStd,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400631
Liz Kammere6583482021-10-19 13:56:10 -0400632 Additional_linker_inputs: baseAttrs.additionalLinkerInputs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400633
634 Strip: stripAttributes{
Liz Kammere6583482021-10-19 13:56:10 -0400635 Keep_symbols: baseAttrs.stripKeepSymbols,
636 Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame,
637 Keep_symbols_list: baseAttrs.stripKeepSymbolsList,
638 All: baseAttrs.stripAll,
639 None: baseAttrs.stripNone,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400640 },
641
Chris Parsons58852a02021-12-09 18:10:18 -0500642 Features: baseAttrs.features,
Yu Liufc603162022-03-01 15:44:08 -0800643
644 sdkAttributes: bp2BuildParseSdkAttributes(m),
Liz Kammer2b8004b2021-10-04 13:55:44 -0400645 }
646
Sam Delmerico75539d62022-01-31 14:37:29 +0000647 ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400648 Rule_class: "cc_binary",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500649 Bzl_load_location: "//build/bazel/rules/cc:cc_binary.bzl",
Liz Kammer2b8004b2021-10-04 13:55:44 -0400650 },
651 android.CommonAttributes{Name: m.Name()},
Sam Delmerico75539d62022-01-31 14:37:29 +0000652 attrs)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400653}
654
655// binaryAttributes contains Bazel attributes corresponding to a cc binary
656type binaryAttributes struct {
657 binaryLinkerAttrs
658 Srcs bazel.LabelListAttribute
659 Srcs_c bazel.LabelListAttribute
660 Srcs_as bazel.LabelListAttribute
661
662 Copts bazel.StringListAttribute
663 Cppflags bazel.StringListAttribute
664 Conlyflags bazel.StringListAttribute
665 Asflags bazel.StringListAttribute
666
667 Deps bazel.LabelListAttribute
668 Dynamic_deps bazel.LabelListAttribute
669 Whole_archive_deps bazel.LabelListAttribute
670 System_deps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700671 Runtime_deps bazel.LabelListAttribute
Liz Kammer2b8004b2021-10-04 13:55:44 -0400672
673 Local_includes bazel.StringListAttribute
674 Absolute_includes bazel.StringListAttribute
675
676 Linkopts bazel.StringListAttribute
677 Additional_linker_inputs bazel.LabelListAttribute
678
Yu Liua79c9462022-03-22 16:35:22 -0700679 Link_crt bazel.BoolAttribute
680 Use_libcrt bazel.BoolAttribute
681 Use_version_lib bazel.BoolAttribute
Liz Kammer2b8004b2021-10-04 13:55:44 -0400682
683 Rtti bazel.BoolAttribute
684 Stl *string
685 Cpp_std *string
686
687 Strip stripAttributes
688
689 Features bazel.StringListAttribute
Yu Liufc603162022-03-01 15:44:08 -0800690
691 sdkAttributes
Liz Kammer2b8004b2021-10-04 13:55:44 -0400692}