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 ( |
| 18 | "github.com/google/blueprint" |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | |
| 21 | "android/soong" |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | type BinaryLinkerProperties struct { |
| 26 | // compile executable with -static |
| 27 | Static_executable *bool `android:"arch_variant"` |
| 28 | |
| 29 | // set the name of the output |
| 30 | Stem string `android:"arch_variant"` |
| 31 | |
| 32 | // append to the name of the output |
| 33 | Suffix string `android:"arch_variant"` |
| 34 | |
| 35 | // if set, add an extra objcopy --prefix-symbols= step |
| 36 | Prefix_symbols string |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame^] | 37 | |
| 38 | // if set, install a symlink to the preferred architecture |
| 39 | Symlink_preferred_arch bool |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func init() { |
| 43 | soong.RegisterModuleType("cc_binary", binaryFactory) |
| 44 | soong.RegisterModuleType("cc_binary_host", binaryHostFactory) |
| 45 | } |
| 46 | |
| 47 | // Module factory for binaries |
| 48 | func binaryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 49 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 50 | return module.Init() |
| 51 | } |
| 52 | |
| 53 | // Module factory for host binaries |
| 54 | func binaryHostFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 55 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 56 | return module.Init() |
| 57 | } |
| 58 | |
| 59 | // |
| 60 | // Executables |
| 61 | // |
| 62 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 63 | type binaryDecorator struct { |
| 64 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame^] | 65 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 66 | stripper |
| 67 | |
| 68 | Properties BinaryLinkerProperties |
| 69 | |
| 70 | hostToolPath android.OptionalPath |
| 71 | } |
| 72 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 73 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 74 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 75 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 76 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 77 | &binary.Properties, |
| 78 | &binary.stripper.StripProperties) |
| 79 | |
| 80 | } |
| 81 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 82 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 83 | stem := ctx.ModuleName() |
| 84 | if binary.Properties.Stem != "" { |
| 85 | stem = binary.Properties.Stem |
| 86 | } |
| 87 | |
| 88 | return stem + binary.Properties.Suffix |
| 89 | } |
| 90 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 91 | func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 92 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 93 | if ctx.Device() { |
| 94 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
| 95 | if !ctx.sdk() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 96 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 97 | deps.CrtBegin = "crtbegin_static" |
| 98 | } else { |
| 99 | deps.CrtBegin = "crtbegin_dynamic" |
| 100 | } |
| 101 | deps.CrtEnd = "crtend_android" |
| 102 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 103 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 104 | deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion() |
| 105 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 106 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 107 | deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion() |
| 108 | } else { |
| 109 | deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion() |
| 110 | } |
| 111 | deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion() |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 116 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | if inList("libc++_static", deps.StaticLibs) { |
| 118 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") |
| 119 | } |
| 120 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with |
| 121 | // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, |
| 122 | // move them to the beginning of deps.LateStaticLibs |
| 123 | var groupLibs []string |
| 124 | deps.StaticLibs, groupLibs = filterList(deps.StaticLibs, |
| 125 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) |
| 126 | deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...) |
| 127 | } |
| 128 | } |
| 129 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 130 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 132 | "from static libs or set static_executable: true") |
| 133 | } |
| 134 | return deps |
| 135 | } |
| 136 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 137 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 138 | return true |
| 139 | } |
| 140 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 141 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 142 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 143 | binary := &binaryDecorator{ |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame^] | 144 | baseLinker: NewBaseLinker(), |
| 145 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 146 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 147 | module.compiler = NewBaseCompiler() |
| 148 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame^] | 149 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 150 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 153 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 154 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 155 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 156 | if ctx.Host() { |
| 157 | if ctx.Os() == android.Linux { |
| 158 | if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 159 | binary.Properties.Static_executable = proptools.BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 160 | } |
| 161 | } else { |
| 162 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 163 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame^] | 166 | |
| 167 | if binary.Properties.Symlink_preferred_arch { |
| 168 | if binary.Properties.Stem == "" && binary.Properties.Suffix == "" { |
| 169 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix") |
| 170 | } |
| 171 | var prefer bool |
| 172 | if ctx.Host() { |
| 173 | prefer = ctx.AConfig().HostPrefer32BitExecutables() |
| 174 | } else { |
| 175 | prefer = ctx.AConfig().DevicePrefer32BitExecutables() |
| 176 | } |
| 177 | if ctx.PrimaryArch() != prefer { |
| 178 | binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks, |
| 179 | ctx.ModuleName()) |
| 180 | } |
| 181 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 184 | func (binary *binaryDecorator) static() bool { |
| 185 | return Bool(binary.Properties.Static_executable) |
| 186 | } |
| 187 | |
| 188 | func (binary *binaryDecorator) staticBinary() bool { |
| 189 | return binary.static() |
| 190 | } |
| 191 | |
| 192 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 193 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 194 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | if ctx.Host() && !binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | flags.LdFlags = append(flags.LdFlags, "-pie") |
| 197 | if ctx.Os() == android.Windows { |
| 198 | flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup") |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 203 | // all code is position independent, and then those warnings get promoted to |
| 204 | // errors. |
| 205 | if ctx.Os() != android.Windows { |
| 206 | flags.CFlags = append(flags.CFlags, "-fpie") |
| 207 | } |
| 208 | |
| 209 | if ctx.Device() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 210 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 211 | // Clang driver needs -static to create static executable. |
| 212 | // However, bionic/linker uses -shared to overwrite. |
| 213 | // Linker for x86 targets does not allow coexistance of -static and -shared, |
| 214 | // so we add -static only if -shared is not used. |
| 215 | if !inList("-shared", flags.LdFlags) { |
| 216 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 217 | } |
| 218 | |
| 219 | flags.LdFlags = append(flags.LdFlags, |
| 220 | "-nostdlib", |
| 221 | "-Bstatic", |
| 222 | "-Wl,--gc-sections", |
| 223 | ) |
| 224 | |
| 225 | } else { |
| 226 | if flags.DynamicLinker == "" { |
| 227 | flags.DynamicLinker = "/system/bin/linker" |
| 228 | if flags.Toolchain.Is64Bit() { |
| 229 | flags.DynamicLinker += "64" |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | flags.LdFlags = append(flags.LdFlags, |
| 234 | "-pie", |
| 235 | "-nostdlib", |
| 236 | "-Bdynamic", |
| 237 | "-Wl,--gc-sections", |
| 238 | "-Wl,-z,nocopyreloc", |
| 239 | ) |
| 240 | } |
| 241 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 242 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 243 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 244 | } |
| 245 | if ctx.Darwin() { |
| 246 | flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return flags |
| 251 | } |
| 252 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 253 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 254 | flags Flags, deps PathDeps, objFiles android.Paths) android.Path { |
| 255 | |
| 256 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 257 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 258 | ret := outputFile |
| 259 | if ctx.Os().Class == android.Host { |
| 260 | binary.hostToolPath = android.OptionalPathForPath(outputFile) |
| 261 | } |
| 262 | |
| 263 | var linkerDeps android.Paths |
| 264 | |
| 265 | sharedLibs := deps.SharedLibs |
| 266 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
| 267 | |
| 268 | if flags.DynamicLinker != "" { |
| 269 | flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker) |
| 270 | } |
| 271 | |
| 272 | builderFlags := flagsToBuilderFlags(flags) |
| 273 | |
| 274 | if binary.stripper.needsStrip(ctx) { |
| 275 | strippedOutputFile := outputFile |
| 276 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 277 | binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags) |
| 278 | } |
| 279 | |
| 280 | if binary.Properties.Prefix_symbols != "" { |
| 281 | afterPrefixSymbols := outputFile |
| 282 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) |
| 283 | TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile, |
| 284 | flagsToBuilderFlags(flags), afterPrefixSymbols) |
| 285 | } |
| 286 | |
| 287 | TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs, |
| 288 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
| 289 | builderFlags, outputFile) |
| 290 | |
| 291 | return ret |
| 292 | } |
| 293 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 294 | func (binary *binaryDecorator) HostToolPath() android.OptionalPath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 295 | return binary.hostToolPath |
| 296 | } |