blob: 7216832c1dd88533a8b0f8b9f772e75ada50b10e [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
15package cc
16
17import (
Jiyong Parkf1194352019-02-25 11:05:47 +090018 "path/filepath"
Jiyong Parkda732bd2018-11-02 18:23:15 +090019 "regexp"
Jiyong Park25fc6a92018-11-18 18:02:45 +090020 "sort"
21 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090023 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024
25 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070026 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070027
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070029 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090030 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070031)
32
Colin Crosseefe9a32019-01-22 14:41:08 -080033type StaticSharedLibraryProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080034 Srcs []string `android:"path,arch_variant"`
35 Cflags []string `android:"path,arch_variant"`
Colin Crosseefe9a32019-01-22 14:41:08 -080036
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"`
42
43 Export_shared_lib_headers []string `android:"arch_variant"`
44 Export_static_lib_headers []string `android:"arch_variant"`
45}
46
Colin Crossb916a382016-07-29 17:28:03 -070047type LibraryProperties struct {
Colin Crosseefe9a32019-01-22 14:41:08 -080048 Static StaticSharedLibraryProperties `android:"arch_variant"`
49 Shared StaticSharedLibraryProperties `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070050
Colin Cross4d9c2d12016-07-29 12:48:20 -070051 // local file name to pass to the linker as -unexported_symbols_list
Colin Cross27b922f2019-03-04 22:35:41 -080052 Unexported_symbols_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070053 // local file name to pass to the linker as -force_symbols_not_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080054 Force_symbols_not_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070055 // local file name to pass to the linker as -force_symbols_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080056 Force_symbols_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070057
58 // rename host libraries to prevent overlap with system installed libraries
59 Unique_host_soname *bool
60
Dan Willemsene1240db2016-11-03 14:28:51 -070061 Aidl struct {
62 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080063 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070064 }
65
Colin Cross0c461f12016-10-20 16:11:43 -070066 Proto struct {
67 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080068 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070069 }
Dan Albertf563d252017-10-13 00:29:00 -070070
Inseob Kimc0907f12019-02-08 21:00:45 +090071 Sysprop struct {
72 // Whether platform owns this sysprop library.
73 Platform *bool
Inseob Kimb3f22ca2019-03-05 12:40:24 +090074 } `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +090075
Nan Zhang0007d812017-11-07 10:57:05 -080076 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090077
78 Stubs struct {
79 // Relative path to the symbol map. The symbol map provides the list of
80 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080081 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090082
83 // List versions to generate stubs libs for.
84 Versions []string
85 }
dimitryd95964a2018-11-07 13:43:34 +010086
87 // set the name of the output
88 Stem *string `android:"arch_variant"`
89
90 // Names of modules to be overridden. Listed modules can only be other shared libraries
91 // (in Make or Soong).
92 // This does not completely prevent installation of the overridden libraries, but if both
93 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
94 // from PRODUCT_PACKAGES.
95 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080096
97 // Properties for ABI compatibility checker
98 Header_abi_checker struct {
99 // Path to a symbol file that specifies the symbols to be included in the generated
100 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800102
103 // Symbol versions that should be ignored from the symbol file
104 Exclude_symbol_versions []string
105
106 // Symbol tags that should be ignored from the symbol file
107 Exclude_symbol_tags []string
108 }
Colin Crossa48ab5b2017-02-14 15:28:44 -0800109}
Colin Cross0c461f12016-10-20 16:11:43 -0700110
Colin Crossa48ab5b2017-02-14 15:28:44 -0800111type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700113
114 // Build a static variant
115 BuildStatic bool `blueprint:"mutated"`
116 // Build a shared variant
117 BuildShared bool `blueprint:"mutated"`
118 // This variant is shared
119 VariantIsShared bool `blueprint:"mutated"`
120 // This variant is static
121 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900122
123 // This variant is a stubs lib
124 BuildStubs bool `blueprint:"mutated"`
125 // Version of the stubs lib
126 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700127}
128
129type FlagExporterProperties struct {
130 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700131 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700132 // against this module. Directories listed in export_include_dirs do not need to be
133 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700134 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700135
136 Target struct {
137 Vendor struct {
138 // list of exported include directories, like
139 // export_include_dirs, that will be applied to the
140 // vendor variant of this library. This will overwrite
141 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800142 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700143 }
144 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145}
146
147func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700148 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
149 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
150 android.RegisterModuleType("cc_library", LibraryFactory)
151 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
152 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
153 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154}
155
156// Module factory for combined static + shared libraries, device by default but with possible host
157// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700158func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800159 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 return module.Init()
161}
162
163// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700164func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800165 module, library := NewLibrary(android.HostAndDeviceSupported)
166 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 return module.Init()
168}
169
170// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700171func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800172 module, library := NewLibrary(android.HostAndDeviceSupported)
173 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 return module.Init()
175}
176
177// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700178func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800179 module, library := NewLibrary(android.HostSupported)
180 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 return module.Init()
182}
183
184// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700185func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800186 module, library := NewLibrary(android.HostSupported)
187 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 return module.Init()
189}
190
Colin Cross5950f382016-12-13 12:50:57 -0800191// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700192func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800193 module, library := NewLibrary(android.HostAndDeviceSupported)
194 library.HeaderOnly()
195 return module.Init()
196}
197
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198type flagExporter struct {
199 Properties FlagExporterProperties
200
Dan Willemsen847dcc72016-09-29 12:13:36 -0700201 flags []string
202 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203}
204
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700205func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800206 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
207 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700208 } else {
209 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
210 }
211}
212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700214 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 for _, dir := range includeDirs.Strings() {
216 f.flags = append(f.flags, inc+dir)
217 }
218}
219
220func (f *flagExporter) reexportFlags(flags []string) {
221 f.flags = append(f.flags, flags...)
222}
223
Dan Willemsen847dcc72016-09-29 12:13:36 -0700224func (f *flagExporter) reexportDeps(deps android.Paths) {
225 f.flagsDeps = append(f.flagsDeps, deps...)
226}
227
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228func (f *flagExporter) exportedFlags() []string {
229 return f.flags
230}
231
Dan Willemsen847dcc72016-09-29 12:13:36 -0700232func (f *flagExporter) exportedFlagsDeps() android.Paths {
233 return f.flagsDeps
234}
235
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236type exportedFlagsProducer interface {
237 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700238 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239}
240
241var _ exportedFlagsProducer = (*flagExporter)(nil)
242
Colin Crossb916a382016-07-29 17:28:03 -0700243// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
244// functionality: static vs. shared linkage, reusing object files for shared libraries
245type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800246 Properties LibraryProperties
247 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248
249 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700250 reuseObjects Objects
251 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700252 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700253
Colin Cross26c34ed2016-09-30 17:10:16 -0700254 // table-of-contents file to optimize out relinking when possible
255 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 flagExporter
258 stripper
259
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 // If we're used as a whole_static_lib, our missing dependencies need
261 // to be given
262 wholeStaticMissingDeps []string
263
264 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700265 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266
267 // Uses the module's name if empty, but can be overridden. Does not include
268 // shlib suffix.
269 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700270
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800271 sabi *sabi
272
Dan Willemsen581341d2017-02-09 16:16:31 -0800273 // Output archive of gcno coverage information files
274 coverageOutputFile android.OptionalPath
275
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800276 // linked Source Abi Dump
277 sAbiOutputFile android.OptionalPath
278
279 // Source Abi Diff
280 sAbiDiff android.OptionalPath
281
Colin Cross0875c522017-11-28 17:34:01 -0800282 // Location of the static library in the sysroot. Empty if the library is
283 // not included in the NDK.
284 ndkSysrootPath android.Path
285
Colin Crossb60190a2018-09-04 16:28:17 -0700286 // Location of the linked, unstripped library for shared libraries
287 unstrippedOutputFile android.Path
288
Dan Willemsen569edc52018-11-19 09:33:29 -0800289 // Location of the file that should be copied to dist dir when requested
290 distFile android.OptionalPath
291
Jiyong Park7ed9de32018-10-15 22:25:07 +0900292 versionScriptPath android.ModuleGenPath
293
Jiyong Parkf1194352019-02-25 11:05:47 +0900294 post_install_cmds []string
295
Colin Crossb916a382016-07-29 17:28:03 -0700296 // Decorated interafaces
297 *baseCompiler
298 *baseLinker
299 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700300}
301
Colin Crossb916a382016-07-29 17:28:03 -0700302func (library *libraryDecorator) linkerProps() []interface{} {
303 var props []interface{}
304 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 return append(props,
306 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800307 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700309 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310}
311
Colin Crossb916a382016-07-29 17:28:03 -0700312func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700313 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314
Colin Crossb916a382016-07-29 17:28:03 -0700315 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
316 // all code is position independent, and then those warnings get promoted to
317 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700318 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700319 flags.CFlags = append(flags.CFlags, "-fPIC")
320 }
321
322 if library.static() {
323 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800324 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700325 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
326 }
327
Colin Crossa48ab5b2017-02-14 15:28:44 -0800328 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700331 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 f = append(f,
333 "-nostdlib",
334 "-Wl,--gc-sections",
335 )
336 }
337
338 if ctx.Darwin() {
339 f = append(f,
340 "-dynamiclib",
341 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
343 )
Colin Cross7863cf52016-10-20 10:47:21 -0700344 if ctx.Arch().ArchType == android.X86 {
345 f = append(f,
346 "-read_only_relocs suppress",
347 )
348 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700349 } else {
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800350 f = append(f, "-shared")
351 if !ctx.Windows() {
352 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
353 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700354 }
355
356 flags.LdFlags = append(f, flags.LdFlags...)
357 }
358
359 return flags
360}
361
Colin Crossf18e1102017-11-16 14:33:08 -0800362func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700363 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700364 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700365 f := includeDirsToFlags(exportIncludeDirs)
366 flags.GlobalFlags = append(flags.GlobalFlags, f)
367 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700368 }
369
Jiyong Park7ed9de32018-10-15 22:25:07 +0900370 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
371 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900372 // Remove -include <file> when compiling stubs. Otherwise, the force included
373 // headers might cause conflicting types error with the symbols in the
374 // generated stubs source code. e.g.
375 // double acos(double); // in header
376 // void acos() {} // in the generated source code
377 removeInclude := func(flags []string) []string {
378 ret := flags[:0]
379 for _, f := range flags {
380 if strings.HasPrefix(f, "-include ") {
381 continue
382 }
383 ret = append(ret, f)
384 }
385 return ret
386 }
387 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
388 flags.CFlags = removeInclude(flags.CFlags)
389
Jiyong Park7ed9de32018-10-15 22:25:07 +0900390 flags = addStubLibraryCompilerFlags(flags)
391 }
392 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700393}
394
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700395func extractExportIncludesFromFlags(flags []string) []string {
396 // This method is used in the generation of rules which produce
397 // abi-dumps for source files. Exported headers are needed to infer the
398 // abi exported by a library and filter out the rest of the abi dumped
399 // from a source. We extract the include flags exported by a library.
400 // This includes the flags exported which are re-exported from static
401 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700402 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700403 // since for bionic libraries, abi-filtering is taken care of by version
404 // scripts.
405 var exportedIncludes []string
406 for _, flag := range flags {
407 if strings.HasPrefix(flag, "-I") {
408 exportedIncludes = append(exportedIncludes, flag)
409 }
410 }
411 return exportedIncludes
412}
413
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700414func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900415 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900416 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900417 library.versionScriptPath = versionScript
418 return objs
419 }
420
Colin Cross5950f382016-12-13 12:50:57 -0800421 if !library.buildShared() && !library.buildStatic() {
422 if len(library.baseCompiler.Properties.Srcs) > 0 {
423 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
424 }
425 if len(library.Properties.Static.Srcs) > 0 {
426 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
427 }
428 if len(library.Properties.Shared.Srcs) > 0 {
429 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
430 }
431 return Objects{}
432 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800433 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700434 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800435 var SourceAbiFlags []string
436 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700437 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800438 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700439 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
440 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
441 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800442 flags.SAbiFlags = SourceAbiFlags
443 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
444 len(library.Properties.Static.Srcs)
445 if total_length > 0 {
446 flags.SAbiDump = true
447 }
448 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700449 objs := library.baseCompiler.compile(ctx, flags, deps)
450 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700451 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700452
Colin Cross4d9c2d12016-07-29 12:48:20 -0700453 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800454 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700455 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800456 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800457 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800458 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700459 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800460 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700461 }
462
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700463 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700464}
465
466type libraryInterface interface {
467 getWholeStaticMissingDeps() []string
468 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700469 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700470 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700471 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700472
473 // Returns true if the build options for the module have selected a static or shared build
474 buildStatic() bool
475 buildShared() bool
476
477 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800478 setStatic()
479 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700480}
481
482func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
483 name := library.libName
484 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100485 name = String(library.Properties.Stem)
486 if name == "" {
487 name = ctx.baseModuleName()
488 }
Colin Crossb916a382016-07-29 17:28:03 -0700489 }
490
Logan Chienf3511742017-10-31 18:04:35 +0800491 if ctx.isVndkExt() {
492 name = ctx.getVndkExtendsModuleName()
493 }
494
Colin Crossb916a382016-07-29 17:28:03 -0700495 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
496 if !strings.HasSuffix(name, "-host") {
497 name = name + "-host"
498 }
499 }
500
Colin Crossa48ab5b2017-02-14 15:28:44 -0800501 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700502}
503
Jiyong Parkda732bd2018-11-02 18:23:15 +0900504var versioningMacroNamesListMutex sync.Mutex
505
Colin Crossb916a382016-07-29 17:28:03 -0700506func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
507 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700508 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700509 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700510 }
511 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700512 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900513 // Let baseLinker know whether this variant is for stubs or not, so that
514 // it can omit things that are not required for linking stubs.
515 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900516
517 if library.buildStubs() {
518 macroNames := versioningMacroNamesList(ctx.Config())
519 myName := versioningMacroName(ctx.ModuleName())
520 versioningMacroNamesListMutex.Lock()
521 defer versioningMacroNamesListMutex.Unlock()
522 if (*macroNames)[myName] == "" {
523 (*macroNames)[myName] = ctx.ModuleName()
524 } else if (*macroNames)[myName] != ctx.ModuleName() {
525 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
526 }
527 }
Colin Crossb916a382016-07-29 17:28:03 -0700528}
529
dimitry0345ad82018-12-05 16:28:14 +0100530func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
531 deps = library.baseCompiler.compilerDeps(ctx, deps)
532
dimitry0345ad82018-12-05 16:28:14 +0100533 return deps
534}
535
Colin Cross37047f12016-12-13 17:06:13 -0800536func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800537 if library.static() {
538 if library.Properties.Static.System_shared_libs != nil {
539 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
540 }
541 } else if library.shared() {
542 if library.Properties.Shared.System_shared_libs != nil {
543 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
544 }
545 }
546
Colin Crossb916a382016-07-29 17:28:03 -0700547 deps = library.baseLinker.linkerDeps(ctx, deps)
548
549 if library.static() {
550 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
551 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
553 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800554
555 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
556 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800557 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800558 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700559 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700560 deps.CrtBegin = "crtbegin_so"
561 deps.CrtEnd = "crtend_so"
562 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800563 // TODO(danalbert): Add generation of crt objects.
564 // For `sdk_version: "current"`, we don't actually have a
565 // freshly generated set of CRT objects. Use the last stable
566 // version.
567 version := ctx.sdkVersion()
568 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700569 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800570 }
571 deps.CrtBegin = "ndk_crtbegin_so." + version
572 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 }
574 }
575 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
576 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
577 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800578
579 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
580 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700581 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900582 if ctx.useVndk() {
583 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
584 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
585 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000586 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
587 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900588 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900589 if ctx.inRecovery() {
590 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
591 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
592 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000593 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
594 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900595 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800596
Colin Cross4d9c2d12016-07-29 12:48:20 -0700597 return deps
598}
599
Colin Crossb916a382016-07-29 17:28:03 -0700600func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700601 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700603 library.objects = deps.WholeStaticLibObjs.Copy()
604 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700605
Colin Cross86803cf2018-02-15 14:12:26 -0800606 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
607 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800608 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700609
Dan Willemsen569edc52018-11-19 09:33:29 -0800610 if Bool(library.baseLinker.Properties.Use_version_lib) {
611 if ctx.Host() {
612 versionedOutputFile := outputFile
613 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
614 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
615 } else {
616 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
617 library.distFile = android.OptionalPathForPath(versionedOutputFile)
618 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
619 }
Colin Cross86803cf2018-02-15 14:12:26 -0800620 }
621
Dan Willemsen581341d2017-02-09 16:16:31 -0800622 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
623
624 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800625 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700626
627 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
628
629 ctx.CheckbuildFile(outputFile)
630
631 return outputFile
632}
633
Colin Crossb916a382016-07-29 17:28:03 -0700634func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700635 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636
637 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700638 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700639
Colin Cross2383f3b2018-02-06 14:40:13 -0800640 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
641 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
642 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700643 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700644 if unexportedSymbols.Valid() {
645 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
646 }
647 if forceNotWeakSymbols.Valid() {
648 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
649 }
650 if forceWeakSymbols.Valid() {
651 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
652 }
653 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654 if unexportedSymbols.Valid() {
655 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
656 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
657 }
658 if forceNotWeakSymbols.Valid() {
659 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
660 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
661 }
662 if forceWeakSymbols.Valid() {
663 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
664 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
665 }
666 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900667 if library.buildStubs() {
668 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
669 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
670 linkerDeps = append(linkerDeps, library.versionScriptPath)
671 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700672
673 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
674 outputFile := android.PathForModuleOut(ctx, fileName)
675 ret := outputFile
676
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800677 var implicitOutputs android.WritablePaths
678 if ctx.Windows() {
679 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "a"))
680
681 flags.LdFlags = append(flags.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
682 implicitOutputs = append(implicitOutputs, importLibraryPath)
683 }
684
Colin Cross4d9c2d12016-07-29 12:48:20 -0700685 builderFlags := flagsToBuilderFlags(flags)
686
Colin Crossb496cfd2018-09-10 16:50:05 -0700687 // Optimize out relinking against shared libraries whose interface hasn't changed by
688 // depending on a table of contents file instead of the library itself.
689 tocPath := outputFile.RelPathString()
690 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
691 tocFile := android.PathForOutput(ctx, tocPath)
692 library.tocFile = android.OptionalPathForPath(tocFile)
693 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700694
Colin Cross4d9c2d12016-07-29 12:48:20 -0700695 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800696 if ctx.Darwin() {
697 builderFlags.stripUseGnuStrip = true
698 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699 strippedOutputFile := outputFile
700 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
701 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
702 }
703
Colin Crossb60190a2018-09-04 16:28:17 -0700704 library.unstrippedOutputFile = outputFile
705
Dan Willemsen569edc52018-11-19 09:33:29 -0800706 if Bool(library.baseLinker.Properties.Use_version_lib) {
707 if ctx.Host() {
708 versionedOutputFile := outputFile
709 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
710 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
711 } else {
712 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
713 library.distFile = android.OptionalPathForPath(versionedOutputFile)
714
715 if library.stripper.needsStrip(ctx) {
716 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
717 library.distFile = android.OptionalPathForPath(out)
718 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
719 }
720
721 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
722 }
Colin Cross86803cf2018-02-15 14:12:26 -0800723 }
724
Jiyong Park64a44f22019-01-18 14:37:08 +0900725 sharedLibs := deps.EarlySharedLibs
726 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
728
Jiyong Park64a44f22019-01-18 14:37:08 +0900729 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700730 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
731 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700732 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700733
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700734 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700735 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800736 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700737
Dan Willemsen581341d2017-02-09 16:16:31 -0800738 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
739 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800740
741 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
742 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
743
Dan Willemsen581341d2017-02-09 16:16:31 -0800744 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700745 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800746
Colin Cross4d9c2d12016-07-29 12:48:20 -0700747 return ret
748}
749
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900750func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
751 return library.unstrippedOutputFile
752}
753
Logan Chien7eefdc42018-07-11 18:10:41 +0800754func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800755 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800756
757 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
758 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
759
760 if refAbiDumpTextFile.Valid() {
761 if refAbiDumpGzipFile.Valid() {
762 ctx.ModuleErrorf(
763 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
764 refAbiDumpTextFile, refAbiDumpGzipFile)
765 return nil
766 }
767 return refAbiDumpTextFile.Path()
768 }
769 if refAbiDumpGzipFile.Valid() {
770 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
771 }
772 return nil
773}
774
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700775func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800776 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800777 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
778 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800779 vndkVersion = ver
780 }
781
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700782 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800783 var SourceAbiFlags []string
784 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700785 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
786 }
787 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
788 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800789 }
790 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800791 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
792 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
793 library.Properties.Header_abi_checker.Exclude_symbol_versions,
794 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800795
Logan Chien7eefdc42018-07-11 18:10:41 +0800796 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
797 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800798 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien6227fed2019-02-18 13:12:21 +0800799 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800800 }
801 }
802}
803
Colin Crossb916a382016-07-29 17:28:03 -0700804func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700805 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700806
Colin Crossad59e752017-11-16 14:29:11 -0800807 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700808 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800809 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700810 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700811 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700812 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700813 }
814
815 library.exportIncludes(ctx, "-I")
816 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700817 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700818
Nan Zhang0007d812017-11-07 10:57:05 -0800819 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700820 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700821 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700822 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700823 }
824 library.reexportFlags(flags)
825 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800826 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
827 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700828 }
829 }
830
Nan Zhang0007d812017-11-07 10:57:05 -0800831 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700832 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800833 includes := []string{}
834 if flags.ProtoRoot {
835 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700836 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800837 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
838 library.reexportFlags(includes)
839 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800840 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
841 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700842 }
843 }
844
Inseob Kim21f26902018-09-06 00:55:20 +0900845 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kimc0907f12019-02-08 21:00:45 +0900846 internalFlags := []string{
Inseob Kim21f26902018-09-06 00:55:20 +0900847 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
848 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900849 systemFlags := []string{
850 "-I" + android.PathForModuleGen(ctx, "sysprop/system", "include").String(),
851 }
852
853 flags := internalFlags
854
855 if library.Properties.Sysprop.Platform != nil {
856 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
857 isVendor := ctx.useVndk()
858 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
859
860 useSystem := isProduct || (isOwnerPlatform == isVendor)
861
862 if useSystem {
863 flags = systemFlags
864 }
865 }
866
Inseob Kim21f26902018-09-06 00:55:20 +0900867 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900868 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900869 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
870 }
871
Jiyong Parkda732bd2018-11-02 18:23:15 +0900872 if library.buildStubs() {
873 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
874 }
875
Colin Cross4d9c2d12016-07-29 12:48:20 -0700876 return out
877}
878
Colin Crossb916a382016-07-29 17:28:03 -0700879func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700880 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700881}
882
Colin Crossb916a382016-07-29 17:28:03 -0700883func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700884 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700885}
886
Colin Crossb916a382016-07-29 17:28:03 -0700887func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700888 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700889}
890
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700891func (library *libraryDecorator) objs() Objects {
892 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700893}
894
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700895func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
896 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700897}
898
Colin Cross26c34ed2016-09-30 17:10:16 -0700899func (library *libraryDecorator) toc() android.OptionalPath {
900 return library.tocFile
901}
902
Jiyong Parkf1194352019-02-25 11:05:47 +0900903func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
904 dir := library.baseInstaller.installDir(ctx)
905 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
906 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
907 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
908 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
909}
910
Colin Crossb916a382016-07-29 17:28:03 -0700911func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700912 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900913 if ctx.Device() && ctx.useVndk() {
914 if ctx.isVndkSp() {
915 library.baseInstaller.subDir = "vndk-sp"
916 } else if ctx.isVndk() {
917 library.baseInstaller.subDir = "vndk"
918 }
Logan Chienf3511742017-10-31 18:04:35 +0800919
920 // Append a version to vndk or vndk-sp directories on the system partition.
921 if ctx.isVndk() && !ctx.isVndkExt() {
922 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
923 if vndkVersion != "current" && vndkVersion != "" {
924 library.baseInstaller.subDir += "-" + vndkVersion
925 }
Justin Yun8effde42017-06-23 19:24:43 +0900926 }
Jiyong Park429660f2019-01-16 22:31:11 +0900927 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900928 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
929 // The original path becomes a symlink to the corresponding file in the
930 // runtime APEX.
931 if isBionic(ctx.baseModuleName()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900932 if ctx.Device() {
933 library.installSymlinkToRuntimeApex(ctx, file)
934 }
Jiyong Park429660f2019-01-16 22:31:11 +0900935 library.baseInstaller.subDir = "bootstrap"
936 }
Justin Yun8effde42017-06-23 19:24:43 +0900937 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700938 library.baseInstaller.install(ctx, file)
939 }
Dan Albertf563d252017-10-13 00:29:00 -0700940
Dan Albert281f22b2017-12-13 15:03:47 -0800941 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900942 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900943 library.baseLinker.sanitize.isUnsanitizedVariant() &&
944 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700945 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700946 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700947
948 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
949 Rule: android.Cp,
950 Description: "install " + installPath.Base(),
951 Output: installPath,
952 Input: file,
953 })
954
Colin Cross0875c522017-11-28 17:34:01 -0800955 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700956 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700957}
958
Colin Crossb916a382016-07-29 17:28:03 -0700959func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800960 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700961}
962
Colin Crossa48ab5b2017-02-14 15:28:44 -0800963func (library *libraryDecorator) shared() bool {
964 return library.MutatedProperties.VariantIsShared
965}
966
967func (library *libraryDecorator) header() bool {
968 return !library.static() && !library.shared()
969}
970
971func (library *libraryDecorator) setStatic() {
972 library.MutatedProperties.VariantIsStatic = true
973 library.MutatedProperties.VariantIsShared = false
974}
975
976func (library *libraryDecorator) setShared() {
977 library.MutatedProperties.VariantIsStatic = false
978 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700979}
980
Colin Crossab3b7322016-12-09 14:46:15 -0800981func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800982 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800983}
984
985func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800986 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800987}
988
Colin Cross5950f382016-12-13 12:50:57 -0800989func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800990 library.MutatedProperties.BuildShared = false
991 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800992}
993
Jiyong Park7ed9de32018-10-15 22:25:07 +0900994func (library *libraryDecorator) buildStubs() bool {
995 return library.MutatedProperties.BuildStubs
996}
997
998func (library *libraryDecorator) stubsVersion() string {
999 return library.MutatedProperties.StubsVersion
1000}
1001
Colin Cross571cccf2019-02-04 11:22:08 -08001002var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1003
Jiyong Parkda732bd2018-11-02 18:23:15 +09001004func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001005 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001006 m := make(map[string]string)
1007 return &m
1008 }).(*map[string]string)
1009}
1010
1011// alphanumeric and _ characters are preserved.
1012// other characters are all converted to _
1013var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1014
1015func versioningMacroName(moduleName string) string {
1016 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1017 macroName = strings.ToUpper(moduleName)
1018 return "__" + macroName + "_API__"
1019}
1020
Colin Crossab3b7322016-12-09 14:46:15 -08001021func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001022 module := newModule(hod, android.MultilibBoth)
1023
Colin Crossb916a382016-07-29 17:28:03 -07001024 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001025 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001026 BuildShared: true,
1027 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001028 },
Colin Crossb916a382016-07-29 17:28:03 -07001029 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001030 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001031 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001032 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001033 }
1034
Colin Crossb916a382016-07-29 17:28:03 -07001035 module.compiler = library
1036 module.linker = library
1037 module.installer = library
1038
1039 return module, library
1040}
1041
Colin Cross10d22312017-05-03 11:01:58 -07001042// connects a shared library to a static library in order to reuse its .o files to avoid
1043// compiling source files twice.
1044func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1045 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1046 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001047
1048 // Check libraries in addition to cflags, since libraries may be exporting different
1049 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001050 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001051 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1052 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1053 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1054 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1055 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1056 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1057 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1058 staticCompiler.Properties.Static.System_shared_libs == nil &&
1059 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001060
1061 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1062 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1063 sharedCompiler.baseCompiler.Properties.Srcs
1064 sharedCompiler.baseCompiler.Properties.Srcs = nil
1065 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001066 } else {
1067 // This dep is just to reference static variant from shared variant
1068 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001069 }
1070 }
1071}
1072
Colin Crosse40b4ea2018-10-02 22:25:58 -07001073func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001074 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1075 if library, ok := m.linker.(libraryInterface); ok {
1076 var modules []blueprint.Module
1077 if library.buildStatic() && library.buildShared() {
1078 modules = mctx.CreateLocalVariations("static", "shared")
1079 static := modules[0].(*Module)
1080 shared := modules[1].(*Module)
1081
Colin Crossa48ab5b2017-02-14 15:28:44 -08001082 static.linker.(libraryInterface).setStatic()
1083 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001084
Colin Cross10d22312017-05-03 11:01:58 -07001085 reuseStaticLibrary(mctx, static, shared)
1086
Colin Crossb916a382016-07-29 17:28:03 -07001087 } else if library.buildStatic() {
1088 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001089 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001090 } else if library.buildShared() {
1091 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001092 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001093 }
1094 }
1095 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001096}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001097
Colin Cross571cccf2019-02-04 11:22:08 -08001098var stubVersionsKey = android.NewOnceKey("stubVersions")
1099
Jiyong Park25fc6a92018-11-18 18:02:45 +09001100// maps a module name to the list of stubs versions available for the module
1101func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001102 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001103 return make(map[string][]string)
1104 }).(map[string][]string)
1105}
1106
1107var stubsVersionsLock sync.Mutex
1108
1109func latestStubsVersionFor(config android.Config, name string) string {
1110 versions, ok := stubsVersionsFor(config)[name]
1111 if ok && len(versions) > 0 {
1112 // the versions are alreay sorted in ascending order
1113 return versions[len(versions)-1]
1114 }
1115 return ""
1116}
1117
Jiyong Park7ed9de32018-10-15 22:25:07 +09001118// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001119// (which is unnamed) and zero or more stubs variants.
1120func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001121 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001122 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1123 len(library.Properties.Stubs.Versions) > 0 {
1124 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001125 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001126 if _, err := strconv.Atoi(v); err != nil {
1127 mctx.PropertyErrorf("versions", "%q is not a number", v)
1128 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001129 versions = append(versions, v)
1130 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001131 sort.Slice(versions, func(i, j int) bool {
1132 left, _ := strconv.Atoi(versions[i])
1133 right, _ := strconv.Atoi(versions[j])
1134 return left < right
1135 })
1136
1137 // save the list of versions for later use
1138 copiedVersions := make([]string, len(versions))
1139 copy(copiedVersions, versions)
1140 stubsVersionsLock.Lock()
1141 defer stubsVersionsLock.Unlock()
1142 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1143
1144 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001145 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001146
Jiyong Park7ed9de32018-10-15 22:25:07 +09001147 modules := mctx.CreateVariations(versions...)
1148 for i, m := range modules {
1149 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001150 if versions[i] != "" {
1151 l.MutatedProperties.BuildStubs = true
1152 l.MutatedProperties.StubsVersion = versions[i]
1153 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001154 m.(*Module).sanitize = nil
1155 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001156 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001157 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001158 }
1159 } else {
1160 mctx.CreateVariations("")
1161 }
1162 return
1163 }
1164 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1165 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1166 mctx.CreateVariations("")
1167 return
1168 }
1169 }
1170}