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 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "android/soong" |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 26 | type LibraryProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 27 | Static struct { |
| 28 | Srcs []string `android:"arch_variant"` |
| 29 | Exclude_srcs []string `android:"arch_variant"` |
| 30 | Cflags []string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 31 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 32 | Enabled *bool `android:"arch_variant"` |
| 33 | Whole_static_libs []string `android:"arch_variant"` |
| 34 | Static_libs []string `android:"arch_variant"` |
| 35 | Shared_libs []string `android:"arch_variant"` |
| 36 | } `android:"arch_variant"` |
| 37 | Shared struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 38 | Srcs []string `android:"arch_variant"` |
| 39 | Exclude_srcs []string `android:"arch_variant"` |
| 40 | Cflags []string `android:"arch_variant"` |
| 41 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | Enabled *bool `android:"arch_variant"` |
| 43 | Whole_static_libs []string `android:"arch_variant"` |
| 44 | Static_libs []string `android:"arch_variant"` |
| 45 | Shared_libs []string `android:"arch_variant"` |
| 46 | } `android:"arch_variant"` |
| 47 | |
| 48 | // local file name to pass to the linker as --version_script |
| 49 | Version_script *string `android:"arch_variant"` |
| 50 | // local file name to pass to the linker as -unexported_symbols_list |
| 51 | Unexported_symbols_list *string `android:"arch_variant"` |
| 52 | // local file name to pass to the linker as -force_symbols_not_weak_list |
| 53 | Force_symbols_not_weak_list *string `android:"arch_variant"` |
| 54 | // local file name to pass to the linker as -force_symbols_weak_list |
| 55 | Force_symbols_weak_list *string `android:"arch_variant"` |
| 56 | |
| 57 | // rename host libraries to prevent overlap with system installed libraries |
| 58 | Unique_host_soname *bool |
| 59 | |
| 60 | VariantName string `blueprint:"mutated"` |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 61 | |
| 62 | // Build a static variant |
| 63 | BuildStatic bool `blueprint:"mutated"` |
| 64 | // Build a shared variant |
| 65 | BuildShared bool `blueprint:"mutated"` |
| 66 | // This variant is shared |
| 67 | VariantIsShared bool `blueprint:"mutated"` |
| 68 | // This variant is static |
| 69 | VariantIsStatic bool `blueprint:"mutated"` |
| 70 | } |
| 71 | |
| 72 | type FlagExporterProperties struct { |
| 73 | // list of directories relative to the Blueprints file that will |
| 74 | // be added to the include path using -I for any module that links against this module |
| 75 | Export_include_dirs []string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func init() { |
| 79 | soong.RegisterModuleType("cc_library_static", libraryStaticFactory) |
| 80 | soong.RegisterModuleType("cc_library_shared", librarySharedFactory) |
| 81 | soong.RegisterModuleType("cc_library", libraryFactory) |
| 82 | soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory) |
| 83 | soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory) |
| 84 | } |
| 85 | |
| 86 | // Module factory for combined static + shared libraries, device by default but with possible host |
| 87 | // support |
| 88 | func libraryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 89 | module, _ := NewLibrary(android.HostAndDeviceSupported, true, true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 90 | return module.Init() |
| 91 | } |
| 92 | |
| 93 | // Module factory for static libraries |
| 94 | func libraryStaticFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 95 | module, _ := NewLibrary(android.HostAndDeviceSupported, false, true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 96 | return module.Init() |
| 97 | } |
| 98 | |
| 99 | // Module factory for shared libraries |
| 100 | func librarySharedFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 101 | module, _ := NewLibrary(android.HostAndDeviceSupported, true, false) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 102 | return module.Init() |
| 103 | } |
| 104 | |
| 105 | // Module factory for host static libraries |
| 106 | func libraryHostStaticFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 107 | module, _ := NewLibrary(android.HostSupported, false, true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 108 | return module.Init() |
| 109 | } |
| 110 | |
| 111 | // Module factory for host shared libraries |
| 112 | func libraryHostSharedFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 113 | module, _ := NewLibrary(android.HostSupported, true, false) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 114 | return module.Init() |
| 115 | } |
| 116 | |
| 117 | type flagExporter struct { |
| 118 | Properties FlagExporterProperties |
| 119 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame^] | 120 | flags []string |
| 121 | flagsDeps android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) { |
| 125 | includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs) |
| 126 | for _, dir := range includeDirs.Strings() { |
| 127 | f.flags = append(f.flags, inc+dir) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func (f *flagExporter) reexportFlags(flags []string) { |
| 132 | f.flags = append(f.flags, flags...) |
| 133 | } |
| 134 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame^] | 135 | func (f *flagExporter) reexportDeps(deps android.Paths) { |
| 136 | f.flagsDeps = append(f.flagsDeps, deps...) |
| 137 | } |
| 138 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | func (f *flagExporter) exportedFlags() []string { |
| 140 | return f.flags |
| 141 | } |
| 142 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame^] | 143 | func (f *flagExporter) exportedFlagsDeps() android.Paths { |
| 144 | return f.flagsDeps |
| 145 | } |
| 146 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 147 | type exportedFlagsProducer interface { |
| 148 | exportedFlags() []string |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame^] | 149 | exportedFlagsDeps() android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | var _ exportedFlagsProducer = (*flagExporter)(nil) |
| 153 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 154 | // libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific |
| 155 | // functionality: static vs. shared linkage, reusing object files for shared libraries |
| 156 | type libraryDecorator struct { |
| 157 | Properties LibraryProperties |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | |
| 159 | // For reusing static library objects for shared library |
| 160 | reuseObjFiles android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 161 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 162 | flagExporter |
| 163 | stripper |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 164 | relocationPacker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 165 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | // If we're used as a whole_static_lib, our missing dependencies need |
| 167 | // to be given |
| 168 | wholeStaticMissingDeps []string |
| 169 | |
| 170 | // For whole_static_libs |
| 171 | objFiles android.Paths |
| 172 | |
| 173 | // Uses the module's name if empty, but can be overridden. Does not include |
| 174 | // shlib suffix. |
| 175 | libName string |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 176 | |
| 177 | sanitize *sanitize |
| 178 | |
| 179 | // Decorated interafaces |
| 180 | *baseCompiler |
| 181 | *baseLinker |
| 182 | *baseInstaller |
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 (library *libraryDecorator) linkerProps() []interface{} { |
| 186 | var props []interface{} |
| 187 | props = append(props, library.baseLinker.linkerProps()...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 188 | return append(props, |
| 189 | &library.Properties, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 190 | &library.flagExporter.Properties, |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 191 | &library.stripper.StripProperties, |
| 192 | &library.relocationPacker.Properties) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 196 | flags = library.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 197 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 198 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 199 | // all code is position independent, and then those warnings get promoted to |
| 200 | // errors. |
| 201 | if ctx.Os() != android.Windows { |
| 202 | flags.CFlags = append(flags.CFlags, "-fPIC") |
| 203 | } |
| 204 | |
| 205 | if library.static() { |
| 206 | flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...) |
| 207 | } else { |
| 208 | flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...) |
| 209 | } |
| 210 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 211 | if !library.static() { |
| 212 | libName := library.getLibName(ctx) |
| 213 | // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead |
| 214 | sharedFlag := "-Wl,-shared" |
| 215 | if flags.Clang || ctx.Host() { |
| 216 | sharedFlag = "-shared" |
| 217 | } |
| 218 | var f []string |
| 219 | if ctx.Device() { |
| 220 | f = append(f, |
| 221 | "-nostdlib", |
| 222 | "-Wl,--gc-sections", |
| 223 | ) |
| 224 | } |
| 225 | |
| 226 | if ctx.Darwin() { |
| 227 | f = append(f, |
| 228 | "-dynamiclib", |
| 229 | "-single_module", |
Colin Cross | 7d82ab7 | 2016-08-25 16:54:53 -0700 | [diff] [blame] | 230 | "-read_only_relocs suppress", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 231 | "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(), |
| 232 | ) |
| 233 | } else { |
| 234 | f = append(f, |
| 235 | sharedFlag, |
| 236 | "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix()) |
| 237 | } |
| 238 | |
| 239 | flags.LdFlags = append(f, flags.LdFlags...) |
| 240 | } |
| 241 | |
| 242 | return flags |
| 243 | } |
| 244 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 245 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths { |
| 246 | var objFiles android.Paths |
| 247 | |
| 248 | objFiles = library.baseCompiler.compile(ctx, flags, deps) |
| 249 | library.reuseObjFiles = objFiles |
| 250 | |
| 251 | pathDeps := deps.GeneratedHeaders |
| 252 | pathDeps = append(pathDeps, ndkPathDeps(ctx)...) |
| 253 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 254 | if library.static() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 255 | objFiles = append(objFiles, compileObjs(ctx, flags, android.DeviceStaticLibrary, |
| 256 | library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs, |
| 257 | nil, pathDeps)...) |
| 258 | } else { |
| 259 | objFiles = append(objFiles, compileObjs(ctx, flags, android.DeviceSharedLibrary, |
| 260 | library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs, |
| 261 | nil, pathDeps)...) |
| 262 | } |
| 263 | |
| 264 | return objFiles |
| 265 | } |
| 266 | |
| 267 | type libraryInterface interface { |
| 268 | getWholeStaticMissingDeps() []string |
| 269 | static() bool |
| 270 | objs() android.Paths |
| 271 | reuseObjs() android.Paths |
| 272 | |
| 273 | // Returns true if the build options for the module have selected a static or shared build |
| 274 | buildStatic() bool |
| 275 | buildShared() bool |
| 276 | |
| 277 | // Sets whether a specific variant is static or shared |
| 278 | setStatic(bool) |
| 279 | } |
| 280 | |
| 281 | func (library *libraryDecorator) getLibName(ctx ModuleContext) string { |
| 282 | name := library.libName |
| 283 | if name == "" { |
| 284 | name = ctx.ModuleName() |
| 285 | } |
| 286 | |
| 287 | if ctx.Host() && Bool(library.Properties.Unique_host_soname) { |
| 288 | if !strings.HasSuffix(name, "-host") { |
| 289 | name = name + "-host" |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return name + library.Properties.VariantName |
| 294 | } |
| 295 | |
| 296 | func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) { |
| 297 | location := InstallInSystem |
| 298 | if library.sanitize.inData() { |
| 299 | location = InstallInData |
| 300 | } |
| 301 | library.baseInstaller.location = location |
| 302 | |
| 303 | library.baseLinker.linkerInit(ctx) |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 304 | |
| 305 | library.relocationPacker.packingInit(ctx) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
| 309 | deps = library.baseLinker.linkerDeps(ctx, deps) |
| 310 | |
| 311 | if library.static() { |
| 312 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, |
| 313 | library.Properties.Static.Whole_static_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 314 | deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...) |
| 315 | deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...) |
| 316 | } else { |
| 317 | if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) { |
| 318 | if !ctx.sdk() { |
| 319 | deps.CrtBegin = "crtbegin_so" |
| 320 | deps.CrtEnd = "crtend_so" |
| 321 | } else { |
| 322 | deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion() |
| 323 | deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion() |
| 324 | } |
| 325 | } |
| 326 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...) |
| 327 | deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...) |
| 328 | deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...) |
| 329 | } |
| 330 | |
| 331 | return deps |
| 332 | } |
| 333 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 334 | func (library *libraryDecorator) linkStatic(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 335 | flags Flags, deps PathDeps, objFiles android.Paths) android.Path { |
| 336 | |
| 337 | library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...) |
| 338 | library.objFiles = append(library.objFiles, objFiles...) |
| 339 | |
| 340 | outputFile := android.PathForModuleOut(ctx, |
| 341 | ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension) |
| 342 | |
| 343 | if ctx.Darwin() { |
| 344 | TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile) |
| 345 | } else { |
| 346 | TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile) |
| 347 | } |
| 348 | |
| 349 | library.wholeStaticMissingDeps = ctx.GetMissingDependencies() |
| 350 | |
| 351 | ctx.CheckbuildFile(outputFile) |
| 352 | |
| 353 | return outputFile |
| 354 | } |
| 355 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 356 | func (library *libraryDecorator) linkShared(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 357 | flags Flags, deps PathDeps, objFiles android.Paths) android.Path { |
| 358 | |
| 359 | var linkerDeps android.Paths |
| 360 | |
| 361 | versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script) |
| 362 | unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list) |
| 363 | forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list) |
| 364 | forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list) |
| 365 | if !ctx.Darwin() { |
| 366 | if versionScript.Valid() { |
| 367 | flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String()) |
| 368 | linkerDeps = append(linkerDeps, versionScript.Path()) |
| 369 | } |
| 370 | if unexportedSymbols.Valid() { |
| 371 | ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin") |
| 372 | } |
| 373 | if forceNotWeakSymbols.Valid() { |
| 374 | ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin") |
| 375 | } |
| 376 | if forceWeakSymbols.Valid() { |
| 377 | ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin") |
| 378 | } |
| 379 | } else { |
| 380 | if versionScript.Valid() { |
| 381 | ctx.PropertyErrorf("version_script", "Not supported on Darwin") |
| 382 | } |
| 383 | if unexportedSymbols.Valid() { |
| 384 | flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String()) |
| 385 | linkerDeps = append(linkerDeps, unexportedSymbols.Path()) |
| 386 | } |
| 387 | if forceNotWeakSymbols.Valid() { |
| 388 | flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String()) |
| 389 | linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path()) |
| 390 | } |
| 391 | if forceWeakSymbols.Valid() { |
| 392 | flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String()) |
| 393 | linkerDeps = append(linkerDeps, forceWeakSymbols.Path()) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
| 398 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 399 | ret := outputFile |
| 400 | |
| 401 | builderFlags := flagsToBuilderFlags(flags) |
| 402 | |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 403 | if library.relocationPacker.needsPacking(ctx) { |
| 404 | packedOutputFile := outputFile |
| 405 | outputFile = android.PathForModuleOut(ctx, "unpacked", fileName) |
| 406 | library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags) |
| 407 | } |
| 408 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 409 | if library.stripper.needsStrip(ctx) { |
| 410 | strippedOutputFile := outputFile |
| 411 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 412 | library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags) |
| 413 | } |
| 414 | |
| 415 | sharedLibs := deps.SharedLibs |
| 416 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
| 417 | |
Dan Albert | d015c4a | 2016-08-10 14:34:08 -0700 | [diff] [blame] | 418 | // TODO(danalbert): Clean this up when soong supports prebuilts. |
| 419 | if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") { |
| 420 | libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++") |
| 421 | |
| 422 | if strings.HasSuffix(ctx.selectedStl(), "_shared") { |
| 423 | deps.StaticLibs = append(deps.StaticLibs, |
| 424 | libDir.Join(ctx, "libandroid_support.a")) |
| 425 | } else { |
| 426 | deps.StaticLibs = append(deps.StaticLibs, |
| 427 | libDir.Join(ctx, "libc++abi.a"), |
| 428 | libDir.Join(ctx, "libandroid_support.a")) |
| 429 | } |
| 430 | |
| 431 | if ctx.Arch().ArchType == android.Arm { |
| 432 | deps.StaticLibs = append(deps.StaticLibs, |
| 433 | libDir.Join(ctx, "libunwind.a")) |
| 434 | } |
| 435 | } |
| 436 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 437 | TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, |
| 438 | deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs, |
| 439 | linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile) |
| 440 | |
| 441 | return ret |
| 442 | } |
| 443 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 444 | func (library *libraryDecorator) link(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 445 | flags Flags, deps PathDeps, objFiles android.Paths) android.Path { |
| 446 | |
| 447 | objFiles = append(objFiles, deps.ObjFiles...) |
| 448 | |
| 449 | var out android.Path |
| 450 | if library.static() { |
| 451 | out = library.linkStatic(ctx, flags, deps, objFiles) |
| 452 | } else { |
| 453 | out = library.linkShared(ctx, flags, deps, objFiles) |
| 454 | } |
| 455 | |
| 456 | library.exportIncludes(ctx, "-I") |
| 457 | library.reexportFlags(deps.ReexportedFlags) |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame^] | 458 | library.reexportDeps(deps.ReexportedFlagsDeps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 459 | |
| 460 | return out |
| 461 | } |
| 462 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 463 | func (library *libraryDecorator) buildStatic() bool { |
| 464 | return library.Properties.BuildStatic && |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 465 | (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled) |
| 466 | } |
| 467 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 468 | func (library *libraryDecorator) buildShared() bool { |
| 469 | return library.Properties.BuildShared && |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 470 | (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled) |
| 471 | } |
| 472 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 473 | func (library *libraryDecorator) getWholeStaticMissingDeps() []string { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 474 | return library.wholeStaticMissingDeps |
| 475 | } |
| 476 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 477 | func (library *libraryDecorator) objs() android.Paths { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 478 | return library.objFiles |
| 479 | } |
| 480 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 481 | func (library *libraryDecorator) reuseObjs() android.Paths { |
| 482 | return library.reuseObjFiles |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 485 | func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) { |
| 486 | if !ctx.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 487 | library.baseInstaller.install(ctx, file) |
| 488 | } |
| 489 | } |
| 490 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 491 | func (library *libraryDecorator) static() bool { |
| 492 | return library.Properties.VariantIsStatic |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 495 | func (library *libraryDecorator) setStatic(static bool) { |
| 496 | library.Properties.VariantIsStatic = static |
| 497 | } |
| 498 | |
| 499 | func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 500 | module := newModule(hod, android.MultilibBoth) |
| 501 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 502 | library := &libraryDecorator{ |
| 503 | Properties: LibraryProperties{ |
| 504 | BuildShared: shared, |
| 505 | BuildStatic: static, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 506 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 507 | baseCompiler: NewBaseCompiler(), |
| 508 | baseLinker: NewBaseLinker(), |
| 509 | baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem), |
| 510 | sanitize: module.sanitize, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 513 | module.compiler = library |
| 514 | module.linker = library |
| 515 | module.installer = library |
| 516 | |
| 517 | return module, library |
| 518 | } |
| 519 | |
| 520 | func linkageMutator(mctx android.BottomUpMutatorContext) { |
| 521 | if m, ok := mctx.Module().(*Module); ok && m.linker != nil { |
| 522 | if library, ok := m.linker.(libraryInterface); ok { |
| 523 | var modules []blueprint.Module |
| 524 | if library.buildStatic() && library.buildShared() { |
| 525 | modules = mctx.CreateLocalVariations("static", "shared") |
| 526 | static := modules[0].(*Module) |
| 527 | shared := modules[1].(*Module) |
| 528 | |
| 529 | static.linker.(libraryInterface).setStatic(true) |
| 530 | shared.linker.(libraryInterface).setStatic(false) |
| 531 | |
| 532 | if staticCompiler, ok := static.compiler.(*libraryDecorator); ok { |
| 533 | sharedCompiler := shared.compiler.(*libraryDecorator) |
| 534 | if len(staticCompiler.Properties.Static.Cflags) == 0 && |
| 535 | len(sharedCompiler.Properties.Shared.Cflags) == 0 { |
| 536 | // Optimize out compiling common .o files twice for static+shared libraries |
| 537 | mctx.AddInterVariantDependency(reuseObjTag, shared, static) |
| 538 | sharedCompiler.baseCompiler.Properties.Srcs = nil |
| 539 | sharedCompiler.baseCompiler.Properties.Generated_sources = nil |
| 540 | } |
| 541 | } |
| 542 | } else if library.buildStatic() { |
| 543 | modules = mctx.CreateLocalVariations("static") |
| 544 | modules[0].(*Module).linker.(libraryInterface).setStatic(true) |
| 545 | } else if library.buildShared() { |
| 546 | modules = mctx.CreateLocalVariations("shared") |
| 547 | modules[0].(*Module).linker.(libraryInterface).setStatic(false) |
| 548 | } |
| 549 | } |
| 550 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 551 | } |