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