blob: 60b00b1936e39d4e0f1abc2ad83bc2c9012ba96e [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
Paul Duffin13879572019-11-28 14:31:38 +000027 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070028 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070029
Colin Cross4d9c2d12016-07-29 12:48:20 -070030 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070031 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090032 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070033)
34
Colin Crossb916a382016-07-29 17:28:03 -070035type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070036 // local file name to pass to the linker as -unexported_symbols_list
Colin Cross27b922f2019-03-04 22:35:41 -080037 Unexported_symbols_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070038 // local file name to pass to the linker as -force_symbols_not_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080039 Force_symbols_not_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 // local file name to pass to the linker as -force_symbols_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080041 Force_symbols_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070042
43 // rename host libraries to prevent overlap with system installed libraries
44 Unique_host_soname *bool
45
Dan Willemsene1240db2016-11-03 14:28:51 -070046 Aidl struct {
47 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080048 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070049 }
50
Colin Cross0c461f12016-10-20 16:11:43 -070051 Proto struct {
52 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080053 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070054 }
Dan Albertf563d252017-10-13 00:29:00 -070055
Inseob Kimc0907f12019-02-08 21:00:45 +090056 Sysprop struct {
57 // Whether platform owns this sysprop library.
58 Platform *bool
Inseob Kimb3f22ca2019-03-05 12:40:24 +090059 } `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +090060
Nan Zhang0007d812017-11-07 10:57:05 -080061 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090062
63 Stubs struct {
64 // Relative path to the symbol map. The symbol map provides the list of
65 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080066 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090067
68 // List versions to generate stubs libs for.
69 Versions []string
70 }
dimitryd95964a2018-11-07 13:43:34 +010071
72 // set the name of the output
73 Stem *string `android:"arch_variant"`
74
Colin Cross0fd6a412019-08-16 14:22:10 -070075 // set suffix of the name of the output
76 Suffix *string `android:"arch_variant"`
77
78 Target struct {
79 Vendor struct {
80 // set suffix of the name of the output
81 Suffix *string `android:"arch_variant"`
82 }
83 }
84
dimitryd95964a2018-11-07 13:43:34 +010085 // Names of modules to be overridden. Listed modules can only be other shared libraries
86 // (in Make or Soong).
87 // This does not completely prevent installation of the overridden libraries, but if both
88 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
89 // from PRODUCT_PACKAGES.
90 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080091
92 // Properties for ABI compatibility checker
93 Header_abi_checker struct {
Logan Chien41eabe62019-04-10 13:33:58 +080094 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
95 Enabled *bool
96
Logan Chiene3d7a0d2019-01-17 00:18:02 +080097 // Path to a symbol file that specifies the symbols to be included in the generated
98 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -080099 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800100
101 // Symbol versions that should be ignored from the symbol file
102 Exclude_symbol_versions []string
103
104 // Symbol tags that should be ignored from the symbol file
105 Exclude_symbol_tags []string
106 }
Vic Yang6cd1be82019-06-24 16:08:48 -0700107
108 // Order symbols in .bss section by their sizes. Only useful for shared libraries.
109 Sort_bss_symbols_by_size *bool
Pete Bentley803e1612019-08-06 22:19:59 +0100110
111 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
112 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Crossa48ab5b2017-02-14 15:28:44 -0800113}
Colin Cross0c461f12016-10-20 16:11:43 -0700114
Colin Crosse1bb5d02019-09-24 14:55:04 -0700115type StaticProperties struct {
116 Static StaticOrSharedProperties `android:"arch_variant"`
117}
118
119type SharedProperties struct {
120 Shared StaticOrSharedProperties `android:"arch_variant"`
121}
122
123type StaticOrSharedProperties struct {
124 Srcs []string `android:"path,arch_variant"`
125 Cflags []string `android:"arch_variant"`
126
127 Enabled *bool `android:"arch_variant"`
128 Whole_static_libs []string `android:"arch_variant"`
129 Static_libs []string `android:"arch_variant"`
130 Shared_libs []string `android:"arch_variant"`
131 System_shared_libs []string `android:"arch_variant"`
132
133 Export_shared_lib_headers []string `android:"arch_variant"`
134 Export_static_lib_headers []string `android:"arch_variant"`
Jiyong Parka90ca002019-10-07 15:47:24 +0900135
136 Apex_available []string `android:"arch_variant"`
Colin Crosse1bb5d02019-09-24 14:55:04 -0700137}
138
Colin Crossa48ab5b2017-02-14 15:28:44 -0800139type LibraryMutatedProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700140 // Build a static variant
141 BuildStatic bool `blueprint:"mutated"`
142 // Build a shared variant
143 BuildShared bool `blueprint:"mutated"`
144 // This variant is shared
145 VariantIsShared bool `blueprint:"mutated"`
146 // This variant is static
147 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900148
149 // This variant is a stubs lib
150 BuildStubs bool `blueprint:"mutated"`
151 // Version of the stubs lib
152 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700153}
154
155type FlagExporterProperties struct {
156 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700157 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700158 // against this module. Directories listed in export_include_dirs do not need to be
159 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700160 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700161
Jiyong Park73c54ee2019-10-22 20:31:18 +0900162 // list of directories that will be added to the system include path
163 // using -isystem for this module and any module that links against this module.
164 Export_system_include_dirs []string `android:"arch_variant"`
165
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700166 Target struct {
167 Vendor struct {
168 // list of exported include directories, like
169 // export_include_dirs, that will be applied to the
170 // vendor variant of this library. This will overwrite
171 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800172 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700173 }
174 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175}
176
177func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700178 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
179 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
180 android.RegisterModuleType("cc_library", LibraryFactory)
181 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
182 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
183 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184}
185
Patrice Arruda83c89e02019-03-25 15:32:39 -0700186// cc_library creates both static and/or shared libraries for a device and/or
187// host. By default, a cc_library has a single variant that targets the device.
188// Specifying `host_supported: true` also creates a library that targets the
189// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700190func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800191 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 return module.Init()
193}
194
Patrice Arruda83c89e02019-03-25 15:32:39 -0700195// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700196func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800197 module, library := NewLibrary(android.HostAndDeviceSupported)
198 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 return module.Init()
200}
201
Patrice Arruda83c89e02019-03-25 15:32:39 -0700202// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700203func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800204 module, library := NewLibrary(android.HostAndDeviceSupported)
205 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 return module.Init()
207}
208
Patrice Arruda83c89e02019-03-25 15:32:39 -0700209// cc_library_host_static creates a static library that is linkable to a host
210// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700211func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800212 module, library := NewLibrary(android.HostSupported)
213 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 return module.Init()
215}
216
Patrice Arruda83c89e02019-03-25 15:32:39 -0700217// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700218func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800219 module, library := NewLibrary(android.HostSupported)
220 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 return module.Init()
222}
223
Patrice Arruda83c89e02019-03-25 15:32:39 -0700224// cc_library_headers contains a set of c/c++ headers which are imported by
225// other soong cc modules using the header_libs property. For best practices,
226// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
227// Make.
Steven Morelandf9e62162017-11-02 17:00:50 -0700228func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800229 module, library := NewLibrary(android.HostAndDeviceSupported)
230 library.HeaderOnly()
231 return module.Init()
232}
233
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234type flagExporter struct {
235 Properties FlagExporterProperties
236
Jiyong Park74955042019-10-22 20:19:51 +0900237 dirs android.Paths
238 systemDirs android.Paths
Inseob Kim69378442019-06-03 19:10:47 +0900239 flags []string
240 deps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241}
242
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700243func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800244 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
245 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700246 } else {
247 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
248 }
249}
250
Inseob Kim69378442019-06-03 19:10:47 +0900251func (f *flagExporter) exportIncludes(ctx ModuleContext) {
Jiyong Park74955042019-10-22 20:19:51 +0900252 f.dirs = append(f.dirs, f.exportedIncludes(ctx)...)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900253 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254}
255
Inseob Kim69378442019-06-03 19:10:47 +0900256func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900257 // all dirs are force exported as system
Jiyong Park74955042019-10-22 20:19:51 +0900258 f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx)...)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900259 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
Inseob Kim69378442019-06-03 19:10:47 +0900260}
261
Jiyong Park74955042019-10-22 20:19:51 +0900262func (f *flagExporter) reexportDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900263 f.dirs = append(f.dirs, dirs...)
264}
265
Jiyong Park74955042019-10-22 20:19:51 +0900266func (f *flagExporter) reexportSystemDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900267 f.systemDirs = append(f.systemDirs, dirs...)
268}
269
270func (f *flagExporter) reexportFlags(flags ...string) {
271 for _, flag := range flags {
272 if strings.HasPrefix(flag, "-I") || strings.HasPrefix(flag, "-isystem") {
273 panic(fmt.Errorf("Exporting invalid flag %q: "+
274 "use reexportDirs or reexportSystemDirs to export directories", flag))
275 }
276 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 f.flags = append(f.flags, flags...)
278}
279
Inseob Kim69378442019-06-03 19:10:47 +0900280func (f *flagExporter) reexportDeps(deps ...android.Path) {
281 f.deps = append(f.deps, deps...)
282}
283
Jiyong Park74955042019-10-22 20:19:51 +0900284func (f *flagExporter) exportedDirs() android.Paths {
Inseob Kim69378442019-06-03 19:10:47 +0900285 return f.dirs
286}
287
Jiyong Park74955042019-10-22 20:19:51 +0900288func (f *flagExporter) exportedSystemDirs() android.Paths {
Inseob Kim69378442019-06-03 19:10:47 +0900289 return f.systemDirs
Dan Willemsen847dcc72016-09-29 12:13:36 -0700290}
291
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292func (f *flagExporter) exportedFlags() []string {
293 return f.flags
294}
295
Inseob Kim69378442019-06-03 19:10:47 +0900296func (f *flagExporter) exportedDeps() android.Paths {
297 return f.deps
Dan Willemsen847dcc72016-09-29 12:13:36 -0700298}
299
Colin Cross4d9c2d12016-07-29 12:48:20 -0700300type exportedFlagsProducer interface {
Jiyong Park74955042019-10-22 20:19:51 +0900301 exportedDirs() android.Paths
302 exportedSystemDirs() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 exportedFlags() []string
Inseob Kim69378442019-06-03 19:10:47 +0900304 exportedDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305}
306
307var _ exportedFlagsProducer = (*flagExporter)(nil)
308
Colin Crossb916a382016-07-29 17:28:03 -0700309// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
310// functionality: static vs. shared linkage, reusing object files for shared libraries
311type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800312 Properties LibraryProperties
Colin Crosse1bb5d02019-09-24 14:55:04 -0700313 StaticProperties StaticProperties
314 SharedProperties SharedProperties
Colin Crossa48ab5b2017-02-14 15:28:44 -0800315 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316
317 // For reusing static library objects for shared library
Inseob Kim69378442019-06-03 19:10:47 +0900318 reuseObjects Objects
Colin Cross10d22312017-05-03 11:01:58 -0700319
Colin Cross26c34ed2016-09-30 17:10:16 -0700320 // table-of-contents file to optimize out relinking when possible
321 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 flagExporter
324 stripper
325
Colin Cross4d9c2d12016-07-29 12:48:20 -0700326 // If we're used as a whole_static_lib, our missing dependencies need
327 // to be given
328 wholeStaticMissingDeps []string
329
330 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700331 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332
333 // Uses the module's name if empty, but can be overridden. Does not include
334 // shlib suffix.
335 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700336
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800337 sabi *sabi
338
Dan Willemsen581341d2017-02-09 16:16:31 -0800339 // Output archive of gcno coverage information files
340 coverageOutputFile android.OptionalPath
341
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800342 // linked Source Abi Dump
343 sAbiOutputFile android.OptionalPath
344
345 // Source Abi Diff
346 sAbiDiff android.OptionalPath
347
Colin Cross0875c522017-11-28 17:34:01 -0800348 // Location of the static library in the sysroot. Empty if the library is
349 // not included in the NDK.
350 ndkSysrootPath android.Path
351
Colin Crossb60190a2018-09-04 16:28:17 -0700352 // Location of the linked, unstripped library for shared libraries
353 unstrippedOutputFile android.Path
354
Dan Willemsen569edc52018-11-19 09:33:29 -0800355 // Location of the file that should be copied to dist dir when requested
356 distFile android.OptionalPath
357
Jiyong Park7ed9de32018-10-15 22:25:07 +0900358 versionScriptPath android.ModuleGenPath
359
Jiyong Parkf1194352019-02-25 11:05:47 +0900360 post_install_cmds []string
361
Vic Yangefd249e2018-11-12 20:19:56 -0800362 // If useCoreVariant is true, the vendor variant of a VNDK library is
363 // not installed.
364 useCoreVariant bool
365
Colin Crossb916a382016-07-29 17:28:03 -0700366 // Decorated interafaces
367 *baseCompiler
368 *baseLinker
369 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370}
371
Colin Crossb916a382016-07-29 17:28:03 -0700372func (library *libraryDecorator) linkerProps() []interface{} {
373 var props []interface{}
374 props = append(props, library.baseLinker.linkerProps()...)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700375 props = append(props,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800377 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700379 &library.stripper.StripProperties)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700380
381 if library.MutatedProperties.BuildShared {
382 props = append(props, &library.SharedProperties)
383 }
384 if library.MutatedProperties.BuildStatic {
385 props = append(props, &library.StaticProperties)
386 }
387
388 return props
Colin Cross4d9c2d12016-07-29 12:48:20 -0700389}
390
Colin Crossb916a382016-07-29 17:28:03 -0700391func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700392 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393
Colin Crossb916a382016-07-29 17:28:03 -0700394 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
395 // all code is position independent, and then those warnings get promoted to
396 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700397 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800398 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIC")
Colin Crossb916a382016-07-29 17:28:03 -0700399 }
400
401 if library.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800402 flags.Local.CFlags = append(flags.Local.CFlags, library.StaticProperties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800403 } else if library.shared() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800404 flags.Local.CFlags = append(flags.Local.CFlags, library.SharedProperties.Shared.Cflags...)
Colin Crossb916a382016-07-29 17:28:03 -0700405 }
406
Colin Crossa48ab5b2017-02-14 15:28:44 -0800407 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700408 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700409 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700410 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700411 f = append(f,
412 "-nostdlib",
413 "-Wl,--gc-sections",
414 )
415 }
416
417 if ctx.Darwin() {
418 f = append(f,
419 "-dynamiclib",
420 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700421 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
422 )
Colin Cross7863cf52016-10-20 10:47:21 -0700423 if ctx.Arch().ArchType == android.X86 {
424 f = append(f,
425 "-read_only_relocs suppress",
426 )
427 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428 } else {
Josh Gao75a50a22019-06-07 17:58:59 -0700429 f = append(f, "-shared")
430 if !ctx.Windows() {
431 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
432 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700433 }
434
Colin Cross4af21ed2019-11-04 09:37:55 -0800435 flags.Global.LdFlags = append(flags.Global.LdFlags, f...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700436 }
437
438 return flags
439}
440
Colin Crossf18e1102017-11-16 14:33:08 -0800441func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700442 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700443 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700444 f := includeDirsToFlags(exportIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800445 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
446 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700447 }
448
Jiyong Park7ed9de32018-10-15 22:25:07 +0900449 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
450 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900451 // Remove -include <file> when compiling stubs. Otherwise, the force included
452 // headers might cause conflicting types error with the symbols in the
453 // generated stubs source code. e.g.
454 // double acos(double); // in header
455 // void acos() {} // in the generated source code
456 removeInclude := func(flags []string) []string {
457 ret := flags[:0]
458 for _, f := range flags {
459 if strings.HasPrefix(f, "-include ") {
460 continue
461 }
462 ret = append(ret, f)
463 }
464 return ret
465 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800466 flags.Local.CommonFlags = removeInclude(flags.Local.CommonFlags)
467 flags.Local.CFlags = removeInclude(flags.Local.CFlags)
Jiyong Park64379952018-12-13 18:37:29 +0900468
Jiyong Park7ed9de32018-10-15 22:25:07 +0900469 flags = addStubLibraryCompilerFlags(flags)
470 }
471 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700472}
473
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800474// Returns a string that represents the class of the ABI dump.
475// Returns an empty string if ABI check is disabled for this library.
476func (library *libraryDecorator) classifySourceAbiDump(ctx ModuleContext) string {
477 enabled := library.Properties.Header_abi_checker.Enabled
478 if enabled != nil && !Bool(enabled) {
479 return ""
480 }
481 // Return NDK if the library is both NDK and LLNDK.
482 if ctx.isNdk() {
483 return "NDK"
484 }
485 if ctx.isLlndkPublic(ctx.Config()) {
486 return "LLNDK"
487 }
488 if ctx.useVndk() && ctx.isVndk() && !ctx.isVndkPrivate(ctx.Config()) {
489 if ctx.isVndkSp() {
490 if ctx.isVndkExt() {
491 return "VNDK-SP-ext"
492 } else {
493 return "VNDK-SP"
494 }
495 } else {
496 if ctx.isVndkExt() {
497 return "VNDK-ext"
498 } else {
499 return "VNDK-core"
500 }
501 }
502 }
Jiyong Parka034b832019-08-27 14:02:19 +0900503 if Bool(enabled) || ctx.hasStubsVariants() {
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800504 return "PLATFORM"
505 }
506 return ""
507}
508
Hsin-Yi Chenaf17d742019-07-29 17:46:59 +0800509func (library *libraryDecorator) shouldCreateSourceAbiDump(ctx ModuleContext) bool {
510 if !ctx.shouldCreateSourceAbiDump() {
511 return false
512 }
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800513 return library.classifySourceAbiDump(ctx) != ""
Logan Chien41eabe62019-04-10 13:33:58 +0800514}
515
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700516func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900517 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900518 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900519 library.versionScriptPath = versionScript
520 return objs
521 }
522
Colin Cross5950f382016-12-13 12:50:57 -0800523 if !library.buildShared() && !library.buildStatic() {
524 if len(library.baseCompiler.Properties.Srcs) > 0 {
525 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
526 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700527 if len(library.StaticProperties.Static.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800528 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
529 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700530 if len(library.SharedProperties.Shared.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800531 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
532 }
533 return Objects{}
534 }
Hsin-Yi Chenaf17d742019-07-29 17:46:59 +0800535 if library.shouldCreateSourceAbiDump(ctx) || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700536 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800537 var SourceAbiFlags []string
538 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700539 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800540 }
Inseob Kim69378442019-06-03 19:10:47 +0900541 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
542 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700543 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800544 flags.SAbiFlags = SourceAbiFlags
Colin Crosse1bb5d02019-09-24 14:55:04 -0700545 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) +
546 len(library.SharedProperties.Shared.Srcs) + len(library.StaticProperties.Static.Srcs)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800547 if total_length > 0 {
548 flags.SAbiDump = true
549 }
550 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700551 objs := library.baseCompiler.compile(ctx, flags, deps)
552 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700553 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700554
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555 if library.static() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700556 srcs := android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700557 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800558 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800559 } else if library.shared() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700560 srcs := android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700561 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800562 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700563 }
564
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700565 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700566}
567
568type libraryInterface interface {
569 getWholeStaticMissingDeps() []string
570 static() bool
Inseob Kimae553032019-05-14 18:52:49 +0900571 shared() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700572 objs() Objects
Inseob Kim69378442019-06-03 19:10:47 +0900573 reuseObjs() (Objects, exportedFlagsProducer)
Colin Cross26c34ed2016-09-30 17:10:16 -0700574 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700575
576 // Returns true if the build options for the module have selected a static or shared build
577 buildStatic() bool
578 buildShared() bool
579
580 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800581 setStatic()
582 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800583
584 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
585 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Jiyong Parka90ca002019-10-07 15:47:24 +0900586
587 availableFor(string) bool
Colin Crossb916a382016-07-29 17:28:03 -0700588}
589
Jooyung Han0302a842019-10-30 18:43:49 +0900590func (library *libraryDecorator) getLibNameHelper(baseModuleName string, useVndk bool) string {
Colin Crossb916a382016-07-29 17:28:03 -0700591 name := library.libName
592 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100593 name = String(library.Properties.Stem)
594 if name == "" {
Jooyung Han0302a842019-10-30 18:43:49 +0900595 name = baseModuleName
dimitryd95964a2018-11-07 13:43:34 +0100596 }
Colin Crossb916a382016-07-29 17:28:03 -0700597 }
598
Colin Cross0fd6a412019-08-16 14:22:10 -0700599 suffix := ""
Jooyung Han0302a842019-10-30 18:43:49 +0900600 if useVndk {
Colin Cross0fd6a412019-08-16 14:22:10 -0700601 suffix = String(library.Properties.Target.Vendor.Suffix)
602 }
603 if suffix == "" {
604 suffix = String(library.Properties.Suffix)
605 }
606
Jooyung Han0302a842019-10-30 18:43:49 +0900607 return name + suffix
608}
609
610func (library *libraryDecorator) getLibName(ctx BaseModuleContext) string {
611 name := library.getLibNameHelper(ctx.baseModuleName(), ctx.useVndk())
Colin Cross0fd6a412019-08-16 14:22:10 -0700612
Logan Chienf3511742017-10-31 18:04:35 +0800613 if ctx.isVndkExt() {
Jooyung Han4c2b9422019-10-22 19:53:47 +0900614 // vndk-ext lib should have the same name with original lib
615 ctx.VisitDirectDepsWithTag(vndkExtDepTag, func(module android.Module) {
616 originalName := module.(*Module).outputFile.Path()
617 name = strings.TrimSuffix(originalName.Base(), originalName.Ext())
618 })
Logan Chienf3511742017-10-31 18:04:35 +0800619 }
620
Colin Crossb916a382016-07-29 17:28:03 -0700621 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
622 if !strings.HasSuffix(name, "-host") {
623 name = name + "-host"
624 }
625 }
626
Inseob Kim0ce291e2019-07-04 14:38:27 +0900627 return name
Colin Crossb916a382016-07-29 17:28:03 -0700628}
629
Jiyong Parkda732bd2018-11-02 18:23:15 +0900630var versioningMacroNamesListMutex sync.Mutex
631
Colin Crossb916a382016-07-29 17:28:03 -0700632func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
633 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700634 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700635 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700636 }
637 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700638 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900639 // Let baseLinker know whether this variant is for stubs or not, so that
640 // it can omit things that are not required for linking stubs.
641 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900642
643 if library.buildStubs() {
644 macroNames := versioningMacroNamesList(ctx.Config())
645 myName := versioningMacroName(ctx.ModuleName())
646 versioningMacroNamesListMutex.Lock()
647 defer versioningMacroNamesListMutex.Unlock()
648 if (*macroNames)[myName] == "" {
649 (*macroNames)[myName] = ctx.ModuleName()
650 } else if (*macroNames)[myName] != ctx.ModuleName() {
651 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
652 }
653 }
Colin Crossb916a382016-07-29 17:28:03 -0700654}
655
dimitry0345ad82018-12-05 16:28:14 +0100656func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
657 deps = library.baseCompiler.compilerDeps(ctx, deps)
658
dimitry0345ad82018-12-05 16:28:14 +0100659 return deps
660}
661
Colin Cross37047f12016-12-13 17:06:13 -0800662func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800663 if library.static() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700664 if library.StaticProperties.Static.System_shared_libs != nil {
665 library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800666 }
667 } else if library.shared() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700668 if library.SharedProperties.Shared.System_shared_libs != nil {
669 library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800670 }
671 }
672
Colin Crossb916a382016-07-29 17:28:03 -0700673 deps = library.baseLinker.linkerDeps(ctx, deps)
674
675 if library.static() {
676 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
Colin Crosse1bb5d02019-09-24 14:55:04 -0700677 library.StaticProperties.Static.Whole_static_libs...)
678 deps.StaticLibs = append(deps.StaticLibs, library.StaticProperties.Static.Static_libs...)
679 deps.SharedLibs = append(deps.SharedLibs, library.StaticProperties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800680
Colin Crosse1bb5d02019-09-24 14:55:04 -0700681 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...)
682 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800683 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800684 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700685 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700686 deps.CrtBegin = "crtbegin_so"
687 deps.CrtEnd = "crtend_so"
688 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800689 // TODO(danalbert): Add generation of crt objects.
690 // For `sdk_version: "current"`, we don't actually have a
691 // freshly generated set of CRT objects. Use the last stable
692 // version.
693 version := ctx.sdkVersion()
694 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700695 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800696 }
697 deps.CrtBegin = "ndk_crtbegin_so." + version
698 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699 }
700 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700701 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.SharedProperties.Shared.Whole_static_libs...)
702 deps.StaticLibs = append(deps.StaticLibs, library.SharedProperties.Shared.Static_libs...)
703 deps.SharedLibs = append(deps.SharedLibs, library.SharedProperties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800704
Colin Crosse1bb5d02019-09-24 14:55:04 -0700705 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.SharedProperties.Shared.Export_shared_lib_headers...)
706 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.SharedProperties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700707 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900708 if ctx.useVndk() {
709 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
710 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
711 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000712 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
713 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900714 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900715 if ctx.inRecovery() {
716 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
717 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
718 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000719 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
720 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900721 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800722
Colin Cross4d9c2d12016-07-29 12:48:20 -0700723 return deps
724}
725
Colin Crossb916a382016-07-29 17:28:03 -0700726func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700727 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700728
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700729 library.objects = deps.WholeStaticLibObjs.Copy()
730 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731
Inseob Kim0ce291e2019-07-04 14:38:27 +0900732 fileName := ctx.ModuleName() + staticLibraryExtension
Colin Cross86803cf2018-02-15 14:12:26 -0800733 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800734 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700735
Dan Willemsen569edc52018-11-19 09:33:29 -0800736 if Bool(library.baseLinker.Properties.Use_version_lib) {
737 if ctx.Host() {
738 versionedOutputFile := outputFile
739 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
740 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
741 } else {
742 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
743 library.distFile = android.OptionalPathForPath(versionedOutputFile)
744 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
745 }
Colin Cross86803cf2018-02-15 14:12:26 -0800746 }
747
Dan Willemsen581341d2017-02-09 16:16:31 -0800748 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
749
Inseob Kim0ce291e2019-07-04 14:38:27 +0900750 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751
752 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
753
754 ctx.CheckbuildFile(outputFile)
755
756 return outputFile
757}
758
Colin Crossb916a382016-07-29 17:28:03 -0700759func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700760 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700761
762 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700763 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700764
Colin Cross2383f3b2018-02-06 14:40:13 -0800765 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
766 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
767 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700768 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700769 if unexportedSymbols.Valid() {
770 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
771 }
772 if forceNotWeakSymbols.Valid() {
773 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
774 }
775 if forceWeakSymbols.Valid() {
776 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
777 }
778 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700779 if unexportedSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800780 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700781 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
782 }
783 if forceNotWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800784 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
786 }
787 if forceWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800788 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700789 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
790 }
791 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900792 if library.buildStubs() {
793 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800794 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlags)
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900795 linkerDeps = append(linkerDeps, library.versionScriptPath)
796 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700797
798 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
799 outputFile := android.PathForModuleOut(ctx, fileName)
800 ret := outputFile
801
Josh Gao75a50a22019-06-07 17:58:59 -0700802 var implicitOutputs android.WritablePaths
803 if ctx.Windows() {
804 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
805
Colin Cross4af21ed2019-11-04 09:37:55 -0800806 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
Josh Gao75a50a22019-06-07 17:58:59 -0700807 implicitOutputs = append(implicitOutputs, importLibraryPath)
808 }
809
Colin Cross4d9c2d12016-07-29 12:48:20 -0700810 builderFlags := flagsToBuilderFlags(flags)
811
Colin Crossb496cfd2018-09-10 16:50:05 -0700812 // Optimize out relinking against shared libraries whose interface hasn't changed by
813 // depending on a table of contents file instead of the library itself.
Colin Cross70dda7e2019-10-01 22:05:35 -0700814 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.ShlibSuffix()[1:]+".toc")
Colin Crossb496cfd2018-09-10 16:50:05 -0700815 library.tocFile = android.OptionalPathForPath(tocFile)
816 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700817
Colin Cross4d9c2d12016-07-29 12:48:20 -0700818 if library.stripper.needsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800819 if ctx.Darwin() {
820 builderFlags.stripUseGnuStrip = true
821 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700822 strippedOutputFile := outputFile
823 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700824 library.stripper.stripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, builderFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700825 }
Colin Crossb60190a2018-09-04 16:28:17 -0700826 library.unstrippedOutputFile = outputFile
827
Colin Crossd7227f92019-09-05 14:26:33 -0700828 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, library.Properties.Inject_bssl_hash, fileName)
Pete Bentley803e1612019-08-06 22:19:59 +0100829
Dan Willemsen569edc52018-11-19 09:33:29 -0800830 if Bool(library.baseLinker.Properties.Use_version_lib) {
831 if ctx.Host() {
832 versionedOutputFile := outputFile
833 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
834 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
835 } else {
836 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
837 library.distFile = android.OptionalPathForPath(versionedOutputFile)
838
839 if library.stripper.needsStrip(ctx) {
840 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
841 library.distFile = android.OptionalPathForPath(out)
Ryan Prichardf979d732019-05-30 20:53:29 -0700842 library.stripper.stripExecutableOrSharedLib(ctx, versionedOutputFile, out, builderFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800843 }
844
845 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
846 }
Colin Cross86803cf2018-02-15 14:12:26 -0800847 }
848
Jiyong Park64a44f22019-01-18 14:37:08 +0900849 sharedLibs := deps.EarlySharedLibs
850 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700851 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
852
Jiyong Park64a44f22019-01-18 14:37:08 +0900853 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700854 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
855 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700856 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700857
Vic Yang6cd1be82019-06-24 16:08:48 -0700858 if Bool(library.Properties.Sort_bss_symbols_by_size) {
859 unsortedOutputFile := android.PathForModuleOut(ctx, "unsorted", fileName)
860 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
861 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
862 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, unsortedOutputFile, implicitOutputs)
863
864 symbolOrderingFile := android.PathForModuleOut(ctx, "unsorted", fileName+".symbol_order")
865 symbolOrderingFlag := library.baseLinker.sortBssSymbolsBySize(ctx, unsortedOutputFile, symbolOrderingFile, builderFlags)
Colin Cross6d88dba2019-11-06 07:06:58 -0800866 builderFlags.localLdFlags += " " + symbolOrderingFlag
Vic Yang6cd1be82019-06-24 16:08:48 -0700867 linkerDeps = append(linkerDeps, symbolOrderingFile)
868 }
869
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700870 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700871 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Josh Gao75a50a22019-06-07 17:58:59 -0700872 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700873
Dan Willemsen581341d2017-02-09 16:16:31 -0800874 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
875 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800876
877 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
878 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
879
Oliver Nguyenc7434142019-04-24 14:22:25 -0700880 library.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700881 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800882
Colin Cross4d9c2d12016-07-29 12:48:20 -0700883 return ret
884}
885
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900886func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
887 return library.unstrippedOutputFile
888}
889
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700890func (library *libraryDecorator) nativeCoverage() bool {
891 if library.header() || library.buildStubs() {
892 return false
893 }
894 return true
895}
896
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900897func (library *libraryDecorator) coverageOutputFilePath() android.OptionalPath {
898 return library.coverageOutputFile
899}
900
Logan Chien7eefdc42018-07-11 18:10:41 +0800901func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800902 isNdk := ctx.isNdk()
903 isLlndkOrVndk := ctx.isLlndkPublic(ctx.Config()) || ctx.isVndk()
Logan Chien7eefdc42018-07-11 18:10:41 +0800904
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800905 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isNdk, isLlndkOrVndk, false)
906 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isNdk, isLlndkOrVndk, true)
Logan Chien7eefdc42018-07-11 18:10:41 +0800907
908 if refAbiDumpTextFile.Valid() {
909 if refAbiDumpGzipFile.Valid() {
910 ctx.ModuleErrorf(
911 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
912 refAbiDumpTextFile, refAbiDumpGzipFile)
913 return nil
914 }
915 return refAbiDumpTextFile.Path()
916 }
917 if refAbiDumpGzipFile.Valid() {
918 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
919 }
920 return nil
921}
922
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700923func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Hsin-Yi Chenaf17d742019-07-29 17:46:59 +0800924 if library.shouldCreateSourceAbiDump(ctx) {
Logan Chiena8f51582018-03-21 11:53:48 +0800925 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
926 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800927 vndkVersion = ver
928 }
929
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700930 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800931 var SourceAbiFlags []string
932 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700933 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
934 }
Inseob Kim69378442019-06-03 19:10:47 +0900935 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
936 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800937 }
938 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800939 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
Jiyong Parka034b832019-08-27 14:02:19 +0900940 android.OptionalPathForModuleSrc(ctx, library.symbolFileForAbiCheck(ctx)),
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800941 library.Properties.Header_abi_checker.Exclude_symbol_versions,
942 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800943
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800944 addLsdumpPath(library.classifySourceAbiDump(ctx) + ":" + library.sAbiOutputFile.String())
945
Logan Chien7eefdc42018-07-11 18:10:41 +0800946 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
947 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800948 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Inseob Kim9516ee92019-05-09 10:56:13 +0900949 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isLlndk(ctx.Config()), ctx.isNdk(), ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800950 }
951 }
952}
953
Colin Crossb916a382016-07-29 17:28:03 -0700954func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700955 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700956
Colin Crossad59e752017-11-16 14:29:11 -0800957 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700958 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800959 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700960 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700961 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700962 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700963 }
964
Inseob Kim69378442019-06-03 19:10:47 +0900965 library.exportIncludes(ctx)
966 library.reexportDirs(deps.ReexportedDirs...)
967 library.reexportSystemDirs(deps.ReexportedSystemDirs...)
968 library.reexportFlags(deps.ReexportedFlags...)
969 library.reexportDeps(deps.ReexportedDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700970
Nan Zhang0007d812017-11-07 10:57:05 -0800971 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700972 if library.baseCompiler.hasSrcExt(".aidl") {
Jiyong Park74955042019-10-22 20:19:51 +0900973 dir := android.PathForModuleGen(ctx, "aidl")
Inseob Kim69378442019-06-03 19:10:47 +0900974 library.reexportDirs(dir)
975 library.reexportDeps(library.baseCompiler.pathDeps...) // TODO: restrict to aidl deps
Dan Willemsene1240db2016-11-03 14:28:51 -0700976 }
977 }
978
Nan Zhang0007d812017-11-07 10:57:05 -0800979 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700980 if library.baseCompiler.hasSrcExt(".proto") {
Jiyong Park74955042019-10-22 20:19:51 +0900981 var includes android.Paths
Colin Cross19878da2019-03-28 14:45:07 -0700982 if flags.proto.CanonicalPathFromRoot {
Jiyong Park74955042019-10-22 20:19:51 +0900983 includes = append(includes, flags.proto.SubDir)
Colin Cross10d22312017-05-03 11:01:58 -0700984 }
Jiyong Park74955042019-10-22 20:19:51 +0900985 includes = append(includes, flags.proto.Dir)
Inseob Kim69378442019-06-03 19:10:47 +0900986 library.reexportDirs(includes...)
987 library.reexportDeps(library.baseCompiler.pathDeps...) // TODO: restrict to proto deps
Colin Cross0c461f12016-10-20 16:11:43 -0700988 }
989 }
990
Inseob Kim21f26902018-09-06 00:55:20 +0900991 if library.baseCompiler.hasSrcExt(".sysprop") {
Jiyong Park74955042019-10-22 20:19:51 +0900992 dir := android.PathForModuleGen(ctx, "sysprop", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +0900993 if library.Properties.Sysprop.Platform != nil {
994 isProduct := ctx.ProductSpecific() && !ctx.useVndk()
995 isVendor := ctx.useVndk()
996 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
997
Inseob Kim42882742019-07-30 17:55:33 +0900998 if !ctx.inRecovery() && (isProduct || (isOwnerPlatform == isVendor)) {
Jiyong Park74955042019-10-22 20:19:51 +0900999 dir = android.PathForModuleGen(ctx, "sysprop/public", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +09001000 }
1001 }
1002
Inseob Kim69378442019-06-03 19:10:47 +09001003 library.reexportDirs(dir)
1004 library.reexportDeps(library.baseCompiler.pathDeps...)
Inseob Kim21f26902018-09-06 00:55:20 +09001005 }
1006
Jiyong Parkda732bd2018-11-02 18:23:15 +09001007 if library.buildStubs() {
Inseob Kim69378442019-06-03 19:10:47 +09001008 library.reexportFlags("-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion())
Jiyong Parkda732bd2018-11-02 18:23:15 +09001009 }
1010
Colin Cross4d9c2d12016-07-29 12:48:20 -07001011 return out
1012}
1013
Colin Crossb916a382016-07-29 17:28:03 -07001014func (library *libraryDecorator) buildStatic() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001015 return library.MutatedProperties.BuildStatic &&
1016 BoolDefault(library.StaticProperties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001017}
1018
Colin Crossb916a382016-07-29 17:28:03 -07001019func (library *libraryDecorator) buildShared() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001020 return library.MutatedProperties.BuildShared &&
1021 BoolDefault(library.SharedProperties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001022}
1023
Colin Crossb916a382016-07-29 17:28:03 -07001024func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -07001025 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001026}
1027
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001028func (library *libraryDecorator) objs() Objects {
1029 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -07001030}
1031
Inseob Kim69378442019-06-03 19:10:47 +09001032func (library *libraryDecorator) reuseObjs() (Objects, exportedFlagsProducer) {
1033 return library.reuseObjects, &library.flagExporter
Colin Cross4d9c2d12016-07-29 12:48:20 -07001034}
1035
Colin Cross26c34ed2016-09-30 17:10:16 -07001036func (library *libraryDecorator) toc() android.OptionalPath {
1037 return library.tocFile
1038}
1039
Jiyong Parkf1194352019-02-25 11:05:47 +09001040func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
1041 dir := library.baseInstaller.installDir(ctx)
1042 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
1043 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
1044 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
1045 library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
1046}
1047
Colin Crossb916a382016-07-29 17:28:03 -07001048func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -07001049 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +09001050 if ctx.Device() && ctx.useVndk() {
1051 if ctx.isVndkSp() {
1052 library.baseInstaller.subDir = "vndk-sp"
1053 } else if ctx.isVndk() {
Vic Yangefd249e2018-11-12 20:19:56 -08001054 if ctx.DeviceConfig().VndkUseCoreVariant() && !ctx.mustUseVendorVariant() {
1055 library.useCoreVariant = true
1056 }
Justin Yun8fe12122017-12-07 17:18:15 +09001057 library.baseInstaller.subDir = "vndk"
1058 }
Logan Chienf3511742017-10-31 18:04:35 +08001059
1060 // Append a version to vndk or vndk-sp directories on the system partition.
1061 if ctx.isVndk() && !ctx.isVndkExt() {
1062 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
1063 if vndkVersion != "current" && vndkVersion != "" {
1064 library.baseInstaller.subDir += "-" + vndkVersion
1065 }
Justin Yun8effde42017-06-23 19:24:43 +09001066 }
Peter Collingbourne49a25cb2019-05-21 16:06:09 -07001067 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Jiyong Parkf1194352019-02-25 11:05:47 +09001068 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
1069 // The original path becomes a symlink to the corresponding file in the
1070 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -07001071 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Martin Stjernholm279de572019-09-10 23:18:20 +01001072 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() && !translatedArch && !ctx.inRecovery() {
1073 if ctx.Device() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +09001074 library.installSymlinkToRuntimeApex(ctx, file)
1075 }
Jiyong Park429660f2019-01-16 22:31:11 +09001076 library.baseInstaller.subDir = "bootstrap"
1077 }
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001078 } else if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && ctx.isLlndk(ctx.Config()) && !isBionic(ctx.baseModuleName()) {
1079 // Skip installing LLNDK (non-bionic) libraries moved to APEX.
1080 ctx.Module().SkipInstall()
Justin Yun8effde42017-06-23 19:24:43 +09001081 }
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001082
Colin Cross4d9c2d12016-07-29 12:48:20 -07001083 library.baseInstaller.install(ctx, file)
1084 }
Dan Albertf563d252017-10-13 00:29:00 -07001085
Dan Albert281f22b2017-12-13 15:03:47 -08001086 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +09001087 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +09001088 library.baseLinker.sanitize.isUnsanitizedVariant() &&
1089 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -07001090 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -07001091 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -07001092
1093 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
1094 Rule: android.Cp,
1095 Description: "install " + installPath.Base(),
1096 Output: installPath,
1097 Input: file,
1098 })
1099
Colin Cross0875c522017-11-28 17:34:01 -08001100 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -07001101 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001102}
1103
Colin Crossb916a382016-07-29 17:28:03 -07001104func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001105 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -07001106}
1107
Colin Crossa48ab5b2017-02-14 15:28:44 -08001108func (library *libraryDecorator) shared() bool {
1109 return library.MutatedProperties.VariantIsShared
1110}
1111
1112func (library *libraryDecorator) header() bool {
1113 return !library.static() && !library.shared()
1114}
1115
1116func (library *libraryDecorator) setStatic() {
1117 library.MutatedProperties.VariantIsStatic = true
1118 library.MutatedProperties.VariantIsShared = false
1119}
1120
1121func (library *libraryDecorator) setShared() {
1122 library.MutatedProperties.VariantIsStatic = false
1123 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001124}
1125
Colin Crossab3b7322016-12-09 14:46:15 -08001126func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001127 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001128}
1129
1130func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001131 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001132}
1133
Colin Cross5950f382016-12-13 12:50:57 -08001134func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001135 library.MutatedProperties.BuildShared = false
1136 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001137}
1138
Jiyong Park7ed9de32018-10-15 22:25:07 +09001139func (library *libraryDecorator) buildStubs() bool {
1140 return library.MutatedProperties.BuildStubs
1141}
1142
Jiyong Parka034b832019-08-27 14:02:19 +09001143func (library *libraryDecorator) symbolFileForAbiCheck(ctx ModuleContext) *string {
1144 if library.Properties.Header_abi_checker.Symbol_file != nil {
1145 return library.Properties.Header_abi_checker.Symbol_file
1146 }
1147 if ctx.hasStubsVariants() && library.Properties.Stubs.Symbol_file != nil {
1148 return library.Properties.Stubs.Symbol_file
1149 }
1150 return nil
1151}
1152
Jiyong Park7ed9de32018-10-15 22:25:07 +09001153func (library *libraryDecorator) stubsVersion() string {
1154 return library.MutatedProperties.StubsVersion
1155}
1156
Jiyong Parka90ca002019-10-07 15:47:24 +09001157func (library *libraryDecorator) availableFor(what string) bool {
1158 var list []string
1159 if library.static() {
1160 list = library.StaticProperties.Static.Apex_available
1161 } else if library.shared() {
1162 list = library.SharedProperties.Shared.Apex_available
1163 }
1164 if len(list) == 0 {
1165 return false
1166 }
1167 return android.CheckAvailableForApex(what, list)
1168}
1169
Colin Cross571cccf2019-02-04 11:22:08 -08001170var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1171
Jiyong Parkda732bd2018-11-02 18:23:15 +09001172func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001173 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001174 m := make(map[string]string)
1175 return &m
1176 }).(*map[string]string)
1177}
1178
1179// alphanumeric and _ characters are preserved.
1180// other characters are all converted to _
1181var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1182
1183func versioningMacroName(moduleName string) string {
1184 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
1185 macroName = strings.ToUpper(moduleName)
1186 return "__" + macroName + "_API__"
1187}
1188
Colin Crossab3b7322016-12-09 14:46:15 -08001189func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001190 module := newModule(hod, android.MultilibBoth)
1191
Colin Crossb916a382016-07-29 17:28:03 -07001192 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001193 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001194 BuildShared: true,
1195 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001196 },
Colin Crossb916a382016-07-29 17:28:03 -07001197 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001198 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001199 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001200 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001201 }
1202
Colin Crossb916a382016-07-29 17:28:03 -07001203 module.compiler = library
1204 module.linker = library
1205 module.installer = library
1206
1207 return module, library
1208}
1209
Colin Cross10d22312017-05-03 11:01:58 -07001210// connects a shared library to a static library in order to reuse its .o files to avoid
1211// compiling source files twice.
1212func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1213 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1214 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001215
1216 // Check libraries in addition to cflags, since libraries may be exporting different
1217 // include directories.
Colin Crosse1bb5d02019-09-24 14:55:04 -07001218 if len(staticCompiler.StaticProperties.Static.Cflags) == 0 &&
1219 len(sharedCompiler.SharedProperties.Shared.Cflags) == 0 &&
1220 len(staticCompiler.StaticProperties.Static.Whole_static_libs) == 0 &&
1221 len(sharedCompiler.SharedProperties.Shared.Whole_static_libs) == 0 &&
1222 len(staticCompiler.StaticProperties.Static.Static_libs) == 0 &&
1223 len(sharedCompiler.SharedProperties.Shared.Static_libs) == 0 &&
1224 len(staticCompiler.StaticProperties.Static.Shared_libs) == 0 &&
1225 len(sharedCompiler.SharedProperties.Shared.Shared_libs) == 0 &&
1226 staticCompiler.StaticProperties.Static.System_shared_libs == nil &&
1227 sharedCompiler.SharedProperties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001228
1229 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1230 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1231 sharedCompiler.baseCompiler.Properties.Srcs
1232 sharedCompiler.baseCompiler.Properties.Srcs = nil
1233 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Jiyong Parke4bb9862019-02-01 00:31:10 +09001234 } else {
1235 // This dep is just to reference static variant from shared variant
1236 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001237 }
1238 }
1239}
1240
Colin Crosse40b4ea2018-10-02 22:25:58 -07001241func LinkageMutator(mctx android.BottomUpMutatorContext) {
Ivan Lozano52767be2019-10-18 14:49:46 -07001242 cc_prebuilt := false
Colin Crossb916a382016-07-29 17:28:03 -07001243 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -07001244 _, cc_prebuilt = m.linker.(prebuiltLibraryInterface)
1245 }
1246 if cc_prebuilt {
1247 library := mctx.Module().(*Module).linker.(prebuiltLibraryInterface)
Colin Cross33b2fb72019-05-14 14:07:01 -07001248
Ivan Lozano52767be2019-10-18 14:49:46 -07001249 // Always create both the static and shared variants for prebuilt libraries, and then disable the one
1250 // that is not being used. This allows them to share the name of a cc_library module, which requires that
1251 // all the variants of the cc_library also exist on the prebuilt.
1252 modules := mctx.CreateLocalVariations("static", "shared")
1253 static := modules[0].(*Module)
1254 shared := modules[1].(*Module)
Colin Cross33b2fb72019-05-14 14:07:01 -07001255
Ivan Lozano52767be2019-10-18 14:49:46 -07001256 static.linker.(prebuiltLibraryInterface).setStatic()
1257 shared.linker.(prebuiltLibraryInterface).setShared()
Colin Cross33b2fb72019-05-14 14:07:01 -07001258
Ivan Lozano52767be2019-10-18 14:49:46 -07001259 if !library.buildStatic() {
1260 static.linker.(prebuiltLibraryInterface).disablePrebuilt()
Colin Crossb916a382016-07-29 17:28:03 -07001261 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001262 if !library.buildShared() {
1263 shared.linker.(prebuiltLibraryInterface).disablePrebuilt()
1264 }
1265 } else if library, ok := mctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001266
Ivan Lozano2b262972019-11-21 12:30:50 -08001267 // Non-cc.Modules may need an empty variant for their mutators.
1268 variations := []string{}
1269 if library.NonCcVariants() {
1270 variations = append(variations, "")
1271 }
1272
1273 if library.BuildStaticVariant() && library.BuildSharedVariant() {
1274 variations := append([]string{"static", "shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001275
1276 modules := mctx.CreateLocalVariations(variations...)
1277 static := modules[0].(LinkableInterface)
1278 shared := modules[1].(LinkableInterface)
1279
1280 static.SetStatic()
1281 shared.SetShared()
1282
1283 if _, ok := library.(*Module); ok {
1284 reuseStaticLibrary(mctx, static.(*Module), shared.(*Module))
1285 }
1286 } else if library.BuildStaticVariant() {
Ivan Lozano2b262972019-11-21 12:30:50 -08001287 variations := append([]string{"static"}, variations...)
1288
1289 modules := mctx.CreateLocalVariations(variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001290 modules[0].(LinkableInterface).SetStatic()
1291 } else if library.BuildSharedVariant() {
Ivan Lozano2b262972019-11-21 12:30:50 -08001292 variations := append([]string{"shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001293
Ivan Lozano2b262972019-11-21 12:30:50 -08001294 modules := mctx.CreateLocalVariations(variations...)
1295 modules[0].(LinkableInterface).SetShared()
1296 } else if len(variations) > 0 {
1297 mctx.CreateLocalVariations(variations...)
1298 }
Colin Crossb916a382016-07-29 17:28:03 -07001299 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001300}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001301
Colin Cross571cccf2019-02-04 11:22:08 -08001302var stubVersionsKey = android.NewOnceKey("stubVersions")
1303
Jiyong Park25fc6a92018-11-18 18:02:45 +09001304// maps a module name to the list of stubs versions available for the module
1305func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001306 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001307 return make(map[string][]string)
1308 }).(map[string][]string)
1309}
1310
1311var stubsVersionsLock sync.Mutex
1312
Jiyong Park73c54ee2019-10-22 20:31:18 +09001313func LatestStubsVersionFor(config android.Config, name string) string {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001314 versions, ok := stubsVersionsFor(config)[name]
1315 if ok && len(versions) > 0 {
1316 // the versions are alreay sorted in ascending order
1317 return versions[len(versions)-1]
1318 }
1319 return ""
1320}
1321
Jiyong Park7ed9de32018-10-15 22:25:07 +09001322// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001323// (which is unnamed) and zero or more stubs variants.
1324func VersionMutator(mctx android.BottomUpMutatorContext) {
Ivan Lozano52767be2019-10-18 14:49:46 -07001325 if library, ok := mctx.Module().(LinkableInterface); ok && !library.InRecovery() {
1326 if library.CcLibrary() && library.BuildSharedVariant() && len(library.StubsVersions()) > 0 {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001327 versions := []string{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001328 for _, v := range library.StubsVersions() {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001329 if _, err := strconv.Atoi(v); err != nil {
1330 mctx.PropertyErrorf("versions", "%q is not a number", v)
1331 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001332 versions = append(versions, v)
1333 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001334 sort.Slice(versions, func(i, j int) bool {
1335 left, _ := strconv.Atoi(versions[i])
1336 right, _ := strconv.Atoi(versions[j])
1337 return left < right
1338 })
1339
1340 // save the list of versions for later use
1341 copiedVersions := make([]string, len(versions))
1342 copy(copiedVersions, versions)
1343 stubsVersionsLock.Lock()
1344 defer stubsVersionsLock.Unlock()
1345 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1346
1347 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001348 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001349
Jiyong Park7ed9de32018-10-15 22:25:07 +09001350 modules := mctx.CreateVariations(versions...)
1351 for i, m := range modules {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001352 if versions[i] != "" {
Ivan Lozano52767be2019-10-18 14:49:46 -07001353 m.(LinkableInterface).SetBuildStubs()
1354 m.(LinkableInterface).SetStubsVersions(versions[i])
Jiyong Park7ed9de32018-10-15 22:25:07 +09001355 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001356 }
1357 } else {
1358 mctx.CreateVariations("")
1359 }
1360 return
1361 }
1362 if genrule, ok := mctx.Module().(*genrule.Module); ok {
Colin Cross7228ecd2019-11-18 16:00:16 -08001363 if _, ok := genrule.Extra.(*GenruleExtraProperties); ok {
1364 if !genrule.InRecovery() {
1365 mctx.CreateVariations("")
1366 return
1367 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001368 }
1369 }
1370}
Colin Crossd7227f92019-09-05 14:26:33 -07001371
1372// maybeInjectBoringSSLHash adds a rule to run bssl_inject_hash on the output file if the module has the
1373// inject_bssl_hash or if any static library dependencies have inject_bssl_hash set. It returns the output path
1374// that the linked output file should be written to.
1375// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
1376func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.ModuleOutPath,
1377 inject *bool, fileName string) android.ModuleOutPath {
1378 // TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
1379 injectBoringSSLHash := Bool(inject)
1380 ctx.VisitDirectDeps(func(dep android.Module) {
1381 tag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano183a3212019-10-18 14:18:45 -07001382 if tag == StaticDepTag || tag == staticExportDepTag || tag == wholeStaticDepTag || tag == lateStaticDepTag {
Colin Crossd7227f92019-09-05 14:26:33 -07001383 if cc, ok := dep.(*Module); ok {
1384 if library, ok := cc.linker.(*libraryDecorator); ok {
1385 if Bool(library.Properties.Inject_bssl_hash) {
1386 injectBoringSSLHash = true
1387 }
1388 }
1389 }
1390 }
1391 })
1392 if injectBoringSSLHash {
1393 hashedOutputfile := outputFile
1394 outputFile = android.PathForModuleOut(ctx, "unhashed", fileName)
1395
1396 rule := android.NewRuleBuilder()
1397 rule.Command().
1398 BuiltTool(ctx, "bssl_inject_hash").
Pete Bentley5c4be822019-10-01 17:03:17 +01001399 Flag("-sha256").
Colin Crossd7227f92019-09-05 14:26:33 -07001400 FlagWithInput("-in-object ", outputFile).
1401 FlagWithOutput("-o ", hashedOutputfile)
1402 rule.Build(pctx, ctx, "injectCryptoHash", "inject crypto hash")
1403 }
1404
1405 return outputFile
1406}
Paul Duffin13879572019-11-28 14:31:38 +00001407
1408var LibrarySdkMemberType = &librarySdkMemberType{}
1409
1410type librarySdkMemberType struct {
1411}
1412
1413func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1414 targets := mctx.MultiTargets()
1415 for _, lib := range names {
1416 for _, target := range targets {
1417 name, version := StubsLibNameAndVersion(lib)
1418 if version == "" {
1419 version = LatestStubsVersionFor(mctx.Config(), name)
1420 }
1421 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
1422 {Mutator: "image", Variation: android.CoreVariation},
1423 {Mutator: "link", Variation: "shared"},
1424 {Mutator: "version", Variation: version},
1425 }...), dependencyTag, name)
1426 }
1427 }
1428}
1429
1430func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1431 _, ok := module.(*Module)
1432 return ok
1433}
1434
1435// copy exported header files and stub *.so files
1436func (mt *librarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1437 info := organizeVariants(member)
1438 buildSharedNativeLibSnapshot(sdkModuleContext, info, builder)
1439}
1440
1441func buildSharedNativeLibSnapshot(sdkModuleContext android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) {
1442 // a function for emitting include dirs
1443 printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) {
1444 includeDirs := lib.exportedIncludeDirs
1445 includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...)
1446 if len(includeDirs) == 0 {
1447 return
1448 }
1449 for _, dir := range includeDirs {
1450 if _, gen := dir.(android.WritablePath); gen {
1451 // generated headers are copied via exportedDeps. See below.
1452 continue
1453 }
1454 targetDir := nativeIncludeDir
1455 if info.hasArchSpecificFlags {
1456 targetDir = filepath.Join(lib.archType, targetDir)
1457 }
1458
1459 // TODO(jiyong) copy headers having other suffixes
1460 headers, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.h", nil)
1461 for _, file := range headers {
1462 src := android.PathForSource(sdkModuleContext, file)
1463 dest := filepath.Join(targetDir, file)
1464 builder.CopyToSnapshot(src, dest)
1465 }
1466 }
1467
1468 genHeaders := lib.exportedDeps
1469 for _, file := range genHeaders {
1470 targetDir := nativeGeneratedIncludeDir
1471 if info.hasArchSpecificFlags {
1472 targetDir = filepath.Join(lib.archType, targetDir)
1473 }
1474 dest := filepath.Join(targetDir, lib.name, file.Rel())
1475 builder.CopyToSnapshot(file, dest)
1476 }
1477 }
1478
1479 if !info.hasArchSpecificFlags {
1480 printExportedDirCopyCommandsForNativeLibs(info.archVariants[0])
1481 }
1482
1483 // for each architecture
1484 for _, av := range info.archVariants {
1485 builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av))
1486
1487 if info.hasArchSpecificFlags {
1488 printExportedDirCopyCommandsForNativeLibs(av)
1489 }
1490 }
1491
1492 info.generatePrebuiltLibrary(sdkModuleContext, builder)
1493}
1494
1495func (info *nativeLibInfo) generatePrebuiltLibrary(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder) {
1496
1497 // a function for emitting include dirs
1498 addExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, properties android.BpPropertySet, systemInclude bool) {
1499 includeDirs := nativeIncludeDirPathsFor(lib, systemInclude, info.hasArchSpecificFlags)
1500 if len(includeDirs) == 0 {
1501 return
1502 }
1503 var propertyName string
1504 if !systemInclude {
1505 propertyName = "export_include_dirs"
1506 } else {
1507 propertyName = "export_system_include_dirs"
1508 }
1509 properties.AddProperty(propertyName, includeDirs)
1510 }
1511
1512 pbm := builder.AddPrebuiltModule(info.name, "cc_prebuilt_library_shared")
1513
1514 if !info.hasArchSpecificFlags {
1515 addExportedDirsForNativeLibs(info.archVariants[0], pbm, false /*systemInclude*/)
1516 addExportedDirsForNativeLibs(info.archVariants[0], pbm, true /*systemInclude*/)
1517 }
1518
1519 archProperties := pbm.AddPropertySet("arch")
1520 for _, av := range info.archVariants {
1521 archTypeProperties := archProperties.AddPropertySet(av.archType)
1522 archTypeProperties.AddProperty("srcs", []string{nativeStubFilePathFor(av)})
1523 if info.hasArchSpecificFlags {
1524 // export_* properties are added inside the arch: {<arch>: {...}} block
1525 addExportedDirsForNativeLibs(av, archTypeProperties, false /*systemInclude*/)
1526 addExportedDirsForNativeLibs(av, archTypeProperties, true /*systemInclude*/)
1527 }
1528 }
1529 pbm.AddProperty("stl", "none")
1530 pbm.AddProperty("system_shared_libs", []string{})
1531}
1532
1533const (
1534 nativeIncludeDir = "include"
1535 nativeGeneratedIncludeDir = "include_gen"
1536 nativeStubDir = "lib"
1537 nativeStubFileSuffix = ".so"
1538)
1539
1540// path to the stub file of a native shared library. Relative to <sdk_root>/<api_dir>
1541func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string {
1542 return filepath.Join(lib.archType,
1543 nativeStubDir, lib.name+nativeStubFileSuffix)
1544}
1545
1546// paths to the include dirs of a native shared library. Relative to <sdk_root>/<api_dir>
1547func nativeIncludeDirPathsFor(lib archSpecificNativeLibInfo, systemInclude bool, archSpecific bool) []string {
1548 var result []string
1549 var includeDirs []android.Path
1550 if !systemInclude {
1551 includeDirs = lib.exportedIncludeDirs
1552 } else {
1553 includeDirs = lib.exportedSystemIncludeDirs
1554 }
1555 for _, dir := range includeDirs {
1556 var path string
1557 if _, gen := dir.(android.WritablePath); gen {
1558 path = filepath.Join(nativeGeneratedIncludeDir, lib.name)
1559 } else {
1560 path = filepath.Join(nativeIncludeDir, dir.String())
1561 }
1562 if archSpecific {
1563 path = filepath.Join(lib.archType, path)
1564 }
1565 result = append(result, path)
1566 }
1567 return result
1568}
1569
1570// archSpecificNativeLibInfo represents an arch-specific variant of a native lib
1571type archSpecificNativeLibInfo struct {
1572 name string
1573 archType string
1574 exportedIncludeDirs android.Paths
1575 exportedSystemIncludeDirs android.Paths
1576 exportedFlags []string
1577 exportedDeps android.Paths
1578 outputFile android.Path
1579}
1580
1581func (lib *archSpecificNativeLibInfo) signature() string {
1582 return fmt.Sprintf("%v %v %v %v",
1583 lib.name,
1584 lib.exportedIncludeDirs.Strings(),
1585 lib.exportedSystemIncludeDirs.Strings(),
1586 lib.exportedFlags)
1587}
1588
1589// nativeLibInfo represents a collection of arch-specific modules having the same name
1590type nativeLibInfo struct {
1591 name string
1592 archVariants []archSpecificNativeLibInfo
1593 // hasArchSpecificFlags is set to true if modules for each architecture all have the same
1594 // include dirs, flags, etc, in which case only those of the first arch is selected.
1595 hasArchSpecificFlags bool
1596}
1597
1598// Organize the variants by architecture.
1599func organizeVariants(member android.SdkMember) *nativeLibInfo {
1600 info := &nativeLibInfo{name: member.Name()}
1601
1602 for _, variant := range member.Variants() {
1603 ccModule := variant.(*Module)
1604
1605 info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{
1606 name: ccModule.BaseModuleName(),
1607 archType: ccModule.Target().Arch.ArchType.String(),
1608 exportedIncludeDirs: ccModule.ExportedIncludeDirs(),
1609 exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(),
1610 exportedFlags: ccModule.ExportedFlags(),
1611 exportedDeps: ccModule.ExportedDeps(),
1612 outputFile: ccModule.OutputFile().Path(),
1613 })
1614 }
1615
1616 // Determine if include dirs and flags for each variant are different across arch-specific
1617 // variants or not. And set hasArchSpecificFlags accordingly
1618 // by default, include paths and flags are assumed to be the same across arches
1619 info.hasArchSpecificFlags = false
1620 oldSignature := ""
1621 for _, av := range info.archVariants {
1622 newSignature := av.signature()
1623 if oldSignature == "" {
1624 oldSignature = newSignature
1625 }
1626 if oldSignature != newSignature {
1627 info.hasArchSpecificFlags = true
1628 break
1629 }
1630 }
1631
1632 return info
1633}