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