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