blob: 11c1d90e815ff5cbf4f2762bfadeafc0f82f9816 [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
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 {
Logan Chien41eabe62019-04-10 13:33:58 +080099 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
100 Enabled *bool
101
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800102 // Path to a symbol file that specifies the symbols to be included in the generated
103 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800105
106 // Symbol versions that should be ignored from the symbol file
107 Exclude_symbol_versions []string
108
109 // Symbol tags that should be ignored from the symbol file
110 Exclude_symbol_tags []string
111 }
Colin Crossa48ab5b2017-02-14 15:28:44 -0800112}
Colin Cross0c461f12016-10-20 16:11:43 -0700113
Colin Crossa48ab5b2017-02-14 15:28:44 -0800114type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700116
117 // Build a static variant
118 BuildStatic bool `blueprint:"mutated"`
119 // Build a shared variant
120 BuildShared bool `blueprint:"mutated"`
121 // This variant is shared
122 VariantIsShared bool `blueprint:"mutated"`
123 // This variant is static
124 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900125
126 // This variant is a stubs lib
127 BuildStubs bool `blueprint:"mutated"`
128 // Version of the stubs lib
129 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700130}
131
132type FlagExporterProperties struct {
133 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700134 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700135 // against this module. Directories listed in export_include_dirs do not need to be
136 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700137 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700138
139 Target struct {
140 Vendor struct {
141 // list of exported include directories, like
142 // export_include_dirs, that will be applied to the
143 // vendor variant of this library. This will overwrite
144 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800145 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700146 }
147 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148}
149
150func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700151 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
152 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
153 android.RegisterModuleType("cc_library", LibraryFactory)
154 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
155 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
156 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157}
158
Patrice Arruda83c89e02019-03-25 15:32:39 -0700159// cc_library creates both static and/or shared libraries for a device and/or
160// host. By default, a cc_library has a single variant that targets the device.
161// Specifying `host_supported: true` also creates a library that targets the
162// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700163func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800164 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 return module.Init()
166}
167
Patrice Arruda83c89e02019-03-25 15:32:39 -0700168// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700169func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800170 module, library := NewLibrary(android.HostAndDeviceSupported)
171 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 return module.Init()
173}
174
Patrice Arruda83c89e02019-03-25 15:32:39 -0700175// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700176func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800177 module, library := NewLibrary(android.HostAndDeviceSupported)
178 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 return module.Init()
180}
181
Patrice Arruda83c89e02019-03-25 15:32:39 -0700182// cc_library_host_static creates a static library that is linkable to a host
183// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700184func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800185 module, library := NewLibrary(android.HostSupported)
186 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 return module.Init()
188}
189
Patrice Arruda83c89e02019-03-25 15:32:39 -0700190// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700191func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800192 module, library := NewLibrary(android.HostSupported)
193 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 return module.Init()
195}
196
Patrice Arruda83c89e02019-03-25 15:32:39 -0700197// cc_library_headers contains a set of c/c++ headers which are imported by
198// other soong cc modules using the header_libs property. For best practices,
199// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
200// Make.
Steven Morelandf9e62162017-11-02 17:00:50 -0700201func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800202 module, library := NewLibrary(android.HostAndDeviceSupported)
203 library.HeaderOnly()
204 return module.Init()
205}
206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207type flagExporter struct {
208 Properties FlagExporterProperties
209
Dan Willemsen847dcc72016-09-29 12:13:36 -0700210 flags []string
211 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212}
213
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700214func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800215 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
216 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700217 } else {
218 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
219 }
220}
221
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700223 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224 for _, dir := range includeDirs.Strings() {
225 f.flags = append(f.flags, inc+dir)
226 }
227}
228
229func (f *flagExporter) reexportFlags(flags []string) {
230 f.flags = append(f.flags, flags...)
231}
232
Dan Willemsen847dcc72016-09-29 12:13:36 -0700233func (f *flagExporter) reexportDeps(deps android.Paths) {
234 f.flagsDeps = append(f.flagsDeps, deps...)
235}
236
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237func (f *flagExporter) exportedFlags() []string {
238 return f.flags
239}
240
Dan Willemsen847dcc72016-09-29 12:13:36 -0700241func (f *flagExporter) exportedFlagsDeps() android.Paths {
242 return f.flagsDeps
243}
244
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245type exportedFlagsProducer interface {
246 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700247 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248}
249
250var _ exportedFlagsProducer = (*flagExporter)(nil)
251
Colin Crossb916a382016-07-29 17:28:03 -0700252// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
253// functionality: static vs. shared linkage, reusing object files for shared libraries
254type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800255 Properties LibraryProperties
256 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257
258 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700259 reuseObjects Objects
260 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700261 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700262
Colin Cross26c34ed2016-09-30 17:10:16 -0700263 // table-of-contents file to optimize out relinking when possible
264 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 flagExporter
267 stripper
268
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 // If we're used as a whole_static_lib, our missing dependencies need
270 // to be given
271 wholeStaticMissingDeps []string
272
273 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700274 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275
276 // Uses the module's name if empty, but can be overridden. Does not include
277 // shlib suffix.
278 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700279
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800280 sabi *sabi
281
Dan Willemsen581341d2017-02-09 16:16:31 -0800282 // Output archive of gcno coverage information files
283 coverageOutputFile android.OptionalPath
284
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800285 // linked Source Abi Dump
286 sAbiOutputFile android.OptionalPath
287
288 // Source Abi Diff
289 sAbiDiff android.OptionalPath
290
Colin Cross0875c522017-11-28 17:34:01 -0800291 // Location of the static library in the sysroot. Empty if the library is
292 // not included in the NDK.
293 ndkSysrootPath android.Path
294
Colin Crossb60190a2018-09-04 16:28:17 -0700295 // Location of the linked, unstripped library for shared libraries
296 unstrippedOutputFile android.Path
297
Dan Willemsen569edc52018-11-19 09:33:29 -0800298 // Location of the file that should be copied to dist dir when requested
299 distFile android.OptionalPath
300
Jiyong Park7ed9de32018-10-15 22:25:07 +0900301 versionScriptPath android.ModuleGenPath
302
Jiyong Parkf1194352019-02-25 11:05:47 +0900303 post_install_cmds []string
304
Vic Yangefd249e2018-11-12 20:19:56 -0800305 // If useCoreVariant is true, the vendor variant of a VNDK library is
306 // not installed.
307 useCoreVariant bool
308
Colin Crossb916a382016-07-29 17:28:03 -0700309 // Decorated interafaces
310 *baseCompiler
311 *baseLinker
312 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313}
314
Colin Crossb916a382016-07-29 17:28:03 -0700315func (library *libraryDecorator) linkerProps() []interface{} {
316 var props []interface{}
317 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 return append(props,
319 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800320 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700322 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323}
324
Colin Crossb916a382016-07-29 17:28:03 -0700325func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700326 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327
Colin Crossb916a382016-07-29 17:28:03 -0700328 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
329 // all code is position independent, and then those warnings get promoted to
330 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700331 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700332 flags.CFlags = append(flags.CFlags, "-fPIC")
333 }
334
335 if library.static() {
336 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800337 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700338 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
339 }
340
Colin Crossa48ab5b2017-02-14 15:28:44 -0800341 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700344 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 f = append(f,
346 "-nostdlib",
347 "-Wl,--gc-sections",
348 )
349 }
350
351 if ctx.Darwin() {
352 f = append(f,
353 "-dynamiclib",
354 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
356 )
Colin Cross7863cf52016-10-20 10:47:21 -0700357 if ctx.Arch().ArchType == android.X86 {
358 f = append(f,
359 "-read_only_relocs suppress",
360 )
361 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362 } else {
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700363 f = append(f, "-shared")
364 if !ctx.Windows() {
365 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
366 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367 }
368
369 flags.LdFlags = append(f, flags.LdFlags...)
370 }
371
372 return flags
373}
374
Colin Crossf18e1102017-11-16 14:33:08 -0800375func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700376 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700377 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700378 f := includeDirsToFlags(exportIncludeDirs)
379 flags.GlobalFlags = append(flags.GlobalFlags, f)
380 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700381 }
382
Jiyong Park7ed9de32018-10-15 22:25:07 +0900383 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
384 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900385 // Remove -include <file> when compiling stubs. Otherwise, the force included
386 // headers might cause conflicting types error with the symbols in the
387 // generated stubs source code. e.g.
388 // double acos(double); // in header
389 // void acos() {} // in the generated source code
390 removeInclude := func(flags []string) []string {
391 ret := flags[:0]
392 for _, f := range flags {
393 if strings.HasPrefix(f, "-include ") {
394 continue
395 }
396 ret = append(ret, f)
397 }
398 return ret
399 }
400 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
401 flags.CFlags = removeInclude(flags.CFlags)
402
Jiyong Park7ed9de32018-10-15 22:25:07 +0900403 flags = addStubLibraryCompilerFlags(flags)
404 }
405 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700406}
407
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700408func extractExportIncludesFromFlags(flags []string) []string {
409 // This method is used in the generation of rules which produce
410 // abi-dumps for source files. Exported headers are needed to infer the
411 // abi exported by a library and filter out the rest of the abi dumped
412 // from a source. We extract the include flags exported by a library.
413 // This includes the flags exported which are re-exported from static
414 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700415 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700416 // since for bionic libraries, abi-filtering is taken care of by version
417 // scripts.
418 var exportedIncludes []string
419 for _, flag := range flags {
420 if strings.HasPrefix(flag, "-I") {
421 exportedIncludes = append(exportedIncludes, flag)
422 }
423 }
424 return exportedIncludes
425}
426
Logan Chien41eabe62019-04-10 13:33:58 +0800427func (library *libraryDecorator) shouldCreateVndkSourceAbiDump(ctx ModuleContext) bool {
428 if library.Properties.Header_abi_checker.Enabled != nil {
429 return Bool(library.Properties.Header_abi_checker.Enabled)
430 }
Inseob Kim9516ee92019-05-09 10:56:13 +0900431 return ctx.shouldCreateVndkSourceAbiDump(ctx.Config())
Logan Chien41eabe62019-04-10 13:33:58 +0800432}
433
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700434func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900435 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900436 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900437 library.versionScriptPath = versionScript
438 return objs
439 }
440
Colin Cross5950f382016-12-13 12:50:57 -0800441 if !library.buildShared() && !library.buildStatic() {
442 if len(library.baseCompiler.Properties.Srcs) > 0 {
443 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
444 }
445 if len(library.Properties.Static.Srcs) > 0 {
446 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
447 }
448 if len(library.Properties.Shared.Srcs) > 0 {
449 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
450 }
451 return Objects{}
452 }
Logan Chien41eabe62019-04-10 13:33:58 +0800453 if library.shouldCreateVndkSourceAbiDump(ctx) || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700454 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800455 var SourceAbiFlags []string
456 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700457 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800458 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700459 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
460 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
461 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800462 flags.SAbiFlags = SourceAbiFlags
463 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
464 len(library.Properties.Static.Srcs)
465 if total_length > 0 {
466 flags.SAbiDump = true
467 }
468 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700469 objs := library.baseCompiler.compile(ctx, flags, deps)
470 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700471 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700472
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800474 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700475 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800476 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800477 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800478 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700479 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800480 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700481 }
482
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700483 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700484}
485
486type libraryInterface interface {
487 getWholeStaticMissingDeps() []string
488 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700489 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700490 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700491 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700492
493 // Returns true if the build options for the module have selected a static or shared build
494 buildStatic() bool
495 buildShared() bool
496
497 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800498 setStatic()
499 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800500
501 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
502 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Colin Crossb916a382016-07-29 17:28:03 -0700503}
504
505func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
506 name := library.libName
507 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100508 name = String(library.Properties.Stem)
509 if name == "" {
510 name = ctx.baseModuleName()
511 }
Colin Crossb916a382016-07-29 17:28:03 -0700512 }
513
Logan Chienf3511742017-10-31 18:04:35 +0800514 if ctx.isVndkExt() {
515 name = ctx.getVndkExtendsModuleName()
516 }
517
Colin Crossb916a382016-07-29 17:28:03 -0700518 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
519 if !strings.HasSuffix(name, "-host") {
520 name = name + "-host"
521 }
522 }
523
Colin Crossa48ab5b2017-02-14 15:28:44 -0800524 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700525}
526
Jiyong Parkda732bd2018-11-02 18:23:15 +0900527var versioningMacroNamesListMutex sync.Mutex
528
Colin Crossb916a382016-07-29 17:28:03 -0700529func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
530 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700531 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700532 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700533 }
534 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700535 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900536 // Let baseLinker know whether this variant is for stubs or not, so that
537 // it can omit things that are not required for linking stubs.
538 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900539
540 if library.buildStubs() {
541 macroNames := versioningMacroNamesList(ctx.Config())
542 myName := versioningMacroName(ctx.ModuleName())
543 versioningMacroNamesListMutex.Lock()
544 defer versioningMacroNamesListMutex.Unlock()
545 if (*macroNames)[myName] == "" {
546 (*macroNames)[myName] = ctx.ModuleName()
547 } else if (*macroNames)[myName] != ctx.ModuleName() {
548 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
549 }
550 }
Colin Crossb916a382016-07-29 17:28:03 -0700551}
552
dimitry0345ad82018-12-05 16:28:14 +0100553func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
554 deps = library.baseCompiler.compilerDeps(ctx, deps)
555
dimitry0345ad82018-12-05 16:28:14 +0100556 return deps
557}
558
Colin Cross37047f12016-12-13 17:06:13 -0800559func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800560 if library.static() {
561 if library.Properties.Static.System_shared_libs != nil {
562 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
563 }
564 } else if library.shared() {
565 if library.Properties.Shared.System_shared_libs != nil {
566 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
567 }
568 }
569
Colin Crossb916a382016-07-29 17:28:03 -0700570 deps = library.baseLinker.linkerDeps(ctx, deps)
571
572 if library.static() {
573 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
574 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700575 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
576 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800577
578 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
579 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800580 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800581 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700582 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700583 deps.CrtBegin = "crtbegin_so"
584 deps.CrtEnd = "crtend_so"
585 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800586 // TODO(danalbert): Add generation of crt objects.
587 // For `sdk_version: "current"`, we don't actually have a
588 // freshly generated set of CRT objects. Use the last stable
589 // version.
590 version := ctx.sdkVersion()
591 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700592 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800593 }
594 deps.CrtBegin = "ndk_crtbegin_so." + version
595 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596 }
597 }
598 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
599 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
600 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800601
602 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
603 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900605 if ctx.useVndk() {
606 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
607 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
608 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000609 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
610 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900611 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900612 if ctx.inRecovery() {
613 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
614 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
615 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000616 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
617 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900618 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800619
Colin Cross4d9c2d12016-07-29 12:48:20 -0700620 return deps
621}
622
Colin Crossb916a382016-07-29 17:28:03 -0700623func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700624 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700626 library.objects = deps.WholeStaticLibObjs.Copy()
627 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700628
Colin Cross86803cf2018-02-15 14:12:26 -0800629 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
630 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800631 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700632
Dan Willemsen569edc52018-11-19 09:33:29 -0800633 if Bool(library.baseLinker.Properties.Use_version_lib) {
634 if ctx.Host() {
635 versionedOutputFile := outputFile
636 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
637 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
638 } else {
639 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
640 library.distFile = android.OptionalPathForPath(versionedOutputFile)
641 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
642 }
Colin Cross86803cf2018-02-15 14:12:26 -0800643 }
644
Dan Willemsen581341d2017-02-09 16:16:31 -0800645 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
646
647 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800648 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649
650 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
651
652 ctx.CheckbuildFile(outputFile)
653
654 return outputFile
655}
656
Colin Crossb916a382016-07-29 17:28:03 -0700657func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700658 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700659
660 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700661 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700662
Colin Cross2383f3b2018-02-06 14:40:13 -0800663 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
664 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
665 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700667 if unexportedSymbols.Valid() {
668 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
669 }
670 if forceNotWeakSymbols.Valid() {
671 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
672 }
673 if forceWeakSymbols.Valid() {
674 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
675 }
676 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700677 if unexportedSymbols.Valid() {
678 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
679 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
680 }
681 if forceNotWeakSymbols.Valid() {
682 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
683 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
684 }
685 if forceWeakSymbols.Valid() {
686 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
687 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
688 }
689 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900690 if library.buildStubs() {
691 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
692 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
693 linkerDeps = append(linkerDeps, library.versionScriptPath)
694 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700695
696 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
697 outputFile := android.PathForModuleOut(ctx, fileName)
698 ret := outputFile
699
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700700 var implicitOutputs android.WritablePaths
701 if ctx.Windows() {
702 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
703
704 flags.LdFlags = append(flags.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
705 implicitOutputs = append(implicitOutputs, importLibraryPath)
706 }
707
Colin Cross4d9c2d12016-07-29 12:48:20 -0700708 builderFlags := flagsToBuilderFlags(flags)
709
Colin Crossb496cfd2018-09-10 16:50:05 -0700710 // Optimize out relinking against shared libraries whose interface hasn't changed by
711 // depending on a table of contents file instead of the library itself.
712 tocPath := outputFile.RelPathString()
713 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
714 tocFile := android.PathForOutput(ctx, tocPath)
715 library.tocFile = android.OptionalPathForPath(tocFile)
716 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700717
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800719 if ctx.Darwin() {
720 builderFlags.stripUseGnuStrip = true
721 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722 strippedOutputFile := outputFile
723 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
724 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
725 }
726
Colin Crossb60190a2018-09-04 16:28:17 -0700727 library.unstrippedOutputFile = outputFile
728
Dan Willemsen569edc52018-11-19 09:33:29 -0800729 if Bool(library.baseLinker.Properties.Use_version_lib) {
730 if ctx.Host() {
731 versionedOutputFile := outputFile
732 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
733 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
734 } else {
735 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
736 library.distFile = android.OptionalPathForPath(versionedOutputFile)
737
738 if library.stripper.needsStrip(ctx) {
739 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
740 library.distFile = android.OptionalPathForPath(out)
741 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
742 }
743
744 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
745 }
Colin Cross86803cf2018-02-15 14:12:26 -0800746 }
747
Jiyong Park64a44f22019-01-18 14:37:08 +0900748 sharedLibs := deps.EarlySharedLibs
749 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700750 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
751
Jiyong Park64a44f22019-01-18 14:37:08 +0900752 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700753 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
754 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700755 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700756
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700757 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700758 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Pirama Arumuga Nainar6d8c0a52019-04-15 23:03:38 -0700759 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700760
Dan Willemsen581341d2017-02-09 16:16:31 -0800761 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
762 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800763
764 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
765 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
766
Dan Willemsen581341d2017-02-09 16:16:31 -0800767 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700768 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800769
Colin Cross4d9c2d12016-07-29 12:48:20 -0700770 return ret
771}
772
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900773func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
774 return library.unstrippedOutputFile
775}
776
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700777func (library *libraryDecorator) nativeCoverage() bool {
778 if library.header() || library.buildStubs() {
779 return false
780 }
781 return true
782}
783
Logan Chien7eefdc42018-07-11 18:10:41 +0800784func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Inseob Kim9516ee92019-05-09 10:56:13 +0900785 isLlndkOrNdk := inList(ctx.baseModuleName(), *llndkLibraries(ctx.Config())) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800786
Logan Chien41eabe62019-04-10 13:33:58 +0800787 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), false)
788 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), true)
Logan Chien7eefdc42018-07-11 18:10:41 +0800789
790 if refAbiDumpTextFile.Valid() {
791 if refAbiDumpGzipFile.Valid() {
792 ctx.ModuleErrorf(
793 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
794 refAbiDumpTextFile, refAbiDumpGzipFile)
795 return nil
796 }
797 return refAbiDumpTextFile.Path()
798 }
799 if refAbiDumpGzipFile.Valid() {
800 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
801 }
802 return nil
803}
804
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700805func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien41eabe62019-04-10 13:33:58 +0800806 if len(objs.sAbiDumpFiles) > 0 && library.shouldCreateVndkSourceAbiDump(ctx) {
Logan Chiena8f51582018-03-21 11:53:48 +0800807 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
808 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800809 vndkVersion = ver
810 }
811
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700812 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800813 var SourceAbiFlags []string
814 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700815 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
816 }
817 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
818 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800819 }
820 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800821 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
822 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
823 library.Properties.Header_abi_checker.Exclude_symbol_versions,
824 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800825
Logan Chien7eefdc42018-07-11 18:10:41 +0800826 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
827 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800828 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Inseob Kim9516ee92019-05-09 10:56:13 +0900829 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(ctx.Config()), ctx.isNdk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800830 }
831 }
832}
833
Colin Crossb916a382016-07-29 17:28:03 -0700834func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700835 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700836
Colin Crossad59e752017-11-16 14:29:11 -0800837 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700838 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800839 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700840 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700841 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700842 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700843 }
844
845 library.exportIncludes(ctx, "-I")
846 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700847 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700848
Nan Zhang0007d812017-11-07 10:57:05 -0800849 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700850 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700851 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700852 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700853 }
854 library.reexportFlags(flags)
855 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800856 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
857 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700858 }
859 }
860
Nan Zhang0007d812017-11-07 10:57:05 -0800861 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700862 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800863 includes := []string{}
Colin Cross19878da2019-03-28 14:45:07 -0700864 if flags.proto.CanonicalPathFromRoot {
865 includes = append(includes, "-I"+flags.proto.SubDir.String())
Colin Cross10d22312017-05-03 11:01:58 -0700866 }
Colin Cross19878da2019-03-28 14:45:07 -0700867 includes = append(includes, "-I"+flags.proto.Dir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800868 library.reexportFlags(includes)
869 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800870 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
871 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700872 }
873 }
874
Inseob Kim21f26902018-09-06 00:55:20 +0900875 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kimc0907f12019-02-08 21:00:45 +0900876 internalFlags := []string{
Inseob Kim21f26902018-09-06 00:55:20 +0900877 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
878 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900879 systemFlags := []string{
880 "-I" + android.PathForModuleGen(ctx, "sysprop/system", "include").String(),
881 }
882
883 flags := internalFlags
884
885 if library.Properties.Sysprop.Platform != nil {
886 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
887 isVendor := ctx.useVndk()
888 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
889
890 useSystem := isProduct || (isOwnerPlatform == isVendor)
891
892 if useSystem {
893 flags = systemFlags
894 }
895 }
896
Inseob Kim21f26902018-09-06 00:55:20 +0900897 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900898 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900899 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
900 }
901
Jiyong Parkda732bd2018-11-02 18:23:15 +0900902 if library.buildStubs() {
903 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
904 }
905
Colin Cross4d9c2d12016-07-29 12:48:20 -0700906 return out
907}
908
Colin Crossb916a382016-07-29 17:28:03 -0700909func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700910 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700911}
912
Colin Crossb916a382016-07-29 17:28:03 -0700913func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700914 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700915}
916
Colin Crossb916a382016-07-29 17:28:03 -0700917func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700918 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700919}
920
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700921func (library *libraryDecorator) objs() Objects {
922 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700923}
924
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700925func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
926 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700927}
928
Colin Cross26c34ed2016-09-30 17:10:16 -0700929func (library *libraryDecorator) toc() android.OptionalPath {
930 return library.tocFile
931}
932
Jiyong Parkf1194352019-02-25 11:05:47 +0900933func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
934 dir := library.baseInstaller.installDir(ctx)
935 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
936 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
937 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
938 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
939}
940
Colin Crossb916a382016-07-29 17:28:03 -0700941func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700942 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900943 if ctx.Device() && ctx.useVndk() {
944 if ctx.isVndkSp() {
945 library.baseInstaller.subDir = "vndk-sp"
946 } else if ctx.isVndk() {
Vic Yangefd249e2018-11-12 20:19:56 -0800947 if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
948 library.useCoreVariant = true
949 }
Justin Yun8fe12122017-12-07 17:18:15 +0900950 library.baseInstaller.subDir = "vndk"
951 }
Logan Chienf3511742017-10-31 18:04:35 +0800952
953 // Append a version to vndk or vndk-sp directories on the system partition.
954 if ctx.isVndk() && !ctx.isVndkExt() {
955 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
956 if vndkVersion != "current" && vndkVersion != "" {
957 library.baseInstaller.subDir += "-" + vndkVersion
958 }
Justin Yun8effde42017-06-23 19:24:43 +0900959 }
Jiyong Park429660f2019-01-16 22:31:11 +0900960 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900961 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
962 // The original path becomes a symlink to the corresponding file in the
963 // runtime APEX.
964 if isBionic(ctx.baseModuleName()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900965 if ctx.Device() {
966 library.installSymlinkToRuntimeApex(ctx, file)
967 }
Jiyong Park429660f2019-01-16 22:31:11 +0900968 library.baseInstaller.subDir = "bootstrap"
969 }
Justin Yun8effde42017-06-23 19:24:43 +0900970 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700971 library.baseInstaller.install(ctx, file)
972 }
Dan Albertf563d252017-10-13 00:29:00 -0700973
Dan Albert281f22b2017-12-13 15:03:47 -0800974 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900975 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900976 library.baseLinker.sanitize.isUnsanitizedVariant() &&
977 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700978 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700979 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700980
981 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
982 Rule: android.Cp,
983 Description: "install " + installPath.Base(),
984 Output: installPath,
985 Input: file,
986 })
987
Colin Cross0875c522017-11-28 17:34:01 -0800988 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700989 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700990}
991
Colin Crossb916a382016-07-29 17:28:03 -0700992func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800993 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700994}
995
Colin Crossa48ab5b2017-02-14 15:28:44 -0800996func (library *libraryDecorator) shared() bool {
997 return library.MutatedProperties.VariantIsShared
998}
999
1000func (library *libraryDecorator) header() bool {
1001 return !library.static() && !library.shared()
1002}
1003
1004func (library *libraryDecorator) setStatic() {
1005 library.MutatedProperties.VariantIsStatic = true
1006 library.MutatedProperties.VariantIsShared = false
1007}
1008
1009func (library *libraryDecorator) setShared() {
1010 library.MutatedProperties.VariantIsStatic = false
1011 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001012}
1013
Colin Crossab3b7322016-12-09 14:46:15 -08001014func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001015 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001016}
1017
1018func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001019 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001020}
1021
Colin Cross5950f382016-12-13 12:50:57 -08001022func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001023 library.MutatedProperties.BuildShared = false
1024 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001025}
1026
Jiyong Park7ed9de32018-10-15 22:25:07 +09001027func (library *libraryDecorator) buildStubs() bool {
1028 return library.MutatedProperties.BuildStubs
1029}
1030
1031func (library *libraryDecorator) stubsVersion() string {
1032 return library.MutatedProperties.StubsVersion
1033}
1034
Colin Cross571cccf2019-02-04 11:22:08 -08001035var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1036
Jiyong Parkda732bd2018-11-02 18:23:15 +09001037func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001038 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001039 m := make(map[string]string)
1040 return &m
1041 }).(*map[string]string)
1042}
1043
1044// alphanumeric and _ characters are preserved.
1045// other characters are all converted to _
1046var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1047
1048func versioningMacroName(moduleName string) string {
1049 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1050 macroName = strings.ToUpper(moduleName)
1051 return "__" + macroName + "_API__"
1052}
1053
Colin Crossab3b7322016-12-09 14:46:15 -08001054func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001055 module := newModule(hod, android.MultilibBoth)
1056
Colin Crossb916a382016-07-29 17:28:03 -07001057 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001058 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001059 BuildShared: true,
1060 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001061 },
Colin Crossb916a382016-07-29 17:28:03 -07001062 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001063 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001064 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001065 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001066 }
1067
Colin Crossb916a382016-07-29 17:28:03 -07001068 module.compiler = library
1069 module.linker = library
1070 module.installer = library
1071
1072 return module, library
1073}
1074
Colin Cross10d22312017-05-03 11:01:58 -07001075// connects a shared library to a static library in order to reuse its .o files to avoid
1076// compiling source files twice.
1077func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1078 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1079 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001080
1081 // Check libraries in addition to cflags, since libraries may be exporting different
1082 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001083 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001084 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1085 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1086 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1087 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1088 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1089 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1090 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1091 staticCompiler.Properties.Static.System_shared_libs == nil &&
1092 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001093
1094 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1095 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1096 sharedCompiler.baseCompiler.Properties.Srcs
1097 sharedCompiler.baseCompiler.Properties.Srcs = nil
1098 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001099 } else {
1100 // This dep is just to reference static variant from shared variant
1101 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001102 }
1103 }
1104}
1105
Colin Crosse40b4ea2018-10-02 22:25:58 -07001106func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001107 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
Colin Cross33b2fb72019-05-14 14:07:01 -07001108 switch library := m.linker.(type) {
1109 case prebuiltLibraryInterface:
1110 // Always create both the static and shared variants for prebuilt libraries, and then disable the one
1111 // that is not being used. This allows them to share the name of a cc_library module, which requires that
1112 // all the variants of the cc_library also exist on the prebuilt.
1113 modules := mctx.CreateLocalVariations("static", "shared")
1114 static := modules[0].(*Module)
1115 shared := modules[1].(*Module)
1116
1117 static.linker.(prebuiltLibraryInterface).setStatic()
1118 shared.linker.(prebuiltLibraryInterface).setShared()
1119
1120 if !library.buildStatic() {
1121 static.linker.(prebuiltLibraryInterface).disablePrebuilt()
1122 }
1123 if !library.buildShared() {
1124 shared.linker.(prebuiltLibraryInterface).disablePrebuilt()
1125 }
1126
1127 case libraryInterface:
Colin Crossb916a382016-07-29 17:28:03 -07001128 if library.buildStatic() && library.buildShared() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001129 modules := mctx.CreateLocalVariations("static", "shared")
Colin Crossb916a382016-07-29 17:28:03 -07001130 static := modules[0].(*Module)
1131 shared := modules[1].(*Module)
1132
Colin Crossa48ab5b2017-02-14 15:28:44 -08001133 static.linker.(libraryInterface).setStatic()
1134 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001135
Colin Cross10d22312017-05-03 11:01:58 -07001136 reuseStaticLibrary(mctx, static, shared)
1137
Colin Crossb916a382016-07-29 17:28:03 -07001138 } else if library.buildStatic() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001139 modules := mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001140 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001141 } else if library.buildShared() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001142 modules := mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001143 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001144 }
1145 }
1146 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001147}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001148
Colin Cross571cccf2019-02-04 11:22:08 -08001149var stubVersionsKey = android.NewOnceKey("stubVersions")
1150
Jiyong Park25fc6a92018-11-18 18:02:45 +09001151// maps a module name to the list of stubs versions available for the module
1152func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001153 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001154 return make(map[string][]string)
1155 }).(map[string][]string)
1156}
1157
1158var stubsVersionsLock sync.Mutex
1159
1160func latestStubsVersionFor(config android.Config, name string) string {
1161 versions, ok := stubsVersionsFor(config)[name]
1162 if ok && len(versions) > 0 {
1163 // the versions are alreay sorted in ascending order
1164 return versions[len(versions)-1]
1165 }
1166 return ""
1167}
1168
Jiyong Park7ed9de32018-10-15 22:25:07 +09001169// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001170// (which is unnamed) and zero or more stubs variants.
1171func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001172 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001173 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1174 len(library.Properties.Stubs.Versions) > 0 {
1175 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001176 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001177 if _, err := strconv.Atoi(v); err != nil {
1178 mctx.PropertyErrorf("versions", "%q is not a number", v)
1179 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001180 versions = append(versions, v)
1181 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001182 sort.Slice(versions, func(i, j int) bool {
1183 left, _ := strconv.Atoi(versions[i])
1184 right, _ := strconv.Atoi(versions[j])
1185 return left < right
1186 })
1187
1188 // save the list of versions for later use
1189 copiedVersions := make([]string, len(versions))
1190 copy(copiedVersions, versions)
1191 stubsVersionsLock.Lock()
1192 defer stubsVersionsLock.Unlock()
1193 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1194
1195 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001196 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001197
Jiyong Park7ed9de32018-10-15 22:25:07 +09001198 modules := mctx.CreateVariations(versions...)
1199 for i, m := range modules {
1200 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001201 if versions[i] != "" {
1202 l.MutatedProperties.BuildStubs = true
1203 l.MutatedProperties.StubsVersion = versions[i]
1204 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001205 m.(*Module).sanitize = nil
1206 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001207 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001208 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001209 }
1210 } else {
1211 mctx.CreateVariations("")
1212 }
1213 return
1214 }
1215 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1216 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1217 mctx.CreateVariations("")
1218 return
1219 }
1220 }
1221}