blob: 203d8760d34050a43991c3c214dbb9f6123336b0 [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 {
Josh Gaob20200b2019-06-07 12:30:30 -0700363 f = append(f,
364 "-shared",
365 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 }
367
368 flags.LdFlags = append(f, flags.LdFlags...)
369 }
370
371 return flags
372}
373
Colin Crossf18e1102017-11-16 14:33:08 -0800374func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700375 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700376 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700377 f := includeDirsToFlags(exportIncludeDirs)
378 flags.GlobalFlags = append(flags.GlobalFlags, f)
379 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700380 }
381
Jiyong Park7ed9de32018-10-15 22:25:07 +0900382 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
383 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900384 // Remove -include <file> when compiling stubs. Otherwise, the force included
385 // headers might cause conflicting types error with the symbols in the
386 // generated stubs source code. e.g.
387 // double acos(double); // in header
388 // void acos() {} // in the generated source code
389 removeInclude := func(flags []string) []string {
390 ret := flags[:0]
391 for _, f := range flags {
392 if strings.HasPrefix(f, "-include ") {
393 continue
394 }
395 ret = append(ret, f)
396 }
397 return ret
398 }
399 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
400 flags.CFlags = removeInclude(flags.CFlags)
401
Jiyong Park7ed9de32018-10-15 22:25:07 +0900402 flags = addStubLibraryCompilerFlags(flags)
403 }
404 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700405}
406
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700407func extractExportIncludesFromFlags(flags []string) []string {
408 // This method is used in the generation of rules which produce
409 // abi-dumps for source files. Exported headers are needed to infer the
410 // abi exported by a library and filter out the rest of the abi dumped
411 // from a source. We extract the include flags exported by a library.
412 // This includes the flags exported which are re-exported from static
413 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700414 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700415 // since for bionic libraries, abi-filtering is taken care of by version
416 // scripts.
417 var exportedIncludes []string
418 for _, flag := range flags {
419 if strings.HasPrefix(flag, "-I") {
420 exportedIncludes = append(exportedIncludes, flag)
421 }
422 }
423 return exportedIncludes
424}
425
Logan Chien41eabe62019-04-10 13:33:58 +0800426func (library *libraryDecorator) shouldCreateVndkSourceAbiDump(ctx ModuleContext) bool {
427 if library.Properties.Header_abi_checker.Enabled != nil {
428 return Bool(library.Properties.Header_abi_checker.Enabled)
429 }
Inseob Kim9516ee92019-05-09 10:56:13 +0900430 return ctx.shouldCreateVndkSourceAbiDump(ctx.Config())
Logan Chien41eabe62019-04-10 13:33:58 +0800431}
432
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700433func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900434 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900435 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900436 library.versionScriptPath = versionScript
437 return objs
438 }
439
Colin Cross5950f382016-12-13 12:50:57 -0800440 if !library.buildShared() && !library.buildStatic() {
441 if len(library.baseCompiler.Properties.Srcs) > 0 {
442 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
443 }
444 if len(library.Properties.Static.Srcs) > 0 {
445 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
446 }
447 if len(library.Properties.Shared.Srcs) > 0 {
448 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
449 }
450 return Objects{}
451 }
Logan Chien41eabe62019-04-10 13:33:58 +0800452 if library.shouldCreateVndkSourceAbiDump(ctx) || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700453 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800454 var SourceAbiFlags []string
455 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700456 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800457 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700458 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
459 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
460 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800461 flags.SAbiFlags = SourceAbiFlags
462 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
463 len(library.Properties.Static.Srcs)
464 if total_length > 0 {
465 flags.SAbiDump = true
466 }
467 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700468 objs := library.baseCompiler.compile(ctx, flags, deps)
469 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700470 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700471
Colin Cross4d9c2d12016-07-29 12:48:20 -0700472 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800473 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700474 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800475 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800476 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800477 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700478 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800479 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700480 }
481
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700482 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700483}
484
485type libraryInterface interface {
486 getWholeStaticMissingDeps() []string
487 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700488 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700489 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700490 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700491
492 // Returns true if the build options for the module have selected a static or shared build
493 buildStatic() bool
494 buildShared() bool
495
496 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800497 setStatic()
498 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800499
500 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
501 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Colin Crossb916a382016-07-29 17:28:03 -0700502}
503
504func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
505 name := library.libName
506 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100507 name = String(library.Properties.Stem)
508 if name == "" {
509 name = ctx.baseModuleName()
510 }
Colin Crossb916a382016-07-29 17:28:03 -0700511 }
512
Logan Chienf3511742017-10-31 18:04:35 +0800513 if ctx.isVndkExt() {
514 name = ctx.getVndkExtendsModuleName()
515 }
516
Colin Crossb916a382016-07-29 17:28:03 -0700517 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
518 if !strings.HasSuffix(name, "-host") {
519 name = name + "-host"
520 }
521 }
522
Colin Crossa48ab5b2017-02-14 15:28:44 -0800523 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700524}
525
Jiyong Parkda732bd2018-11-02 18:23:15 +0900526var versioningMacroNamesListMutex sync.Mutex
527
Colin Crossb916a382016-07-29 17:28:03 -0700528func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
529 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700530 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700531 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700532 }
533 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700534 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900535 // Let baseLinker know whether this variant is for stubs or not, so that
536 // it can omit things that are not required for linking stubs.
537 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900538
539 if library.buildStubs() {
540 macroNames := versioningMacroNamesList(ctx.Config())
541 myName := versioningMacroName(ctx.ModuleName())
542 versioningMacroNamesListMutex.Lock()
543 defer versioningMacroNamesListMutex.Unlock()
544 if (*macroNames)[myName] == "" {
545 (*macroNames)[myName] = ctx.ModuleName()
546 } else if (*macroNames)[myName] != ctx.ModuleName() {
547 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
548 }
549 }
Colin Crossb916a382016-07-29 17:28:03 -0700550}
551
dimitry0345ad82018-12-05 16:28:14 +0100552func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
553 deps = library.baseCompiler.compilerDeps(ctx, deps)
554
dimitry0345ad82018-12-05 16:28:14 +0100555 return deps
556}
557
Colin Cross37047f12016-12-13 17:06:13 -0800558func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800559 if library.static() {
560 if library.Properties.Static.System_shared_libs != nil {
561 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
562 }
563 } else if library.shared() {
564 if library.Properties.Shared.System_shared_libs != nil {
565 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
566 }
567 }
568
Colin Crossb916a382016-07-29 17:28:03 -0700569 deps = library.baseLinker.linkerDeps(ctx, deps)
570
571 if library.static() {
572 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
573 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700574 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
575 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800576
577 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
578 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800579 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800580 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700581 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582 deps.CrtBegin = "crtbegin_so"
583 deps.CrtEnd = "crtend_so"
584 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800585 // TODO(danalbert): Add generation of crt objects.
586 // For `sdk_version: "current"`, we don't actually have a
587 // freshly generated set of CRT objects. Use the last stable
588 // version.
589 version := ctx.sdkVersion()
590 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700591 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800592 }
593 deps.CrtBegin = "ndk_crtbegin_so." + version
594 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595 }
596 }
597 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
598 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
599 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800600
601 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
602 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900604 if ctx.useVndk() {
605 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
606 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
607 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000608 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
609 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900610 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900611 if ctx.inRecovery() {
612 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
613 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
614 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000615 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
616 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900617 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800618
Colin Cross4d9c2d12016-07-29 12:48:20 -0700619 return deps
620}
621
Colin Crossb916a382016-07-29 17:28:03 -0700622func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700623 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700624
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700625 library.objects = deps.WholeStaticLibObjs.Copy()
626 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627
Colin Cross86803cf2018-02-15 14:12:26 -0800628 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
629 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800630 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700631
Dan Willemsen569edc52018-11-19 09:33:29 -0800632 if Bool(library.baseLinker.Properties.Use_version_lib) {
633 if ctx.Host() {
634 versionedOutputFile := outputFile
635 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
636 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
637 } else {
638 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
639 library.distFile = android.OptionalPathForPath(versionedOutputFile)
640 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
641 }
Colin Cross86803cf2018-02-15 14:12:26 -0800642 }
643
Dan Willemsen581341d2017-02-09 16:16:31 -0800644 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
645
Oliver Nguyenc7434142019-04-24 14:22:25 -0700646 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, library.objects,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800647 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700648
649 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
650
651 ctx.CheckbuildFile(outputFile)
652
653 return outputFile
654}
655
Colin Crossb916a382016-07-29 17:28:03 -0700656func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700657 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700658
659 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700660 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700661
Colin Cross2383f3b2018-02-06 14:40:13 -0800662 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
663 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
664 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700665 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666 if unexportedSymbols.Valid() {
667 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
668 }
669 if forceNotWeakSymbols.Valid() {
670 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
671 }
672 if forceWeakSymbols.Valid() {
673 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
674 }
675 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700676 if unexportedSymbols.Valid() {
677 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
678 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
679 }
680 if forceNotWeakSymbols.Valid() {
681 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
682 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
683 }
684 if forceWeakSymbols.Valid() {
685 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
686 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
687 }
688 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900689 if library.buildStubs() {
690 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
691 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
692 linkerDeps = append(linkerDeps, library.versionScriptPath)
693 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700694
695 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
696 outputFile := android.PathForModuleOut(ctx, fileName)
697 ret := outputFile
698
699 builderFlags := flagsToBuilderFlags(flags)
700
Colin Crossb496cfd2018-09-10 16:50:05 -0700701 // Optimize out relinking against shared libraries whose interface hasn't changed by
702 // depending on a table of contents file instead of the library itself.
703 tocPath := outputFile.RelPathString()
704 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
705 tocFile := android.PathForOutput(ctx, tocPath)
706 library.tocFile = android.OptionalPathForPath(tocFile)
707 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700708
Colin Cross4d9c2d12016-07-29 12:48:20 -0700709 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800710 if ctx.Darwin() {
711 builderFlags.stripUseGnuStrip = true
712 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700713 strippedOutputFile := outputFile
714 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700715 library.stripper.stripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, builderFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700716 }
717
Colin Crossb60190a2018-09-04 16:28:17 -0700718 library.unstrippedOutputFile = outputFile
719
Dan Willemsen569edc52018-11-19 09:33:29 -0800720 if Bool(library.baseLinker.Properties.Use_version_lib) {
721 if ctx.Host() {
722 versionedOutputFile := outputFile
723 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
724 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
725 } else {
726 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
727 library.distFile = android.OptionalPathForPath(versionedOutputFile)
728
729 if library.stripper.needsStrip(ctx) {
730 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
731 library.distFile = android.OptionalPathForPath(out)
Ryan Prichardf979d732019-05-30 20:53:29 -0700732 library.stripper.stripExecutableOrSharedLib(ctx, versionedOutputFile, out, builderFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800733 }
734
735 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
736 }
Colin Cross86803cf2018-02-15 14:12:26 -0800737 }
738
Jiyong Park64a44f22019-01-18 14:37:08 +0900739 sharedLibs := deps.EarlySharedLibs
740 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700741 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
742
Jiyong Park64a44f22019-01-18 14:37:08 +0900743 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700744 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
745 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700746 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700747
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700748 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700749 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Josh Gaob20200b2019-06-07 12:30:30 -0700750 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751
Dan Willemsen581341d2017-02-09 16:16:31 -0800752 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
753 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800754
755 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
756 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
757
Oliver Nguyenc7434142019-04-24 14:22:25 -0700758 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700759 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800760
Colin Cross4d9c2d12016-07-29 12:48:20 -0700761 return ret
762}
763
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900764func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
765 return library.unstrippedOutputFile
766}
767
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700768func (library *libraryDecorator) nativeCoverage() bool {
769 if library.header() || library.buildStubs() {
770 return false
771 }
772 return true
773}
774
Logan Chien7eefdc42018-07-11 18:10:41 +0800775func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Inseob Kim9516ee92019-05-09 10:56:13 +0900776 isLlndkOrNdk := inList(ctx.baseModuleName(), *llndkLibraries(ctx.Config())) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800777
Logan Chien41eabe62019-04-10 13:33:58 +0800778 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), false)
779 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), true)
Logan Chien7eefdc42018-07-11 18:10:41 +0800780
781 if refAbiDumpTextFile.Valid() {
782 if refAbiDumpGzipFile.Valid() {
783 ctx.ModuleErrorf(
784 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
785 refAbiDumpTextFile, refAbiDumpGzipFile)
786 return nil
787 }
788 return refAbiDumpTextFile.Path()
789 }
790 if refAbiDumpGzipFile.Valid() {
791 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
792 }
793 return nil
794}
795
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700796func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien41eabe62019-04-10 13:33:58 +0800797 if len(objs.sAbiDumpFiles) > 0 && library.shouldCreateVndkSourceAbiDump(ctx) {
Logan Chiena8f51582018-03-21 11:53:48 +0800798 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
799 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800800 vndkVersion = ver
801 }
802
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700803 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800804 var SourceAbiFlags []string
805 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700806 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
807 }
808 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
809 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800810 }
811 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800812 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
813 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
814 library.Properties.Header_abi_checker.Exclude_symbol_versions,
815 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800816
Logan Chien7eefdc42018-07-11 18:10:41 +0800817 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
818 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800819 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Inseob Kim9516ee92019-05-09 10:56:13 +0900820 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(ctx.Config()), ctx.isNdk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800821 }
822 }
823}
824
Colin Crossb916a382016-07-29 17:28:03 -0700825func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700826 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700827
Colin Crossad59e752017-11-16 14:29:11 -0800828 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700829 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800830 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700831 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700832 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700833 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700834 }
835
836 library.exportIncludes(ctx, "-I")
837 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700838 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700839
Nan Zhang0007d812017-11-07 10:57:05 -0800840 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700841 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700842 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700843 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700844 }
845 library.reexportFlags(flags)
846 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800847 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
848 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700849 }
850 }
851
Nan Zhang0007d812017-11-07 10:57:05 -0800852 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700853 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800854 includes := []string{}
Colin Cross19878da2019-03-28 14:45:07 -0700855 if flags.proto.CanonicalPathFromRoot {
856 includes = append(includes, "-I"+flags.proto.SubDir.String())
Colin Cross10d22312017-05-03 11:01:58 -0700857 }
Colin Cross19878da2019-03-28 14:45:07 -0700858 includes = append(includes, "-I"+flags.proto.Dir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800859 library.reexportFlags(includes)
860 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800861 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
862 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700863 }
864 }
865
Inseob Kim21f26902018-09-06 00:55:20 +0900866 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kimc0907f12019-02-08 21:00:45 +0900867 internalFlags := []string{
Inseob Kim21f26902018-09-06 00:55:20 +0900868 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
869 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900870 systemFlags := []string{
871 "-I" + android.PathForModuleGen(ctx, "sysprop/system", "include").String(),
872 }
873
874 flags := internalFlags
875
876 if library.Properties.Sysprop.Platform != nil {
877 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
878 isVendor := ctx.useVndk()
879 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
880
881 useSystem := isProduct || (isOwnerPlatform == isVendor)
882
883 if useSystem {
884 flags = systemFlags
885 }
886 }
887
Inseob Kim21f26902018-09-06 00:55:20 +0900888 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900889 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900890 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
891 }
892
Jiyong Parkda732bd2018-11-02 18:23:15 +0900893 if library.buildStubs() {
894 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
895 }
896
Colin Cross4d9c2d12016-07-29 12:48:20 -0700897 return out
898}
899
Colin Crossb916a382016-07-29 17:28:03 -0700900func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700901 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700902}
903
Colin Crossb916a382016-07-29 17:28:03 -0700904func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700905 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700906}
907
Colin Crossb916a382016-07-29 17:28:03 -0700908func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700909 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700910}
911
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700912func (library *libraryDecorator) objs() Objects {
913 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700914}
915
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700916func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
917 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700918}
919
Colin Cross26c34ed2016-09-30 17:10:16 -0700920func (library *libraryDecorator) toc() android.OptionalPath {
921 return library.tocFile
922}
923
Jiyong Parkf1194352019-02-25 11:05:47 +0900924func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
925 dir := library.baseInstaller.installDir(ctx)
926 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
927 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
928 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
929 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
930}
931
Colin Crossb916a382016-07-29 17:28:03 -0700932func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700933 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900934 if ctx.Device() && ctx.useVndk() {
935 if ctx.isVndkSp() {
936 library.baseInstaller.subDir = "vndk-sp"
937 } else if ctx.isVndk() {
Vic Yangefd249e2018-11-12 20:19:56 -0800938 if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
939 library.useCoreVariant = true
940 }
Justin Yun8fe12122017-12-07 17:18:15 +0900941 library.baseInstaller.subDir = "vndk"
942 }
Logan Chienf3511742017-10-31 18:04:35 +0800943
944 // Append a version to vndk or vndk-sp directories on the system partition.
945 if ctx.isVndk() && !ctx.isVndkExt() {
946 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
947 if vndkVersion != "current" && vndkVersion != "" {
948 library.baseInstaller.subDir += "-" + vndkVersion
949 }
Justin Yun8effde42017-06-23 19:24:43 +0900950 }
Peter Collingbourne49a25cb2019-05-21 16:06:09 -0700951 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900952 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
953 // The original path becomes a symlink to the corresponding file in the
954 // runtime APEX.
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700955 if installToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
956 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900957 library.installSymlinkToRuntimeApex(ctx, file)
958 }
Jiyong Park429660f2019-01-16 22:31:11 +0900959 library.baseInstaller.subDir = "bootstrap"
960 }
Justin Yun8effde42017-06-23 19:24:43 +0900961 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700962 library.baseInstaller.install(ctx, file)
963 }
Dan Albertf563d252017-10-13 00:29:00 -0700964
Dan Albert281f22b2017-12-13 15:03:47 -0800965 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900966 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900967 library.baseLinker.sanitize.isUnsanitizedVariant() &&
968 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700969 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700970 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700971
972 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
973 Rule: android.Cp,
974 Description: "install " + installPath.Base(),
975 Output: installPath,
976 Input: file,
977 })
978
Colin Cross0875c522017-11-28 17:34:01 -0800979 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700980 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700981}
982
Colin Crossb916a382016-07-29 17:28:03 -0700983func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800984 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700985}
986
Colin Crossa48ab5b2017-02-14 15:28:44 -0800987func (library *libraryDecorator) shared() bool {
988 return library.MutatedProperties.VariantIsShared
989}
990
991func (library *libraryDecorator) header() bool {
992 return !library.static() && !library.shared()
993}
994
995func (library *libraryDecorator) setStatic() {
996 library.MutatedProperties.VariantIsStatic = true
997 library.MutatedProperties.VariantIsShared = false
998}
999
1000func (library *libraryDecorator) setShared() {
1001 library.MutatedProperties.VariantIsStatic = false
1002 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001003}
1004
Colin Crossab3b7322016-12-09 14:46:15 -08001005func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001006 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001007}
1008
1009func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001010 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001011}
1012
Colin Cross5950f382016-12-13 12:50:57 -08001013func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001014 library.MutatedProperties.BuildShared = false
1015 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001016}
1017
Jiyong Park7ed9de32018-10-15 22:25:07 +09001018func (library *libraryDecorator) buildStubs() bool {
1019 return library.MutatedProperties.BuildStubs
1020}
1021
1022func (library *libraryDecorator) stubsVersion() string {
1023 return library.MutatedProperties.StubsVersion
1024}
1025
Colin Cross571cccf2019-02-04 11:22:08 -08001026var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1027
Jiyong Parkda732bd2018-11-02 18:23:15 +09001028func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001029 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001030 m := make(map[string]string)
1031 return &m
1032 }).(*map[string]string)
1033}
1034
1035// alphanumeric and _ characters are preserved.
1036// other characters are all converted to _
1037var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1038
1039func versioningMacroName(moduleName string) string {
1040 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1041 macroName = strings.ToUpper(moduleName)
1042 return "__" + macroName + "_API__"
1043}
1044
Colin Crossab3b7322016-12-09 14:46:15 -08001045func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001046 module := newModule(hod, android.MultilibBoth)
1047
Colin Crossb916a382016-07-29 17:28:03 -07001048 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001049 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001050 BuildShared: true,
1051 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001052 },
Colin Crossb916a382016-07-29 17:28:03 -07001053 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001054 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001055 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001056 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001057 }
1058
Colin Crossb916a382016-07-29 17:28:03 -07001059 module.compiler = library
1060 module.linker = library
1061 module.installer = library
1062
1063 return module, library
1064}
1065
Colin Cross10d22312017-05-03 11:01:58 -07001066// connects a shared library to a static library in order to reuse its .o files to avoid
1067// compiling source files twice.
1068func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1069 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1070 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001071
1072 // Check libraries in addition to cflags, since libraries may be exporting different
1073 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001074 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001075 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1076 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1077 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1078 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1079 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1080 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1081 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1082 staticCompiler.Properties.Static.System_shared_libs == nil &&
1083 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001084
1085 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1086 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1087 sharedCompiler.baseCompiler.Properties.Srcs
1088 sharedCompiler.baseCompiler.Properties.Srcs = nil
1089 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001090 } else {
1091 // This dep is just to reference static variant from shared variant
1092 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001093 }
1094 }
1095}
1096
Colin Crosse40b4ea2018-10-02 22:25:58 -07001097func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001098 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
Colin Cross33b2fb72019-05-14 14:07:01 -07001099 switch library := m.linker.(type) {
1100 case prebuiltLibraryInterface:
1101 // Always create both the static and shared variants for prebuilt libraries, and then disable the one
1102 // that is not being used. This allows them to share the name of a cc_library module, which requires that
1103 // all the variants of the cc_library also exist on the prebuilt.
1104 modules := mctx.CreateLocalVariations("static", "shared")
1105 static := modules[0].(*Module)
1106 shared := modules[1].(*Module)
1107
1108 static.linker.(prebuiltLibraryInterface).setStatic()
1109 shared.linker.(prebuiltLibraryInterface).setShared()
1110
1111 if !library.buildStatic() {
1112 static.linker.(prebuiltLibraryInterface).disablePrebuilt()
1113 }
1114 if !library.buildShared() {
1115 shared.linker.(prebuiltLibraryInterface).disablePrebuilt()
1116 }
1117
1118 case libraryInterface:
Colin Crossb916a382016-07-29 17:28:03 -07001119 if library.buildStatic() && library.buildShared() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001120 modules := mctx.CreateLocalVariations("static", "shared")
Colin Crossb916a382016-07-29 17:28:03 -07001121 static := modules[0].(*Module)
1122 shared := modules[1].(*Module)
1123
Colin Crossa48ab5b2017-02-14 15:28:44 -08001124 static.linker.(libraryInterface).setStatic()
1125 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001126
Colin Cross10d22312017-05-03 11:01:58 -07001127 reuseStaticLibrary(mctx, static, shared)
1128
Colin Crossb916a382016-07-29 17:28:03 -07001129 } else if library.buildStatic() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001130 modules := mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001131 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001132 } else if library.buildShared() {
Colin Cross33b2fb72019-05-14 14:07:01 -07001133 modules := mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001134 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001135 }
1136 }
1137 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001138}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001139
Colin Cross571cccf2019-02-04 11:22:08 -08001140var stubVersionsKey = android.NewOnceKey("stubVersions")
1141
Jiyong Park25fc6a92018-11-18 18:02:45 +09001142// maps a module name to the list of stubs versions available for the module
1143func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001144 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001145 return make(map[string][]string)
1146 }).(map[string][]string)
1147}
1148
1149var stubsVersionsLock sync.Mutex
1150
1151func latestStubsVersionFor(config android.Config, name string) string {
1152 versions, ok := stubsVersionsFor(config)[name]
1153 if ok && len(versions) > 0 {
1154 // the versions are alreay sorted in ascending order
1155 return versions[len(versions)-1]
1156 }
1157 return ""
1158}
1159
Jiyong Park7ed9de32018-10-15 22:25:07 +09001160// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001161// (which is unnamed) and zero or more stubs variants.
1162func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001163 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001164 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1165 len(library.Properties.Stubs.Versions) > 0 {
1166 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001167 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001168 if _, err := strconv.Atoi(v); err != nil {
1169 mctx.PropertyErrorf("versions", "%q is not a number", v)
1170 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001171 versions = append(versions, v)
1172 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001173 sort.Slice(versions, func(i, j int) bool {
1174 left, _ := strconv.Atoi(versions[i])
1175 right, _ := strconv.Atoi(versions[j])
1176 return left < right
1177 })
1178
1179 // save the list of versions for later use
1180 copiedVersions := make([]string, len(versions))
1181 copy(copiedVersions, versions)
1182 stubsVersionsLock.Lock()
1183 defer stubsVersionsLock.Unlock()
1184 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1185
1186 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001187 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001188
Jiyong Park7ed9de32018-10-15 22:25:07 +09001189 modules := mctx.CreateVariations(versions...)
1190 for i, m := range modules {
1191 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001192 if versions[i] != "" {
1193 l.MutatedProperties.BuildStubs = true
1194 l.MutatedProperties.StubsVersion = versions[i]
1195 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001196 m.(*Module).sanitize = nil
1197 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001198 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001199 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001200 }
1201 } else {
1202 mctx.CreateVariations("")
1203 }
1204 return
1205 }
1206 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1207 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1208 mctx.CreateVariations("")
1209 return
1210 }
1211 }
1212}