blob: d9754df99ecd3f1029eeead58d247db12e2f1c8e [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 Cross4d9c2d12016-07-29 12:48:20 -070026 "android/soong/android"
Kiyoung Kimaa394802024-01-08 12:55:45 +090027
Chris Parsons51f8c392021-08-03 21:01:05 -040028 "github.com/google/blueprint"
29 "github.com/google/blueprint/pathtools"
Spandan Das4238c652022-09-09 01:38:47 +000030 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070031)
32
Liz Kammer2b376bc2022-01-12 12:00:49 -050033// LibraryProperties is a collection of properties shared by cc library rules/cc.
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
Florian Mayer6dab96c2020-12-31 11:39:48 +000062 // Generate stubs to make this library accessible to APEXes.
Jiyong Park7ed9de32018-10-15 22:25:07 +090063 Stubs struct {
64 // Relative path to the symbol map. The symbol map provides the list of
65 // symbols that are exported for stubs variant of this library.
Colin Cross27b922f2019-03-04 22:35:41 -080066 Symbol_file *string `android:"path"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090067
Jiyong Parkd4a3a132021-03-17 20:21:35 +090068 // List versions to generate stubs libs for. The version name "current" is always
69 // implicitly added.
Jiyong Park7ed9de32018-10-15 22:25:07 +090070 Versions []string
Alan Stokes73feba32022-11-14 12:21:24 +000071
72 // Whether to not require the implementation of the library to be installed if a
73 // client of the stubs is installed. Defaults to true; set to false if the
74 // implementation is made available by some other means, e.g. in a Microdroid
75 // virtual machine.
76 Implementation_installable *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090077 }
dimitryd95964a2018-11-07 13:43:34 +010078
79 // set the name of the output
80 Stem *string `android:"arch_variant"`
81
Colin Cross0fd6a412019-08-16 14:22:10 -070082 // set suffix of the name of the output
83 Suffix *string `android:"arch_variant"`
84
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +080085 // Properties for ABI compatibility checker.
86 Header_abi_checker headerAbiCheckerProperties
87
Colin Cross0fd6a412019-08-16 14:22:10 -070088 Target struct {
Justin Yun63e9ec72020-10-29 16:49:43 +090089 Vendor, Product struct {
Colin Cross0fd6a412019-08-16 14:22:10 -070090 // set suffix of the name of the output
91 Suffix *string `android:"arch_variant"`
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +080092
93 Header_abi_checker headerAbiCheckerProperties
Jooyung Han85707de2023-12-01 14:21:13 +090094
95 // Disable stubs for vendor/product variants
96 // This is a workaround to keep `stubs` only for "core" variant (not product/vendor).
97 // It would be nice if we could put `stubs` into a `target: { core: {} }`
98 // block but it's not supported in soong yet. This could be removed/simplified once we have
99 // a better syntax.
100 No_stubs bool
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800101 }
102
103 Platform struct {
104 Header_abi_checker headerAbiCheckerProperties
Colin Cross0fd6a412019-08-16 14:22:10 -0700105 }
106 }
107
dimitryd95964a2018-11-07 13:43:34 +0100108 // Names of modules to be overridden. Listed modules can only be other shared libraries
109 // (in Make or Soong).
110 // This does not completely prevent installation of the overridden libraries, but if both
111 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
112 // from PRODUCT_PACKAGES.
113 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800114
Pete Bentley803e1612019-08-06 22:19:59 +0100115 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
116 Inject_bssl_hash *bool `android:"arch_variant"`
Colin Cross0477b422020-10-13 18:43:54 -0700117
Colin Cross127bb8b2020-12-16 16:46:01 -0800118 // If this is an LLNDK library, properties to describe the LLNDK stubs. Will be copied from
119 // the module pointed to by llndk_stubs if it is set.
120 Llndk llndkLibraryProperties
Colin Cross5271fea2021-04-27 13:06:04 -0700121
122 // If this is a vendor public library, properties to describe the vendor public library stubs.
123 Vendor_public_library vendorPublicLibraryProperties
Colin Crossa48ab5b2017-02-14 15:28:44 -0800124}
Colin Cross0c461f12016-10-20 16:11:43 -0700125
Chris Parsons3c27ca32020-11-20 12:42:07 -0500126// StaticProperties is a properties stanza to affect only attributes of the "static" variants of a
127// library module.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700128type StaticProperties struct {
129 Static StaticOrSharedProperties `android:"arch_variant"`
130}
131
Chris Parsons3c27ca32020-11-20 12:42:07 -0500132// SharedProperties is a properties stanza to affect only attributes of the "shared" variants of a
133// library module.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700134type SharedProperties struct {
135 Shared StaticOrSharedProperties `android:"arch_variant"`
136}
137
Chris Parsons3c27ca32020-11-20 12:42:07 -0500138// StaticOrSharedProperties is an embedded struct representing properties to affect attributes of
139// either only the "static" variants or only the "shared" variants of a library module. These override
140// the base properties of the same name.
141// Use `StaticProperties` or `SharedProperties`, depending on which variant is needed.
142// `StaticOrSharedProperties` exists only to avoid duplication.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700143type StaticOrSharedProperties struct {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700144 Srcs []string `android:"path,arch_variant"`
145
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700146 Tidy_disabled_srcs []string `android:"path,arch_variant"`
147
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800148 Tidy_timeout_srcs []string `android:"path,arch_variant"`
149
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700150 Sanitized Sanitized `android:"arch_variant"`
151
Colin Crosse1bb5d02019-09-24 14:55:04 -0700152 Cflags []string `android:"arch_variant"`
153
Colin Cross6b8f4252021-07-22 11:39:44 -0700154 Enabled *bool `android:"arch_variant"`
155 Whole_static_libs []string `android:"arch_variant"`
156 Static_libs []string `android:"arch_variant"`
157 Shared_libs []string `android:"arch_variant"`
158 System_shared_libs []string `android:"arch_variant"`
Colin Crosse1bb5d02019-09-24 14:55:04 -0700159
160 Export_shared_lib_headers []string `android:"arch_variant"`
161 Export_static_lib_headers []string `android:"arch_variant"`
Jiyong Parka90ca002019-10-07 15:47:24 +0900162
163 Apex_available []string `android:"arch_variant"`
Colin Cross1bc94122021-10-28 13:25:54 -0700164
165 Installable *bool `android:"arch_variant"`
Colin Crosse1bb5d02019-09-24 14:55:04 -0700166}
167
Colin Crossa48ab5b2017-02-14 15:28:44 -0800168type LibraryMutatedProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700169 // Build a static variant
170 BuildStatic bool `blueprint:"mutated"`
171 // Build a shared variant
172 BuildShared bool `blueprint:"mutated"`
173 // This variant is shared
174 VariantIsShared bool `blueprint:"mutated"`
175 // This variant is static
176 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900177
178 // This variant is a stubs lib
179 BuildStubs bool `blueprint:"mutated"`
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900180 // This variant is the latest version
181 IsLatestVersion bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900182 // Version of the stubs lib
183 StubsVersion string `blueprint:"mutated"`
Colin Crossd1f898e2020-08-18 18:35:15 -0700184 // List of all stubs versions associated with an implementation lib
185 AllStubsVersions []string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700186}
187
188type FlagExporterProperties struct {
189 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700190 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700191 // against this module. Directories listed in export_include_dirs do not need to be
192 // listed in local_include_dirs.
Colin Cross0ed579e2021-06-29 00:51:12 +0000193 Export_include_dirs []string `android:"arch_variant,variant_prepend"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700194
Jiyong Park73c54ee2019-10-22 20:31:18 +0900195 // list of directories that will be added to the system include path
196 // using -isystem for this module and any module that links against this module.
Colin Cross0ed579e2021-06-29 00:51:12 +0000197 Export_system_include_dirs []string `android:"arch_variant,variant_prepend"`
Jiyong Park73c54ee2019-10-22 20:31:18 +0900198
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700199 Target struct {
Justin Yun63e9ec72020-10-29 16:49:43 +0900200 Vendor, Product struct {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700201 // list of exported include directories, like
Justin Yun63e9ec72020-10-29 16:49:43 +0900202 // export_include_dirs, that will be applied to
203 // vendor or product variant of this library.
204 // This will overwrite any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800205 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700206 }
207 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208}
209
210func init() {
Paul Duffin6c26dc72019-12-19 15:02:40 +0000211 RegisterLibraryBuildComponents(android.InitRegistrationContext)
212}
213
214func RegisterLibraryBuildComponents(ctx android.RegistrationContext) {
215 ctx.RegisterModuleType("cc_library_static", LibraryStaticFactory)
216 ctx.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
217 ctx.RegisterModuleType("cc_library", LibraryFactory)
218 ctx.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
219 ctx.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220}
221
Patrice Arruda83c89e02019-03-25 15:32:39 -0700222// cc_library creates both static and/or shared libraries for a device and/or
223// host. By default, a cc_library has a single variant that targets the device.
224// Specifying `host_supported: true` also creates a library that targets the
225// host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700226func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800227 module, _ := NewLibrary(android.HostAndDeviceSupported)
Paul Duffina0843f62019-12-13 19:50:38 +0000228 // Can be used as both a static and a shared library.
229 module.sdkMemberTypes = []android.SdkMemberType{
230 sharedLibrarySdkMemberType,
231 staticLibrarySdkMemberType,
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000232 staticAndSharedLibrarySdkMemberType,
Paul Duffina0843f62019-12-13 19:50:38 +0000233 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 return module.Init()
235}
236
Patrice Arruda83c89e02019-03-25 15:32:39 -0700237// cc_library_static creates a static library for a device and/or host binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700238func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800239 module, library := NewLibrary(android.HostAndDeviceSupported)
240 library.BuildOnlyStatic()
Paul Duffina0843f62019-12-13 19:50:38 +0000241 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 return module.Init()
243}
244
Patrice Arruda83c89e02019-03-25 15:32:39 -0700245// cc_library_shared creates a shared library for a device and/or host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700246func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800247 module, library := NewLibrary(android.HostAndDeviceSupported)
248 library.BuildOnlyShared()
Paul Duffina0843f62019-12-13 19:50:38 +0000249 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 return module.Init()
251}
252
Patrice Arruda83c89e02019-03-25 15:32:39 -0700253// cc_library_host_static creates a static library that is linkable to a host
254// binary.
Steven Morelandf9e62162017-11-02 17:00:50 -0700255func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800256 module, library := NewLibrary(android.HostSupported)
257 library.BuildOnlyStatic()
Paul Duffina0843f62019-12-13 19:50:38 +0000258 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 return module.Init()
260}
261
Patrice Arruda83c89e02019-03-25 15:32:39 -0700262// cc_library_host_shared creates a shared library that is usable on a host.
Steven Morelandf9e62162017-11-02 17:00:50 -0700263func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800264 module, library := NewLibrary(android.HostSupported)
265 library.BuildOnlyShared()
Paul Duffina0843f62019-12-13 19:50:38 +0000266 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 return module.Init()
268}
269
Chris Parsons3c27ca32020-11-20 12:42:07 -0500270// flagExporter is a separated portion of libraryDecorator pertaining to exported
271// include paths and flags. Keeping this dependency-related information separate
272// from the rest of library information is helpful in keeping data more structured
273// and explicit.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274type flagExporter struct {
275 Properties FlagExporterProperties
276
Chris Parsons3c27ca32020-11-20 12:42:07 -0500277 dirs android.Paths // Include directories to be included with -I
278 systemDirs android.Paths // System include directories to be included with -isystem
279 flags []string // Exported raw flags.
Inseob Kim69378442019-06-03 19:10:47 +0900280 deps android.Paths
Inseob Kimd110f872019-12-06 13:15:38 +0900281 headers android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282}
283
Chris Parsons3c27ca32020-11-20 12:42:07 -0500284// exportedIncludes returns the effective include paths for this module and
285// any module that links against this module. This is obtained from
286// the export_include_dirs property in the appropriate target stanza.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700287func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Justin Yun6977e8a2020-10-29 18:24:11 +0900288 if ctx.inVendor() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800289 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700290 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900291 if ctx.inProduct() && f.Properties.Target.Product.Override_export_include_dirs != nil {
292 return android.PathsForModuleSrc(ctx, f.Properties.Target.Product.Override_export_include_dirs)
293 }
294 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700295}
296
Chris Parsons3c27ca32020-11-20 12:42:07 -0500297// exportIncludes registers the include directories and system include directories to be exported
298// transitively to modules depending on this module.
Inseob Kim69378442019-06-03 19:10:47 +0900299func (f *flagExporter) exportIncludes(ctx ModuleContext) {
Jiyong Park74955042019-10-22 20:19:51 +0900300 f.dirs = append(f.dirs, 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)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302}
303
Chris Parsons3c27ca32020-11-20 12:42:07 -0500304// exportIncludesAsSystem registers the include directories and system include directories to be
305// exported transitively both as system include directories to modules depending on this module.
Inseob Kim69378442019-06-03 19:10:47 +0900306func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900307 // all dirs are force exported as system
Jiyong Park74955042019-10-22 20:19:51 +0900308 f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx)...)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900309 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
Inseob Kim69378442019-06-03 19:10:47 +0900310}
311
Chris Parsons3c27ca32020-11-20 12:42:07 -0500312// reexportDirs registers the given directories as include directories to be exported transitively
313// to modules depending on this module.
Jiyong Park74955042019-10-22 20:19:51 +0900314func (f *flagExporter) reexportDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900315 f.dirs = append(f.dirs, dirs...)
316}
317
Chris Parsons3c27ca32020-11-20 12:42:07 -0500318// reexportSystemDirs registers the given directories as system include directories
319// to be exported transitively to modules depending on this module.
Jiyong Park74955042019-10-22 20:19:51 +0900320func (f *flagExporter) reexportSystemDirs(dirs ...android.Path) {
Inseob Kim69378442019-06-03 19:10:47 +0900321 f.systemDirs = append(f.systemDirs, dirs...)
322}
323
Chris Parsons3c27ca32020-11-20 12:42:07 -0500324// reexportFlags registers the flags to be exported transitively to modules depending on this
325// module.
Inseob Kim69378442019-06-03 19:10:47 +0900326func (f *flagExporter) reexportFlags(flags ...string) {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800327 if android.PrefixInList(flags, "-I") || android.PrefixInList(flags, "-isystem") {
328 panic(fmt.Errorf("Exporting invalid flag %q: "+
329 "use reexportDirs or reexportSystemDirs to export directories", flag))
Inseob Kim69378442019-06-03 19:10:47 +0900330 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 f.flags = append(f.flags, flags...)
332}
333
Inseob Kim69378442019-06-03 19:10:47 +0900334func (f *flagExporter) reexportDeps(deps ...android.Path) {
335 f.deps = append(f.deps, deps...)
336}
337
Inseob Kimd110f872019-12-06 13:15:38 +0900338// addExportedGeneratedHeaders does nothing but collects generated header files.
339// This can be differ to exportedDeps which may contain phony files to minimize ninja.
340func (f *flagExporter) addExportedGeneratedHeaders(headers ...android.Path) {
341 f.headers = append(f.headers, headers...)
342}
343
Colin Cross0de8a1e2020-09-18 14:15:30 -0700344func (f *flagExporter) setProvider(ctx android.ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800345 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Chris Parsonsf60ecf02021-04-27 14:48:30 -0400346 // Comes from Export_include_dirs property, and those of exported transitive deps
347 IncludeDirs: android.FirstUniquePaths(f.dirs),
348 // Comes from Export_system_include_dirs property, and those of exported transitive deps
Colin Crossc82e6e22021-04-20 18:21:50 -0700349 SystemIncludeDirs: android.FirstUniquePaths(f.systemDirs),
Chris Parsonsf60ecf02021-04-27 14:48:30 -0400350 // Used in very few places as a one-off way of adding extra defines.
351 Flags: f.flags,
352 // Used sparingly, for extra files that need to be explicitly exported to dependers,
353 // or for phony files to minimize ninja.
354 Deps: f.deps,
355 // For exported generated headers, such as exported aidl headers, proto headers, or
356 // sysprop headers.
357 GeneratedHeaders: f.headers,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700358 })
Inseob Kim69378442019-06-03 19:10:47 +0900359}
360
Colin Crossb916a382016-07-29 17:28:03 -0700361// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
362// functionality: static vs. shared linkage, reusing object files for shared libraries
363type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800364 Properties LibraryProperties
Colin Crosse1bb5d02019-09-24 14:55:04 -0700365 StaticProperties StaticProperties
366 SharedProperties SharedProperties
Colin Crossa48ab5b2017-02-14 15:28:44 -0800367 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368
369 // For reusing static library objects for shared library
Inseob Kim69378442019-06-03 19:10:47 +0900370 reuseObjects Objects
Colin Cross10d22312017-05-03 11:01:58 -0700371
Colin Cross26c34ed2016-09-30 17:10:16 -0700372 // table-of-contents file to optimize out relinking when possible
373 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374
Colin Cross4d9c2d12016-07-29 12:48:20 -0700375 flagExporter
Liz Kammerb6a55bf2021-04-12 15:42:51 -0400376 flagExporterInfo *FlagExporterInfo
377 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379 // For whole_static_libs
Colin Crossa2bcf2c2022-02-11 13:11:55 -0800380 objects Objects
381 wholeStaticLibsFromPrebuilts android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382
383 // Uses the module's name if empty, but can be overridden. Does not include
384 // shlib suffix.
385 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700386
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800387 sabi *sabi
388
Dan Willemsen581341d2017-02-09 16:16:31 -0800389 // Output archive of gcno coverage information files
390 coverageOutputFile android.OptionalPath
391
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800392 // Source Abi Diff
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +0800393 sAbiDiff android.Paths
Mu-Le Lee0a9005e2022-07-05 09:49:50 +0000394
Colin Cross0875c522017-11-28 17:34:01 -0800395 // Location of the static library in the sysroot. Empty if the library is
396 // not included in the NDK.
397 ndkSysrootPath android.Path
398
Colin Crossb60190a2018-09-04 16:28:17 -0700399 // Location of the linked, unstripped library for shared libraries
400 unstrippedOutputFile android.Path
Wei Li5f5d2712023-12-11 15:40:29 -0800401 // Location of the linked, stripped library for shared libraries, strip: "all"
402 strippedAllOutputFile android.Path
Colin Crossb60190a2018-09-04 16:28:17 -0700403
Dan Willemsen569edc52018-11-19 09:33:29 -0800404 // Location of the file that should be copied to dist dir when requested
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000405 distFile android.Path
Dan Willemsen569edc52018-11-19 09:33:29 -0800406
Colin Cross8e21aa52020-09-28 18:28:02 -0700407 versionScriptPath android.OptionalPath
Jiyong Park7ed9de32018-10-15 22:25:07 +0900408
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800409 postInstallCmds []string
Jiyong Parkf1194352019-02-25 11:05:47 +0900410
Vic Yangefd249e2018-11-12 20:19:56 -0800411 // If useCoreVariant is true, the vendor variant of a VNDK library is
412 // not installed.
Vic Yangd92090f2020-01-08 14:32:28 -0800413 useCoreVariant bool
414 checkSameCoreVariant bool
Vic Yangefd249e2018-11-12 20:19:56 -0800415
Colin Cross5ec407b2020-09-30 11:41:33 -0700416 skipAPIDefine bool
417
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100418 // Decorated interfaces
Colin Crossb916a382016-07-29 17:28:03 -0700419 *baseCompiler
420 *baseLinker
421 *baseInstaller
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900422
sophiez4c4f8032021-08-16 22:54:00 -0700423 apiListCoverageXmlPath android.ModuleOutPath
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900424}
425
Chris Parsons3c27ca32020-11-20 12:42:07 -0500426// linkerProps returns the list of properties structs relevant for this library. (For example, if
427// the library is cc_shared_library, then static-library properties are omitted.)
Colin Crossb916a382016-07-29 17:28:03 -0700428func (library *libraryDecorator) linkerProps() []interface{} {
429 var props []interface{}
430 props = append(props, library.baseLinker.linkerProps()...)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700431 props = append(props,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700432 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800433 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700434 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700435 &library.stripper.StripProperties)
Colin Crosse1bb5d02019-09-24 14:55:04 -0700436
437 if library.MutatedProperties.BuildShared {
438 props = append(props, &library.SharedProperties)
439 }
440 if library.MutatedProperties.BuildStatic {
441 props = append(props, &library.StaticProperties)
442 }
443
444 return props
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445}
446
Chris Parsons3c27ca32020-11-20 12:42:07 -0500447// linkerFlags takes a Flags struct and augments it to contain linker flags that are defined by this
448// library, or that are implied by attributes of this library (such as whether this library is a
449// shared library).
Colin Crossb916a382016-07-29 17:28:03 -0700450func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700451 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700452
Colin Crossb916a382016-07-29 17:28:03 -0700453 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
454 // all code is position independent, and then those warnings get promoted to
455 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700456 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800457 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIC")
Colin Crossb916a382016-07-29 17:28:03 -0700458 }
459
460 if library.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800461 flags.Local.CFlags = append(flags.Local.CFlags, library.StaticProperties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800462 } else if library.shared() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800463 flags.Local.CFlags = append(flags.Local.CFlags, library.SharedProperties.Shared.Cflags...)
Colin Crossb916a382016-07-29 17:28:03 -0700464 }
465
Colin Crossa48ab5b2017-02-14 15:28:44 -0800466 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700467 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700469 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700470 f = append(f,
471 "-nostdlib",
472 "-Wl,--gc-sections",
473 )
474 }
475
476 if ctx.Darwin() {
477 f = append(f,
478 "-dynamiclib",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700479 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
480 )
Colin Cross7863cf52016-10-20 10:47:21 -0700481 if ctx.Arch().ArchType == android.X86 {
482 f = append(f,
483 "-read_only_relocs suppress",
484 )
485 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486 } else {
Josh Gao75a50a22019-06-07 17:58:59 -0700487 f = append(f, "-shared")
488 if !ctx.Windows() {
489 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
490 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491 }
492
Colin Cross4af21ed2019-11-04 09:37:55 -0800493 flags.Global.LdFlags = append(flags.Global.LdFlags, f...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494 }
495
496 return flags
497}
498
Chris Parsons3c27ca32020-11-20 12:42:07 -0500499// compilerFlags takes a Flags and augments it to contain compile flags from global values,
500// per-target values, module type values, per-module Blueprints properties, extra flags from
501// `flags`, and generated sources from `deps`.
Colin Crossf18e1102017-11-16 14:33:08 -0800502func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700503 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700504 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700505 f := includeDirsToFlags(exportIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800506 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
507 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700508 }
509
Jiyong Park7ed9de32018-10-15 22:25:07 +0900510 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
Colin Cross127bb8b2020-12-16 16:46:01 -0800511 if ctx.IsLlndk() {
512 // LLNDK libraries ignore most of the properties on the cc_library and use the
513 // LLNDK-specific properties instead.
514 // Wipe all the module-local properties, leaving only the global properties.
515 flags.Local = LocalOrGlobalFlags{}
516 }
Jiyong Park7ed9de32018-10-15 22:25:07 +0900517 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900518 // Remove -include <file> when compiling stubs. Otherwise, the force included
519 // headers might cause conflicting types error with the symbols in the
520 // generated stubs source code. e.g.
521 // double acos(double); // in header
522 // void acos() {} // in the generated source code
523 removeInclude := func(flags []string) []string {
524 ret := flags[:0]
525 for _, f := range flags {
526 if strings.HasPrefix(f, "-include ") {
527 continue
528 }
529 ret = append(ret, f)
530 }
531 return ret
532 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800533 flags.Local.CommonFlags = removeInclude(flags.Local.CommonFlags)
534 flags.Local.CFlags = removeInclude(flags.Local.CFlags)
Jiyong Park64379952018-12-13 18:37:29 +0900535
Jiyong Park7ed9de32018-10-15 22:25:07 +0900536 flags = addStubLibraryCompilerFlags(flags)
537 }
538 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700539}
540
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800541func (library *libraryDecorator) getHeaderAbiCheckerProperties(ctx android.BaseModuleContext) headerAbiCheckerProperties {
542 m := ctx.Module().(*Module)
543 variantProps := &library.Properties.Target.Platform.Header_abi_checker
544 if m.InVendor() {
545 variantProps = &library.Properties.Target.Vendor.Header_abi_checker
546 } else if m.InProduct() {
547 variantProps = &library.Properties.Target.Product.Header_abi_checker
548 }
549 props := library.Properties.Header_abi_checker
550 err := proptools.AppendProperties(&props, variantProps, nil)
551 if err != nil {
552 ctx.ModuleErrorf("Cannot merge headerAbiCheckerProperties: %s", err.Error())
553 }
554 return props
Logan Chien41eabe62019-04-10 13:33:58 +0800555}
556
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700557func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross127bb8b2020-12-16 16:46:01 -0800558 if ctx.IsLlndk() {
Jooyung Han33eb6152024-03-11 15:46:48 +0900559 vendorApiLevel := ctx.Config().VendorApiLevel()
560 if vendorApiLevel == "" {
561 // TODO(b/321892570): Some tests relying on old fixtures which
562 // doesn't set vendorApiLevel. Needs to fix them.
563 vendorApiLevel = ctx.Config().PlatformSdkVersion().String()
564 }
Colin Cross127bb8b2020-12-16 16:46:01 -0800565 // This is the vendor variant of an LLNDK library, build the LLNDK stubs.
Dan Albertf1d14c72020-07-30 14:32:55 -0700566 nativeAbiResult := parseNativeAbiDefinition(ctx,
567 String(library.Properties.Llndk.Symbol_file),
Jooyung Han33eb6152024-03-11 15:46:48 +0900568 android.ApiLevelOrPanic(ctx, vendorApiLevel), "--llndk")
Dan Albertf1d14c72020-07-30 14:32:55 -0700569 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
Colin Cross127bb8b2020-12-16 16:46:01 -0800570 if !Bool(library.Properties.Llndk.Unversioned) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700571 library.versionScriptPath = android.OptionalPathForPath(
572 nativeAbiResult.versionScript)
Colin Cross127bb8b2020-12-16 16:46:01 -0800573 }
574 return objs
575 }
Colin Cross5271fea2021-04-27 13:06:04 -0700576 if ctx.IsVendorPublicLibrary() {
Dan Albertf1d14c72020-07-30 14:32:55 -0700577 nativeAbiResult := parseNativeAbiDefinition(ctx,
578 String(library.Properties.Vendor_public_library.Symbol_file),
579 android.FutureApiLevel, "")
580 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
Colin Cross5271fea2021-04-27 13:06:04 -0700581 if !Bool(library.Properties.Vendor_public_library.Unversioned) {
Dan Albertf1d14c72020-07-30 14:32:55 -0700582 library.versionScriptPath = android.OptionalPathForPath(nativeAbiResult.versionScript)
Colin Cross5271fea2021-04-27 13:06:04 -0700583 }
584 return objs
585 }
Jiyong Park7ed9de32018-10-15 22:25:07 +0900586 if library.buildStubs() {
Jiyong Parka4175572021-02-04 21:05:47 +0900587 symbolFile := String(library.Properties.Stubs.Symbol_file)
588 if symbolFile != "" && !strings.HasSuffix(symbolFile, ".map.txt") {
589 ctx.PropertyErrorf("symbol_file", "%q doesn't have .map.txt suffix", symbolFile)
590 return Objects{}
591 }
Jiyong Park85cc35a2022-07-17 11:30:47 +0900592 // b/239274367 --apex and --systemapi filters symbols tagged with # apex and #
593 // systemapi, respectively. The former is for symbols defined in platform libraries
594 // and the latter is for symbols defined in APEXes.
Spandan Das934c97d2023-01-18 23:54:13 +0000595 // A single library can contain either # apex or # systemapi, but not both.
596 // The stub generator (ndkstubgen) is additive, so passing _both_ of these to it should be a no-op.
597 // However, having this distinction helps guard accidental
598 // promotion or demotion of API and also helps the API review process b/191371676
Jiyong Park85cc35a2022-07-17 11:30:47 +0900599 var flag string
600 if ctx.Module().(android.ApexModule).NotInPlatform() {
601 flag = "--apex"
602 } else {
Jiyong Parkffea9b32022-07-17 17:32:56 +0900603 flag = "--systemapi"
Jiyong Park85cc35a2022-07-17 11:30:47 +0900604 }
Jiyong Parkaa27c082022-09-26 21:16:09 +0900605 // b/184712170, unless the lib is an NDK library, exclude all public symbols from
606 // the stub so that it is mandated that all symbols are explicitly marked with
607 // either apex or systemapi.
608 if !ctx.Module().(*Module).IsNdk(ctx.Config()) {
609 flag = flag + " --no-ndk"
610 }
Dan Albertf1d14c72020-07-30 14:32:55 -0700611 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile,
Jiyong Park85cc35a2022-07-17 11:30:47 +0900612 android.ApiLevelOrPanic(ctx, library.MutatedProperties.StubsVersion), flag)
Dan Albertf1d14c72020-07-30 14:32:55 -0700613 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
614 library.versionScriptPath = android.OptionalPathForPath(
615 nativeAbiResult.versionScript)
sophiez4c4f8032021-08-16 22:54:00 -0700616
617 // Parse symbol file to get API list for coverage
Colin Crossceaa5322021-09-28 16:37:50 -0700618 if library.stubsVersion() == "current" && ctx.PrimaryArch() && !ctx.inRecovery() && !ctx.inProduct() && !ctx.inVendor() {
sophiez4c4f8032021-08-16 22:54:00 -0700619 library.apiListCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
620 }
621
Jiyong Park7ed9de32018-10-15 22:25:07 +0900622 return objs
623 }
624
Colin Cross5950f382016-12-13 12:50:57 -0800625 if !library.buildShared() && !library.buildStatic() {
626 if len(library.baseCompiler.Properties.Srcs) > 0 {
627 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
628 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700629 if len(library.StaticProperties.Static.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800630 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
631 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700632 if len(library.SharedProperties.Shared.Srcs) > 0 {
Colin Cross5950f382016-12-13 12:50:57 -0800633 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
634 }
635 return Objects{}
636 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800637 if library.sabi.shouldCreateSourceAbiDump() {
Hsin-Yi Chenaf369882024-03-29 20:10:36 +0800638 dirs := library.exportedIncludeDirsForAbiCheck(ctx)
639 flags.SAbiFlags = make([]string, 0, len(dirs))
640 for _, dir := range dirs {
641 flags.SAbiFlags = append(flags.SAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800642 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800643 totalLength := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) +
Colin Crosse1bb5d02019-09-24 14:55:04 -0700644 len(library.SharedProperties.Shared.Srcs) + len(library.StaticProperties.Static.Srcs)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800645 if totalLength > 0 {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800646 flags.SAbiDump = true
647 }
648 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700649 objs := library.baseCompiler.compile(ctx, flags, deps)
650 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700651 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700652
Colin Cross4d9c2d12016-07-29 12:48:20 -0700653 if library.static() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700654 srcs := android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Srcs)
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700655 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary, srcs,
656 android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_disabled_srcs),
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800657 android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_timeout_srcs),
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700658 library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800659 } else if library.shared() {
Colin Crosse1bb5d02019-09-24 14:55:04 -0700660 srcs := android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Srcs)
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700661 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary, srcs,
662 android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_disabled_srcs),
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800663 android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_timeout_srcs),
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700664 library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700665 }
666
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700667 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700668}
669
670type libraryInterface interface {
Colin Cross3572cf72020-10-01 15:58:11 -0700671 versionedInterface
672
Colin Crossb916a382016-07-29 17:28:03 -0700673 static() bool
Inseob Kimae553032019-05-14 18:52:49 +0900674 shared() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700675 objs() Objects
Colin Cross0de8a1e2020-09-18 14:15:30 -0700676 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700677 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700678
679 // Returns true if the build options for the module have selected a static or shared build
680 buildStatic() bool
681 buildShared() bool
682
683 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800684 setStatic()
685 setShared()
Logan Chien41eabe62019-04-10 13:33:58 +0800686
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800687 // Gets the ABI properties for vendor, product, or platform variant
688 getHeaderAbiCheckerProperties(ctx android.BaseModuleContext) headerAbiCheckerProperties
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800689
Logan Chien41eabe62019-04-10 13:33:58 +0800690 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
691 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
Jiyong Parka90ca002019-10-07 15:47:24 +0900692
693 availableFor(string) bool
Colin Crossceaa5322021-09-28 16:37:50 -0700694
695 getAPIListCoverageXMLPath() android.ModuleOutPath
Colin Cross1bc94122021-10-28 13:25:54 -0700696
697 installable() *bool
Colin Crossb916a382016-07-29 17:28:03 -0700698}
699
Colin Crossc88c2722020-09-28 17:32:47 -0700700type versionedInterface interface {
701 buildStubs() bool
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900702 setBuildStubs(isLatest bool)
Colin Crossc88c2722020-09-28 17:32:47 -0700703 hasStubsVariants() bool
Alan Stokes73feba32022-11-14 12:21:24 +0000704 isStubsImplementationRequired() bool
Colin Crossc88c2722020-09-28 17:32:47 -0700705 setStubsVersion(string)
706 stubsVersion() string
707
Colin Cross3572cf72020-10-01 15:58:11 -0700708 stubsVersions(ctx android.BaseMutatorContext) []string
Colin Crossc88c2722020-09-28 17:32:47 -0700709 setAllStubsVersions([]string)
710 allStubsVersions() []string
Colin Cross0477b422020-10-13 18:43:54 -0700711
712 implementationModuleName(name string) string
Colin Cross127bb8b2020-12-16 16:46:01 -0800713 hasLLNDKStubs() bool
Colin Cross1f3f1302021-04-26 18:37:44 -0700714 hasLLNDKHeaders() bool
Colin Cross5271fea2021-04-27 13:06:04 -0700715 hasVendorPublicLibrary() bool
Colin Crossc88c2722020-09-28 17:32:47 -0700716}
717
718var _ libraryInterface = (*libraryDecorator)(nil)
719var _ versionedInterface = (*libraryDecorator)(nil)
720
Justin Yun6977e8a2020-10-29 18:24:11 +0900721func (library *libraryDecorator) getLibNameHelper(baseModuleName string, inVendor bool, inProduct bool) string {
Colin Crossb916a382016-07-29 17:28:03 -0700722 name := library.libName
723 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100724 name = String(library.Properties.Stem)
725 if name == "" {
Jooyung Han0302a842019-10-30 18:43:49 +0900726 name = baseModuleName
dimitryd95964a2018-11-07 13:43:34 +0100727 }
Colin Crossb916a382016-07-29 17:28:03 -0700728 }
729
Colin Cross0fd6a412019-08-16 14:22:10 -0700730 suffix := ""
Justin Yun6977e8a2020-10-29 18:24:11 +0900731 if inVendor {
Colin Cross0fd6a412019-08-16 14:22:10 -0700732 suffix = String(library.Properties.Target.Vendor.Suffix)
Justin Yun6977e8a2020-10-29 18:24:11 +0900733 } else if inProduct {
734 suffix = String(library.Properties.Target.Product.Suffix)
Colin Cross0fd6a412019-08-16 14:22:10 -0700735 }
736 if suffix == "" {
737 suffix = String(library.Properties.Suffix)
738 }
739
Jooyung Han0302a842019-10-30 18:43:49 +0900740 return name + suffix
741}
742
Chris Parsons3c27ca32020-11-20 12:42:07 -0500743// getLibName returns the actual canonical name of the library (the name which
744// should be passed to the linker via linker flags).
Jooyung Han0302a842019-10-30 18:43:49 +0900745func (library *libraryDecorator) getLibName(ctx BaseModuleContext) string {
Justin Yun6977e8a2020-10-29 18:24:11 +0900746 name := library.getLibNameHelper(ctx.baseModuleName(), ctx.inVendor(), ctx.inProduct())
Colin Cross0fd6a412019-08-16 14:22:10 -0700747
Colin Crossb916a382016-07-29 17:28:03 -0700748 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
749 if !strings.HasSuffix(name, "-host") {
750 name = name + "-host"
751 }
752 }
753
Inseob Kim0ce291e2019-07-04 14:38:27 +0900754 return name
Colin Crossb916a382016-07-29 17:28:03 -0700755}
756
Jiyong Parkda732bd2018-11-02 18:23:15 +0900757var versioningMacroNamesListMutex sync.Mutex
758
Colin Crossb916a382016-07-29 17:28:03 -0700759func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
760 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700761 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700762 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700763 }
764 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700765 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900766 // Let baseLinker know whether this variant is for stubs or not, so that
767 // it can omit things that are not required for linking stubs.
768 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900769
770 if library.buildStubs() {
771 macroNames := versioningMacroNamesList(ctx.Config())
772 myName := versioningMacroName(ctx.ModuleName())
773 versioningMacroNamesListMutex.Lock()
774 defer versioningMacroNamesListMutex.Unlock()
775 if (*macroNames)[myName] == "" {
776 (*macroNames)[myName] = ctx.ModuleName()
777 } else if (*macroNames)[myName] != ctx.ModuleName() {
778 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
779 }
780 }
Colin Crossb916a382016-07-29 17:28:03 -0700781}
782
dimitry0345ad82018-12-05 16:28:14 +0100783func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross127bb8b2020-12-16 16:46:01 -0800784 if ctx.IsLlndk() {
785 // LLNDK libraries ignore most of the properties on the cc_library and use the
786 // LLNDK-specific properties instead.
787 return deps
788 }
789
dimitry0345ad82018-12-05 16:28:14 +0100790 deps = library.baseCompiler.compilerDeps(ctx, deps)
791
dimitry0345ad82018-12-05 16:28:14 +0100792 return deps
793}
794
Colin Cross37047f12016-12-13 17:06:13 -0800795func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross127bb8b2020-12-16 16:46:01 -0800796 if ctx.IsLlndk() {
797 // LLNDK libraries ignore most of the properties on the cc_library and use the
798 // LLNDK-specific properties instead.
Colin Cross0fb7fcd2021-03-02 11:00:07 -0800799 deps.HeaderLibs = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...)
800 deps.ReexportHeaderLibHeaders = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...)
Colin Cross127bb8b2020-12-16 16:46:01 -0800801 return deps
802 }
Colin Cross5271fea2021-04-27 13:06:04 -0700803 if ctx.IsVendorPublicLibrary() {
804 headers := library.Properties.Vendor_public_library.Export_public_headers
805 deps.HeaderLibs = append([]string(nil), headers...)
806 deps.ReexportHeaderLibHeaders = append([]string(nil), headers...)
807 return deps
808 }
Colin Cross127bb8b2020-12-16 16:46:01 -0800809
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800810 if library.static() {
Martin Stjernholm10566a02020-03-24 01:19:52 +0000811 // Compare with nil because an empty list needs to be propagated.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700812 if library.StaticProperties.Static.System_shared_libs != nil {
813 library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800814 }
815 } else if library.shared() {
Martin Stjernholm10566a02020-03-24 01:19:52 +0000816 // Compare with nil because an empty list needs to be propagated.
Colin Crosse1bb5d02019-09-24 14:55:04 -0700817 if library.SharedProperties.Shared.System_shared_libs != nil {
818 library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800819 }
820 }
821
Colin Crossb916a382016-07-29 17:28:03 -0700822 deps = library.baseLinker.linkerDeps(ctx, deps)
823
824 if library.static() {
825 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
Colin Crosse1bb5d02019-09-24 14:55:04 -0700826 library.StaticProperties.Static.Whole_static_libs...)
827 deps.StaticLibs = append(deps.StaticLibs, library.StaticProperties.Static.Static_libs...)
828 deps.SharedLibs = append(deps.SharedLibs, library.StaticProperties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800829
Colin Crosse1bb5d02019-09-24 14:55:04 -0700830 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...)
831 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800832 } else if library.shared() {
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000833 if library.baseLinker.Properties.crt() {
Colin Crossd1a28132021-06-21 17:34:47 -0700834 deps.CrtBegin = append(deps.CrtBegin, ctx.toolchain().CrtBeginSharedLibrary()...)
835 deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtEndSharedLibrary()...)
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -0800836
837 }
838 if library.baseLinker.Properties.crtPadSegment() {
839 deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtPadSegmentSharedLibrary()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700840 }
Colin Crosse1bb5d02019-09-24 14:55:04 -0700841 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.SharedProperties.Shared.Whole_static_libs...)
842 deps.StaticLibs = append(deps.StaticLibs, library.SharedProperties.Shared.Static_libs...)
843 deps.SharedLibs = append(deps.SharedLibs, library.SharedProperties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800844
Colin Crosse1bb5d02019-09-24 14:55:04 -0700845 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.SharedProperties.Shared.Export_shared_lib_headers...)
846 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.SharedProperties.Shared.Export_static_lib_headers...)
Hsin-Yi Chen715142a2024-03-27 16:31:16 +0800847
848 deps.LlndkHeaderLibs = append(deps.LlndkHeaderLibs, library.Properties.Llndk.Export_llndk_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700849 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900850 if ctx.inVendor() {
Jiyong Park52d25bd2017-10-13 09:17:01 +0900851 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
852 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
853 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000854 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
855 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900856 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900857 if ctx.inProduct() {
858 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
859 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
860 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
861 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
862 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
863 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900864 if ctx.inRecovery() {
865 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
866 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
867 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000868 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
869 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900870 }
Yifan Hongcf4832c2020-01-21 17:04:13 -0800871 if ctx.inRamdisk() {
872 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
873 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
874 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
875 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
876 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
877 }
Yifan Hong6da33c22020-10-27 15:01:21 -0700878 if ctx.inVendorRamdisk() {
879 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
880 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
881 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
882 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
883 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
884 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800885
Colin Cross4d9c2d12016-07-29 12:48:20 -0700886 return deps
887}
888
Paul Duffin13f02712020-03-06 12:30:43 +0000889func (library *libraryDecorator) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
890 specifiedDeps = library.baseLinker.linkerSpecifiedDeps(specifiedDeps)
891 var properties StaticOrSharedProperties
892 if library.static() {
893 properties = library.StaticProperties.Static
894 } else if library.shared() {
895 properties = library.SharedProperties.Shared
896 }
897
898 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, properties.Shared_libs...)
Martin Stjernholm10566a02020-03-24 01:19:52 +0000899
900 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
901 // either input list doesn't come out as nil.
902 if specifiedDeps.systemSharedLibs == nil {
903 specifiedDeps.systemSharedLibs = properties.System_shared_libs
904 } else {
905 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, properties.System_shared_libs...)
906 }
Paul Duffin13f02712020-03-06 12:30:43 +0000907
908 specifiedDeps.sharedLibs = android.FirstUniqueStrings(specifiedDeps.sharedLibs)
Martin Stjernholm10566a02020-03-24 01:19:52 +0000909 if len(specifiedDeps.systemSharedLibs) > 0 {
910 // Skip this if systemSharedLibs is either nil or [], to ensure they are
911 // retained.
912 specifiedDeps.systemSharedLibs = android.FirstUniqueStrings(specifiedDeps.systemSharedLibs)
913 }
Paul Duffin13f02712020-03-06 12:30:43 +0000914 return specifiedDeps
915}
916
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800917func (library *libraryDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
918 if library.static() {
919 moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"}
920 moduleInfoJSON.Uninstallable = true
921 } else if library.shared() {
922 moduleInfoJSON.Class = []string{"SHARED_LIBRARIES"}
923 } else if library.header() {
924 moduleInfoJSON.Class = []string{"HEADER_LIBRARIES"}
925 moduleInfoJSON.Uninstallable = true
926 }
927
928 if library.buildStubs() && library.stubsVersion() != "" {
929 moduleInfoJSON.SubName += "." + library.stubsVersion()
930 }
931
932 // If a library providing a stub is included in an APEX, the private APIs of the library
933 // is accessible only inside the APEX. From outside of the APEX, clients can only use the
934 // public APIs via the stub. To enforce this, the (latest version of the) stub gets the
935 // name of the library. The impl library instead gets the `.bootstrap` suffix to so that
936 // they can be exceptionally used directly when APEXes are not available (e.g. during the
937 // very early stage in the boot process).
938 if len(library.Properties.Stubs.Versions) > 0 && !ctx.Host() && ctx.notInPlatform() &&
939 !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && !ctx.useVndk() && !ctx.static() {
940 if library.buildStubs() && library.isLatestStubVersion() {
941 moduleInfoJSON.SubName = ""
942 }
943 if !library.buildStubs() {
944 moduleInfoJSON.SubName = ".bootstrap"
945 }
946 }
947
948 library.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON)
949}
950
Colin Crossb916a382016-07-29 17:28:03 -0700951func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700952 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700953
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700954 library.objects = deps.WholeStaticLibObjs.Copy()
955 library.objects = library.objects.Append(objs)
Colin Crossa2bcf2c2022-02-11 13:11:55 -0800956 library.wholeStaticLibsFromPrebuilts = android.CopyOfPaths(deps.WholeStaticLibsFromPrebuilts)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700957
Inseob Kim0ce291e2019-07-04 14:38:27 +0900958 fileName := ctx.ModuleName() + staticLibraryExtension
Colin Cross86803cf2018-02-15 14:12:26 -0800959 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800960 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700961
Dan Willemsen569edc52018-11-19 09:33:29 -0800962 if Bool(library.baseLinker.Properties.Use_version_lib) {
963 if ctx.Host() {
964 versionedOutputFile := outputFile
965 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
966 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
967 } else {
968 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000969 library.distFile = versionedOutputFile
Dan Willemsen569edc52018-11-19 09:33:29 -0800970 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
971 }
Colin Cross86803cf2018-02-15 14:12:26 -0800972 }
973
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800974 transformObjToStaticLib(ctx, library.objects.objFiles, deps.WholeStaticLibsFromPrebuilts, builderFlags, outputFile, nil, objs.tidyDepFiles)
Dan Willemsen581341d2017-02-09 16:16:31 -0800975
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500976 library.coverageOutputFile = transformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700977
Colin Cross4d9c2d12016-07-29 12:48:20 -0700978 ctx.CheckbuildFile(outputFile)
979
Colin Cross649d8172020-12-10 12:30:21 -0800980 if library.static() {
Colin Cross40213022023-12-13 15:19:49 -0800981 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Crossa2bcf2c2022-02-11 13:11:55 -0800982 StaticLibrary: outputFile,
983 ReuseObjects: library.reuseObjects,
984 Objects: library.objects,
985 WholeStaticLibsFromPrebuilts: library.wholeStaticLibsFromPrebuilts,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700986
Colin Crossc85750b2022-04-21 12:50:51 -0700987 TransitiveStaticLibrariesForOrdering: android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
Colin Cross649d8172020-12-10 12:30:21 -0800988 Direct(outputFile).
989 Transitive(deps.TranstiveStaticLibrariesForOrdering).
990 Build(),
991 })
992 }
993
994 if library.header() {
Colin Cross40213022023-12-13 15:19:49 -0800995 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800996 }
Colin Cross0de8a1e2020-09-18 14:15:30 -0700997
Colin Cross4d9c2d12016-07-29 12:48:20 -0700998 return outputFile
999}
1000
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -08001001func ndkSharedLibDeps(ctx ModuleContext) android.Paths {
1002 if ctx.Module().(*Module).IsSdkVariant() {
1003 // The NDK sysroot timestamp file depends on all the NDK
1004 // sysroot header and shared library files.
1005 return android.Paths{getNdkBaseTimestampFile(ctx)}
1006 }
1007 return nil
1008}
1009
Colin Crossb916a382016-07-29 17:28:03 -07001010func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001011 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001012
1013 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001014 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Chih-Hung Hsiehf6ca1b92021-12-05 18:02:50 -08001015 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001016
Colin Cross2383f3b2018-02-06 14:40:13 -08001017 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
1018 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
1019 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -07001020 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001021 if unexportedSymbols.Valid() {
1022 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1023 }
1024 if forceNotWeakSymbols.Valid() {
1025 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1026 }
1027 if forceWeakSymbols.Valid() {
1028 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1029 }
1030 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001031 if unexportedSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001032 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001033 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
1034 }
1035 if forceNotWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001036 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001037 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
1038 }
1039 if forceWeakSymbols.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -08001040 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Colin Cross4d9c2d12016-07-29 12:48:20 -07001041 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
1042 }
1043 }
Colin Cross8e21aa52020-09-28 18:28:02 -07001044 if library.versionScriptPath.Valid() {
Jiyong Parkc1e7f482019-01-12 13:39:10 +09001045 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -08001046 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlags)
Colin Cross8e21aa52020-09-28 18:28:02 -07001047 linkerDeps = append(linkerDeps, library.versionScriptPath.Path())
Jiyong Parkc1e7f482019-01-12 13:39:10 +09001048 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001049
1050 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
1051 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001052 unstrippedOutputFile := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -07001053
Josh Gao75a50a22019-06-07 17:58:59 -07001054 var implicitOutputs android.WritablePaths
1055 if ctx.Windows() {
1056 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
1057
Colin Cross4af21ed2019-11-04 09:37:55 -08001058 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
Josh Gao75a50a22019-06-07 17:58:59 -07001059 implicitOutputs = append(implicitOutputs, importLibraryPath)
1060 }
1061
Colin Cross4d9c2d12016-07-29 12:48:20 -07001062 builderFlags := flagsToBuilderFlags(flags)
1063
Dan Willemsen47450072021-10-19 20:24:49 -07001064 if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
1065 fatOutputFile := outputFile
1066 outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
1067 transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
1068 }
1069
Colin Crossb496cfd2018-09-10 16:50:05 -07001070 // Optimize out relinking against shared libraries whose interface hasn't changed by
1071 // depending on a table of contents file instead of the library itself.
Colin Cross70dda7e2019-10-01 22:05:35 -07001072 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.ShlibSuffix()[1:]+".toc")
Colin Crossb496cfd2018-09-10 16:50:05 -07001073 library.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -04001074 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross89562dc2016-10-03 17:47:19 -07001075
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001076 stripFlags := flagsToStripFlags(flags)
Colin Crossadc81a02020-12-14 17:01:55 -08001077 needsStrip := library.stripper.NeedsStrip(ctx)
1078 if library.buildStubs() {
1079 // No need to strip stubs libraries
1080 needsStrip = false
1081 }
1082 if needsStrip {
Yi Kongb5c34d72018-11-07 16:28:49 -08001083 if ctx.Darwin() {
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001084 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -08001085 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001086 strippedOutputFile := outputFile
1087 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001088 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001089 }
Colin Crossb60190a2018-09-04 16:28:17 -07001090 library.unstrippedOutputFile = outputFile
1091
Colin Crossd7227f92019-09-05 14:26:33 -07001092 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, library.Properties.Inject_bssl_hash, fileName)
Pete Bentley803e1612019-08-06 22:19:59 +01001093
Dan Willemsen569edc52018-11-19 09:33:29 -08001094 if Bool(library.baseLinker.Properties.Use_version_lib) {
1095 if ctx.Host() {
1096 versionedOutputFile := outputFile
1097 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
1098 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1099 } else {
1100 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001101 library.distFile = versionedOutputFile
Dan Willemsen569edc52018-11-19 09:33:29 -08001102
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001103 if library.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -08001104 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001105 library.distFile = out
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001106 library.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -08001107 }
1108
1109 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1110 }
Colin Cross86803cf2018-02-15 14:12:26 -08001111 }
1112
Wei Li5f5d2712023-12-11 15:40:29 -08001113 // Generate an output file for dist as if strip: "all" is set on the module.
1114 // Currently this is for layoutlib release process only.
1115 for _, dist := range ctx.Module().(*Module).Dists() {
1116 if dist.Tag != nil && *dist.Tag == "stripped_all" {
1117 strippedAllOutputFile := android.PathForModuleOut(ctx, "stripped_all", fileName)
1118 transformStrip(ctx, outputFile, strippedAllOutputFile, StripFlags{Toolchain: flags.Toolchain})
1119 library.strippedAllOutputFile = strippedAllOutputFile
1120 break
1121 }
1122 }
1123
Jiyong Park64a44f22019-01-18 14:37:08 +09001124 sharedLibs := deps.EarlySharedLibs
1125 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001126 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1127
Jiyong Park64a44f22019-01-18 14:37:08 +09001128 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -07001129 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
1130 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001131 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001132 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -08001133 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs, objs.tidyDepFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001134
Dan Willemsen581341d2017-02-09 16:16:31 -08001135 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
1136 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001137 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
1138 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
1139
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001140 library.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001141 library.linkSAbiDumpFiles(ctx, deps, objs, fileName, unstrippedOutputFile)
Dan Willemsen581341d2017-02-09 16:16:31 -08001142
Colin Crossc85750b2022-04-21 12:50:51 -07001143 var transitiveStaticLibrariesForOrdering *android.DepSet[android.Path]
Colin Cross0de8a1e2020-09-18 14:15:30 -07001144 if static := ctx.GetDirectDepsWithTag(staticVariantTag); len(static) > 0 {
Colin Cross313aa542023-12-13 13:47:44 -08001145 s, _ := android.OtherModuleProvider(ctx, static[0], StaticLibraryInfoProvider)
Liz Kammeref6dfea2021-06-08 15:37:09 -04001146 transitiveStaticLibrariesForOrdering = s.TransitiveStaticLibrariesForOrdering
Colin Cross0de8a1e2020-09-18 14:15:30 -07001147 }
1148
Colin Cross40213022023-12-13 15:19:49 -08001149 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -04001150 TableOfContents: android.OptionalPathForPath(tocFile),
1151 SharedLibrary: unstrippedOutputFile,
1152 TransitiveStaticLibrariesForOrdering: transitiveStaticLibrariesForOrdering,
1153 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -07001154 })
1155
Sam Delmerico75dbca22023-04-20 13:13:25 +00001156 addStubDependencyProviders(ctx)
1157
1158 return unstrippedOutputFile
1159}
1160
1161func addStubDependencyProviders(ctx ModuleContext) {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001162 stubs := ctx.GetDirectDepsWithTag(stubImplDepTag)
1163 if len(stubs) > 0 {
Chris Parsons3c27ca32020-11-20 12:42:07 -05001164 var stubsInfo []SharedStubLibrary
Colin Cross0de8a1e2020-09-18 14:15:30 -07001165 for _, stub := range stubs {
Colin Cross313aa542023-12-13 13:47:44 -08001166 stubInfo, _ := android.OtherModuleProvider(ctx, stub, SharedLibraryInfoProvider)
1167 flagInfo, _ := android.OtherModuleProvider(ctx, stub, FlagExporterInfoProvider)
Chris Parsons3c27ca32020-11-20 12:42:07 -05001168 stubsInfo = append(stubsInfo, SharedStubLibrary{
Colin Cross31076b32020-10-23 17:22:06 -07001169 Version: moduleLibraryInterface(stub).stubsVersion(),
Colin Cross0de8a1e2020-09-18 14:15:30 -07001170 SharedLibraryInfo: stubInfo,
1171 FlagExporterInfo: flagInfo,
1172 })
1173 }
Colin Cross40213022023-12-13 15:19:49 -08001174 android.SetProvider(ctx, SharedLibraryStubsProvider, SharedLibraryStubsInfo{
Chris Parsons3c27ca32020-11-20 12:42:07 -05001175 SharedStubLibraries: stubsInfo,
Sam Delmerico75dbca22023-04-20 13:13:25 +00001176 IsLLNDK: ctx.IsLlndk(),
Colin Cross0de8a1e2020-09-18 14:15:30 -07001177 })
1178 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001179}
1180
Jiyong Parkaf6d8952019-01-31 12:21:23 +09001181func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
1182 return library.unstrippedOutputFile
1183}
1184
Wei Li5f5d2712023-12-11 15:40:29 -08001185func (library *libraryDecorator) strippedAllOutputFilePath() android.Path {
1186 return library.strippedAllOutputFile
1187}
1188
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001189func (library *libraryDecorator) disableStripping() {
1190 library.stripper.StripProperties.Strip.None = BoolPtr(true)
1191}
1192
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -07001193func (library *libraryDecorator) nativeCoverage() bool {
1194 if library.header() || library.buildStubs() {
1195 return false
1196 }
1197 return true
1198}
1199
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001200func (library *libraryDecorator) coverageOutputFilePath() android.OptionalPath {
1201 return library.coverageOutputFile
1202}
1203
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001204func (library *libraryDecorator) exportedIncludeDirsForAbiCheck(ctx ModuleContext) []string {
1205 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx).Strings()
1206 exportIncludeDirs = append(exportIncludeDirs, library.sabi.Properties.ReexportedIncludes...)
1207 return exportIncludeDirs
1208}
1209
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001210func (library *libraryDecorator) llndkIncludeDirsForAbiCheck(ctx ModuleContext, deps PathDeps) []string {
1211 // The ABI checker does not need the preprocess which adds macro guards to function declarations.
1212 includeDirs := android.PathsForModuleSrc(ctx, library.Properties.Llndk.Export_preprocessed_headers).Strings()
1213
1214 if library.Properties.Llndk.Override_export_include_dirs != nil {
1215 includeDirs = append(includeDirs, android.PathsForModuleSrc(
1216 ctx, library.Properties.Llndk.Override_export_include_dirs).Strings()...)
1217 } else {
1218 includeDirs = append(includeDirs, library.flagExporter.exportedIncludes(ctx).Strings()...)
1219 // Ignore library.sabi.Properties.ReexportedIncludes because
1220 // LLNDK does not reexport the implementation's dependencies, such as export_header_libs.
1221 }
1222
1223 systemIncludeDirs := []string{}
1224 if Bool(library.Properties.Llndk.Export_headers_as_system) {
1225 systemIncludeDirs = append(systemIncludeDirs, includeDirs...)
1226 includeDirs = nil
1227 }
1228 // Header libs.
1229 includeDirs = append(includeDirs, deps.LlndkIncludeDirs.Strings()...)
1230 systemIncludeDirs = append(systemIncludeDirs, deps.LlndkSystemIncludeDirs.Strings()...)
1231 // The ABI checker does not distinguish normal and system headers.
1232 return append(includeDirs, systemIncludeDirs...)
1233}
1234
1235func (library *libraryDecorator) linkLlndkSAbiDumpFiles(ctx ModuleContext,
1236 deps PathDeps, sAbiDumpFiles android.Paths, soFile android.Path, libFileName string,
1237 excludeSymbolVersions, excludeSymbolTags []string) android.Path {
1238 // NDK symbols in version 34 are LLNDK symbols. Those in version 35 are not.
1239 // TODO(b/314010764): Add parameters to read LLNDK symbols from the symbol file.
1240 return transformDumpToLinkedDump(ctx,
1241 sAbiDumpFiles, soFile, libFileName+".llndk",
1242 library.llndkIncludeDirsForAbiCheck(ctx, deps),
1243 android.OptionalPathForModuleSrc(ctx, library.Properties.Llndk.Symbol_file),
1244 append([]string{"*_PLATFORM", "*_PRIVATE"}, excludeSymbolVersions...),
1245 append([]string{"platform-only"}, excludeSymbolTags...),
1246 "34")
1247}
1248
Hsin-Yi Chen0af4e672022-11-23 14:39:46 +08001249func getRefAbiDumpFile(ctx android.ModuleInstallPathContext,
1250 versionedDumpDir, fileName string) android.OptionalPath {
Mu-Le Leef5ed30b2022-08-30 10:37:21 +00001251
1252 currentArchType := ctx.Arch().ArchType
1253 primaryArchType := ctx.Config().DevicePrimaryArchType()
1254 archName := currentArchType.String()
1255 if currentArchType != primaryArchType {
1256 archName += "_" + primaryArchType.String()
1257 }
1258
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001259 return android.ExistentPathForSource(ctx, versionedDumpDir, archName, "source-based",
Hsin-Yi Chen0af4e672022-11-23 14:39:46 +08001260 fileName+".lsdump")
Mu-Le Leef5ed30b2022-08-30 10:37:21 +00001261}
1262
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001263// Return the previous and current SDK versions for cross-version ABI diff.
1264func crossVersionAbiDiffSdkVersions(ctx ModuleContext, dumpDir string) (string, string) {
Mu-Le Lee0a9005e2022-07-05 09:49:50 +00001265 sdkVersionInt := ctx.Config().PlatformSdkVersion().FinalInt()
1266 sdkVersionStr := ctx.Config().PlatformSdkVersion().String()
1267
1268 if ctx.Config().PlatformSdkFinal() {
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001269 return strconv.Itoa(sdkVersionInt - 1), sdkVersionStr
Mu-Le Lee0a9005e2022-07-05 09:49:50 +00001270 } else {
Mu-Le Lee0a9005e2022-07-05 09:49:50 +00001271 // The platform SDK version can be upgraded before finalization while the corresponding abi dumps hasn't
1272 // been generated. Thus the Cross-Version Check chooses PLATFORM_SDK_VERION - 1 as previous version.
1273 // This situation could be identified by checking the existence of the PLATFORM_SDK_VERION dump directory.
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001274 versionedDumpDir := android.ExistentPathForSource(ctx, dumpDir, sdkVersionStr)
1275 if versionedDumpDir.Valid() {
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001276 return sdkVersionStr, strconv.Itoa(sdkVersionInt + 1)
Mu-Le Lee0a9005e2022-07-05 09:49:50 +00001277 } else {
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001278 return strconv.Itoa(sdkVersionInt - 1), sdkVersionStr
Mu-Le Lee0a9005e2022-07-05 09:49:50 +00001279 }
1280 }
1281}
1282
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001283// Return the SDK version for same-version ABI diff.
1284func currRefAbiDumpSdkVersion(ctx ModuleContext) string {
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +08001285 if ctx.Config().PlatformSdkFinal() {
Hsin-Yi Chena90bd382022-11-10 16:56:27 +08001286 // After sdk finalization, the ABI of the latest API level must be consistent with the source code,
1287 // so choose PLATFORM_SDK_VERSION as the current version.
1288 return ctx.Config().PlatformSdkVersion().String()
1289 } else {
1290 return "current"
1291 }
1292}
1293
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001294// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.lsdump).
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001295func (library *libraryDecorator) sourceAbiDiff(ctx android.ModuleContext,
1296 sourceDump, referenceDump android.Path,
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001297 baseName, nameExt string, isLlndkOrNdk, allowExtensions bool,
1298 sourceVersion, errorMessage string) {
1299
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001300 extraFlags := []string{"-target-version", sourceVersion}
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +08001301 headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx)
1302 if Bool(headerAbiChecker.Check_all_apis) {
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001303 extraFlags = append(extraFlags, "-check-all-apis")
1304 } else {
1305 extraFlags = append(extraFlags,
1306 "-allow-unreferenced-changes",
1307 "-allow-unreferenced-elf-symbol-changes")
1308 }
1309 if isLlndkOrNdk {
1310 extraFlags = append(extraFlags, "-consider-opaque-types-different")
1311 }
1312 if allowExtensions {
1313 extraFlags = append(extraFlags, "-allow-extensions")
1314 }
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +08001315 extraFlags = append(extraFlags, headerAbiChecker.Diff_flags...)
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001316
1317 library.sAbiDiff = append(
1318 library.sAbiDiff,
1319 transformAbiDumpToAbiDiff(ctx, sourceDump, referenceDump,
1320 baseName, nameExt, extraFlags, errorMessage))
1321}
1322
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001323func (library *libraryDecorator) crossVersionAbiDiff(ctx android.ModuleContext,
1324 sourceDump, referenceDump android.Path,
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001325 baseName string, isLlndkOrNdk bool, sourceVersion, prevVersion string) {
1326
Elliott Hughes10363162024-01-09 22:02:03 +00001327 errorMessage := "error: Please follow https://android.googlesource.com/platform/development/+/main/vndk/tools/header-checker/README.md#configure-cross_version-abi-check to resolve the ABI difference between your source code and version " + prevVersion + "."
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001328
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001329 library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, prevVersion,
Sasha Smundak39a301c2022-12-29 17:11:49 -08001330 isLlndkOrNdk, true /* allowExtensions */, sourceVersion, errorMessage)
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001331}
1332
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001333func (library *libraryDecorator) sameVersionAbiDiff(ctx android.ModuleContext,
1334 sourceDump, referenceDump android.Path,
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001335 baseName, nameExt string, isLlndkOrNdk bool) {
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001336
1337 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
1338 errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l " + libName
1339
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001340 library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, nameExt,
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +08001341 isLlndkOrNdk, false /* allowExtensions */, "current", errorMessage)
Hsin-Yi Chena6ddb142022-10-27 14:55:42 +08001342}
1343
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001344func (library *libraryDecorator) optInAbiDiff(ctx android.ModuleContext,
1345 sourceDump, referenceDump android.Path,
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001346 baseName, nameExt string, refDumpDir string) {
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001347
1348 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
1349 errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l " + libName + " -ref-dump-dir $$ANDROID_BUILD_TOP/" + refDumpDir
Hsin-Yi Chen8b85d812023-06-29 16:51:27 +08001350 // Most opt-in libraries do not have dumps for all default architectures.
1351 if ctx.Config().HasDeviceProduct() {
1352 errorMessage += " -products " + ctx.Config().DeviceProduct()
1353 }
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001354
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001355 library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, nameExt,
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001356 false /* isLlndkOrNdk */, false /* allowExtensions */, "current", errorMessage)
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001357}
1358
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001359func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, deps PathDeps, objs Objects, fileName string, soFile android.Path) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +08001360 if library.sabi.shouldCreateSourceAbiDump() {
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001361 exportedIncludeDirs := library.exportedIncludeDirsForAbiCheck(ctx)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +08001362 headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx)
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001363 currSdkVersion := currRefAbiDumpSdkVersion(ctx)
1364 currVendorVersion := ctx.Config().VendorApiLevel()
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001365
1366 // Generate source dumps.
1367 implDump := transformDumpToLinkedDump(ctx,
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001368 objs.sAbiDumpFiles, soFile, fileName,
1369 exportedIncludeDirs,
Hsin-Yi Chen843c0632023-09-25 15:26:30 +08001370 android.OptionalPathForModuleSrc(ctx, library.symbolFileForAbiCheck(ctx)),
1371 headerAbiChecker.Exclude_symbol_versions,
1372 headerAbiChecker.Exclude_symbol_tags,
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001373 currSdkVersion)
Hsin-Yi Chen843c0632023-09-25 15:26:30 +08001374
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001375 var llndkDump android.Path
1376 tags := classifySourceAbiDump(ctx)
1377 for _, tag := range tags {
1378 if tag == llndkLsdumpTag {
1379 if llndkDump == nil {
1380 // TODO(b/323447559): Evaluate if replacing sAbiDumpFiles with implDump is faster
1381 llndkDump = library.linkLlndkSAbiDumpFiles(ctx,
1382 deps, objs.sAbiDumpFiles, soFile, fileName,
1383 headerAbiChecker.Exclude_symbol_versions,
1384 headerAbiChecker.Exclude_symbol_tags)
1385 }
1386 addLsdumpPath(string(tag) + ":" + llndkDump.String())
1387 } else {
1388 addLsdumpPath(string(tag) + ":" + implDump.String())
1389 }
1390 }
1391
1392 // Diff source dumps and reference dumps.
1393 for _, tag := range tags {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001394 dumpDirName := tag.dirName()
1395 if dumpDirName == "" {
1396 continue
1397 }
1398 dumpDir := filepath.Join("prebuilts", "abi-dumps", dumpDirName)
1399 isLlndk := (tag == llndkLsdumpTag)
1400 isNdk := (tag == ndkLsdumpTag)
1401 binderBitness := ctx.DeviceConfig().BinderBitness()
1402 nameExt := ""
1403 if isLlndk {
1404 nameExt = "llndk"
1405 }
1406 // Check against the previous version.
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001407 var prevVersion, currVersion string
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001408 sourceDump := implDump
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001409 // If this release config does not define VendorApiLevel, fall back to the old policy.
1410 if isLlndk && currVendorVersion != "" {
1411 prevVersion = ctx.Config().PrevVendorApiLevel()
1412 currVersion = currVendorVersion
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001413 // LLNDK dumps are generated by different rules after trunk stable.
1414 if android.IsTrunkStableVendorApiLevel(prevVersion) {
1415 sourceDump = llndkDump
1416 }
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001417 } else {
1418 prevVersion, currVersion = crossVersionAbiDiffSdkVersions(ctx, dumpDir)
1419 }
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001420 prevDumpDir := filepath.Join(dumpDir, prevVersion, binderBitness)
1421 prevDumpFile := getRefAbiDumpFile(ctx, prevDumpDir, fileName)
1422 if prevDumpFile.Valid() {
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001423 library.crossVersionAbiDiff(ctx, sourceDump, prevDumpFile.Path(),
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001424 fileName, isLlndk || isNdk, currVersion, nameExt+prevVersion)
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001425 }
1426 // Check against the current version.
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001427 sourceDump = implDump
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001428 if isLlndk && currVendorVersion != "" {
1429 currVersion = currVendorVersion
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001430 if android.IsTrunkStableVendorApiLevel(currVersion) {
1431 sourceDump = llndkDump
1432 }
Hsin-Yi Chen2c4a9772024-02-05 17:37:27 +08001433 } else {
1434 currVersion = currSdkVersion
1435 }
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001436 currDumpDir := filepath.Join(dumpDir, currVersion, binderBitness)
1437 currDumpFile := getRefAbiDumpFile(ctx, currDumpDir, fileName)
1438 if currDumpFile.Valid() {
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001439 library.sameVersionAbiDiff(ctx, sourceDump, currDumpFile.Path(),
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001440 fileName, nameExt, isLlndk || isNdk)
1441 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001442 }
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001443 // Check against the opt-in reference dumps.
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +08001444 for i, optInDumpDir := range headerAbiChecker.Ref_dump_dirs {
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001445 optInDumpDirPath := android.PathForModuleSrc(ctx, optInDumpDir)
1446 // Ref_dump_dirs are not versioned.
1447 // They do not contain subdir for binder bitness because 64-bit binder has been mandatory.
1448 optInDumpFile := getRefAbiDumpFile(ctx, optInDumpDirPath.String(), fileName)
1449 if !optInDumpFile.Valid() {
1450 continue
1451 }
Hsin-Yi Chenaf369882024-03-29 20:10:36 +08001452 library.optInAbiDiff(ctx,
Hsin-Yi Chen64b2d032024-03-29 19:12:35 +08001453 implDump, optInDumpFile.Path(),
Hsin-Yi Chen362c1882024-02-06 15:43:17 +08001454 fileName, "opt"+strconv.Itoa(i), optInDumpDirPath.String())
Hsin-Yi Chenf3630122022-11-17 14:29:43 +08001455 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001456 }
1457}
1458
Inseob Kimcc12d902021-05-14 20:15:55 +09001459func processLLNDKHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) (timestamp android.Path, installPaths android.WritablePaths) {
Colin Cross127bb8b2020-12-16 16:46:01 -08001460 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
1461 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
1462
Colin Cross127bb8b2020-12-16 16:46:01 -08001463 for _, header := range srcFiles {
1464 headerDir := filepath.Dir(header.String())
1465 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
1466 if err != nil {
1467 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
1468 srcDir.String(), headerDir, err)
1469 continue
1470 }
1471
1472 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
1473 }
1474
Inseob Kimcc12d902021-05-14 20:15:55 +09001475 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths), installPaths
Colin Cross127bb8b2020-12-16 16:46:01 -08001476}
1477
Chris Parsons3c27ca32020-11-20 12:42:07 -05001478// link registers actions to link this library, and sets various fields
1479// on this library to reflect information that should be exported up the build
1480// tree (for example, exported flags and include paths).
Colin Crossb916a382016-07-29 17:28:03 -07001481func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001482 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001483
Colin Cross127bb8b2020-12-16 16:46:01 -08001484 if ctx.IsLlndk() {
1485 if len(library.Properties.Llndk.Export_preprocessed_headers) > 0 {
1486 // This is the vendor variant of an LLNDK library with preprocessed headers.
1487 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
1488
1489 var timestampFiles android.Paths
1490 for _, dir := range library.Properties.Llndk.Export_preprocessed_headers {
Inseob Kimcc12d902021-05-14 20:15:55 +09001491 timestampFile, installPaths := processLLNDKHeaders(ctx, dir, genHeaderOutDir)
1492 timestampFiles = append(timestampFiles, timestampFile)
1493 library.addExportedGeneratedHeaders(installPaths.Paths()...)
Colin Cross127bb8b2020-12-16 16:46:01 -08001494 }
1495
1496 if Bool(library.Properties.Llndk.Export_headers_as_system) {
1497 library.reexportSystemDirs(genHeaderOutDir)
1498 } else {
1499 library.reexportDirs(genHeaderOutDir)
1500 }
1501
1502 library.reexportDeps(timestampFiles...)
1503 }
1504
Colin Cross0fb7fcd2021-03-02 11:00:07 -08001505 // override the module's export_include_dirs with llndk.override_export_include_dirs
1506 // if it is set.
1507 if override := library.Properties.Llndk.Override_export_include_dirs; override != nil {
1508 library.flagExporter.Properties.Export_include_dirs = override
1509 }
1510
Colin Cross127bb8b2020-12-16 16:46:01 -08001511 if Bool(library.Properties.Llndk.Export_headers_as_system) {
1512 library.flagExporter.Properties.Export_system_include_dirs = append(
1513 library.flagExporter.Properties.Export_system_include_dirs,
1514 library.flagExporter.Properties.Export_include_dirs...)
1515 library.flagExporter.Properties.Export_include_dirs = nil
1516 }
1517 }
1518
Colin Cross5271fea2021-04-27 13:06:04 -07001519 if ctx.IsVendorPublicLibrary() {
1520 // override the module's export_include_dirs with vendor_public_library.override_export_include_dirs
1521 // if it is set.
1522 if override := library.Properties.Vendor_public_library.Override_export_include_dirs; override != nil {
1523 library.flagExporter.Properties.Export_include_dirs = override
1524 }
1525 }
1526
Chris Parsons3c27ca32020-11-20 12:42:07 -05001527 // Linking this library consists of linking `deps.Objs` (.o files in dependencies
1528 // of this library), together with `objs` (.o files created by compiling this
1529 // library).
Colin Crossad59e752017-11-16 14:29:11 -08001530 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001531 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -08001532 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001533 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001534 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001535 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001536 }
1537
Chris Parsons3c27ca32020-11-20 12:42:07 -05001538 // Export include paths and flags to be propagated up the tree.
Inseob Kim69378442019-06-03 19:10:47 +09001539 library.exportIncludes(ctx)
1540 library.reexportDirs(deps.ReexportedDirs...)
1541 library.reexportSystemDirs(deps.ReexportedSystemDirs...)
1542 library.reexportFlags(deps.ReexportedFlags...)
1543 library.reexportDeps(deps.ReexportedDeps...)
Inseob Kimd110f872019-12-06 13:15:38 +09001544 library.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001545
Chris Parsons3c27ca32020-11-20 12:42:07 -05001546 // Optionally export aidl headers.
Nan Zhang0007d812017-11-07 10:57:05 -08001547 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Vinh Tran367d89d2023-04-28 11:21:25 -04001548 if library.baseCompiler.hasAidl(deps) {
Vinh Tran09581952023-05-16 16:03:20 -04001549 if library.baseCompiler.hasSrcExt(".aidl") {
1550 dir := android.PathForModuleGen(ctx, "aidl")
1551 library.reexportDirs(dir)
1552 }
1553 if len(deps.AidlLibraryInfos) > 0 {
1554 dir := android.PathForModuleGen(ctx, "aidl_library")
1555 library.reexportDirs(dir)
1556 }
Inseob Kimd110f872019-12-06 13:15:38 +09001557
Paul Duffin33056e82021-02-19 13:49:08 +00001558 library.reexportDeps(library.baseCompiler.aidlOrderOnlyDeps...)
1559 library.addExportedGeneratedHeaders(library.baseCompiler.aidlHeaders...)
Dan Willemsene1240db2016-11-03 14:28:51 -07001560 }
1561 }
1562
Chris Parsons3c27ca32020-11-20 12:42:07 -05001563 // Optionally export proto headers.
Nan Zhang0007d812017-11-07 10:57:05 -08001564 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -07001565 if library.baseCompiler.hasSrcExt(".proto") {
Jiyong Park74955042019-10-22 20:19:51 +09001566 var includes android.Paths
Colin Cross19878da2019-03-28 14:45:07 -07001567 if flags.proto.CanonicalPathFromRoot {
Jiyong Park74955042019-10-22 20:19:51 +09001568 includes = append(includes, flags.proto.SubDir)
Colin Cross10d22312017-05-03 11:01:58 -07001569 }
Jiyong Park74955042019-10-22 20:19:51 +09001570 includes = append(includes, flags.proto.Dir)
Inseob Kim69378442019-06-03 19:10:47 +09001571 library.reexportDirs(includes...)
Inseob Kimd110f872019-12-06 13:15:38 +09001572
Paul Duffin33056e82021-02-19 13:49:08 +00001573 library.reexportDeps(library.baseCompiler.protoOrderOnlyDeps...)
1574 library.addExportedGeneratedHeaders(library.baseCompiler.protoHeaders...)
Colin Cross0c461f12016-10-20 16:11:43 -07001575 }
1576 }
1577
Inseob Kim07def122020-11-23 14:43:02 +09001578 // If the library is sysprop_library, expose either public or internal header selectively.
Inseob Kim21f26902018-09-06 00:55:20 +09001579 if library.baseCompiler.hasSrcExt(".sysprop") {
Jiyong Park74955042019-10-22 20:19:51 +09001580 dir := android.PathForModuleGen(ctx, "sysprop", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +09001581 if library.Properties.Sysprop.Platform != nil {
Inseob Kimc0907f12019-02-08 21:00:45 +09001582 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
1583
Inseob Kim07def122020-11-23 14:43:02 +09001584 // If the owner is different from the user, expose public header. That is,
1585 // 1) if the user is product (as owner can only be platform / vendor)
Inseob Kim06410042021-01-18 15:23:28 +09001586 // 2) if the owner is platform and the client is vendor
1587 // We don't care Platform -> Vendor dependency as it's already forbidden.
1588 if ctx.Device() && (ctx.ProductSpecific() || (isOwnerPlatform && ctx.inVendor())) {
Jiyong Park74955042019-10-22 20:19:51 +09001589 dir = android.PathForModuleGen(ctx, "sysprop/public", "include")
Inseob Kimc0907f12019-02-08 21:00:45 +09001590 }
1591 }
1592
Paul Duffin37e0de52021-02-19 17:05:39 +00001593 // Make sure to only export headers which are within the include directory.
1594 _, headers := android.FilterPathListPredicate(library.baseCompiler.syspropHeaders, func(path android.Path) bool {
1595 _, isRel := android.MaybeRel(ctx, dir.String(), path.String())
1596 return isRel
1597 })
1598
Chris Parsons3c27ca32020-11-20 12:42:07 -05001599 // Add sysprop-related directories to the exported directories of this library.
Inseob Kim69378442019-06-03 19:10:47 +09001600 library.reexportDirs(dir)
Paul Duffin33056e82021-02-19 13:49:08 +00001601 library.reexportDeps(library.baseCompiler.syspropOrderOnlyDeps...)
Paul Duffin37e0de52021-02-19 17:05:39 +00001602 library.addExportedGeneratedHeaders(headers...)
Inseob Kim21f26902018-09-06 00:55:20 +09001603 }
1604
Chris Parsons3c27ca32020-11-20 12:42:07 -05001605 // Add stub-related flags if this library is a stub library.
Jiyong Park892a98f2020-12-14 09:20:00 +09001606 library.exportVersioningMacroIfNeeded(ctx)
Jiyong Parkda732bd2018-11-02 18:23:15 +09001607
Chris Parsons3c27ca32020-11-20 12:42:07 -05001608 // Propagate a Provider containing information about exported flags, deps, and include paths.
Colin Cross0de8a1e2020-09-18 14:15:30 -07001609 library.flagExporter.setProvider(ctx)
1610
Colin Cross4d9c2d12016-07-29 12:48:20 -07001611 return out
1612}
1613
Jiyong Park892a98f2020-12-14 09:20:00 +09001614func (library *libraryDecorator) exportVersioningMacroIfNeeded(ctx android.BaseModuleContext) {
Colin Cross127bb8b2020-12-16 16:46:01 -08001615 if library.buildStubs() && library.stubsVersion() != "" && !library.skipAPIDefine {
Jiyong Park892a98f2020-12-14 09:20:00 +09001616 name := versioningMacroName(ctx.Module().(*Module).ImplementationModuleName(ctx))
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001617 apiLevel, err := android.ApiLevelFromUser(ctx, library.stubsVersion())
1618 if err != nil {
1619 ctx.ModuleErrorf("Can't export version macro: %s", err.Error())
1620 }
1621 library.reexportFlags("-D" + name + "=" + strconv.Itoa(apiLevel.FinalOrPreviewInt()))
Jiyong Park892a98f2020-12-14 09:20:00 +09001622 }
1623}
1624
Chris Parsons3c27ca32020-11-20 12:42:07 -05001625// buildStatic returns true if this library should be built as a static library.
Colin Crossb916a382016-07-29 17:28:03 -07001626func (library *libraryDecorator) buildStatic() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001627 return library.MutatedProperties.BuildStatic &&
1628 BoolDefault(library.StaticProperties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001629}
1630
Chris Parsons3c27ca32020-11-20 12:42:07 -05001631// buildShared returns true if this library should be built as a shared library.
Colin Crossb916a382016-07-29 17:28:03 -07001632func (library *libraryDecorator) buildShared() bool {
Colin Crosse1bb5d02019-09-24 14:55:04 -07001633 return library.MutatedProperties.BuildShared &&
1634 BoolDefault(library.SharedProperties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -07001635}
1636
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001637func (library *libraryDecorator) objs() Objects {
1638 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -07001639}
1640
Colin Cross0de8a1e2020-09-18 14:15:30 -07001641func (library *libraryDecorator) reuseObjs() Objects {
1642 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -07001643}
1644
Colin Cross26c34ed2016-09-30 17:10:16 -07001645func (library *libraryDecorator) toc() android.OptionalPath {
1646 return library.tocFile
1647}
1648
Jiyong Parkf1194352019-02-25 11:05:47 +09001649func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
1650 dir := library.baseInstaller.installDir(ctx)
1651 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
Florian Mayer95cd6db2023-03-23 17:48:07 -07001652 // libc_hwasan has relative_install_dir set, which would mess up the dir.Base() logic.
1653 // hardcode here because it's the only target, if we have other targets that use this
1654 // we can generalise this.
1655 var target string
1656 if ctx.baseModuleName() == "libc_hwasan" {
1657 target = "/" + filepath.Join("apex", "com.android.runtime", "lib64", "bionic", "hwasan", file.Base())
1658 } else {
1659 base := dir.Base()
1660 target = "/" + filepath.Join("apex", "com.android.runtime", base, "bionic", file.Base())
1661 }
Jiyong Parkf1194352019-02-25 11:05:47 +09001662 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001663 library.postInstallCmds = append(library.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
Jiyong Parkf1194352019-02-25 11:05:47 +09001664}
1665
Colin Crossb916a382016-07-29 17:28:03 -07001666func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -07001667 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +09001668 if ctx.Device() && ctx.useVndk() {
Jooyung Han261e1582020-10-20 18:54:21 +09001669 // set subDir for VNDK extensions
Ivan Lozanof9e21722020-12-02 09:00:51 -05001670 if ctx.IsVndkExt() {
Jooyung Han261e1582020-10-20 18:54:21 +09001671 if ctx.isVndkSp() {
1672 library.baseInstaller.subDir = "vndk-sp"
1673 } else {
1674 library.baseInstaller.subDir = "vndk"
1675 }
1676 }
1677
Justin Yun31094b12020-12-24 16:11:23 +09001678 // In some cases we want to use core variant for VNDK-Core libs.
1679 // Skip product variant since VNDKs use only the vendor variant.
1680 if ctx.isVndk() && !ctx.isVndkSp() && !ctx.IsVndkExt() && !ctx.inProduct() {
Vic Yang1a5812a2020-01-31 10:38:40 -08001681 mayUseCoreVariant := true
1682
1683 if ctx.mustUseVendorVariant() {
1684 mayUseCoreVariant = false
1685 }
1686
Elvis Chien9c993542021-06-25 01:15:17 +08001687 if ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) {
Vic Yang1a5812a2020-01-31 10:38:40 -08001688 mayUseCoreVariant = false
1689 }
1690
1691 if mayUseCoreVariant {
Vic Yangd92090f2020-01-08 14:32:28 -08001692 library.checkSameCoreVariant = true
1693 if ctx.DeviceConfig().VndkUseCoreVariant() {
1694 library.useCoreVariant = true
1695 }
Vic Yangefd249e2018-11-12 20:19:56 -08001696 }
Justin Yun8fe12122017-12-07 17:18:15 +09001697 }
Logan Chienf3511742017-10-31 18:04:35 +08001698
Jooyung Han261e1582020-10-20 18:54:21 +09001699 // do not install vndk libs
1700 // vndk libs are packaged into VNDK APEX
Justin Yund5784122023-10-25 13:25:32 +09001701 if ctx.isVndk() && !ctx.IsVndkExt() && !ctx.Config().IsVndkDeprecated() && !ctx.inProduct() {
Jooyung Han261e1582020-10-20 18:54:21 +09001702 return
Justin Yun8effde42017-06-23 19:24:43 +09001703 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001704 } else if library.hasStubsVariants() && !ctx.Host() && ctx.directlyInAnyApex() {
Jiyong Parkf1194352019-02-25 11:05:47 +09001705 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
1706 // The original path becomes a symlink to the corresponding file in the
1707 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -07001708 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -07001709 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001710 !translatedArch && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() {
Martin Stjernholm279de572019-09-10 23:18:20 +01001711 if ctx.Device() {
Jiyong Parkc3e2c862019-03-16 01:10:08 +09001712 library.installSymlinkToRuntimeApex(ctx, file)
1713 }
Jiyong Park429660f2019-01-16 22:31:11 +09001714 library.baseInstaller.subDir = "bootstrap"
1715 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001716 } else if ctx.directlyInAnyApex() && ctx.IsLlndk() && !isBionic(ctx.baseModuleName()) {
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001717 // Skip installing LLNDK (non-bionic) libraries moved to APEX.
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001718 ctx.Module().HideFromMake()
Justin Yun8effde42017-06-23 19:24:43 +09001719 }
Przemyslaw Szczepaniake6fc5072019-07-12 14:06:23 +01001720
Colin Cross4d9c2d12016-07-29 12:48:20 -07001721 library.baseInstaller.install(ctx, file)
1722 }
Dan Albertf563d252017-10-13 00:29:00 -07001723
Dan Albert281f22b2017-12-13 15:03:47 -08001724 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Kiyoung Kimaa394802024-01-08 12:55:45 +09001725 !ctx.InVendorOrProduct() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +09001726 library.baseLinker.sanitize.isUnsanitizedVariant() &&
Colin Cross95b07f22020-12-16 11:06:50 -08001727 ctx.isForPlatform() && !ctx.isPreventInstall() {
Dan Albert4048bb02023-04-03 20:19:07 +00001728 installPath := getUnversionedLibraryInstallPath(ctx).Join(ctx, file.Base())
Dan Albertf563d252017-10-13 00:29:00 -07001729
1730 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
1731 Rule: android.Cp,
1732 Description: "install " + installPath.Base(),
1733 Output: installPath,
1734 Input: file,
1735 })
1736
Colin Cross0875c522017-11-28 17:34:01 -08001737 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -07001738 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001739}
1740
Paul Duffin0cb37b92020-03-04 14:52:46 +00001741func (library *libraryDecorator) everInstallable() bool {
1742 // Only shared and static libraries are installed. Header libraries (which are
1743 // neither static or shared) are not installed.
1744 return library.shared() || library.static()
1745}
1746
Chris Parsons3c27ca32020-11-20 12:42:07 -05001747// static returns true if this library is for a "static' variant.
Colin Crossb916a382016-07-29 17:28:03 -07001748func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001749 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -07001750}
1751
Chris Parsons3c27ca32020-11-20 12:42:07 -05001752// shared returns true if this library is for a "shared' variant.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001753func (library *libraryDecorator) shared() bool {
1754 return library.MutatedProperties.VariantIsShared
1755}
1756
Chris Parsons3c27ca32020-11-20 12:42:07 -05001757// header returns true if this library is for a header-only variant.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001758func (library *libraryDecorator) header() bool {
Chris Parsons3c27ca32020-11-20 12:42:07 -05001759 // Neither "static" nor "shared" implies this library is header-only.
Colin Crossa48ab5b2017-02-14 15:28:44 -08001760 return !library.static() && !library.shared()
1761}
1762
Chris Parsons3c27ca32020-11-20 12:42:07 -05001763// setStatic marks the library variant as "static".
Colin Crossa48ab5b2017-02-14 15:28:44 -08001764func (library *libraryDecorator) setStatic() {
1765 library.MutatedProperties.VariantIsStatic = true
1766 library.MutatedProperties.VariantIsShared = false
1767}
1768
Chris Parsons3c27ca32020-11-20 12:42:07 -05001769// setShared marks the library variant as "shared".
Colin Crossa48ab5b2017-02-14 15:28:44 -08001770func (library *libraryDecorator) setShared() {
1771 library.MutatedProperties.VariantIsStatic = false
1772 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -07001773}
1774
Chris Parsons3c27ca32020-11-20 12:42:07 -05001775// BuildOnlyStatic disables building this library as a shared library.
Colin Crossab3b7322016-12-09 14:46:15 -08001776func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001777 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -08001778}
1779
Chris Parsons3c27ca32020-11-20 12:42:07 -05001780// BuildOnlyShared disables building this library as a static library.
Colin Crossab3b7322016-12-09 14:46:15 -08001781func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001782 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -08001783}
1784
Chris Parsons3c27ca32020-11-20 12:42:07 -05001785// HeaderOnly disables building this library as a shared or static library;
1786// the library only exists to propagate header file dependencies up the build graph.
Colin Cross5950f382016-12-13 12:50:57 -08001787func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -08001788 library.MutatedProperties.BuildShared = false
1789 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -08001790}
1791
Colin Cross127bb8b2020-12-16 16:46:01 -08001792// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs.
1793func (library *libraryDecorator) hasLLNDKStubs() bool {
Colin Cross203b4212021-04-26 17:19:41 -07001794 return String(library.Properties.Llndk.Symbol_file) != ""
Colin Cross0fb7fcd2021-03-02 11:00:07 -08001795}
1796
Colin Cross203b4212021-04-26 17:19:41 -07001797// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs.
Colin Cross1f3f1302021-04-26 18:37:44 -07001798func (library *libraryDecorator) hasLLNDKHeaders() bool {
1799 return Bool(library.Properties.Llndk.Llndk_headers)
1800}
1801
Colin Cross5271fea2021-04-27 13:06:04 -07001802// hasVendorPublicLibrary returns true if this cc_library module has a variant that will build
1803// vendor public library stubs.
1804func (library *libraryDecorator) hasVendorPublicLibrary() bool {
1805 return String(library.Properties.Vendor_public_library.Symbol_file) != ""
1806}
1807
Colin Cross0477b422020-10-13 18:43:54 -07001808func (library *libraryDecorator) implementationModuleName(name string) string {
1809 return name
1810}
1811
Jiyong Park7ed9de32018-10-15 22:25:07 +09001812func (library *libraryDecorator) buildStubs() bool {
1813 return library.MutatedProperties.BuildStubs
1814}
1815
Jiyong Parka034b832019-08-27 14:02:19 +09001816func (library *libraryDecorator) symbolFileForAbiCheck(ctx ModuleContext) *string {
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +08001817 if props := library.getHeaderAbiCheckerProperties(ctx); props.Symbol_file != nil {
1818 return props.Symbol_file
Jiyong Parka034b832019-08-27 14:02:19 +09001819 }
Colin Cross31076b32020-10-23 17:22:06 -07001820 if library.hasStubsVariants() && library.Properties.Stubs.Symbol_file != nil {
Jiyong Parka034b832019-08-27 14:02:19 +09001821 return library.Properties.Stubs.Symbol_file
1822 }
Hsin-Yi Chenf1f276c2024-03-11 20:02:34 +08001823 // TODO(b/309880485): Distinguish platform, NDK, LLNDK, and APEX version scripts.
1824 if library.baseLinker.Properties.Version_script != nil {
1825 return library.baseLinker.Properties.Version_script
1826 }
Jiyong Parka034b832019-08-27 14:02:19 +09001827 return nil
1828}
1829
Colin Crossc88c2722020-09-28 17:32:47 -07001830func (library *libraryDecorator) hasStubsVariants() bool {
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001831 // Just having stubs.symbol_file is enough to create a stub variant. In that case
1832 // the stub for the future API level is created.
1833 return library.Properties.Stubs.Symbol_file != nil ||
1834 len(library.Properties.Stubs.Versions) > 0
Colin Crossc88c2722020-09-28 17:32:47 -07001835}
1836
Alan Stokes73feba32022-11-14 12:21:24 +00001837func (library *libraryDecorator) isStubsImplementationRequired() bool {
1838 return BoolDefault(library.Properties.Stubs.Implementation_installable, true)
1839}
1840
Colin Cross3572cf72020-10-01 15:58:11 -07001841func (library *libraryDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001842 if !library.hasStubsVariants() {
1843 return nil
1844 }
1845
Kiyoung Kimaa394802024-01-08 12:55:45 +09001846 if library.hasLLNDKStubs() && ctx.Module().(*Module).InVendorOrProduct() {
Jooyung Han5e8994e2024-03-12 14:12:12 +09001847 // LLNDK libraries only need a single stubs variant (""), which is
1848 // added automatically in createVersionVariations().
1849 return nil
Colin Crossc1b36442021-05-06 13:42:48 -07001850 }
1851
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001852 // Future API level is implicitly added if there isn't
Jooyung Han5e8994e2024-03-12 14:12:12 +09001853 versions := addCurrentVersionIfNotPresent(library.Properties.Stubs.Versions)
1854 normalizeVersions(ctx, versions)
1855 return versions
Sam Delmerico75dbca22023-04-20 13:13:25 +00001856}
1857
1858func addCurrentVersionIfNotPresent(vers []string) []string {
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001859 if inList(android.FutureApiLevel.String(), vers) {
1860 return vers
1861 }
1862 // In some cases, people use the raw value "10000" in the versions property.
1863 // We shouldn't add the future API level in that case, otherwise there will
1864 // be two identical versions.
1865 if inList(strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt()), vers) {
1866 return vers
1867 }
1868 return append(vers, android.FutureApiLevel.String())
Colin Crossc88c2722020-09-28 17:32:47 -07001869}
1870
1871func (library *libraryDecorator) setStubsVersion(version string) {
1872 library.MutatedProperties.StubsVersion = version
1873}
1874
Jiyong Park7ed9de32018-10-15 22:25:07 +09001875func (library *libraryDecorator) stubsVersion() string {
1876 return library.MutatedProperties.StubsVersion
1877}
1878
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001879func (library *libraryDecorator) setBuildStubs(isLatest bool) {
Colin Crossc88c2722020-09-28 17:32:47 -07001880 library.MutatedProperties.BuildStubs = true
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001881 library.MutatedProperties.IsLatestVersion = isLatest
Colin Crossc88c2722020-09-28 17:32:47 -07001882}
1883
1884func (library *libraryDecorator) setAllStubsVersions(versions []string) {
1885 library.MutatedProperties.AllStubsVersions = versions
1886}
1887
1888func (library *libraryDecorator) allStubsVersions() []string {
1889 return library.MutatedProperties.AllStubsVersions
1890}
1891
Jooyung Hanad4c1872020-02-27 17:56:44 +09001892func (library *libraryDecorator) isLatestStubVersion() bool {
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001893 return library.MutatedProperties.IsLatestVersion
Jooyung Hanad4c1872020-02-27 17:56:44 +09001894}
1895
Jiyong Parka90ca002019-10-07 15:47:24 +09001896func (library *libraryDecorator) availableFor(what string) bool {
1897 var list []string
1898 if library.static() {
1899 list = library.StaticProperties.Static.Apex_available
1900 } else if library.shared() {
1901 list = library.SharedProperties.Shared.Apex_available
1902 }
1903 if len(list) == 0 {
1904 return false
1905 }
1906 return android.CheckAvailableForApex(what, list)
1907}
1908
Colin Cross1bc94122021-10-28 13:25:54 -07001909func (library *libraryDecorator) installable() *bool {
1910 if library.static() {
1911 return library.StaticProperties.Static.Installable
1912 } else if library.shared() {
1913 return library.SharedProperties.Shared.Installable
1914 }
1915 return nil
1916}
1917
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +00001918func (library *libraryDecorator) makeUninstallable(mod *Module) {
1919 if library.static() && library.buildStatic() && !library.buildStubs() {
1920 // If we're asked to make a static library uninstallable we don't do
1921 // anything since AndroidMkEntries always sets LOCAL_UNINSTALLABLE_MODULE
1922 // for these entries. This is done to still get the make targets for NOTICE
1923 // files from notice_files.mk, which other libraries might depend on.
1924 return
1925 }
1926 mod.ModuleBase.MakeUninstallable()
1927}
1928
Jihoon Kangf78a8902022-09-01 22:47:07 +00001929func (library *libraryDecorator) getPartition() string {
1930 return library.path.Partition()
1931}
1932
Colin Crossceaa5322021-09-28 16:37:50 -07001933func (library *libraryDecorator) getAPIListCoverageXMLPath() android.ModuleOutPath {
1934 return library.apiListCoverageXmlPath
1935}
1936
Inseob Kima1888ce2022-10-04 14:42:02 +09001937func (library *libraryDecorator) overriddenModules() []string {
1938 return library.Properties.Overrides
1939}
1940
1941var _ overridable = (*libraryDecorator)(nil)
1942
Colin Cross571cccf2019-02-04 11:22:08 -08001943var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
1944
Chris Parsons3c27ca32020-11-20 12:42:07 -05001945// versioningMacroNamesList returns a singleton map, where keys are "version macro names",
1946// and values are the module name responsible for registering the version macro name.
1947//
1948// Version macros are used when building against stubs, to provide version information about
1949// the stub. Only stub libraries should have an entry in this list.
1950//
1951// For example, when building against libFoo#ver, __LIBFOO_API__ macro is set to ver so
1952// that headers from libFoo can be conditionally compiled (this may hide APIs
1953// that are not available for the version).
1954//
1955// This map is used to ensure that there aren't conflicts between these version macro names.
Jiyong Parkda732bd2018-11-02 18:23:15 +09001956func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001957 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +09001958 m := make(map[string]string)
1959 return &m
1960 }).(*map[string]string)
1961}
1962
1963// alphanumeric and _ characters are preserved.
1964// other characters are all converted to _
1965var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
1966
Chris Parsons3c27ca32020-11-20 12:42:07 -05001967// versioningMacroName returns the canonical version macro name for the given module.
Jiyong Parkda732bd2018-11-02 18:23:15 +09001968func versioningMacroName(moduleName string) string {
1969 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
Jooyung Hanb04a4992020-03-13 18:57:35 +09001970 macroName = strings.ToUpper(macroName)
Jiyong Parkda732bd2018-11-02 18:23:15 +09001971 return "__" + macroName + "_API__"
1972}
1973
Chris Parsons3c27ca32020-11-20 12:42:07 -05001974// NewLibrary builds and returns a new Module corresponding to a C++ library.
1975// Individual module implementations which comprise a C++ library (or something like
1976// a C++ library) should call this function, set some fields on the result, and
1977// then call the Init function.
Colin Crossab3b7322016-12-09 14:46:15 -08001978func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -07001979 module := newModule(hod, android.MultilibBoth)
1980
Colin Crossb916a382016-07-29 17:28:03 -07001981 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -08001982 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -08001983 BuildShared: true,
1984 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001985 },
Colin Crossb916a382016-07-29 17:28:03 -07001986 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -07001987 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001988 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001989 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001990 }
1991
Colin Crossb916a382016-07-29 17:28:03 -07001992 module.compiler = library
1993 module.linker = library
1994 module.installer = library
Colin Cross31076b32020-10-23 17:22:06 -07001995 module.library = library
Colin Crossb916a382016-07-29 17:28:03 -07001996
1997 return module, library
1998}
1999
Colin Cross10d22312017-05-03 11:01:58 -07002000// connects a shared library to a static library in order to reuse its .o files to avoid
2001// compiling source files twice.
2002func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
2003 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
2004 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08002005
2006 // Check libraries in addition to cflags, since libraries may be exporting different
2007 // include directories.
Colin Crosse1bb5d02019-09-24 14:55:04 -07002008 if len(staticCompiler.StaticProperties.Static.Cflags) == 0 &&
2009 len(sharedCompiler.SharedProperties.Shared.Cflags) == 0 &&
2010 len(staticCompiler.StaticProperties.Static.Whole_static_libs) == 0 &&
2011 len(sharedCompiler.SharedProperties.Shared.Whole_static_libs) == 0 &&
2012 len(staticCompiler.StaticProperties.Static.Static_libs) == 0 &&
2013 len(sharedCompiler.SharedProperties.Shared.Static_libs) == 0 &&
2014 len(staticCompiler.StaticProperties.Static.Shared_libs) == 0 &&
2015 len(sharedCompiler.SharedProperties.Shared.Shared_libs) == 0 &&
Martin Stjernholm10566a02020-03-24 01:19:52 +00002016 // Compare System_shared_libs properties with nil because empty lists are
2017 // semantically significant for them.
Colin Crosse1bb5d02019-09-24 14:55:04 -07002018 staticCompiler.StaticProperties.Static.System_shared_libs == nil &&
2019 sharedCompiler.SharedProperties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07002020
2021 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2022 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
2023 sharedCompiler.baseCompiler.Properties.Srcs
2024 sharedCompiler.baseCompiler.Properties.Srcs = nil
2025 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
2026 }
Colin Cross0de8a1e2020-09-18 14:15:30 -07002027
2028 // This dep is just to reference static variant from shared variant
2029 mctx.AddInterVariantDependency(staticVariantTag, shared, static)
Colin Cross10d22312017-05-03 11:01:58 -07002030 }
2031}
2032
Chris Parsons3c27ca32020-11-20 12:42:07 -05002033// LinkageMutator adds "static" or "shared" variants for modules depending
2034// on whether the module can be built as a static library or a shared library.
Colin Crosse40b4ea2018-10-02 22:25:58 -07002035func LinkageMutator(mctx android.BottomUpMutatorContext) {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002036 ccPrebuilt := false
Colin Crossb916a382016-07-29 17:28:03 -07002037 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002038 _, ccPrebuilt = m.linker.(prebuiltLibraryInterface)
Ivan Lozano52767be2019-10-18 14:49:46 -07002039 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002040 if ccPrebuilt {
Ivan Lozano52767be2019-10-18 14:49:46 -07002041 library := mctx.Module().(*Module).linker.(prebuiltLibraryInterface)
Colin Cross33b2fb72019-05-14 14:07:01 -07002042
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002043 // Differentiate between header only and building an actual static/shared library
Colin Cross127bb8b2020-12-16 16:46:01 -08002044 buildStatic := library.buildStatic()
2045 buildShared := library.buildShared()
2046 if buildStatic || buildShared {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002047 // Always create both the static and shared variants for prebuilt libraries, and then disable the one
2048 // that is not being used. This allows them to share the name of a cc_library module, which requires that
2049 // all the variants of the cc_library also exist on the prebuilt.
2050 modules := mctx.CreateLocalVariations("static", "shared")
2051 static := modules[0].(*Module)
2052 shared := modules[1].(*Module)
Colin Cross33b2fb72019-05-14 14:07:01 -07002053
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002054 static.linker.(prebuiltLibraryInterface).setStatic()
2055 shared.linker.(prebuiltLibraryInterface).setShared()
Colin Cross33b2fb72019-05-14 14:07:01 -07002056
Colin Cross127bb8b2020-12-16 16:46:01 -08002057 if buildShared {
Colin Cross81ca6cd2020-08-06 17:46:48 -07002058 mctx.AliasVariation("shared")
Colin Cross127bb8b2020-12-16 16:46:01 -08002059 } else if buildStatic {
Colin Cross81ca6cd2020-08-06 17:46:48 -07002060 mctx.AliasVariation("static")
2061 }
2062
Colin Cross127bb8b2020-12-16 16:46:01 -08002063 if !buildStatic {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002064 static.linker.(prebuiltLibraryInterface).disablePrebuilt()
2065 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002066 if !buildShared {
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002067 shared.linker.(prebuiltLibraryInterface).disablePrebuilt()
2068 }
2069 } else {
2070 // Header only
Colin Crossb916a382016-07-29 17:28:03 -07002071 }
Paul Duffinf5ea9e12020-02-21 10:57:00 +00002072
Ivan Lozano52767be2019-10-18 14:49:46 -07002073 } else if library, ok := mctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() {
Ivan Lozano52767be2019-10-18 14:49:46 -07002074
Ivan Lozano2b262972019-11-21 12:30:50 -08002075 // Non-cc.Modules may need an empty variant for their mutators.
2076 variations := []string{}
2077 if library.NonCcVariants() {
2078 variations = append(variations, "")
2079 }
2080
Colin Cross127bb8b2020-12-16 16:46:01 -08002081 isLLNDK := false
2082 if m, ok := mctx.Module().(*Module); ok {
Colin Cross203b4212021-04-26 17:19:41 -07002083 isLLNDK = m.IsLlndk()
Colin Cross127bb8b2020-12-16 16:46:01 -08002084 }
2085 buildStatic := library.BuildStaticVariant() && !isLLNDK
2086 buildShared := library.BuildSharedVariant()
2087 if buildStatic && buildShared {
Ivan Lozano2b262972019-11-21 12:30:50 -08002088 variations := append([]string{"static", "shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07002089
2090 modules := mctx.CreateLocalVariations(variations...)
2091 static := modules[0].(LinkableInterface)
2092 shared := modules[1].(LinkableInterface)
2093
2094 static.SetStatic()
2095 shared.SetShared()
2096
2097 if _, ok := library.(*Module); ok {
2098 reuseStaticLibrary(mctx, static.(*Module), shared.(*Module))
2099 }
Colin Cross81ca6cd2020-08-06 17:46:48 -07002100 mctx.AliasVariation("shared")
Colin Cross127bb8b2020-12-16 16:46:01 -08002101 } else if buildStatic {
Ivan Lozano2b262972019-11-21 12:30:50 -08002102 variations := append([]string{"static"}, variations...)
2103
2104 modules := mctx.CreateLocalVariations(variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07002105 modules[0].(LinkableInterface).SetStatic()
Colin Cross81ca6cd2020-08-06 17:46:48 -07002106 mctx.AliasVariation("static")
Colin Cross127bb8b2020-12-16 16:46:01 -08002107 } else if buildShared {
Ivan Lozano2b262972019-11-21 12:30:50 -08002108 variations := append([]string{"shared"}, variations...)
Ivan Lozano52767be2019-10-18 14:49:46 -07002109
Ivan Lozano2b262972019-11-21 12:30:50 -08002110 modules := mctx.CreateLocalVariations(variations...)
2111 modules[0].(LinkableInterface).SetShared()
Colin Cross81ca6cd2020-08-06 17:46:48 -07002112 mctx.AliasVariation("shared")
Ivan Lozano2b262972019-11-21 12:30:50 -08002113 } else if len(variations) > 0 {
2114 mctx.CreateLocalVariations(variations...)
Colin Cross81ca6cd2020-08-06 17:46:48 -07002115 mctx.AliasVariation(variations[0])
Ivan Lozano2b262972019-11-21 12:30:50 -08002116 }
Colin Crossb916a382016-07-29 17:28:03 -07002117 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07002118}
Jiyong Park7ed9de32018-10-15 22:25:07 +09002119
Chris Parsons3c27ca32020-11-20 12:42:07 -05002120// normalizeVersions modifies `versions` in place, so that each raw version
2121// string becomes its normalized canonical form.
2122// Validates that the versions in `versions` are specified in least to greatest order.
Colin Cross8ff10582023-12-07 13:10:56 -08002123func normalizeVersions(ctx android.BaseModuleContext, versions []string) {
Dan Albertc8060532020-07-22 22:32:17 -07002124 var previous android.ApiLevel
Jooyung Hanad4c1872020-02-27 17:56:44 +09002125 for i, v := range versions {
Dan Albertc8060532020-07-22 22:32:17 -07002126 ver, err := android.ApiLevelFromUser(ctx, v)
Jooyung Hanad4c1872020-02-27 17:56:44 +09002127 if err != nil {
Jooyung Hanaed150d2020-04-02 01:41:41 +09002128 ctx.PropertyErrorf("versions", "%s", err.Error())
2129 return
Jooyung Hanad4c1872020-02-27 17:56:44 +09002130 }
Dan Albertc8060532020-07-22 22:32:17 -07002131 if i > 0 && ver.LessThanOrEqualTo(previous) {
2132 ctx.PropertyErrorf("versions", "not sorted: %v", versions)
2133 }
2134 versions[i] = ver.String()
2135 previous = ver
Jooyung Hanaed150d2020-04-02 01:41:41 +09002136 }
Jooyung Hanad4c1872020-02-27 17:56:44 +09002137}
2138
Jooyung Han61b66e92020-03-21 14:21:46 +00002139func createVersionVariations(mctx android.BottomUpMutatorContext, versions []string) {
Colin Cross127bb8b2020-12-16 16:46:01 -08002140 // "" is for the non-stubs (implementation) variant for system modules, or the LLNDK variant
2141 // for LLNDK modules.
Colin Cross0de8a1e2020-09-18 14:15:30 -07002142 variants := append(android.CopyOf(versions), "")
Jooyung Han61b66e92020-03-21 14:21:46 +00002143
Colin Cross127bb8b2020-12-16 16:46:01 -08002144 m := mctx.Module().(*Module)
2145 isLLNDK := m.IsLlndk()
Colin Cross5271fea2021-04-27 13:06:04 -07002146 isVendorPublicLibrary := m.IsVendorPublicLibrary()
Kiyoung Kimee58c932022-10-25 22:59:41 +09002147 isImportedApiLibrary := m.isImportedApiLibrary()
Colin Cross127bb8b2020-12-16 16:46:01 -08002148
Colin Crossd1f898e2020-08-18 18:35:15 -07002149 modules := mctx.CreateLocalVariations(variants...)
Jooyung Han61b66e92020-03-21 14:21:46 +00002150 for i, m := range modules {
Colin Cross127bb8b2020-12-16 16:46:01 -08002151
Kiyoung Kimee58c932022-10-25 22:59:41 +09002152 if variants[i] != "" || isLLNDK || isVendorPublicLibrary || isImportedApiLibrary {
Colin Cross127bb8b2020-12-16 16:46:01 -08002153 // A stubs or LLNDK stubs variant.
Colin Cross31076b32020-10-23 17:22:06 -07002154 c := m.(*Module)
Colin Cross31076b32020-10-23 17:22:06 -07002155 c.sanitize = nil
2156 c.stl = nil
2157 c.Properties.PreventInstall = true
2158 lib := moduleLibraryInterface(m)
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002159 isLatest := i == (len(versions) - 1)
2160 lib.setBuildStubs(isLatest)
Colin Cross127bb8b2020-12-16 16:46:01 -08002161
2162 if variants[i] != "" {
2163 // A non-LLNDK stubs module is hidden from make and has a dependency from the
2164 // implementation module to the stubs module.
2165 c.Properties.HideFromMake = true
2166 lib.setStubsVersion(variants[i])
2167 mctx.AddInterVariantDependency(stubImplDepTag, modules[len(modules)-1], modules[i])
2168 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002169 }
2170 }
Colin Cross094faa52020-08-06 17:38:25 -07002171 mctx.AliasVariation("")
Colin Crossd1f898e2020-08-18 18:35:15 -07002172 latestVersion := ""
2173 if len(versions) > 0 {
2174 latestVersion = versions[len(versions)-1]
2175 }
2176 mctx.CreateAliasVariation("latest", latestVersion)
Jooyung Han61b66e92020-03-21 14:21:46 +00002177}
2178
Colin Crossbbc941b2020-09-30 12:27:01 -07002179func createPerApiVersionVariations(mctx android.BottomUpMutatorContext, minSdkVersion string) {
2180 from, err := nativeApiLevelFromUser(mctx, minSdkVersion)
2181 if err != nil {
2182 mctx.PropertyErrorf("min_sdk_version", err.Error())
2183 return
2184 }
2185
2186 versionStrs := ndkLibraryVersions(mctx, from)
2187 modules := mctx.CreateLocalVariations(versionStrs...)
2188
2189 for i, module := range modules {
2190 module.(*Module).Properties.Sdk_version = StringPtr(versionStrs[i])
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09002191 module.(*Module).Properties.Min_sdk_version = StringPtr(versionStrs[i])
Colin Crossbbc941b2020-09-30 12:27:01 -07002192 }
2193}
2194
Liz Kammer23942242022-04-08 15:41:00 -04002195func canBeOrLinkAgainstVersionVariants(module interface {
Jooyung Han624d35c2020-04-10 12:57:24 +09002196 Host() bool
2197 InRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002198 InVendorRamdisk() bool
Jooyung Han624d35c2020-04-10 12:57:24 +09002199}) bool {
Jose Galmes6f843bc2020-12-11 13:36:29 -08002200 return !module.Host() && !module.InRamdisk() && !module.InVendorRamdisk()
Jooyung Han624d35c2020-04-10 12:57:24 +09002201}
2202
Liz Kammer23942242022-04-08 15:41:00 -04002203func canBeVersionVariant(module interface {
Colin Cross3146c5c2020-09-30 15:34:40 -07002204 Host() bool
2205 InRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002206 InVendorRamdisk() bool
Colin Cross3146c5c2020-09-30 15:34:40 -07002207 CcLibraryInterface() bool
2208 Shared() bool
Colin Cross3146c5c2020-09-30 15:34:40 -07002209}) bool {
Liz Kammer23942242022-04-08 15:41:00 -04002210 return canBeOrLinkAgainstVersionVariants(module) &&
Colin Crossa717db72020-10-23 14:53:06 -07002211 module.CcLibraryInterface() && module.Shared()
Colin Cross3146c5c2020-09-30 15:34:40 -07002212}
2213
Colin Cross127bb8b2020-12-16 16:46:01 -08002214func moduleLibraryInterface(module blueprint.Module) libraryInterface {
Colin Cross31076b32020-10-23 17:22:06 -07002215 if m, ok := module.(*Module); ok {
2216 return m.library
2217 }
2218 return nil
2219}
2220
Liz Kammer23942242022-04-08 15:41:00 -04002221// setStubsVersions normalizes the versions in the Stubs.Versions property into MutatedProperties.AllStubsVersions.
2222func setStubsVersions(mctx android.BottomUpMutatorContext, library libraryInterface, module *Module) {
2223 if !library.buildShared() || !canBeVersionVariant(module) {
2224 return
Colin Crossd1f898e2020-08-18 18:35:15 -07002225 }
Liz Kammer23942242022-04-08 15:41:00 -04002226 versions := library.stubsVersions(mctx)
Liz Kammer23942242022-04-08 15:41:00 -04002227 if mctx.Failed() {
2228 return
2229 }
2230 // Set the versions on the pre-mutated module so they can be read by any llndk modules that
2231 // depend on the implementation library and haven't been mutated yet.
2232 library.setAllStubsVersions(versions)
Colin Crossd1f898e2020-08-18 18:35:15 -07002233}
Jooyung Han61b66e92020-03-21 14:21:46 +00002234
Colin Crossd1f898e2020-08-18 18:35:15 -07002235// versionMutator splits a module into the mandatory non-stubs variant
2236// (which is unnamed) and zero or more stubs variants.
2237func versionMutator(mctx android.BottomUpMutatorContext) {
Liz Kammer23942242022-04-08 15:41:00 -04002238 if mctx.Os() != android.Android {
2239 return
2240 }
2241
2242 m, ok := mctx.Module().(*Module)
2243 if library := moduleLibraryInterface(mctx.Module()); library != nil && canBeVersionVariant(m) {
2244 setStubsVersions(mctx, library, m)
2245
Colin Cross31076b32020-10-23 17:22:06 -07002246 createVersionVariations(mctx, library.allStubsVersions())
Colin Crossbbc941b2020-09-30 12:27:01 -07002247 return
2248 }
2249
Liz Kammer23942242022-04-08 15:41:00 -04002250 if ok {
Colin Crossbbc941b2020-09-30 12:27:01 -07002251 if m.SplitPerApiLevel() && m.IsSdkVariant() {
Colin Crossbbc941b2020-09-30 12:27:01 -07002252 createPerApiVersionVariations(mctx, m.MinSdkVersion())
2253 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002254 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002255}
Colin Crossd7227f92019-09-05 14:26:33 -07002256
2257// maybeInjectBoringSSLHash adds a rule to run bssl_inject_hash on the output file if the module has the
2258// inject_bssl_hash or if any static library dependencies have inject_bssl_hash set. It returns the output path
2259// that the linked output file should be written to.
2260// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
2261func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.ModuleOutPath,
2262 inject *bool, fileName string) android.ModuleOutPath {
2263 // TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
2264 injectBoringSSLHash := Bool(inject)
2265 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross6e511a92020-07-27 21:26:48 -07002266 if tag, ok := ctx.OtherModuleDependencyTag(dep).(libraryDependencyTag); ok && tag.static() {
Colin Crossd7227f92019-09-05 14:26:33 -07002267 if cc, ok := dep.(*Module); ok {
2268 if library, ok := cc.linker.(*libraryDecorator); ok {
2269 if Bool(library.Properties.Inject_bssl_hash) {
2270 injectBoringSSLHash = true
2271 }
2272 }
2273 }
2274 }
2275 })
2276 if injectBoringSSLHash {
2277 hashedOutputfile := outputFile
2278 outputFile = android.PathForModuleOut(ctx, "unhashed", fileName)
2279
Colin Crossf1a035e2020-11-16 17:32:30 -08002280 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossd7227f92019-09-05 14:26:33 -07002281 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08002282 BuiltTool("bssl_inject_hash").
Colin Crossd7227f92019-09-05 14:26:33 -07002283 FlagWithInput("-in-object ", outputFile).
2284 FlagWithOutput("-o ", hashedOutputfile)
Colin Crossf1a035e2020-11-16 17:32:30 -08002285 rule.Build("injectCryptoHash", "inject crypto hash")
Colin Crossd7227f92019-09-05 14:26:33 -07002286 }
2287
2288 return outputFile
2289}