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