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