blob: cab75acb597dfcc3c8a730f57efa9e473656ba84 [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
Patrice Arruda83c89e02019-03-25 15:32:39 -0700156// cc_library creates both static and/or shared libraries for a device and/or
157// host. By default, a cc_library has a single variant that targets the device.
158// Specifying `host_supported: true` also creates a library that targets the
159// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700160func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800161 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 return module.Init()
163}
164
Patrice Arruda83c89e02019-03-25 15:32:39 -0700165// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700166func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800167 module, library := NewLibrary(android.HostAndDeviceSupported)
168 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 return module.Init()
170}
171
Patrice Arruda83c89e02019-03-25 15:32:39 -0700172// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700173func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800174 module, library := NewLibrary(android.HostAndDeviceSupported)
175 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 return module.Init()
177}
178
Patrice Arruda83c89e02019-03-25 15:32:39 -0700179// cc_library_host_static creates a static library that is linkable to a host
180// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700181func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800182 module, library := NewLibrary(android.HostSupported)
183 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 return module.Init()
185}
186
Patrice Arruda83c89e02019-03-25 15:32:39 -0700187// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700188func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800189 module, library := NewLibrary(android.HostSupported)
190 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 return module.Init()
192}
193
Patrice Arruda83c89e02019-03-25 15:32:39 -0700194// cc_library_headers contains a set of c/c++ headers which are imported by
195// other soong cc modules using the header_libs property. For best practices,
196// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
197// Make.
Steven Morelandf9e62162017-11-02 17:00:50 -0700198func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800199 module, library := NewLibrary(android.HostAndDeviceSupported)
200 library.HeaderOnly()
201 return module.Init()
202}
203
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204type flagExporter struct {
205 Properties FlagExporterProperties
206
Dan Willemsen847dcc72016-09-29 12:13:36 -0700207 flags []string
208 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209}
210
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700211func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800212 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
213 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700214 } else {
215 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
216 }
217}
218
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700220 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 for _, dir := range includeDirs.Strings() {
222 f.flags = append(f.flags, inc+dir)
223 }
224}
225
226func (f *flagExporter) reexportFlags(flags []string) {
227 f.flags = append(f.flags, flags...)
228}
229
Dan Willemsen847dcc72016-09-29 12:13:36 -0700230func (f *flagExporter) reexportDeps(deps android.Paths) {
231 f.flagsDeps = append(f.flagsDeps, deps...)
232}
233
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234func (f *flagExporter) exportedFlags() []string {
235 return f.flags
236}
237
Dan Willemsen847dcc72016-09-29 12:13:36 -0700238func (f *flagExporter) exportedFlagsDeps() android.Paths {
239 return f.flagsDeps
240}
241
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242type exportedFlagsProducer interface {
243 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700244 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245}
246
247var _ exportedFlagsProducer = (*flagExporter)(nil)
248
Colin Crossb916a382016-07-29 17:28:03 -0700249// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
250// functionality: static vs. shared linkage, reusing object files for shared libraries
251type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800252 Properties LibraryProperties
253 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254
255 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700256 reuseObjects Objects
257 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700258 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700259
Colin Cross26c34ed2016-09-30 17:10:16 -0700260 // table-of-contents file to optimize out relinking when possible
261 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 flagExporter
264 stripper
265
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 // If we're used as a whole_static_lib, our missing dependencies need
267 // to be given
268 wholeStaticMissingDeps []string
269
270 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700271 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272
273 // Uses the module's name if empty, but can be overridden. Does not include
274 // shlib suffix.
275 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700276
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800277 sabi *sabi
278
Dan Willemsen581341d2017-02-09 16:16:31 -0800279 // Output archive of gcno coverage information files
280 coverageOutputFile android.OptionalPath
281
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800282 // linked Source Abi Dump
283 sAbiOutputFile android.OptionalPath
284
285 // Source Abi Diff
286 sAbiDiff android.OptionalPath
287
Colin Cross0875c522017-11-28 17:34:01 -0800288 // Location of the static library in the sysroot. Empty if the library is
289 // not included in the NDK.
290 ndkSysrootPath android.Path
291
Colin Crossb60190a2018-09-04 16:28:17 -0700292 // Location of the linked, unstripped library for shared libraries
293 unstrippedOutputFile android.Path
294
Dan Willemsen569edc52018-11-19 09:33:29 -0800295 // Location of the file that should be copied to dist dir when requested
296 distFile android.OptionalPath
297
Jiyong Park7ed9de32018-10-15 22:25:07 +0900298 versionScriptPath android.ModuleGenPath
299
Jiyong Parkf1194352019-02-25 11:05:47 +0900300 post_install_cmds []string
301
Vic Yangefd249e2018-11-12 20:19:56 -0800302 // If useCoreVariant is true, the vendor variant of a VNDK library is
303 // not installed.
304 useCoreVariant bool
305
Colin Crossb916a382016-07-29 17:28:03 -0700306 // Decorated interafaces
307 *baseCompiler
308 *baseLinker
309 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310}
311
Colin Crossb916a382016-07-29 17:28:03 -0700312func (library *libraryDecorator) linkerProps() []interface{} {
313 var props []interface{}
314 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 return append(props,
316 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800317 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700319 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320}
321
Colin Crossb916a382016-07-29 17:28:03 -0700322func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700323 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324
Colin Crossb916a382016-07-29 17:28:03 -0700325 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
326 // all code is position independent, and then those warnings get promoted to
327 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700328 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700329 flags.CFlags = append(flags.CFlags, "-fPIC")
330 }
331
332 if library.static() {
333 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800334 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700335 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
336 }
337
Colin Crossa48ab5b2017-02-14 15:28:44 -0800338 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700341 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 f = append(f,
343 "-nostdlib",
344 "-Wl,--gc-sections",
345 )
346 }
347
348 if ctx.Darwin() {
349 f = append(f,
350 "-dynamiclib",
351 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
353 )
Colin Cross7863cf52016-10-20 10:47:21 -0700354 if ctx.Arch().ArchType == android.X86 {
355 f = append(f,
356 "-read_only_relocs suppress",
357 )
358 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700359 } else {
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800360 f = append(f, "-shared")
361 if !ctx.Windows() {
362 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
363 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700364 }
365
366 flags.LdFlags = append(f, flags.LdFlags...)
367 }
368
369 return flags
370}
371
Colin Crossf18e1102017-11-16 14:33:08 -0800372func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700373 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700374 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700375 f := includeDirsToFlags(exportIncludeDirs)
376 flags.GlobalFlags = append(flags.GlobalFlags, f)
377 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700378 }
379
Jiyong Park7ed9de32018-10-15 22:25:07 +0900380 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
381 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900382 // Remove -include <file> when compiling stubs. Otherwise, the force included
383 // headers might cause conflicting types error with the symbols in the
384 // generated stubs source code. e.g.
385 // double acos(double); // in header
386 // void acos() {} // in the generated source code
387 removeInclude := func(flags []string) []string {
388 ret := flags[:0]
389 for _, f := range flags {
390 if strings.HasPrefix(f, "-include ") {
391 continue
392 }
393 ret = append(ret, f)
394 }
395 return ret
396 }
397 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
398 flags.CFlags = removeInclude(flags.CFlags)
399
Jiyong Park7ed9de32018-10-15 22:25:07 +0900400 flags = addStubLibraryCompilerFlags(flags)
401 }
402 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700403}
404
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700405func extractExportIncludesFromFlags(flags []string) []string {
406 // This method is used in the generation of rules which produce
407 // abi-dumps for source files. Exported headers are needed to infer the
408 // abi exported by a library and filter out the rest of the abi dumped
409 // from a source. We extract the include flags exported by a library.
410 // This includes the flags exported which are re-exported from static
411 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700412 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700413 // since for bionic libraries, abi-filtering is taken care of by version
414 // scripts.
415 var exportedIncludes []string
416 for _, flag := range flags {
417 if strings.HasPrefix(flag, "-I") {
418 exportedIncludes = append(exportedIncludes, flag)
419 }
420 }
421 return exportedIncludes
422}
423
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700424func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900425 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900426 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900427 library.versionScriptPath = versionScript
428 return objs
429 }
430
Colin Cross5950f382016-12-13 12:50:57 -0800431 if !library.buildShared() && !library.buildStatic() {
432 if len(library.baseCompiler.Properties.Srcs) > 0 {
433 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
434 }
435 if len(library.Properties.Static.Srcs) > 0 {
436 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
437 }
438 if len(library.Properties.Shared.Srcs) > 0 {
439 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
440 }
441 return Objects{}
442 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800443 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700444 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800445 var SourceAbiFlags []string
446 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700447 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800448 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700449 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
450 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
451 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800452 flags.SAbiFlags = SourceAbiFlags
453 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
454 len(library.Properties.Static.Srcs)
455 if total_length > 0 {
456 flags.SAbiDump = true
457 }
458 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700459 objs := library.baseCompiler.compile(ctx, flags, deps)
460 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700461 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700462
Colin Cross4d9c2d12016-07-29 12:48:20 -0700463 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800464 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700465 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800466 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800467 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800468 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700469 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800470 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700471 }
472
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700473 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700474}
475
476type libraryInterface interface {
477 getWholeStaticMissingDeps() []string
478 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700479 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700480 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700481 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700482
483 // Returns true if the build options for the module have selected a static or shared build
484 buildStatic() bool
485 buildShared() bool
486
487 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800488 setStatic()
489 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700490}
491
492func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
493 name := library.libName
494 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100495 name = String(library.Properties.Stem)
496 if name == "" {
497 name = ctx.baseModuleName()
498 }
Colin Crossb916a382016-07-29 17:28:03 -0700499 }
500
Logan Chienf3511742017-10-31 18:04:35 +0800501 if ctx.isVndkExt() {
502 name = ctx.getVndkExtendsModuleName()
503 }
504
Colin Crossb916a382016-07-29 17:28:03 -0700505 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
506 if !strings.HasSuffix(name, "-host") {
507 name = name + "-host"
508 }
509 }
510
Colin Crossa48ab5b2017-02-14 15:28:44 -0800511 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700512}
513
Jiyong Parkda732bd2018-11-02 18:23:15 +0900514var versioningMacroNamesListMutex sync.Mutex
515
Colin Crossb916a382016-07-29 17:28:03 -0700516func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
517 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700518 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700519 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700520 }
521 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700522 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900523 // Let baseLinker know whether this variant is for stubs or not, so that
524 // it can omit things that are not required for linking stubs.
525 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900526
527 if library.buildStubs() {
528 macroNames := versioningMacroNamesList(ctx.Config())
529 myName := versioningMacroName(ctx.ModuleName())
530 versioningMacroNamesListMutex.Lock()
531 defer versioningMacroNamesListMutex.Unlock()
532 if (*macroNames)[myName] == "" {
533 (*macroNames)[myName] = ctx.ModuleName()
534 } else if (*macroNames)[myName] != ctx.ModuleName() {
535 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
536 }
537 }
Colin Crossb916a382016-07-29 17:28:03 -0700538}
539
dimitry0345ad82018-12-05 16:28:14 +0100540func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
541 deps = library.baseCompiler.compilerDeps(ctx, deps)
542
dimitry0345ad82018-12-05 16:28:14 +0100543 return deps
544}
545
Colin Cross37047f12016-12-13 17:06:13 -0800546func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800547 if library.static() {
548 if library.Properties.Static.System_shared_libs != nil {
549 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
550 }
551 } else if library.shared() {
552 if library.Properties.Shared.System_shared_libs != nil {
553 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
554 }
555 }
556
Colin Crossb916a382016-07-29 17:28:03 -0700557 deps = library.baseLinker.linkerDeps(ctx, deps)
558
559 if library.static() {
560 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
561 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700562 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
563 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800564
565 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
566 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800567 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800568 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700569 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 deps.CrtBegin = "crtbegin_so"
571 deps.CrtEnd = "crtend_so"
572 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800573 // TODO(danalbert): Add generation of crt objects.
574 // For `sdk_version: "current"`, we don't actually have a
575 // freshly generated set of CRT objects. Use the last stable
576 // version.
577 version := ctx.sdkVersion()
578 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700579 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800580 }
581 deps.CrtBegin = "ndk_crtbegin_so." + version
582 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700583 }
584 }
585 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
586 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
587 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800588
589 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
590 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900592 if ctx.useVndk() {
593 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
594 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
595 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000596 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
597 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900598 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900599 if ctx.inRecovery() {
600 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
601 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
602 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000603 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
604 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900605 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800606
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 return deps
608}
609
Colin Crossb916a382016-07-29 17:28:03 -0700610func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700611 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700612
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700613 library.objects = deps.WholeStaticLibObjs.Copy()
614 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615
Colin Cross86803cf2018-02-15 14:12:26 -0800616 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
617 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800618 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700619
Dan Willemsen569edc52018-11-19 09:33:29 -0800620 if Bool(library.baseLinker.Properties.Use_version_lib) {
621 if ctx.Host() {
622 versionedOutputFile := outputFile
623 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
624 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
625 } else {
626 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
627 library.distFile = android.OptionalPathForPath(versionedOutputFile)
628 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
629 }
Colin Cross86803cf2018-02-15 14:12:26 -0800630 }
631
Dan Willemsen581341d2017-02-09 16:16:31 -0800632 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
633
634 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800635 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636
637 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
638
639 ctx.CheckbuildFile(outputFile)
640
641 return outputFile
642}
643
Colin Crossb916a382016-07-29 17:28:03 -0700644func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700645 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700646
647 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700648 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649
Colin Cross2383f3b2018-02-06 14:40:13 -0800650 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
651 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
652 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700653 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654 if unexportedSymbols.Valid() {
655 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
656 }
657 if forceNotWeakSymbols.Valid() {
658 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
659 }
660 if forceWeakSymbols.Valid() {
661 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
662 }
663 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664 if unexportedSymbols.Valid() {
665 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
666 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
667 }
668 if forceNotWeakSymbols.Valid() {
669 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
670 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
671 }
672 if forceWeakSymbols.Valid() {
673 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
674 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
675 }
676 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900677 if library.buildStubs() {
678 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
679 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
680 linkerDeps = append(linkerDeps, library.versionScriptPath)
681 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700682
683 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
684 outputFile := android.PathForModuleOut(ctx, fileName)
685 ret := outputFile
686
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800687 var implicitOutputs android.WritablePaths
688 if ctx.Windows() {
689 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "a"))
690
691 flags.LdFlags = append(flags.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
692 implicitOutputs = append(implicitOutputs, importLibraryPath)
693 }
694
Colin Cross4d9c2d12016-07-29 12:48:20 -0700695 builderFlags := flagsToBuilderFlags(flags)
696
Colin Crossb496cfd2018-09-10 16:50:05 -0700697 // Optimize out relinking against shared libraries whose interface hasn't changed by
698 // depending on a table of contents file instead of the library itself.
699 tocPath := outputFile.RelPathString()
700 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
701 tocFile := android.PathForOutput(ctx, tocPath)
702 library.tocFile = android.OptionalPathForPath(tocFile)
703 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700704
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800706 if ctx.Darwin() {
707 builderFlags.stripUseGnuStrip = true
708 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700709 strippedOutputFile := outputFile
710 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
711 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
712 }
713
Colin Crossb60190a2018-09-04 16:28:17 -0700714 library.unstrippedOutputFile = outputFile
715
Dan Willemsen569edc52018-11-19 09:33:29 -0800716 if Bool(library.baseLinker.Properties.Use_version_lib) {
717 if ctx.Host() {
718 versionedOutputFile := outputFile
719 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
720 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
721 } else {
722 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
723 library.distFile = android.OptionalPathForPath(versionedOutputFile)
724
725 if library.stripper.needsStrip(ctx) {
726 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
727 library.distFile = android.OptionalPathForPath(out)
728 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
729 }
730
731 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
732 }
Colin Cross86803cf2018-02-15 14:12:26 -0800733 }
734
Jiyong Park64a44f22019-01-18 14:37:08 +0900735 sharedLibs := deps.EarlySharedLibs
736 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700737 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
738
Jiyong Park64a44f22019-01-18 14:37:08 +0900739 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700740 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
741 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700742 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700743
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700744 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700745 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Pirama Arumuga Nainar3c21c0b2019-02-28 15:52:05 -0800746 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700747
Dan Willemsen581341d2017-02-09 16:16:31 -0800748 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
749 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800750
751 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
752 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
753
Dan Willemsen581341d2017-02-09 16:16:31 -0800754 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700755 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800756
Colin Cross4d9c2d12016-07-29 12:48:20 -0700757 return ret
758}
759
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900760func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
761 return library.unstrippedOutputFile
762}
763
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700764func (library *libraryDecorator) nativeCoverage() bool {
765 if library.header() || library.buildStubs() {
766 return false
767 }
768 return true
769}
770
Logan Chien7eefdc42018-07-11 18:10:41 +0800771func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800772 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800773
774 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
775 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
776
777 if refAbiDumpTextFile.Valid() {
778 if refAbiDumpGzipFile.Valid() {
779 ctx.ModuleErrorf(
780 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
781 refAbiDumpTextFile, refAbiDumpGzipFile)
782 return nil
783 }
784 return refAbiDumpTextFile.Path()
785 }
786 if refAbiDumpGzipFile.Valid() {
787 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
788 }
789 return nil
790}
791
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700792func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800793 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800794 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
795 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800796 vndkVersion = ver
797 }
798
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700799 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800800 var SourceAbiFlags []string
801 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700802 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
803 }
804 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
805 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800806 }
807 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800808 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
809 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
810 library.Properties.Header_abi_checker.Exclude_symbol_versions,
811 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800812
Logan Chien7eefdc42018-07-11 18:10:41 +0800813 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
814 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800815 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien6227fed2019-02-18 13:12:21 +0800816 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800817 }
818 }
819}
820
Colin Crossb916a382016-07-29 17:28:03 -0700821func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700822 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700823
Colin Crossad59e752017-11-16 14:29:11 -0800824 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700825 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800826 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700827 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700828 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700829 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700830 }
831
832 library.exportIncludes(ctx, "-I")
833 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700834 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700835
Nan Zhang0007d812017-11-07 10:57:05 -0800836 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700837 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700838 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700839 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700840 }
841 library.reexportFlags(flags)
842 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800843 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
844 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700845 }
846 }
847
Nan Zhang0007d812017-11-07 10:57:05 -0800848 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700849 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800850 includes := []string{}
851 if flags.ProtoRoot {
852 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700853 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800854 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
855 library.reexportFlags(includes)
856 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800857 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
858 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700859 }
860 }
861
Inseob Kim21f26902018-09-06 00:55:20 +0900862 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kimc0907f12019-02-08 21:00:45 +0900863 internalFlags := []string{
Inseob Kim21f26902018-09-06 00:55:20 +0900864 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
865 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900866 systemFlags := []string{
867 "-I" + android.PathForModuleGen(ctx, "sysprop/system", "include").String(),
868 }
869
870 flags := internalFlags
871
872 if library.Properties.Sysprop.Platform != nil {
873 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
874 isVendor := ctx.useVndk()
875 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
876
877 useSystem := isProduct || (isOwnerPlatform == isVendor)
878
879 if useSystem {
880 flags = systemFlags
881 }
882 }
883
Inseob Kim21f26902018-09-06 00:55:20 +0900884 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900885 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900886 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
887 }
888
Jiyong Parkda732bd2018-11-02 18:23:15 +0900889 if library.buildStubs() {
890 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
891 }
892
Colin Cross4d9c2d12016-07-29 12:48:20 -0700893 return out
894}
895
Colin Crossb916a382016-07-29 17:28:03 -0700896func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700897 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700898}
899
Colin Crossb916a382016-07-29 17:28:03 -0700900func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700901 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700902}
903
Colin Crossb916a382016-07-29 17:28:03 -0700904func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700905 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700906}
907
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700908func (library *libraryDecorator) objs() Objects {
909 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700910}
911
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700912func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
913 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700914}
915
Colin Cross26c34ed2016-09-30 17:10:16 -0700916func (library *libraryDecorator) toc() android.OptionalPath {
917 return library.tocFile
918}
919
Jiyong Parkf1194352019-02-25 11:05:47 +0900920func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
921 dir := library.baseInstaller.installDir(ctx)
922 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
923 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
924 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
925 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
926}
927
Colin Crossb916a382016-07-29 17:28:03 -0700928func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700929 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900930 if ctx.Device() && ctx.useVndk() {
931 if ctx.isVndkSp() {
932 library.baseInstaller.subDir = "vndk-sp"
933 } else if ctx.isVndk() {
Vic Yangefd249e2018-11-12 20:19:56 -0800934 if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
935 library.useCoreVariant = true
936 }
Justin Yun8fe12122017-12-07 17:18:15 +0900937 library.baseInstaller.subDir = "vndk"
938 }
Logan Chienf3511742017-10-31 18:04:35 +0800939
940 // Append a version to vndk or vndk-sp directories on the system partition.
941 if ctx.isVndk() && !ctx.isVndkExt() {
942 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
943 if vndkVersion != "current" && vndkVersion != "" {
944 library.baseInstaller.subDir += "-" + vndkVersion
945 }
Justin Yun8effde42017-06-23 19:24:43 +0900946 }
Jiyong Park429660f2019-01-16 22:31:11 +0900947 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900948 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
949 // The original path becomes a symlink to the corresponding file in the
950 // runtime APEX.
951 if isBionic(ctx.baseModuleName()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900952 if ctx.Device() {
953 library.installSymlinkToRuntimeApex(ctx, file)
954 }
Jiyong Park429660f2019-01-16 22:31:11 +0900955 library.baseInstaller.subDir = "bootstrap"
956 }
Justin Yun8effde42017-06-23 19:24:43 +0900957 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700958 library.baseInstaller.install(ctx, file)
959 }
Dan Albertf563d252017-10-13 00:29:00 -0700960
Dan Albert281f22b2017-12-13 15:03:47 -0800961 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900962 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900963 library.baseLinker.sanitize.isUnsanitizedVariant() &&
964 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700965 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700966 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700967
968 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
969 Rule: android.Cp,
970 Description: "install " + installPath.Base(),
971 Output: installPath,
972 Input: file,
973 })
974
Colin Cross0875c522017-11-28 17:34:01 -0800975 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700976 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700977}
978
Colin Crossb916a382016-07-29 17:28:03 -0700979func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800980 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700981}
982
Colin Crossa48ab5b2017-02-14 15:28:44 -0800983func (library *libraryDecorator) shared() bool {
984 return library.MutatedProperties.VariantIsShared
985}
986
987func (library *libraryDecorator) header() bool {
988 return !library.static() && !library.shared()
989}
990
991func (library *libraryDecorator) setStatic() {
992 library.MutatedProperties.VariantIsStatic = true
993 library.MutatedProperties.VariantIsShared = false
994}
995
996func (library *libraryDecorator) setShared() {
997 library.MutatedProperties.VariantIsStatic = false
998 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700999}
1000
Colin Crossab3b7322016-12-09 14:46:15 -08001001func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001002 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001003}
1004
1005func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001006 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001007}
1008
Colin Cross5950f382016-12-13 12:50:57 -08001009func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001010 library.MutatedProperties.BuildShared = false
1011 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001012}
1013
Jiyong Park7ed9de32018-10-15 22:25:07 +09001014func (library *libraryDecorator) buildStubs() bool {
1015 return library.MutatedProperties.BuildStubs
1016}
1017
1018func (library *libraryDecorator) stubsVersion() string {
1019 return library.MutatedProperties.StubsVersion
1020}
1021
Colin Cross571cccf2019-02-04 11:22:08 -08001022var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1023
Jiyong Parkda732bd2018-11-02 18:23:15 +09001024func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001025 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001026 m := make(map[string]string)
1027 return &m
1028 }).(*map[string]string)
1029}
1030
1031// alphanumeric and _ characters are preserved.
1032// other characters are all converted to _
1033var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1034
1035func versioningMacroName(moduleName string) string {
1036 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1037 macroName = strings.ToUpper(moduleName)
1038 return "__" + macroName + "_API__"
1039}
1040
Colin Crossab3b7322016-12-09 14:46:15 -08001041func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001042 module := newModule(hod, android.MultilibBoth)
1043
Colin Crossb916a382016-07-29 17:28:03 -07001044 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001045 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001046 BuildShared: true,
1047 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001048 },
Colin Crossb916a382016-07-29 17:28:03 -07001049 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001050 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001051 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001052 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001053 }
1054
Colin Crossb916a382016-07-29 17:28:03 -07001055 module.compiler = library
1056 module.linker = library
1057 module.installer = library
1058
1059 return module, library
1060}
1061
Colin Cross10d22312017-05-03 11:01:58 -07001062// connects a shared library to a static library in order to reuse its .o files to avoid
1063// compiling source files twice.
1064func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1065 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1066 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001067
1068 // Check libraries in addition to cflags, since libraries may be exporting different
1069 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001070 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001071 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1072 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1073 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1074 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1075 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1076 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1077 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1078 staticCompiler.Properties.Static.System_shared_libs == nil &&
1079 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001080
1081 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1082 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1083 sharedCompiler.baseCompiler.Properties.Srcs
1084 sharedCompiler.baseCompiler.Properties.Srcs = nil
1085 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001086 } else {
1087 // This dep is just to reference static variant from shared variant
1088 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001089 }
1090 }
1091}
1092
Colin Crosse40b4ea2018-10-02 22:25:58 -07001093func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001094 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1095 if library, ok := m.linker.(libraryInterface); ok {
1096 var modules []blueprint.Module
1097 if library.buildStatic() && library.buildShared() {
1098 modules = mctx.CreateLocalVariations("static", "shared")
1099 static := modules[0].(*Module)
1100 shared := modules[1].(*Module)
1101
Colin Crossa48ab5b2017-02-14 15:28:44 -08001102 static.linker.(libraryInterface).setStatic()
1103 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001104
Colin Cross10d22312017-05-03 11:01:58 -07001105 reuseStaticLibrary(mctx, static, shared)
1106
Colin Crossb916a382016-07-29 17:28:03 -07001107 } else if library.buildStatic() {
1108 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001109 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001110 } else if library.buildShared() {
1111 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001112 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001113 }
1114 }
1115 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001116}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001117
Colin Cross571cccf2019-02-04 11:22:08 -08001118var stubVersionsKey = android.NewOnceKey("stubVersions")
1119
Jiyong Park25fc6a92018-11-18 18:02:45 +09001120// maps a module name to the list of stubs versions available for the module
1121func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001122 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001123 return make(map[string][]string)
1124 }).(map[string][]string)
1125}
1126
1127var stubsVersionsLock sync.Mutex
1128
1129func latestStubsVersionFor(config android.Config, name string) string {
1130 versions, ok := stubsVersionsFor(config)[name]
1131 if ok && len(versions) > 0 {
1132 // the versions are alreay sorted in ascending order
1133 return versions[len(versions)-1]
1134 }
1135 return ""
1136}
1137
Jiyong Park7ed9de32018-10-15 22:25:07 +09001138// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001139// (which is unnamed) and zero or more stubs variants.
1140func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001141 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001142 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1143 len(library.Properties.Stubs.Versions) > 0 {
1144 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001145 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001146 if _, err := strconv.Atoi(v); err != nil {
1147 mctx.PropertyErrorf("versions", "%q is not a number", v)
1148 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001149 versions = append(versions, v)
1150 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001151 sort.Slice(versions, func(i, j int) bool {
1152 left, _ := strconv.Atoi(versions[i])
1153 right, _ := strconv.Atoi(versions[j])
1154 return left < right
1155 })
1156
1157 // save the list of versions for later use
1158 copiedVersions := make([]string, len(versions))
1159 copy(copiedVersions, versions)
1160 stubsVersionsLock.Lock()
1161 defer stubsVersionsLock.Unlock()
1162 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1163
1164 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001165 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001166
Jiyong Park7ed9de32018-10-15 22:25:07 +09001167 modules := mctx.CreateVariations(versions...)
1168 for i, m := range modules {
1169 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001170 if versions[i] != "" {
1171 l.MutatedProperties.BuildStubs = true
1172 l.MutatedProperties.StubsVersion = versions[i]
1173 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001174 m.(*Module).sanitize = nil
1175 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001176 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001177 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001178 }
1179 } else {
1180 mctx.CreateVariations("")
1181 }
1182 return
1183 }
1184 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1185 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1186 mctx.CreateVariations("")
1187 return
1188 }
1189 }
1190}