blob: 8be0d77eccac8abbe5de7a785427a4fa6ca102dd [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Jiyong Parkf1194352019-02-25 11:05:47 +090018 "path/filepath"
19
Dan Willemsena0790e32018-10-12 00:24:23 -070020 "github.com/google/blueprint"
21
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "android/soong/android"
Liz Kammer2b8004b2021-10-04 13:55:44 -040023 "android/soong/bazel"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
26type BinaryLinkerProperties struct {
27 // compile executable with -static
28 Static_executable *bool `android:"arch_variant"`
29
30 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080031 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070032
33 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080034 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070035
36 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080037 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070038
39 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010040 Symlink_preferred_arch *bool `android:"arch_variant"`
Colin Cross522e3732016-09-07 13:14:06 -070041
Colin Cross9b09f242016-12-07 13:37:42 -080042 // install symlinks to the binary. Symlink names will have the suffix and the binary
43 // extension (if any) appended
44 Symlinks []string `android:"arch_variant"`
45
Chris Parsonse0f2ab32020-11-20 17:27:25 -050046 // override the dynamic linker
Colin Cross522e3732016-09-07 13:14:06 -070047 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070048
49 // Names of modules to be overridden. Listed modules can only be other binaries
50 // (in Make or Soong).
51 // This does not completely prevent installation of the overridden binaries, but if both
52 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
53 // from PRODUCT_PACKAGES.
54 Overrides []string
Colin Crossd7227f92019-09-05 14:26:33 -070055
56 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
57 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070058}
59
60func init() {
Paul Duffin2ee69792020-01-16 12:14:42 +000061 RegisterBinaryBuildComponents(android.InitRegistrationContext)
62}
63
64func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
65 ctx.RegisterModuleType("cc_binary", BinaryFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040066 ctx.RegisterModuleType("cc_binary_host", BinaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070067}
68
Patrice Arrudac249c712019-03-19 17:00:29 -070069// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090070func BinaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070071 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070072 return module.Init()
73}
74
Patrice Arrudac249c712019-03-19 17:00:29 -070075// cc_binary_host produces a binary that is runnable on a host.
Liz Kammer2b8004b2021-10-04 13:55:44 -040076func BinaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070077 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 return module.Init()
79}
80
81//
82// Executables
83//
84
Chris Parsonse0f2ab32020-11-20 17:27:25 -050085// binaryDecorator is a decorator containing information for C++ binary modules.
Colin Crossb916a382016-07-29 17:28:03 -070086type binaryDecorator struct {
87 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070088 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020089 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070090
91 Properties BinaryLinkerProperties
92
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070093 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080094
Colin Crossb60190a2018-09-04 16:28:17 -070095 // Location of the linked, unstripped binary
96 unstrippedOutputFile android.Path
97
Colin Cross9b09f242016-12-07 13:37:42 -080098 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
99 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -0800100
Colin Crossa6159012020-11-12 12:14:36 -0800101 // If the module has symlink_preferred_arch set, the name of the symlink to the
102 // binary for the preferred arch.
103 preferredArchSymlink string
104
Dan Willemsen581341d2017-02-09 16:16:31 -0800105 // Output archive of gcno coverage information
106 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800107
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000108 // Location of the files that should be copied to dist dir when requested
109 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900110
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500111 // Action command lines to run directly after the binary is installed. For example,
112 // may be used to symlink runtime dependencies (such as bionic) alongside installation.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800113 postInstallCmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114}
115
Colin Crossb916a382016-07-29 17:28:03 -0700116var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500118// linkerProps returns the list of individual properties objects relevant
119// for this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700120func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700121 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122 &binary.Properties,
123 &binary.stripper.StripProperties)
124
125}
126
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500127// getStemWithoutSuffix returns the main section of the name to use for the symlink of
128// the main output file of this binary module. This may be derived from the module name
129// or other property overrides.
130// For the full symlink name, the `Suffix` property of a binary module must be appended.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100131func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700132 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800133 if String(binary.Properties.Stem) != "" {
134 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 }
136
Roland Levillain9efb8c72019-06-05 13:31:31 +0100137 return stem
138}
139
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500140// getStem returns the full name to use for the symlink of the main output file of this binary
141// module. This may be derived from the module name and/or other property overrides.
Roland Levillain9efb8c72019-06-05 13:31:31 +0100142func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
143 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144}
145
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500146// linkerDeps augments and returns the given `deps` to contain dependencies on
147// modules common to most binaries, such as bionic libraries.
Colin Cross37047f12016-12-13 17:06:13 -0800148func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700149 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Crossd1a28132021-06-21 17:34:47 -0700150 if !Bool(binary.baseLinker.Properties.Nocrt) {
151 if binary.static() {
152 deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
153 deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
154 } else {
155 deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
156 deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 }
Colin Crossd1a28132021-06-21 17:34:47 -0700158 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159
Colin Cross450744e2021-08-18 13:52:09 -0700160 if binary.static() {
161 deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
162 }
163
Colin Crossd1a28132021-06-21 17:34:47 -0700164 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700165 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800166 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700167 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 }
169 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
170 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
171 // move them to the beginning of deps.LateStaticLibs
172 var groupLibs []string
173 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
174 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
175 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
176 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700177
178 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700179 deps.DynamicLinker = "linker"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700180 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 }
182
Liz Kammer356f7d42021-01-26 09:18:53 -0500183 if !binary.static() && inList("libc", deps.StaticLibs) && !ctx.BazelConversionMode() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
185 "from static libs or set static_executable: true")
186 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800187
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 return deps
189}
190
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500191// NewBinary builds and returns a new Module corresponding to a C++ binary.
192// Individual module implementations which comprise a C++ binary should call this function,
193// set some fields on the result, and then call the Init function.
Colin Crossb916a382016-07-29 17:28:03 -0700194func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700196 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700197 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700198 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 }
Colin Crossb916a382016-07-29 17:28:03 -0700200 module.compiler = NewBaseCompiler()
201 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700202 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000203
204 // Allow module to be added as member of an sdk/module_exports.
205 module.sdkMemberTypes = []android.SdkMemberType{
206 ccBinarySdkMemberType,
207 }
Colin Crossb916a382016-07-29 17:28:03 -0700208 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209}
210
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500211// linkerInit initializes dynamic properties of the linker (such as runpath) based
212// on properties of this binary.
Colin Crossb916a382016-07-29 17:28:03 -0700213func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700214 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215
Colin Crossa793db32021-08-18 13:53:58 -0700216 if !ctx.toolchain().Bionic() && !ctx.toolchain().Musl() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 if ctx.Os() == android.Linux {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500218 // Unless explicitly specified otherwise, host static binaries are built with -static
219 // if HostStaticBinaries is true for the product configuration.
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700220 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800221 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
Colin Crosscb0ac952021-07-20 13:17:15 -0700223 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700225 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 }
227 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (binary *binaryDecorator) static() bool {
231 return Bool(binary.Properties.Static_executable)
232}
233
234func (binary *binaryDecorator) staticBinary() bool {
235 return binary.static()
236}
237
Inseob Kim7f283f42020-06-01 21:53:49 +0900238func (binary *binaryDecorator) binary() bool {
239 return true
240}
241
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500242// linkerFlags returns a Flags object containing linker flags that are defined
243// by this binary, or that are implied by attributes of this binary. These flags are
244// combined with the given flags.
Colin Crossb916a382016-07-29 17:28:03 -0700245func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700246 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500248 // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
Colin Cross446c6662018-09-14 16:00:16 -0700249 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800250 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800251 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 }
253 }
254
255 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
256 // all code is position independent, and then those warnings get promoted to
257 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700258 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800259 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 }
261
Dan Willemsen2e47b342016-11-17 01:02:25 -0800262 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700263 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264 // Clang driver needs -static to create static executable.
265 // However, bionic/linker uses -shared to overwrite.
266 // Linker for x86 targets does not allow coexistance of -static and -shared,
267 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800268 if !inList("-shared", flags.Local.LdFlags) {
269 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 }
271
Colin Cross4af21ed2019-11-04 09:37:55 -0800272 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 "-nostdlib",
274 "-Bstatic",
275 "-Wl,--gc-sections",
276 )
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500277 } else { // not static
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700279 if binary.Properties.DynamicLinker != "" {
280 flags.DynamicLinker = binary.Properties.DynamicLinker
281 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700282 switch ctx.Os() {
283 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700284 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900285 flags.DynamicLinker = "/system/bin/bootstrap/linker"
286 } else {
287 flags.DynamicLinker = "/system/bin/linker"
288 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700289 if flags.Toolchain.Is64Bit() {
290 flags.DynamicLinker += "64"
291 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700292 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700293 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700294 default:
295 ctx.ModuleErrorf("unknown dynamic linker")
296 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700297 }
298
299 if ctx.Os() == android.LinuxBionic {
300 // Use the dlwrap entry point, but keep _start around so
301 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800302 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700303 "-Wl,--entry=__dlwrap__start",
304 "-Wl,--undefined=_start",
305 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 }
307 }
308
Colin Cross4af21ed2019-11-04 09:37:55 -0800309 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 "-pie",
311 "-nostdlib",
312 "-Bdynamic",
313 "-Wl,--gc-sections",
314 "-Wl,-z,nocopyreloc",
315 )
316 }
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500317 } else { // not bionic
Colin Crossb916a382016-07-29 17:28:03 -0700318 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800319 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 }
321 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800322 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 }
324 }
325
326 return flags
327}
328
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500329// link registers actions to link this binary, and sets various fields
330// on this binary to reflect information that should be exported up the build
331// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -0700332func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700333 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334
335 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
336 outputFile := android.PathForModuleOut(ctx, fileName)
337 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700338
339 var linkerDeps android.Paths
340
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800342 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Colin Cross528d67e2021-07-23 22:23:07 +0000343 } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800344 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 }
346
347 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200348 stripFlags := flagsToStripFlags(flags)
349 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800350 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200351 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800352 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 strippedOutputFile := outputFile
354 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200355 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700356 }
357
Colin Crossb60190a2018-09-04 16:28:17 -0700358 binary.unstrippedOutputFile = outputFile
359
Nan Zhang0007d812017-11-07 10:57:05 -0800360 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 afterPrefixSymbols := outputFile
362 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500363 transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200364 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 }
366
Colin Crossd7227f92019-09-05 14:26:33 -0700367 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
368
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500369 // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
Dan Willemsen569edc52018-11-19 09:33:29 -0800370 if Bool(binary.baseLinker.Properties.Use_version_lib) {
371 if ctx.Host() {
372 versionedOutputFile := outputFile
373 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
374 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
375 } else {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500376 // When dist'ing a library or binary that has use_version_lib set, always
377 // distribute the stamped version, even for the device.
Dan Willemsen569edc52018-11-19 09:33:29 -0800378 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000379 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800380
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200381 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800382 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000383 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200384 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800385 }
386
387 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
388 }
Colin Cross86803cf2018-02-15 14:12:26 -0800389 }
390
Colin Crossf04eb992021-06-11 15:22:41 -0700391 var validations android.WritablePaths
392
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500393 // Handle host bionic linker symbols.
Dan Willemsena0790e32018-10-12 00:24:23 -0700394 if ctx.Os() == android.LinuxBionic && !binary.static() {
Colin Crossf04eb992021-06-11 15:22:41 -0700395 verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
Dan Willemsena0790e32018-10-12 00:24:23 -0700396
397 if !deps.DynamicLinker.Valid() {
398 panic("Non-static host bionic modules must have a dynamic linker")
399 }
400
Colin Crossf04eb992021-06-11 15:22:41 -0700401 binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
402 validations = append(validations, verifyFile)
Dan Willemsena0790e32018-10-12 00:24:23 -0700403 }
404
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800405 var sharedLibs android.Paths
406 // Ignore shared libs for static executables.
407 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900408 sharedLibs = deps.EarlySharedLibs
409 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800410 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900411 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800412 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
413 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -0800414 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800415 }
416
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700417 validations = append(validations, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700418 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700419
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500420 // Register link action.
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500421 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700422 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossf04eb992021-06-11 15:22:41 -0700423 builderFlags, outputFile, nil, validations)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700424
Dan Willemsen581341d2017-02-09 16:16:31 -0800425 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
426 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500427 binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800428
Alex Light3d673592019-01-18 14:37:31 -0800429 // Need to determine symlinks early since some targets (ie APEX) need this
430 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800431 for _, symlink := range binary.Properties.Symlinks {
432 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800433 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800434 }
435
Nan Zhang0007d812017-11-07 10:57:05 -0800436 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100437 if String(binary.Properties.Suffix) == "" {
438 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800439 }
440 if ctx.TargetPrimary() {
Chris Parsonse0f2ab32020-11-20 17:27:25 -0500441 // Install a symlink to the preferred architecture
Colin Crossa6159012020-11-12 12:14:36 -0800442 symlinkName := binary.getStemWithoutSuffix(ctx)
443 binary.symlinks = append(binary.symlinks, symlinkName)
444 binary.preferredArchSymlink = symlinkName
Colin Cross9b09f242016-12-07 13:37:42 -0800445 }
446 }
447
Alex Light3d673592019-01-18 14:37:31 -0800448 return ret
449}
450
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900451func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
452 return binary.unstrippedOutputFile
453}
454
Alex Light3d673592019-01-18 14:37:31 -0800455func (binary *binaryDecorator) symlinkList() []string {
456 return binary.symlinks
457}
458
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700459func (binary *binaryDecorator) nativeCoverage() bool {
460 return true
461}
462
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900463func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
464 return binary.coverageOutputFile
465}
466
Jiyong Parkf1194352019-02-25 11:05:47 +0900467// /system/bin/linker -> /apex/com.android.runtime/bin/linker
468func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
469 dir := binary.baseInstaller.installDir(ctx)
470 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
471 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
472
473 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800474 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900475
476 for _, symlink := range binary.symlinks {
477 ctx.InstallAbsoluteSymlink(dir, symlink, target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800478 binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
Jiyong Parkf1194352019-02-25 11:05:47 +0900479 }
480}
481
Alex Light3d673592019-01-18 14:37:31 -0800482func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900483 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
484 // The original path becomes a symlink to the corresponding file in the
485 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700486 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700487 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700488 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
489 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700490
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700491 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900492 binary.installSymlinkToRuntimeApex(ctx, file)
493 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900494 binary.baseInstaller.subDir = "bootstrap"
495 }
Alex Light3d673592019-01-18 14:37:31 -0800496 binary.baseInstaller.install(ctx, file)
Colin Crossa6159012020-11-12 12:14:36 -0800497
498 var preferredArchSymlinkPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -0800499 for _, symlink := range binary.symlinks {
Colin Crossa6159012020-11-12 12:14:36 -0800500 installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
501 binary.baseInstaller.path)
502 if symlink == binary.preferredArchSymlink {
503 // If this is the preferred arch symlink, save the installed path for use as the
504 // tool path.
505 preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
506 }
Colin Cross9b09f242016-12-07 13:37:42 -0800507 }
508
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700509 if ctx.Os().Class == android.Host {
Colin Crossa6159012020-11-12 12:14:36 -0800510 // If the binary is multilib with a symlink to the preferred architecture, use the
511 // symlink instead of the binary because that's the more "canonical" name.
512 if preferredArchSymlinkPath.Valid() {
513 binary.toolPath = preferredArchSymlinkPath
514 } else {
515 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
516 }
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700517 }
518}
519
520func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
521 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522}
Dan Willemsena0790e32018-10-12 00:24:23 -0700523
524func init() {
Colin Crossf04eb992021-06-11 15:22:41 -0700525 pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
Dan Willemsena0790e32018-10-12 00:24:23 -0700526}
527
Colin Crossf04eb992021-06-11 15:22:41 -0700528var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700529 blueprint.RuleParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700530 Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
531 CommandDeps: []string{"$verifyHostBionicCmd"},
Dan Willemsena0790e32018-10-12 00:24:23 -0700532 }, "linker")
533
Colin Crossf04eb992021-06-11 15:22:41 -0700534func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
Dan Willemsena0790e32018-10-12 00:24:23 -0700535 ctx.Build(pctx, android.BuildParams{
Colin Crossf04eb992021-06-11 15:22:41 -0700536 Rule: verifyHostBionic,
537 Description: "verify host bionic",
Dan Willemsena0790e32018-10-12 00:24:23 -0700538 Input: in,
539 Implicit: linker,
540 Output: out,
541 Args: map[string]string{
542 "linker": linker.String(),
543 },
544 })
545}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400546
547func init() {
548 android.RegisterBp2BuildMutator("cc_binary", BinaryBp2build)
549 android.RegisterBp2BuildMutator("cc_binary_host", BinaryHostBp2build)
550}
551
552func BinaryBp2build(ctx android.TopDownMutatorContext) {
553 binaryBp2build(ctx, "cc_binary")
554}
555
556func BinaryHostBp2build(ctx android.TopDownMutatorContext) {
557 binaryBp2build(ctx, "cc_binary_host")
558}
559
560func binaryBp2build(ctx android.TopDownMutatorContext, typ string) {
561 m, ok := ctx.Module().(*Module)
562 if !ok {
563 // Not a cc module
564 return
565 }
566 if !m.ConvertWithBp2build(ctx) {
567 return
568 }
569
570 if ctx.ModuleType() != typ {
571 return
572 }
573
574 var compatibleWith bazel.StringListAttribute
575 if typ == "cc_binary_host" {
576 //incompatible with android OS
577 compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, android.Android.Name, []string{"@platforms//:incompatible"})
578 compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, bazel.ConditionsDefaultConfigKey, []string{})
579 }
580
Liz Kammere6583482021-10-19 13:56:10 -0400581 baseAttrs := bp2BuildParseBaseProps(ctx, m)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400582
583 attrs := &binaryAttributes{
584 binaryLinkerAttrs: bp2buildBinaryLinkerProps(ctx, m),
585
Liz Kammere6583482021-10-19 13:56:10 -0400586 Srcs: baseAttrs.srcs,
587 Srcs_c: baseAttrs.cSrcs,
588 Srcs_as: baseAttrs.asSrcs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400589
Liz Kammere6583482021-10-19 13:56:10 -0400590 Copts: baseAttrs.copts,
591 Cppflags: baseAttrs.cppFlags,
592 Conlyflags: baseAttrs.conlyFlags,
593 Asflags: baseAttrs.asFlags,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400594
Liz Kammere6583482021-10-19 13:56:10 -0400595 Deps: baseAttrs.implementationDeps,
596 Dynamic_deps: baseAttrs.implementationDynamicDeps,
597 Whole_archive_deps: baseAttrs.wholeArchiveDeps,
598 System_deps: baseAttrs.systemDynamicDeps,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400599
Liz Kammere6583482021-10-19 13:56:10 -0400600 Local_includes: baseAttrs.localIncludes,
601 Absolute_includes: baseAttrs.absoluteIncludes,
602 Linkopts: baseAttrs.linkopts,
603 Link_crt: baseAttrs.linkCrt,
604 Use_libcrt: baseAttrs.useLibcrt,
605 Rtti: baseAttrs.rtti,
606 Stl: baseAttrs.stl,
607 Cpp_std: baseAttrs.cppStd,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400608
Liz Kammere6583482021-10-19 13:56:10 -0400609 Additional_linker_inputs: baseAttrs.additionalLinkerInputs,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400610
611 Strip: stripAttributes{
Liz Kammere6583482021-10-19 13:56:10 -0400612 Keep_symbols: baseAttrs.stripKeepSymbols,
613 Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame,
614 Keep_symbols_list: baseAttrs.stripKeepSymbolsList,
615 All: baseAttrs.stripAll,
616 None: baseAttrs.stripNone,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400617 },
618
619 Target_compatible_with: compatibleWith,
Liz Kammere6583482021-10-19 13:56:10 -0400620 Features: baseAttrs.features,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400621 }
622
623 ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
624 Rule_class: "cc_binary",
625 Bzl_load_location: "//build/bazel/rules:cc_binary.bzl",
626 },
627 android.CommonAttributes{Name: m.Name()},
628 attrs)
629}
630
631// binaryAttributes contains Bazel attributes corresponding to a cc binary
632type binaryAttributes struct {
633 binaryLinkerAttrs
634 Srcs bazel.LabelListAttribute
635 Srcs_c bazel.LabelListAttribute
636 Srcs_as bazel.LabelListAttribute
637
638 Copts bazel.StringListAttribute
639 Cppflags bazel.StringListAttribute
640 Conlyflags bazel.StringListAttribute
641 Asflags bazel.StringListAttribute
642
643 Deps bazel.LabelListAttribute
644 Dynamic_deps bazel.LabelListAttribute
645 Whole_archive_deps bazel.LabelListAttribute
646 System_deps bazel.LabelListAttribute
647
648 Local_includes bazel.StringListAttribute
649 Absolute_includes bazel.StringListAttribute
650
651 Linkopts bazel.StringListAttribute
652 Additional_linker_inputs bazel.LabelListAttribute
653
654 Link_crt bazel.BoolAttribute
655 Use_libcrt bazel.BoolAttribute
656
657 Rtti bazel.BoolAttribute
658 Stl *string
659 Cpp_std *string
660
661 Strip stripAttributes
662
663 Features bazel.StringListAttribute
664
665 Target_compatible_with bazel.StringListAttribute
666}