blob: 6a3b8769347b623d19c926cbe83b25a9a27fcdc1 [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"
Jooyung Han11b0fbd2021-02-05 02:28:22 +090022 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090024 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025
Colin Cross127bb8b2020-12-16 16:46:01 -080026 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070027 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070030 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070031)
32
Chris Parsons3c27ca32020-11-20 12:42:07 -050033// LibraryProperties is a collection of properties shared by cc library rules.
Colin Crossb916a382016-07-29 17:28:03 -070034type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070035 // local file name to pass to the linker as -unexported_symbols_list
Colin Cross27b922f2019-03-04 22:35:41 -080036 Unexported_symbols_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070037 // local file name to pass to the linker as -force_symbols_not_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080038 Force_symbols_not_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070039 // local file name to pass to the linker as -force_symbols_weak_list
Colin Cross27b922f2019-03-04 22:35:41 -080040 Force_symbols_weak_list *string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070041
42 // rename host libraries to prevent overlap with system installed libraries
43 Unique_host_soname *bool
44
Dan Willemsene1240db2016-11-03 14:28:51 -070045 Aidl struct {
46 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080047 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070048 }
49
Colin Cross0c461f12016-10-20 16:11:43 -070050 Proto struct {
51 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080052 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070053 }
Dan Albertf563d252017-10-13 00:29:00 -070054
Inseob Kimc0907f12019-02-08 21:00:45 +090055 Sysprop struct {
56 // Whether platform owns this sysprop library.
57 Platform *bool
Inseob Kimb3f22ca2019-03-05 12:40:24 +090058 } `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +090059
Nan Zhang0007d812017-11-07 10:57:05 -080060 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090061
62 Stubs struct {
63 // Relative path to the symbol map. The symbol map provides the list of
64 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080065 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090066
67 // List versions to generate stubs libs for.
68 Versions []string
69 }
dimitryd95964a2018-11-07 13:43:34 +010070
71 // set the name of the output
72 Stem *string `android:"arch_variant"`
73
Colin Cross0fd6a412019-08-16 14:22:10 -070074 // set suffix of the name of the output
75 Suffix *string `android:"arch_variant"`
76
77 Target struct {
Justin Yun63e9ec72020-10-29 16:49:43 +090078 Vendor, Product struct {
Colin Cross0fd6a412019-08-16 14:22:10 -070079 // set suffix of the name of the output
80 Suffix *string `android:"arch_variant"`
81 }
82 }
83
dimitryd95964a2018-11-07 13:43:34 +010084 // Names of modules to be overridden. Listed modules can only be other shared libraries
85 // (in Make or Soong).
86 // This does not completely prevent installation of the overridden libraries, but if both
87 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
88 // from PRODUCT_PACKAGES.
89 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080090
91 // Properties for ABI compatibility checker
92 Header_abi_checker struct {
Logan Chien41eabe62019-04-10 13:33:58 +080093 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
94 Enabled *bool
95
Logan Chiene3d7a0d2019-01-17 00:18:02 +080096 // Path to a symbol file that specifies the symbols to be included in the generated
97 // ABI dump file
Colin Cross27b922f2019-03-04 22:35:41 -080098 Symbol_file *string `android:"path"`
Logan Chiene3d7a0d2019-01-17 00:18:02 +080099
100 // Symbol versions that should be ignored from the symbol file
101 Exclude_symbol_versions []string
102
103 // Symbol tags that should be ignored from the symbol file
104 Exclude_symbol_tags []string
Logan Chien2a65dda2019-10-01 15:58:07 -0700105
106 // Run checks on all APIs (in addition to the ones referred by
107 // one of exported ELF symbols.)
108 Check_all_apis *bool
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800109 }
Vic Yang6cd1be82019-06-24 16:08:48 -0700110
111 // Order symbols in .bss section by their sizes. Only useful for shared libraries.
112 Sort_bss_symbols_by_size *bool
Pete Bentley803e1612019-08-06 22:19:59 +0100113
114 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
115 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross0477b422020-10-13 18:43:54 -0700116
117 // If this is an LLNDK library, the name of the equivalent llndk_library module.
118 Llndk_stubs *string
Colin Cross127bb8b2020-12-16 16:46:01 -0800119
120 // If this is an LLNDK library, properties to describe the LLNDK stubs. Will be copied from
121 // the module pointed to by llndk_stubs if it is set.
122 Llndk llndkLibraryProperties
Colin Crossa48ab5b2017-02-14 15:28:44 -0800123}
Colin Cross0c461f12016-10-20 16:11:43 -0700124
Chris Parsons3c27ca32020-11-20 12:42:07 -0500125// StaticProperties is a properties stanza to affect only attributes of the "static" variants of a
126// library module.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700127type StaticProperties struct {
128 Static StaticOrSharedProperties `android:"arch_variant"`
129}
130
Chris Parsons3c27ca32020-11-20 12:42:07 -0500131// SharedProperties is a properties stanza to affect only attributes of the "shared" variants of a
132// library module.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700133type SharedProperties struct {
134 Shared StaticOrSharedProperties `android:"arch_variant"`
135}
136
Chris Parsons3c27ca32020-11-20 12:42:07 -0500137// StaticOrSharedProperties is an embedded struct representing properties to affect attributes of
138// either only the "static" variants or only the "shared" variants of a library module. These override
139// the base properties of the same name.
140// Use `StaticProperties` or `SharedProperties`, depending on which variant is needed.
141// `StaticOrSharedProperties` exists only to avoid duplication.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700142type StaticOrSharedProperties struct {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700143 Srcs []string `android:"path,arch_variant"`
144
145 Sanitized Sanitized `android:"arch_variant"`
146
Colin Crosse1bb5d02019-09-24 14:55:04 -0700147 Cflags []string `android:"arch_variant"`
148
149 Enabled *bool `android:"arch_variant"`
150 Whole_static_libs []string `android:"arch_variant"`
151 Static_libs []string `android:"arch_variant"`
152 Shared_libs []string `android:"arch_variant"`
153 System_shared_libs []string `android:"arch_variant"`
154
155 Export_shared_lib_headers []string `android:"arch_variant"`
156 Export_static_lib_headers []string `android:"arch_variant"`
Jiyong Parka90ca002019-10-07 15:47:24 +0900157
158 Apex_available []string `android:"arch_variant"`
Colin Crosse1bb5d02019-09-24 14:55:04 -0700159}
160
Colin Crossa48ab5b2017-02-14 15:28:44 -0800161type LibraryMutatedProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700162 // Build a static variant
163 BuildStatic bool `blueprint:"mutated"`
164 // Build a shared variant
165 BuildShared bool `blueprint:"mutated"`
166 // This variant is shared
167 VariantIsShared bool `blueprint:"mutated"`
168 // This variant is static
169 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900170
171 // This variant is a stubs lib
172 BuildStubs bool `blueprint:"mutated"`
173 // Version of the stubs lib
174 StubsVersion string `blueprint:"mutated"`
Colin Crossd1f898e2020-08-18 18:35:15 -0700175 // List of all stubs versions associated with an implementation lib
176 AllStubsVersions []string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700177}
178
179type FlagExporterProperties struct {
180 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700181 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700182 // against this module. Directories listed in export_include_dirs do not need to be
183 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700184 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700185
Jiyong Park73c54ee2019-10-22 20:31:18 +0900186 // list of directories that will be added to the system include path
187 // using -isystem for this module and any module that links against this module.
188 Export_system_include_dirs []string `android:"arch_variant"`
189
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700190 Target struct {
Justin Yun63e9ec72020-10-29 16:49:43 +0900191 Vendor, Product struct {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700192 // list of exported include directories, like
Justin Yun63e9ec72020-10-29 16:49:43 +0900193 // export_include_dirs, that will be applied to
194 // vendor or product variant of this library.
195 // This will overwrite any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800196 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700197 }
198 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199}
200
201func init() {
Paul Duffin6c26dc72019-12-19 15:02:40 +0000202 RegisterLibraryBuildComponents(android.InitRegistrationContext)
203}
204
205func RegisterLibraryBuildComponents(ctx android.RegistrationContext) {
206 ctx.RegisterModuleType("cc_library_static", LibraryStaticFactory)
207 ctx.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
208 ctx.RegisterModuleType("cc_library", LibraryFactory)
209 ctx.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
210 ctx.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211}
212
Patrice Arruda83c89e02019-03-25 15:32:39 -0700213// cc_library creates both static and/or shared libraries for a device and/or
214// host. By default, a cc_library has a single variant that targets the device.
215// Specifying `host_supported: true` also creates a library that targets the
216// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700217func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800218 module, _ := NewLibrary(android.HostAndDeviceSupported)
Paul Duffina0843f62019-12-13 19:50:38 +0000219 // Can be used as both a static and a shared library.
220 module.sdkMemberTypes = []android.SdkMemberType{
221 sharedLibrarySdkMemberType,
222 staticLibrarySdkMemberType,
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000223 staticAndSharedLibrarySdkMemberType,
Paul Duffina0843f62019-12-13 19:50:38 +0000224 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 return module.Init()
226}
227
Patrice Arruda83c89e02019-03-25 15:32:39 -0700228// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700229func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800230 module, library := NewLibrary(android.HostAndDeviceSupported)
231 library.BuildOnlyStatic()
Paul Duffina0843f62019-12-13 19:50:38 +0000232 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
Chris Parsons808d84c2021-03-09 20:43:32 -0500233 module.bazelHandler = &staticLibraryBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 return module.Init()
235}
236
Patrice Arruda83c89e02019-03-25 15:32:39 -0700237// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700238func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800239 module, library := NewLibrary(android.HostAndDeviceSupported)
240 library.BuildOnlyShared()
Paul Duffina0843f62019-12-13 19:50:38 +0000241 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 return module.Init()
243}
244
Patrice Arruda83c89e02019-03-25 15:32:39 -0700245// cc_library_host_static creates a static library that is linkable to a host
246// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700247func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800248 module, library := NewLibrary(android.HostSupported)
249 library.BuildOnlyStatic()
Paul Duffina0843f62019-12-13 19:50:38 +0000250 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 return module.Init()
252}
253
Patrice Arruda83c89e02019-03-25 15:32:39 -0700254// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700255func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800256 module, library := NewLibrary(android.HostSupported)
257 library.BuildOnlyShared()
Paul Duffina0843f62019-12-13 19:50:38 +0000258 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 return module.Init()
260}
261
Chris Parsons3c27ca32020-11-20 12:42:07 -0500262// flagExporter is a separated portion of libraryDecorator pertaining to exported
263// include paths and flags. Keeping this dependency-related information separate
264// from the rest of library information is helpful in keeping data more structured
265// and explicit.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266type flagExporter struct {
267 Properties FlagExporterProperties
268
Chris Parsons3c27ca32020-11-20 12:42:07 -0500269 dirs android.Paths // Include directories to be included with -I
270 systemDirs android.Paths // System include directories to be included with -isystem
271 flags []string // Exported raw flags.
Inseob Kim69378442019-06-03 19:10:47 +0900272 deps android.Paths
Inseob Kimd110f872019-12-06 13:15:38 +0900273 headers android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274}
275
Chris Parsons3c27ca32020-11-20 12:42:07 -0500276// exportedIncludes returns the effective include paths for this module and
277// any module that links against this module. This is obtained from
278// the export_include_dirs property in the appropriate target stanza.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700279func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Justin Yun6977e8a2020-10-29 18:24:11 +0900280 if ctx.inVendor() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800281 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700282 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900283 if ctx.inProduct() && f.Properties.Target.Product.Override_export_include_dirs != nil {
284 return android.PathsForModuleSrc(ctx, f.Properties.Target.Product.Override_export_include_dirs)
285 }
286 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700287}
288
Chris Parsons3c27ca32020-11-20 12:42:07 -0500289// exportIncludes registers the include directories and system include directories to be exported
290// transitively to modules depending on this module.
Inseob Kim69378442019-06-03 19:10:47 +0900291func (f *flagExporter) exportIncludes(ctx ModuleContext) {
Jiyong Park74955042019-10-22 20:19:51 +0900292 f.dirs = append(f.dirs, f.exportedIncludes(ctx)...)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900293 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294}
295
Chris Parsons3c27ca32020-11-20 12:42:07 -0500296// exportIncludesAsSystem registers the include directories and system include directories to be
297// exported transitively both as system include directories to modules depending on this module.
Inseob Kim69378442019-06-03 19:10:47 +0900298func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900299 // all dirs are force exported as system
Jiyong Park74955042019-10-22 20:19:51 +0900300 f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx)...)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900301 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
Inseob Kim69378442019-06-03 19:10:47 +0900302}
303
Chris Parsons3c27ca32020-11-20 12:42:07 -0500304// reexportDirs registers the given directories as include directories to be exported transitively
305// to modules depending on this module.
Jiyong Park74955042019-10-22 20:19:51 +0900306func (f *flagExporter) reexportDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900307 f.dirs = append(f.dirs, dirs...)
308}
309
Chris Parsons3c27ca32020-11-20 12:42:07 -0500310// reexportSystemDirs registers the given directories as system include directories
311// to be exported transitively to modules depending on this module.
Jiyong Park74955042019-10-22 20:19:51 +0900312func (f *flagExporter) reexportSystemDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900313 f.systemDirs = append(f.systemDirs, dirs...)
314}
315
Chris Parsons3c27ca32020-11-20 12:42:07 -0500316// reexportFlags registers the flags to be exported transitively to modules depending on this
317// module.
Inseob Kim69378442019-06-03 19:10:47 +0900318func (f *flagExporter) reexportFlags(flags ...string) {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800319 if android.PrefixInList(flags, "-I") || android.PrefixInList(flags, "-isystem") {
320 panic(fmt.Errorf("Exporting invalid flag %q: "+
321 "use reexportDirs or reexportSystemDirs to export directories", flag))
Inseob Kim69378442019-06-03 19:10:47 +0900322 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 f.flags = append(f.flags, flags...)
324}
325
Inseob Kim69378442019-06-03 19:10:47 +0900326func (f *flagExporter) reexportDeps(deps ...android.Path) {
327 f.deps = append(f.deps, deps...)
328}
329
Inseob Kimd110f872019-12-06 13:15:38 +0900330// addExportedGeneratedHeaders does nothing but collects generated header files.
331// This can be differ to exportedDeps which may contain phony files to minimize ninja.
332func (f *flagExporter) addExportedGeneratedHeaders(headers ...android.Path) {
333 f.headers = append(f.headers, headers...)
334}
335
Colin Cross0de8a1e2020-09-18 14:15:30 -0700336func (f *flagExporter) setProvider(ctx android.ModuleContext) {
337 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
338 IncludeDirs: f.dirs,
339 SystemIncludeDirs: f.systemDirs,
340 Flags: f.flags,
341 Deps: f.deps,
342 GeneratedHeaders: f.headers,
343 })
Inseob Kim69378442019-06-03 19:10:47 +0900344}
345
Colin Crossb916a382016-07-29 17:28:03 -0700346// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
347// functionality: static vs. shared linkage, reusing object files for shared libraries
348type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800349 Properties LibraryProperties
Colin Crosse1bb5d02019-09-24 14:55:04 -0700350 StaticProperties StaticProperties
351 SharedProperties SharedProperties
Colin Crossa48ab5b2017-02-14 15:28:44 -0800352 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353
354 // For reusing static library objects for shared library
Inseob Kim69378442019-06-03 19:10:47 +0900355 reuseObjects Objects
Colin Cross10d22312017-05-03 11:01:58 -0700356
Colin Cross26c34ed2016-09-30 17:10:16 -0700357 // table-of-contents file to optimize out relinking when possible
358 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700359
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 flagExporter
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200361 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700364 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365
366 // Uses the module's name if empty, but can be overridden. Does not include
367 // shlib suffix.
368 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700369
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800370 sabi *sabi
371
Dan Willemsen581341d2017-02-09 16:16:31 -0800372 // Output archive of gcno coverage information files
373 coverageOutputFile android.OptionalPath
374
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800375 // linked Source Abi Dump
376 sAbiOutputFile android.OptionalPath
377
378 // Source Abi Diff
379 sAbiDiff android.OptionalPath
380
Colin Cross0875c522017-11-28 17:34:01 -0800381 // Location of the static library in the sysroot. Empty if the library is
382 // not included in the NDK.
383 ndkSysrootPath android.Path
384
Colin Crossb60190a2018-09-04 16:28:17 -0700385 // Location of the linked, unstripped library for shared libraries
386 unstrippedOutputFile android.Path
387
Dan Willemsen569edc52018-11-19 09:33:29 -0800388 // Location of the file that should be copied to dist dir when requested
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000389 distFile android.Path
Dan Willemsen569edc52018-11-19 09:33:29 -0800390
Colin Cross8e21aa52020-09-28 18:28:02 -0700391 versionScriptPath android.OptionalPath
Jiyong Park7ed9de32018-10-15 22:25:07 +0900392
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800393 postInstallCmds []string
Jiyong Parkf1194352019-02-25 11:05:47 +0900394
Vic Yangefd249e2018-11-12 20:19:56 -0800395 // If useCoreVariant is true, the vendor variant of a VNDK library is
396 // not installed.
Vic Yangd92090f2020-01-08 14:32:28 -0800397 useCoreVariant bool
398 checkSameCoreVariant bool
Vic Yangefd249e2018-11-12 20:19:56 -0800399
Colin Cross5ec407b2020-09-30 11:41:33 -0700400 skipAPIDefine bool
401
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100402 // Decorated interfaces
Colin Crossb916a382016-07-29 17:28:03 -0700403 *baseCompiler
404 *baseLinker
405 *baseInstaller
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900406
407 collectedSnapshotHeaders android.Paths
408}
409
Chris Parsons808d84c2021-03-09 20:43:32 -0500410type staticLibraryBazelHandler struct {
411 bazelHandler
412
413 module *Module
414}
415
416func (handler *staticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
417 bazelCtx := ctx.Config().BazelContext
418 outputPaths, objPaths, ok := bazelCtx.GetAllFilesAndCcObjectFiles(label, ctx.Arch().ArchType)
419 if ok {
420 if len(outputPaths) != 1 {
421 // TODO(cparsons): This is actually expected behavior for static libraries with no srcs.
422 // We should support this.
423 ctx.ModuleErrorf("expected exactly one output file for '%s', but got %s", label, objPaths)
424 return false
425 }
426 outputFilePath := android.PathForBazelOut(ctx, outputPaths[0])
427 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
428
429 objFiles := make(android.Paths, len(objPaths))
430 for i, objPath := range objPaths {
431 objFiles[i] = android.PathForBazelOut(ctx, objPath)
432 }
433 objects := Objects{
434 objFiles: objFiles,
435 }
436
437 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
438 StaticLibrary: outputFilePath,
439 ReuseObjects: objects,
440 Objects: objects,
441
442 // TODO(cparsons): Include transitive static libraries in this provider to support
443 // static libraries with deps.
444 TransitiveStaticLibrariesForOrdering: android.NewDepSetBuilder(android.TOPOLOGICAL).
445 Direct(outputFilePath).
446 Build(),
447 })
448 handler.module.outputFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, objPaths[0]))
449 }
450 return ok
451}
452
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900453// collectHeadersForSnapshot collects all exported headers from library.
454// It globs header files in the source tree for exported include directories,
455// and tracks generated header files separately.
456//
457// This is to be called from GenerateAndroidBuildActions, and then collected
458// header files can be retrieved by snapshotHeaders().
459func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext) {
460 ret := android.Paths{}
461
462 // Headers in the source tree should be globbed. On the contrast, generated headers
463 // can't be globbed, and they should be manually collected.
464 // So, we first filter out intermediate directories (which contains generated headers)
465 // from exported directories, and then glob headers under remaining directories.
Colin Cross0de8a1e2020-09-18 14:15:30 -0700466 for _, path := range append(android.CopyOfPaths(l.flagExporter.dirs), l.flagExporter.systemDirs...) {
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900467 dir := path.String()
468 // Skip if dir is for generated headers
469 if strings.HasPrefix(dir, android.PathForOutput(ctx).String()) {
470 continue
471 }
Inseob Kim04f2d6a2020-05-29 22:30:58 +0900472 // libeigen wrongly exports the root directory "external/eigen". But only two
473 // subdirectories "Eigen" and "unsupported" contain exported header files. Even worse
474 // some of them have no extension. So we need special treatment for libeigen in order
475 // to glob correctly.
476 if dir == "external/eigen" {
477 // Only these two directories contains exported headers.
478 for _, subdir := range []string{"Eigen", "unsupported/Eigen"} {
479 glob, err := ctx.GlobWithDeps("external/eigen/"+subdir+"/**/*", nil)
480 if err != nil {
481 ctx.ModuleErrorf("glob failed: %#v", err)
482 return
483 }
484 for _, header := range glob {
485 if strings.HasSuffix(header, "/") {
486 continue
487 }
488 ext := filepath.Ext(header)
489 if ext != "" && ext != ".h" {
490 continue
491 }
492 ret = append(ret, android.PathForSource(ctx, header))
493 }
494 }
495 continue
496 }
Colin Crossec118d02021-01-22 18:05:59 +0000497 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
498 if err != nil {
499 ctx.ModuleErrorf("glob failed: %#v", err)
500 return
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900501 }
Colin Crossec118d02021-01-22 18:05:59 +0000502 isLibcxx := strings.HasPrefix(dir, "external/libcxx/include")
503 j := 0
504 for i, header := range glob {
505 if isLibcxx {
506 // Glob all files under this special directory, because of C++ headers with no
507 // extension.
508 if !strings.HasSuffix(header, "/") {
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900509 continue
510 }
Colin Crossec118d02021-01-22 18:05:59 +0000511 } else {
512 // Filter out only the files with extensions that are headers.
513 found := false
514 for _, ext := range headerExts {
515 if strings.HasSuffix(header, ext) {
516 found = true
517 break
518 }
519 }
520 if !found {
521 continue
522 }
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900523 }
Colin Crossec118d02021-01-22 18:05:59 +0000524 if i != j {
525 glob[j] = glob[i]
526 }
527 j++
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900528 }
Colin Cross1f2f4eb2021-01-22 18:06:30 +0000529 glob = glob[:j]
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900530 }
531
532 // Collect generated headers
Colin Cross0de8a1e2020-09-18 14:15:30 -0700533 for _, header := range append(android.CopyOfPaths(l.flagExporter.headers), l.flagExporter.deps...) {
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900534 // TODO(b/148123511): remove exportedDeps after cleaning up genrule
535 if strings.HasSuffix(header.Base(), "-phony") {
536 continue
537 }
538 ret = append(ret, header)
539 }
540
541 l.collectedSnapshotHeaders = ret
542}
543
544// This returns all exported header files, both generated ones and headers from source tree.
545// collectHeadersForSnapshot() must be called before calling this.
546func (l *libraryDecorator) snapshotHeaders() android.Paths {
547 if l.collectedSnapshotHeaders == nil {
548 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
549 }
550 return l.collectedSnapshotHeaders
Colin Cross4d9c2d12016-07-29 12:48:20 -0700551}
552
Chris Parsons3c27ca32020-11-20 12:42:07 -0500553// linkerProps returns the list of properties structs relevant for this library. (For example, if
554// the library is cc_shared_library, then static-library properties are omitted.)
Colin Crossb916a382016-07-29 17:28:03 -0700555func (library *libraryDecorator) linkerProps() []interface{} {
556 var props []interface{}
557 props = append(props, library.baseLinker.linkerProps()...)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700558 props = append(props,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800560 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700562 &library.stripper.StripProperties)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700563
564 if library.MutatedProperties.BuildShared {
565 props = append(props, &library.SharedProperties)
566 }
567 if library.MutatedProperties.BuildStatic {
568 props = append(props, &library.StaticProperties)
569 }
570
571 return props
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572}
573
Chris Parsons3c27ca32020-11-20 12:42:07 -0500574// linkerFlags takes a Flags struct and augments it to contain linker flags that are defined by this
575// library, or that are implied by attributes of this library (such as whether this library is a
576// shared library).
Colin Crossb916a382016-07-29 17:28:03 -0700577func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700578 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700579
Colin Crossb916a382016-07-29 17:28:03 -0700580 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
581 // all code is position independent, and then those warnings get promoted to
582 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700583 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800584 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIC")
Colin Crossb916a382016-07-29 17:28:03 -0700585 }
586
587 if library.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800588 flags.Local.CFlags = append(flags.Local.CFlags, library.StaticProperties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800589 } else if library.shared() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800590 flags.Local.CFlags = append(flags.Local.CFlags, library.SharedProperties.Shared.Cflags...)
Colin Crossb916a382016-07-29 17:28:03 -0700591 }
592
Colin Crossa48ab5b2017-02-14 15:28:44 -0800593 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700596 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700597 f = append(f,
598 "-nostdlib",
599 "-Wl,--gc-sections",
600 )
601 }
602
603 if ctx.Darwin() {
604 f = append(f,
605 "-dynamiclib",
606 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
608 )
Colin Cross7863cf52016-10-20 10:47:21 -0700609 if ctx.Arch().ArchType == android.X86 {
610 f = append(f,
611 "-read_only_relocs suppress",
612 )
613 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700614 } else {
Josh Gao75a50a22019-06-07 17:58:59 -0700615 f = append(f, "-shared")
616 if !ctx.Windows() {
617 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
618 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700619 }
620
Colin Cross4af21ed2019-11-04 09:37:55 -0800621 flags.Global.LdFlags = append(flags.Global.LdFlags, f...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700622 }
623
624 return flags
625}
626
Chris Parsons3c27ca32020-11-20 12:42:07 -0500627// compilerFlags takes a Flags and augments it to contain compile flags from global values,
628// per-target values, module type values, per-module Blueprints properties, extra flags from
629// `flags`, and generated sources from `deps`.
Colin Crossf18e1102017-11-16 14:33:08 -0800630func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700631 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700632 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700633 f := includeDirsToFlags(exportIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800634 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
635 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700636 }
637
Jiyong Park7ed9de32018-10-15 22:25:07 +0900638 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
Colin Cross127bb8b2020-12-16 16:46:01 -0800639 if ctx.IsLlndk() {
640 // LLNDK libraries ignore most of the properties on the cc_library and use the
641 // LLNDK-specific properties instead.
642 // Wipe all the module-local properties, leaving only the global properties.
643 flags.Local = LocalOrGlobalFlags{}
644 }
Jiyong Park7ed9de32018-10-15 22:25:07 +0900645 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900646 // Remove -include <file> when compiling stubs. Otherwise, the force included
647 // headers might cause conflicting types error with the symbols in the
648 // generated stubs source code. e.g.
649 // double acos(double); // in header
650 // void acos() {} // in the generated source code
651 removeInclude := func(flags []string) []string {
652 ret := flags[:0]
653 for _, f := range flags {
654 if strings.HasPrefix(f, "-include ") {
655 continue
656 }
657 ret = append(ret, f)
658 }
659 return ret
660 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800661 flags.Local.CommonFlags = removeInclude(flags.Local.CommonFlags)
662 flags.Local.CFlags = removeInclude(flags.Local.CFlags)
Jiyong Park64379952018-12-13 18:37:29 +0900663
Jiyong Park7ed9de32018-10-15 22:25:07 +0900664 flags = addStubLibraryCompilerFlags(flags)
665 }
666 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700667}
668
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800669func (library *libraryDecorator) headerAbiCheckerEnabled() bool {
670 return Bool(library.Properties.Header_abi_checker.Enabled)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800671}
672
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800673func (library *libraryDecorator) headerAbiCheckerExplicitlyDisabled() bool {
674 return !BoolDefault(library.Properties.Header_abi_checker.Enabled, true)
Logan Chien41eabe62019-04-10 13:33:58 +0800675}
676
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700677func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross127bb8b2020-12-16 16:46:01 -0800678 if ctx.IsLlndk() {
679 // This is the vendor variant of an LLNDK library, build the LLNDK stubs.
680 vndkVer := ctx.Module().(*Module).VndkVersion()
681 if !inList(vndkVer, ctx.Config().PlatformVersionActiveCodenames()) || vndkVer == "" {
682 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
683 vndkVer = "current"
684 }
685 if library.stubsVersion() != "" {
686 vndkVer = library.stubsVersion()
687 }
688 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Llndk.Symbol_file), vndkVer, "--llndk")
689 if !Bool(library.Properties.Llndk.Unversioned) {
690 library.versionScriptPath = android.OptionalPathForPath(versionScript)
691 }
692 return objs
693 }
Jiyong Park7ed9de32018-10-15 22:25:07 +0900694 if library.buildStubs() {
Jiyong Parka4175572021-02-04 21:05:47 +0900695 symbolFile := String(library.Properties.Stubs.Symbol_file)
696 if symbolFile != "" && !strings.HasSuffix(symbolFile, ".map.txt") {
697 ctx.PropertyErrorf("symbol_file", "%q doesn't have .map.txt suffix", symbolFile)
698 return Objects{}
699 }
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900700 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Colin Cross8e21aa52020-09-28 18:28:02 -0700701 library.versionScriptPath = android.OptionalPathForPath(versionScript)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900702 return objs
703 }
704
Colin Cross5950f382016-12-13 12:50:57 -0800705 if !library.buildShared() && !library.buildStatic() {
706 if len(library.baseCompiler.Properties.Srcs) > 0 {
707 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
708 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700709 if len(library.StaticProperties.Static.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800710 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
711 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700712 if len(library.SharedProperties.Shared.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800713 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
714 }
715 return Objects{}
716 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800717 if library.sabi.shouldCreateSourceAbiDump() {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700718 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800719 var SourceAbiFlags []string
720 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700721 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800722 }
Inseob Kim69378442019-06-03 19:10:47 +0900723 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
724 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700725 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800726 flags.SAbiFlags = SourceAbiFlags
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800727 totalLength := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) +
Colin Crosse1bb5d02019-09-24 14:55:04 -0700728 len(library.SharedProperties.Shared.Srcs) + len(library.StaticProperties.Static.Srcs)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800729 if totalLength > 0 {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800730 flags.SAbiDump = true
731 }
732 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700733 objs := library.baseCompiler.compile(ctx, flags, deps)
734 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700735 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700736
Colin Cross4d9c2d12016-07-29 12:48:20 -0700737 if library.static() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700738 srcs := android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700739 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800740 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800741 } else if library.shared() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700742 srcs := android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700743 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800744 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700745 }
746
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700747 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700748}
749
750type libraryInterface interface {
Colin Cross3572cf72020-10-01 15:58:11 -0700751 versionedInterface
752
Colin Crossb916a382016-07-29 17:28:03 -0700753 static() bool
Inseob Kimae553032019-05-14 18:52:49 +0900754 shared() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700755 objs() Objects
Colin Cross0de8a1e2020-09-18 14:15:30 -0700756 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700757 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700758
759 // Returns true if the build options for the module have selected a static or shared build
760 buildStatic() bool
761 buildShared() bool
762
763 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800764 setStatic()
765 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800766
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800767 // Check whether header_abi_checker is enabled or explicitly disabled.
768 headerAbiCheckerEnabled() bool
769 headerAbiCheckerExplicitlyDisabled() bool
770
Logan Chien41eabe62019-04-10 13:33:58 +0800771 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
772 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Jiyong Parka90ca002019-10-07 15:47:24 +0900773
774 availableFor(string) bool
Colin Crossb916a382016-07-29 17:28:03 -0700775}
776
Colin Crossc88c2722020-09-28 17:32:47 -0700777type versionedInterface interface {
778 buildStubs() bool
779 setBuildStubs()
780 hasStubsVariants() bool
781 setStubsVersion(string)
782 stubsVersion() string
783
Colin Cross3572cf72020-10-01 15:58:11 -0700784 stubsVersions(ctx android.BaseMutatorContext) []string
Colin Crossc88c2722020-09-28 17:32:47 -0700785 setAllStubsVersions([]string)
786 allStubsVersions() []string
Colin Cross0477b422020-10-13 18:43:54 -0700787
788 implementationModuleName(name string) string
Colin Cross127bb8b2020-12-16 16:46:01 -0800789 hasLLNDKStubs() bool
Colin Crossc88c2722020-09-28 17:32:47 -0700790}
791
792var _ libraryInterface = (*libraryDecorator)(nil)
793var _ versionedInterface = (*libraryDecorator)(nil)
794
Justin Yun6977e8a2020-10-29 18:24:11 +0900795func (library *libraryDecorator) getLibNameHelper(baseModuleName string, inVendor bool, inProduct bool) string {
Colin Crossb916a382016-07-29 17:28:03 -0700796 name := library.libName
797 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100798 name = String(library.Properties.Stem)
799 if name == "" {
Jooyung Han0302a842019-10-30 18:43:49 +0900800 name = baseModuleName
dimitryd95964a2018-11-07 13:43:34 +0100801 }
Colin Crossb916a382016-07-29 17:28:03 -0700802 }
803
Colin Cross0fd6a412019-08-16 14:22:10 -0700804 suffix := ""
Justin Yun6977e8a2020-10-29 18:24:11 +0900805 if inVendor {
Colin Cross0fd6a412019-08-16 14:22:10 -0700806 suffix = String(library.Properties.Target.Vendor.Suffix)
Justin Yun6977e8a2020-10-29 18:24:11 +0900807 } else if inProduct {
808 suffix = String(library.Properties.Target.Product.Suffix)
Colin Cross0fd6a412019-08-16 14:22:10 -0700809 }
810 if suffix == "" {
811 suffix = String(library.Properties.Suffix)
812 }
813
Jooyung Han0302a842019-10-30 18:43:49 +0900814 return name + suffix
815}
816
Chris Parsons3c27ca32020-11-20 12:42:07 -0500817// getLibName returns the actual canonical name of the library (the name which
818// should be passed to the linker via linker flags).
Jooyung Han0302a842019-10-30 18:43:49 +0900819func (library *libraryDecorator) getLibName(ctx BaseModuleContext) string {
Justin Yun6977e8a2020-10-29 18:24:11 +0900820 name := library.getLibNameHelper(ctx.baseModuleName(), ctx.inVendor(), ctx.inProduct())
Colin Cross0fd6a412019-08-16 14:22:10 -0700821
Ivan Lozanof9e21722020-12-02 09:00:51 -0500822 if ctx.IsVndkExt() {
Jooyung Han4c2b9422019-10-22 19:53:47 +0900823 // vndk-ext lib should have the same name with original lib
824 ctx.VisitDirectDepsWithTag(vndkExtDepTag, func(module android.Module) {
825 originalName := module.(*Module).outputFile.Path()
826 name = strings.TrimSuffix(originalName.Base(), originalName.Ext())
827 })
Logan Chienf3511742017-10-31 18:04:35 +0800828 }
829
Colin Crossb916a382016-07-29 17:28:03 -0700830 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
831 if !strings.HasSuffix(name, "-host") {
832 name = name + "-host"
833 }
834 }
835
Inseob Kim0ce291e2019-07-04 14:38:27 +0900836 return name
Colin Crossb916a382016-07-29 17:28:03 -0700837}
838
Jiyong Parkda732bd2018-11-02 18:23:15 +0900839var versioningMacroNamesListMutex sync.Mutex
840
Colin Crossb916a382016-07-29 17:28:03 -0700841func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
842 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700843 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700844 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700845 }
846 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700847 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900848 // Let baseLinker know whether this variant is for stubs or not, so that
849 // it can omit things that are not required for linking stubs.
850 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900851
852 if library.buildStubs() {
853 macroNames := versioningMacroNamesList(ctx.Config())
854 myName := versioningMacroName(ctx.ModuleName())
855 versioningMacroNamesListMutex.Lock()
856 defer versioningMacroNamesListMutex.Unlock()
857 if (*macroNames)[myName] == "" {
858 (*macroNames)[myName] = ctx.ModuleName()
859 } else if (*macroNames)[myName] != ctx.ModuleName() {
860 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
861 }
862 }
Colin Crossb916a382016-07-29 17:28:03 -0700863}
864
dimitry0345ad82018-12-05 16:28:14 +0100865func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross127bb8b2020-12-16 16:46:01 -0800866 if ctx.IsLlndk() {
867 // LLNDK libraries ignore most of the properties on the cc_library and use the
868 // LLNDK-specific properties instead.
869 return deps
870 }
871
dimitry0345ad82018-12-05 16:28:14 +0100872 deps = library.baseCompiler.compilerDeps(ctx, deps)
873
dimitry0345ad82018-12-05 16:28:14 +0100874 return deps
875}
876
Colin Cross37047f12016-12-13 17:06:13 -0800877func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross127bb8b2020-12-16 16:46:01 -0800878 if ctx.IsLlndk() {
879 // LLNDK libraries ignore most of the properties on the cc_library and use the
880 // LLNDK-specific properties instead.
881 deps.HeaderLibs = append(deps.HeaderLibs, library.Properties.Llndk.Export_llndk_headers...)
882 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders,
883 library.Properties.Llndk.Export_llndk_headers...)
884 return deps
885 }
886
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800887 if library.static() {
Martin Stjernholm10566a02020-03-24 01:19:52 +0000888 // Compare with nil because an empty list needs to be propagated.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700889 if library.StaticProperties.Static.System_shared_libs != nil {
890 library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800891 }
892 } else if library.shared() {
Martin Stjernholm10566a02020-03-24 01:19:52 +0000893 // Compare with nil because an empty list needs to be propagated.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700894 if library.SharedProperties.Shared.System_shared_libs != nil {
895 library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800896 }
897 }
898
Colin Crossb916a382016-07-29 17:28:03 -0700899 deps = library.baseLinker.linkerDeps(ctx, deps)
900
901 if library.static() {
902 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
Colin Crosse1bb5d02019-09-24 14:55:04 -0700903 library.StaticProperties.Static.Whole_static_libs...)
904 deps.StaticLibs = append(deps.StaticLibs, library.StaticProperties.Static.Static_libs...)
905 deps.SharedLibs = append(deps.SharedLibs, library.StaticProperties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800906
Colin Crosse1bb5d02019-09-24 14:55:04 -0700907 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...)
908 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800909 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800910 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Albert92fe7402020-07-15 13:33:30 -0700911 deps.CrtBegin = "crtbegin_so"
912 deps.CrtEnd = "crtend_so"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700913 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700914 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.SharedProperties.Shared.Whole_static_libs...)
915 deps.StaticLibs = append(deps.StaticLibs, library.SharedProperties.Shared.Static_libs...)
916 deps.SharedLibs = append(deps.SharedLibs, library.SharedProperties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800917
Colin Crosse1bb5d02019-09-24 14:55:04 -0700918 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.SharedProperties.Shared.Export_shared_lib_headers...)
919 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.SharedProperties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700920 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900921 if ctx.inVendor() {
Jiyong Park52d25bd2017-10-13 09:17:01 +0900922 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
923 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
924 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000925 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
926 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900927 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900928 if ctx.inProduct() {
929 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
930 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
931 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
932 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
933 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
934 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900935 if ctx.inRecovery() {
936 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
937 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
938 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000939 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
940 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900941 }
Yifan Hongcf4832c2020-01-21 17:04:13 -0800942 if ctx.inRamdisk() {
943 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
944 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
945 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
946 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
947 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
948 }
Yifan Hong6da33c22020-10-27 15:01:21 -0700949 if ctx.inVendorRamdisk() {
950 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
951 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
952 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
953 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
954 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
955 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800956
Colin Cross4d9c2d12016-07-29 12:48:20 -0700957 return deps
958}
959
Paul Duffin13f02712020-03-06 12:30:43 +0000960func (library *libraryDecorator) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
961 specifiedDeps = library.baseLinker.linkerSpecifiedDeps(specifiedDeps)
962 var properties StaticOrSharedProperties
963 if library.static() {
964 properties = library.StaticProperties.Static
965 } else if library.shared() {
966 properties = library.SharedProperties.Shared
967 }
968
969 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, properties.Shared_libs...)
Martin Stjernholm10566a02020-03-24 01:19:52 +0000970
971 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
972 // either input list doesn't come out as nil.
973 if specifiedDeps.systemSharedLibs == nil {
974 specifiedDeps.systemSharedLibs = properties.System_shared_libs
975 } else {
976 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, properties.System_shared_libs...)
977 }
Paul Duffin13f02712020-03-06 12:30:43 +0000978
979 specifiedDeps.sharedLibs = android.FirstUniqueStrings(specifiedDeps.sharedLibs)
Martin Stjernholm10566a02020-03-24 01:19:52 +0000980 if len(specifiedDeps.systemSharedLibs) > 0 {
981 // Skip this if systemSharedLibs is either nil or [], to ensure they are
982 // retained.
983 specifiedDeps.systemSharedLibs = android.FirstUniqueStrings(specifiedDeps.systemSharedLibs)
984 }
Paul Duffin13f02712020-03-06 12:30:43 +0000985 return specifiedDeps
986}
987
Colin Crossb916a382016-07-29 17:28:03 -0700988func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700989 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700990
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700991 library.objects = deps.WholeStaticLibObjs.Copy()
992 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700993
Inseob Kim0ce291e2019-07-04 14:38:27 +0900994 fileName := ctx.ModuleName() + staticLibraryExtension
Colin Cross86803cf2018-02-15 14:12:26 -0800995 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800996 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700997
Dan Willemsen569edc52018-11-19 09:33:29 -0800998 if Bool(library.baseLinker.Properties.Use_version_lib) {
999 if ctx.Host() {
1000 versionedOutputFile := outputFile
1001 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
1002 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1003 } else {
1004 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001005 library.distFile = versionedOutputFile
Dan Willemsen569edc52018-11-19 09:33:29 -08001006 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1007 }
Colin Cross86803cf2018-02-15 14:12:26 -08001008 }
1009
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001010 transformObjToStaticLib(ctx, library.objects.objFiles, deps.WholeStaticLibsFromPrebuilts, builderFlags, outputFile, objs.tidyFiles)
Dan Willemsen581341d2017-02-09 16:16:31 -08001011
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001012 library.coverageOutputFile = transformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001013
Colin Cross4d9c2d12016-07-29 12:48:20 -07001014 ctx.CheckbuildFile(outputFile)
1015
Colin Cross649d8172020-12-10 12:30:21 -08001016 if library.static() {
1017 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
1018 StaticLibrary: outputFile,
1019 ReuseObjects: library.reuseObjects,
1020 Objects: library.objects,
Colin Cross0de8a1e2020-09-18 14:15:30 -07001021
Colin Cross649d8172020-12-10 12:30:21 -08001022 TransitiveStaticLibrariesForOrdering: android.NewDepSetBuilder(android.TOPOLOGICAL).
1023 Direct(outputFile).
1024 Transitive(deps.TranstiveStaticLibrariesForOrdering).
1025 Build(),
1026 })
1027 }
1028
1029 if library.header() {
1030 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
1031 }
Colin Cross0de8a1e2020-09-18 14:15:30 -07001032
Colin Cross4d9c2d12016-07-29 12:48:20 -07001033 return outputFile
1034}
1035
Colin Crossb916a382016-07-29 17:28:03 -07001036func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001037 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001038
1039 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001040 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001041
Colin Cross2383f3b2018-02-06 14:40:13 -08001042 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
1043 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
1044 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -07001045 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001046 if unexportedSymbols.Valid() {
1047 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1048 }
1049 if forceNotWeakSymbols.Valid() {
1050 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1051 }
1052 if forceWeakSymbols.Valid() {
1053 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1054 }
1055 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001056 if unexportedSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001057 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001058 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
1059 }
1060 if forceNotWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001061 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001062 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
1063 }
1064 if forceWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001065 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001066 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
1067 }
1068 }
Colin Cross8e21aa52020-09-28 18:28:02 -07001069 if library.versionScriptPath.Valid() {
Jiyong Parkc1e7f482019-01-12 13:39:10 +09001070 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -08001071 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlags)
Colin Cross8e21aa52020-09-28 18:28:02 -07001072 linkerDeps = append(linkerDeps, library.versionScriptPath.Path())
Jiyong Parkc1e7f482019-01-12 13:39:10 +09001073 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001074
1075 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
1076 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001077 unstrippedOutputFile := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -07001078
Josh Gao75a50a22019-06-07 17:58:59 -07001079 var implicitOutputs android.WritablePaths
1080 if ctx.Windows() {
1081 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
1082
Colin Cross4af21ed2019-11-04 09:37:55 -08001083 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
Josh Gao75a50a22019-06-07 17:58:59 -07001084 implicitOutputs = append(implicitOutputs, importLibraryPath)
1085 }
1086
Colin Cross4d9c2d12016-07-29 12:48:20 -07001087 builderFlags := flagsToBuilderFlags(flags)
1088
Colin Crossb496cfd2018-09-10 16:50:05 -07001089 // Optimize out relinking against shared libraries whose interface hasn't changed by
1090 // depending on a table of contents file instead of the library itself.
Colin Cross70dda7e2019-10-01 22:05:35 -07001091 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.ShlibSuffix()[1:]+".toc")
Colin Crossb496cfd2018-09-10 16:50:05 -07001092 library.tocFile = android.OptionalPathForPath(tocFile)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001093 transformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -07001094
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001095 stripFlags := flagsToStripFlags(flags)
Colin Crossadc81a02020-12-14 17:01:55 -08001096 needsStrip := library.stripper.NeedsStrip(ctx)
1097 if library.buildStubs() {
1098 // No need to strip stubs libraries
1099 needsStrip = false
1100 }
1101 if needsStrip {
Yi Kongb5c34d72018-11-07 16:28:49 -08001102 if ctx.Darwin() {
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001103 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -08001104 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001105 strippedOutputFile := outputFile
1106 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001107 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001108 }
Colin Crossb60190a2018-09-04 16:28:17 -07001109 library.unstrippedOutputFile = outputFile
1110
Colin Crossd7227f92019-09-05 14:26:33 -07001111 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, library.Properties.Inject_bssl_hash, fileName)
Pete Bentley803e1612019-08-06 22:19:59 +01001112
Dan Willemsen569edc52018-11-19 09:33:29 -08001113 if Bool(library.baseLinker.Properties.Use_version_lib) {
1114 if ctx.Host() {
1115 versionedOutputFile := outputFile
1116 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
1117 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1118 } else {
1119 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001120 library.distFile = versionedOutputFile
Dan Willemsen569edc52018-11-19 09:33:29 -08001121
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001122 if library.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -08001123 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001124 library.distFile = out
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001125 library.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -08001126 }
1127
1128 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1129 }
Colin Cross86803cf2018-02-15 14:12:26 -08001130 }
1131
Jiyong Park64a44f22019-01-18 14:37:08 +09001132 sharedLibs := deps.EarlySharedLibs
1133 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001134 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1135
Jiyong Park64a44f22019-01-18 14:37:08 +09001136 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -07001137 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
1138 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001139 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -07001140
Colin Cross127bb8b2020-12-16 16:46:01 -08001141 if Bool(library.Properties.Sort_bss_symbols_by_size) && !library.buildStubs() {
Vic Yang6cd1be82019-06-24 16:08:48 -07001142 unsortedOutputFile := android.PathForModuleOut(ctx, "unsorted", fileName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001143 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Vic Yang6cd1be82019-06-24 16:08:48 -07001144 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1145 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, unsortedOutputFile, implicitOutputs)
1146
1147 symbolOrderingFile := android.PathForModuleOut(ctx, "unsorted", fileName+".symbol_order")
1148 symbolOrderingFlag := library.baseLinker.sortBssSymbolsBySize(ctx, unsortedOutputFile, symbolOrderingFile, builderFlags)
Colin Cross6d88dba2019-11-06 07:06:58 -08001149 builderFlags.localLdFlags += " " + symbolOrderingFlag
Vic Yang6cd1be82019-06-24 16:08:48 -07001150 linkerDeps = append(linkerDeps, symbolOrderingFile)
1151 }
1152
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001153 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001154 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Josh Gao75a50a22019-06-07 17:58:59 -07001155 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001156
Dan Willemsen581341d2017-02-09 16:16:31 -08001157 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
1158 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001159
1160 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
1161 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
1162
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001163 library.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
Colin Cross0de8a1e2020-09-18 14:15:30 -07001164 library.linkSAbiDumpFiles(ctx, objs, fileName, unstrippedOutputFile)
Dan Willemsen581341d2017-02-09 16:16:31 -08001165
Colin Cross0de8a1e2020-09-18 14:15:30 -07001166 var staticAnalogue *StaticLibraryInfo
1167 if static := ctx.GetDirectDepsWithTag(staticVariantTag); len(static) > 0 {
1168 s := ctx.OtherModuleProvider(static[0], StaticLibraryInfoProvider).(StaticLibraryInfo)
1169 staticAnalogue = &s
1170 }
1171
1172 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
1173 TableOfContents: android.OptionalPathForPath(tocFile),
1174 SharedLibrary: unstrippedOutputFile,
1175 UnstrippedSharedLibrary: library.unstrippedOutputFile,
1176 CoverageSharedLibrary: library.coverageOutputFile,
1177 StaticAnalogue: staticAnalogue,
1178 })
1179
1180 stubs := ctx.GetDirectDepsWithTag(stubImplDepTag)
1181 if len(stubs) > 0 {
Chris Parsons3c27ca32020-11-20 12:42:07 -05001182 var stubsInfo []SharedStubLibrary
Colin Cross0de8a1e2020-09-18 14:15:30 -07001183 for _, stub := range stubs {
1184 stubInfo := ctx.OtherModuleProvider(stub, SharedLibraryInfoProvider).(SharedLibraryInfo)
1185 flagInfo := ctx.OtherModuleProvider(stub, FlagExporterInfoProvider).(FlagExporterInfo)
Chris Parsons3c27ca32020-11-20 12:42:07 -05001186 stubsInfo = append(stubsInfo, SharedStubLibrary{
Colin Cross31076b32020-10-23 17:22:06 -07001187 Version: moduleLibraryInterface(stub).stubsVersion(),
Colin Cross0de8a1e2020-09-18 14:15:30 -07001188 SharedLibraryInfo: stubInfo,
1189 FlagExporterInfo: flagInfo,
1190 })
1191 }
Chris Parsons3c27ca32020-11-20 12:42:07 -05001192 ctx.SetProvider(SharedLibraryStubsProvider, SharedLibraryStubsInfo{
1193 SharedStubLibraries: stubsInfo,
Colin Cross0de8a1e2020-09-18 14:15:30 -07001194
Colin Cross127bb8b2020-12-16 16:46:01 -08001195 IsLLNDK: ctx.IsLlndk(),
Colin Cross0de8a1e2020-09-18 14:15:30 -07001196 })
1197 }
1198
1199 return unstrippedOutputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -07001200}
1201
Jiyong Parkaf6d8952019-01-31 12:21:23 +09001202func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
1203 return library.unstrippedOutputFile
1204}
1205
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001206func (library *libraryDecorator) disableStripping() {
1207 library.stripper.StripProperties.Strip.None = BoolPtr(true)
1208}
1209
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -07001210func (library *libraryDecorator) nativeCoverage() bool {
1211 if library.header() || library.buildStubs() {
1212 return false
1213 }
1214 return true
1215}
1216
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001217func (library *libraryDecorator) coverageOutputFilePath() android.OptionalPath {
1218 return library.coverageOutputFile
1219}
1220
Logan Chien7eefdc42018-07-11 18:10:41 +08001221func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Hsin-Yi Chen27d235f2020-03-26 17:50:29 +08001222 // The logic must be consistent with classifySourceAbiDump.
Colin Cross95f1ca02020-10-29 20:47:22 -07001223 isNdk := ctx.isNdk(ctx.Config())
Colin Cross127bb8b2020-12-16 16:46:01 -08001224 isLlndkOrVndk := ctx.IsLlndkPublic() || (ctx.useVndk() && ctx.isVndk())
Logan Chien7eefdc42018-07-11 18:10:41 +08001225
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001226 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isNdk, isLlndkOrVndk, false)
1227 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isNdk, isLlndkOrVndk, true)
Logan Chien7eefdc42018-07-11 18:10:41 +08001228
1229 if refAbiDumpTextFile.Valid() {
1230 if refAbiDumpGzipFile.Valid() {
1231 ctx.ModuleErrorf(
1232 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
1233 refAbiDumpTextFile, refAbiDumpGzipFile)
1234 return nil
1235 }
1236 return refAbiDumpTextFile.Path()
1237 }
1238 if refAbiDumpGzipFile.Valid() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001239 return unzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
Logan Chien7eefdc42018-07-11 18:10:41 +08001240 }
1241 return nil
1242}
1243
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -07001244func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +08001245 if library.sabi.shouldCreateSourceAbiDump() {
Inseob Kim96f17f02020-06-09 22:05:09 +09001246 var vndkVersion string
1247
1248 if ctx.useVndk() {
1249 // For modules linking against vndk, follow its vndk version
1250 vndkVersion = ctx.Module().(*Module).VndkVersion()
1251 } else {
1252 // Regard the other modules as PLATFORM_VNDK_VERSION
1253 vndkVersion = ctx.DeviceConfig().PlatformVndkVersion()
Logan Chienf3511742017-10-31 18:04:35 +08001254 }
1255
Jayant Chowdharya4fce192017-09-06 13:10:03 -07001256 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001257 var SourceAbiFlags []string
1258 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001259 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
1260 }
Inseob Kim69378442019-06-03 19:10:47 +09001261 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes {
1262 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001263 }
1264 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001265 library.sAbiOutputFile = transformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
Jiyong Parka034b832019-08-27 14:02:19 +09001266 android.OptionalPathForModuleSrc(ctx, library.symbolFileForAbiCheck(ctx)),
Logan Chiene3d7a0d2019-01-17 00:18:02 +08001267 library.Properties.Header_abi_checker.Exclude_symbol_versions,
1268 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +08001269
Yo Chiang2bbadfa2020-12-14 11:42:16 +08001270 addLsdumpPath(classifySourceAbiDump(ctx) + ":" + library.sAbiOutputFile.String())
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001271
Logan Chien7eefdc42018-07-11 18:10:41 +08001272 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
1273 if refAbiDumpFile != nil {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001274 library.sAbiDiff = sourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien2a65dda2019-10-01 15:58:07 -07001275 refAbiDumpFile, fileName, exportedHeaderFlags,
1276 Bool(library.Properties.Header_abi_checker.Check_all_apis),
Colin Cross127bb8b2020-12-16 16:46:01 -08001277 ctx.IsLlndk(), ctx.isNdk(ctx.Config()), ctx.IsVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001278 }
1279 }
1280}
1281
Colin Cross127bb8b2020-12-16 16:46:01 -08001282func processLLNDKHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
1283 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
1284 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
1285
1286 var installPaths []android.WritablePath
1287 for _, header := range srcFiles {
1288 headerDir := filepath.Dir(header.String())
1289 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
1290 if err != nil {
1291 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
1292 srcDir.String(), headerDir, err)
1293 continue
1294 }
1295
1296 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
1297 }
1298
1299 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
1300}
1301
Chris Parsons3c27ca32020-11-20 12:42:07 -05001302// link registers actions to link this library, and sets various fields
1303// on this library to reflect information that should be exported up the build
1304// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -07001305func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001306 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001307
Colin Cross127bb8b2020-12-16 16:46:01 -08001308 if ctx.IsLlndk() {
1309 if len(library.Properties.Llndk.Export_preprocessed_headers) > 0 {
1310 // This is the vendor variant of an LLNDK library with preprocessed headers.
1311 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
1312
1313 var timestampFiles android.Paths
1314 for _, dir := range library.Properties.Llndk.Export_preprocessed_headers {
1315 timestampFiles = append(timestampFiles, processLLNDKHeaders(ctx, dir, genHeaderOutDir))
1316 }
1317
1318 if Bool(library.Properties.Llndk.Export_headers_as_system) {
1319 library.reexportSystemDirs(genHeaderOutDir)
1320 } else {
1321 library.reexportDirs(genHeaderOutDir)
1322 }
1323
1324 library.reexportDeps(timestampFiles...)
1325 }
1326
1327 if Bool(library.Properties.Llndk.Export_headers_as_system) {
1328 library.flagExporter.Properties.Export_system_include_dirs = append(
1329 library.flagExporter.Properties.Export_system_include_dirs,
1330 library.flagExporter.Properties.Export_include_dirs...)
1331 library.flagExporter.Properties.Export_include_dirs = nil
1332 }
1333 }
1334
Chris Parsons3c27ca32020-11-20 12:42:07 -05001335 // Linking this library consists of linking `deps.Objs` (.o files in dependencies
1336 // of this library), together with `objs` (.o files created by compiling this
1337 // library).
Colin Crossad59e752017-11-16 14:29:11 -08001338 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001339 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -08001340 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001341 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001342 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001343 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001344 }
1345
Chris Parsons3c27ca32020-11-20 12:42:07 -05001346 // Export include paths and flags to be propagated up the tree.
Inseob Kim69378442019-06-03 19:10:47 +09001347 library.exportIncludes(ctx)
1348 library.reexportDirs(deps.ReexportedDirs...)
1349 library.reexportSystemDirs(deps.ReexportedSystemDirs...)
1350 library.reexportFlags(deps.ReexportedFlags...)
1351 library.reexportDeps(deps.ReexportedDeps...)
Inseob Kimd110f872019-12-06 13:15:38 +09001352 library.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001353
Chris Parsons3c27ca32020-11-20 12:42:07 -05001354 // Optionally export aidl headers.
Nan Zhang0007d812017-11-07 10:57:05 -08001355 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -07001356 if library.baseCompiler.hasSrcExt(".aidl") {
Jiyong Park74955042019-10-22 20:19:51 +09001357 dir := android.PathForModuleGen(ctx, "aidl")
Inseob Kim69378442019-06-03 19:10:47 +09001358 library.reexportDirs(dir)
Inseob Kimd110f872019-12-06 13:15:38 +09001359
Paul Duffin33056e82021-02-19 13:49:08 +00001360 library.reexportDeps(library.baseCompiler.aidlOrderOnlyDeps...)
1361 library.addExportedGeneratedHeaders(library.baseCompiler.aidlHeaders...)
Dan Willemsene1240db2016-11-03 14:28:51 -07001362 }
1363 }
1364
Chris Parsons3c27ca32020-11-20 12:42:07 -05001365 // Optionally export proto headers.
Nan Zhang0007d812017-11-07 10:57:05 -08001366 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -07001367 if library.baseCompiler.hasSrcExt(".proto") {
Jiyong Park74955042019-10-22 20:19:51 +09001368 var includes android.Paths
Colin Cross19878da2019-03-28 14:45:07 -07001369 if flags.proto.CanonicalPathFromRoot {
Jiyong Park74955042019-10-22 20:19:51 +09001370 includes = append(includes, flags.proto.SubDir)
Colin Cross10d22312017-05-03 11:01:58 -07001371 }
Jiyong Park74955042019-10-22 20:19:51 +09001372 includes = append(includes, flags.proto.Dir)
Inseob Kim69378442019-06-03 19:10:47 +09001373 library.reexportDirs(includes...)
Inseob Kimd110f872019-12-06 13:15:38 +09001374
Paul Duffin33056e82021-02-19 13:49:08 +00001375 library.reexportDeps(library.baseCompiler.protoOrderOnlyDeps...)
1376 library.addExportedGeneratedHeaders(library.baseCompiler.protoHeaders...)
Colin Cross0c461f12016-10-20 16:11:43 -07001377 }
1378 }
1379
Inseob Kim07def122020-11-23 14:43:02 +09001380 // If the library is sysprop_library, expose either public or internal header selectively.
Inseob Kim21f26902018-09-06 00:55:20 +09001381 if library.baseCompiler.hasSrcExt(".sysprop") {
Jiyong Park74955042019-10-22 20:19:51 +09001382 dir := android.PathForModuleGen(ctx, "sysprop", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +09001383 if library.Properties.Sysprop.Platform != nil {
Inseob Kimc0907f12019-02-08 21:00:45 +09001384 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
1385
Inseob Kim07def122020-11-23 14:43:02 +09001386 // If the owner is different from the user, expose public header. That is,
1387 // 1) if the user is product (as owner can only be platform / vendor)
Inseob Kim06410042021-01-18 15:23:28 +09001388 // 2) if the owner is platform and the client is vendor
1389 // We don't care Platform -> Vendor dependency as it's already forbidden.
1390 if ctx.Device() && (ctx.ProductSpecific() || (isOwnerPlatform && ctx.inVendor())) {
Jiyong Park74955042019-10-22 20:19:51 +09001391 dir = android.PathForModuleGen(ctx, "sysprop/public", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +09001392 }
1393 }
1394
Paul Duffin37e0de52021-02-19 17:05:39 +00001395 // Make sure to only export headers which are within the include directory.
1396 _, headers := android.FilterPathListPredicate(library.baseCompiler.syspropHeaders, func(path android.Path) bool {
1397 _, isRel := android.MaybeRel(ctx, dir.String(), path.String())
1398 return isRel
1399 })
1400
Chris Parsons3c27ca32020-11-20 12:42:07 -05001401 // Add sysprop-related directories to the exported directories of this library.
Inseob Kim69378442019-06-03 19:10:47 +09001402 library.reexportDirs(dir)
Paul Duffin33056e82021-02-19 13:49:08 +00001403 library.reexportDeps(library.baseCompiler.syspropOrderOnlyDeps...)
Paul Duffin37e0de52021-02-19 17:05:39 +00001404 library.addExportedGeneratedHeaders(headers...)
Inseob Kim21f26902018-09-06 00:55:20 +09001405 }
1406
Chris Parsons3c27ca32020-11-20 12:42:07 -05001407 // Add stub-related flags if this library is a stub library.
Jiyong Park892a98f2020-12-14 09:20:00 +09001408 library.exportVersioningMacroIfNeeded(ctx)
Jiyong Parkda732bd2018-11-02 18:23:15 +09001409
Chris Parsons3c27ca32020-11-20 12:42:07 -05001410 // Propagate a Provider containing information about exported flags, deps, and include paths.
Colin Cross0de8a1e2020-09-18 14:15:30 -07001411 library.flagExporter.setProvider(ctx)
1412
Colin Cross4d9c2d12016-07-29 12:48:20 -07001413 return out
1414}
1415
Jiyong Park892a98f2020-12-14 09:20:00 +09001416func (library *libraryDecorator) exportVersioningMacroIfNeeded(ctx android.BaseModuleContext) {
Colin Cross127bb8b2020-12-16 16:46:01 -08001417 if library.buildStubs() && library.stubsVersion() != "" && !library.skipAPIDefine {
Jiyong Park892a98f2020-12-14 09:20:00 +09001418 name := versioningMacroName(ctx.Module().(*Module).ImplementationModuleName(ctx))
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001419 apiLevel, err := android.ApiLevelFromUser(ctx, library.stubsVersion())
1420 if err != nil {
1421 ctx.ModuleErrorf("Can't export version macro: %s", err.Error())
1422 }
1423 library.reexportFlags("-D" + name + "=" + strconv.Itoa(apiLevel.FinalOrPreviewInt()))
Jiyong Park892a98f2020-12-14 09:20:00 +09001424 }
1425}
1426
Chris Parsons3c27ca32020-11-20 12:42:07 -05001427// buildStatic returns true if this library should be built as a static library.
Colin Crossb916a382016-07-29 17:28:03 -07001428func (library *libraryDecorator) buildStatic() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001429 return library.MutatedProperties.BuildStatic &&
1430 BoolDefault(library.StaticProperties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001431}
1432
Chris Parsons3c27ca32020-11-20 12:42:07 -05001433// buildShared returns true if this library should be built as a shared library.
Colin Crossb916a382016-07-29 17:28:03 -07001434func (library *libraryDecorator) buildShared() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001435 return library.MutatedProperties.BuildShared &&
1436 BoolDefault(library.SharedProperties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001437}
1438
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001439func (library *libraryDecorator) objs() Objects {
1440 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -07001441}
1442
Colin Cross0de8a1e2020-09-18 14:15:30 -07001443func (library *libraryDecorator) reuseObjs() Objects {
1444 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -07001445}
1446
Colin Cross26c34ed2016-09-30 17:10:16 -07001447func (library *libraryDecorator) toc() android.OptionalPath {
1448 return library.tocFile
1449}
1450
Jiyong Parkf1194352019-02-25 11:05:47 +09001451func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
1452 dir := library.baseInstaller.installDir(ctx)
1453 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
1454 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
1455 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001456 library.postInstallCmds = append(library.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +09001457}
1458
Colin Crossb916a382016-07-29 17:28:03 -07001459func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -07001460 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +09001461 if ctx.Device() && ctx.useVndk() {
Jooyung Han261e1582020-10-20 18:54:21 +09001462 // set subDir for VNDK extensions
Ivan Lozanof9e21722020-12-02 09:00:51 -05001463 if ctx.IsVndkExt() {
Jooyung Han261e1582020-10-20 18:54:21 +09001464 if ctx.isVndkSp() {
1465 library.baseInstaller.subDir = "vndk-sp"
1466 } else {
1467 library.baseInstaller.subDir = "vndk"
1468 }
1469 }
1470
Justin Yun31094b12020-12-24 16:11:23 +09001471 // In some cases we want to use core variant for VNDK-Core libs.
1472 // Skip product variant since VNDKs use only the vendor variant.
1473 if ctx.isVndk() && !ctx.isVndkSp() && !ctx.IsVndkExt() && !ctx.inProduct() {
Vic Yang1a5812a2020-01-31 10:38:40 -08001474 mayUseCoreVariant := true
1475
1476 if ctx.mustUseVendorVariant() {
1477 mayUseCoreVariant = false
1478 }
1479
Vic Yang1a5812a2020-01-31 10:38:40 -08001480 if ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
1481 mayUseCoreVariant = false
1482 }
1483
1484 if mayUseCoreVariant {
Vic Yangd92090f2020-01-08 14:32:28 -08001485 library.checkSameCoreVariant = true
1486 if ctx.DeviceConfig().VndkUseCoreVariant() {
1487 library.useCoreVariant = true
1488 }
Vic Yangefd249e2018-11-12 20:19:56 -08001489 }
Justin Yun8fe12122017-12-07 17:18:15 +09001490 }
Logan Chienf3511742017-10-31 18:04:35 +08001491
Jooyung Han261e1582020-10-20 18:54:21 +09001492 // do not install vndk libs
1493 // vndk libs are packaged into VNDK APEX
Ivan Lozanof9e21722020-12-02 09:00:51 -05001494 if ctx.isVndk() && !ctx.IsVndkExt() {
Jooyung Han261e1582020-10-20 18:54:21 +09001495 return
Justin Yun8effde42017-06-23 19:24:43 +09001496 }
Colin Cross56a83212020-09-15 18:30:11 -07001497 } else if len(library.Properties.Stubs.Versions) > 0 && !ctx.Host() && ctx.directlyInAnyApex() {
Jiyong Parkf1194352019-02-25 11:05:47 +09001498 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
1499 // The original path becomes a symlink to the corresponding file in the
1500 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -07001501 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -07001502 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001503 !translatedArch && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() {
Martin Stjernholm279de572019-09-10 23:18:20 +01001504 if ctx.Device() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +09001505 library.installSymlinkToRuntimeApex(ctx, file)
1506 }
Jiyong Park429660f2019-01-16 22:31:11 +09001507 library.baseInstaller.subDir = "bootstrap"
1508 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001509 } else if ctx.directlyInAnyApex() && ctx.IsLlndk() && !isBionic(ctx.baseModuleName()) {
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001510 // Skip installing LLNDK (non-bionic) libraries moved to APEX.
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001511 ctx.Module().HideFromMake()
Justin Yun8effde42017-06-23 19:24:43 +09001512 }
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001513
Colin Cross4d9c2d12016-07-29 12:48:20 -07001514 library.baseInstaller.install(ctx, file)
1515 }
Dan Albertf563d252017-10-13 00:29:00 -07001516
Dan Albert281f22b2017-12-13 15:03:47 -08001517 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001518 !ctx.useVndk() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +09001519 library.baseLinker.sanitize.isUnsanitizedVariant() &&
Colin Cross95b07f22020-12-16 11:06:50 -08001520 ctx.isForPlatform() && !ctx.isPreventInstall() {
Dan Albertf563d252017-10-13 00:29:00 -07001521 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -07001522 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -07001523
1524 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
1525 Rule: android.Cp,
1526 Description: "install " + installPath.Base(),
1527 Output: installPath,
1528 Input: file,
1529 })
1530
Colin Cross0875c522017-11-28 17:34:01 -08001531 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -07001532 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001533}
1534
Paul Duffin0cb37b92020-03-04 14:52:46 +00001535func (library *libraryDecorator) everInstallable() bool {
1536 // Only shared and static libraries are installed. Header libraries (which are
1537 // neither static or shared) are not installed.
1538 return library.shared() || library.static()
1539}
1540
Chris Parsons3c27ca32020-11-20 12:42:07 -05001541// static returns true if this library is for a "static' variant.
Colin Crossb916a382016-07-29 17:28:03 -07001542func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001543 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -07001544}
1545
Chris Parsons3c27ca32020-11-20 12:42:07 -05001546// shared returns true if this library is for a "shared' variant.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001547func (library *libraryDecorator) shared() bool {
1548 return library.MutatedProperties.VariantIsShared
1549}
1550
Chris Parsons3c27ca32020-11-20 12:42:07 -05001551// header returns true if this library is for a header-only variant.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001552func (library *libraryDecorator) header() bool {
Chris Parsons3c27ca32020-11-20 12:42:07 -05001553 // Neither "static" nor "shared" implies this library is header-only.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001554 return !library.static() && !library.shared()
1555}
1556
Chris Parsons3c27ca32020-11-20 12:42:07 -05001557// setStatic marks the library variant as "static".
Colin Crossa48ab5b2017-02-14 15:28:44 -08001558func (library *libraryDecorator) setStatic() {
1559 library.MutatedProperties.VariantIsStatic = true
1560 library.MutatedProperties.VariantIsShared = false
1561}
1562
Chris Parsons3c27ca32020-11-20 12:42:07 -05001563// setShared marks the library variant as "shared".
Colin Crossa48ab5b2017-02-14 15:28:44 -08001564func (library *libraryDecorator) setShared() {
1565 library.MutatedProperties.VariantIsStatic = false
1566 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001567}
1568
Chris Parsons3c27ca32020-11-20 12:42:07 -05001569// BuildOnlyStatic disables building this library as a shared library.
Colin Crossab3b7322016-12-09 14:46:15 -08001570func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001571 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001572}
1573
Chris Parsons3c27ca32020-11-20 12:42:07 -05001574// BuildOnlyShared disables building this library as a static library.
Colin Crossab3b7322016-12-09 14:46:15 -08001575func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001576 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001577}
1578
Chris Parsons3c27ca32020-11-20 12:42:07 -05001579// HeaderOnly disables building this library as a shared or static library;
1580// the library only exists to propagate header file dependencies up the build graph.
Colin Cross5950f382016-12-13 12:50:57 -08001581func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001582 library.MutatedProperties.BuildShared = false
1583 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001584}
1585
Colin Cross127bb8b2020-12-16 16:46:01 -08001586// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs.
1587func (library *libraryDecorator) hasLLNDKStubs() bool {
1588 return String(library.Properties.Llndk_stubs) != ""
1589}
1590
Colin Cross0477b422020-10-13 18:43:54 -07001591func (library *libraryDecorator) implementationModuleName(name string) string {
1592 return name
1593}
1594
Jiyong Park7ed9de32018-10-15 22:25:07 +09001595func (library *libraryDecorator) buildStubs() bool {
1596 return library.MutatedProperties.BuildStubs
1597}
1598
Jiyong Parka034b832019-08-27 14:02:19 +09001599func (library *libraryDecorator) symbolFileForAbiCheck(ctx ModuleContext) *string {
1600 if library.Properties.Header_abi_checker.Symbol_file != nil {
1601 return library.Properties.Header_abi_checker.Symbol_file
1602 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001603 if ctx.Module().(*Module).IsLlndk() {
1604 return library.Properties.Llndk.Symbol_file
1605 }
Colin Cross31076b32020-10-23 17:22:06 -07001606 if library.hasStubsVariants() && library.Properties.Stubs.Symbol_file != nil {
Jiyong Parka034b832019-08-27 14:02:19 +09001607 return library.Properties.Stubs.Symbol_file
1608 }
1609 return nil
1610}
1611
Colin Crossc88c2722020-09-28 17:32:47 -07001612func (library *libraryDecorator) hasStubsVariants() bool {
1613 return len(library.Properties.Stubs.Versions) > 0
1614}
1615
Colin Cross3572cf72020-10-01 15:58:11 -07001616func (library *libraryDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
Colin Crossc88c2722020-09-28 17:32:47 -07001617 return library.Properties.Stubs.Versions
1618}
1619
1620func (library *libraryDecorator) setStubsVersion(version string) {
1621 library.MutatedProperties.StubsVersion = version
1622}
1623
Jiyong Park7ed9de32018-10-15 22:25:07 +09001624func (library *libraryDecorator) stubsVersion() string {
1625 return library.MutatedProperties.StubsVersion
1626}
1627
Colin Crossc88c2722020-09-28 17:32:47 -07001628func (library *libraryDecorator) setBuildStubs() {
1629 library.MutatedProperties.BuildStubs = true
1630}
1631
1632func (library *libraryDecorator) setAllStubsVersions(versions []string) {
1633 library.MutatedProperties.AllStubsVersions = versions
1634}
1635
1636func (library *libraryDecorator) allStubsVersions() []string {
1637 return library.MutatedProperties.AllStubsVersions
1638}
1639
Jooyung Hanad4c1872020-02-27 17:56:44 +09001640func (library *libraryDecorator) isLatestStubVersion() bool {
1641 versions := library.Properties.Stubs.Versions
1642 return versions[len(versions)-1] == library.stubsVersion()
1643}
1644
Jiyong Parka90ca002019-10-07 15:47:24 +09001645func (library *libraryDecorator) availableFor(what string) bool {
1646 var list []string
1647 if library.static() {
1648 list = library.StaticProperties.Static.Apex_available
1649 } else if library.shared() {
1650 list = library.SharedProperties.Shared.Apex_available
1651 }
1652 if len(list) == 0 {
1653 return false
1654 }
1655 return android.CheckAvailableForApex(what, list)
1656}
1657
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001658func (library *libraryDecorator) makeUninstallable(mod *Module) {
Martin Stjernholmbf37d162020-03-31 16:05:34 +01001659 if library.static() && library.buildStatic() && !library.buildStubs() {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001660 // If we're asked to make a static library uninstallable we don't do
1661 // anything since AndroidMkEntries always sets LOCAL_UNINSTALLABLE_MODULE
1662 // for these entries. This is done to still get the make targets for NOTICE
1663 // files from notice_files.mk, which other libraries might depend on.
Martin Stjernholmbf37d162020-03-31 16:05:34 +01001664 return
1665 }
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001666 mod.ModuleBase.MakeUninstallable()
Martin Stjernholmbf37d162020-03-31 16:05:34 +01001667}
1668
Colin Cross571cccf2019-02-04 11:22:08 -08001669var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1670
Chris Parsons3c27ca32020-11-20 12:42:07 -05001671// versioningMacroNamesList returns a singleton map, where keys are "version macro names",
1672// and values are the module name responsible for registering the version macro name.
1673//
1674// Version macros are used when building against stubs, to provide version information about
1675// the stub. Only stub libraries should have an entry in this list.
1676//
1677// For example, when building against libFoo#ver, __LIBFOO_API__ macro is set to ver so
1678// that headers from libFoo can be conditionally compiled (this may hide APIs
1679// that are not available for the version).
1680//
1681// This map is used to ensure that there aren't conflicts between these version macro names.
Jiyong Parkda732bd2018-11-02 18:23:15 +09001682func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001683 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001684 m := make(map[string]string)
1685 return &m
1686 }).(*map[string]string)
1687}
1688
1689// alphanumeric and _ characters are preserved.
1690// other characters are all converted to _
1691var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1692
Chris Parsons3c27ca32020-11-20 12:42:07 -05001693// versioningMacroName returns the canonical version macro name for the given module.
Jiyong Parkda732bd2018-11-02 18:23:15 +09001694func versioningMacroName(moduleName string) string {
1695 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
Jooyung Hanb04a4992020-03-13 18:57:35 +09001696 macroName = strings.ToUpper(macroName)
Jiyong Parkda732bd2018-11-02 18:23:15 +09001697 return "__" + macroName + "_API__"
1698}
1699
Chris Parsons3c27ca32020-11-20 12:42:07 -05001700// NewLibrary builds and returns a new Module corresponding to a C++ library.
1701// Individual module implementations which comprise a C++ library (or something like
1702// a C++ library) should call this function, set some fields on the result, and
1703// then call the Init function.
Colin Crossab3b7322016-12-09 14:46:15 -08001704func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001705 module := newModule(hod, android.MultilibBoth)
1706
Colin Crossb916a382016-07-29 17:28:03 -07001707 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001708 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001709 BuildShared: true,
1710 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001711 },
Colin Crossb916a382016-07-29 17:28:03 -07001712 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001713 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001714 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001715 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001716 }
1717
Colin Crossb916a382016-07-29 17:28:03 -07001718 module.compiler = library
1719 module.linker = library
1720 module.installer = library
Colin Cross31076b32020-10-23 17:22:06 -07001721 module.library = library
Colin Crossb916a382016-07-29 17:28:03 -07001722
1723 return module, library
1724}
1725
Colin Cross10d22312017-05-03 11:01:58 -07001726// connects a shared library to a static library in order to reuse its .o files to avoid
1727// compiling source files twice.
1728func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1729 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1730 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001731
1732 // Check libraries in addition to cflags, since libraries may be exporting different
1733 // include directories.
Colin Crosse1bb5d02019-09-24 14:55:04 -07001734 if len(staticCompiler.StaticProperties.Static.Cflags) == 0 &&
1735 len(sharedCompiler.SharedProperties.Shared.Cflags) == 0 &&
1736 len(staticCompiler.StaticProperties.Static.Whole_static_libs) == 0 &&
1737 len(sharedCompiler.SharedProperties.Shared.Whole_static_libs) == 0 &&
1738 len(staticCompiler.StaticProperties.Static.Static_libs) == 0 &&
1739 len(sharedCompiler.SharedProperties.Shared.Static_libs) == 0 &&
1740 len(staticCompiler.StaticProperties.Static.Shared_libs) == 0 &&
1741 len(sharedCompiler.SharedProperties.Shared.Shared_libs) == 0 &&
Martin Stjernholm10566a02020-03-24 01:19:52 +00001742 // Compare System_shared_libs properties with nil because empty lists are
1743 // semantically significant for them.
Colin Crosse1bb5d02019-09-24 14:55:04 -07001744 staticCompiler.StaticProperties.Static.System_shared_libs == nil &&
1745 sharedCompiler.SharedProperties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001746
1747 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1748 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1749 sharedCompiler.baseCompiler.Properties.Srcs
1750 sharedCompiler.baseCompiler.Properties.Srcs = nil
1751 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
1752 }
Colin Cross0de8a1e2020-09-18 14:15:30 -07001753
1754 // This dep is just to reference static variant from shared variant
1755 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07001756 }
1757}
1758
Chris Parsons3c27ca32020-11-20 12:42:07 -05001759// LinkageMutator adds "static" or "shared" variants for modules depending
1760// on whether the module can be built as a static library or a shared library.
Colin Crosse40b4ea2018-10-02 22:25:58 -07001761func LinkageMutator(mctx android.BottomUpMutatorContext) {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001762 ccPrebuilt := false
Colin Crossb916a382016-07-29 17:28:03 -07001763 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001764 _, ccPrebuilt = m.linker.(prebuiltLibraryInterface)
Ivan Lozano52767be2019-10-18 14:49:46 -07001765 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001766 if ccPrebuilt {
Ivan Lozano52767be2019-10-18 14:49:46 -07001767 library := mctx.Module().(*Module).linker.(prebuiltLibraryInterface)
Colin Cross33b2fb72019-05-14 14:07:01 -07001768
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001769 // Differentiate between header only and building an actual static/shared library
Colin Cross127bb8b2020-12-16 16:46:01 -08001770 buildStatic := library.buildStatic()
1771 buildShared := library.buildShared()
1772 if buildStatic || buildShared {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001773 // Always create both the static and shared variants for prebuilt libraries, and then disable the one
1774 // that is not being used. This allows them to share the name of a cc_library module, which requires that
1775 // all the variants of the cc_library also exist on the prebuilt.
1776 modules := mctx.CreateLocalVariations("static", "shared")
1777 static := modules[0].(*Module)
1778 shared := modules[1].(*Module)
Colin Cross33b2fb72019-05-14 14:07:01 -07001779
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001780 static.linker.(prebuiltLibraryInterface).setStatic()
1781 shared.linker.(prebuiltLibraryInterface).setShared()
Colin Cross33b2fb72019-05-14 14:07:01 -07001782
Colin Cross127bb8b2020-12-16 16:46:01 -08001783 if buildShared {
Colin Cross81ca6cd2020-08-06 17:46:48 -07001784 mctx.AliasVariation("shared")
Colin Cross127bb8b2020-12-16 16:46:01 -08001785 } else if buildStatic {
Colin Cross81ca6cd2020-08-06 17:46:48 -07001786 mctx.AliasVariation("static")
1787 }
1788
Colin Cross127bb8b2020-12-16 16:46:01 -08001789 if !buildStatic {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001790 static.linker.(prebuiltLibraryInterface).disablePrebuilt()
1791 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001792 if !buildShared {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001793 shared.linker.(prebuiltLibraryInterface).disablePrebuilt()
1794 }
1795 } else {
1796 // Header only
Colin Crossb916a382016-07-29 17:28:03 -07001797 }
Paul Duffinf5ea9e12020-02-21 10:57:00 +00001798
Ivan Lozano52767be2019-10-18 14:49:46 -07001799 } else if library, ok := mctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001800
Ivan Lozano2b262972019-11-21 12:30:50 -08001801 // Non-cc.Modules may need an empty variant for their mutators.
1802 variations := []string{}
1803 if library.NonCcVariants() {
1804 variations = append(variations, "")
1805 }
1806
Colin Cross127bb8b2020-12-16 16:46:01 -08001807 isLLNDK := false
1808 if m, ok := mctx.Module().(*Module); ok {
Colin Cross127bb8b2020-12-16 16:46:01 -08001809 // Don't count the vestigial llndk_library module as isLLNDK, it needs a static
1810 // variant so that a cc_library_prebuilt can depend on it.
Colin Cross4f4f8eb2021-01-06 14:07:27 -08001811 isLLNDK = m.IsLlndk() && !isVestigialLLNDKModule(m)
Colin Cross127bb8b2020-12-16 16:46:01 -08001812 }
1813 buildStatic := library.BuildStaticVariant() && !isLLNDK
1814 buildShared := library.BuildSharedVariant()
1815 if buildStatic && buildShared {
Ivan Lozano2b262972019-11-21 12:30:50 -08001816 variations := append([]string{"static", "shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001817
1818 modules := mctx.CreateLocalVariations(variations...)
1819 static := modules[0].(LinkableInterface)
1820 shared := modules[1].(LinkableInterface)
1821
1822 static.SetStatic()
1823 shared.SetShared()
1824
1825 if _, ok := library.(*Module); ok {
1826 reuseStaticLibrary(mctx, static.(*Module), shared.(*Module))
1827 }
Colin Cross81ca6cd2020-08-06 17:46:48 -07001828 mctx.AliasVariation("shared")
Colin Cross127bb8b2020-12-16 16:46:01 -08001829 } else if buildStatic {
Ivan Lozano2b262972019-11-21 12:30:50 -08001830 variations := append([]string{"static"}, variations...)
1831
1832 modules := mctx.CreateLocalVariations(variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001833 modules[0].(LinkableInterface).SetStatic()
Colin Cross81ca6cd2020-08-06 17:46:48 -07001834 mctx.AliasVariation("static")
Colin Cross127bb8b2020-12-16 16:46:01 -08001835 } else if buildShared {
Ivan Lozano2b262972019-11-21 12:30:50 -08001836 variations := append([]string{"shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001837
Ivan Lozano2b262972019-11-21 12:30:50 -08001838 modules := mctx.CreateLocalVariations(variations...)
1839 modules[0].(LinkableInterface).SetShared()
Colin Cross81ca6cd2020-08-06 17:46:48 -07001840 mctx.AliasVariation("shared")
Ivan Lozano2b262972019-11-21 12:30:50 -08001841 } else if len(variations) > 0 {
1842 mctx.CreateLocalVariations(variations...)
Colin Cross81ca6cd2020-08-06 17:46:48 -07001843 mctx.AliasVariation(variations[0])
Ivan Lozano2b262972019-11-21 12:30:50 -08001844 }
Colin Crossb916a382016-07-29 17:28:03 -07001845 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001846}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001847
Chris Parsons3c27ca32020-11-20 12:42:07 -05001848// normalizeVersions modifies `versions` in place, so that each raw version
1849// string becomes its normalized canonical form.
1850// Validates that the versions in `versions` are specified in least to greatest order.
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851func normalizeVersions(ctx android.BaseModuleContext, versions []string) {
Dan Albertc8060532020-07-22 22:32:17 -07001852 var previous android.ApiLevel
Jooyung Hanad4c1872020-02-27 17:56:44 +09001853 for i, v := range versions {
Dan Albertc8060532020-07-22 22:32:17 -07001854 ver, err := android.ApiLevelFromUser(ctx, v)
Jooyung Hanad4c1872020-02-27 17:56:44 +09001855 if err != nil {
Jooyung Hanaed150d2020-04-02 01:41:41 +09001856 ctx.PropertyErrorf("versions", "%s", err.Error())
1857 return
Jooyung Hanad4c1872020-02-27 17:56:44 +09001858 }
Dan Albertc8060532020-07-22 22:32:17 -07001859 if i > 0 && ver.LessThanOrEqualTo(previous) {
1860 ctx.PropertyErrorf("versions", "not sorted: %v", versions)
1861 }
1862 versions[i] = ver.String()
1863 previous = ver
Jooyung Hanaed150d2020-04-02 01:41:41 +09001864 }
Jooyung Hanad4c1872020-02-27 17:56:44 +09001865}
1866
Jooyung Han61b66e92020-03-21 14:21:46 +00001867func createVersionVariations(mctx android.BottomUpMutatorContext, versions []string) {
Colin Cross127bb8b2020-12-16 16:46:01 -08001868 // "" is for the non-stubs (implementation) variant for system modules, or the LLNDK variant
1869 // for LLNDK modules.
Colin Cross0de8a1e2020-09-18 14:15:30 -07001870 variants := append(android.CopyOf(versions), "")
Jooyung Han61b66e92020-03-21 14:21:46 +00001871
Colin Cross127bb8b2020-12-16 16:46:01 -08001872 m := mctx.Module().(*Module)
1873 isLLNDK := m.IsLlndk()
1874
Colin Crossd1f898e2020-08-18 18:35:15 -07001875 modules := mctx.CreateLocalVariations(variants...)
Jooyung Han61b66e92020-03-21 14:21:46 +00001876 for i, m := range modules {
Colin Cross127bb8b2020-12-16 16:46:01 -08001877
1878 if variants[i] != "" || isLLNDK {
1879 // A stubs or LLNDK stubs variant.
Colin Cross31076b32020-10-23 17:22:06 -07001880 c := m.(*Module)
Colin Cross31076b32020-10-23 17:22:06 -07001881 c.sanitize = nil
1882 c.stl = nil
1883 c.Properties.PreventInstall = true
1884 lib := moduleLibraryInterface(m)
1885 lib.setBuildStubs()
Colin Cross127bb8b2020-12-16 16:46:01 -08001886
1887 if variants[i] != "" {
1888 // A non-LLNDK stubs module is hidden from make and has a dependency from the
1889 // implementation module to the stubs module.
1890 c.Properties.HideFromMake = true
1891 lib.setStubsVersion(variants[i])
1892 mctx.AddInterVariantDependency(stubImplDepTag, modules[len(modules)-1], modules[i])
1893 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001894 }
1895 }
Colin Cross094faa52020-08-06 17:38:25 -07001896 mctx.AliasVariation("")
Colin Crossd1f898e2020-08-18 18:35:15 -07001897 latestVersion := ""
1898 if len(versions) > 0 {
1899 latestVersion = versions[len(versions)-1]
1900 }
1901 mctx.CreateAliasVariation("latest", latestVersion)
Jooyung Han61b66e92020-03-21 14:21:46 +00001902}
1903
Colin Crossbbc941b2020-09-30 12:27:01 -07001904func createPerApiVersionVariations(mctx android.BottomUpMutatorContext, minSdkVersion string) {
1905 from, err := nativeApiLevelFromUser(mctx, minSdkVersion)
1906 if err != nil {
1907 mctx.PropertyErrorf("min_sdk_version", err.Error())
1908 return
1909 }
1910
1911 versionStrs := ndkLibraryVersions(mctx, from)
1912 modules := mctx.CreateLocalVariations(versionStrs...)
1913
1914 for i, module := range modules {
1915 module.(*Module).Properties.Sdk_version = StringPtr(versionStrs[i])
1916 }
1917}
1918
Colin Cross3146c5c2020-09-30 15:34:40 -07001919func CanBeOrLinkAgainstVersionVariants(module interface {
Jooyung Han624d35c2020-04-10 12:57:24 +09001920 Host() bool
1921 InRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001922 InVendorRamdisk() bool
Jooyung Han624d35c2020-04-10 12:57:24 +09001923}) bool {
Jose Galmes6f843bc2020-12-11 13:36:29 -08001924 return !module.Host() && !module.InRamdisk() && !module.InVendorRamdisk()
Jooyung Han624d35c2020-04-10 12:57:24 +09001925}
1926
Colin Cross3146c5c2020-09-30 15:34:40 -07001927func CanBeVersionVariant(module interface {
1928 Host() bool
1929 InRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001930 InVendorRamdisk() bool
Colin Cross3146c5c2020-09-30 15:34:40 -07001931 InRecovery() bool
1932 CcLibraryInterface() bool
1933 Shared() bool
Colin Cross3146c5c2020-09-30 15:34:40 -07001934}) bool {
1935 return CanBeOrLinkAgainstVersionVariants(module) &&
Colin Crossa717db72020-10-23 14:53:06 -07001936 module.CcLibraryInterface() && module.Shared()
Colin Cross3146c5c2020-09-30 15:34:40 -07001937}
1938
Colin Cross127bb8b2020-12-16 16:46:01 -08001939func moduleLibraryInterface(module blueprint.Module) libraryInterface {
Colin Cross31076b32020-10-23 17:22:06 -07001940 if m, ok := module.(*Module); ok {
1941 return m.library
1942 }
1943 return nil
1944}
1945
Colin Crossd1f898e2020-08-18 18:35:15 -07001946// versionSelector normalizes the versions in the Stubs.Versions property into MutatedProperties.AllStubsVersions,
1947// and propagates the value from implementation libraries to llndk libraries with the same name.
1948func versionSelectorMutator(mctx android.BottomUpMutatorContext) {
Colin Cross31076b32020-10-23 17:22:06 -07001949 if library := moduleLibraryInterface(mctx.Module()); library != nil && CanBeVersionVariant(mctx.Module().(*Module)) {
1950 if library.buildShared() {
1951 versions := library.stubsVersions(mctx)
Colin Cross3572cf72020-10-01 15:58:11 -07001952 if len(versions) > 0 {
1953 normalizeVersions(mctx, versions)
1954 if mctx.Failed() {
Colin Crossbbc941b2020-09-30 12:27:01 -07001955 return
1956 }
Colin Cross3572cf72020-10-01 15:58:11 -07001957 // Set the versions on the pre-mutated module so they can be read by any llndk modules that
1958 // depend on the implementation library and haven't been mutated yet.
Colin Cross31076b32020-10-23 17:22:06 -07001959 library.setAllStubsVersions(versions)
Colin Cross127bb8b2020-12-16 16:46:01 -08001960 }
1961
1962 if mctx.Module().(*Module).UseVndk() && library.hasLLNDKStubs() {
1963 // Propagate the version to the llndk stubs module.
1964 mctx.VisitDirectDepsWithTag(llndkStubDepTag, func(stubs android.Module) {
1965 if stubsLib := moduleLibraryInterface(stubs); stubsLib != nil {
1966 stubsLib.setAllStubsVersions(library.allStubsVersions())
1967 }
1968 })
Jiyong Park7ed9de32018-10-15 22:25:07 +09001969 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001970 }
Colin Crossd1f898e2020-08-18 18:35:15 -07001971 }
1972}
Jooyung Han61b66e92020-03-21 14:21:46 +00001973
Colin Crossd1f898e2020-08-18 18:35:15 -07001974// versionMutator splits a module into the mandatory non-stubs variant
1975// (which is unnamed) and zero or more stubs variants.
1976func versionMutator(mctx android.BottomUpMutatorContext) {
Colin Cross31076b32020-10-23 17:22:06 -07001977 if library := moduleLibraryInterface(mctx.Module()); library != nil && CanBeVersionVariant(mctx.Module().(*Module)) {
1978 createVersionVariations(mctx, library.allStubsVersions())
Colin Crossbbc941b2020-09-30 12:27:01 -07001979 return
1980 }
1981
1982 if m, ok := mctx.Module().(*Module); ok {
1983 if m.SplitPerApiLevel() && m.IsSdkVariant() {
1984 if mctx.Os() != android.Android {
1985 return
1986 }
1987 createPerApiVersionVariations(mctx, m.MinSdkVersion())
1988 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001989 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001990}
Colin Crossd7227f92019-09-05 14:26:33 -07001991
1992// maybeInjectBoringSSLHash adds a rule to run bssl_inject_hash on the output file if the module has the
1993// inject_bssl_hash or if any static library dependencies have inject_bssl_hash set. It returns the output path
1994// that the linked output file should be written to.
1995// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
1996func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.ModuleOutPath,
1997 inject *bool, fileName string) android.ModuleOutPath {
1998 // TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
1999 injectBoringSSLHash := Bool(inject)
2000 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross6e511a92020-07-27 21:26:48 -07002001 if tag, ok := ctx.OtherModuleDependencyTag(dep).(libraryDependencyTag); ok && tag.static() {
Colin Crossd7227f92019-09-05 14:26:33 -07002002 if cc, ok := dep.(*Module); ok {
2003 if library, ok := cc.linker.(*libraryDecorator); ok {
2004 if Bool(library.Properties.Inject_bssl_hash) {
2005 injectBoringSSLHash = true
2006 }
2007 }
2008 }
2009 }
2010 })
2011 if injectBoringSSLHash {
2012 hashedOutputfile := outputFile
2013 outputFile = android.PathForModuleOut(ctx, "unhashed", fileName)
2014
Colin Crossf1a035e2020-11-16 17:32:30 -08002015 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossd7227f92019-09-05 14:26:33 -07002016 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08002017 BuiltTool("bssl_inject_hash").
Pete Bentley5c4be822019-10-01 17:03:17 +01002018 Flag("-sha256").
Colin Crossd7227f92019-09-05 14:26:33 -07002019 FlagWithInput("-in-object ", outputFile).
2020 FlagWithOutput("-o ", hashedOutputfile)
Colin Crossf1a035e2020-11-16 17:32:30 -08002021 rule.Build("injectCryptoHash", "inject crypto hash")
Colin Crossd7227f92019-09-05 14:26:33 -07002022 }
2023
2024 return outputFile
2025}