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