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