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