blob: 3f951eca542fa5403b76323b6ee775b47b589143 [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 {
Colin Crossb916a382016-07-29 17:28:03 -070072 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070073 return module.Init()
74}
75
Patrice Arrudac249c712019-03-19 17:00:29 -070076// cc_binary_host produces a binary that is runnable on a host.
Liz Kammer2b8004b2021-10-04 13:55:44 -040077func BinaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070078 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 return module.Init()
80}
81
82//
83// Executables
84//
85
Chris Parsonse0f2ab32020-11-20 17:27:25 -050086// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070087type binaryDecorator struct {
88 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070089 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020090 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070091
92 Properties BinaryLinkerProperties
93
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070094 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080095
Colin Crossb60190a2018-09-04 16:28:17 -070096 // Location of the linked, unstripped binary
97 unstrippedOutputFile android.Path
98
Colin Cross9b09f242016-12-07 13:37:42 -080099 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
100 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -0800101
Colin Crossa6159012020-11-12 12:14:36 -0800102 // If the module has symlink_preferred_arch set, the name of the symlink to the
103 // binary for the preferred arch.
104 preferredArchSymlink string
105
Dan Willemsen581341d2017-02-09 16:16:31 -0800106 // Output archive of gcno coverage information
107 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800108
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000109 // Location of the files that should be copied to dist dir when requested
110 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900111
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500112 // Action command lines to run directly after the binary is installed. For example,
113 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800114 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115}
116
Colin Crossb916a382016-07-29 17:28:03 -0700117var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500119// linkerProps returns the list of individual properties objects relevant
120// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700121func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700122 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 &binary.Properties,
124 &binary.stripper.StripProperties)
125
126}
127
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500128// getStemWithoutSuffix returns the main section of the name to use for the symlink of
129// the main output file of this binary module. This may be derived from the module name
130// or other property overrides.
131// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100132func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700133 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800134 if String(binary.Properties.Stem) != "" {
135 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 }
137
Roland Levillain9efb8c72019-06-05 13:31:31 +0100138 return stem
139}
140
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500141// getStem returns the full name to use for the symlink of the main output file of this binary
142// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100143func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
144 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145}
146
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500147// linkerDeps augments and returns the given `deps` to contain dependencies on
148// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800149func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700150 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Crossd1a28132021-06-21 17:34:47 -0700151 if !Bool(binary.baseLinker.Properties.Nocrt) {
152 if binary.static() {
153 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
154 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
155 } else {
156 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
157 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 }
Colin Crossd1a28132021-06-21 17:34:47 -0700159 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160
Colin Cross450744e2021-08-18 13:52:09 -0700161 if binary.static() {
162 deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
163 }
164
Colin Crossd1a28132021-06-21 17:34:47 -0700165 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700166 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800167 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700168 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 }
170 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
171 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
172 // move them to the beginning of deps.LateStaticLibs
173 var groupLibs []string
174 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
175 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
176 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
177 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700178
179 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700180 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700181 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 }
183
Liz Kammer356f7d42021-01-26 09:18:53 -0500184 if !binary.static() && inList("libc", deps.StaticLibs) && !ctx.BazelConversionMode() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
186 "from static libs or set static_executable: true")
187 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800188
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189 return deps
190}
191
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500192// NewBinary builds and returns a new Module corresponding to a C++ binary.
193// Individual module implementations which comprise a C++ binary should call this function,
194// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700195func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700197 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700198 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700199 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 }
Colin Crossb916a382016-07-29 17:28:03 -0700201 module.compiler = NewBaseCompiler()
202 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700203 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000204
205 // Allow module to be added as member of an sdk/module_exports.
206 module.sdkMemberTypes = []android.SdkMemberType{
207 ccBinarySdkMemberType,
208 }
Colin Crossb916a382016-07-29 17:28:03 -0700209 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210}
211
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500212// linkerInit initializes dynamic properties of the linker (such as runpath) based
213// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700214func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700215 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216
Colin Crossa793db32021-08-18 13:53:58 -0700217 if !ctx.toolchain().Bionic() && !ctx.toolchain().Musl() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 if ctx.Os() == android.Linux {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500219 // Unless explicitly specified otherwise, host static binaries are built with -static
220 // if HostStaticBinaries is true for the product configuration.
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700221 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800222 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 }
Colin Crosscb0ac952021-07-20 13:17:15 -0700224 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700226 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 }
228 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229}
230
Colin Crossb916a382016-07-29 17:28:03 -0700231func (binary *binaryDecorator) static() bool {
232 return Bool(binary.Properties.Static_executable)
233}
234
235func (binary *binaryDecorator) staticBinary() bool {
236 return binary.static()
237}
238
Inseob Kim7f283f42020-06-01 21:53:49 +0900239func (binary *binaryDecorator) binary() bool {
240 return true
241}
242
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500243// linkerFlags returns a Flags object containing linker flags that are defined
244// by this binary, or that are implied by attributes of this binary. These flags are
245// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700246func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700247 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500249 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700250 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800251 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800252 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 }
254 }
255
256 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
257 // all code is position independent, and then those warnings get promoted to
258 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700259 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800260 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 }
262
Dan Willemsen2e47b342016-11-17 01:02:25 -0800263 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700264 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265 // Clang driver needs -static to create static executable.
266 // However, bionic/linker uses -shared to overwrite.
267 // Linker for x86 targets does not allow coexistance of -static and -shared,
268 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800269 if !inList("-shared", flags.Local.LdFlags) {
270 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 }
272
Colin Cross4af21ed2019-11-04 09:37:55 -0800273 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 "-nostdlib",
275 "-Bstatic",
276 "-Wl,--gc-sections",
277 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500278 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700280 if binary.Properties.DynamicLinker != "" {
281 flags.DynamicLinker = binary.Properties.DynamicLinker
282 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700283 switch ctx.Os() {
284 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700285 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900286 flags.DynamicLinker = "/system/bin/bootstrap/linker"
287 } else {
288 flags.DynamicLinker = "/system/bin/linker"
289 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700290 if flags.Toolchain.Is64Bit() {
291 flags.DynamicLinker += "64"
292 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700293 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700294 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700295 default:
296 ctx.ModuleErrorf("unknown dynamic linker")
297 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700298 }
299
300 if ctx.Os() == android.LinuxBionic {
301 // Use the dlwrap entry point, but keep _start around so
302 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800303 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700304 "-Wl,--entry=__dlwrap__start",
305 "-Wl,--undefined=_start",
306 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307 }
308 }
309
Colin Cross4af21ed2019-11-04 09:37:55 -0800310 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 "-pie",
312 "-nostdlib",
313 "-Bdynamic",
314 "-Wl,--gc-sections",
315 "-Wl,-z,nocopyreloc",
316 )
317 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500318 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700319 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800320 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 }
322 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800323 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324 }
325 }
326
327 return flags
328}
329
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500330// link registers actions to link this binary, and sets various fields
331// on this binary to reflect information that should be exported up the build
332// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700333func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700334 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335
336 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
337 outputFile := android.PathForModuleOut(ctx, fileName)
338 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339
340 var linkerDeps android.Paths
341
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800343 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000344 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800345 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 }
347
348 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200349 stripFlags := flagsToStripFlags(flags)
350 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800351 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200352 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800353 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700354 strippedOutputFile := outputFile
355 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200356 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700357 }
358
Colin Crossb60190a2018-09-04 16:28:17 -0700359 binary.unstrippedOutputFile = outputFile
360
Nan Zhang0007d812017-11-07 10:57:05 -0800361 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362 afterPrefixSymbols := outputFile
363 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500364 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200365 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 }
367
Colin Crossd7227f92019-09-05 14:26:33 -0700368 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
369
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500370 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800371 if Bool(binary.baseLinker.Properties.Use_version_lib) {
372 if ctx.Host() {
373 versionedOutputFile := outputFile
374 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
375 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
376 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500377 // When dist'ing a library or binary that has use_version_lib set, always
378 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800379 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000380 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800381
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200382 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800383 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000384 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200385 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800386 }
387
388 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
389 }
Colin Cross86803cf2018-02-15 14:12:26 -0800390 }
391
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700392 var validations android.Paths
Colin Crossf04eb992021-06-11 15:22:41 -0700393
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500394 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700395 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700396 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700397
398 if !deps.DynamicLinker.Valid() {
399 panic("Non-static host bionic modules must have a dynamic linker")
400 }
401
Colin Crossf04eb992021-06-11 15:22:41 -0700402 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
403 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700404 }
405
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800406 var sharedLibs android.Paths
407 // Ignore shared libs for static executables.
408 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900409 sharedLibs = deps.EarlySharedLibs
410 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800411 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900412 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800413 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
414 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -0800415 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800416 }
417
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700418 validations = append(validations, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700419 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700420
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500421 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500422 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700423 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700424 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700425
Dan Willemsen581341d2017-02-09 16:16:31 -0800426 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
427 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500428 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800429
Alex Light3d673592019-01-18 14:37:31 -0800430 // Need to determine symlinks early since some targets (ie APEX) need this
431 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800432 for _, symlink := range binary.Properties.Symlinks {
433 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800434 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800435 }
436
Nan Zhang0007d812017-11-07 10:57:05 -0800437 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100438 if String(binary.Properties.Suffix) == "" {
439 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800440 }
441 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500442 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800443 symlinkName := binary.getStemWithoutSuffix(ctx)
444 binary.symlinks = append(binary.symlinks, symlinkName)
445 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800446 }
447 }
448
Alex Light3d673592019-01-18 14:37:31 -0800449 return ret
450}
451
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900452func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
453 return binary.unstrippedOutputFile
454}
455
Alex Light3d673592019-01-18 14:37:31 -0800456func (binary *binaryDecorator) symlinkList() []string {
457 return binary.symlinks
458}
459
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700460func (binary *binaryDecorator) nativeCoverage() bool {
461 return true
462}
463
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900464func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
465 return binary.coverageOutputFile
466}
467
Jiyong Parkf1194352019-02-25 11:05:47 +0900468// /system/bin/linker -> /apex/com.android.runtime/bin/linker
469func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
470 dir := binary.baseInstaller.installDir(ctx)
471 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
472 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
473
474 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800475 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900476
477 for _, symlink := range binary.symlinks {
478 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800479 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900480 }
481}
482
Alex Light3d673592019-01-18 14:37:31 -0800483func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900484 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
485 // The original path becomes a symlink to the corresponding file in the
486 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700487 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700488 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700489 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
490 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700491
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700492 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900493 binary.installSymlinkToRuntimeApex(ctx, file)
494 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900495 binary.baseInstaller.subDir = "bootstrap"
496 }
Alex Light3d673592019-01-18 14:37:31 -0800497 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800498
499 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800500 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800501 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
502 binary.baseInstaller.path)
503 if symlink == binary.preferredArchSymlink {
504 // If this is the preferred arch symlink, save the installed path for use as the
505 // tool path.
506 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
507 }
Colin Cross9b09f242016-12-07 13:37:42 -0800508 }
509
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700510 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800511 // If the binary is multilib with a symlink to the preferred architecture, use the
512 // symlink instead of the binary because that's the more "canonical" name.
513 if preferredArchSymlinkPath.Valid() {
514 binary.toolPath = preferredArchSymlinkPath
515 } else {
516 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
517 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700518 }
519}
520
521func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
522 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523}
Dan Willemsena0790e32018-10-12 00:24:23 -0700524
525func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700526 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700527}
528
Colin Crossf04eb992021-06-11 15:22:41 -0700529var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700530 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700531 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
532 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700533 }, "linker")
534
Colin Crossf04eb992021-06-11 15:22:41 -0700535func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700536 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700537 Rule: verifyHostBionic,
538 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700539 Input: in,
540 Implicit: linker,
541 Output: out,
542 Args: map[string]string{
543 "linker": linker.String(),
544 },
545 })
546}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400547
548func init() {
549 android.RegisterBp2BuildMutator("cc_binary", BinaryBp2build)
550 android.RegisterBp2BuildMutator("cc_binary_host", BinaryHostBp2build)
551}
552
553func BinaryBp2build(ctx android.TopDownMutatorContext) {
554 binaryBp2build(ctx, "cc_binary")
555}
556
557func BinaryHostBp2build(ctx android.TopDownMutatorContext) {
558 binaryBp2build(ctx, "cc_binary_host")
559}
560
561func binaryBp2build(ctx android.TopDownMutatorContext, typ string) {
562 m, ok := ctx.Module().(*Module)
563 if !ok {
564 // Not a cc module
565 return
566 }
567 if !m.ConvertWithBp2build(ctx) {
568 return
569 }
570
571 if ctx.ModuleType() != typ {
572 return
573 }
574
575 var compatibleWith bazel.StringListAttribute
576 if typ == "cc_binary_host" {
577 //incompatible with android OS
578 compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, android.Android.Name, []string{"@platforms//:incompatible"})
579 compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, bazel.ConditionsDefaultConfigKey, []string{})
580 }
581
Liz Kammere6583482021-10-19 13:56:10 -0400582 baseAttrs := bp2BuildParseBaseProps(ctx, m)
Liz Kammer12615db2021-09-28 09:19:17 -0400583 binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m)
584
585 if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) {
586 baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency)
587 } else {
588 baseAttrs.implementationDeps.Add(baseAttrs.protoDependency)
589 }
Liz Kammer2b8004b2021-10-04 13:55:44 -0400590
591 attrs := &binaryAttributes{
Liz Kammer12615db2021-09-28 09:19:17 -0400592 binaryLinkerAttrs: binaryLinkerAttrs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400593
Liz Kammere6583482021-10-19 13:56:10 -0400594 Srcs: baseAttrs.srcs,
595 Srcs_c: baseAttrs.cSrcs,
596 Srcs_as: baseAttrs.asSrcs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400597
Liz Kammere6583482021-10-19 13:56:10 -0400598 Copts: baseAttrs.copts,
599 Cppflags: baseAttrs.cppFlags,
600 Conlyflags: baseAttrs.conlyFlags,
601 Asflags: baseAttrs.asFlags,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400602
Liz Kammere6583482021-10-19 13:56:10 -0400603 Deps: baseAttrs.implementationDeps,
604 Dynamic_deps: baseAttrs.implementationDynamicDeps,
605 Whole_archive_deps: baseAttrs.wholeArchiveDeps,
606 System_deps: baseAttrs.systemDynamicDeps,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400607
Liz Kammere6583482021-10-19 13:56:10 -0400608 Local_includes: baseAttrs.localIncludes,
609 Absolute_includes: baseAttrs.absoluteIncludes,
610 Linkopts: baseAttrs.linkopts,
611 Link_crt: baseAttrs.linkCrt,
612 Use_libcrt: baseAttrs.useLibcrt,
613 Rtti: baseAttrs.rtti,
614 Stl: baseAttrs.stl,
615 Cpp_std: baseAttrs.cppStd,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400616
Liz Kammere6583482021-10-19 13:56:10 -0400617 Additional_linker_inputs: baseAttrs.additionalLinkerInputs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400618
619 Strip: stripAttributes{
Liz Kammere6583482021-10-19 13:56:10 -0400620 Keep_symbols: baseAttrs.stripKeepSymbols,
621 Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame,
622 Keep_symbols_list: baseAttrs.stripKeepSymbolsList,
623 All: baseAttrs.stripAll,
624 None: baseAttrs.stripNone,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400625 },
626
627 Target_compatible_with: compatibleWith,
Liz Kammere6583482021-10-19 13:56:10 -0400628 Features: baseAttrs.features,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400629 }
630
631 ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
632 Rule_class: "cc_binary",
633 Bzl_load_location: "//build/bazel/rules:cc_binary.bzl",
634 },
635 android.CommonAttributes{Name: m.Name()},
636 attrs)
637}
638
639// binaryAttributes contains Bazel attributes corresponding to a cc binary
640type binaryAttributes struct {
641 binaryLinkerAttrs
642 Srcs bazel.LabelListAttribute
643 Srcs_c bazel.LabelListAttribute
644 Srcs_as bazel.LabelListAttribute
645
646 Copts bazel.StringListAttribute
647 Cppflags bazel.StringListAttribute
648 Conlyflags bazel.StringListAttribute
649 Asflags bazel.StringListAttribute
650
651 Deps bazel.LabelListAttribute
652 Dynamic_deps bazel.LabelListAttribute
653 Whole_archive_deps bazel.LabelListAttribute
654 System_deps bazel.LabelListAttribute
655
656 Local_includes bazel.StringListAttribute
657 Absolute_includes bazel.StringListAttribute
658
659 Linkopts bazel.StringListAttribute
660 Additional_linker_inputs bazel.LabelListAttribute
661
662 Link_crt bazel.BoolAttribute
663 Use_libcrt bazel.BoolAttribute
664
665 Rtti bazel.BoolAttribute
666 Stl *string
667 Cpp_std *string
668
669 Strip stripAttributes
670
671 Features bazel.StringListAttribute
672
673 Target_compatible_with bazel.StringListAttribute
674}