blob: 1f79becb01c374510983ae2c1583065cc86a3d1d [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 (
Logan Chien41eabe62019-04-10 13:33:58 +080018 "io"
Jiyong Parkf1194352019-02-25 11:05:47 +090019 "path/filepath"
Jiyong Parkda732bd2018-11-02 18:23:15 +090020 "regexp"
Jiyong Park25fc6a92018-11-18 18:02:45 +090021 "sort"
22 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090024 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025
26 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070027 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070030 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090031 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070032)
33
Colin Crosseefe9a32019-01-22 14:41:08 -080034type StaticSharedLibraryProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080035 Srcs []string `android:"path,arch_variant"`
36 Cflags []string `android:"path,arch_variant"`
Colin Crosseefe9a32019-01-22 14:41:08 -080037
38 Enabled *bool `android:"arch_variant"`
39 Whole_static_libs []string `android:"arch_variant"`
40 Static_libs []string `android:"arch_variant"`
41 Shared_libs []string `android:"arch_variant"`
42 System_shared_libs []string `android:"arch_variant"`
43
44 Export_shared_lib_headers []string `android:"arch_variant"`
45 Export_static_lib_headers []string `android:"arch_variant"`
46}
47
Colin Crossb916a382016-07-29 17:28:03 -070048type LibraryProperties struct {
Colin Crosseefe9a32019-01-22 14:41:08 -080049 Static StaticSharedLibraryProperties `android:"arch_variant"`
50 Shared StaticSharedLibraryProperties `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070051
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 // local file name to pass to the linker as -unexported_symbols_list
Colin Cross27b922f2019-03-04 22:35:41 -080053 Unexported_symbols_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070054 // local file name to pass to the linker as -force_symbols_not_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080055 Force_symbols_not_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070056 // local file name to pass to the linker as -force_symbols_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080057 Force_symbols_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070058
59 // rename host libraries to prevent overlap with system installed libraries
60 Unique_host_soname *bool
61
Dan Willemsene1240db2016-11-03 14:28:51 -070062 Aidl struct {
63 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080064 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070065 }
66
Colin Cross0c461f12016-10-20 16:11:43 -070067 Proto struct {
68 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080069 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070070 }
Dan Albertf563d252017-10-13 00:29:00 -070071
Inseob Kimc0907f12019-02-08 21:00:45 +090072 Sysprop struct {
73 // Whether platform owns this sysprop library.
74 Platform *bool
Inseob Kimb3f22ca2019-03-05 12:40:24 +090075 } `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +090076
Nan Zhang0007d812017-11-07 10:57:05 -080077 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090078
79 Stubs struct {
80 // Relative path to the symbol map. The symbol map provides the list of
81 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090083
84 // List versions to generate stubs libs for.
85 Versions []string
86 }
dimitryd95964a2018-11-07 13:43:34 +010087
88 // set the name of the output
89 Stem *string `android:"arch_variant"`
90
91 // Names of modules to be overridden. Listed modules can only be other shared libraries
92 // (in Make or Soong).
93 // This does not completely prevent installation of the overridden libraries, but if both
94 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
95 // from PRODUCT_PACKAGES.
96 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080097
98 // Properties for ABI compatibility checker
99 Header_abi_checker struct {
Logan Chien41eabe62019-04-10 13:33:58 +0800100 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
101 Enabled *bool
102
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800103 // Path to a symbol file that specifies the symbols to be included in the generated
104 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800106
107 // Symbol versions that should be ignored from the symbol file
108 Exclude_symbol_versions []string
109
110 // Symbol tags that should be ignored from the symbol file
111 Exclude_symbol_tags []string
112 }
Colin Crossa48ab5b2017-02-14 15:28:44 -0800113}
Colin Cross0c461f12016-10-20 16:11:43 -0700114
Colin Crossa48ab5b2017-02-14 15:28:44 -0800115type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700117
118 // Build a static variant
119 BuildStatic bool `blueprint:"mutated"`
120 // Build a shared variant
121 BuildShared bool `blueprint:"mutated"`
122 // This variant is shared
123 VariantIsShared bool `blueprint:"mutated"`
124 // This variant is static
125 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900126
127 // This variant is a stubs lib
128 BuildStubs bool `blueprint:"mutated"`
129 // Version of the stubs lib
130 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700131}
132
133type FlagExporterProperties struct {
134 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700135 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700136 // against this module. Directories listed in export_include_dirs do not need to be
137 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700138 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700139
140 Target struct {
141 Vendor struct {
142 // list of exported include directories, like
143 // export_include_dirs, that will be applied to the
144 // vendor variant of this library. This will overwrite
145 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800146 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700147 }
148 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149}
150
151func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700152 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
153 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
154 android.RegisterModuleType("cc_library", LibraryFactory)
155 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
156 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
157 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158}
159
Patrice Arruda83c89e02019-03-25 15:32:39 -0700160// cc_library creates both static and/or shared libraries for a device and/or
161// host. By default, a cc_library has a single variant that targets the device.
162// Specifying `host_supported: true` also creates a library that targets the
163// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700164func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800165 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 return module.Init()
167}
168
Patrice Arruda83c89e02019-03-25 15:32:39 -0700169// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700170func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800171 module, library := NewLibrary(android.HostAndDeviceSupported)
172 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 return module.Init()
174}
175
Patrice Arruda83c89e02019-03-25 15:32:39 -0700176// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700177func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800178 module, library := NewLibrary(android.HostAndDeviceSupported)
179 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 return module.Init()
181}
182
Patrice Arruda83c89e02019-03-25 15:32:39 -0700183// cc_library_host_static creates a static library that is linkable to a host
184// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700185func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800186 module, library := NewLibrary(android.HostSupported)
187 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 return module.Init()
189}
190
Patrice Arruda83c89e02019-03-25 15:32:39 -0700191// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700192func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800193 module, library := NewLibrary(android.HostSupported)
194 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 return module.Init()
196}
197
Patrice Arruda83c89e02019-03-25 15:32:39 -0700198// cc_library_headers contains a set of c/c++ headers which are imported by
199// other soong cc modules using the header_libs property. For best practices,
200// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
201// Make.
Steven Morelandf9e62162017-11-02 17:00:50 -0700202func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800203 module, library := NewLibrary(android.HostAndDeviceSupported)
204 library.HeaderOnly()
205 return module.Init()
206}
207
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208type flagExporter struct {
209 Properties FlagExporterProperties
210
Dan Willemsen847dcc72016-09-29 12:13:36 -0700211 flags []string
212 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213}
214
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700215func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800216 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
217 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700218 } else {
219 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
220 }
221}
222
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700224 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 for _, dir := range includeDirs.Strings() {
226 f.flags = append(f.flags, inc+dir)
227 }
228}
229
230func (f *flagExporter) reexportFlags(flags []string) {
231 f.flags = append(f.flags, flags...)
232}
233
Dan Willemsen847dcc72016-09-29 12:13:36 -0700234func (f *flagExporter) reexportDeps(deps android.Paths) {
235 f.flagsDeps = append(f.flagsDeps, deps...)
236}
237
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238func (f *flagExporter) exportedFlags() []string {
239 return f.flags
240}
241
Dan Willemsen847dcc72016-09-29 12:13:36 -0700242func (f *flagExporter) exportedFlagsDeps() android.Paths {
243 return f.flagsDeps
244}
245
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246type exportedFlagsProducer interface {
247 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700248 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249}
250
251var _ exportedFlagsProducer = (*flagExporter)(nil)
252
Colin Crossb916a382016-07-29 17:28:03 -0700253// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
254// functionality: static vs. shared linkage, reusing object files for shared libraries
255type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800256 Properties LibraryProperties
257 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258
259 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700260 reuseObjects Objects
261 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700262 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700263
Colin Cross26c34ed2016-09-30 17:10:16 -0700264 // table-of-contents file to optimize out relinking when possible
265 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 flagExporter
268 stripper
269
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 // If we're used as a whole_static_lib, our missing dependencies need
271 // to be given
272 wholeStaticMissingDeps []string
273
274 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700275 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276
277 // Uses the module's name if empty, but can be overridden. Does not include
278 // shlib suffix.
279 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700280
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800281 sabi *sabi
282
Dan Willemsen581341d2017-02-09 16:16:31 -0800283 // Output archive of gcno coverage information files
284 coverageOutputFile android.OptionalPath
285
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800286 // linked Source Abi Dump
287 sAbiOutputFile android.OptionalPath
288
289 // Source Abi Diff
290 sAbiDiff android.OptionalPath
291
Colin Cross0875c522017-11-28 17:34:01 -0800292 // Location of the static library in the sysroot. Empty if the library is
293 // not included in the NDK.
294 ndkSysrootPath android.Path
295
Colin Crossb60190a2018-09-04 16:28:17 -0700296 // Location of the linked, unstripped library for shared libraries
297 unstrippedOutputFile android.Path
298
Dan Willemsen569edc52018-11-19 09:33:29 -0800299 // Location of the file that should be copied to dist dir when requested
300 distFile android.OptionalPath
301
Jiyong Park7ed9de32018-10-15 22:25:07 +0900302 versionScriptPath android.ModuleGenPath
303
Jiyong Parkf1194352019-02-25 11:05:47 +0900304 post_install_cmds []string
305
Vic Yangefd249e2018-11-12 20:19:56 -0800306 // If useCoreVariant is true, the vendor variant of a VNDK library is
307 // not installed.
308 useCoreVariant bool
309
Colin Crossb916a382016-07-29 17:28:03 -0700310 // Decorated interafaces
311 *baseCompiler
312 *baseLinker
313 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314}
315
Colin Crossb916a382016-07-29 17:28:03 -0700316func (library *libraryDecorator) linkerProps() []interface{} {
317 var props []interface{}
318 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319 return append(props,
320 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800321 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700323 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324}
325
Colin Crossb916a382016-07-29 17:28:03 -0700326func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700327 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328
Colin Crossb916a382016-07-29 17:28:03 -0700329 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
330 // all code is position independent, and then those warnings get promoted to
331 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700332 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700333 flags.CFlags = append(flags.CFlags, "-fPIC")
334 }
335
336 if library.static() {
337 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800338 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700339 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
340 }
341
Colin Crossa48ab5b2017-02-14 15:28:44 -0800342 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700344 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700345 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 f = append(f,
347 "-nostdlib",
348 "-Wl,--gc-sections",
349 )
350 }
351
352 if ctx.Darwin() {
353 f = append(f,
354 "-dynamiclib",
355 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700356 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
357 )
Colin Cross7863cf52016-10-20 10:47:21 -0700358 if ctx.Arch().ArchType == android.X86 {
359 f = append(f,
360 "-read_only_relocs suppress",
361 )
362 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 } else {
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700364 f = append(f, "-shared")
365 if !ctx.Windows() {
366 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
367 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368 }
369
370 flags.LdFlags = append(f, flags.LdFlags...)
371 }
372
373 return flags
374}
375
Colin Crossf18e1102017-11-16 14:33:08 -0800376func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700377 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700378 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700379 f := includeDirsToFlags(exportIncludeDirs)
380 flags.GlobalFlags = append(flags.GlobalFlags, f)
381 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700382 }
383
Jiyong Park7ed9de32018-10-15 22:25:07 +0900384 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
385 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900386 // Remove -include <file> when compiling stubs. Otherwise, the force included
387 // headers might cause conflicting types error with the symbols in the
388 // generated stubs source code. e.g.
389 // double acos(double); // in header
390 // void acos() {} // in the generated source code
391 removeInclude := func(flags []string) []string {
392 ret := flags[:0]
393 for _, f := range flags {
394 if strings.HasPrefix(f, "-include ") {
395 continue
396 }
397 ret = append(ret, f)
398 }
399 return ret
400 }
401 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
402 flags.CFlags = removeInclude(flags.CFlags)
403
Jiyong Park7ed9de32018-10-15 22:25:07 +0900404 flags = addStubLibraryCompilerFlags(flags)
405 }
406 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700407}
408
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700409func extractExportIncludesFromFlags(flags []string) []string {
410 // This method is used in the generation of rules which produce
411 // abi-dumps for source files. Exported headers are needed to infer the
412 // abi exported by a library and filter out the rest of the abi dumped
413 // from a source. We extract the include flags exported by a library.
414 // This includes the flags exported which are re-exported from static
415 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700416 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700417 // since for bionic libraries, abi-filtering is taken care of by version
418 // scripts.
419 var exportedIncludes []string
420 for _, flag := range flags {
421 if strings.HasPrefix(flag, "-I") {
422 exportedIncludes = append(exportedIncludes, flag)
423 }
424 }
425 return exportedIncludes
426}
427
Logan Chien41eabe62019-04-10 13:33:58 +0800428func (library *libraryDecorator) shouldCreateVndkSourceAbiDump(ctx ModuleContext) bool {
429 if library.Properties.Header_abi_checker.Enabled != nil {
430 return Bool(library.Properties.Header_abi_checker.Enabled)
431 }
432 return ctx.shouldCreateVndkSourceAbiDump()
433}
434
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700435func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900436 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900437 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900438 library.versionScriptPath = versionScript
439 return objs
440 }
441
Colin Cross5950f382016-12-13 12:50:57 -0800442 if !library.buildShared() && !library.buildStatic() {
443 if len(library.baseCompiler.Properties.Srcs) > 0 {
444 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
445 }
446 if len(library.Properties.Static.Srcs) > 0 {
447 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
448 }
449 if len(library.Properties.Shared.Srcs) > 0 {
450 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
451 }
452 return Objects{}
453 }
Logan Chien41eabe62019-04-10 13:33:58 +0800454 if library.shouldCreateVndkSourceAbiDump(ctx) || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700455 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800456 var SourceAbiFlags []string
457 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700458 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800459 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700460 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
461 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
462 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800463 flags.SAbiFlags = SourceAbiFlags
464 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
465 len(library.Properties.Static.Srcs)
466 if total_length > 0 {
467 flags.SAbiDump = true
468 }
469 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700470 objs := library.baseCompiler.compile(ctx, flags, deps)
471 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700472 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700473
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800475 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700476 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800477 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800478 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800479 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700480 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800481 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700482 }
483
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700484 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700485}
486
487type libraryInterface interface {
488 getWholeStaticMissingDeps() []string
489 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700490 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700491 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700492 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700493
494 // Returns true if the build options for the module have selected a static or shared build
495 buildStatic() bool
496 buildShared() bool
497
498 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800499 setStatic()
500 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800501
502 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
503 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Colin Crossb916a382016-07-29 17:28:03 -0700504}
505
506func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
507 name := library.libName
508 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100509 name = String(library.Properties.Stem)
510 if name == "" {
511 name = ctx.baseModuleName()
512 }
Colin Crossb916a382016-07-29 17:28:03 -0700513 }
514
Logan Chienf3511742017-10-31 18:04:35 +0800515 if ctx.isVndkExt() {
516 name = ctx.getVndkExtendsModuleName()
517 }
518
Colin Crossb916a382016-07-29 17:28:03 -0700519 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
520 if !strings.HasSuffix(name, "-host") {
521 name = name + "-host"
522 }
523 }
524
Colin Crossa48ab5b2017-02-14 15:28:44 -0800525 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700526}
527
Jiyong Parkda732bd2018-11-02 18:23:15 +0900528var versioningMacroNamesListMutex sync.Mutex
529
Colin Crossb916a382016-07-29 17:28:03 -0700530func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
531 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700532 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700533 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700534 }
535 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700536 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900537 // Let baseLinker know whether this variant is for stubs or not, so that
538 // it can omit things that are not required for linking stubs.
539 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900540
541 if library.buildStubs() {
542 macroNames := versioningMacroNamesList(ctx.Config())
543 myName := versioningMacroName(ctx.ModuleName())
544 versioningMacroNamesListMutex.Lock()
545 defer versioningMacroNamesListMutex.Unlock()
546 if (*macroNames)[myName] == "" {
547 (*macroNames)[myName] = ctx.ModuleName()
548 } else if (*macroNames)[myName] != ctx.ModuleName() {
549 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
550 }
551 }
Colin Crossb916a382016-07-29 17:28:03 -0700552}
553
dimitry0345ad82018-12-05 16:28:14 +0100554func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
555 deps = library.baseCompiler.compilerDeps(ctx, deps)
556
dimitry0345ad82018-12-05 16:28:14 +0100557 return deps
558}
559
Colin Cross37047f12016-12-13 17:06:13 -0800560func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800561 if library.static() {
562 if library.Properties.Static.System_shared_libs != nil {
563 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
564 }
565 } else if library.shared() {
566 if library.Properties.Shared.System_shared_libs != nil {
567 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
568 }
569 }
570
Colin Crossb916a382016-07-29 17:28:03 -0700571 deps = library.baseLinker.linkerDeps(ctx, deps)
572
573 if library.static() {
574 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
575 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
577 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800578
579 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
580 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800581 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800582 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700583 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584 deps.CrtBegin = "crtbegin_so"
585 deps.CrtEnd = "crtend_so"
586 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800587 // TODO(danalbert): Add generation of crt objects.
588 // For `sdk_version: "current"`, we don't actually have a
589 // freshly generated set of CRT objects. Use the last stable
590 // version.
591 version := ctx.sdkVersion()
592 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700593 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800594 }
595 deps.CrtBegin = "ndk_crtbegin_so." + version
596 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700597 }
598 }
599 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
600 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
601 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800602
603 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
604 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700605 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900606 if ctx.useVndk() {
607 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
608 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
609 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000610 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
611 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900612 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900613 if ctx.inRecovery() {
614 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
615 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
616 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000617 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
618 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900619 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800620
Colin Cross4d9c2d12016-07-29 12:48:20 -0700621 return deps
622}
623
Colin Crossb916a382016-07-29 17:28:03 -0700624func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700625 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700626
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700627 library.objects = deps.WholeStaticLibObjs.Copy()
628 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700629
Colin Cross86803cf2018-02-15 14:12:26 -0800630 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
631 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800632 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633
Dan Willemsen569edc52018-11-19 09:33:29 -0800634 if Bool(library.baseLinker.Properties.Use_version_lib) {
635 if ctx.Host() {
636 versionedOutputFile := outputFile
637 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
638 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
639 } else {
640 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
641 library.distFile = android.OptionalPathForPath(versionedOutputFile)
642 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
643 }
Colin Cross86803cf2018-02-15 14:12:26 -0800644 }
645
Dan Willemsen581341d2017-02-09 16:16:31 -0800646 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
647
648 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800649 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700650
651 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
652
653 ctx.CheckbuildFile(outputFile)
654
655 return outputFile
656}
657
Colin Crossb916a382016-07-29 17:28:03 -0700658func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700659 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700660
661 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700662 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700663
Colin Cross2383f3b2018-02-06 14:40:13 -0800664 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
665 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
666 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700667 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700668 if unexportedSymbols.Valid() {
669 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
670 }
671 if forceNotWeakSymbols.Valid() {
672 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
673 }
674 if forceWeakSymbols.Valid() {
675 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
676 }
677 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700678 if unexportedSymbols.Valid() {
679 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
680 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
681 }
682 if forceNotWeakSymbols.Valid() {
683 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
684 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
685 }
686 if forceWeakSymbols.Valid() {
687 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
688 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
689 }
690 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900691 if library.buildStubs() {
692 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
693 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
694 linkerDeps = append(linkerDeps, library.versionScriptPath)
695 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696
697 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
698 outputFile := android.PathForModuleOut(ctx, fileName)
699 ret := outputFile
700
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700701 var implicitOutputs android.WritablePaths
702 if ctx.Windows() {
703 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
704
705 flags.LdFlags = append(flags.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
706 implicitOutputs = append(implicitOutputs, importLibraryPath)
707 }
708
Colin Cross4d9c2d12016-07-29 12:48:20 -0700709 builderFlags := flagsToBuilderFlags(flags)
710
Colin Crossb496cfd2018-09-10 16:50:05 -0700711 // Optimize out relinking against shared libraries whose interface hasn't changed by
712 // depending on a table of contents file instead of the library itself.
713 tocPath := outputFile.RelPathString()
714 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
715 tocFile := android.PathForOutput(ctx, tocPath)
716 library.tocFile = android.OptionalPathForPath(tocFile)
717 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700718
Colin Cross4d9c2d12016-07-29 12:48:20 -0700719 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800720 if ctx.Darwin() {
721 builderFlags.stripUseGnuStrip = true
722 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700723 strippedOutputFile := outputFile
724 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
725 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
726 }
727
Colin Crossb60190a2018-09-04 16:28:17 -0700728 library.unstrippedOutputFile = outputFile
729
Dan Willemsen569edc52018-11-19 09:33:29 -0800730 if Bool(library.baseLinker.Properties.Use_version_lib) {
731 if ctx.Host() {
732 versionedOutputFile := outputFile
733 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
734 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
735 } else {
736 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
737 library.distFile = android.OptionalPathForPath(versionedOutputFile)
738
739 if library.stripper.needsStrip(ctx) {
740 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
741 library.distFile = android.OptionalPathForPath(out)
742 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
743 }
744
745 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
746 }
Colin Cross86803cf2018-02-15 14:12:26 -0800747 }
748
Jiyong Park64a44f22019-01-18 14:37:08 +0900749 sharedLibs := deps.EarlySharedLibs
750 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
752
Jiyong Park64a44f22019-01-18 14:37:08 +0900753 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700754 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
755 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700756 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700757
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700758 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700759 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700760 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700761
Dan Willemsen581341d2017-02-09 16:16:31 -0800762 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
763 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800764
765 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
766 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
767
Dan Willemsen581341d2017-02-09 16:16:31 -0800768 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700769 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800770
Colin Cross4d9c2d12016-07-29 12:48:20 -0700771 return ret
772}
773
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900774func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
775 return library.unstrippedOutputFile
776}
777
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700778func (library *libraryDecorator) nativeCoverage() bool {
779 if library.header() || library.buildStubs() {
780 return false
781 }
782 return true
783}
784
Logan Chien7eefdc42018-07-11 18:10:41 +0800785func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chien41eabe62019-04-10 13:33:58 +0800786 isLlndkOrNdk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800787
Logan Chien41eabe62019-04-10 13:33:58 +0800788 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), false)
789 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), true)
Logan Chien7eefdc42018-07-11 18:10:41 +0800790
791 if refAbiDumpTextFile.Valid() {
792 if refAbiDumpGzipFile.Valid() {
793 ctx.ModuleErrorf(
794 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
795 refAbiDumpTextFile, refAbiDumpGzipFile)
796 return nil
797 }
798 return refAbiDumpTextFile.Path()
799 }
800 if refAbiDumpGzipFile.Valid() {
801 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
802 }
803 return nil
804}
805
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700806func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien41eabe62019-04-10 13:33:58 +0800807 if len(objs.sAbiDumpFiles) > 0 && library.shouldCreateVndkSourceAbiDump(ctx) {
Logan Chiena8f51582018-03-21 11:53:48 +0800808 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
809 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800810 vndkVersion = ver
811 }
812
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700813 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800814 var SourceAbiFlags []string
815 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700816 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
817 }
818 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
819 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800820 }
821 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800822 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
823 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
824 library.Properties.Header_abi_checker.Exclude_symbol_versions,
825 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800826
Logan Chien7eefdc42018-07-11 18:10:41 +0800827 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
828 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800829 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien62f1f942019-02-18 15:40:42 +0800830 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(), ctx.isNdk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800831 }
832 }
833}
834
Colin Crossb916a382016-07-29 17:28:03 -0700835func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700836 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700837
Colin Crossad59e752017-11-16 14:29:11 -0800838 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700839 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800840 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700841 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700842 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700843 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700844 }
845
846 library.exportIncludes(ctx, "-I")
847 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700848 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700849
Nan Zhang0007d812017-11-07 10:57:05 -0800850 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700851 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700852 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700853 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700854 }
855 library.reexportFlags(flags)
856 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800857 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
858 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700859 }
860 }
861
Nan Zhang0007d812017-11-07 10:57:05 -0800862 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700863 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800864 includes := []string{}
Colin Cross19878da2019-03-28 14:45:07 -0700865 if flags.proto.CanonicalPathFromRoot {
866 includes = append(includes, "-I"+flags.proto.SubDir.String())
Colin Cross10d22312017-05-03 11:01:58 -0700867 }
Colin Cross19878da2019-03-28 14:45:07 -0700868 includes = append(includes, "-I"+flags.proto.Dir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800869 library.reexportFlags(includes)
870 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800871 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
872 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700873 }
874 }
875
Inseob Kim21f26902018-09-06 00:55:20 +0900876 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kimc0907f12019-02-08 21:00:45 +0900877 internalFlags := []string{
Inseob Kim21f26902018-09-06 00:55:20 +0900878 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
879 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900880 systemFlags := []string{
881 "-I" + android.PathForModuleGen(ctx, "sysprop/system", "include").String(),
882 }
883
884 flags := internalFlags
885
886 if library.Properties.Sysprop.Platform != nil {
887 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
888 isVendor := ctx.useVndk()
889 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
890
891 useSystem := isProduct || (isOwnerPlatform == isVendor)
892
893 if useSystem {
894 flags = systemFlags
895 }
896 }
897
Inseob Kim21f26902018-09-06 00:55:20 +0900898 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900899 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900900 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
901 }
902
Jiyong Parkda732bd2018-11-02 18:23:15 +0900903 if library.buildStubs() {
904 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
905 }
906
Colin Cross4d9c2d12016-07-29 12:48:20 -0700907 return out
908}
909
Colin Crossb916a382016-07-29 17:28:03 -0700910func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700911 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700912}
913
Colin Crossb916a382016-07-29 17:28:03 -0700914func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700915 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700916}
917
Colin Crossb916a382016-07-29 17:28:03 -0700918func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700919 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700920}
921
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700922func (library *libraryDecorator) objs() Objects {
923 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700924}
925
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700926func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
927 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700928}
929
Colin Cross26c34ed2016-09-30 17:10:16 -0700930func (library *libraryDecorator) toc() android.OptionalPath {
931 return library.tocFile
932}
933
Jiyong Parkf1194352019-02-25 11:05:47 +0900934func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
935 dir := library.baseInstaller.installDir(ctx)
936 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
937 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
938 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
939 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
940}
941
Colin Crossb916a382016-07-29 17:28:03 -0700942func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700943 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900944 if ctx.Device() && ctx.useVndk() {
945 if ctx.isVndkSp() {
946 library.baseInstaller.subDir = "vndk-sp"
947 } else if ctx.isVndk() {
Vic Yangefd249e2018-11-12 20:19:56 -0800948 if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
949 library.useCoreVariant = true
950 }
Justin Yun8fe12122017-12-07 17:18:15 +0900951 library.baseInstaller.subDir = "vndk"
952 }
Logan Chienf3511742017-10-31 18:04:35 +0800953
954 // Append a version to vndk or vndk-sp directories on the system partition.
955 if ctx.isVndk() && !ctx.isVndkExt() {
956 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
957 if vndkVersion != "current" && vndkVersion != "" {
958 library.baseInstaller.subDir += "-" + vndkVersion
959 }
Justin Yun8effde42017-06-23 19:24:43 +0900960 }
Jiyong Park429660f2019-01-16 22:31:11 +0900961 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900962 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
963 // The original path becomes a symlink to the corresponding file in the
964 // runtime APEX.
965 if isBionic(ctx.baseModuleName()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900966 if ctx.Device() {
967 library.installSymlinkToRuntimeApex(ctx, file)
968 }
Jiyong Park429660f2019-01-16 22:31:11 +0900969 library.baseInstaller.subDir = "bootstrap"
970 }
Justin Yun8effde42017-06-23 19:24:43 +0900971 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700972 library.baseInstaller.install(ctx, file)
973 }
Dan Albertf563d252017-10-13 00:29:00 -0700974
Dan Albert281f22b2017-12-13 15:03:47 -0800975 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900976 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900977 library.baseLinker.sanitize.isUnsanitizedVariant() &&
978 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700979 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700980 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700981
982 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
983 Rule: android.Cp,
984 Description: "install " + installPath.Base(),
985 Output: installPath,
986 Input: file,
987 })
988
Colin Cross0875c522017-11-28 17:34:01 -0800989 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700990 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700991}
992
Colin Crossb916a382016-07-29 17:28:03 -0700993func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800994 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700995}
996
Colin Crossa48ab5b2017-02-14 15:28:44 -0800997func (library *libraryDecorator) shared() bool {
998 return library.MutatedProperties.VariantIsShared
999}
1000
1001func (library *libraryDecorator) header() bool {
1002 return !library.static() && !library.shared()
1003}
1004
1005func (library *libraryDecorator) setStatic() {
1006 library.MutatedProperties.VariantIsStatic = true
1007 library.MutatedProperties.VariantIsShared = false
1008}
1009
1010func (library *libraryDecorator) setShared() {
1011 library.MutatedProperties.VariantIsStatic = false
1012 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001013}
1014
Colin Crossab3b7322016-12-09 14:46:15 -08001015func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001016 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001017}
1018
1019func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001020 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001021}
1022
Colin Cross5950f382016-12-13 12:50:57 -08001023func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001024 library.MutatedProperties.BuildShared = false
1025 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001026}
1027
Jiyong Park7ed9de32018-10-15 22:25:07 +09001028func (library *libraryDecorator) buildStubs() bool {
1029 return library.MutatedProperties.BuildStubs
1030}
1031
1032func (library *libraryDecorator) stubsVersion() string {
1033 return library.MutatedProperties.StubsVersion
1034}
1035
Colin Cross571cccf2019-02-04 11:22:08 -08001036var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1037
Jiyong Parkda732bd2018-11-02 18:23:15 +09001038func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001039 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001040 m := make(map[string]string)
1041 return &m
1042 }).(*map[string]string)
1043}
1044
1045// alphanumeric and _ characters are preserved.
1046// other characters are all converted to _
1047var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1048
1049func versioningMacroName(moduleName string) string {
1050 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1051 macroName = strings.ToUpper(moduleName)
1052 return "__" + macroName + "_API__"
1053}
1054
Colin Crossab3b7322016-12-09 14:46:15 -08001055func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001056 module := newModule(hod, android.MultilibBoth)
1057
Colin Crossb916a382016-07-29 17:28:03 -07001058 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001059 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001060 BuildShared: true,
1061 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001062 },
Colin Crossb916a382016-07-29 17:28:03 -07001063 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001064 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001065 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001066 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001067 }
1068
Colin Crossb916a382016-07-29 17:28:03 -07001069 module.compiler = library
1070 module.linker = library
1071 module.installer = library
1072
1073 return module, library
1074}
1075
Colin Cross10d22312017-05-03 11:01:58 -07001076// connects a shared library to a static library in order to reuse its .o files to avoid
1077// compiling source files twice.
1078func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1079 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1080 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001081
1082 // Check libraries in addition to cflags, since libraries may be exporting different
1083 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001084 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001085 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1086 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1087 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1088 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1089 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1090 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1091 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1092 staticCompiler.Properties.Static.System_shared_libs == nil &&
1093 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001094
1095 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1096 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1097 sharedCompiler.baseCompiler.Properties.Srcs
1098 sharedCompiler.baseCompiler.Properties.Srcs = nil
1099 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001100 } else {
1101 // This dep is just to reference static variant from shared variant
1102 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001103 }
1104 }
1105}
1106
Colin Crosse40b4ea2018-10-02 22:25:58 -07001107func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001108 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1109 if library, ok := m.linker.(libraryInterface); ok {
1110 var modules []blueprint.Module
1111 if library.buildStatic() && library.buildShared() {
1112 modules = mctx.CreateLocalVariations("static", "shared")
1113 static := modules[0].(*Module)
1114 shared := modules[1].(*Module)
1115
Colin Crossa48ab5b2017-02-14 15:28:44 -08001116 static.linker.(libraryInterface).setStatic()
1117 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001118
Colin Cross10d22312017-05-03 11:01:58 -07001119 reuseStaticLibrary(mctx, static, shared)
1120
Colin Crossb916a382016-07-29 17:28:03 -07001121 } else if library.buildStatic() {
1122 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001123 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001124 } else if library.buildShared() {
1125 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001126 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001127 }
1128 }
1129 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001130}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001131
Colin Cross571cccf2019-02-04 11:22:08 -08001132var stubVersionsKey = android.NewOnceKey("stubVersions")
1133
Jiyong Park25fc6a92018-11-18 18:02:45 +09001134// maps a module name to the list of stubs versions available for the module
1135func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001136 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001137 return make(map[string][]string)
1138 }).(map[string][]string)
1139}
1140
1141var stubsVersionsLock sync.Mutex
1142
1143func latestStubsVersionFor(config android.Config, name string) string {
1144 versions, ok := stubsVersionsFor(config)[name]
1145 if ok && len(versions) > 0 {
1146 // the versions are alreay sorted in ascending order
1147 return versions[len(versions)-1]
1148 }
1149 return ""
1150}
1151
Jiyong Park7ed9de32018-10-15 22:25:07 +09001152// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001153// (which is unnamed) and zero or more stubs variants.
1154func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001155 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001156 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1157 len(library.Properties.Stubs.Versions) > 0 {
1158 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001159 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001160 if _, err := strconv.Atoi(v); err != nil {
1161 mctx.PropertyErrorf("versions", "%q is not a number", v)
1162 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001163 versions = append(versions, v)
1164 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001165 sort.Slice(versions, func(i, j int) bool {
1166 left, _ := strconv.Atoi(versions[i])
1167 right, _ := strconv.Atoi(versions[j])
1168 return left < right
1169 })
1170
1171 // save the list of versions for later use
1172 copiedVersions := make([]string, len(versions))
1173 copy(copiedVersions, versions)
1174 stubsVersionsLock.Lock()
1175 defer stubsVersionsLock.Unlock()
1176 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1177
1178 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001179 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001180
Jiyong Park7ed9de32018-10-15 22:25:07 +09001181 modules := mctx.CreateVariations(versions...)
1182 for i, m := range modules {
1183 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001184 if versions[i] != "" {
1185 l.MutatedProperties.BuildStubs = true
1186 l.MutatedProperties.StubsVersion = versions[i]
1187 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001188 m.(*Module).sanitize = nil
1189 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001190 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001191 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001192 }
1193 } else {
1194 mctx.CreateVariations("")
1195 }
1196 return
1197 }
1198 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1199 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1200 mctx.CreateVariations("")
1201 return
1202 }
1203 }
1204}