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