blob: 6ac0c39bce077fe06df470b974531ca05f7b6d2f [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 (
Inseob Kim69378442019-06-03 19:10:47 +090018 "fmt"
Logan Chien41eabe62019-04-10 13:33:58 +080019 "io"
Jiyong Parkf1194352019-02-25 11:05:47 +090020 "path/filepath"
Jiyong Parkda732bd2018-11-02 18:23:15 +090021 "regexp"
Jiyong Park25fc6a92018-11-18 18:02:45 +090022 "sort"
23 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090025 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026
Colin Cross26c34ed2016-09-30 17:10:16 -070027 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070030 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090031 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070032)
33
Colin Crosseefe9a32019-01-22 14:41:08 -080034type StaticSharedLibraryProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080035 Srcs []string `android:"path,arch_variant"`
36 Cflags []string `android:"path,arch_variant"`
Colin Crosseefe9a32019-01-22 14:41:08 -080037
38 Enabled *bool `android:"arch_variant"`
39 Whole_static_libs []string `android:"arch_variant"`
40 Static_libs []string `android:"arch_variant"`
41 Shared_libs []string `android:"arch_variant"`
42 System_shared_libs []string `android:"arch_variant"`
43
44 Export_shared_lib_headers []string `android:"arch_variant"`
45 Export_static_lib_headers []string `android:"arch_variant"`
46}
47
Colin Crossb916a382016-07-29 17:28:03 -070048type LibraryProperties struct {
Colin Crosseefe9a32019-01-22 14:41:08 -080049 Static StaticSharedLibraryProperties `android:"arch_variant"`
50 Shared StaticSharedLibraryProperties `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070051
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 // local file name to pass to the linker as -unexported_symbols_list
Colin Cross27b922f2019-03-04 22:35:41 -080053 Unexported_symbols_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070054 // local file name to pass to the linker as -force_symbols_not_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080055 Force_symbols_not_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070056 // local file name to pass to the linker as -force_symbols_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080057 Force_symbols_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070058
59 // rename host libraries to prevent overlap with system installed libraries
60 Unique_host_soname *bool
61
Dan Willemsene1240db2016-11-03 14:28:51 -070062 Aidl struct {
63 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080064 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070065 }
66
Colin Cross0c461f12016-10-20 16:11:43 -070067 Proto struct {
68 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080069 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070070 }
Dan Albertf563d252017-10-13 00:29:00 -070071
Inseob Kimc0907f12019-02-08 21:00:45 +090072 Sysprop struct {
73 // Whether platform owns this sysprop library.
74 Platform *bool
Inseob Kimb3f22ca2019-03-05 12:40:24 +090075 } `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +090076
Nan Zhang0007d812017-11-07 10:57:05 -080077 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090078
79 Stubs struct {
80 // Relative path to the symbol map. The symbol map provides the list of
81 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090083
84 // List versions to generate stubs libs for.
85 Versions []string
86 }
dimitryd95964a2018-11-07 13:43:34 +010087
88 // set the name of the output
89 Stem *string `android:"arch_variant"`
90
91 // Names of modules to be overridden. Listed modules can only be other shared libraries
92 // (in Make or Soong).
93 // This does not completely prevent installation of the overridden libraries, but if both
94 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
95 // from PRODUCT_PACKAGES.
96 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080097
98 // Properties for ABI compatibility checker
99 Header_abi_checker struct {
Logan Chien41eabe62019-04-10 13:33:58 +0800100 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
101 Enabled *bool
102
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800103 // Path to a symbol file that specifies the symbols to be included in the generated
104 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800106
107 // Symbol versions that should be ignored from the symbol file
108 Exclude_symbol_versions []string
109
110 // Symbol tags that should be ignored from the symbol file
111 Exclude_symbol_tags []string
112 }
Colin Crossa48ab5b2017-02-14 15:28:44 -0800113}
Colin Cross0c461f12016-10-20 16:11:43 -0700114
Colin Crossa48ab5b2017-02-14 15:28:44 -0800115type LibraryMutatedProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700116 // Build a static variant
117 BuildStatic bool `blueprint:"mutated"`
118 // Build a shared variant
119 BuildShared bool `blueprint:"mutated"`
120 // This variant is shared
121 VariantIsShared bool `blueprint:"mutated"`
122 // This variant is static
123 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900124
125 // This variant is a stubs lib
126 BuildStubs bool `blueprint:"mutated"`
127 // Version of the stubs lib
128 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700129}
130
131type FlagExporterProperties struct {
132 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700133 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700134 // against this module. Directories listed in export_include_dirs do not need to be
135 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700136 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700137
138 Target struct {
139 Vendor struct {
140 // list of exported include directories, like
141 // export_include_dirs, that will be applied to the
142 // vendor variant of this library. This will overwrite
143 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800144 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700145 }
146 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147}
148
149func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700150 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
151 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
152 android.RegisterModuleType("cc_library", LibraryFactory)
153 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
154 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
155 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156}
157
Patrice Arruda83c89e02019-03-25 15:32:39 -0700158// cc_library creates both static and/or shared libraries for a device and/or
159// host. By default, a cc_library has a single variant that targets the device.
160// Specifying `host_supported: true` also creates a library that targets the
161// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700162func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800163 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 return module.Init()
165}
166
Patrice Arruda83c89e02019-03-25 15:32:39 -0700167// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700168func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800169 module, library := NewLibrary(android.HostAndDeviceSupported)
170 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 return module.Init()
172}
173
Patrice Arruda83c89e02019-03-25 15:32:39 -0700174// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700175func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800176 module, library := NewLibrary(android.HostAndDeviceSupported)
177 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 return module.Init()
179}
180
Patrice Arruda83c89e02019-03-25 15:32:39 -0700181// cc_library_host_static creates a static library that is linkable to a host
182// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700183func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800184 module, library := NewLibrary(android.HostSupported)
185 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 return module.Init()
187}
188
Patrice Arruda83c89e02019-03-25 15:32:39 -0700189// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700190func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800191 module, library := NewLibrary(android.HostSupported)
192 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 return module.Init()
194}
195
Patrice Arruda83c89e02019-03-25 15:32:39 -0700196// cc_library_headers contains a set of c/c++ headers which are imported by
197// other soong cc modules using the header_libs property. For best practices,
198// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
199// Make.
Steven Morelandf9e62162017-11-02 17:00:50 -0700200func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800201 module, library := NewLibrary(android.HostAndDeviceSupported)
202 library.HeaderOnly()
203 return module.Init()
204}
205
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206type flagExporter struct {
207 Properties FlagExporterProperties
208
Inseob Kim69378442019-06-03 19:10:47 +0900209 dirs []string
210 systemDirs []string
211 flags []string
212 deps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213}
214
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700215func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800216 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
217 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700218 } else {
219 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
220 }
221}
222
Inseob Kim69378442019-06-03 19:10:47 +0900223func (f *flagExporter) exportIncludes(ctx ModuleContext) {
224 f.dirs = append(f.dirs, f.exportedIncludes(ctx).Strings()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225}
226
Inseob Kim69378442019-06-03 19:10:47 +0900227func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) {
228 f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx).Strings()...)
229}
230
231func (f *flagExporter) reexportDirs(dirs ...string) {
232 f.dirs = append(f.dirs, dirs...)
233}
234
235func (f *flagExporter) reexportSystemDirs(dirs ...string) {
236 f.systemDirs = append(f.systemDirs, dirs...)
237}
238
239func (f *flagExporter) reexportFlags(flags ...string) {
240 for _, flag := range flags {
241 if strings.HasPrefix(flag, "-I") || strings.HasPrefix(flag, "-isystem") {
242 panic(fmt.Errorf("Exporting invalid flag %q: "+
243 "use reexportDirs or reexportSystemDirs to export directories", flag))
244 }
245 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 f.flags = append(f.flags, flags...)
247}
248
Inseob Kim69378442019-06-03 19:10:47 +0900249func (f *flagExporter) reexportDeps(deps ...android.Path) {
250 f.deps = append(f.deps, deps...)
251}
252
253func (f *flagExporter) exportedDirs() []string {
254 return f.dirs
255}
256
257func (f *flagExporter) exportedSystemDirs() []string {
258 return f.systemDirs
Dan Willemsen847dcc72016-09-29 12:13:36 -0700259}
260
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261func (f *flagExporter) exportedFlags() []string {
262 return f.flags
263}
264
Inseob Kim69378442019-06-03 19:10:47 +0900265func (f *flagExporter) exportedDeps() android.Paths {
266 return f.deps
Dan Willemsen847dcc72016-09-29 12:13:36 -0700267}
268
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269type exportedFlagsProducer interface {
Inseob Kim69378442019-06-03 19:10:47 +0900270 exportedDirs() []string
271 exportedSystemDirs() []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 exportedFlags() []string
Inseob Kim69378442019-06-03 19:10:47 +0900273 exportedDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274}
275
276var _ exportedFlagsProducer = (*flagExporter)(nil)
277
Colin Crossb916a382016-07-29 17:28:03 -0700278// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
279// functionality: static vs. shared linkage, reusing object files for shared libraries
280type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800281 Properties LibraryProperties
282 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283
284 // For reusing static library objects for shared library
Inseob Kim69378442019-06-03 19:10:47 +0900285 reuseObjects Objects
Colin Cross10d22312017-05-03 11:01:58 -0700286
Colin Cross26c34ed2016-09-30 17:10:16 -0700287 // table-of-contents file to optimize out relinking when possible
288 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 flagExporter
291 stripper
292
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 // If we're used as a whole_static_lib, our missing dependencies need
294 // to be given
295 wholeStaticMissingDeps []string
296
297 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700298 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299
300 // Uses the module's name if empty, but can be overridden. Does not include
301 // shlib suffix.
302 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700303
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800304 sabi *sabi
305
Dan Willemsen581341d2017-02-09 16:16:31 -0800306 // Output archive of gcno coverage information files
307 coverageOutputFile android.OptionalPath
308
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800309 // linked Source Abi Dump
310 sAbiOutputFile android.OptionalPath
311
312 // Source Abi Diff
313 sAbiDiff android.OptionalPath
314
Colin Cross0875c522017-11-28 17:34:01 -0800315 // Location of the static library in the sysroot. Empty if the library is
316 // not included in the NDK.
317 ndkSysrootPath android.Path
318
Colin Crossb60190a2018-09-04 16:28:17 -0700319 // Location of the linked, unstripped library for shared libraries
320 unstrippedOutputFile android.Path
321
Dan Willemsen569edc52018-11-19 09:33:29 -0800322 // Location of the file that should be copied to dist dir when requested
323 distFile android.OptionalPath
324
Jiyong Park7ed9de32018-10-15 22:25:07 +0900325 versionScriptPath android.ModuleGenPath
326
Jiyong Parkf1194352019-02-25 11:05:47 +0900327 post_install_cmds []string
328
Vic Yangefd249e2018-11-12 20:19:56 -0800329 // If useCoreVariant is true, the vendor variant of a VNDK library is
330 // not installed.
331 useCoreVariant bool
332
Colin Crossb916a382016-07-29 17:28:03 -0700333 // Decorated interafaces
334 *baseCompiler
335 *baseLinker
336 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337}
338
Colin Crossb916a382016-07-29 17:28:03 -0700339func (library *libraryDecorator) linkerProps() []interface{} {
340 var props []interface{}
341 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 return append(props,
343 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800344 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700346 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700347}
348
Colin Crossb916a382016-07-29 17:28:03 -0700349func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700350 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351
Colin Crossb916a382016-07-29 17:28:03 -0700352 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
353 // all code is position independent, and then those warnings get promoted to
354 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700355 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700356 flags.CFlags = append(flags.CFlags, "-fPIC")
357 }
358
359 if library.static() {
360 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800361 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700362 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
363 }
364
Colin Crossa48ab5b2017-02-14 15:28:44 -0800365 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700368 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 f = append(f,
370 "-nostdlib",
371 "-Wl,--gc-sections",
372 )
373 }
374
375 if ctx.Darwin() {
376 f = append(f,
377 "-dynamiclib",
378 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
380 )
Colin Cross7863cf52016-10-20 10:47:21 -0700381 if ctx.Arch().ArchType == android.X86 {
382 f = append(f,
383 "-read_only_relocs suppress",
384 )
385 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386 } else {
Josh Gao75a50a22019-06-07 17:58:59 -0700387 f = append(f, "-shared")
388 if !ctx.Windows() {
389 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
390 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700391 }
392
393 flags.LdFlags = append(f, flags.LdFlags...)
394 }
395
396 return flags
397}
398
Colin Crossf18e1102017-11-16 14:33:08 -0800399func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700400 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700401 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700402 f := includeDirsToFlags(exportIncludeDirs)
403 flags.GlobalFlags = append(flags.GlobalFlags, f)
404 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700405 }
406
Jiyong Park7ed9de32018-10-15 22:25:07 +0900407 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
408 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900409 // Remove -include <file> when compiling stubs. Otherwise, the force included
410 // headers might cause conflicting types error with the symbols in the
411 // generated stubs source code. e.g.
412 // double acos(double); // in header
413 // void acos() {} // in the generated source code
414 removeInclude := func(flags []string) []string {
415 ret := flags[:0]
416 for _, f := range flags {
417 if strings.HasPrefix(f, "-include ") {
418 continue
419 }
420 ret = append(ret, f)
421 }
422 return ret
423 }
424 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
425 flags.CFlags = removeInclude(flags.CFlags)
426
Jiyong Park7ed9de32018-10-15 22:25:07 +0900427 flags = addStubLibraryCompilerFlags(flags)
428 }
429 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700430}
431
Logan Chien41eabe62019-04-10 13:33:58 +0800432func (library *libraryDecorator) shouldCreateVndkSourceAbiDump(ctx ModuleContext) bool {
433 if library.Properties.Header_abi_checker.Enabled != nil {
434 return Bool(library.Properties.Header_abi_checker.Enabled)
435 }
Inseob Kim9516ee92019-05-09 10:56:13 +0900436 return ctx.shouldCreateVndkSourceAbiDump(ctx.Config())
Logan Chien41eabe62019-04-10 13:33:58 +0800437}
438
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700439func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900440 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900441 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900442 library.versionScriptPath = versionScript
443 return objs
444 }
445
Colin Cross5950f382016-12-13 12:50:57 -0800446 if !library.buildShared() && !library.buildStatic() {
447 if len(library.baseCompiler.Properties.Srcs) > 0 {
448 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
449 }
450 if len(library.Properties.Static.Srcs) > 0 {
451 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
452 }
453 if len(library.Properties.Shared.Srcs) > 0 {
454 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
455 }
456 return Objects{}
457 }
Logan Chien41eabe62019-04-10 13:33:58 +0800458 if library.shouldCreateVndkSourceAbiDump(ctx) || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700459 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800460 var SourceAbiFlags []string
461 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700462 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800463 }
Inseob Kim69378442019-06-03 19:10:47 +0900464 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
465 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700466 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800467 flags.SAbiFlags = SourceAbiFlags
468 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
469 len(library.Properties.Static.Srcs)
470 if total_length > 0 {
471 flags.SAbiDump = true
472 }
473 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700474 objs := library.baseCompiler.compile(ctx, flags, deps)
475 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700476 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700477
Colin Cross4d9c2d12016-07-29 12:48:20 -0700478 if library.static() {
Colin Cross8a497952019-03-05 22:25:09 -0800479 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700480 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800481 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800482 } else if library.shared() {
Colin Cross8a497952019-03-05 22:25:09 -0800483 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700484 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800485 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700486 }
487
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700488 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700489}
490
491type libraryInterface interface {
492 getWholeStaticMissingDeps() []string
493 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700494 objs() Objects
Inseob Kim69378442019-06-03 19:10:47 +0900495 reuseObjs() (Objects, exportedFlagsProducer)
Colin Cross26c34ed2016-09-30 17:10:16 -0700496 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700497
498 // Returns true if the build options for the module have selected a static or shared build
499 buildStatic() bool
500 buildShared() bool
501
502 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800503 setStatic()
504 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800505
506 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
507 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Colin Crossb916a382016-07-29 17:28:03 -0700508}
509
510func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
511 name := library.libName
512 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100513 name = String(library.Properties.Stem)
514 if name == "" {
515 name = ctx.baseModuleName()
516 }
Colin Crossb916a382016-07-29 17:28:03 -0700517 }
518
Logan Chienf3511742017-10-31 18:04:35 +0800519 if ctx.isVndkExt() {
520 name = ctx.getVndkExtendsModuleName()
521 }
522
Colin Crossb916a382016-07-29 17:28:03 -0700523 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
524 if !strings.HasSuffix(name, "-host") {
525 name = name + "-host"
526 }
527 }
528
Inseob Kim0ce291e2019-07-04 14:38:27 +0900529 return name
Colin Crossb916a382016-07-29 17:28:03 -0700530}
531
Jiyong Parkda732bd2018-11-02 18:23:15 +0900532var versioningMacroNamesListMutex sync.Mutex
533
Colin Crossb916a382016-07-29 17:28:03 -0700534func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
535 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700536 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700537 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700538 }
539 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700540 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900541 // Let baseLinker know whether this variant is for stubs or not, so that
542 // it can omit things that are not required for linking stubs.
543 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900544
545 if library.buildStubs() {
546 macroNames := versioningMacroNamesList(ctx.Config())
547 myName := versioningMacroName(ctx.ModuleName())
548 versioningMacroNamesListMutex.Lock()
549 defer versioningMacroNamesListMutex.Unlock()
550 if (*macroNames)[myName] == "" {
551 (*macroNames)[myName] = ctx.ModuleName()
552 } else if (*macroNames)[myName] != ctx.ModuleName() {
553 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
554 }
555 }
Colin Crossb916a382016-07-29 17:28:03 -0700556}
557
dimitry0345ad82018-12-05 16:28:14 +0100558func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
559 deps = library.baseCompiler.compilerDeps(ctx, deps)
560
dimitry0345ad82018-12-05 16:28:14 +0100561 return deps
562}
563
Colin Cross37047f12016-12-13 17:06:13 -0800564func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800565 if library.static() {
566 if library.Properties.Static.System_shared_libs != nil {
567 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
568 }
569 } else if library.shared() {
570 if library.Properties.Shared.System_shared_libs != nil {
571 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
572 }
573 }
574
Colin Crossb916a382016-07-29 17:28:03 -0700575 deps = library.baseLinker.linkerDeps(ctx, deps)
576
577 if library.static() {
578 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
579 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700580 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
581 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800582
583 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
584 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800585 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800586 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700587 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588 deps.CrtBegin = "crtbegin_so"
589 deps.CrtEnd = "crtend_so"
590 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800591 // TODO(danalbert): Add generation of crt objects.
592 // For `sdk_version: "current"`, we don't actually have a
593 // freshly generated set of CRT objects. Use the last stable
594 // version.
595 version := ctx.sdkVersion()
596 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700597 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800598 }
599 deps.CrtBegin = "ndk_crtbegin_so." + version
600 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700601 }
602 }
603 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
604 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
605 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800606
607 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
608 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700609 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900610 if ctx.useVndk() {
611 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
612 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
613 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000614 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
615 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900616 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900617 if ctx.inRecovery() {
618 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
619 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
620 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000621 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
622 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900623 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800624
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625 return deps
626}
627
Colin Crossb916a382016-07-29 17:28:03 -0700628func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700629 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700630
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700631 library.objects = deps.WholeStaticLibObjs.Copy()
632 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633
Inseob Kim0ce291e2019-07-04 14:38:27 +0900634 fileName := ctx.ModuleName() + staticLibraryExtension
Colin Cross86803cf2018-02-15 14:12:26 -0800635 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800636 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700637
Dan Willemsen569edc52018-11-19 09:33:29 -0800638 if Bool(library.baseLinker.Properties.Use_version_lib) {
639 if ctx.Host() {
640 versionedOutputFile := outputFile
641 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
642 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
643 } else {
644 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
645 library.distFile = android.OptionalPathForPath(versionedOutputFile)
646 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
647 }
Colin Cross86803cf2018-02-15 14:12:26 -0800648 }
649
Dan Willemsen581341d2017-02-09 16:16:31 -0800650 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
651
Inseob Kim0ce291e2019-07-04 14:38:27 +0900652 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700653
654 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
655
656 ctx.CheckbuildFile(outputFile)
657
658 return outputFile
659}
660
Colin Crossb916a382016-07-29 17:28:03 -0700661func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700662 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700663
664 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700665 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666
Colin Cross2383f3b2018-02-06 14:40:13 -0800667 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
668 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
669 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700670 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700671 if unexportedSymbols.Valid() {
672 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
673 }
674 if forceNotWeakSymbols.Valid() {
675 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
676 }
677 if forceWeakSymbols.Valid() {
678 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
679 }
680 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700681 if unexportedSymbols.Valid() {
682 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
683 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
684 }
685 if forceNotWeakSymbols.Valid() {
686 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
687 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
688 }
689 if forceWeakSymbols.Valid() {
690 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
691 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
692 }
693 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900694 if library.buildStubs() {
695 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
696 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
697 linkerDeps = append(linkerDeps, library.versionScriptPath)
698 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699
700 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
701 outputFile := android.PathForModuleOut(ctx, fileName)
702 ret := outputFile
703
Josh Gao75a50a22019-06-07 17:58:59 -0700704 var implicitOutputs android.WritablePaths
705 if ctx.Windows() {
706 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
707
708 flags.LdFlags = append(flags.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
709 implicitOutputs = append(implicitOutputs, importLibraryPath)
710 }
711
Colin Cross4d9c2d12016-07-29 12:48:20 -0700712 builderFlags := flagsToBuilderFlags(flags)
713
Colin Crossb496cfd2018-09-10 16:50:05 -0700714 // Optimize out relinking against shared libraries whose interface hasn't changed by
715 // depending on a table of contents file instead of the library itself.
716 tocPath := outputFile.RelPathString()
717 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
718 tocFile := android.PathForOutput(ctx, tocPath)
719 library.tocFile = android.OptionalPathForPath(tocFile)
720 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700721
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800723 if ctx.Darwin() {
724 builderFlags.stripUseGnuStrip = true
725 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700726 strippedOutputFile := outputFile
727 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700728 library.stripper.stripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, builderFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700729 }
730
Colin Crossb60190a2018-09-04 16:28:17 -0700731 library.unstrippedOutputFile = outputFile
732
Dan Willemsen569edc52018-11-19 09:33:29 -0800733 if Bool(library.baseLinker.Properties.Use_version_lib) {
734 if ctx.Host() {
735 versionedOutputFile := outputFile
736 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
737 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
738 } else {
739 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
740 library.distFile = android.OptionalPathForPath(versionedOutputFile)
741
742 if library.stripper.needsStrip(ctx) {
743 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
744 library.distFile = android.OptionalPathForPath(out)
Ryan Prichardf979d732019-05-30 20:53:29 -0700745 library.stripper.stripExecutableOrSharedLib(ctx, versionedOutputFile, out, builderFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800746 }
747
748 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
749 }
Colin Cross86803cf2018-02-15 14:12:26 -0800750 }
751
Jiyong Park64a44f22019-01-18 14:37:08 +0900752 sharedLibs := deps.EarlySharedLibs
753 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700754 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
755
Jiyong Park64a44f22019-01-18 14:37:08 +0900756 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700757 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
758 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700759 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700760
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700761 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700762 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Josh Gao75a50a22019-06-07 17:58:59 -0700763 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700764
Dan Willemsen581341d2017-02-09 16:16:31 -0800765 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
766 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800767
768 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
769 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
770
Oliver Nguyenc7434142019-04-24 14:22:25 -0700771 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700772 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800773
Colin Cross4d9c2d12016-07-29 12:48:20 -0700774 return ret
775}
776
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900777func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
778 return library.unstrippedOutputFile
779}
780
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700781func (library *libraryDecorator) nativeCoverage() bool {
782 if library.header() || library.buildStubs() {
783 return false
784 }
785 return true
786}
787
Logan Chien7eefdc42018-07-11 18:10:41 +0800788func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Inseob Kim9516ee92019-05-09 10:56:13 +0900789 isLlndkOrNdk := inList(ctx.baseModuleName(), *llndkLibraries(ctx.Config())) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800790
Logan Chien41eabe62019-04-10 13:33:58 +0800791 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), false)
792 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndkOrNdk, ctx.isVndk(), true)
Logan Chien7eefdc42018-07-11 18:10:41 +0800793
794 if refAbiDumpTextFile.Valid() {
795 if refAbiDumpGzipFile.Valid() {
796 ctx.ModuleErrorf(
797 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
798 refAbiDumpTextFile, refAbiDumpGzipFile)
799 return nil
800 }
801 return refAbiDumpTextFile.Path()
802 }
803 if refAbiDumpGzipFile.Valid() {
804 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
805 }
806 return nil
807}
808
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700809func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Hsin-Yi Chenf6a95462019-06-25 14:46:52 +0800810 if library.shouldCreateVndkSourceAbiDump(ctx) {
Logan Chiena8f51582018-03-21 11:53:48 +0800811 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
812 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800813 vndkVersion = ver
814 }
815
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700816 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800817 var SourceAbiFlags []string
818 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700819 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
820 }
Inseob Kim69378442019-06-03 19:10:47 +0900821 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
822 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800823 }
824 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800825 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
826 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
827 library.Properties.Header_abi_checker.Exclude_symbol_versions,
828 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800829
Logan Chien7eefdc42018-07-11 18:10:41 +0800830 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
831 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800832 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Inseob Kim9516ee92019-05-09 10:56:13 +0900833 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(ctx.Config()), ctx.isNdk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800834 }
835 }
836}
837
Colin Crossb916a382016-07-29 17:28:03 -0700838func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700839 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700840
Colin Crossad59e752017-11-16 14:29:11 -0800841 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700842 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800843 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700844 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700845 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700846 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700847 }
848
Inseob Kim69378442019-06-03 19:10:47 +0900849 library.exportIncludes(ctx)
850 library.reexportDirs(deps.ReexportedDirs...)
851 library.reexportSystemDirs(deps.ReexportedSystemDirs...)
852 library.reexportFlags(deps.ReexportedFlags...)
853 library.reexportDeps(deps.ReexportedDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700854
Nan Zhang0007d812017-11-07 10:57:05 -0800855 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700856 if library.baseCompiler.hasSrcExt(".aidl") {
Inseob Kim69378442019-06-03 19:10:47 +0900857 dir := android.PathForModuleGen(ctx, "aidl").String()
858 library.reexportDirs(dir)
859 library.reexportDeps(library.baseCompiler.pathDeps...) // TODO: restrict to aidl deps
Dan Willemsene1240db2016-11-03 14:28:51 -0700860 }
861 }
862
Nan Zhang0007d812017-11-07 10:57:05 -0800863 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700864 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800865 includes := []string{}
Colin Cross19878da2019-03-28 14:45:07 -0700866 if flags.proto.CanonicalPathFromRoot {
Inseob Kim69378442019-06-03 19:10:47 +0900867 includes = append(includes, flags.proto.SubDir.String())
Colin Cross10d22312017-05-03 11:01:58 -0700868 }
Inseob Kim69378442019-06-03 19:10:47 +0900869 includes = append(includes, flags.proto.Dir.String())
870 library.reexportDirs(includes...)
871 library.reexportDeps(library.baseCompiler.pathDeps...) // TODO: restrict to proto deps
Colin Cross0c461f12016-10-20 16:11:43 -0700872 }
873 }
874
Inseob Kim21f26902018-09-06 00:55:20 +0900875 if library.baseCompiler.hasSrcExt(".sysprop") {
Inseob Kim69378442019-06-03 19:10:47 +0900876 dir := android.PathForModuleGen(ctx, "sysprop", "include").String()
Inseob Kimc0907f12019-02-08 21:00:45 +0900877 if library.Properties.Sysprop.Platform != nil {
878 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
879 isVendor := ctx.useVndk()
880 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
881
Inseob Kim5cefbd22019-06-08 20:36:59 +0900882 usePublic := isProduct || (isOwnerPlatform == isVendor)
Inseob Kimc0907f12019-02-08 21:00:45 +0900883
Inseob Kim5cefbd22019-06-08 20:36:59 +0900884 if usePublic {
885 dir = android.PathForModuleGen(ctx, "sysprop/public", "include").String()
Inseob Kimc0907f12019-02-08 21:00:45 +0900886 }
887 }
888
Inseob Kim69378442019-06-03 19:10:47 +0900889 library.reexportDirs(dir)
890 library.reexportDeps(library.baseCompiler.pathDeps...)
Inseob Kim21f26902018-09-06 00:55:20 +0900891 }
892
Jiyong Parkda732bd2018-11-02 18:23:15 +0900893 if library.buildStubs() {
Inseob Kim69378442019-06-03 19:10:47 +0900894 library.reexportFlags("-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion())
Jiyong Parkda732bd2018-11-02 18:23:15 +0900895 }
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
Inseob Kim69378442019-06-03 19:10:47 +0900916func (library *libraryDecorator) reuseObjs() (Objects, exportedFlagsProducer) {
917 return library.reuseObjects, &library.flagExporter
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}