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 ( |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 18 | "regexp" |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 19 | "sort" |
| 20 | "strconv" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "strings" |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 22 | "sync" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint/pathtools" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 26 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 27 | "android/soong/android" |
Dan Albert | ea4b7b9 | 2018-04-25 16:05:30 -0700 | [diff] [blame] | 28 | "android/soong/cc/config" |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 29 | "android/soong/genrule" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 32 | type LibraryProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 33 | Static struct { |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 34 | Srcs []string `android:"arch_variant"` |
| 35 | Cflags []string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 36 | |
Dan Willemsen | 3a26eef | 2018-12-03 15:25:46 -0800 | [diff] [blame] | 37 | Enabled *bool `android:"arch_variant"` |
| 38 | Whole_static_libs []string `android:"arch_variant"` |
| 39 | Static_libs []string `android:"arch_variant"` |
| 40 | Shared_libs []string `android:"arch_variant"` |
| 41 | System_shared_libs []string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | } `android:"arch_variant"` |
| 43 | Shared struct { |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 44 | Srcs []string `android:"arch_variant"` |
| 45 | Cflags []string `android:"arch_variant"` |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 46 | |
Dan Willemsen | 3a26eef | 2018-12-03 15:25:46 -0800 | [diff] [blame] | 47 | Enabled *bool `android:"arch_variant"` |
| 48 | Whole_static_libs []string `android:"arch_variant"` |
| 49 | Static_libs []string `android:"arch_variant"` |
| 50 | Shared_libs []string `android:"arch_variant"` |
| 51 | System_shared_libs []string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 52 | } `android:"arch_variant"` |
| 53 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 54 | // local file name to pass to the linker as -unexported_symbols_list |
| 55 | Unexported_symbols_list *string `android:"arch_variant"` |
| 56 | // local file name to pass to the linker as -force_symbols_not_weak_list |
| 57 | Force_symbols_not_weak_list *string `android:"arch_variant"` |
| 58 | // local file name to pass to the linker as -force_symbols_weak_list |
| 59 | Force_symbols_weak_list *string `android:"arch_variant"` |
| 60 | |
| 61 | // rename host libraries to prevent overlap with system installed libraries |
| 62 | Unique_host_soname *bool |
| 63 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 64 | Aidl struct { |
| 65 | // export headers generated from .aidl sources |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 66 | Export_aidl_headers *bool |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 69 | Proto struct { |
| 70 | // export headers generated from .proto sources |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 71 | Export_proto_headers *bool |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 72 | } |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 73 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 74 | Static_ndk_lib *bool |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 75 | |
| 76 | Stubs struct { |
| 77 | // Relative path to the symbol map. The symbol map provides the list of |
| 78 | // symbols that are exported for stubs variant of this library. |
| 79 | Symbol_file *string |
| 80 | |
| 81 | // List versions to generate stubs libs for. |
| 82 | Versions []string |
| 83 | } |
dimitry | d95964a | 2018-11-07 13:43:34 +0100 | [diff] [blame] | 84 | |
| 85 | // set the name of the output |
| 86 | Stem *string `android:"arch_variant"` |
| 87 | |
| 88 | // Names of modules to be overridden. Listed modules can only be other shared libraries |
| 89 | // (in Make or Soong). |
| 90 | // This does not completely prevent installation of the overridden libraries, but if both |
| 91 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed |
| 92 | // from PRODUCT_PACKAGES. |
| 93 | Overrides []string |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 94 | } |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 95 | |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 96 | type LibraryMutatedProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 97 | VariantName string `blueprint:"mutated"` |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 98 | |
| 99 | // Build a static variant |
| 100 | BuildStatic bool `blueprint:"mutated"` |
| 101 | // Build a shared variant |
| 102 | BuildShared bool `blueprint:"mutated"` |
| 103 | // This variant is shared |
| 104 | VariantIsShared bool `blueprint:"mutated"` |
| 105 | // This variant is static |
| 106 | VariantIsStatic bool `blueprint:"mutated"` |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 107 | |
| 108 | // This variant is a stubs lib |
| 109 | BuildStubs bool `blueprint:"mutated"` |
| 110 | // Version of the stubs lib |
| 111 | StubsVersion string `blueprint:"mutated"` |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | type FlagExporterProperties struct { |
| 115 | // list of directories relative to the Blueprints file that will |
Dan Willemsen | 273af7f | 2016-11-03 15:53:42 -0700 | [diff] [blame] | 116 | // 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] | 117 | // against this module. Directories listed in export_include_dirs do not need to be |
| 118 | // listed in local_include_dirs. |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 119 | Export_include_dirs []string `android:"arch_variant"` |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 120 | |
| 121 | Target struct { |
| 122 | Vendor struct { |
| 123 | // list of exported include directories, like |
| 124 | // export_include_dirs, that will be applied to the |
| 125 | // vendor variant of this library. This will overwrite |
| 126 | // any other declarations. |
Steven Moreland | b21df8f | 2018-01-05 14:42:54 -0800 | [diff] [blame] | 127 | Override_export_include_dirs []string |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | func init() { |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 133 | android.RegisterModuleType("cc_library_static", LibraryStaticFactory) |
| 134 | android.RegisterModuleType("cc_library_shared", LibrarySharedFactory) |
| 135 | android.RegisterModuleType("cc_library", LibraryFactory) |
| 136 | android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory) |
| 137 | android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory) |
| 138 | android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Module factory for combined static + shared libraries, device by default but with possible host |
| 142 | // support |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 143 | func LibraryFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 144 | module, _ := NewLibrary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | return module.Init() |
| 146 | } |
| 147 | |
| 148 | // Module factory for static libraries |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 149 | func LibraryStaticFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 150 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 151 | library.BuildOnlyStatic() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 152 | return module.Init() |
| 153 | } |
| 154 | |
| 155 | // Module factory for shared libraries |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 156 | func LibrarySharedFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 157 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 158 | library.BuildOnlyShared() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 159 | return module.Init() |
| 160 | } |
| 161 | |
| 162 | // Module factory for host static libraries |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 163 | func LibraryHostStaticFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 164 | module, library := NewLibrary(android.HostSupported) |
| 165 | library.BuildOnlyStatic() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | return module.Init() |
| 167 | } |
| 168 | |
| 169 | // Module factory for host shared libraries |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 170 | func LibraryHostSharedFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 171 | module, library := NewLibrary(android.HostSupported) |
| 172 | library.BuildOnlyShared() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 173 | return module.Init() |
| 174 | } |
| 175 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 176 | // Module factory for header-only libraries |
Steven Moreland | f9e6216 | 2017-11-02 17:00:50 -0700 | [diff] [blame] | 177 | func LibraryHeaderFactory() android.Module { |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 178 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 179 | library.HeaderOnly() |
| 180 | return module.Init() |
| 181 | } |
| 182 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 183 | type flagExporter struct { |
| 184 | Properties FlagExporterProperties |
| 185 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 186 | flags []string |
| 187 | flagsDeps android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 190 | func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths { |
Steven Moreland | b21df8f | 2018-01-05 14:42:54 -0800 | [diff] [blame] | 191 | if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil { |
| 192 | return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs) |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 193 | } else { |
| 194 | return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs) |
| 195 | } |
| 196 | } |
| 197 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 198 | func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) { |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 199 | includeDirs := f.exportedIncludes(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | for _, dir := range includeDirs.Strings() { |
| 201 | f.flags = append(f.flags, inc+dir) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func (f *flagExporter) reexportFlags(flags []string) { |
| 206 | f.flags = append(f.flags, flags...) |
| 207 | } |
| 208 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 209 | func (f *flagExporter) reexportDeps(deps android.Paths) { |
| 210 | f.flagsDeps = append(f.flagsDeps, deps...) |
| 211 | } |
| 212 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 213 | func (f *flagExporter) exportedFlags() []string { |
| 214 | return f.flags |
| 215 | } |
| 216 | |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 217 | func (f *flagExporter) exportedFlagsDeps() android.Paths { |
| 218 | return f.flagsDeps |
| 219 | } |
| 220 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 221 | type exportedFlagsProducer interface { |
| 222 | exportedFlags() []string |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 223 | exportedFlagsDeps() android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | var _ exportedFlagsProducer = (*flagExporter)(nil) |
| 227 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 228 | // libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific |
| 229 | // functionality: static vs. shared linkage, reusing object files for shared libraries |
| 230 | type libraryDecorator struct { |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 231 | Properties LibraryProperties |
| 232 | MutatedProperties LibraryMutatedProperties |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 233 | |
| 234 | // For reusing static library objects for shared library |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 235 | reuseObjects Objects |
| 236 | reuseExportedFlags []string |
Colin Cross | bbc9f4d | 2017-05-03 16:24:55 -0700 | [diff] [blame] | 237 | reuseExportedDeps android.Paths |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 238 | |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 239 | // table-of-contents file to optimize out relinking when possible |
| 240 | tocFile android.OptionalPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 241 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 242 | flagExporter |
| 243 | stripper |
| 244 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 245 | // If we're used as a whole_static_lib, our missing dependencies need |
| 246 | // to be given |
| 247 | wholeStaticMissingDeps []string |
| 248 | |
| 249 | // For whole_static_libs |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 250 | objects Objects |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 251 | |
| 252 | // Uses the module's name if empty, but can be overridden. Does not include |
| 253 | // shlib suffix. |
| 254 | libName string |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 255 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 256 | sabi *sabi |
| 257 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 258 | // Output archive of gcno coverage information files |
| 259 | coverageOutputFile android.OptionalPath |
| 260 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 261 | // linked Source Abi Dump |
| 262 | sAbiOutputFile android.OptionalPath |
| 263 | |
| 264 | // Source Abi Diff |
| 265 | sAbiDiff android.OptionalPath |
| 266 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 267 | // Location of the static library in the sysroot. Empty if the library is |
| 268 | // not included in the NDK. |
| 269 | ndkSysrootPath android.Path |
| 270 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 271 | // Location of the linked, unstripped library for shared libraries |
| 272 | unstrippedOutputFile android.Path |
| 273 | |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 274 | // Location of the file that should be copied to dist dir when requested |
| 275 | distFile android.OptionalPath |
| 276 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 277 | versionScriptPath android.ModuleGenPath |
| 278 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 279 | // Decorated interafaces |
| 280 | *baseCompiler |
| 281 | *baseLinker |
| 282 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 285 | func (library *libraryDecorator) linkerProps() []interface{} { |
| 286 | var props []interface{} |
| 287 | props = append(props, library.baseLinker.linkerProps()...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 288 | return append(props, |
| 289 | &library.Properties, |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 290 | &library.MutatedProperties, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 291 | &library.flagExporter.Properties, |
Colin Cross | 22f3795 | 2018-09-05 10:43:13 -0700 | [diff] [blame] | 292 | &library.stripper.StripProperties) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 295 | func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 296 | flags = library.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 297 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 298 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 299 | // all code is position independent, and then those warnings get promoted to |
| 300 | // errors. |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 301 | if !ctx.Windows() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 302 | flags.CFlags = append(flags.CFlags, "-fPIC") |
| 303 | } |
| 304 | |
| 305 | if library.static() { |
| 306 | flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...) |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 307 | } else if library.shared() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 308 | flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...) |
| 309 | } |
| 310 | |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 311 | if library.shared() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 312 | libName := library.getLibName(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 313 | var f []string |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 314 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 315 | f = append(f, |
| 316 | "-nostdlib", |
| 317 | "-Wl,--gc-sections", |
| 318 | ) |
| 319 | } |
| 320 | |
| 321 | if ctx.Darwin() { |
| 322 | f = append(f, |
| 323 | "-dynamiclib", |
| 324 | "-single_module", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 325 | "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(), |
| 326 | ) |
Colin Cross | 7863cf5 | 2016-10-20 10:47:21 -0700 | [diff] [blame] | 327 | if ctx.Arch().ArchType == android.X86 { |
| 328 | f = append(f, |
| 329 | "-read_only_relocs suppress", |
| 330 | ) |
| 331 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 332 | } else { |
| 333 | f = append(f, |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 334 | "-shared", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 335 | "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix()) |
| 336 | } |
| 337 | |
| 338 | flags.LdFlags = append(f, flags.LdFlags...) |
| 339 | } |
| 340 | |
| 341 | return flags |
| 342 | } |
| 343 | |
Colin Cross | f18e110 | 2017-11-16 14:33:08 -0800 | [diff] [blame] | 344 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 345 | exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) |
Dan Willemsen | 273af7f | 2016-11-03 15:53:42 -0700 | [diff] [blame] | 346 | if len(exportIncludeDirs) > 0 { |
Colin Cross | dad8c95 | 2017-04-26 14:55:27 -0700 | [diff] [blame] | 347 | f := includeDirsToFlags(exportIncludeDirs) |
| 348 | flags.GlobalFlags = append(flags.GlobalFlags, f) |
| 349 | flags.YasmFlags = append(flags.YasmFlags, f) |
Dan Willemsen | 273af7f | 2016-11-03 15:53:42 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 352 | flags = library.baseCompiler.compilerFlags(ctx, flags, deps) |
| 353 | if library.buildStubs() { |
Jiyong Park | 6437995 | 2018-12-13 18:37:29 +0900 | [diff] [blame] | 354 | // Remove -include <file> when compiling stubs. Otherwise, the force included |
| 355 | // headers might cause conflicting types error with the symbols in the |
| 356 | // generated stubs source code. e.g. |
| 357 | // double acos(double); // in header |
| 358 | // void acos() {} // in the generated source code |
| 359 | removeInclude := func(flags []string) []string { |
| 360 | ret := flags[:0] |
| 361 | for _, f := range flags { |
| 362 | if strings.HasPrefix(f, "-include ") { |
| 363 | continue |
| 364 | } |
| 365 | ret = append(ret, f) |
| 366 | } |
| 367 | return ret |
| 368 | } |
| 369 | flags.GlobalFlags = removeInclude(flags.GlobalFlags) |
| 370 | flags.CFlags = removeInclude(flags.CFlags) |
| 371 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 372 | flags = addStubLibraryCompilerFlags(flags) |
| 373 | } |
| 374 | return flags |
Dan Willemsen | 273af7f | 2016-11-03 15:53:42 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 377 | func extractExportIncludesFromFlags(flags []string) []string { |
| 378 | // This method is used in the generation of rules which produce |
| 379 | // abi-dumps for source files. Exported headers are needed to infer the |
| 380 | // abi exported by a library and filter out the rest of the abi dumped |
| 381 | // from a source. We extract the include flags exported by a library. |
| 382 | // This includes the flags exported which are re-exported from static |
| 383 | // library dependencies, exported header library dependencies and |
Jayant Chowdhary | af6eb71 | 2017-08-23 16:08:29 -0700 | [diff] [blame] | 384 | // generated header dependencies. -isystem headers are not included |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 385 | // since for bionic libraries, abi-filtering is taken care of by version |
| 386 | // scripts. |
| 387 | var exportedIncludes []string |
| 388 | for _, flag := range flags { |
| 389 | if strings.HasPrefix(flag, "-I") { |
| 390 | exportedIncludes = append(exportedIncludes, flag) |
| 391 | } |
| 392 | } |
| 393 | return exportedIncludes |
| 394 | } |
| 395 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 396 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 397 | if library.buildStubs() { |
Jiyong Park | 3fd0baf | 2018-12-07 16:25:39 +0900 | [diff] [blame^] | 398 | objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex") |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 399 | library.versionScriptPath = versionScript |
| 400 | return objs |
| 401 | } |
| 402 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 403 | if !library.buildShared() && !library.buildStatic() { |
| 404 | if len(library.baseCompiler.Properties.Srcs) > 0 { |
| 405 | ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs") |
| 406 | } |
| 407 | if len(library.Properties.Static.Srcs) > 0 { |
| 408 | ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs") |
| 409 | } |
| 410 | if len(library.Properties.Shared.Srcs) > 0 { |
| 411 | ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs") |
| 412 | } |
| 413 | return Objects{} |
| 414 | } |
Logan Chien | 2f2b890 | 2018-07-10 15:01:19 +0800 | [diff] [blame] | 415 | if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps { |
Jayant Chowdhary | a4fce19 | 2017-09-06 13:10:03 -0700 | [diff] [blame] | 416 | exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 417 | var SourceAbiFlags []string |
| 418 | for _, dir := range exportIncludeDirs.Strings() { |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 419 | SourceAbiFlags = append(SourceAbiFlags, "-I"+dir) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 420 | } |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 421 | for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) { |
| 422 | SourceAbiFlags = append(SourceAbiFlags, reexportedInclude) |
| 423 | } |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 424 | flags.SAbiFlags = SourceAbiFlags |
| 425 | total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) + |
| 426 | len(library.Properties.Static.Srcs) |
| 427 | if total_length > 0 { |
| 428 | flags.SAbiDump = true |
| 429 | } |
| 430 | } |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 431 | objs := library.baseCompiler.compile(ctx, flags, deps) |
| 432 | library.reuseObjects = objs |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 433 | buildFlags := flagsToBuilderFlags(flags) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 434 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 435 | if library.static() { |
dimitry | 0345ad8 | 2018-12-05 16:28:14 +0100 | [diff] [blame] | 436 | srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil) |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 437 | objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary, |
Pirama Arumuga Nainar | f231b19 | 2018-01-23 10:49:04 -0800 | [diff] [blame] | 438 | srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps)) |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 439 | } else if library.shared() { |
dimitry | 0345ad8 | 2018-12-05 16:28:14 +0100 | [diff] [blame] | 440 | srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil) |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 441 | objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary, |
Pirama Arumuga Nainar | f231b19 | 2018-01-23 10:49:04 -0800 | [diff] [blame] | 442 | srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps)) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 445 | return objs |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | type libraryInterface interface { |
| 449 | getWholeStaticMissingDeps() []string |
| 450 | static() bool |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 451 | objs() Objects |
Colin Cross | bbc9f4d | 2017-05-03 16:24:55 -0700 | [diff] [blame] | 452 | reuseObjs() (Objects, []string, android.Paths) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 453 | toc() android.OptionalPath |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 454 | |
| 455 | // Returns true if the build options for the module have selected a static or shared build |
| 456 | buildStatic() bool |
| 457 | buildShared() bool |
| 458 | |
| 459 | // Sets whether a specific variant is static or shared |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 460 | setStatic() |
| 461 | setShared() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | func (library *libraryDecorator) getLibName(ctx ModuleContext) string { |
| 465 | name := library.libName |
| 466 | if name == "" { |
dimitry | d95964a | 2018-11-07 13:43:34 +0100 | [diff] [blame] | 467 | name = String(library.Properties.Stem) |
| 468 | if name == "" { |
| 469 | name = ctx.baseModuleName() |
| 470 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 473 | if ctx.isVndkExt() { |
| 474 | name = ctx.getVndkExtendsModuleName() |
| 475 | } |
| 476 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 477 | if ctx.Host() && Bool(library.Properties.Unique_host_soname) { |
| 478 | if !strings.HasSuffix(name, "-host") { |
| 479 | name = name + "-host" |
| 480 | } |
| 481 | } |
| 482 | |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 483 | return name + library.MutatedProperties.VariantName |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 486 | var versioningMacroNamesListMutex sync.Mutex |
| 487 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 488 | func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) { |
| 489 | location := InstallInSystem |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 490 | if library.baseLinker.sanitize.inSanitizerDir() { |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 491 | location = InstallInSanitizerDir |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 492 | } |
| 493 | library.baseInstaller.location = location |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 494 | library.baseLinker.linkerInit(ctx) |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 495 | // Let baseLinker know whether this variant is for stubs or not, so that |
| 496 | // it can omit things that are not required for linking stubs. |
| 497 | library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs() |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 498 | |
| 499 | if library.buildStubs() { |
| 500 | macroNames := versioningMacroNamesList(ctx.Config()) |
| 501 | myName := versioningMacroName(ctx.ModuleName()) |
| 502 | versioningMacroNamesListMutex.Lock() |
| 503 | defer versioningMacroNamesListMutex.Unlock() |
| 504 | if (*macroNames)[myName] == "" { |
| 505 | (*macroNames)[myName] = ctx.ModuleName() |
| 506 | } else if (*macroNames)[myName] != ctx.ModuleName() { |
| 507 | ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName]) |
| 508 | } |
| 509 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 510 | } |
| 511 | |
dimitry | 0345ad8 | 2018-12-05 16:28:14 +0100 | [diff] [blame] | 512 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 513 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 514 | |
| 515 | if library.static() { |
| 516 | android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs) |
| 517 | } else if library.shared() { |
| 518 | android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs) |
| 519 | } |
| 520 | |
| 521 | return deps |
| 522 | } |
| 523 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 524 | func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Dan Willemsen | 3a26eef | 2018-12-03 15:25:46 -0800 | [diff] [blame] | 525 | if library.static() { |
| 526 | if library.Properties.Static.System_shared_libs != nil { |
| 527 | library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs |
| 528 | } |
| 529 | } else if library.shared() { |
| 530 | if library.Properties.Shared.System_shared_libs != nil { |
| 531 | library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs |
| 532 | } |
| 533 | } |
| 534 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 535 | deps = library.baseLinker.linkerDeps(ctx, deps) |
| 536 | |
| 537 | if library.static() { |
| 538 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, |
| 539 | library.Properties.Static.Whole_static_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 540 | deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...) |
| 541 | deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...) |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 542 | } else if library.shared() { |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 543 | if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 544 | if !ctx.useSdk() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 545 | deps.CrtBegin = "crtbegin_so" |
| 546 | deps.CrtEnd = "crtend_so" |
| 547 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 548 | // TODO(danalbert): Add generation of crt objects. |
| 549 | // For `sdk_version: "current"`, we don't actually have a |
| 550 | // freshly generated set of CRT objects. Use the last stable |
| 551 | // version. |
| 552 | version := ctx.sdkVersion() |
| 553 | if version == "current" { |
Jayant Chowdhary | 6e8115a | 2017-05-09 10:21:52 -0700 | [diff] [blame] | 554 | version = getCurrentNdkPrebuiltVersion(ctx) |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 555 | } |
| 556 | deps.CrtBegin = "ndk_crtbegin_so." + version |
| 557 | deps.CrtEnd = "ndk_crtend_so." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...) |
| 561 | deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...) |
| 562 | deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...) |
| 563 | } |
Jiyong Park | 52d25bd | 2017-10-13 09:17:01 +0900 | [diff] [blame] | 564 | if ctx.useVndk() { |
| 565 | deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs) |
| 566 | deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs) |
| 567 | deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs) |
| 568 | } |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 569 | if ctx.inRecovery() { |
| 570 | deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs) |
| 571 | deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs) |
| 572 | deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs) |
| 573 | } |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 574 | |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 575 | android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list) |
| 576 | android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list) |
| 577 | android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list) |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 578 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 579 | return deps |
| 580 | } |
| 581 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 582 | func (library *libraryDecorator) linkStatic(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 583 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 584 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 585 | library.objects = deps.WholeStaticLibObjs.Copy() |
| 586 | library.objects = library.objects.Append(objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 587 | |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 588 | fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension |
| 589 | outputFile := android.PathForModuleOut(ctx, fileName) |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 590 | builderFlags := flagsToBuilderFlags(flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 591 | |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 592 | if Bool(library.baseLinker.Properties.Use_version_lib) { |
| 593 | if ctx.Host() { |
| 594 | versionedOutputFile := outputFile |
| 595 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 596 | library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 597 | } else { |
| 598 | versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) |
| 599 | library.distFile = android.OptionalPathForPath(versionedOutputFile) |
| 600 | library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 601 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 604 | TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles) |
| 605 | |
| 606 | library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags, |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 607 | ctx.ModuleName()+library.MutatedProperties.VariantName) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 608 | |
| 609 | library.wholeStaticMissingDeps = ctx.GetMissingDependencies() |
| 610 | |
| 611 | ctx.CheckbuildFile(outputFile) |
| 612 | |
| 613 | return outputFile |
| 614 | } |
| 615 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 616 | func (library *libraryDecorator) linkShared(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 617 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 618 | |
| 619 | var linkerDeps android.Paths |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 620 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 621 | |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 622 | unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list") |
| 623 | forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list") |
| 624 | forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 625 | if !ctx.Darwin() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 626 | if unexportedSymbols.Valid() { |
| 627 | ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin") |
| 628 | } |
| 629 | if forceNotWeakSymbols.Valid() { |
| 630 | ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin") |
| 631 | } |
| 632 | if forceWeakSymbols.Valid() { |
| 633 | ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin") |
| 634 | } |
| 635 | } else { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 636 | if unexportedSymbols.Valid() { |
| 637 | flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String()) |
| 638 | linkerDeps = append(linkerDeps, unexportedSymbols.Path()) |
| 639 | } |
| 640 | if forceNotWeakSymbols.Valid() { |
| 641 | flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String()) |
| 642 | linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path()) |
| 643 | } |
| 644 | if forceWeakSymbols.Valid() { |
| 645 | flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String()) |
| 646 | linkerDeps = append(linkerDeps, forceWeakSymbols.Path()) |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
| 651 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 652 | ret := outputFile |
| 653 | |
| 654 | builderFlags := flagsToBuilderFlags(flags) |
| 655 | |
Colin Cross | b496cfd | 2018-09-10 16:50:05 -0700 | [diff] [blame] | 656 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 657 | // depending on a table of contents file instead of the library itself. |
| 658 | tocPath := outputFile.RelPathString() |
| 659 | tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc") |
| 660 | tocFile := android.PathForOutput(ctx, tocPath) |
| 661 | library.tocFile = android.OptionalPathForPath(tocFile) |
| 662 | TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags) |
Colin Cross | 89562dc | 2016-10-03 17:47:19 -0700 | [diff] [blame] | 663 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 664 | if library.stripper.needsStrip(ctx) { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 665 | // b/80093681, GNU strip/objcopy bug. |
| 666 | // Use llvm-{strip,objcopy} when clang lld is used. |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 667 | builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 668 | strippedOutputFile := outputFile |
| 669 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 670 | library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags) |
| 671 | } |
| 672 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 673 | library.unstrippedOutputFile = outputFile |
| 674 | |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 675 | if Bool(library.baseLinker.Properties.Use_version_lib) { |
| 676 | if ctx.Host() { |
| 677 | versionedOutputFile := outputFile |
| 678 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 679 | library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 680 | } else { |
| 681 | versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) |
| 682 | library.distFile = android.OptionalPathForPath(versionedOutputFile) |
| 683 | |
| 684 | if library.stripper.needsStrip(ctx) { |
| 685 | out := android.PathForModuleOut(ctx, "versioned-stripped", fileName) |
| 686 | library.distFile = android.OptionalPathForPath(out) |
| 687 | library.stripper.strip(ctx, versionedOutputFile, out, builderFlags) |
| 688 | } |
| 689 | |
| 690 | library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 691 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 694 | sharedLibs := deps.SharedLibs |
| 695 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
| 696 | |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 697 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 698 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 699 | linkerDeps = append(linkerDeps, objs.tidyFiles...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 700 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 701 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 702 | deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs, |
| 703 | linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile) |
| 704 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 705 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) |
| 706 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 707 | |
| 708 | objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...) |
| 709 | objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...) |
| 710 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 711 | library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx)) |
Jayant Chowdhary | 6ab3d84 | 2017-06-26 12:52:58 -0700 | [diff] [blame] | 712 | library.linkSAbiDumpFiles(ctx, objs, fileName, ret) |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 713 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 714 | return ret |
| 715 | } |
| 716 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 717 | func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path { |
Logan Chien | f4b79c6 | 2018-08-02 02:27:02 +0800 | [diff] [blame] | 718 | isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs) |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 719 | |
| 720 | refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false) |
| 721 | refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true) |
| 722 | |
| 723 | if refAbiDumpTextFile.Valid() { |
| 724 | if refAbiDumpGzipFile.Valid() { |
| 725 | ctx.ModuleErrorf( |
| 726 | "Two reference ABI dump files are found: %q and %q. Please delete the stale one.", |
| 727 | refAbiDumpTextFile, refAbiDumpGzipFile) |
| 728 | return nil |
| 729 | } |
| 730 | return refAbiDumpTextFile.Path() |
| 731 | } |
| 732 | if refAbiDumpGzipFile.Valid() { |
| 733 | return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName) |
| 734 | } |
| 735 | return nil |
| 736 | } |
| 737 | |
Jayant Chowdhary | 6ab3d84 | 2017-06-26 12:52:58 -0700 | [diff] [blame] | 738 | func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) { |
Logan Chien | 2f2b890 | 2018-07-10 15:01:19 +0800 | [diff] [blame] | 739 | if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() { |
Logan Chien | a8f5158 | 2018-03-21 11:53:48 +0800 | [diff] [blame] | 740 | vndkVersion := ctx.DeviceConfig().PlatformVndkVersion() |
| 741 | if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" { |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 742 | vndkVersion = ver |
| 743 | } |
| 744 | |
Jayant Chowdhary | a4fce19 | 2017-09-06 13:10:03 -0700 | [diff] [blame] | 745 | exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 746 | var SourceAbiFlags []string |
| 747 | for _, dir := range exportIncludeDirs.Strings() { |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 748 | SourceAbiFlags = append(SourceAbiFlags, "-I"+dir) |
| 749 | } |
| 750 | for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) { |
| 751 | SourceAbiFlags = append(SourceAbiFlags, reexportedInclude) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 752 | } |
| 753 | exportedHeaderFlags := strings.Join(SourceAbiFlags, " ") |
Jayant Chowdhary | df344d5 | 2018-01-17 11:11:42 -0800 | [diff] [blame] | 754 | library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags) |
Logan Chien | 2f2b890 | 2018-07-10 15:01:19 +0800 | [diff] [blame] | 755 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 756 | refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName) |
| 757 | if refAbiDumpFile != nil { |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 758 | library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 759 | refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt()) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 764 | func (library *libraryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 765 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 766 | |
Colin Cross | ad59e75 | 2017-11-16 14:29:11 -0800 | [diff] [blame] | 767 | objs = deps.Objs.Copy().Append(objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 768 | var out android.Path |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 769 | if library.static() || library.header() { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 770 | out = library.linkStatic(ctx, flags, deps, objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 771 | } else { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 772 | out = library.linkShared(ctx, flags, deps, objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | library.exportIncludes(ctx, "-I") |
| 776 | library.reexportFlags(deps.ReexportedFlags) |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 777 | library.reexportDeps(deps.ReexportedFlagsDeps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 778 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 779 | if Bool(library.Properties.Aidl.Export_aidl_headers) { |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 780 | if library.baseCompiler.hasSrcExt(".aidl") { |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 781 | flags := []string{ |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 782 | "-I" + android.PathForModuleGen(ctx, "aidl").String(), |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 783 | } |
| 784 | library.reexportFlags(flags) |
| 785 | library.reuseExportedFlags = append(library.reuseExportedFlags, flags...) |
Pirama Arumuga Nainar | f231b19 | 2018-01-23 10:49:04 -0800 | [diff] [blame] | 786 | library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps |
| 787 | library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 791 | if Bool(library.Properties.Proto.Export_proto_headers) { |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 792 | if library.baseCompiler.hasSrcExt(".proto") { |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 793 | includes := []string{} |
| 794 | if flags.ProtoRoot { |
| 795 | includes = append(includes, "-I"+android.ProtoSubDir(ctx).String()) |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 796 | } |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 797 | includes = append(includes, "-I"+android.ProtoDir(ctx).String()) |
| 798 | library.reexportFlags(includes) |
| 799 | library.reuseExportedFlags = append(library.reuseExportedFlags, includes...) |
Pirama Arumuga Nainar | f231b19 | 2018-01-23 10:49:04 -0800 | [diff] [blame] | 800 | library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps |
| 801 | library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 805 | if library.baseCompiler.hasSrcExt(".sysprop") { |
| 806 | flags := []string{ |
| 807 | "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(), |
| 808 | } |
| 809 | library.reexportFlags(flags) |
| 810 | library.reuseExportedFlags = append(library.reuseExportedFlags, flags...) |
| 811 | } |
| 812 | |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 813 | if library.buildStubs() { |
| 814 | library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()}) |
| 815 | } |
| 816 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 817 | return out |
| 818 | } |
| 819 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 820 | func (library *libraryDecorator) buildStatic() bool { |
Colin Cross | 38b40df | 2018-04-10 16:14:46 -0700 | [diff] [blame] | 821 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 824 | func (library *libraryDecorator) buildShared() bool { |
Colin Cross | 38b40df | 2018-04-10 16:14:46 -0700 | [diff] [blame] | 825 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 828 | func (library *libraryDecorator) getWholeStaticMissingDeps() []string { |
Colin Cross | d2343a3 | 2018-04-30 14:50:01 -0700 | [diff] [blame] | 829 | return append([]string(nil), library.wholeStaticMissingDeps...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 832 | func (library *libraryDecorator) objs() Objects { |
| 833 | return library.objects |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Colin Cross | bbc9f4d | 2017-05-03 16:24:55 -0700 | [diff] [blame] | 836 | func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) { |
| 837 | return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 840 | func (library *libraryDecorator) toc() android.OptionalPath { |
| 841 | return library.tocFile |
| 842 | } |
| 843 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 844 | func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) { |
Colin Cross | c43ae77 | 2017-04-14 15:42:53 -0700 | [diff] [blame] | 845 | if library.shared() { |
Justin Yun | 8fe1212 | 2017-12-07 17:18:15 +0900 | [diff] [blame] | 846 | if ctx.Device() && ctx.useVndk() { |
| 847 | if ctx.isVndkSp() { |
| 848 | library.baseInstaller.subDir = "vndk-sp" |
| 849 | } else if ctx.isVndk() { |
| 850 | library.baseInstaller.subDir = "vndk" |
| 851 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 852 | |
| 853 | // Append a version to vndk or vndk-sp directories on the system partition. |
| 854 | if ctx.isVndk() && !ctx.isVndkExt() { |
| 855 | vndkVersion := ctx.DeviceConfig().PlatformVndkVersion() |
| 856 | if vndkVersion != "current" && vndkVersion != "" { |
| 857 | library.baseInstaller.subDir += "-" + vndkVersion |
| 858 | } |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 859 | } |
| 860 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 861 | library.baseInstaller.install(ctx, file) |
| 862 | } |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 863 | |
Dan Albert | 281f22b | 2017-12-13 15:03:47 -0800 | [diff] [blame] | 864 | if Bool(library.Properties.Static_ndk_lib) && library.static() && |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 865 | !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() && |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 866 | library.baseLinker.sanitize.isUnsanitizedVariant() && |
| 867 | !library.buildStubs() { |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 868 | installPath := getNdkSysrootBase(ctx).Join( |
Dan Albert | ea4b7b9 | 2018-04-25 16:05:30 -0700 | [diff] [blame] | 869 | ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base()) |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 870 | |
| 871 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 872 | Rule: android.Cp, |
| 873 | Description: "install " + installPath.Base(), |
| 874 | Output: installPath, |
| 875 | Input: file, |
| 876 | }) |
| 877 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 878 | library.ndkSysrootPath = installPath |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 879 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 882 | func (library *libraryDecorator) static() bool { |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 883 | return library.MutatedProperties.VariantIsStatic |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 886 | func (library *libraryDecorator) shared() bool { |
| 887 | return library.MutatedProperties.VariantIsShared |
| 888 | } |
| 889 | |
| 890 | func (library *libraryDecorator) header() bool { |
| 891 | return !library.static() && !library.shared() |
| 892 | } |
| 893 | |
| 894 | func (library *libraryDecorator) setStatic() { |
| 895 | library.MutatedProperties.VariantIsStatic = true |
| 896 | library.MutatedProperties.VariantIsShared = false |
| 897 | } |
| 898 | |
| 899 | func (library *libraryDecorator) setShared() { |
| 900 | library.MutatedProperties.VariantIsStatic = false |
| 901 | library.MutatedProperties.VariantIsShared = true |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 904 | func (library *libraryDecorator) BuildOnlyStatic() { |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 905 | library.MutatedProperties.BuildShared = false |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | func (library *libraryDecorator) BuildOnlyShared() { |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 909 | library.MutatedProperties.BuildStatic = false |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 912 | func (library *libraryDecorator) HeaderOnly() { |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 913 | library.MutatedProperties.BuildShared = false |
| 914 | library.MutatedProperties.BuildStatic = false |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 917 | func (library *libraryDecorator) buildStubs() bool { |
| 918 | return library.MutatedProperties.BuildStubs |
| 919 | } |
| 920 | |
| 921 | func (library *libraryDecorator) stubsVersion() string { |
| 922 | return library.MutatedProperties.StubsVersion |
| 923 | } |
| 924 | |
Jiyong Park | da732bd | 2018-11-02 18:23:15 +0900 | [diff] [blame] | 925 | func versioningMacroNamesList(config android.Config) *map[string]string { |
| 926 | return config.Once("versioningMacroNamesList", func() interface{} { |
| 927 | m := make(map[string]string) |
| 928 | return &m |
| 929 | }).(*map[string]string) |
| 930 | } |
| 931 | |
| 932 | // alphanumeric and _ characters are preserved. |
| 933 | // other characters are all converted to _ |
| 934 | var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 935 | |
| 936 | func versioningMacroName(moduleName string) string { |
| 937 | macroName := charsNotForMacro.ReplaceAllString(moduleName, "_") |
| 938 | macroName = strings.ToUpper(moduleName) |
| 939 | return "__" + macroName + "_API__" |
| 940 | } |
| 941 | |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 942 | func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 943 | module := newModule(hod, android.MultilibBoth) |
| 944 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 945 | library := &libraryDecorator{ |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 946 | MutatedProperties: LibraryMutatedProperties{ |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 947 | BuildShared: true, |
| 948 | BuildStatic: true, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 949 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 950 | baseCompiler: NewBaseCompiler(), |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 951 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 952 | baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem), |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 953 | sabi: module.sabi, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 956 | module.compiler = library |
| 957 | module.linker = library |
| 958 | module.installer = library |
| 959 | |
| 960 | return module, library |
| 961 | } |
| 962 | |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 963 | // connects a shared library to a static library in order to reuse its .o files to avoid |
| 964 | // compiling source files twice. |
| 965 | func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) { |
| 966 | if staticCompiler, ok := static.compiler.(*libraryDecorator); ok { |
| 967 | sharedCompiler := shared.compiler.(*libraryDecorator) |
Dan Willemsen | 3a26eef | 2018-12-03 15:25:46 -0800 | [diff] [blame] | 968 | |
| 969 | // Check libraries in addition to cflags, since libraries may be exporting different |
| 970 | // include directories. |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 971 | if len(staticCompiler.Properties.Static.Cflags) == 0 && |
Dan Willemsen | 3a26eef | 2018-12-03 15:25:46 -0800 | [diff] [blame] | 972 | len(sharedCompiler.Properties.Shared.Cflags) == 0 && |
| 973 | len(staticCompiler.Properties.Static.Whole_static_libs) == 0 && |
| 974 | len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 && |
| 975 | len(staticCompiler.Properties.Static.Static_libs) == 0 && |
| 976 | len(sharedCompiler.Properties.Shared.Static_libs) == 0 && |
| 977 | len(staticCompiler.Properties.Static.Shared_libs) == 0 && |
| 978 | len(sharedCompiler.Properties.Shared.Shared_libs) == 0 && |
| 979 | staticCompiler.Properties.Static.System_shared_libs == nil && |
| 980 | sharedCompiler.Properties.Shared.System_shared_libs == nil { |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 981 | |
| 982 | mctx.AddInterVariantDependency(reuseObjTag, shared, static) |
| 983 | sharedCompiler.baseCompiler.Properties.OriginalSrcs = |
| 984 | sharedCompiler.baseCompiler.Properties.Srcs |
| 985 | sharedCompiler.baseCompiler.Properties.Srcs = nil |
| 986 | sharedCompiler.baseCompiler.Properties.Generated_sources = nil |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 991 | func LinkageMutator(mctx android.BottomUpMutatorContext) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 992 | if m, ok := mctx.Module().(*Module); ok && m.linker != nil { |
| 993 | if library, ok := m.linker.(libraryInterface); ok { |
| 994 | var modules []blueprint.Module |
| 995 | if library.buildStatic() && library.buildShared() { |
| 996 | modules = mctx.CreateLocalVariations("static", "shared") |
| 997 | static := modules[0].(*Module) |
| 998 | shared := modules[1].(*Module) |
| 999 | |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 1000 | static.linker.(libraryInterface).setStatic() |
| 1001 | shared.linker.(libraryInterface).setShared() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 1002 | |
Colin Cross | 10d2231 | 2017-05-03 11:01:58 -0700 | [diff] [blame] | 1003 | reuseStaticLibrary(mctx, static, shared) |
| 1004 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 1005 | } else if library.buildStatic() { |
| 1006 | modules = mctx.CreateLocalVariations("static") |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 1007 | modules[0].(*Module).linker.(libraryInterface).setStatic() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 1008 | } else if library.buildShared() { |
| 1009 | modules = mctx.CreateLocalVariations("shared") |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 1010 | modules[0].(*Module).linker.(libraryInterface).setShared() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1014 | } |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1015 | |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1016 | // maps a module name to the list of stubs versions available for the module |
| 1017 | func stubsVersionsFor(config android.Config) map[string][]string { |
| 1018 | return config.Once("stubVersions", func() interface{} { |
| 1019 | return make(map[string][]string) |
| 1020 | }).(map[string][]string) |
| 1021 | } |
| 1022 | |
| 1023 | var stubsVersionsLock sync.Mutex |
| 1024 | |
| 1025 | func latestStubsVersionFor(config android.Config, name string) string { |
| 1026 | versions, ok := stubsVersionsFor(config)[name] |
| 1027 | if ok && len(versions) > 0 { |
| 1028 | // the versions are alreay sorted in ascending order |
| 1029 | return versions[len(versions)-1] |
| 1030 | } |
| 1031 | return "" |
| 1032 | } |
| 1033 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1034 | // Version mutator splits a module into the mandatory non-stubs variant |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1035 | // (which is unnamed) and zero or more stubs variants. |
| 1036 | func VersionMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1037 | if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1038 | if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() && |
| 1039 | len(library.Properties.Stubs.Versions) > 0 { |
| 1040 | versions := []string{} |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1041 | for _, v := range library.Properties.Stubs.Versions { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1042 | if _, err := strconv.Atoi(v); err != nil { |
| 1043 | mctx.PropertyErrorf("versions", "%q is not a number", v) |
| 1044 | } |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1045 | versions = append(versions, v) |
| 1046 | } |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1047 | sort.Slice(versions, func(i, j int) bool { |
| 1048 | left, _ := strconv.Atoi(versions[i]) |
| 1049 | right, _ := strconv.Atoi(versions[j]) |
| 1050 | return left < right |
| 1051 | }) |
| 1052 | |
| 1053 | // save the list of versions for later use |
| 1054 | copiedVersions := make([]string, len(versions)) |
| 1055 | copy(copiedVersions, versions) |
| 1056 | stubsVersionsLock.Lock() |
| 1057 | defer stubsVersionsLock.Unlock() |
| 1058 | stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions |
| 1059 | |
| 1060 | // "" is for the non-stubs variant |
| 1061 | versions = append(versions, "") |
| 1062 | |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1063 | modules := mctx.CreateVariations(versions...) |
| 1064 | for i, m := range modules { |
| 1065 | l := m.(*Module).linker.(*libraryDecorator) |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 1066 | if versions[i] != "" { |
| 1067 | l.MutatedProperties.BuildStubs = true |
| 1068 | l.MutatedProperties.StubsVersion = versions[i] |
| 1069 | m.(*Module).Properties.HideFromMake = true |
Jiyong Park | 090d9df | 2018-12-11 02:47:16 +0900 | [diff] [blame] | 1070 | m.(*Module).sanitize = nil |
| 1071 | m.(*Module).stl = nil |
Jiyong Park | 127d565 | 2018-12-12 10:26:20 +0900 | [diff] [blame] | 1072 | m.(*Module).Properties.PreventInstall = true |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1073 | } |
Jiyong Park | 7ed9de3 | 2018-10-15 22:25:07 +0900 | [diff] [blame] | 1074 | } |
| 1075 | } else { |
| 1076 | mctx.CreateVariations("") |
| 1077 | } |
| 1078 | return |
| 1079 | } |
| 1080 | if genrule, ok := mctx.Module().(*genrule.Module); ok { |
| 1081 | if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery { |
| 1082 | mctx.CreateVariations("") |
| 1083 | return |
| 1084 | } |
| 1085 | } |
| 1086 | } |