Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 18 | "path/filepath" |
| 19 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 22 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 24 | "android/soong/bazel" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | type BinaryLinkerProperties struct { |
| 28 | // compile executable with -static |
| 29 | Static_executable *bool `android:"arch_variant"` |
| 30 | |
| 31 | // set the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 32 | Stem *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 33 | |
| 34 | // append to the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 35 | Suffix *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 36 | |
| 37 | // if set, add an extra objcopy --prefix-symbols= step |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 38 | Prefix_symbols *string |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 39 | |
| 40 | // if set, install a symlink to the preferred architecture |
Roland Levillain | d9bf9be | 2019-06-05 19:20:33 +0100 | [diff] [blame] | 41 | Symlink_preferred_arch *bool `android:"arch_variant"` |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 42 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 43 | // 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 Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 47 | // override the dynamic linker |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 48 | DynamicLinker string `blueprint:"mutated"` |
Yifan Hong | 946e32e | 2018-04-03 13:22:50 -0700 | [diff] [blame] | 49 | |
| 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 Cross | d7227f9 | 2019-09-05 14:26:33 -0700 | [diff] [blame] | 56 | |
| 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func init() { |
Paul Duffin | 2ee6979 | 2020-01-16 12:14:42 +0000 | [diff] [blame] | 62 | RegisterBinaryBuildComponents(android.InitRegistrationContext) |
| 63 | } |
| 64 | |
| 65 | func RegisterBinaryBuildComponents(ctx android.RegistrationContext) { |
| 66 | ctx.RegisterModuleType("cc_binary", BinaryFactory) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 67 | ctx.RegisterModuleType("cc_binary_host", BinaryHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 70 | // cc_binary produces a binary that is runnable on a device. |
Jiyong Park | 16e91a0 | 2018-12-20 18:18:08 +0900 | [diff] [blame] | 71 | func BinaryFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 72 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 73 | return module.Init() |
| 74 | } |
| 75 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 76 | // cc_binary_host produces a binary that is runnable on a host. |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 77 | func BinaryHostFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 78 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 79 | return module.Init() |
| 80 | } |
| 81 | |
| 82 | // |
| 83 | // Executables |
| 84 | // |
| 85 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 86 | // binaryDecorator is a decorator containing information for C++ binary modules. |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 87 | type binaryDecorator struct { |
| 88 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 89 | *baseInstaller |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 90 | stripper Stripper |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 91 | |
| 92 | Properties BinaryLinkerProperties |
| 93 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 94 | toolPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 95 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 96 | // Location of the linked, unstripped binary |
| 97 | unstrippedOutputFile android.Path |
| 98 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 99 | // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS |
| 100 | symlinks []string |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 101 | |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame] | 102 | // 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 Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 106 | // Output archive of gcno coverage information |
| 107 | coverageOutputFile android.OptionalPath |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 108 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 109 | // Location of the files that should be copied to dist dir when requested |
| 110 | distFiles android.TaggedDistFiles |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 111 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 112 | // 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 Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 114 | postInstallCmds []string |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 117 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 118 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 119 | // linkerProps returns the list of individual properties objects relevant |
| 120 | // for this binary. |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 121 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 122 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 123 | &binary.Properties, |
| 124 | &binary.stripper.StripProperties) |
| 125 | |
| 126 | } |
| 127 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 128 | // 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 Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 132 | func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 133 | stem := ctx.baseModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 134 | if String(binary.Properties.Stem) != "" { |
| 135 | stem = String(binary.Properties.Stem) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Roland Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 138 | return stem |
| 139 | } |
| 140 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 141 | // 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 Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 143 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
| 144 | return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 147 | // linkerDeps augments and returns the given `deps` to contain dependencies on |
| 148 | // modules common to most binaries, such as bionic libraries. |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 149 | func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 150 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 151 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | } |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 159 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 160 | |
Colin Cross | 450744e | 2021-08-18 13:52:09 -0700 | [diff] [blame] | 161 | if binary.static() { |
| 162 | deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...) |
| 163 | } |
| 164 | |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 165 | if ctx.toolchain().Bionic() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 166 | if binary.static() { |
Dan Albert | dc2597d | 2017-01-26 17:44:26 -0800 | [diff] [blame] | 167 | if ctx.selectedStl() == "libc++_static" { |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 168 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 169 | } |
| 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 Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 178 | |
| 179 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 180 | deps.DynamicLinker = "linker" |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 181 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 184 | if !binary.static() && inList("libc", deps.StaticLibs) && !ctx.BazelConversionMode() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 185 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 186 | "from static libs or set static_executable: true") |
| 187 | } |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 188 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 189 | return deps |
| 190 | } |
| 191 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 192 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 197 | binary := &binaryDecorator{ |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 198 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 199 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 201 | module.compiler = NewBaseCompiler() |
| 202 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 203 | module.installer = binary |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 204 | |
| 205 | // Allow module to be added as member of an sdk/module_exports. |
| 206 | module.sdkMemberTypes = []android.SdkMemberType{ |
| 207 | ccBinarySdkMemberType, |
| 208 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 209 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 212 | // linkerInit initializes dynamic properties of the linker (such as runpath) based |
| 213 | // on properties of this binary. |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 214 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 215 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 216 | |
Colin Cross | a793db3 | 2021-08-18 13:53:58 -0700 | [diff] [blame] | 217 | if !ctx.toolchain().Bionic() && !ctx.toolchain().Musl() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 218 | if ctx.Os() == android.Linux { |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 219 | // Unless explicitly specified otherwise, host static binaries are built with -static |
| 220 | // if HostStaticBinaries is true for the product configuration. |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 221 | if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 222 | binary.Properties.Static_executable = BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 223 | } |
Colin Cross | cb0ac95 | 2021-07-20 13:17:15 -0700 | [diff] [blame] | 224 | } else { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 225 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 226 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 231 | func (binary *binaryDecorator) static() bool { |
| 232 | return Bool(binary.Properties.Static_executable) |
| 233 | } |
| 234 | |
| 235 | func (binary *binaryDecorator) staticBinary() bool { |
| 236 | return binary.static() |
| 237 | } |
| 238 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 239 | func (binary *binaryDecorator) binary() bool { |
| 240 | return true |
| 241 | } |
| 242 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 243 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 246 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 247 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 248 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 249 | // Passing -pie to clang for Windows binaries causes a warning that -pie is unused. |
Colin Cross | 446c666 | 2018-09-14 16:00:16 -0700 | [diff] [blame] | 250 | if ctx.Host() && !ctx.Windows() && !binary.static() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 251 | if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 252 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 253 | } |
| 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 Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 259 | if !ctx.Windows() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 260 | flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 263 | if ctx.toolchain().Bionic() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 264 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 265 | // 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 Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 269 | if !inList("-shared", flags.Local.LdFlags) { |
| 270 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-static") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 273 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 274 | "-nostdlib", |
| 275 | "-Bstatic", |
| 276 | "-Wl,--gc-sections", |
| 277 | ) |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 278 | } else { // not static |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 279 | if flags.DynamicLinker == "" { |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 280 | if binary.Properties.DynamicLinker != "" { |
| 281 | flags.DynamicLinker = binary.Properties.DynamicLinker |
| 282 | } else { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 283 | switch ctx.Os() { |
| 284 | case android.Android: |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 285 | if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() { |
Jiyong Park | a4b9dd0 | 2019-01-16 22:53:13 +0900 | [diff] [blame] | 286 | flags.DynamicLinker = "/system/bin/bootstrap/linker" |
| 287 | } else { |
| 288 | flags.DynamicLinker = "/system/bin/linker" |
| 289 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 290 | if flags.Toolchain.Is64Bit() { |
| 291 | flags.DynamicLinker += "64" |
| 292 | } |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 293 | case android.LinuxBionic: |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 294 | flags.DynamicLinker = "" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 295 | default: |
| 296 | ctx.ModuleErrorf("unknown dynamic linker") |
| 297 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 298 | } |
| 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 Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 303 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 304 | "-Wl,--entry=__dlwrap__start", |
| 305 | "-Wl,--undefined=_start", |
| 306 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 310 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 311 | "-pie", |
| 312 | "-nostdlib", |
| 313 | "-Bdynamic", |
| 314 | "-Wl,--gc-sections", |
| 315 | "-Wl,-z,nocopyreloc", |
| 316 | ) |
| 317 | } |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 318 | } else { // not bionic |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 319 | if binary.static() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 320 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-static") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 321 | } |
| 322 | if ctx.Darwin() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 323 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | return flags |
| 328 | } |
| 329 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 330 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 333 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 334 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 335 | |
| 336 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 337 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 338 | ret := outputFile |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 339 | |
| 340 | var linkerDeps android.Paths |
| 341 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 342 | if flags.DynamicLinker != "" { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 343 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker) |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 344 | } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 345 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame^] | 348 | if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() { |
| 349 | fatOutputFile := outputFile |
| 350 | outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName) |
| 351 | transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path()) |
| 352 | } |
| 353 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 354 | builderFlags := flagsToBuilderFlags(flags) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 355 | stripFlags := flagsToStripFlags(flags) |
| 356 | if binary.stripper.NeedsStrip(ctx) { |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 357 | if ctx.Darwin() { |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 358 | stripFlags.StripUseGnuStrip = true |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 359 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 360 | strippedOutputFile := outputFile |
| 361 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 362 | binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 365 | binary.unstrippedOutputFile = outputFile |
| 366 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 367 | if String(binary.Properties.Prefix_symbols) != "" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 368 | afterPrefixSymbols := outputFile |
| 369 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 370 | transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile, |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 371 | builderFlags, afterPrefixSymbols) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Colin Cross | d7227f9 | 2019-09-05 14:26:33 -0700 | [diff] [blame] | 374 | outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName) |
| 375 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 376 | // If use_version_lib is true, make an android::build::GetBuildNumber() function available. |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 377 | if Bool(binary.baseLinker.Properties.Use_version_lib) { |
| 378 | if ctx.Host() { |
| 379 | versionedOutputFile := outputFile |
| 380 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 381 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 382 | } else { |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 383 | // When dist'ing a library or binary that has use_version_lib set, always |
| 384 | // distribute the stamped version, even for the device. |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 385 | versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 386 | binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile) |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 387 | |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 388 | if binary.stripper.NeedsStrip(ctx) { |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 389 | out := android.PathForModuleOut(ctx, "versioned-stripped", fileName) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 390 | binary.distFiles = android.MakeDefaultDistFiles(out) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 391 | binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags) |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 395 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 398 | var validations android.WritablePaths |
| 399 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 400 | // Handle host bionic linker symbols. |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 401 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 402 | verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp") |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 403 | |
| 404 | if !deps.DynamicLinker.Valid() { |
| 405 | panic("Non-static host bionic modules must have a dynamic linker") |
| 406 | } |
| 407 | |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 408 | binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile) |
| 409 | validations = append(validations, verifyFile) |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 412 | var sharedLibs android.Paths |
| 413 | // Ignore shared libs for static executables. |
| 414 | if !binary.static() { |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 415 | sharedLibs = deps.EarlySharedLibs |
| 416 | sharedLibs = append(sharedLibs, deps.SharedLibs...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 417 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 418 | linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 419 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 420 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
Chih-Hung Hsieh | f6ca1b9 | 2021-12-05 18:02:50 -0800 | [diff] [blame] | 421 | linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Chih-Hung Hsieh | 3bb934f | 2021-10-25 11:09:19 -0700 | [diff] [blame] | 424 | validations = append(validations, objs.tidyFiles...) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 425 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 426 | |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 427 | // Register link action. |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 428 | transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 429 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 430 | builderFlags, outputFile, nil, validations) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 431 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 432 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) |
| 433 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 434 | binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx)) |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 435 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 436 | // Need to determine symlinks early since some targets (ie APEX) need this |
| 437 | // information but will not call 'install' |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 438 | for _, symlink := range binary.Properties.Symlinks { |
| 439 | binary.symlinks = append(binary.symlinks, |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 440 | symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix()) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 443 | if Bool(binary.Properties.Symlink_preferred_arch) { |
Roland Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 444 | if String(binary.Properties.Suffix) == "" { |
| 445 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix") |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 446 | } |
| 447 | if ctx.TargetPrimary() { |
Chris Parsons | e0f2ab3 | 2020-11-20 17:27:25 -0500 | [diff] [blame] | 448 | // Install a symlink to the preferred architecture |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame] | 449 | symlinkName := binary.getStemWithoutSuffix(ctx) |
| 450 | binary.symlinks = append(binary.symlinks, symlinkName) |
| 451 | binary.preferredArchSymlink = symlinkName |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 455 | return ret |
| 456 | } |
| 457 | |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 458 | func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path { |
| 459 | return binary.unstrippedOutputFile |
| 460 | } |
| 461 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 462 | func (binary *binaryDecorator) symlinkList() []string { |
| 463 | return binary.symlinks |
| 464 | } |
| 465 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 466 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 467 | return true |
| 468 | } |
| 469 | |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 470 | func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath { |
| 471 | return binary.coverageOutputFile |
| 472 | } |
| 473 | |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 474 | // /system/bin/linker -> /apex/com.android.runtime/bin/linker |
| 475 | func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) { |
| 476 | dir := binary.baseInstaller.installDir(ctx) |
| 477 | dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir) |
| 478 | target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base()) |
| 479 | |
| 480 | ctx.InstallAbsoluteSymlink(dir, file.Base(), target) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 481 | binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target)) |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 482 | |
| 483 | for _, symlink := range binary.symlinks { |
| 484 | ctx.InstallAbsoluteSymlink(dir, symlink, target) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 485 | binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target)) |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 489 | func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) { |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 490 | // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory. |
| 491 | // The original path becomes a symlink to the corresponding file in the |
| 492 | // runtime APEX. |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 493 | translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 494 | if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() && |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 495 | !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() && |
| 496 | !ctx.inVendorRamdisk() { |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 497 | |
Peter Collingbourne | 3478bb2 | 2019-04-24 14:41:12 -0700 | [diff] [blame] | 498 | if ctx.Device() && isBionic(ctx.baseModuleName()) { |
Jiyong Park | c3e2c86 | 2019-03-16 01:10:08 +0900 | [diff] [blame] | 499 | binary.installSymlinkToRuntimeApex(ctx, file) |
| 500 | } |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 501 | binary.baseInstaller.subDir = "bootstrap" |
| 502 | } |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 503 | binary.baseInstaller.install(ctx, file) |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame] | 504 | |
| 505 | var preferredArchSymlinkPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 506 | for _, symlink := range binary.symlinks { |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame] | 507 | installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, |
| 508 | binary.baseInstaller.path) |
| 509 | if symlink == binary.preferredArchSymlink { |
| 510 | // If this is the preferred arch symlink, save the installed path for use as the |
| 511 | // tool path. |
| 512 | preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink) |
| 513 | } |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 516 | if ctx.Os().Class == android.Host { |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame] | 517 | // If the binary is multilib with a symlink to the preferred architecture, use the |
| 518 | // symlink instead of the binary because that's the more "canonical" name. |
| 519 | if preferredArchSymlinkPath.Valid() { |
| 520 | binary.toolPath = preferredArchSymlinkPath |
| 521 | } else { |
| 522 | binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path) |
| 523 | } |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 528 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 529 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 530 | |
| 531 | func init() { |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 532 | pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify") |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 535 | var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic", |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 536 | blueprint.RuleParams{ |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 537 | Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out", |
| 538 | CommandDeps: []string{"$verifyHostBionicCmd"}, |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 539 | }, "linker") |
| 540 | |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 541 | func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) { |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 542 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | f04eb99 | 2021-06-11 15:22:41 -0700 | [diff] [blame] | 543 | Rule: verifyHostBionic, |
| 544 | Description: "verify host bionic", |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 545 | Input: in, |
| 546 | Implicit: linker, |
| 547 | Output: out, |
| 548 | Args: map[string]string{ |
| 549 | "linker": linker.String(), |
| 550 | }, |
| 551 | }) |
| 552 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 553 | |
| 554 | func init() { |
| 555 | android.RegisterBp2BuildMutator("cc_binary", BinaryBp2build) |
| 556 | android.RegisterBp2BuildMutator("cc_binary_host", BinaryHostBp2build) |
| 557 | } |
| 558 | |
| 559 | func BinaryBp2build(ctx android.TopDownMutatorContext) { |
| 560 | binaryBp2build(ctx, "cc_binary") |
| 561 | } |
| 562 | |
| 563 | func BinaryHostBp2build(ctx android.TopDownMutatorContext) { |
| 564 | binaryBp2build(ctx, "cc_binary_host") |
| 565 | } |
| 566 | |
| 567 | func binaryBp2build(ctx android.TopDownMutatorContext, typ string) { |
| 568 | m, ok := ctx.Module().(*Module) |
| 569 | if !ok { |
| 570 | // Not a cc module |
| 571 | return |
| 572 | } |
| 573 | if !m.ConvertWithBp2build(ctx) { |
| 574 | return |
| 575 | } |
| 576 | |
| 577 | if ctx.ModuleType() != typ { |
| 578 | return |
| 579 | } |
| 580 | |
| 581 | var compatibleWith bazel.StringListAttribute |
| 582 | if typ == "cc_binary_host" { |
| 583 | //incompatible with android OS |
| 584 | compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, android.Android.Name, []string{"@platforms//:incompatible"}) |
| 585 | compatibleWith.SetSelectValue(bazel.OsConfigurationAxis, bazel.ConditionsDefaultConfigKey, []string{}) |
| 586 | } |
| 587 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 588 | baseAttrs := bp2BuildParseBaseProps(ctx, m) |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 589 | binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m) |
| 590 | |
| 591 | if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) { |
| 592 | baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency) |
| 593 | } else { |
| 594 | baseAttrs.implementationDeps.Add(baseAttrs.protoDependency) |
| 595 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 596 | |
| 597 | attrs := &binaryAttributes{ |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 598 | binaryLinkerAttrs: binaryLinkerAttrs, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 599 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 600 | Srcs: baseAttrs.srcs, |
| 601 | Srcs_c: baseAttrs.cSrcs, |
| 602 | Srcs_as: baseAttrs.asSrcs, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 603 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 604 | Copts: baseAttrs.copts, |
| 605 | Cppflags: baseAttrs.cppFlags, |
| 606 | Conlyflags: baseAttrs.conlyFlags, |
| 607 | Asflags: baseAttrs.asFlags, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 608 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 609 | Deps: baseAttrs.implementationDeps, |
| 610 | Dynamic_deps: baseAttrs.implementationDynamicDeps, |
| 611 | Whole_archive_deps: baseAttrs.wholeArchiveDeps, |
| 612 | System_deps: baseAttrs.systemDynamicDeps, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 613 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 614 | Local_includes: baseAttrs.localIncludes, |
| 615 | Absolute_includes: baseAttrs.absoluteIncludes, |
| 616 | Linkopts: baseAttrs.linkopts, |
| 617 | Link_crt: baseAttrs.linkCrt, |
| 618 | Use_libcrt: baseAttrs.useLibcrt, |
| 619 | Rtti: baseAttrs.rtti, |
| 620 | Stl: baseAttrs.stl, |
| 621 | Cpp_std: baseAttrs.cppStd, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 622 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 623 | Additional_linker_inputs: baseAttrs.additionalLinkerInputs, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 624 | |
| 625 | Strip: stripAttributes{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 626 | Keep_symbols: baseAttrs.stripKeepSymbols, |
| 627 | Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame, |
| 628 | Keep_symbols_list: baseAttrs.stripKeepSymbolsList, |
| 629 | All: baseAttrs.stripAll, |
| 630 | None: baseAttrs.stripNone, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 631 | }, |
| 632 | |
| 633 | Target_compatible_with: compatibleWith, |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 634 | Features: baseAttrs.features, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{ |
| 638 | Rule_class: "cc_binary", |
| 639 | Bzl_load_location: "//build/bazel/rules:cc_binary.bzl", |
| 640 | }, |
| 641 | android.CommonAttributes{Name: m.Name()}, |
| 642 | attrs) |
| 643 | } |
| 644 | |
| 645 | // binaryAttributes contains Bazel attributes corresponding to a cc binary |
| 646 | type binaryAttributes struct { |
| 647 | binaryLinkerAttrs |
| 648 | Srcs bazel.LabelListAttribute |
| 649 | Srcs_c bazel.LabelListAttribute |
| 650 | Srcs_as bazel.LabelListAttribute |
| 651 | |
| 652 | Copts bazel.StringListAttribute |
| 653 | Cppflags bazel.StringListAttribute |
| 654 | Conlyflags bazel.StringListAttribute |
| 655 | Asflags bazel.StringListAttribute |
| 656 | |
| 657 | Deps bazel.LabelListAttribute |
| 658 | Dynamic_deps bazel.LabelListAttribute |
| 659 | Whole_archive_deps bazel.LabelListAttribute |
| 660 | System_deps bazel.LabelListAttribute |
| 661 | |
| 662 | Local_includes bazel.StringListAttribute |
| 663 | Absolute_includes bazel.StringListAttribute |
| 664 | |
| 665 | Linkopts bazel.StringListAttribute |
| 666 | Additional_linker_inputs bazel.LabelListAttribute |
| 667 | |
| 668 | Link_crt bazel.BoolAttribute |
| 669 | Use_libcrt bazel.BoolAttribute |
| 670 | |
| 671 | Rtti bazel.BoolAttribute |
| 672 | Stl *string |
| 673 | Cpp_std *string |
| 674 | |
| 675 | Strip stripAttributes |
| 676 | |
| 677 | Features bazel.StringListAttribute |
| 678 | |
| 679 | Target_compatible_with bazel.StringListAttribute |
| 680 | } |