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 ( |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 18 | "android/soong/android" |
| 19 | ) |
| 20 | |
| 21 | type BinaryLinkerProperties struct { |
| 22 | // compile executable with -static |
| 23 | Static_executable *bool `android:"arch_variant"` |
| 24 | |
| 25 | // set the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 26 | Stem *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 27 | |
| 28 | // append to the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 29 | Suffix *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 30 | |
| 31 | // if set, add an extra objcopy --prefix-symbols= step |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 32 | Prefix_symbols *string |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 33 | |
| 34 | // if set, install a symlink to the preferred architecture |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 35 | Symlink_preferred_arch *bool |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 36 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 37 | // install symlinks to the binary. Symlink names will have the suffix and the binary |
| 38 | // extension (if any) appended |
| 39 | Symlinks []string `android:"arch_variant"` |
| 40 | |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 41 | DynamicLinker string `blueprint:"mutated"` |
Yifan Hong | 946e32e | 2018-04-03 13:22:50 -0700 | [diff] [blame] | 42 | |
| 43 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 44 | // (in Make or Soong). |
| 45 | // This does not completely prevent installation of the overridden binaries, but if both |
| 46 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 47 | // from PRODUCT_PACKAGES. |
| 48 | Overrides []string |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 52 | android.RegisterModuleType("cc_binary", binaryFactory) |
| 53 | android.RegisterModuleType("cc_binary_host", binaryHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Module factory for binaries |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 57 | func binaryFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 58 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 59 | return module.Init() |
| 60 | } |
| 61 | |
| 62 | // Module factory for host binaries |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 63 | func binaryHostFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 64 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 65 | return module.Init() |
| 66 | } |
| 67 | |
| 68 | // |
| 69 | // Executables |
| 70 | // |
| 71 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 72 | type binaryDecorator struct { |
| 73 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 74 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 75 | stripper |
| 76 | |
| 77 | Properties BinaryLinkerProperties |
| 78 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 79 | toolPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 80 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 81 | // Location of the linked, unstripped binary |
| 82 | unstrippedOutputFile android.Path |
| 83 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 84 | // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS |
| 85 | symlinks []string |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 86 | |
| 87 | // Output archive of gcno coverage information |
| 88 | coverageOutputFile android.OptionalPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 91 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 92 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 93 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 94 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 95 | &binary.Properties, |
| 96 | &binary.stripper.StripProperties) |
| 97 | |
| 98 | } |
| 99 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 100 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 101 | stem := ctx.baseModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 102 | if String(binary.Properties.Stem) != "" { |
| 103 | stem = String(binary.Properties.Stem) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 106 | return stem + String(binary.Properties.Suffix) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 109 | func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 110 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 111 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 112 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 113 | if !ctx.useSdk() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 114 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | deps.CrtBegin = "crtbegin_static" |
| 116 | } else { |
| 117 | deps.CrtBegin = "crtbegin_dynamic" |
| 118 | } |
| 119 | deps.CrtEnd = "crtend_android" |
| 120 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 121 | // TODO(danalbert): Add generation of crt objects. |
| 122 | // For `sdk_version: "current"`, we don't actually have a |
| 123 | // freshly generated set of CRT objects. Use the last stable |
| 124 | // version. |
| 125 | version := ctx.sdkVersion() |
| 126 | if version == "current" { |
Jayant Chowdhary | 6e8115a | 2017-05-09 10:21:52 -0700 | [diff] [blame] | 127 | version = getCurrentNdkPrebuiltVersion(ctx) |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 130 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 131 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 132 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 133 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 134 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 135 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 136 | deps.CrtBegin = "ndk_crtbegin_dynamic." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 137 | } |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 138 | deps.CrtEnd = "ndk_crtend_android." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 143 | if binary.static() { |
Dan Albert | dc2597d | 2017-01-26 17:44:26 -0800 | [diff] [blame] | 144 | if ctx.selectedStl() == "libc++_static" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") |
| 146 | } |
| 147 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with |
| 148 | // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, |
| 149 | // move them to the beginning of deps.LateStaticLibs |
| 150 | var groupLibs []string |
| 151 | deps.StaticLibs, groupLibs = filterList(deps.StaticLibs, |
| 152 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) |
| 153 | deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...) |
| 154 | } |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 155 | |
| 156 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
| 157 | deps.LinkerScript = "host_bionic_linker_script" |
| 158 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 161 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 162 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 163 | "from static libs or set static_executable: true") |
| 164 | } |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 165 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | return deps |
| 167 | } |
| 168 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 169 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 170 | return true |
| 171 | } |
| 172 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 173 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 174 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 175 | binary := &binaryDecorator{ |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 176 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 177 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 178 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 179 | module.compiler = NewBaseCompiler() |
| 180 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 181 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 182 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 185 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 186 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 187 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 188 | if !ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 189 | if ctx.Os() == android.Linux { |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 190 | if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 191 | binary.Properties.Static_executable = BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 192 | } |
| 193 | } else { |
| 194 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 200 | func (binary *binaryDecorator) static() bool { |
| 201 | return Bool(binary.Properties.Static_executable) |
| 202 | } |
| 203 | |
| 204 | func (binary *binaryDecorator) staticBinary() bool { |
| 205 | return binary.static() |
| 206 | } |
| 207 | |
| 208 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 209 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 210 | |
Colin Cross | 446c666 | 2018-09-14 16:00:16 -0700 | [diff] [blame^] | 211 | if ctx.Host() && !ctx.Windows() && !binary.static() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 212 | if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") { |
Colin Cross | 7a108bc | 2017-01-30 22:44:19 -0800 | [diff] [blame] | 213 | flags.LdFlags = append(flags.LdFlags, "-pie") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 218 | // all code is position independent, and then those warnings get promoted to |
| 219 | // errors. |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 220 | if !ctx.Windows() { |
Vishwath Mohan | e87b768 | 2017-04-17 16:21:41 -0700 | [diff] [blame] | 221 | flags.CFlags = append(flags.CFlags, "-fPIE") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 224 | if ctx.toolchain().Bionic() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 225 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 226 | // Clang driver needs -static to create static executable. |
| 227 | // However, bionic/linker uses -shared to overwrite. |
| 228 | // Linker for x86 targets does not allow coexistance of -static and -shared, |
| 229 | // so we add -static only if -shared is not used. |
| 230 | if !inList("-shared", flags.LdFlags) { |
| 231 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 232 | } |
| 233 | |
| 234 | flags.LdFlags = append(flags.LdFlags, |
| 235 | "-nostdlib", |
| 236 | "-Bstatic", |
| 237 | "-Wl,--gc-sections", |
| 238 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 239 | } else { |
| 240 | if flags.DynamicLinker == "" { |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 241 | if binary.Properties.DynamicLinker != "" { |
| 242 | flags.DynamicLinker = binary.Properties.DynamicLinker |
| 243 | } else { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 244 | switch ctx.Os() { |
| 245 | case android.Android: |
| 246 | flags.DynamicLinker = "/system/bin/linker" |
| 247 | case android.LinuxBionic: |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 248 | flags.DynamicLinker = "" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 249 | default: |
| 250 | ctx.ModuleErrorf("unknown dynamic linker") |
| 251 | } |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 252 | if flags.Toolchain.Is64Bit() { |
| 253 | flags.DynamicLinker += "64" |
| 254 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | flags.LdFlags = append(flags.LdFlags, |
| 259 | "-pie", |
| 260 | "-nostdlib", |
| 261 | "-Bdynamic", |
| 262 | "-Wl,--gc-sections", |
| 263 | "-Wl,-z,nocopyreloc", |
| 264 | ) |
dimitry | feda20b | 2017-08-29 15:00:01 +0200 | [diff] [blame] | 265 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 266 | } |
| 267 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 268 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 269 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 270 | } |
| 271 | if ctx.Darwin() { |
| 272 | flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return flags |
| 277 | } |
| 278 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 279 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 280 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 281 | |
| 282 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 283 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 284 | ret := outputFile |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 285 | |
| 286 | var linkerDeps android.Paths |
| 287 | |
| 288 | sharedLibs := deps.SharedLibs |
| 289 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
| 290 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 291 | if deps.LinkerScript.Valid() { |
| 292 | flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String()) |
| 293 | linkerDeps = append(linkerDeps, deps.LinkerScript.Path()) |
| 294 | } |
| 295 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 296 | if flags.DynamicLinker != "" { |
| 297 | flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker) |
| 298 | } |
| 299 | |
| 300 | builderFlags := flagsToBuilderFlags(flags) |
| 301 | |
| 302 | if binary.stripper.needsStrip(ctx) { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 303 | // b/80093681, GNU strip/objcopy bug. |
| 304 | // Use llvm-{strip,objcopy} when clang lld is used. |
| 305 | builderFlags.stripUseLlvmStrip = |
| 306 | flags.Clang && binary.baseLinker.useClangLld(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 307 | strippedOutputFile := outputFile |
| 308 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 309 | binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags) |
| 310 | } |
| 311 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 312 | binary.unstrippedOutputFile = outputFile |
| 313 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 314 | if String(binary.Properties.Prefix_symbols) != "" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 315 | afterPrefixSymbols := outputFile |
| 316 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 317 | TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 318 | flagsToBuilderFlags(flags), afterPrefixSymbols) |
| 319 | } |
| 320 | |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 321 | if Bool(binary.baseLinker.Properties.Use_version_lib) && ctx.Host() { |
| 322 | versionedOutputFile := outputFile |
| 323 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 324 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 325 | } |
| 326 | |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 327 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 328 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 329 | linkerDeps = append(linkerDeps, objs.tidyFiles...) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 330 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 331 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 332 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 333 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
| 334 | builderFlags, outputFile) |
| 335 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 336 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) |
| 337 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) |
| 338 | binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx)) |
| 339 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 340 | return ret |
| 341 | } |
| 342 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 343 | func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) { |
| 344 | binary.baseInstaller.install(ctx, file) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 345 | for _, symlink := range binary.Properties.Symlinks { |
| 346 | binary.symlinks = append(binary.symlinks, |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 347 | symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix()) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 350 | if Bool(binary.Properties.Symlink_preferred_arch) { |
| 351 | if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" { |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 352 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix") |
| 353 | } |
| 354 | if ctx.TargetPrimary() { |
| 355 | binary.symlinks = append(binary.symlinks, ctx.baseModuleName()) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | for _, symlink := range binary.symlinks { |
| 360 | ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path) |
| 361 | } |
| 362 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 363 | if ctx.Os().Class == android.Host { |
| 364 | binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 369 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 370 | } |