blob: 7aec7f21a191d6031eead373229255eff163fbca [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
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
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross41955e82019-05-29 14:40:35 -070022 "fmt"
Logan Chien41eabe62019-04-10 13:33:58 +080023 "io"
Dan Albert9e10cd42016-08-03 14:12:14 -070024 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
26
Colin Cross97ba0732015-03-23 17:50:24 -070027 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070028 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070031 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Paul Duffin036e7002019-12-19 19:16:28 +000036 RegisterCCBuildComponents(android.InitRegistrationContext)
Colin Cross463a90e2015-06-17 14:20:06 -070037
Paul Duffin036e7002019-12-19 19:16:28 +000038 pctx.Import("android/soong/cc/config")
39}
40
41func RegisterCCBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("cc_defaults", defaultsFactory)
43
44 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Colin Crossc511bc52020-04-07 16:50:32 +000045 ctx.BottomUp("sdk", sdkMutator).Parallel()
Inseob Kim64c43952019-08-26 16:52:35 +090046 ctx.BottomUp("vndk", VndkMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070047 ctx.BottomUp("link", LinkageMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +010048 ctx.BottomUp("test_per_src", TestPerSrcMutator).Parallel()
Colin Crossd1f898e2020-08-18 18:35:15 -070049 ctx.BottomUp("version_selector", versionSelectorMutator).Parallel()
50 ctx.BottomUp("version", versionMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070051 ctx.BottomUp("begin", BeginMutator).Parallel()
Inseob Kimac1e9862019-12-09 18:15:47 +090052 ctx.BottomUp("sysprop_cc", SyspropMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070053 })
Colin Cross16b23492016-01-06 14:41:07 -080054
Paul Duffin036e7002019-12-19 19:16:28 +000055 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Liz Kammer75db9312021-07-07 16:41:50 -040056 for _, san := range Sanitizers {
57 san.registerMutators(ctx)
58 }
Dan Willemsen581341d2017-02-09 16:16:31 -080059
Colin Cross0b908332019-06-19 23:00:20 -070060 ctx.TopDown("sanitize_runtime_deps", sanitizerRuntimeDepsMutator).Parallel()
Jiyong Park379de2f2018-12-19 02:47:14 +090061 ctx.BottomUp("sanitize_runtime", sanitizerRuntimeMutator).Parallel()
Ivan Lozano30c5db22018-02-21 15:49:20 -080062
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080063 ctx.BottomUp("coverage", coverageMutator).Parallel()
Stephen Craneba090d12017-05-09 15:44:35 -070064
65 ctx.TopDown("lto_deps", ltoDepsMutator)
66 ctx.BottomUp("lto", ltoMutator).Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +090067
Jooyung Han479ca172020-10-19 18:51:07 +090068 ctx.BottomUp("check_linktype", checkLinkTypeMutator).Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +090069 ctx.TopDown("double_loadable", checkDoubleLoadableLibraries).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070070 })
Colin Crossb98c8b02016-07-29 13:44:28 -070071
Yo Chiang8aa4e3f2020-11-19 16:30:49 +080072 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
73 // sabi mutator needs to be run after apex mutator finishes.
74 ctx.TopDown("sabi_deps", sabiDepsMutator)
75 })
76
Colin Cross57898582020-10-29 18:25:19 -070077 ctx.RegisterSingletonType("kythe_extract_all", kytheExtractAllFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070078}
79
Chris Parsonsef6e0cf2020-12-01 18:26:21 -050080// Deps is a struct containing module names of dependencies, separated by the kind of dependency.
81// Mutators should use `AddVariationDependencies` or its sibling methods to add actual dependency
82// edges to these modules.
83// This object is constructed in DepsMutator, by calling to various module delegates to set
84// relevant fields. For example, `module.compiler.compilerDeps()` may append type-specific
85// dependencies.
86// This is then consumed by the same DepsMutator, which will call `ctx.AddVariationDependencies()`
87// (or its sibling methods) to set real dependencies on the given modules.
Colin Crossca860ac2016-01-04 14:34:37 -080088type Deps struct {
89 SharedLibs, LateSharedLibs []string
90 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080091 HeaderLibs []string
Logan Chien43d34c32017-12-20 01:17:32 +080092 RuntimeLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070093
Chris Parsons79d66a52020-06-05 17:26:16 -040094 // Used for data dependencies adjacent to tests
95 DataLibs []string
96
Yo Chiang219968c2020-09-22 18:45:04 +080097 // Used by DepsMutator to pass system_shared_libs information to check_elf_file.py.
98 SystemSharedLibs []string
99
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500100 // If true, statically link the unwinder into native libraries/binaries.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800101 StaticUnwinderIfLegacy bool
102
Colin Cross5950f382016-12-13 12:50:57 -0800103 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700104
Colin Cross81413472016-04-11 14:37:39 -0700105 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700106
Dan Willemsenb40aab62016-04-20 14:21:14 -0700107 GeneratedSources []string
108 GeneratedHeaders []string
Inseob Kimd110f872019-12-06 13:15:38 +0900109 GeneratedDeps []string
Dan Willemsenb40aab62016-04-20 14:21:14 -0700110
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700111 ReexportGeneratedHeaders []string
112
Colin Crossc465efd2021-06-11 18:00:04 -0700113 CrtBegin, CrtEnd []string
Dan Willemsena0790e32018-10-12 00:24:23 -0700114
115 // Used for host bionic
Colin Cross9cfe6112021-06-11 18:02:22 -0700116 DynamicLinker string
Jiyong Parke3867542020-12-03 17:28:25 +0900117
118 // List of libs that need to be excluded for APEX variant
119 ExcludeLibsForApex []string
Colin Crossc472d572015-03-17 15:06:21 -0700120}
121
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500122// PathDeps is a struct containing file paths to dependencies of a module.
123// It's constructed in depsToPath() by traversing the direct dependencies of the current module.
124// It's used to construct flags for various build statements (such as for compiling and linking).
125// It is then passed to module decorator functions responsible for registering build statements
126// (such as `module.compiler.compile()`).`
Colin Crossca860ac2016-01-04 14:34:37 -0800127type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -0700128 // Paths to .so files
Jiyong Park64a44f22019-01-18 14:37:08 +0900129 SharedLibs, EarlySharedLibs, LateSharedLibs android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700130 // Paths to the dependencies to use for .so files (.so.toc files)
Jiyong Park64a44f22019-01-18 14:37:08 +0900131 SharedLibsDeps, EarlySharedLibsDeps, LateSharedLibsDeps android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700132 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -0700133 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700134
Colin Cross0de8a1e2020-09-18 14:15:30 -0700135 // Transitive static library dependencies of static libraries for use in ordering.
136 TranstiveStaticLibrariesForOrdering *android.DepSet
137
Colin Cross26c34ed2016-09-30 17:10:16 -0700138 // Paths to .o files
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100139 Objs Objects
140 // Paths to .o files in dependencies that provide them. Note that these lists
141 // aren't complete since prebuilt modules don't provide the .o files.
Dan Willemsen581341d2017-02-09 16:16:31 -0800142 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700143 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100145 // Paths to .a files in prebuilts. Complements WholeStaticLibObjs to contain
146 // the libs from all whole_static_lib dependencies.
147 WholeStaticLibsFromPrebuilts android.Paths
148
Colin Cross26c34ed2016-09-30 17:10:16 -0700149 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -0700150 GeneratedSources android.Paths
Inseob Kimd110f872019-12-06 13:15:38 +0900151 GeneratedDeps android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700152
Inseob Kimd110f872019-12-06 13:15:38 +0900153 Flags []string
154 IncludeDirs android.Paths
155 SystemIncludeDirs android.Paths
156 ReexportedDirs android.Paths
157 ReexportedSystemDirs android.Paths
158 ReexportedFlags []string
159 ReexportedGeneratedHeaders android.Paths
160 ReexportedDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700161
Colin Cross26c34ed2016-09-30 17:10:16 -0700162 // Paths to crt*.o files
Colin Crossc465efd2021-06-11 18:00:04 -0700163 CrtBegin, CrtEnd android.Paths
Dan Willemsena0790e32018-10-12 00:24:23 -0700164
Dan Willemsena0790e32018-10-12 00:24:23 -0700165 // Path to the dynamic linker binary
166 DynamicLinker android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167}
168
Colin Cross4af21ed2019-11-04 09:37:55 -0800169// LocalOrGlobalFlags contains flags that need to have values set globally by the build system or locally by the module
170// tracked separately, in order to maintain the required ordering (most of the global flags need to go first on the
171// command line so they can be overridden by the local module flags).
172type LocalOrGlobalFlags struct {
173 CommonFlags []string // Flags that apply to C, C++, and assembly source files
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700174 AsFlags []string // Flags that apply to assembly source files
Colin Cross4af21ed2019-11-04 09:37:55 -0800175 YasmFlags []string // Flags that apply to yasm assembly source files
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700176 CFlags []string // Flags that apply to C and C++ source files
177 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
178 ConlyFlags []string // Flags that apply to C source files
179 CppFlags []string // Flags that apply to C++ source files
180 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700181 LdFlags []string // Flags that apply to linker command lines
Colin Cross4af21ed2019-11-04 09:37:55 -0800182}
183
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500184// Flags contains various types of command line flags (and settings) for use in building build
185// statements related to C++.
Colin Cross4af21ed2019-11-04 09:37:55 -0800186type Flags struct {
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500187 // Local flags (which individual modules are responsible for). These may override global flags.
188 Local LocalOrGlobalFlags
189 // Global flags (which build system or toolchain is responsible for).
Colin Cross4af21ed2019-11-04 09:37:55 -0800190 Global LocalOrGlobalFlags
191
192 aidlFlags []string // Flags that apply to aidl source files
193 rsFlags []string // Flags that apply to renderscript source files
194 libFlags []string // Flags to add libraries early to the link order
195 extraLibFlags []string // Flags to add libraries late in the link order after LdFlags
196 TidyFlags []string // Flags that apply to clang-tidy
197 SAbiFlags []string // Flags that apply to header-abi-dumper
Colin Cross28344522015-04-22 13:07:53 -0700198
Colin Crossc3199482017-03-30 15:03:04 -0700199 // Global include flags that apply to C, C++, and assembly source files
Colin Cross4af21ed2019-11-04 09:37:55 -0800200 // These must be after any module include flags, which will be in CommonFlags.
Colin Crossc3199482017-03-30 15:03:04 -0700201 SystemIncludeFlags []string
202
Oliver Nguyen04526782020-04-21 12:40:27 -0700203 Toolchain config.Toolchain
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500204 Tidy bool // True if clang-tidy is enabled.
205 GcovCoverage bool // True if coverage files should be generated.
206 SAbiDump bool // True if header abi dumps should be generated.
Oliver Nguyen04526782020-04-21 12:40:27 -0700207 EmitXrefs bool // If true, generate Ninja rules to generate emitXrefs input files for Kythe
Colin Crossca860ac2016-01-04 14:34:37 -0800208
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500209 // The instruction set required for clang ("arm" or "thumb").
Colin Crossca860ac2016-01-04 14:34:37 -0800210 RequiredInstructionSet string
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500211 // The target-device system path to the dynamic linker.
212 DynamicLinker string
Colin Cross16b23492016-01-06 14:41:07 -0800213
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700214 CFlagsDeps android.Paths // Files depended on by compiler flags
215 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800216
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500217 // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700218 AssemblerWithCpp bool
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500219 // True if static libraries should be grouped (using `-Wl,--start-group` and `-Wl,--end-group`).
220 GroupStaticLibs bool
Dan Willemsen60e62f02018-11-16 21:05:32 -0800221
Colin Cross19878da2019-03-28 14:45:07 -0700222 proto android.ProtoFlags
Colin Cross19878da2019-03-28 14:45:07 -0700223 protoC bool // Whether to use C instead of C++
224 protoOptionsFile bool // Whether to look for a .options file next to the .proto
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700225
226 Yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200227 Lex *LexProperties
Colin Crossc472d572015-03-17 15:06:21 -0700228}
229
Colin Crossca860ac2016-01-04 14:34:37 -0800230// Properties used to compile all C or C++ modules
231type BaseProperties struct {
Dan Willemsen742a5452018-07-23 17:19:36 -0700232 // Deprecated. true is the default, false is invalid.
Colin Crossca860ac2016-01-04 14:34:37 -0800233 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700234
Jiyong Parkb35a8192020-08-10 15:59:36 +0900235 // The API level that this module is built against. The APIs of this API level will be
236 // visible at build time, but use of any APIs newer than min_sdk_version will render the
237 // module unloadable on older devices. In the future it will be possible to weakly-link new
238 // APIs, making the behavior match Java: such modules will load on older devices, but
239 // calling new APIs on devices that do not support them will result in a crash.
240 //
241 // This property has the same behavior as sdk_version does for Java modules. For those
242 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
243 // does for Java code.
244 //
245 // In addition, setting this property causes two variants to be built, one for the platform
246 // and one for apps.
Nan Zhang0007d812017-11-07 10:57:05 -0800247 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700248
Jiyong Parkb35a8192020-08-10 15:59:36 +0900249 // Minimum OS API level supported by this C or C++ module. This property becomes the value
250 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
251 // this property is also used to ensure that the min_sdk_version of the containing module is
252 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
253 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
254 // min_sdk_version of the containing APEX. When the module
255 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Jooyung Han379660c2020-04-21 15:24:00 +0900256 Min_sdk_version *string
257
Colin Crossc511bc52020-04-07 16:50:32 +0000258 // If true, always create an sdk variant and don't create a platform variant.
259 Sdk_variant_only *bool
260
Jiyong Parkde866cb2018-12-07 23:08:36 +0900261 AndroidMkSharedLibs []string `blueprint:"mutated"`
262 AndroidMkStaticLibs []string `blueprint:"mutated"`
263 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
264 AndroidMkWholeStaticLibs []string `blueprint:"mutated"`
Bill Peckhama46de702020-04-21 17:23:37 -0700265 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Jiyong Parkde866cb2018-12-07 23:08:36 +0900266 HideFromMake bool `blueprint:"mutated"`
267 PreventInstall bool `blueprint:"mutated"`
268 ApexesProvidingSharedLibs []string `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700269
Yo Chiang219968c2020-09-22 18:45:04 +0800270 // Set by DepsMutator.
271 AndroidMkSystemSharedLibs []string `blueprint:"mutated"`
272
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200273 // The name of the image this module is built for, suffixed with a '.'
Justin Yun5f7f7e82019-11-18 19:52:14 +0900274 ImageVariationPrefix string `blueprint:"mutated"`
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200275
276 // The VNDK version this module is built against. If empty, the module is not
277 // build against the VNDK.
278 VndkVersion string `blueprint:"mutated"`
279
280 // Suffix for the name of Android.mk entries generated by this module
281 SubName string `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800282
283 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
284 // file
285 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900286
Yifan Hong39143a92020-10-26 12:43:12 -0700287 // Make this module available when building for ramdisk.
288 // On device without a dedicated recovery partition, the module is only
289 // available after switching root into
290 // /first_stage_ramdisk. To expose the module before switching root, install
291 // the recovery variant instead.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800292 Ramdisk_available *bool
293
Yifan Hong39143a92020-10-26 12:43:12 -0700294 // Make this module available when building for vendor ramdisk.
295 // On device without a dedicated recovery partition, the module is only
296 // available after switching root into
297 // /first_stage_ramdisk. To expose the module before switching root, install
298 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700299 Vendor_ramdisk_available *bool
300
Jiyong Parkf9332f12018-02-01 00:54:12 +0900301 // Make this module available when building for recovery
302 Recovery_available *bool
303
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200304 // Used by imageMutator, set by ImageMutatorBegin()
305 CoreVariantNeeded bool `blueprint:"mutated"`
306 RamdiskVariantNeeded bool `blueprint:"mutated"`
307 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
308 RecoveryVariantNeeded bool `blueprint:"mutated"`
309
310 // A list of variations for the "image" mutator of the form
311 //<image name> '.' <version char>, for example, 'vendor.S'
312 ExtraVersionedImageVariations []string `blueprint:"mutated"`
Jiyong Parkb0788572018-12-20 22:10:17 +0900313
314 // Allows this module to use non-APEX version of libraries. Useful
315 // for building binaries that are started before APEXes are activated.
316 Bootstrap *bool
Jooyung Han097087b2019-10-22 19:32:18 +0900317
318 // Even if DeviceConfig().VndkUseCoreVariant() is set, this module must use vendor variant.
319 // see soong/cc/config/vndk.go
320 MustUseVendorVariant bool `blueprint:"mutated"`
Inseob Kim8471cda2019-11-15 09:59:12 +0900321
322 // Used by vendor snapshot to record dependencies from snapshot modules.
323 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900324 SnapshotStaticLibs []string `blueprint:"mutated"`
Inseob Kim8471cda2019-11-15 09:59:12 +0900325 SnapshotRuntimeLibs []string `blueprint:"mutated"`
Paul Duffin0cb37b92020-03-04 14:52:46 +0000326
327 Installable *bool
Colin Crossc511bc52020-04-07 16:50:32 +0000328
329 // Set by factories of module types that can only be referenced from variants compiled against
330 // the SDK.
331 AlwaysSdk bool `blueprint:"mutated"`
332
333 // Variant is an SDK variant created by sdkMutator
334 IsSdkVariant bool `blueprint:"mutated"`
335 // Set when both SDK and platform variants are exported to Make to trigger renaming the SDK
336 // variant to have a ".sdk" suffix.
337 SdkAndPlatformVariantVisibleToMake bool `blueprint:"mutated"`
Bill Peckham945441c2020-08-31 16:07:58 -0700338
339 // Normally Soong uses the directory structure to decide which modules
340 // should be included (framework) or excluded (non-framework) from the
Jose Galmes6f843bc2020-12-11 13:36:29 -0800341 // different snapshots (vendor, recovery, etc.), but this property
342 // allows a partner to exclude a module normally thought of as a
343 // framework module from the vendor snapshot.
344 Exclude_from_vendor_snapshot *bool
345
346 // Normally Soong uses the directory structure to decide which modules
347 // should be included (framework) or excluded (non-framework) from the
348 // different snapshots (vendor, recovery, etc.), but this property
349 // allows a partner to exclude a module normally thought of as a
350 // framework module from the recovery snapshot.
Jose Galmesf7294582020-11-13 12:07:36 -0800351 Exclude_from_recovery_snapshot *bool
Jiyong Park46a512f2020-12-04 18:02:13 +0900352
353 // List of APEXes that this module has private access to for testing purpose. The module
354 // can depend on libraries that are not exported by the APEXes and use private symbols
355 // from the exported libraries.
Martin Stjernholm855e90b2021-03-25 02:33:14 +0000356 Test_for []string `android:"arch_variant"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700357}
358
359type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900360 // whether this module should be allowed to be directly depended by other
361 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
Justin Yun63e9ec72020-10-29 16:49:43 +0900362 // If set to true, two variants will be built separately, one like
363 // normal, and the other limited to the set of libraries and headers
364 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700365 //
Justin Yun63e9ec72020-10-29 16:49:43 +0900366 // The vendor variant may be used with a different (newer) /system,
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700367 // so it shouldn't have any unversioned runtime dependencies, or
368 // make assumptions about the system that may not be true in the
369 // future.
370 //
Justin Yun63e9ec72020-10-29 16:49:43 +0900371 // If set to false, this module becomes inaccessible from /vendor modules.
Jiyong Park82e2bf32017-08-16 14:05:54 +0900372 //
Justin Yun6977e8a2020-10-29 18:24:11 +0900373 // The modules with vndk: {enabled: true} must define 'vendor_available'
Justin Yun0b1db6d2021-01-08 15:22:34 +0900374 // to 'true'.
Jiyong Park82e2bf32017-08-16 14:05:54 +0900375 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700376 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
377 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900378
Justin Yunebcf0c52021-01-08 18:00:19 +0900379 // This is the same as the "vendor_available" except that the install path
380 // of the vendor variant is /odm or /vendor/odm.
381 // By replacing "vendor_available: true" with "odm_available: true", the
382 // module will install its vendor variant to the /odm partition or /vendor/odm.
383 // As the modules with "odm_available: true" still create the vendor variants,
384 // they can link to the other vendor modules as the vendor_available modules do.
385 // Also, the vendor modules can link to odm_available modules.
386 //
387 // It may not be used for VNDK modules.
388 Odm_available *bool
389
Justin Yun63e9ec72020-10-29 16:49:43 +0900390 // whether this module should be allowed to be directly depended by other
391 // modules with `product_specific: true` or `product_available: true`.
392 // If set to true, an additional product variant will be built separately
393 // that is limited to the set of libraries and headers that are exposed to
394 // /product modules.
395 //
396 // The product variant may be used with a different (newer) /system,
397 // so it shouldn't have any unversioned runtime dependencies, or
398 // make assumptions about the system that may not be true in the
399 // future.
400 //
Justin Yun6977e8a2020-10-29 18:24:11 +0900401 // If set to false, this module becomes inaccessible from /product modules.
402 //
403 // Different from the 'vendor_available' property, the modules with
404 // vndk: {enabled: true} don't have to define 'product_available'. The VNDK
405 // library without 'product_available' may not be depended on by any other
406 // modules that has product variants including the product available VNDKs.
Justin Yun63e9ec72020-10-29 16:49:43 +0900407 //
408 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
409 // and PRODUCT_PRODUCT_VNDK_VERSION isn't set.
410 Product_available *bool
411
Jiyong Park5fb8c102018-04-09 12:03:06 +0900412 // whether this module is capable of being loaded with other instance
413 // (possibly an older version) of the same module in the same process.
414 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
415 // can be double loaded in a vendor process if the library is also a
416 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
417 // explicitly marked as `double_loadable: true` by the owner, or the dependency
418 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
419 Double_loadable *bool
Colin Cross127bb8b2020-12-16 16:46:01 -0800420
421 // IsLLNDK is set to true for the vendor variant of a cc_library module that has LLNDK stubs.
422 IsLLNDK bool `blueprint:"mutated"`
423
Colin Cross78212242021-01-06 14:51:30 -0800424 // IsVNDKUsingCoreVariant is true for VNDK modules if the global VndkUseCoreVariant option is
425 // set and the module is not listed in VndkMustUseVendorVariantList.
426 IsVNDKUsingCoreVariant bool `blueprint:"mutated"`
427
428 // IsVNDKCore is set if a VNDK module does not set the vndk.support_system_process property.
429 IsVNDKCore bool `blueprint:"mutated"`
430
431 // IsVNDKSP is set if a VNDK module sets the vndk.support_system_process property.
432 IsVNDKSP bool `blueprint:"mutated"`
433
434 // IsVNDKPrivate is set if a VNDK module sets the vndk.private property or an LLNDK
435 // module sets the llndk.private property.
436 IsVNDKPrivate bool `blueprint:"mutated"`
437
438 // IsVNDKProduct is set if a VNDK module sets the product_available property.
439 IsVNDKProduct bool `blueprint:"mutated"`
Colin Cross5271fea2021-04-27 13:06:04 -0700440
441 // IsVendorPublicLibrary is set for the core and product variants of a library that has
442 // vendor_public_library stubs.
443 IsVendorPublicLibrary bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800444}
445
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500446// ModuleContextIntf is an interface (on a module context helper) consisting of functions related
447// to understanding details about the type of the current module.
448// For example, one might call these functions to determine whether the current module is a static
449// library and/or is installed in vendor directories.
Colin Crossca860ac2016-01-04 14:34:37 -0800450type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800451 static() bool
452 staticBinary() bool
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700453 testBinary() bool
Jiyong Park1d1119f2019-07-29 21:27:18 +0900454 header() bool
Inseob Kim7f283f42020-06-01 21:53:49 +0900455 binary() bool
Inseob Kim1042d292020-06-01 23:23:05 +0900456 object() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700457 toolchain() config.Toolchain
Jooyung Hanccce2f22020-03-07 03:45:53 +0900458 canUseSdk() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700459 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800460 sdkVersion() string
Jiyong Parkb35a8192020-08-10 15:59:36 +0900461 minSdkVersion() string
462 isSdkVariant() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700463 useVndk() bool
Colin Cross95f1ca02020-10-29 20:47:22 -0700464 isNdk(config android.Config) bool
Colin Cross127bb8b2020-12-16 16:46:01 -0800465 IsLlndk() bool
466 IsLlndkPublic() bool
467 isImplementationForLLNDKPublic() bool
468 IsVndkPrivate() bool
Justin Yun8effde42017-06-23 19:24:43 +0900469 isVndk() bool
470 isVndkSp() bool
Ivan Lozanof9e21722020-12-02 09:00:51 -0500471 IsVndkExt() bool
Colin Cross5271fea2021-04-27 13:06:04 -0700472 IsVendorPublicLibrary() bool
Justin Yun5f7f7e82019-11-18 19:52:14 +0900473 inProduct() bool
474 inVendor() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800475 inRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700476 inVendorRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900477 inRecovery() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700478 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700479 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800480 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800481 isPgoCompile() bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800482 isNDKStubLibrary() bool
Ivan Lozanobd721262018-11-27 14:33:03 -0800483 useClangLld(actx ModuleContext) bool
Logan Chiene274fc92019-12-03 11:18:32 -0800484 isForPlatform() bool
Colin Crosse07f2312020-08-13 11:24:56 -0700485 apexVariationName() string
Dan Albertc8060532020-07-22 22:32:17 -0700486 apexSdkVersion() android.ApiLevel
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900487 bootstrap() bool
Vic Yangefd249e2018-11-12 20:19:56 -0800488 mustUseVendorVariant() bool
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700489 nativeCoverage() bool
Colin Cross56a83212020-09-15 18:30:11 -0700490 directlyInAnyApex() bool
Colin Cross95b07f22020-12-16 11:06:50 -0800491 isPreventInstall() bool
Cindy Zhou5d5cfc12021-01-09 08:25:22 -0800492 isCfiAssemblySupportEnabled() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800493}
494
495type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700496 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800497 ModuleContextIntf
498}
499
500type BaseModuleContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700501 android.BaseModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800502 ModuleContextIntf
503}
504
Colin Cross37047f12016-12-13 17:06:13 -0800505type DepsContext interface {
506 android.BottomUpMutatorContext
507 ModuleContextIntf
508}
509
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500510// feature represents additional (optional) steps to building cc-related modules, such as invocation
511// of clang-tidy.
Colin Crossca860ac2016-01-04 14:34:37 -0800512type feature interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800513 flags(ctx ModuleContext, flags Flags) Flags
514 props() []interface{}
515}
516
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500517// compiler is the interface for a compiler helper object. Different module decorators may implement
518// this helper differently. For example, compiling a `cc_library` may use a different build
519// statement than building a `toolchain_library`.
Colin Crossca860ac2016-01-04 14:34:37 -0800520type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700521 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800522 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800523 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700524 compilerProps() []interface{}
525
Colin Cross76fada02016-07-27 10:31:13 -0700526 appendCflags([]string)
527 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700528 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800529}
530
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500531// linker is the interface for a linker decorator object. Individual module types can provide
532// their own implementation for this decorator, and thus specify custom logic regarding build
533// statements pertaining to linking.
Colin Crossca860ac2016-01-04 14:34:37 -0800534type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700535 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800536 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700537 linkerFlags(ctx ModuleContext, flags Flags) Flags
538 linkerProps() []interface{}
Ivan Lozanobd721262018-11-27 14:33:03 -0800539 useClangLld(actx ModuleContext) bool
Colin Cross42742b82016-08-01 13:20:05 -0700540
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700541 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700542 appendLdflags([]string)
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900543 unstrippedOutputFilePath() android.Path
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700544
545 nativeCoverage() bool
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900546 coverageOutputFilePath() android.OptionalPath
Paul Duffin13f02712020-03-06 12:30:43 +0000547
548 // Get the deps that have been explicitly specified in the properties.
Paul Duffin13f02712020-03-06 12:30:43 +0000549 linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps
550}
551
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500552// specifiedDeps is a tuple struct representing dependencies of a linked binary owned by the linker.
Paul Duffin13f02712020-03-06 12:30:43 +0000553type specifiedDeps struct {
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500554 sharedLibs []string
555 // Note nil and [] are semantically distinct. [] prevents linking against the defaults (usually
556 // libc, libm, etc.)
Colin Cross6b8f4252021-07-22 11:39:44 -0700557 systemSharedLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -0800558}
559
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500560// installer is the interface for an installer helper object. This helper is responsible for
561// copying build outputs to the appropriate locations so that they may be installed on device.
Colin Crossca860ac2016-01-04 14:34:37 -0800562type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700563 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700564 install(ctx ModuleContext, path android.Path)
Paul Duffin0cb37b92020-03-04 14:52:46 +0000565 everInstallable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800566 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700567 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700568 hostToolPath() android.OptionalPath
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900569 relativeInstallPath() string
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100570 makeUninstallable(mod *Module)
Inseob Kim800d1142021-06-14 12:03:51 +0900571 installInRoot() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800572}
573
Chris Parsons8d6e4332021-02-22 16:13:50 -0500574// bazelHandler is the interface for a helper object related to deferring to Bazel for
575// processing a module (during Bazel mixed builds). Individual module types should define
576// their own bazel handler if they support deferring to Bazel.
577type bazelHandler interface {
578 // Issue query to Bazel to retrieve information about Bazel's view of the current module.
579 // If Bazel returns this information, set module properties on the current module to reflect
580 // the returned information.
581 // Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
582 generateBazelBuildActions(ctx android.ModuleContext, label string) bool
583}
584
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800585type xref interface {
586 XrefCcFiles() android.Paths
587}
588
Colin Cross6e511a92020-07-27 21:26:48 -0700589type libraryDependencyKind int
590
591const (
592 headerLibraryDependency = iota
593 sharedLibraryDependency
594 staticLibraryDependency
595)
596
597func (k libraryDependencyKind) String() string {
598 switch k {
599 case headerLibraryDependency:
600 return "headerLibraryDependency"
601 case sharedLibraryDependency:
602 return "sharedLibraryDependency"
603 case staticLibraryDependency:
604 return "staticLibraryDependency"
605 default:
606 panic(fmt.Errorf("unknown libraryDependencyKind %d", k))
607 }
608}
609
610type libraryDependencyOrder int
611
612const (
613 earlyLibraryDependency = -1
614 normalLibraryDependency = 0
615 lateLibraryDependency = 1
616)
617
618func (o libraryDependencyOrder) String() string {
619 switch o {
620 case earlyLibraryDependency:
621 return "earlyLibraryDependency"
622 case normalLibraryDependency:
623 return "normalLibraryDependency"
624 case lateLibraryDependency:
625 return "lateLibraryDependency"
626 default:
627 panic(fmt.Errorf("unknown libraryDependencyOrder %d", o))
628 }
629}
630
631// libraryDependencyTag is used to tag dependencies on libraries. Unlike many dependency
632// tags that have a set of predefined tag objects that are reused for each dependency, a
633// libraryDependencyTag is designed to contain extra metadata and is constructed as needed.
634// That means that comparing a libraryDependencyTag for equality will only be equal if all
635// of the metadata is equal. Most usages will want to type assert to libraryDependencyTag and
636// then check individual metadata fields instead.
637type libraryDependencyTag struct {
638 blueprint.BaseDependencyTag
639
640 // These are exported so that fmt.Printf("%#v") can call their String methods.
641 Kind libraryDependencyKind
642 Order libraryDependencyOrder
643
644 wholeStatic bool
645
646 reexportFlags bool
647 explicitlyVersioned bool
648 dataLib bool
649 ndk bool
650
651 staticUnwinder bool
652
653 makeSuffix string
Jiyong Parke3867542020-12-03 17:28:25 +0900654
Cindy Zhou18417cb2020-12-10 07:12:38 -0800655 // Whether or not this dependency should skip the apex dependency check
656 skipApexAllowedDependenciesCheck bool
657
Jiyong Parke3867542020-12-03 17:28:25 +0900658 // Whether or not this dependency has to be followed for the apex variants
659 excludeInApex bool
Colin Cross6e511a92020-07-27 21:26:48 -0700660}
661
662// header returns true if the libraryDependencyTag is tagging a header lib dependency.
663func (d libraryDependencyTag) header() bool {
664 return d.Kind == headerLibraryDependency
665}
666
667// shared returns true if the libraryDependencyTag is tagging a shared lib dependency.
668func (d libraryDependencyTag) shared() bool {
669 return d.Kind == sharedLibraryDependency
670}
671
672// shared returns true if the libraryDependencyTag is tagging a static lib dependency.
673func (d libraryDependencyTag) static() bool {
674 return d.Kind == staticLibraryDependency
675}
676
Colin Crosse9fe2942020-11-10 18:12:15 -0800677// InstallDepNeeded returns true for shared libraries so that shared library dependencies of
678// binaries or other shared libraries are installed as dependencies.
679func (d libraryDependencyTag) InstallDepNeeded() bool {
680 return d.shared()
681}
682
683var _ android.InstallNeededDependencyTag = libraryDependencyTag{}
684
685// dependencyTag is used for tagging miscellaneous dependency types that don't fit into
Colin Cross6e511a92020-07-27 21:26:48 -0700686// libraryDependencyTag. Each tag object is created globally and reused for multiple
687// dependencies (although since the object contains no references, assigning a tag to a
688// variable and modifying it will not modify the original). Users can compare the tag
689// returned by ctx.OtherModuleDependencyTag against the global original
690type dependencyTag struct {
691 blueprint.BaseDependencyTag
692 name string
693}
694
Colin Crosse9fe2942020-11-10 18:12:15 -0800695// installDependencyTag is used for tagging miscellaneous dependency types that don't fit into
696// libraryDependencyTag, but where the dependency needs to be installed when the parent is
697// installed.
698type installDependencyTag struct {
699 blueprint.BaseDependencyTag
700 android.InstallAlwaysNeededDependencyTag
701 name string
702}
703
Colin Crossc99deeb2016-04-11 15:06:20 -0700704var (
Colin Cross6e511a92020-07-27 21:26:48 -0700705 genSourceDepTag = dependencyTag{name: "gen source"}
706 genHeaderDepTag = dependencyTag{name: "gen header"}
707 genHeaderExportDepTag = dependencyTag{name: "gen header export"}
708 objDepTag = dependencyTag{name: "obj"}
Jiyong Parkd630bdd2020-11-25 11:47:24 +0900709 dynamicLinkerDepTag = installDependencyTag{name: "dynamic linker"}
Colin Cross6e511a92020-07-27 21:26:48 -0700710 reuseObjTag = dependencyTag{name: "reuse objects"}
711 staticVariantTag = dependencyTag{name: "static variant"}
712 vndkExtDepTag = dependencyTag{name: "vndk extends"}
713 dataLibDepTag = dependencyTag{name: "data lib"}
Colin Crosse9fe2942020-11-10 18:12:15 -0800714 runtimeDepTag = installDependencyTag{name: "runtime lib"}
Colin Cross6e511a92020-07-27 21:26:48 -0700715 testPerSrcDepTag = dependencyTag{name: "test_per_src"}
Colin Cross0de8a1e2020-09-18 14:15:30 -0700716 stubImplDepTag = dependencyTag{name: "stub_impl"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700717)
718
Roland Levillainf89cd092019-07-29 16:22:59 +0100719func IsSharedDepTag(depTag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700720 ccLibDepTag, ok := depTag.(libraryDependencyTag)
721 return ok && ccLibDepTag.shared()
722}
723
724func IsStaticDepTag(depTag blueprint.DependencyTag) bool {
725 ccLibDepTag, ok := depTag.(libraryDependencyTag)
726 return ok && ccLibDepTag.static()
Roland Levillainf89cd092019-07-29 16:22:59 +0100727}
728
Zach Johnson3df4e632020-11-06 11:56:27 -0800729func IsHeaderDepTag(depTag blueprint.DependencyTag) bool {
730 ccLibDepTag, ok := depTag.(libraryDependencyTag)
731 return ok && ccLibDepTag.header()
732}
733
Roland Levillainf89cd092019-07-29 16:22:59 +0100734func IsRuntimeDepTag(depTag blueprint.DependencyTag) bool {
Colin Crosse9fe2942020-11-10 18:12:15 -0800735 return depTag == runtimeDepTag
Roland Levillainf89cd092019-07-29 16:22:59 +0100736}
737
738func IsTestPerSrcDepTag(depTag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700739 ccDepTag, ok := depTag.(dependencyTag)
Roland Levillainf89cd092019-07-29 16:22:59 +0100740 return ok && ccDepTag == testPerSrcDepTag
741}
742
Colin Crossca860ac2016-01-04 14:34:37 -0800743// Module contains the properties and members used by all C/C++ module types, and implements
744// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500745// to construct the output file. Behavior can be customized with a Customizer, or "decorator",
746// interface.
747//
748// To define a C/C++ related module, construct a new Module object and point its delegates to
749// type-specific structs. These delegates will be invoked to register module-specific build
750// statements which may be unique to the module type. For example, module.compiler.compile() should
751// be defined so as to register build statements which are responsible for compiling the module.
752//
753// Another example: to construct a cc_binary module, one can create a `cc.binaryDecorator` struct
754// which implements the `linker` and `installer` interfaces, and points the `linker` and `installer`
755// members of the cc.Module to this decorator. Thus, a cc_binary module has custom linker and
756// installer logic.
Colin Crossca860ac2016-01-04 14:34:37 -0800757type Module struct {
hamzeh41ad8812021-07-07 14:00:07 -0700758 FuzzModule
759
Jiyong Parkd1063c12019-07-17 20:08:41 +0900760 android.SdkBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500761 android.BazelModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700762
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700763 VendorProperties VendorProperties
hamzeh41ad8812021-07-07 14:00:07 -0700764 Properties BaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700765
Colin Crossca860ac2016-01-04 14:34:37 -0800766 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700767 hod android.HostOrDeviceSupported
768 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700769
Paul Duffina0843f62019-12-13 19:50:38 +0000770 // Allowable SdkMemberTypes of this module type.
771 sdkMemberTypes []android.SdkMemberType
772
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500773 // decorator delegates, initialize before calling Init
774 // these may contain module-specific implementations, and effectively allow for custom
775 // type-specific logic. These members may reference different objects or the same object.
776 // Functions of these decorators will be invoked to initialize and register type-specific
777 // build statements.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500778 compiler compiler
779 linker linker
780 installer installer
781 bazelHandler bazelHandler
Chris Parsonsef6e0cf2020-12-01 18:26:21 -0500782
783 features []feature
784 stl *stl
785 sanitize *sanitize
786 coverage *coverage
787 sabi *sabi
788 vndkdep *vndkdep
789 lto *lto
790 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800791
Colin Cross31076b32020-10-23 17:22:06 -0700792 library libraryInterface
793
Colin Cross635c3b02016-05-18 15:37:25 -0700794 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800795
Colin Crossb98c8b02016-07-29 13:44:28 -0700796 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700797
798 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800799
800 // Flags used to compile this module
801 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700802
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800803 // only non-nil when this is a shared library that reuses the objects of a static library
Colin Cross0de8a1e2020-09-18 14:15:30 -0700804 staticAnalogue *StaticLibraryInfo
Inseob Kim9516ee92019-05-09 10:56:13 +0900805
806 makeLinkType string
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800807 // Kythe (source file indexer) paths for this compilation module
808 kytheFiles android.Paths
Jooyung Han75568392020-03-20 04:29:24 +0900809
810 // For apex variants, this is set as apex.min_sdk_version
Dan Albertc8060532020-07-22 22:32:17 -0700811 apexSdkVersion android.ApiLevel
Colin Cross56a83212020-09-15 18:30:11 -0700812
813 hideApexVariantFromMake bool
Colin Crossc472d572015-03-17 15:06:21 -0700814}
815
Lukacs T. Berkid18d8ca2021-06-25 09:11:22 +0200816func (c *Module) AddJSONData(d *map[string]interface{}) {
817 c.AndroidModuleBase().AddJSONData(d)
818 (*d)["Cc"] = map[string]interface{}{
819 "SdkVersion": c.SdkVersion(),
820 "MinSdkVersion": c.MinSdkVersion(),
821 "VndkVersion": c.VndkVersion(),
822 "ProductSpecific": c.ProductSpecific(),
823 "SocSpecific": c.SocSpecific(),
824 "DeviceSpecific": c.DeviceSpecific(),
825 "InProduct": c.InProduct(),
826 "InVendor": c.InVendor(),
827 "InRamdisk": c.InRamdisk(),
828 "InVendorRamdisk": c.InVendorRamdisk(),
829 "InRecovery": c.InRecovery(),
830 "VendorAvailable": c.VendorAvailable(),
831 "ProductAvailable": c.ProductAvailable(),
832 "RamdiskAvailable": c.RamdiskAvailable(),
833 "VendorRamdiskAvailable": c.VendorRamdiskAvailable(),
834 "RecoveryAvailable": c.RecoveryAvailable(),
835 "OdmAvailable": c.OdmAvailable(),
836 "InstallInData": c.InstallInData(),
837 "InstallInRamdisk": c.InstallInRamdisk(),
838 "InstallInSanitizerDir": c.InstallInSanitizerDir(),
839 "InstallInVendorRamdisk": c.InstallInVendorRamdisk(),
840 "InstallInRecovery": c.InstallInRecovery(),
841 "InstallInRoot": c.InstallInRoot(),
842 "IsVndk": c.IsVndk(),
843 "IsVndkExt": c.IsVndkExt(),
844 "IsVndkPrivate": c.IsVndkPrivate(),
845 "IsVndkSp": c.IsVndkSp(),
846 "IsLlndk": c.IsLlndk(),
847 "IsLlndkPublic": c.IsLlndkPublic(),
848 "IsSnapshotLibrary": c.IsSnapshotLibrary(),
849 "IsSnapshotPrebuilt": c.IsSnapshotPrebuilt(),
850 "IsVendorPublicLibrary": c.IsVendorPublicLibrary(),
851 "ApexSdkVersion": c.apexSdkVersion,
852 "TestFor": c.TestFor(),
853 }
854}
855
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500856func (c *Module) SetPreventInstall() {
857 c.Properties.PreventInstall = true
858}
859
860func (c *Module) SetHideFromMake() {
861 c.Properties.HideFromMake = true
862}
863
Ivan Lozanod7586b62021-04-01 09:49:36 -0400864func (c *Module) HiddenFromMake() bool {
865 return c.Properties.HideFromMake
866}
867
Ivan Lozano52767be2019-10-18 14:49:46 -0700868func (c *Module) Toc() android.OptionalPath {
869 if c.linker != nil {
870 if library, ok := c.linker.(libraryInterface); ok {
871 return library.toc()
872 }
873 }
874 panic(fmt.Errorf("Toc() called on non-library module: %q", c.BaseModuleName()))
875}
876
877func (c *Module) ApiLevel() string {
878 if c.linker != nil {
879 if stub, ok := c.linker.(*stubDecorator); ok {
Dan Albert1a246272020-07-06 14:49:35 -0700880 return stub.apiLevel.String()
Ivan Lozano52767be2019-10-18 14:49:46 -0700881 }
882 }
883 panic(fmt.Errorf("ApiLevel() called on non-stub library module: %q", c.BaseModuleName()))
884}
885
886func (c *Module) Static() bool {
887 if c.linker != nil {
888 if library, ok := c.linker.(libraryInterface); ok {
889 return library.static()
890 }
891 }
892 panic(fmt.Errorf("Static() called on non-library module: %q", c.BaseModuleName()))
893}
894
895func (c *Module) Shared() bool {
896 if c.linker != nil {
897 if library, ok := c.linker.(libraryInterface); ok {
898 return library.shared()
899 }
900 }
901 panic(fmt.Errorf("Shared() called on non-library module: %q", c.BaseModuleName()))
902}
903
904func (c *Module) SelectedStl() string {
Colin Crossc511bc52020-04-07 16:50:32 +0000905 if c.stl != nil {
906 return c.stl.Properties.SelectedStl
907 }
908 return ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700909}
910
911func (c *Module) ToolchainLibrary() bool {
912 if _, ok := c.linker.(*toolchainLibraryDecorator); ok {
913 return true
914 }
915 return false
916}
917
918func (c *Module) NdkPrebuiltStl() bool {
919 if _, ok := c.linker.(*ndkPrebuiltStlLinker); ok {
920 return true
921 }
922 return false
923}
924
925func (c *Module) StubDecorator() bool {
926 if _, ok := c.linker.(*stubDecorator); ok {
927 return true
928 }
929 return false
930}
931
932func (c *Module) SdkVersion() string {
933 return String(c.Properties.Sdk_version)
934}
935
Artur Satayev480e25b2020-04-27 18:53:18 +0100936func (c *Module) MinSdkVersion() string {
937 return String(c.Properties.Min_sdk_version)
938}
939
Dan Albert92fe7402020-07-15 13:33:30 -0700940func (c *Module) SplitPerApiLevel() bool {
Colin Cross1348ce32020-10-01 13:37:16 -0700941 if !c.canUseSdk() {
942 return false
943 }
Dan Albert92fe7402020-07-15 13:33:30 -0700944 if linker, ok := c.linker.(*objectLinker); ok {
945 return linker.isCrt()
946 }
947 return false
948}
949
Colin Crossc511bc52020-04-07 16:50:32 +0000950func (c *Module) AlwaysSdk() bool {
951 return c.Properties.AlwaysSdk || Bool(c.Properties.Sdk_variant_only)
952}
953
Ivan Lozano183a3212019-10-18 14:18:45 -0700954func (c *Module) CcLibrary() bool {
955 if c.linker != nil {
956 if _, ok := c.linker.(*libraryDecorator); ok {
957 return true
958 }
Colin Crossd48fe732020-09-23 20:37:24 -0700959 if _, ok := c.linker.(*prebuiltLibraryLinker); ok {
960 return true
961 }
Ivan Lozano183a3212019-10-18 14:18:45 -0700962 }
963 return false
964}
965
966func (c *Module) CcLibraryInterface() bool {
Ivan Lozano52767be2019-10-18 14:49:46 -0700967 if _, ok := c.linker.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700968 return true
969 }
970 return false
971}
972
Ivan Lozano2b262972019-11-21 12:30:50 -0800973func (c *Module) NonCcVariants() bool {
974 return false
975}
976
Ivan Lozano183a3212019-10-18 14:18:45 -0700977func (c *Module) SetStatic() {
978 if c.linker != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700979 if library, ok := c.linker.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700980 library.setStatic()
981 return
982 }
983 }
984 panic(fmt.Errorf("SetStatic called on non-library module: %q", c.BaseModuleName()))
985}
986
987func (c *Module) SetShared() {
988 if c.linker != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700989 if library, ok := c.linker.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700990 library.setShared()
991 return
992 }
993 }
994 panic(fmt.Errorf("SetShared called on non-library module: %q", c.BaseModuleName()))
995}
996
997func (c *Module) BuildStaticVariant() bool {
998 if c.linker != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700999 if library, ok := c.linker.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -07001000 return library.buildStatic()
1001 }
1002 }
1003 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", c.BaseModuleName()))
1004}
1005
1006func (c *Module) BuildSharedVariant() bool {
1007 if c.linker != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 if library, ok := c.linker.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -07001009 return library.buildShared()
1010 }
1011 }
1012 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", c.BaseModuleName()))
1013}
1014
1015func (c *Module) Module() android.Module {
1016 return c
1017}
1018
Jiyong Parkc20eee32018-09-05 22:36:17 +09001019func (c *Module) OutputFile() android.OptionalPath {
1020 return c.outputFile
1021}
1022
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001023func (c *Module) CoverageFiles() android.Paths {
1024 if c.linker != nil {
1025 if library, ok := c.linker.(libraryInterface); ok {
1026 return library.objs().coverageFiles
1027 }
1028 }
1029 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", c.BaseModuleName()))
1030}
1031
Ivan Lozano183a3212019-10-18 14:18:45 -07001032var _ LinkableInterface = (*Module)(nil)
1033
Jiyong Park719b4462019-01-13 00:39:51 +09001034func (c *Module) UnstrippedOutputFile() android.Path {
Jiyong Parkaf6d8952019-01-31 12:21:23 +09001035 if c.linker != nil {
1036 return c.linker.unstrippedOutputFilePath()
Jiyong Park719b4462019-01-13 00:39:51 +09001037 }
1038 return nil
1039}
1040
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001041func (c *Module) CoverageOutputFile() android.OptionalPath {
1042 if c.linker != nil {
1043 return c.linker.coverageOutputFilePath()
1044 }
1045 return android.OptionalPath{}
1046}
1047
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001048func (c *Module) RelativeInstallPath() string {
1049 if c.installer != nil {
1050 return c.installer.relativeInstallPath()
1051 }
1052 return ""
1053}
1054
Jooyung Han344d5432019-08-23 11:17:39 +09001055func (c *Module) VndkVersion() string {
Justin Yun5f7f7e82019-11-18 19:52:14 +09001056 return c.Properties.VndkVersion
Jooyung Han344d5432019-08-23 11:17:39 +09001057}
1058
Colin Cross36242852017-06-23 15:06:31 -07001059func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -07001060 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001061 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -07001062 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -08001063 }
1064 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -07001065 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -08001066 }
1067 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -07001068 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -08001069 }
Colin Crossa8e07cc2016-04-04 15:07:06 -07001070 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -07001071 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -07001072 }
Colin Cross16b23492016-01-06 14:41:07 -08001073 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -07001074 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -08001075 }
Dan Willemsen581341d2017-02-09 16:16:31 -08001076 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -07001077 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001078 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001079 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -07001080 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001081 }
Justin Yun8effde42017-06-23 19:24:43 +09001082 if c.vndkdep != nil {
1083 c.AddProperties(c.vndkdep.props()...)
1084 }
Stephen Craneba090d12017-05-09 15:44:35 -07001085 if c.lto != nil {
1086 c.AddProperties(c.lto.props()...)
1087 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001088 if c.pgo != nil {
1089 c.AddProperties(c.pgo.props()...)
1090 }
Colin Crossca860ac2016-01-04 14:34:37 -08001091 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -07001092 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -08001093 }
Colin Crossc472d572015-03-17 15:06:21 -07001094
Colin Cross36242852017-06-23 15:06:31 -07001095 android.InitAndroidArchModule(c, c.hod, c.multilib)
Liz Kammerea6666f2021-02-17 10:17:28 -05001096 android.InitBazelModule(c)
Jiyong Park7916bfc2019-09-30 19:13:12 +09001097 android.InitApexModule(c)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001098 android.InitSdkAwareModule(c)
Jooyung Han18020ea2019-11-13 10:50:48 +09001099 android.InitDefaultableModule(c)
Jiyong Park9d452992018-10-03 00:38:19 +09001100
Colin Cross36242852017-06-23 15:06:31 -07001101 return c
Colin Crossc472d572015-03-17 15:06:21 -07001102}
1103
Ivan Lozano52767be2019-10-18 14:49:46 -07001104func (c *Module) UseVndk() bool {
Inseob Kim64c43952019-08-26 16:52:35 +09001105 return c.Properties.VndkVersion != ""
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001106}
1107
Colin Crossc511bc52020-04-07 16:50:32 +00001108func (c *Module) canUseSdk() bool {
Colin Cross94e347e2021-01-19 14:56:07 -08001109 return c.Os() == android.Android && c.Target().NativeBridge == android.NativeBridgeDisabled &&
1110 !c.UseVndk() && !c.InRamdisk() && !c.InRecovery() && !c.InVendorRamdisk()
Colin Crossc511bc52020-04-07 16:50:32 +00001111}
1112
1113func (c *Module) UseSdk() bool {
1114 if c.canUseSdk() {
Colin Cross1348ce32020-10-01 13:37:16 -07001115 return String(c.Properties.Sdk_version) != ""
Colin Crossc511bc52020-04-07 16:50:32 +00001116 }
1117 return false
1118}
1119
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -08001120func (c *Module) isCoverageVariant() bool {
1121 return c.coverage.Properties.IsCoverageVariant
1122}
1123
Colin Cross95f1ca02020-10-29 20:47:22 -07001124func (c *Module) IsNdk(config android.Config) bool {
1125 return inList(c.BaseModuleName(), *getNDKKnownLibs(config))
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001126}
1127
Colin Cross127bb8b2020-12-16 16:46:01 -08001128func (c *Module) IsLlndk() bool {
1129 return c.VendorProperties.IsLLNDK
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001130}
1131
Colin Cross127bb8b2020-12-16 16:46:01 -08001132func (c *Module) IsLlndkPublic() bool {
Colin Cross78212242021-01-06 14:51:30 -08001133 return c.VendorProperties.IsLLNDK && !c.VendorProperties.IsVNDKPrivate
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001134}
1135
Colin Cross1f3f1302021-04-26 18:37:44 -07001136func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -04001137 lib := moduleLibraryInterface(m)
Colin Cross1f3f1302021-04-26 18:37:44 -07001138 return lib != nil && (lib.hasLLNDKStubs() || lib.hasLLNDKHeaders())
Ivan Lozano3a7d0002021-03-30 12:19:36 -04001139}
1140
Colin Cross5271fea2021-04-27 13:06:04 -07001141func (m *Module) NeedsVendorPublicLibraryVariants() bool {
1142 lib := moduleLibraryInterface(m)
1143 return lib != nil && (lib.hasVendorPublicLibrary())
1144}
1145
1146// IsVendorPublicLibrary returns true for vendor public libraries.
1147func (c *Module) IsVendorPublicLibrary() bool {
1148 return c.VendorProperties.IsVendorPublicLibrary
1149}
1150
Ivan Lozanod7586b62021-04-01 09:49:36 -04001151func (c *Module) HasLlndkStubs() bool {
1152 lib := moduleLibraryInterface(c)
1153 return lib != nil && lib.hasLLNDKStubs()
1154}
1155
1156func (c *Module) StubsVersion() string {
1157 if lib, ok := c.linker.(versionedInterface); ok {
1158 return lib.stubsVersion()
1159 }
1160 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", c.BaseModuleName()))
1161}
1162
Colin Cross127bb8b2020-12-16 16:46:01 -08001163// isImplementationForLLNDKPublic returns true for any variant of a cc_library that has LLNDK stubs
1164// and does not set llndk.vendor_available: false.
1165func (c *Module) isImplementationForLLNDKPublic() bool {
1166 library, _ := c.library.(*libraryDecorator)
1167 return library != nil && library.hasLLNDKStubs() &&
Colin Cross0fb7fcd2021-03-02 11:00:07 -08001168 !Bool(library.Properties.Llndk.Private)
Colin Cross127bb8b2020-12-16 16:46:01 -08001169}
1170
Justin Yunfd9e8042020-12-23 18:23:14 +09001171// Returns true for LLNDK-private, VNDK-SP-private, and VNDK-core-private.
Colin Cross127bb8b2020-12-16 16:46:01 -08001172func (c *Module) IsVndkPrivate() bool {
Justin Yunfd9e8042020-12-23 18:23:14 +09001173 // Check if VNDK-core-private or VNDK-SP-private
1174 if c.IsVndk() {
Justin Yunc0d8c492021-01-07 17:45:31 +09001175 return Bool(c.vndkdep.Properties.Vndk.Private)
Justin Yunfd9e8042020-12-23 18:23:14 +09001176 }
1177
1178 // Check if LLNDK-private
1179 if library, ok := c.library.(*libraryDecorator); ok && c.IsLlndk() {
Justin Yunc0d8c492021-01-07 17:45:31 +09001180 return Bool(library.Properties.Llndk.Private)
Justin Yunfd9e8042020-12-23 18:23:14 +09001181 }
1182
1183 return false
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001184}
1185
Ivan Lozano52767be2019-10-18 14:49:46 -07001186func (c *Module) IsVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +08001187 if vndkdep := c.vndkdep; vndkdep != nil {
1188 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +09001189 }
1190 return false
1191}
1192
Yi Kong7e53c572018-02-14 18:16:12 +08001193func (c *Module) isPgoCompile() bool {
1194 if pgo := c.pgo; pgo != nil {
1195 return pgo.Properties.PgoCompile
1196 }
1197 return false
1198}
1199
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -08001200func (c *Module) isNDKStubLibrary() bool {
1201 if _, ok := c.compiler.(*stubDecorator); ok {
1202 return true
1203 }
1204 return false
1205}
1206
Ivan Lozanod7586b62021-04-01 09:49:36 -04001207func (c *Module) IsVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +08001208 if vndkdep := c.vndkdep; vndkdep != nil {
1209 return vndkdep.isVndkSp()
1210 }
1211 return false
1212}
1213
Ivan Lozanof9e21722020-12-02 09:00:51 -05001214func (c *Module) IsVndkExt() bool {
Logan Chienf3511742017-10-31 18:04:35 +08001215 if vndkdep := c.vndkdep; vndkdep != nil {
1216 return vndkdep.isVndkExt()
1217 }
1218 return false
1219}
1220
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001221func (c *Module) SubName() string {
1222 return c.Properties.SubName
1223}
1224
Ivan Lozano52767be2019-10-18 14:49:46 -07001225func (c *Module) MustUseVendorVariant() bool {
Ivan Lozanod7586b62021-04-01 09:49:36 -04001226 return c.IsVndkSp() || c.Properties.MustUseVendorVariant
Vic Yangefd249e2018-11-12 20:19:56 -08001227}
1228
Logan Chienf3511742017-10-31 18:04:35 +08001229func (c *Module) getVndkExtendsModuleName() string {
1230 if vndkdep := c.vndkdep; vndkdep != nil {
1231 return vndkdep.getVndkExtendsModuleName()
1232 }
1233 return ""
1234}
1235
Jiyong Park25fc6a92018-11-18 18:02:45 +09001236func (c *Module) IsStubs() bool {
Colin Cross31076b32020-10-23 17:22:06 -07001237 if lib := c.library; lib != nil {
1238 return lib.buildStubs()
Jiyong Park25fc6a92018-11-18 18:02:45 +09001239 }
1240 return false
1241}
1242
1243func (c *Module) HasStubsVariants() bool {
Colin Cross31076b32020-10-23 17:22:06 -07001244 if lib := c.library; lib != nil {
1245 return lib.hasStubsVariants()
Peter Collingbourne3478bb22019-04-24 14:41:12 -07001246 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001247 return false
1248}
1249
Colin Cross0477b422020-10-13 18:43:54 -07001250// If this is a stubs library, ImplementationModuleName returns the name of the module that contains
1251// the implementation. If it is an implementation library it returns its own name.
1252func (c *Module) ImplementationModuleName(ctx android.BaseModuleContext) string {
1253 name := ctx.OtherModuleName(c)
1254 if versioned, ok := c.linker.(versionedInterface); ok {
1255 name = versioned.implementationModuleName(name)
1256 }
1257 return name
1258}
1259
Martin Stjernholm2856c662020-12-02 15:03:42 +00001260// Similar to ImplementationModuleName, but uses the Make variant of the module
1261// name as base name, for use in AndroidMk output. E.g. for a prebuilt module
1262// where the Soong name is prebuilt_foo, this returns foo (which works in Make
1263// under the premise that the prebuilt module overrides its source counterpart
1264// if it is exposed to Make).
1265func (c *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
1266 name := c.BaseModuleName()
1267 if versioned, ok := c.linker.(versionedInterface); ok {
1268 name = versioned.implementationModuleName(name)
1269 }
1270 return name
1271}
1272
Jiyong Park7d55b612021-06-11 17:22:09 +09001273func (c *Module) Bootstrap() bool {
Jiyong Parka4b9dd02019-01-16 22:53:13 +09001274 return Bool(c.Properties.Bootstrap)
1275}
1276
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -07001277func (c *Module) nativeCoverage() bool {
Pirama Arumuga Nainar5f69b9a2019-09-12 13:18:48 -07001278 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
1279 if c.Target().NativeBridge == android.NativeBridgeEnabled {
1280 return false
1281 }
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -07001282 return c.linker != nil && c.linker.nativeCoverage()
1283}
1284
Ivan Lozano3a7d0002021-03-30 12:19:36 -04001285func (c *Module) IsSnapshotPrebuilt() bool {
Ivan Lozanod1dec542021-05-26 15:33:11 -04001286 if p, ok := c.linker.(SnapshotInterface); ok {
1287 return p.IsSnapshotPrebuilt()
Inseob Kimeec88e12020-01-22 11:11:29 +09001288 }
1289 return false
Inseob Kim8471cda2019-11-15 09:59:12 +09001290}
1291
Bill Peckham945441c2020-08-31 16:07:58 -07001292func (c *Module) ExcludeFromVendorSnapshot() bool {
1293 return Bool(c.Properties.Exclude_from_vendor_snapshot)
1294}
1295
Jose Galmesf7294582020-11-13 12:07:36 -08001296func (c *Module) ExcludeFromRecoverySnapshot() bool {
1297 return Bool(c.Properties.Exclude_from_recovery_snapshot)
1298}
1299
Jiyong Parkf1194352019-02-25 11:05:47 +09001300func isBionic(name string) bool {
1301 switch name {
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09001302 case "libc", "libm", "libdl", "libdl_android", "linker", "linkerconfig":
Jiyong Parkf1194352019-02-25 11:05:47 +09001303 return true
1304 }
1305 return false
1306}
1307
Martin Stjernholm279de572019-09-10 23:18:20 +01001308func InstallToBootstrap(name string, config android.Config) bool {
Peter Collingbourne3478bb22019-04-24 14:41:12 -07001309 if name == "libclang_rt.hwasan-aarch64-android" {
Jooyung Han8ce8db92020-05-15 19:05:05 +09001310 return true
Peter Collingbourne3478bb22019-04-24 14:41:12 -07001311 }
1312 return isBionic(name)
1313}
1314
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001315func (c *Module) XrefCcFiles() android.Paths {
1316 return c.kytheFiles
1317}
1318
Cindy Zhou5d5cfc12021-01-09 08:25:22 -08001319func (c *Module) isCfiAssemblySupportEnabled() bool {
1320 return c.sanitize != nil &&
1321 Bool(c.sanitize.Properties.Sanitize.Config.Cfi_assembly_support)
1322}
1323
Inseob Kim800d1142021-06-14 12:03:51 +09001324func (c *Module) InstallInRoot() bool {
1325 return c.installer != nil && c.installer.installInRoot()
1326}
1327
Colin Crossca860ac2016-01-04 14:34:37 -08001328type baseModuleContext struct {
Colin Cross0ea8ba82019-06-06 14:33:29 -07001329 android.BaseModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -08001330 moduleContextImpl
1331}
1332
Colin Cross37047f12016-12-13 17:06:13 -08001333type depsContext struct {
1334 android.BottomUpMutatorContext
1335 moduleContextImpl
1336}
1337
Colin Crossca860ac2016-01-04 14:34:37 -08001338type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001339 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -08001340 moduleContextImpl
1341}
1342
1343type moduleContextImpl struct {
1344 mod *Module
1345 ctx BaseModuleContext
1346}
1347
Colin Crossb98c8b02016-07-29 13:44:28 -07001348func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -08001349 return ctx.mod.toolchain(ctx.ctx)
1350}
1351
1352func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001353 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -08001354}
1355
1356func (ctx *moduleContextImpl) staticBinary() bool {
Jiyong Park379de2f2018-12-19 02:47:14 +09001357 return ctx.mod.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -08001358}
1359
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001360func (ctx *moduleContextImpl) testBinary() bool {
1361 return ctx.mod.testBinary()
1362}
1363
Jiyong Park1d1119f2019-07-29 21:27:18 +09001364func (ctx *moduleContextImpl) header() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001365 return ctx.mod.Header()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001366}
1367
Inseob Kim7f283f42020-06-01 21:53:49 +09001368func (ctx *moduleContextImpl) binary() bool {
Ivan Lozanod7586b62021-04-01 09:49:36 -04001369 return ctx.mod.Binary()
Inseob Kim7f283f42020-06-01 21:53:49 +09001370}
1371
Inseob Kim1042d292020-06-01 23:23:05 +09001372func (ctx *moduleContextImpl) object() bool {
Ivan Lozanod7586b62021-04-01 09:49:36 -04001373 return ctx.mod.Object()
Inseob Kim1042d292020-06-01 23:23:05 +09001374}
1375
Jooyung Hanccce2f22020-03-07 03:45:53 +09001376func (ctx *moduleContextImpl) canUseSdk() bool {
Colin Crossc511bc52020-04-07 16:50:32 +00001377 return ctx.mod.canUseSdk()
Jooyung Hanccce2f22020-03-07 03:45:53 +09001378}
1379
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001380func (ctx *moduleContextImpl) useSdk() bool {
Colin Crossc511bc52020-04-07 16:50:32 +00001381 return ctx.mod.UseSdk()
Colin Crossca860ac2016-01-04 14:34:37 -08001382}
1383
1384func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -07001385 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001386 if ctx.useVndk() {
Justin Yun5f7f7e82019-11-18 19:52:14 +09001387 vndkVer := ctx.mod.VndkVersion()
Jooyung Han03302ee2020-04-08 09:22:26 +09001388 if inList(vndkVer, ctx.ctx.Config().PlatformVersionActiveCodenames()) {
Justin Yun5f7f7e82019-11-18 19:52:14 +09001389 return "current"
Justin Yun732aa6a2018-03-23 17:43:47 +09001390 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09001391 return vndkVer
Dan Willemsend2ede872016-11-18 14:54:24 -08001392 }
Justin Yun732aa6a2018-03-23 17:43:47 +09001393 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -07001394 }
1395 return ""
Colin Crossca860ac2016-01-04 14:34:37 -08001396}
1397
Jiyong Parkb35a8192020-08-10 15:59:36 +09001398func (ctx *moduleContextImpl) minSdkVersion() string {
1399 ver := ctx.mod.MinSdkVersion()
1400 if ver == "apex_inherit" && !ctx.isForPlatform() {
1401 ver = ctx.apexSdkVersion().String()
1402 }
1403 if ver == "apex_inherit" || ver == "" {
1404 ver = ctx.sdkVersion()
1405 }
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09001406 // For crt objects, the meaning of min_sdk_version is very different from other types of
1407 // module. For them, min_sdk_version defines the oldest version that the build system will
1408 // create versioned variants for. For example, if min_sdk_version is 16, then sdk variant of
1409 // the crt object has local variants of 16, 17, ..., up to the latest version. sdk_version
1410 // and min_sdk_version properties of the variants are set to the corresponding version
1411 // numbers. However, the platform (non-sdk) variant of the crt object is left untouched.
1412 // min_sdk_version: 16 doesn't actually mean that the platform variant has to support such
1413 // an old version. Since the variant is for the platform, it's preferred to target the
1414 // latest version.
1415 if ctx.mod.SplitPerApiLevel() && !ctx.isSdkVariant() {
1416 ver = strconv.Itoa(android.FutureApiLevelInt)
1417 }
1418
Jiyong Parkb35a8192020-08-10 15:59:36 +09001419 // Also make sure that minSdkVersion is not greater than sdkVersion, if they are both numbers
1420 sdkVersionInt, err := strconv.Atoi(ctx.sdkVersion())
1421 minSdkVersionInt, err2 := strconv.Atoi(ver)
1422 if err == nil && err2 == nil {
1423 if sdkVersionInt < minSdkVersionInt {
1424 return strconv.Itoa(sdkVersionInt)
1425 }
1426 }
1427 return ver
1428}
1429
1430func (ctx *moduleContextImpl) isSdkVariant() bool {
1431 return ctx.mod.IsSdkVariant()
1432}
1433
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001434func (ctx *moduleContextImpl) useVndk() bool {
Ivan Lozano52767be2019-10-18 14:49:46 -07001435 return ctx.mod.UseVndk()
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001436}
Justin Yun8effde42017-06-23 19:24:43 +09001437
Colin Cross95f1ca02020-10-29 20:47:22 -07001438func (ctx *moduleContextImpl) isNdk(config android.Config) bool {
1439 return ctx.mod.IsNdk(config)
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001440}
1441
Colin Cross127bb8b2020-12-16 16:46:01 -08001442func (ctx *moduleContextImpl) IsLlndk() bool {
1443 return ctx.mod.IsLlndk()
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001444}
1445
Colin Cross127bb8b2020-12-16 16:46:01 -08001446func (ctx *moduleContextImpl) IsLlndkPublic() bool {
1447 return ctx.mod.IsLlndkPublic()
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001448}
1449
Colin Cross127bb8b2020-12-16 16:46:01 -08001450func (ctx *moduleContextImpl) isImplementationForLLNDKPublic() bool {
1451 return ctx.mod.isImplementationForLLNDKPublic()
1452}
1453
1454func (ctx *moduleContextImpl) IsVndkPrivate() bool {
1455 return ctx.mod.IsVndkPrivate()
Logan Chienf6dbd9c2019-01-16 20:19:51 +08001456}
1457
Logan Chienf3511742017-10-31 18:04:35 +08001458func (ctx *moduleContextImpl) isVndk() bool {
Ivan Lozano52767be2019-10-18 14:49:46 -07001459 return ctx.mod.IsVndk()
Logan Chienf3511742017-10-31 18:04:35 +08001460}
1461
Yi Kong7e53c572018-02-14 18:16:12 +08001462func (ctx *moduleContextImpl) isPgoCompile() bool {
1463 return ctx.mod.isPgoCompile()
1464}
1465
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -08001466func (ctx *moduleContextImpl) isNDKStubLibrary() bool {
1467 return ctx.mod.isNDKStubLibrary()
1468}
1469
Justin Yun8effde42017-06-23 19:24:43 +09001470func (ctx *moduleContextImpl) isVndkSp() bool {
Ivan Lozanod7586b62021-04-01 09:49:36 -04001471 return ctx.mod.IsVndkSp()
Logan Chienf3511742017-10-31 18:04:35 +08001472}
1473
Ivan Lozanof9e21722020-12-02 09:00:51 -05001474func (ctx *moduleContextImpl) IsVndkExt() bool {
1475 return ctx.mod.IsVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +09001476}
1477
Colin Cross5271fea2021-04-27 13:06:04 -07001478func (ctx *moduleContextImpl) IsVendorPublicLibrary() bool {
1479 return ctx.mod.IsVendorPublicLibrary()
1480}
1481
Vic Yangefd249e2018-11-12 20:19:56 -08001482func (ctx *moduleContextImpl) mustUseVendorVariant() bool {
Ivan Lozano52767be2019-10-18 14:49:46 -07001483 return ctx.mod.MustUseVendorVariant()
Vic Yangefd249e2018-11-12 20:19:56 -08001484}
1485
Dan Willemsen8146b2f2016-03-30 21:00:30 -07001486func (ctx *moduleContextImpl) selectedStl() string {
1487 if stl := ctx.mod.stl; stl != nil {
1488 return stl.Properties.SelectedStl
1489 }
1490 return ""
1491}
1492
Ivan Lozanobd721262018-11-27 14:33:03 -08001493func (ctx *moduleContextImpl) useClangLld(actx ModuleContext) bool {
1494 return ctx.mod.linker.useClangLld(actx)
1495}
1496
Colin Crossce75d2c2016-10-06 16:12:58 -07001497func (ctx *moduleContextImpl) baseModuleName() string {
1498 return ctx.mod.ModuleBase.BaseModuleName()
1499}
1500
Logan Chienf3511742017-10-31 18:04:35 +08001501func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
1502 return ctx.mod.getVndkExtendsModuleName()
1503}
1504
Logan Chiene274fc92019-12-03 11:18:32 -08001505func (ctx *moduleContextImpl) isForPlatform() bool {
Colin Cross56a83212020-09-15 18:30:11 -07001506 return ctx.ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
Logan Chiene274fc92019-12-03 11:18:32 -08001507}
1508
Colin Crosse07f2312020-08-13 11:24:56 -07001509func (ctx *moduleContextImpl) apexVariationName() string {
Colin Cross56a83212020-09-15 18:30:11 -07001510 return ctx.ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
Jiyong Park25fc6a92018-11-18 18:02:45 +09001511}
1512
Dan Albertc8060532020-07-22 22:32:17 -07001513func (ctx *moduleContextImpl) apexSdkVersion() android.ApiLevel {
Jooyung Han75568392020-03-20 04:29:24 +09001514 return ctx.mod.apexSdkVersion
Jooyung Hanccce2f22020-03-07 03:45:53 +09001515}
1516
Jiyong Parka4b9dd02019-01-16 22:53:13 +09001517func (ctx *moduleContextImpl) bootstrap() bool {
Jiyong Park7d55b612021-06-11 17:22:09 +09001518 return ctx.mod.Bootstrap()
Jiyong Parka4b9dd02019-01-16 22:53:13 +09001519}
1520
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -07001521func (ctx *moduleContextImpl) nativeCoverage() bool {
1522 return ctx.mod.nativeCoverage()
1523}
1524
Colin Cross56a83212020-09-15 18:30:11 -07001525func (ctx *moduleContextImpl) directlyInAnyApex() bool {
1526 return ctx.mod.DirectlyInAnyApex()
1527}
1528
Colin Cross95b07f22020-12-16 11:06:50 -08001529func (ctx *moduleContextImpl) isPreventInstall() bool {
1530 return ctx.mod.Properties.PreventInstall
1531}
1532
Cindy Zhou5d5cfc12021-01-09 08:25:22 -08001533func (ctx *moduleContextImpl) isCfiAssemblySupportEnabled() bool {
1534 return ctx.mod.isCfiAssemblySupportEnabled()
1535}
1536
Colin Cross635c3b02016-05-18 15:37:25 -07001537func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001538 return &Module{
1539 hod: hod,
1540 multilib: multilib,
1541 }
1542}
1543
Colin Cross635c3b02016-05-18 15:37:25 -07001544func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001545 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001546 module.features = []feature{
1547 &tidyFeature{},
1548 }
Colin Crossa8e07cc2016-04-04 15:07:06 -07001549 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -08001550 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -08001551 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001552 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +09001553 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -07001554 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001555 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -08001556 return module
1557}
1558
Colin Crossce75d2c2016-10-06 16:12:58 -07001559func (c *Module) Prebuilt() *android.Prebuilt {
1560 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
1561 return p.prebuilt()
1562 }
1563 return nil
1564}
1565
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001566func (c *Module) IsPrebuilt() bool {
1567 return c.Prebuilt() != nil
1568}
1569
Colin Crossce75d2c2016-10-06 16:12:58 -07001570func (c *Module) Name() string {
1571 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -07001572 if p, ok := c.linker.(interface {
1573 Name(string) string
1574 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -07001575 name = p.Name(name)
1576 }
1577 return name
1578}
1579
Alex Light3d673592019-01-18 14:37:31 -08001580func (c *Module) Symlinks() []string {
1581 if p, ok := c.installer.(interface {
1582 symlinkList() []string
1583 }); ok {
1584 return p.symlinkList()
1585 }
1586 return nil
1587}
1588
Roland Levillainf89cd092019-07-29 16:22:59 +01001589func (c *Module) IsTestPerSrcAllTestsVariation() bool {
1590 test, ok := c.linker.(testPerSrc)
1591 return ok && test.isAllTestsVariation()
1592}
1593
Chris Parsons216e10a2020-07-09 17:12:52 -04001594func (c *Module) DataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -07001595 if p, ok := c.installer.(interface {
Chris Parsons216e10a2020-07-09 17:12:52 -04001596 dataPaths() []android.DataPath
Liz Kammer1c14a212020-05-12 15:26:55 -07001597 }); ok {
1598 return p.dataPaths()
1599 }
1600 return nil
1601}
1602
Justin Yun5f7f7e82019-11-18 19:52:14 +09001603func (c *Module) getNameSuffixWithVndkVersion(ctx android.ModuleContext) string {
1604 // Returns the name suffix for product and vendor variants. If the VNDK version is not
1605 // "current", it will append the VNDK version to the name suffix.
1606 var vndkVersion string
1607 var nameSuffix string
Ivan Lozanof9e21722020-12-02 09:00:51 -05001608 if c.InProduct() {
Justin Yund00f5ca2021-02-03 19:43:02 +09001609 if c.ProductSpecific() {
1610 // If the module is product specific with 'product_specific: true',
1611 // do not add a name suffix because it is a base module.
1612 return ""
1613 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09001614 vndkVersion = ctx.DeviceConfig().ProductVndkVersion()
1615 nameSuffix = productSuffix
1616 } else {
1617 vndkVersion = ctx.DeviceConfig().VndkVersion()
Ivan Lozanoe6d30982021-02-05 10:57:43 -05001618 nameSuffix = VendorSuffix
Justin Yun5f7f7e82019-11-18 19:52:14 +09001619 }
1620 if vndkVersion == "current" {
1621 vndkVersion = ctx.DeviceConfig().PlatformVndkVersion()
1622 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001623 if c.Properties.VndkVersion != vndkVersion && c.Properties.VndkVersion != "" {
Justin Yun5f7f7e82019-11-18 19:52:14 +09001624 // add version suffix only if the module is using different vndk version than the
1625 // version in product or vendor partition.
1626 nameSuffix += "." + c.Properties.VndkVersion
1627 }
1628 return nameSuffix
1629}
1630
Chris Parsons8d6e4332021-02-22 16:13:50 -05001631func (c *Module) setSubnameProperty(actx android.ModuleContext) {
Inseob Kim64c43952019-08-26 16:52:35 +09001632 c.Properties.SubName = ""
1633
1634 if c.Target().NativeBridge == android.NativeBridgeEnabled {
1635 c.Properties.SubName += nativeBridgeSuffix
1636 }
1637
Colin Cross127bb8b2020-12-16 16:46:01 -08001638 llndk := c.IsLlndk()
Colin Cross627280f2021-04-26 16:53:58 -07001639 if llndk || (c.UseVndk() && c.HasNonSystemVariants()) {
Justin Yun5f7f7e82019-11-18 19:52:14 +09001640 // .vendor.{version} suffix is added for vendor variant or .product.{version} suffix is
1641 // added for product variant only when we have vendor and product variants with core
1642 // variant. The suffix is not added for vendor-only or product-only module.
1643 c.Properties.SubName += c.getNameSuffixWithVndkVersion(actx)
Colin Cross5271fea2021-04-27 13:06:04 -07001644 } else if c.IsVendorPublicLibrary() {
1645 c.Properties.SubName += vendorPublicLibrarySuffix
Justin Yun5f7f7e82019-11-18 19:52:14 +09001646 } else if _, ok := c.linker.(*vndkPrebuiltLibraryDecorator); ok {
Inseob Kim64c43952019-08-26 16:52:35 +09001647 // .vendor suffix is added for backward compatibility with VNDK snapshot whose names with
1648 // such suffixes are already hard-coded in prebuilts/vndk/.../Android.bp.
Ivan Lozanoe6d30982021-02-05 10:57:43 -05001649 c.Properties.SubName += VendorSuffix
Yifan Hong1b3348d2020-01-21 15:53:22 -08001650 } else if c.InRamdisk() && !c.OnlyInRamdisk() {
1651 c.Properties.SubName += ramdiskSuffix
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001652 } else if c.InVendorRamdisk() && !c.OnlyInVendorRamdisk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -05001653 c.Properties.SubName += VendorRamdiskSuffix
Ivan Lozano52767be2019-10-18 14:49:46 -07001654 } else if c.InRecovery() && !c.OnlyInRecovery() {
Inseob Kim64c43952019-08-26 16:52:35 +09001655 c.Properties.SubName += recoverySuffix
Dan Albert92fe7402020-07-15 13:33:30 -07001656 } else if c.IsSdkVariant() && (c.Properties.SdkAndPlatformVariantVisibleToMake || c.SplitPerApiLevel()) {
Colin Crossc511bc52020-04-07 16:50:32 +00001657 c.Properties.SubName += sdkSuffix
Dan Albert92fe7402020-07-15 13:33:30 -07001658 if c.SplitPerApiLevel() {
1659 c.Properties.SubName += "." + c.SdkVersion()
1660 }
Inseob Kim64c43952019-08-26 16:52:35 +09001661 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001662}
1663
1664// Returns true if Bazel was successfully used for the analysis of this module.
1665func (c *Module) maybeGenerateBazelActions(actx android.ModuleContext) bool {
Liz Kammerbdc60992021-02-24 16:55:11 -05001666 bazelModuleLabel := c.GetBazelLabel(actx, c)
Chris Parsons8d6e4332021-02-22 16:13:50 -05001667 bazelActionsUsed := false
Chris Parsonsbab4d7e2021-04-15 17:27:08 -04001668 if c.MixedBuildsEnabled(actx) && c.bazelHandler != nil {
Chris Parsons8d6e4332021-02-22 16:13:50 -05001669 bazelActionsUsed = c.bazelHandler.generateBazelBuildActions(actx, bazelModuleLabel)
1670 }
1671 return bazelActionsUsed
1672}
1673
1674func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1675 // TODO(cparsons): Any logic in this method occurring prior to querying Bazel should be
1676 // requested from Bazel instead.
1677
1678 // Handle the case of a test module split by `test_per_src` mutator.
1679 //
1680 // The `test_per_src` mutator adds an extra variation named "", depending on all the other
1681 // `test_per_src` variations of the test module. Set `outputFile` to an empty path for this
1682 // module and return early, as this module does not produce an output file per se.
1683 if c.IsTestPerSrcAllTestsVariation() {
1684 c.outputFile = android.OptionalPath{}
1685 return
1686 }
1687
1688 c.setSubnameProperty(actx)
1689 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1690 if !apexInfo.IsForPlatform() {
1691 c.hideApexVariantFromMake = true
1692 }
1693
Chris Parsonseefc9e62021-04-02 17:36:47 -04001694 c.makeLinkType = GetMakeLinkType(actx, c)
1695
Colin Crossca860ac2016-01-04 14:34:37 -08001696 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -07001697 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -08001698 moduleContextImpl: moduleContextImpl{
1699 mod: c,
1700 },
1701 }
1702 ctx.ctx = ctx
1703
Chris Parsons94a0bba2021-06-04 15:03:47 -04001704 if c.maybeGenerateBazelActions(actx) {
1705 c.maybeInstall(ctx, apexInfo)
1706 return
1707 }
1708
Colin Crossf18e1102017-11-16 14:33:08 -08001709 deps := c.depsToPaths(ctx)
1710 if ctx.Failed() {
1711 return
1712 }
1713
Dan Willemsen8536d6b2018-10-07 20:54:34 -07001714 if c.Properties.Clang != nil && *c.Properties.Clang == false {
1715 ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
1716 }
1717
Colin Crossca860ac2016-01-04 14:34:37 -08001718 flags := Flags{
1719 Toolchain: c.toolchain(ctx),
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001720 EmitXrefs: ctx.Config().EmitXrefRules(),
Colin Crossca860ac2016-01-04 14:34:37 -08001721 }
Colin Crossca860ac2016-01-04 14:34:37 -08001722 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -08001723 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08001724 }
1725 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -07001726 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -08001727 }
Colin Crossa8e07cc2016-04-04 15:07:06 -07001728 if c.stl != nil {
1729 flags = c.stl.flags(ctx, flags)
1730 }
Colin Cross16b23492016-01-06 14:41:07 -08001731 if c.sanitize != nil {
1732 flags = c.sanitize.flags(ctx, flags)
1733 }
Dan Willemsen581341d2017-02-09 16:16:31 -08001734 if c.coverage != nil {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -07001735 flags, deps = c.coverage.flags(ctx, flags, deps)
Dan Willemsen581341d2017-02-09 16:16:31 -08001736 }
Stephen Craneba090d12017-05-09 15:44:35 -07001737 if c.lto != nil {
1738 flags = c.lto.flags(ctx, flags)
1739 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001740 if c.pgo != nil {
1741 flags = c.pgo.flags(ctx, flags)
1742 }
Colin Crossca860ac2016-01-04 14:34:37 -08001743 for _, feature := range c.features {
1744 flags = feature.flags(ctx, flags)
1745 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001746 if ctx.Failed() {
1747 return
1748 }
1749
Colin Cross4af21ed2019-11-04 09:37:55 -08001750 flags.Local.CFlags, _ = filterList(flags.Local.CFlags, config.IllegalFlags)
1751 flags.Local.CppFlags, _ = filterList(flags.Local.CppFlags, config.IllegalFlags)
1752 flags.Local.ConlyFlags, _ = filterList(flags.Local.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001753
Colin Cross4af21ed2019-11-04 09:37:55 -08001754 flags.Local.CommonFlags = append(flags.Local.CommonFlags, deps.Flags...)
Inseob Kim69378442019-06-03 19:10:47 +09001755
1756 for _, dir := range deps.IncludeDirs {
Colin Cross4af21ed2019-11-04 09:37:55 -08001757 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+dir.String())
Inseob Kim69378442019-06-03 19:10:47 +09001758 }
1759 for _, dir := range deps.SystemIncludeDirs {
Colin Cross4af21ed2019-11-04 09:37:55 -08001760 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-isystem "+dir.String())
Inseob Kim69378442019-06-03 19:10:47 +09001761 }
1762
Fabien Sanglardd61f1f42017-01-10 16:21:22 -08001763 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -07001764 // We need access to all the flags seen by a source file.
1765 if c.sabi != nil {
1766 flags = c.sabi.flags(ctx, flags)
1767 }
Dan Willemsen98ab3112019-08-27 21:20:40 -07001768
Colin Cross4af21ed2019-11-04 09:37:55 -08001769 flags.AssemblerWithCpp = inList("-xassembler-with-cpp", flags.Local.AsFlags)
Dan Willemsen98ab3112019-08-27 21:20:40 -07001770
Colin Crossca860ac2016-01-04 14:34:37 -08001771 // Optimization to reduce size of build.ninja
1772 // Replace the long list of flags for each file with a module-local variable
Colin Cross4af21ed2019-11-04 09:37:55 -08001773 ctx.Variable(pctx, "cflags", strings.Join(flags.Local.CFlags, " "))
1774 ctx.Variable(pctx, "cppflags", strings.Join(flags.Local.CppFlags, " "))
1775 ctx.Variable(pctx, "asflags", strings.Join(flags.Local.AsFlags, " "))
1776 flags.Local.CFlags = []string{"$cflags"}
1777 flags.Local.CppFlags = []string{"$cppflags"}
1778 flags.Local.AsFlags = []string{"$asflags"}
Colin Crossca860ac2016-01-04 14:34:37 -08001779
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001780 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -08001781 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001782 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08001783 if ctx.Failed() {
1784 return
1785 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001786 c.kytheFiles = objs.kytheFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001787 }
1788
Colin Crossca860ac2016-01-04 14:34:37 -08001789 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001790 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -08001791 if ctx.Failed() {
1792 return
1793 }
Colin Cross635c3b02016-05-18 15:37:25 -07001794 c.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Parkb0788572018-12-20 22:10:17 +09001795
Chris Parsons94a0bba2021-06-04 15:03:47 -04001796 c.maybeUnhideFromMake()
Inseob Kimeda2e9c2020-03-03 22:06:32 +09001797
Jose Galmes6f843bc2020-12-11 13:36:29 -08001798 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1799 // RECOVERY_SNAPSHOT_VERSION is current.
1800 if i, ok := c.linker.(snapshotLibraryInterface); ok {
Ivan Lozanod7586b62021-04-01 09:49:36 -04001801 if ShouldCollectHeadersForSnapshot(ctx, c, apexInfo) {
Inseob Kimeda2e9c2020-03-03 22:06:32 +09001802 i.collectHeadersForSnapshot(ctx)
1803 }
1804 }
Colin Crossce75d2c2016-10-06 16:12:58 -07001805 }
Colin Cross5049f022015-03-18 13:28:46 -07001806
Chris Parsons94a0bba2021-06-04 15:03:47 -04001807 c.maybeInstall(ctx, apexInfo)
1808}
1809
1810func (c *Module) maybeUnhideFromMake() {
1811 // If a lib is directly included in any of the APEXes or is not available to the
1812 // platform (which is often the case when the stub is provided as a prebuilt),
1813 // unhide the stubs variant having the latest version gets visible to make. In
1814 // addition, the non-stubs variant is renamed to <libname>.bootstrap. This is to
1815 // force anything in the make world to link against the stubs library. (unless it
1816 // is explicitly referenced via .bootstrap suffix or the module is marked with
1817 // 'bootstrap: true').
1818 if c.HasStubsVariants() && c.NotInPlatform() && !c.InRamdisk() &&
1819 !c.InRecovery() && !c.UseVndk() && !c.static() && !c.isCoverageVariant() &&
1820 c.IsStubs() && !c.InVendorRamdisk() {
1821 c.Properties.HideFromMake = false // unhide
1822 // Note: this is still non-installable
1823 }
1824}
1825
1826func (c *Module) maybeInstall(ctx ModuleContext, apexInfo android.ApexInfo) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001827 if !proptools.BoolDefault(c.Properties.Installable, true) {
1828 // If the module has been specifically configure to not be installed then
1829 // hide from make as otherwise it will break when running inside make
1830 // as the output path to install will not be specified. Not all uninstallable
1831 // modules can be hidden from make as some are needed for resolving make side
1832 // dependencies.
1833 c.HideFromMake()
Ivan Lozanod7586b62021-04-01 09:49:36 -04001834 } else if !installable(c, apexInfo) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001835 c.SkipInstall()
1836 }
1837
1838 // Still call c.installer.install though, the installs will be stored as PackageSpecs
1839 // to allow using the outputs in a genrule.
1840 if c.installer != nil && c.outputFile.Valid() {
Colin Crossce75d2c2016-10-06 16:12:58 -07001841 c.installer.install(ctx, c.outputFile.Path())
1842 if ctx.Failed() {
1843 return
Colin Crossca860ac2016-01-04 14:34:37 -08001844 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001845 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001846}
1847
Colin Cross0ea8ba82019-06-06 14:33:29 -07001848func (c *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -08001849 if c.cachedToolchain == nil {
Liz Kammer356f7d42021-01-26 09:18:53 -05001850 c.cachedToolchain = config.FindToolchainWithContext(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -08001851 }
Colin Crossca860ac2016-01-04 14:34:37 -08001852 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -08001853}
1854
Colin Crossca860ac2016-01-04 14:34:37 -08001855func (c *Module) begin(ctx BaseModuleContext) {
1856 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -07001857 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -07001858 }
Colin Crossca860ac2016-01-04 14:34:37 -08001859 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -07001860 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -08001861 }
Colin Crossa8e07cc2016-04-04 15:07:06 -07001862 if c.stl != nil {
1863 c.stl.begin(ctx)
1864 }
Colin Cross16b23492016-01-06 14:41:07 -08001865 if c.sanitize != nil {
1866 c.sanitize.begin(ctx)
1867 }
Dan Willemsen581341d2017-02-09 16:16:31 -08001868 if c.coverage != nil {
1869 c.coverage.begin(ctx)
1870 }
Stephen Craneba090d12017-05-09 15:44:35 -07001871 if c.lto != nil {
1872 c.lto.begin(ctx)
1873 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001874 if c.pgo != nil {
1875 c.pgo.begin(ctx)
1876 }
Dan Albert92fe7402020-07-15 13:33:30 -07001877 if ctx.useSdk() && c.IsSdkVariant() {
Dan Albert1a246272020-07-06 14:49:35 -07001878 version, err := nativeApiLevelFromUser(ctx, ctx.sdkVersion())
Dan Albert7fa7b2e2016-08-05 16:37:52 -07001879 if err != nil {
1880 ctx.PropertyErrorf("sdk_version", err.Error())
Dan Albert1a246272020-07-06 14:49:35 -07001881 c.Properties.Sdk_version = nil
1882 } else {
1883 c.Properties.Sdk_version = StringPtr(version.String())
Dan Albert7fa7b2e2016-08-05 16:37:52 -07001884 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -07001885 }
Colin Crossca860ac2016-01-04 14:34:37 -08001886}
1887
Colin Cross37047f12016-12-13 17:06:13 -08001888func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -07001889 deps := Deps{}
1890
1891 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -07001892 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001893 }
1894 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -07001895 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001896 }
Colin Crossa8e07cc2016-04-04 15:07:06 -07001897 if c.stl != nil {
1898 deps = c.stl.deps(ctx, deps)
1899 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +00001900 if c.coverage != nil {
1901 deps = c.coverage.deps(ctx, deps)
1902 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001903
Colin Crossb6715442017-10-24 11:13:31 -07001904 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
1905 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
1906 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
1907 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1908 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
1909 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +08001910 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -07001911
Liz Kammer356f7d42021-01-26 09:18:53 -05001912 // In Bazel conversion mode, we dependency and build validations will occur in Bazel, so there is
1913 // no need to do so in Soong.
1914 if ctx.BazelConversionMode() {
1915 return deps
1916 }
1917
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001918 for _, lib := range deps.ReexportSharedLibHeaders {
1919 if !inList(lib, deps.SharedLibs) {
1920 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
1921 }
1922 }
1923
1924 for _, lib := range deps.ReexportStaticLibHeaders {
Steven Morelandba407c82021-04-01 22:17:50 +00001925 if !inList(lib, deps.StaticLibs) && !inList(lib, deps.WholeStaticLibs) {
1926 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs or whole_static_libs: '%s'", lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001927 }
1928 }
1929
Colin Cross5950f382016-12-13 12:50:57 -08001930 for _, lib := range deps.ReexportHeaderLibHeaders {
1931 if !inList(lib, deps.HeaderLibs) {
1932 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
1933 }
1934 }
1935
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001936 for _, gen := range deps.ReexportGeneratedHeaders {
1937 if !inList(gen, deps.GeneratedHeaders) {
1938 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
1939 }
1940 }
1941
Colin Crossc99deeb2016-04-11 15:06:20 -07001942 return deps
1943}
1944
Dan Albert7e9d2952016-08-04 13:02:36 -07001945func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001946 ctx := &baseModuleContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -07001947 BaseModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -08001948 moduleContextImpl: moduleContextImpl{
1949 mod: c,
1950 },
1951 }
1952 ctx.ctx = ctx
1953
Colin Crossca860ac2016-01-04 14:34:37 -08001954 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -07001955}
1956
Jiyong Park7ed9de32018-10-15 22:25:07 +09001957// Split name#version into name and version
Jiyong Park73c54ee2019-10-22 20:31:18 +09001958func StubsLibNameAndVersion(name string) (string, string) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001959 if sharp := strings.LastIndex(name, "#"); sharp != -1 && sharp != len(name)-1 {
1960 version := name[sharp+1:]
1961 libname := name[:sharp]
1962 return libname, version
1963 }
1964 return name, ""
1965}
1966
Dan Albert92fe7402020-07-15 13:33:30 -07001967func GetCrtVariations(ctx android.BottomUpMutatorContext,
1968 m LinkableInterface) []blueprint.Variation {
1969 if ctx.Os() != android.Android {
1970 return nil
1971 }
1972 if m.UseSdk() {
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09001973 // Choose the CRT that best satisfies the min_sdk_version requirement of this module
1974 minSdkVersion := m.MinSdkVersion()
1975 if minSdkVersion == "" || minSdkVersion == "apex_inherit" {
1976 minSdkVersion = m.SdkVersion()
1977 }
Jooyung Han94a76ee2021-06-08 09:49:48 +09001978 apiLevel, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1979 if err != nil {
1980 ctx.PropertyErrorf("min_sdk_version", err.Error())
1981 }
Dan Albert92fe7402020-07-15 13:33:30 -07001982 return []blueprint.Variation{
1983 {Mutator: "sdk", Variation: "sdk"},
Jooyung Han94a76ee2021-06-08 09:49:48 +09001984 {Mutator: "version", Variation: apiLevel.String()},
Dan Albert92fe7402020-07-15 13:33:30 -07001985 }
1986 }
1987 return []blueprint.Variation{
1988 {Mutator: "sdk", Variation: ""},
1989 }
1990}
1991
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001992func AddSharedLibDependenciesWithVersions(ctx android.BottomUpMutatorContext, mod LinkableInterface,
1993 variations []blueprint.Variation, depTag blueprint.DependencyTag, name, version string, far bool) {
Colin Crosse7257d22020-09-24 09:56:18 -07001994
1995 variations = append([]blueprint.Variation(nil), variations...)
1996
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001997 if version != "" && CanBeOrLinkAgainstVersionVariants(mod) {
Colin Crosse7257d22020-09-24 09:56:18 -07001998 // Version is explicitly specified. i.e. libFoo#30
1999 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002000 if tag, ok := depTag.(libraryDependencyTag); ok {
2001 tag.explicitlyVersioned = true
2002 } else {
2003 panic(fmt.Errorf("Unexpected dependency tag: %T", depTag))
2004 }
Colin Crosse7257d22020-09-24 09:56:18 -07002005 }
Colin Crosse7257d22020-09-24 09:56:18 -07002006
Colin Cross0de8a1e2020-09-18 14:15:30 -07002007 if far {
2008 ctx.AddFarVariationDependencies(variations, depTag, name)
2009 } else {
2010 ctx.AddVariationDependencies(variations, depTag, name)
Colin Crosse7257d22020-09-24 09:56:18 -07002011 }
2012}
2013
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002014func GetSnapshot(c LinkableInterface, snapshotInfo **SnapshotInfo, actx android.BottomUpMutatorContext) SnapshotInfo {
Yi-Yo Chiangec14a4d2021-07-02 21:26:00 +08002015 // Only device modules with BOARD_VNDK_VERSION uses snapshot. Others use the zero value of
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002016 // SnapshotInfo, which provides no mappings.
Yi-Yo Chiangec14a4d2021-07-02 21:26:00 +08002017 if *snapshotInfo == nil && c.Device() {
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002018 // Only retrieve the snapshot on demand in order to avoid circular dependencies
2019 // between the modules in the snapshot and the snapshot itself.
2020 var snapshotModule []blueprint.Module
2021 if c.InVendor() && c.VndkVersion() == actx.DeviceConfig().VndkVersion() {
2022 snapshotModule = actx.AddVariationDependencies(nil, nil, "vendor_snapshot")
2023 } else if recoverySnapshotVersion := actx.DeviceConfig().RecoverySnapshotVersion(); recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" && c.InRecovery() {
2024 snapshotModule = actx.AddVariationDependencies(nil, nil, "recovery_snapshot")
2025 }
Yi-Yo Chiangec14a4d2021-07-02 21:26:00 +08002026 if len(snapshotModule) > 0 && snapshotModule[0] != nil {
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002027 snapshot := actx.OtherModuleProvider(snapshotModule[0], SnapshotInfoProvider).(SnapshotInfo)
2028 *snapshotInfo = &snapshot
2029 // republish the snapshot for use in later mutators on this module
2030 actx.SetProvider(SnapshotInfoProvider, snapshot)
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002031 }
2032 }
Yi-Yo Chiangec14a4d2021-07-02 21:26:00 +08002033 if *snapshotInfo == nil {
2034 *snapshotInfo = &SnapshotInfo{}
2035 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002036 return **snapshotInfo
2037}
2038
2039func RewriteSnapshotLib(lib string, snapshotMap map[string]string) string {
2040 if snapshot, ok := snapshotMap[lib]; ok {
2041 return snapshot
2042 }
2043
2044 return lib
2045}
2046
2047// RewriteLibs takes a list of names of shared libraries and scans it for three types
2048// of names:
2049//
2050// 1. Name of an NDK library that refers to a prebuilt module.
2051// For each of these, it adds the name of the prebuilt module (which will be in
2052// prebuilts/ndk) to the list of nonvariant libs.
2053// 2. Name of an NDK library that refers to an ndk_library module.
2054// For each of these, it adds the name of the ndk_library module to the list of
2055// variant libs.
2056// 3. Anything else (so anything that isn't an NDK library).
2057// It adds these to the nonvariantLibs list.
2058//
2059// The caller can then know to add the variantLibs dependencies differently from the
2060// nonvariantLibs
2061func RewriteLibs(c LinkableInterface, snapshotInfo **SnapshotInfo, actx android.BottomUpMutatorContext, config android.Config, list []string) (nonvariantLibs []string, variantLibs []string) {
2062 variantLibs = []string{}
2063
2064 nonvariantLibs = []string{}
2065 for _, entry := range list {
2066 // strip #version suffix out
2067 name, _ := StubsLibNameAndVersion(entry)
2068 if c.InRecovery() {
2069 nonvariantLibs = append(nonvariantLibs, RewriteSnapshotLib(entry, GetSnapshot(c, snapshotInfo, actx).SharedLibs))
2070 } else if c.UseSdk() && inList(name, *getNDKKnownLibs(config)) {
2071 variantLibs = append(variantLibs, name+ndkLibrarySuffix)
2072 } else if c.UseVndk() {
2073 nonvariantLibs = append(nonvariantLibs, RewriteSnapshotLib(entry, GetSnapshot(c, snapshotInfo, actx).SharedLibs))
2074 } else {
2075 // put name#version back
2076 nonvariantLibs = append(nonvariantLibs, entry)
2077 }
2078 }
2079 return nonvariantLibs, variantLibs
2080}
2081
Colin Cross1e676be2016-10-12 14:38:15 -07002082func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
Inseob Kimeec88e12020-01-22 11:11:29 +09002083 if !c.Enabled() {
2084 return
2085 }
2086
Colin Cross37047f12016-12-13 17:06:13 -08002087 ctx := &depsContext{
2088 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -07002089 moduleContextImpl: moduleContextImpl{
2090 mod: c,
2091 },
2092 }
2093 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -08002094
Colin Crossc99deeb2016-04-11 15:06:20 -07002095 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -08002096
Yo Chiang219968c2020-09-22 18:45:04 +08002097 c.Properties.AndroidMkSystemSharedLibs = deps.SystemSharedLibs
2098
Colin Crosse0edaf92021-01-11 17:31:17 -08002099 var snapshotInfo *SnapshotInfo
Colin Crosse0edaf92021-01-11 17:31:17 -08002100
Dan Albert914449f2016-06-17 16:45:24 -07002101 variantNdkLibs := []string{}
2102 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -07002103 if ctx.Os() == android.Android {
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002104 deps.SharedLibs, variantNdkLibs = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
2105 deps.LateSharedLibs, variantLateNdkLibs = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.LateSharedLibs)
2106 deps.ReexportSharedLibHeaders, _ = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.ReexportSharedLibHeaders)
Colin Crosse0edaf92021-01-11 17:31:17 -08002107
2108 for idx, lib := range deps.RuntimeLibs {
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002109 deps.RuntimeLibs[idx] = RewriteSnapshotLib(lib, GetSnapshot(c, &snapshotInfo, actx).SharedLibs)
Inseob Kimeec88e12020-01-22 11:11:29 +09002110 }
Dan Willemsen72d39932016-07-08 23:23:48 -07002111 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002112
Colin Cross32ec36c2016-12-15 07:39:51 -08002113 for _, lib := range deps.HeaderLibs {
Colin Cross6e511a92020-07-27 21:26:48 -07002114 depTag := libraryDependencyTag{Kind: headerLibraryDependency}
Colin Cross32ec36c2016-12-15 07:39:51 -08002115 if inList(lib, deps.ReexportHeaderLibHeaders) {
Colin Cross6e511a92020-07-27 21:26:48 -07002116 depTag.reexportFlags = true
Colin Cross32ec36c2016-12-15 07:39:51 -08002117 }
Inseob Kimeec88e12020-01-22 11:11:29 +09002118
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002119 lib = RewriteSnapshotLib(lib, GetSnapshot(c, &snapshotInfo, actx).HeaderLibs)
Inseob Kimeec88e12020-01-22 11:11:29 +09002120
Jiyong Park1ad8e162020-12-01 23:40:09 +09002121 if c.IsStubs() {
Colin Cross7228ecd2019-11-18 16:00:16 -08002122 actx.AddFarVariationDependencies(append(ctx.Target().Variations(), c.ImageVariation()),
Colin Cross0f7d2ef2019-10-16 11:03:10 -07002123 depTag, lib)
Jiyong Park7e636d02019-01-28 16:16:54 +09002124 } else {
2125 actx.AddVariationDependencies(nil, depTag, lib)
2126 }
2127 }
2128
Dan Albertf1d14c72020-07-30 14:32:55 -07002129 if c.isNDKStubLibrary() {
2130 // NDK stubs depend on their implementation because the ABI dumps are
2131 // generated from the implementation library.
2132 actx.AddFarVariationDependencies(append(ctx.Target().Variations(),
2133 c.ImageVariation(),
2134 blueprint.Variation{Mutator: "link", Variation: "shared"},
2135 ), stubImplementation, c.BaseModuleName())
2136 }
2137
Inseob Kim07def122020-11-23 14:43:02 +09002138 // sysprop_library has to support both C++ and Java. So sysprop_library internally creates one
2139 // C++ implementation library and one Java implementation library. When a module links against
2140 // sysprop_library, the C++ implementation library has to be linked. syspropImplLibraries is a
2141 // map from sysprop_library to implementation library; it will be used in whole_static_libs,
2142 // static_libs, and shared_libs.
Inseob Kimc0907f12019-02-08 21:00:45 +09002143 syspropImplLibraries := syspropImplLibraries(actx.Config())
2144
Jiyong Park5d1598f2019-02-25 22:14:17 +09002145 for _, lib := range deps.WholeStaticLibs {
Colin Cross6e511a92020-07-27 21:26:48 -07002146 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true, reexportFlags: true}
Jiyong Park5d1598f2019-02-25 22:14:17 +09002147 if impl, ok := syspropImplLibraries[lib]; ok {
2148 lib = impl
2149 }
Inseob Kimeec88e12020-01-22 11:11:29 +09002150
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002151 lib = RewriteSnapshotLib(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs)
Inseob Kimeec88e12020-01-22 11:11:29 +09002152
Jiyong Park5d1598f2019-02-25 22:14:17 +09002153 actx.AddVariationDependencies([]blueprint.Variation{
2154 {Mutator: "link", Variation: "static"},
2155 }, depTag, lib)
2156 }
2157
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002158 for _, lib := range deps.StaticLibs {
Colin Cross6e511a92020-07-27 21:26:48 -07002159 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002160 if inList(lib, deps.ReexportStaticLibHeaders) {
Colin Cross6e511a92020-07-27 21:26:48 -07002161 depTag.reexportFlags = true
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002162 }
Jiyong Parke3867542020-12-03 17:28:25 +09002163 if inList(lib, deps.ExcludeLibsForApex) {
2164 depTag.excludeInApex = true
2165 }
Inseob Kimc0907f12019-02-08 21:00:45 +09002166
2167 if impl, ok := syspropImplLibraries[lib]; ok {
2168 lib = impl
2169 }
2170
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002171 lib = RewriteSnapshotLib(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs)
Inseob Kimeec88e12020-01-22 11:11:29 +09002172
Dan Willemsen59339a22018-07-22 21:18:45 -07002173 actx.AddVariationDependencies([]blueprint.Variation{
2174 {Mutator: "link", Variation: "static"},
2175 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002176 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002177
Jooyung Han75568392020-03-20 04:29:24 +09002178 // staticUnwinderDep is treated as staticDep for Q apexes
2179 // so that native libraries/binaries are linked with static unwinder
2180 // because Q libc doesn't have unwinder APIs
2181 if deps.StaticUnwinderIfLegacy {
Colin Cross6e511a92020-07-27 21:26:48 -07002182 depTag := libraryDependencyTag{Kind: staticLibraryDependency, staticUnwinder: true}
Peter Collingbournedc4f9862020-02-12 17:13:25 -08002183 actx.AddVariationDependencies([]blueprint.Variation{
2184 {Mutator: "link", Variation: "static"},
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002185 }, depTag, RewriteSnapshotLib(staticUnwinder(actx), GetSnapshot(c, &snapshotInfo, actx).StaticLibs))
Peter Collingbournedc4f9862020-02-12 17:13:25 -08002186 }
2187
Jiyong Park7ed9de32018-10-15 22:25:07 +09002188 // shared lib names without the #version suffix
2189 var sharedLibNames []string
2190
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002191 for _, lib := range deps.SharedLibs {
Colin Cross6e511a92020-07-27 21:26:48 -07002192 depTag := libraryDependencyTag{Kind: sharedLibraryDependency}
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002193 if inList(lib, deps.ReexportSharedLibHeaders) {
Colin Cross6e511a92020-07-27 21:26:48 -07002194 depTag.reexportFlags = true
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002195 }
Jiyong Parke3867542020-12-03 17:28:25 +09002196 if inList(lib, deps.ExcludeLibsForApex) {
2197 depTag.excludeInApex = true
2198 }
Inseob Kimc0907f12019-02-08 21:00:45 +09002199
2200 if impl, ok := syspropImplLibraries[lib]; ok {
2201 lib = impl
2202 }
2203
Jiyong Park73c54ee2019-10-22 20:31:18 +09002204 name, version := StubsLibNameAndVersion(lib)
Inseob Kimc0907f12019-02-08 21:00:45 +09002205 sharedLibNames = append(sharedLibNames, name)
2206
Colin Crosse7257d22020-09-24 09:56:18 -07002207 variations := []blueprint.Variation{
2208 {Mutator: "link", Variation: "shared"},
2209 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002210 AddSharedLibDependenciesWithVersions(ctx, c, variations, depTag, name, version, false)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07002211 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002212
Colin Crossfe9acfe2021-06-14 16:13:03 -07002213 for _, lib := range deps.LateStaticLibs {
2214 depTag := libraryDependencyTag{Kind: staticLibraryDependency, Order: lateLibraryDependency}
2215 actx.AddVariationDependencies([]blueprint.Variation{
2216 {Mutator: "link", Variation: "static"},
2217 }, depTag, RewriteSnapshotLib(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs))
2218 }
2219
Jiyong Park7ed9de32018-10-15 22:25:07 +09002220 for _, lib := range deps.LateSharedLibs {
Jiyong Park25fc6a92018-11-18 18:02:45 +09002221 if inList(lib, sharedLibNames) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09002222 // This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
2223 // are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
2224 // linking against both the stubs lib and the non-stubs lib at the same time.
2225 continue
2226 }
Colin Cross6e511a92020-07-27 21:26:48 -07002227 depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: lateLibraryDependency}
Colin Crosse7257d22020-09-24 09:56:18 -07002228 variations := []blueprint.Variation{
2229 {Mutator: "link", Variation: "shared"},
2230 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002231 AddSharedLibDependenciesWithVersions(ctx, c, variations, depTag, lib, "", false)
Jiyong Park7ed9de32018-10-15 22:25:07 +09002232 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002233
Dan Willemsen59339a22018-07-22 21:18:45 -07002234 actx.AddVariationDependencies([]blueprint.Variation{
2235 {Mutator: "link", Variation: "shared"},
Chris Parsons79d66a52020-06-05 17:26:16 -04002236 }, dataLibDepTag, deps.DataLibs...)
2237
2238 actx.AddVariationDependencies([]blueprint.Variation{
2239 {Mutator: "link", Variation: "shared"},
Dan Willemsen59339a22018-07-22 21:18:45 -07002240 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08002241
Colin Cross68861832016-07-08 10:41:41 -07002242 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07002243
2244 for _, gen := range deps.GeneratedHeaders {
2245 depTag := genHeaderDepTag
2246 if inList(gen, deps.ReexportGeneratedHeaders) {
2247 depTag = genHeaderExportDepTag
2248 }
2249 actx.AddDependency(c, depTag, gen)
2250 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07002251
Dan Albert92fe7402020-07-15 13:33:30 -07002252 crtVariations := GetCrtVariations(ctx, c)
Colin Crossbbc941b2020-09-30 12:27:01 -07002253 actx.AddVariationDependencies(crtVariations, objDepTag, deps.ObjFiles...)
Colin Crossc465efd2021-06-11 18:00:04 -07002254 for _, crt := range deps.CrtBegin {
Dan Albert92fe7402020-07-15 13:33:30 -07002255 actx.AddVariationDependencies(crtVariations, CrtBeginDepTag,
Colin Crossc465efd2021-06-11 18:00:04 -07002256 RewriteSnapshotLib(crt, GetSnapshot(c, &snapshotInfo, actx).Objects))
Colin Crossca860ac2016-01-04 14:34:37 -08002257 }
Colin Crossc465efd2021-06-11 18:00:04 -07002258 for _, crt := range deps.CrtEnd {
Dan Albert92fe7402020-07-15 13:33:30 -07002259 actx.AddVariationDependencies(crtVariations, CrtEndDepTag,
Colin Crossc465efd2021-06-11 18:00:04 -07002260 RewriteSnapshotLib(crt, GetSnapshot(c, &snapshotInfo, actx).Objects))
Colin Cross21b9a242015-03-24 14:15:58 -07002261 }
Dan Willemsena0790e32018-10-12 00:24:23 -07002262 if deps.DynamicLinker != "" {
2263 actx.AddDependency(c, dynamicLinkerDepTag, deps.DynamicLinker)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07002264 }
Dan Albert914449f2016-06-17 16:45:24 -07002265
2266 version := ctx.sdkVersion()
Colin Cross6e511a92020-07-27 21:26:48 -07002267
2268 ndkStubDepTag := libraryDependencyTag{Kind: sharedLibraryDependency, ndk: true, makeSuffix: "." + version}
Dan Albert914449f2016-06-17 16:45:24 -07002269 actx.AddVariationDependencies([]blueprint.Variation{
Colin Cross5ec407b2020-09-30 11:41:33 -07002270 {Mutator: "version", Variation: version},
Dan Willemsen59339a22018-07-22 21:18:45 -07002271 {Mutator: "link", Variation: "shared"},
2272 }, ndkStubDepTag, variantNdkLibs...)
Colin Cross6e511a92020-07-27 21:26:48 -07002273
2274 ndkLateStubDepTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: lateLibraryDependency, ndk: true, makeSuffix: "." + version}
Dan Albert914449f2016-06-17 16:45:24 -07002275 actx.AddVariationDependencies([]blueprint.Variation{
Colin Cross5ec407b2020-09-30 11:41:33 -07002276 {Mutator: "version", Variation: version},
Dan Willemsen59339a22018-07-22 21:18:45 -07002277 {Mutator: "link", Variation: "shared"},
2278 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08002279
2280 if vndkdep := c.vndkdep; vndkdep != nil {
2281 if vndkdep.isVndkExt() {
Logan Chienf3511742017-10-31 18:04:35 +08002282 actx.AddVariationDependencies([]blueprint.Variation{
Colin Cross7228ecd2019-11-18 16:00:16 -08002283 c.ImageVariation(),
Dan Willemsen59339a22018-07-22 21:18:45 -07002284 {Mutator: "link", Variation: "shared"},
2285 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08002286 }
2287 }
Colin Cross6362e272015-10-29 15:25:03 -07002288}
Colin Cross21b9a242015-03-24 14:15:58 -07002289
Colin Crosse40b4ea2018-10-02 22:25:58 -07002290func BeginMutator(ctx android.BottomUpMutatorContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -07002291 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
2292 c.beginMutator(ctx)
2293 }
2294}
2295
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002296// Whether a module can link to another module, taking into
2297// account NDK linking.
Jooyung Han479ca172020-10-19 18:51:07 +09002298func checkLinkType(ctx android.BaseModuleContext, from LinkableInterface, to LinkableInterface,
Colin Cross6e511a92020-07-27 21:26:48 -07002299 tag blueprint.DependencyTag) {
2300
2301 switch t := tag.(type) {
2302 case dependencyTag:
2303 if t != vndkExtDepTag {
2304 return
2305 }
2306 case libraryDependencyTag:
2307 default:
2308 return
2309 }
2310
Ivan Lozanof9e21722020-12-02 09:00:51 -05002311 if from.Target().Os != android.Android {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002312 // Host code is not restricted
2313 return
2314 }
Ivan Lozano52767be2019-10-18 14:49:46 -07002315
2316 // VNDK is cc.Module supported only for now.
2317 if ccFrom, ok := from.(*Module); ok && from.UseVndk() {
Justin Yun63e9ec72020-10-29 16:49:43 +09002318 // Though allowed dependency is limited by the image mutator,
2319 // each vendor and product module needs to check link-type
2320 // for VNDK.
Ivan Lozano52767be2019-10-18 14:49:46 -07002321 if ccTo, ok := to.(*Module); ok {
2322 if ccFrom.vndkdep != nil {
2323 ccFrom.vndkdep.vndkCheckLinkType(ctx, ccTo, tag)
2324 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -04002325 } else if _, ok := to.(LinkableInterface); !ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07002326 ctx.ModuleErrorf("Attempting to link VNDK cc.Module with unsupported module type")
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002327 }
2328 return
2329 }
Ivan Lozano52767be2019-10-18 14:49:46 -07002330 if from.SdkVersion() == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002331 // Platform code can link to anything
2332 return
2333 }
Yifan Hong1b3348d2020-01-21 15:53:22 -08002334 if from.InRamdisk() {
2335 // Ramdisk code is not NDK
2336 return
2337 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002338 if from.InVendorRamdisk() {
2339 // Vendor ramdisk code is not NDK
2340 return
2341 }
Ivan Lozano52767be2019-10-18 14:49:46 -07002342 if from.InRecovery() {
Jiyong Parkf9332f12018-02-01 00:54:12 +09002343 // Recovery code is not NDK
2344 return
2345 }
Colin Cross31076b32020-10-23 17:22:06 -07002346 if c, ok := to.(*Module); ok {
2347 if c.ToolchainLibrary() {
2348 // These are always allowed
2349 return
2350 }
2351 if c.NdkPrebuiltStl() {
2352 // These are allowed, but they don't set sdk_version
2353 return
2354 }
2355 if c.StubDecorator() {
2356 // These aren't real libraries, but are the stub shared libraries that are included in
2357 // the NDK.
2358 return
2359 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002360 }
Logan Chien834b9a62019-01-14 15:39:03 +08002361
Ivan Lozano52767be2019-10-18 14:49:46 -07002362 if strings.HasPrefix(ctx.ModuleName(), "libclang_rt.") && to.Module().Name() == "libc++" {
Logan Chien834b9a62019-01-14 15:39:03 +08002363 // Bug: http://b/121358700 - Allow libclang_rt.* shared libraries (with sdk_version)
2364 // to link to libc++ (non-NDK and without sdk_version).
2365 return
2366 }
2367
Ivan Lozano52767be2019-10-18 14:49:46 -07002368 if to.SdkVersion() == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002369 // NDK code linking to platform code is never okay.
2370 ctx.ModuleErrorf("depends on non-NDK-built library %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002371 ctx.OtherModuleName(to.Module()))
Dan Willemsen155d17c2019-02-06 18:30:02 -08002372 return
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002373 }
2374
2375 // At this point we know we have two NDK libraries, but we need to
2376 // check that we're not linking against anything built against a higher
2377 // API level, as it is only valid to link against older or equivalent
2378 // APIs.
2379
Inseob Kim01a28722018-04-11 09:48:45 +09002380 // Current can link against anything.
Ivan Lozano52767be2019-10-18 14:49:46 -07002381 if from.SdkVersion() != "current" {
Inseob Kim01a28722018-04-11 09:48:45 +09002382 // Otherwise we need to check.
Ivan Lozano52767be2019-10-18 14:49:46 -07002383 if to.SdkVersion() == "current" {
Inseob Kim01a28722018-04-11 09:48:45 +09002384 // Current can't be linked against by anything else.
2385 ctx.ModuleErrorf("links %q built against newer API version %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002386 ctx.OtherModuleName(to.Module()), "current")
Inseob Kim01a28722018-04-11 09:48:45 +09002387 } else {
Ivan Lozano52767be2019-10-18 14:49:46 -07002388 fromApi, err := strconv.Atoi(from.SdkVersion())
Inseob Kim01a28722018-04-11 09:48:45 +09002389 if err != nil {
2390 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09002391 "Invalid sdk_version value (must be int or current): %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002392 from.SdkVersion())
Inseob Kim01a28722018-04-11 09:48:45 +09002393 }
Ivan Lozano52767be2019-10-18 14:49:46 -07002394 toApi, err := strconv.Atoi(to.SdkVersion())
Inseob Kim01a28722018-04-11 09:48:45 +09002395 if err != nil {
2396 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09002397 "Invalid sdk_version value (must be int or current): %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002398 to.SdkVersion())
Inseob Kim01a28722018-04-11 09:48:45 +09002399 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002400
Inseob Kim01a28722018-04-11 09:48:45 +09002401 if toApi > fromApi {
2402 ctx.ModuleErrorf("links %q built against newer API version %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002403 ctx.OtherModuleName(to.Module()), to.SdkVersion())
Inseob Kim01a28722018-04-11 09:48:45 +09002404 }
2405 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002406 }
Dan Albert202fe492017-12-15 13:56:59 -08002407
2408 // Also check that the two STL choices are compatible.
Ivan Lozano52767be2019-10-18 14:49:46 -07002409 fromStl := from.SelectedStl()
2410 toStl := to.SelectedStl()
Dan Albert202fe492017-12-15 13:56:59 -08002411 if fromStl == "" || toStl == "" {
2412 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09002413 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08002414 // We can be permissive with the system "STL" since it is only the C++
2415 // ABI layer, but in the future we should make sure that everyone is
2416 // using either libc++ or nothing.
Colin Crossb60190a2018-09-04 16:28:17 -07002417 } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
Dan Albert202fe492017-12-15 13:56:59 -08002418 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
Ivan Lozano52767be2019-10-18 14:49:46 -07002419 from.SelectedStl(), ctx.OtherModuleName(to.Module()),
2420 to.SelectedStl())
Dan Albert202fe492017-12-15 13:56:59 -08002421 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002422}
2423
Jooyung Han479ca172020-10-19 18:51:07 +09002424func checkLinkTypeMutator(ctx android.BottomUpMutatorContext) {
2425 if c, ok := ctx.Module().(*Module); ok {
2426 ctx.VisitDirectDeps(func(dep android.Module) {
2427 depTag := ctx.OtherModuleDependencyTag(dep)
2428 ccDep, ok := dep.(LinkableInterface)
2429 if ok {
2430 checkLinkType(ctx, c, ccDep, depTag)
2431 }
2432 })
2433 }
2434}
2435
Jiyong Park5fb8c102018-04-09 12:03:06 +09002436// Tests whether the dependent library is okay to be double loaded inside a single process.
Jooyung Hana70f0672019-01-18 15:20:43 +09002437// If a library has a vendor variant and is a (transitive) dependency of an LLNDK library,
2438// it is subject to be double loaded. Such lib should be explicitly marked as double_loadable: true
Jiyong Park5fb8c102018-04-09 12:03:06 +09002439// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
Jooyung Hana70f0672019-01-18 15:20:43 +09002440func checkDoubleLoadableLibraries(ctx android.TopDownMutatorContext) {
2441 check := func(child, parent android.Module) bool {
2442 to, ok := child.(*Module)
2443 if !ok {
Jooyung Han479ca172020-10-19 18:51:07 +09002444 return false
Jooyung Hana70f0672019-01-18 15:20:43 +09002445 }
Jiyong Park5fb8c102018-04-09 12:03:06 +09002446
Jooyung Hana70f0672019-01-18 15:20:43 +09002447 if lib, ok := to.linker.(*libraryDecorator); !ok || !lib.shared() {
2448 return false
Jiyong Park5fb8c102018-04-09 12:03:06 +09002449 }
Jooyung Hana70f0672019-01-18 15:20:43 +09002450
Jiyong Park0474e1f2021-01-14 14:26:06 +09002451 // These dependencies are not excercised at runtime. Tracking these will give us
2452 // false negative, so skip.
Jiyong Park1ad8e162020-12-01 23:40:09 +09002453 depTag := ctx.OtherModuleDependencyTag(child)
2454 if IsHeaderDepTag(depTag) {
2455 return false
2456 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09002457 if depTag == staticVariantTag {
2458 return false
2459 }
2460 if depTag == stubImplDepTag {
2461 return false
2462 }
Jiyong Park1ad8e162020-12-01 23:40:09 +09002463
Justin Yun63e9ec72020-10-29 16:49:43 +09002464 // Even if target lib has no vendor variant, keep checking dependency
2465 // graph in case it depends on vendor_available or product_available
2466 // but not double_loadable transtively.
2467 if !to.HasNonSystemVariants() {
Jooyung Hana70f0672019-01-18 15:20:43 +09002468 return true
Jiyong Park5fb8c102018-04-09 12:03:06 +09002469 }
Jooyung Hana70f0672019-01-18 15:20:43 +09002470
Jiyong Park0474e1f2021-01-14 14:26:06 +09002471 // The happy path. Keep tracking dependencies until we hit a non double-loadable
2472 // one.
2473 if Bool(to.VendorProperties.Double_loadable) {
2474 return true
2475 }
2476
Ivan Lozanod7586b62021-04-01 09:49:36 -04002477 if to.IsVndkSp() || to.IsLlndk() {
Jooyung Hana70f0672019-01-18 15:20:43 +09002478 return false
2479 }
2480
Jooyung Hana70f0672019-01-18 15:20:43 +09002481 ctx.ModuleErrorf("links a library %q which is not LL-NDK, "+
2482 "VNDK-SP, or explicitly marked as 'double_loadable:true'. "+
Jiyong Park0474e1f2021-01-14 14:26:06 +09002483 "Dependency list: %s", ctx.OtherModuleName(to), ctx.GetPathString(false))
Jooyung Hana70f0672019-01-18 15:20:43 +09002484 return false
2485 }
2486 if module, ok := ctx.Module().(*Module); ok {
2487 if lib, ok := module.linker.(*libraryDecorator); ok && lib.shared() {
Jiyong Park0474e1f2021-01-14 14:26:06 +09002488 if lib.hasLLNDKStubs() {
Jooyung Hana70f0672019-01-18 15:20:43 +09002489 ctx.WalkDeps(check)
2490 }
Jiyong Park5fb8c102018-04-09 12:03:06 +09002491 }
2492 }
2493}
2494
Colin Crossc99deeb2016-04-11 15:06:20 -07002495// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07002496func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08002497 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08002498
Colin Cross0de8a1e2020-09-18 14:15:30 -07002499 var directStaticDeps []StaticLibraryInfo
2500 var directSharedDeps []SharedLibraryInfo
Jeff Gaston294356f2017-09-27 17:05:30 -07002501
Colin Cross0de8a1e2020-09-18 14:15:30 -07002502 reexportExporter := func(exporter FlagExporterInfo) {
2503 depPaths.ReexportedDirs = append(depPaths.ReexportedDirs, exporter.IncludeDirs...)
2504 depPaths.ReexportedSystemDirs = append(depPaths.ReexportedSystemDirs, exporter.SystemIncludeDirs...)
2505 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, exporter.Flags...)
2506 depPaths.ReexportedDeps = append(depPaths.ReexportedDeps, exporter.Deps...)
2507 depPaths.ReexportedGeneratedHeaders = append(depPaths.ReexportedGeneratedHeaders, exporter.GeneratedHeaders...)
Inseob Kim69378442019-06-03 19:10:47 +09002508 }
2509
Jooyung Hande34d232020-07-23 13:04:15 +09002510 // For the dependency from platform to apex, use the latest stubs
2511 c.apexSdkVersion = android.FutureApiLevel
Colin Cross56a83212020-09-15 18:30:11 -07002512 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2513 if !apexInfo.IsForPlatform() {
Jiyong Park4eab21d2021-04-15 15:17:54 +09002514 c.apexSdkVersion = apexInfo.MinSdkVersion
Jooyung Hande34d232020-07-23 13:04:15 +09002515 }
2516
2517 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
2518 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
2519 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
2520 // (b/144430859)
2521 c.apexSdkVersion = android.FutureApiLevel
2522 }
2523
Colin Crossd11fcda2017-10-23 17:59:01 -07002524 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002525 depName := ctx.OtherModuleName(dep)
2526 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07002527
Ivan Lozano52767be2019-10-18 14:49:46 -07002528 ccDep, ok := dep.(LinkableInterface)
2529 if !ok {
2530
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002531 // handling for a few module types that aren't cc Module but that are also supported
2532 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07002533 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002534 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07002535 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
2536 genRule.GeneratedSourceFiles()...)
2537 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002538 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07002539 }
Colin Crosse90bfd12017-04-26 16:59:26 -07002540 // Support exported headers from a generated_sources dependency
2541 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07002542 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002543 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Inseob Kimd110f872019-12-06 13:15:38 +09002544 depPaths.GeneratedDeps = append(depPaths.GeneratedDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08002545 genRule.GeneratedDeps()...)
Jiyong Park74955042019-10-22 20:19:51 +09002546 dirs := genRule.GeneratedHeaderDirs()
Inseob Kim69378442019-06-03 19:10:47 +09002547 depPaths.IncludeDirs = append(depPaths.IncludeDirs, dirs...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002548 if depTag == genHeaderExportDepTag {
Inseob Kim69378442019-06-03 19:10:47 +09002549 depPaths.ReexportedDirs = append(depPaths.ReexportedDirs, dirs...)
Inseob Kimd110f872019-12-06 13:15:38 +09002550 depPaths.ReexportedGeneratedHeaders = append(depPaths.ReexportedGeneratedHeaders,
2551 genRule.GeneratedSourceFiles()...)
Inseob Kim69378442019-06-03 19:10:47 +09002552 depPaths.ReexportedDeps = append(depPaths.ReexportedDeps, genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07002553 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
Jiyong Park74955042019-10-22 20:19:51 +09002554 c.sabi.Properties.ReexportedIncludes = append(c.sabi.Properties.ReexportedIncludes, dirs.Strings()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07002555
Dan Willemsenb3454ab2016-09-28 17:34:58 -07002556 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07002557 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002558 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07002559 }
Colin Crosscef792e2021-06-11 18:01:26 -07002560 case CrtBeginDepTag:
2561 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
2562 case CrtEndDepTag:
2563 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
Colin Crossca860ac2016-01-04 14:34:37 -08002564 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002565 return
2566 }
2567
Colin Crossfe17f6f2019-03-28 19:30:56 -07002568 if depTag == android.ProtoPluginDepTag {
2569 return
2570 }
2571
Colin Crossd11fcda2017-10-23 17:59:01 -07002572 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002573 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
2574 return
2575 }
Colin Crossd11fcda2017-10-23 17:59:01 -07002576 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jooyung Han61b66e92020-03-21 14:21:46 +00002577 ctx.ModuleErrorf("Arch mismatch between %q(%v) and %q(%v)",
2578 ctx.ModuleName(), ctx.Arch().ArchType, depName, dep.Target().Arch.ArchType)
Colin Crossa1ad8d12016-06-01 17:09:44 -07002579 return
2580 }
2581
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07002582 if depTag == reuseObjTag {
Colin Crossa717db72020-10-23 14:53:06 -07002583 // Skip reused objects for stub libraries, they use their own stub object file instead.
2584 // The reuseObjTag dependency still exists because the LinkageMutator runs before the
2585 // version mutator, so the stubs variant is created from the shared variant that
2586 // already has the reuseObjTag dependency on the static variant.
Colin Cross31076b32020-10-23 17:22:06 -07002587 if !c.library.buildStubs() {
Colin Crossa717db72020-10-23 14:53:06 -07002588 staticAnalogue := ctx.OtherModuleProvider(dep, StaticLibraryInfoProvider).(StaticLibraryInfo)
2589 objs := staticAnalogue.ReuseObjects
2590 depPaths.Objs = depPaths.Objs.Append(objs)
2591 depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
2592 reexportExporter(depExporterInfo)
2593 }
Colin Cross0de8a1e2020-09-18 14:15:30 -07002594 return
Jiyong Parke4bb9862019-02-01 00:31:10 +09002595 }
2596
Colin Cross6e511a92020-07-27 21:26:48 -07002597 linkFile := ccDep.OutputFile()
2598
2599 if libDepTag, ok := depTag.(libraryDependencyTag); ok {
2600 // Only use static unwinder for legacy (min_sdk_version = 29) apexes (b/144430859)
Dan Albertc8060532020-07-22 22:32:17 -07002601 if libDepTag.staticUnwinder && c.apexSdkVersion.GreaterThan(android.SdkVersion_Android10) {
Peter Collingbournedc4f9862020-02-12 17:13:25 -08002602 return
2603 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08002604
Jiyong Parke3867542020-12-03 17:28:25 +09002605 if !apexInfo.IsForPlatform() && libDepTag.excludeInApex {
2606 return
2607 }
2608
Colin Cross0de8a1e2020-09-18 14:15:30 -07002609 depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
Colin Crossc99deeb2016-04-11 15:06:20 -07002610
Colin Cross6e511a92020-07-27 21:26:48 -07002611 var ptr *android.Paths
2612 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07002613
Colin Cross6e511a92020-07-27 21:26:48 -07002614 depFile := android.OptionalPath{}
Colin Cross26c34ed2016-09-30 17:10:16 -07002615
Colin Cross6e511a92020-07-27 21:26:48 -07002616 switch {
2617 case libDepTag.header():
Colin Cross649d8172020-12-10 12:30:21 -08002618 if !ctx.OtherModuleHasProvider(dep, HeaderLibraryInfoProvider) {
2619 if !ctx.Config().AllowMissingDependencies() {
2620 ctx.ModuleErrorf("module %q is not a header library", depName)
2621 } else {
2622 ctx.AddMissingDependencies([]string{depName})
2623 }
2624 return
2625 }
Colin Cross6e511a92020-07-27 21:26:48 -07002626 case libDepTag.shared():
Colin Cross0de8a1e2020-09-18 14:15:30 -07002627 if !ctx.OtherModuleHasProvider(dep, SharedLibraryInfoProvider) {
2628 if !ctx.Config().AllowMissingDependencies() {
2629 ctx.ModuleErrorf("module %q is not a shared library", depName)
2630 } else {
2631 ctx.AddMissingDependencies([]string{depName})
2632 }
2633 return
2634 }
Jiyong Parke3867542020-12-03 17:28:25 +09002635
Jiyong Park7d55b612021-06-11 17:22:09 +09002636 sharedLibraryInfo, returnedDepExporterInfo := ChooseStubOrImpl(ctx, dep)
2637 depExporterInfo = returnedDepExporterInfo
Colin Cross0de8a1e2020-09-18 14:15:30 -07002638
Jiyong Park1ad8e162020-12-01 23:40:09 +09002639 // Stubs lib doesn't link to the shared lib dependencies. Don't set
2640 // linkFile, depFile, and ptr.
2641 if c.IsStubs() {
2642 break
2643 }
2644
Colin Cross0de8a1e2020-09-18 14:15:30 -07002645 linkFile = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
2646 depFile = sharedLibraryInfo.TableOfContents
2647
Colin Cross6e511a92020-07-27 21:26:48 -07002648 ptr = &depPaths.SharedLibs
2649 switch libDepTag.Order {
2650 case earlyLibraryDependency:
2651 ptr = &depPaths.EarlySharedLibs
2652 depPtr = &depPaths.EarlySharedLibsDeps
2653 case normalLibraryDependency:
2654 ptr = &depPaths.SharedLibs
2655 depPtr = &depPaths.SharedLibsDeps
Colin Cross0de8a1e2020-09-18 14:15:30 -07002656 directSharedDeps = append(directSharedDeps, sharedLibraryInfo)
Colin Cross6e511a92020-07-27 21:26:48 -07002657 case lateLibraryDependency:
2658 ptr = &depPaths.LateSharedLibs
2659 depPtr = &depPaths.LateSharedLibsDeps
2660 default:
2661 panic(fmt.Errorf("unexpected library dependency order %d", libDepTag.Order))
Colin Crossc99deeb2016-04-11 15:06:20 -07002662 }
Colin Cross6e511a92020-07-27 21:26:48 -07002663 case libDepTag.static():
Colin Cross0de8a1e2020-09-18 14:15:30 -07002664 if !ctx.OtherModuleHasProvider(dep, StaticLibraryInfoProvider) {
2665 if !ctx.Config().AllowMissingDependencies() {
2666 ctx.ModuleErrorf("module %q is not a static library", depName)
2667 } else {
2668 ctx.AddMissingDependencies([]string{depName})
2669 }
2670 return
2671 }
Jiyong Park1ad8e162020-12-01 23:40:09 +09002672
2673 // Stubs lib doesn't link to the static lib dependencies. Don't set
2674 // linkFile, depFile, and ptr.
2675 if c.IsStubs() {
2676 break
2677 }
2678
Colin Cross0de8a1e2020-09-18 14:15:30 -07002679 staticLibraryInfo := ctx.OtherModuleProvider(dep, StaticLibraryInfoProvider).(StaticLibraryInfo)
2680 linkFile = android.OptionalPathForPath(staticLibraryInfo.StaticLibrary)
Colin Cross6e511a92020-07-27 21:26:48 -07002681 if libDepTag.wholeStatic {
2682 ptr = &depPaths.WholeStaticLibs
Colin Cross0de8a1e2020-09-18 14:15:30 -07002683 if len(staticLibraryInfo.Objects.objFiles) > 0 {
2684 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLibraryInfo.Objects)
Inseob Kimeec88e12020-01-22 11:11:29 +09002685 } else {
Colin Cross0de8a1e2020-09-18 14:15:30 -07002686 // This case normally catches prebuilt static
2687 // libraries, but it can also occur when
2688 // AllowMissingDependencies is on and the
2689 // dependencies has no sources of its own
2690 // but has a whole_static_libs dependency
2691 // on a missing library. We want to depend
2692 // on the .a file so that there is something
2693 // in the dependency tree that contains the
2694 // error rule for the missing transitive
2695 // dependency.
2696 depPaths.WholeStaticLibsFromPrebuilts = append(depPaths.WholeStaticLibsFromPrebuilts, linkFile.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07002697 }
Colin Cross6e511a92020-07-27 21:26:48 -07002698 } else {
2699 switch libDepTag.Order {
2700 case earlyLibraryDependency:
2701 panic(fmt.Errorf("early static libs not suppported"))
2702 case normalLibraryDependency:
2703 // static dependencies will be handled separately so they can be ordered
2704 // using transitive dependencies.
2705 ptr = nil
Colin Cross0de8a1e2020-09-18 14:15:30 -07002706 directStaticDeps = append(directStaticDeps, staticLibraryInfo)
Colin Cross6e511a92020-07-27 21:26:48 -07002707 case lateLibraryDependency:
2708 ptr = &depPaths.LateStaticLibs
2709 default:
2710 panic(fmt.Errorf("unexpected library dependency order %d", libDepTag.Order))
Inseob Kimeec88e12020-01-22 11:11:29 +09002711 }
2712 }
2713 }
2714
Colin Cross6e511a92020-07-27 21:26:48 -07002715 if libDepTag.static() && !libDepTag.wholeStatic {
2716 if !ccDep.CcLibraryInterface() || !ccDep.Static() {
2717 ctx.ModuleErrorf("module %q not a static library", depName)
2718 return
2719 }
Logan Chien43d34c32017-12-20 01:17:32 +08002720
Colin Cross6e511a92020-07-27 21:26:48 -07002721 // When combining coverage files for shared libraries and executables, coverage files
2722 // in static libraries act as if they were whole static libraries. The same goes for
2723 // source based Abi dump files.
2724 if c, ok := ccDep.(*Module); ok {
2725 staticLib := c.linker.(libraryInterface)
2726 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
2727 staticLib.objs().coverageFiles...)
2728 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
2729 staticLib.objs().sAbiDumpFiles...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002730 } else {
Colin Cross6e511a92020-07-27 21:26:48 -07002731 // Handle non-CC modules here
2732 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
Colin Cross0de8a1e2020-09-18 14:15:30 -07002733 ccDep.CoverageFiles()...)
Jiyong Parkde866cb2018-12-07 23:08:36 +09002734 }
2735 }
2736
Colin Cross6e511a92020-07-27 21:26:48 -07002737 if ptr != nil {
2738 if !linkFile.Valid() {
2739 if !ctx.Config().AllowMissingDependencies() {
2740 ctx.ModuleErrorf("module %q missing output file", depName)
2741 } else {
2742 ctx.AddMissingDependencies([]string{depName})
2743 }
2744 return
2745 }
2746 *ptr = append(*ptr, linkFile.Path())
2747 }
2748
2749 if depPtr != nil {
2750 dep := depFile
2751 if !dep.Valid() {
2752 dep = linkFile
2753 }
2754 *depPtr = append(*depPtr, dep.Path())
2755 }
2756
Colin Cross0de8a1e2020-09-18 14:15:30 -07002757 depPaths.IncludeDirs = append(depPaths.IncludeDirs, depExporterInfo.IncludeDirs...)
2758 depPaths.SystemIncludeDirs = append(depPaths.SystemIncludeDirs, depExporterInfo.SystemIncludeDirs...)
2759 depPaths.GeneratedDeps = append(depPaths.GeneratedDeps, depExporterInfo.Deps...)
2760 depPaths.Flags = append(depPaths.Flags, depExporterInfo.Flags...)
2761
2762 if libDepTag.reexportFlags {
2763 reexportExporter(depExporterInfo)
2764 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
2765 // Re-exported shared library headers must be included as well since they can help us with type information
2766 // about template instantiations (instantiated from their headers).
2767 // -isystem headers are not included since for bionic libraries, abi-filtering is taken care of by version
2768 // scripts.
2769 c.sabi.Properties.ReexportedIncludes = append(
2770 c.sabi.Properties.ReexportedIncludes, depExporterInfo.IncludeDirs.Strings()...)
2771 }
2772
Ivan Lozanoc08897c2021-04-02 12:41:32 -04002773 makeLibName := MakeLibName(ctx, c, ccDep, depName) + libDepTag.makeSuffix
Colin Cross6e511a92020-07-27 21:26:48 -07002774 switch {
2775 case libDepTag.header():
Colin Cross370173e2020-07-29 12:48:33 -07002776 c.Properties.AndroidMkHeaderLibs = append(
2777 c.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07002778 case libDepTag.shared():
Colin Cross31076b32020-10-23 17:22:06 -07002779 if lib := moduleLibraryInterface(dep); lib != nil {
2780 if lib.buildStubs() && dep.(android.ApexModule).InAnyApex() {
Colin Cross6e511a92020-07-27 21:26:48 -07002781 // Add the dependency to the APEX(es) providing the library so that
2782 // m <module> can trigger building the APEXes as well.
Colin Cross56a83212020-09-15 18:30:11 -07002783 depApexInfo := ctx.OtherModuleProvider(dep, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09002784 for _, an := range depApexInfo.InApexVariants {
Colin Cross6e511a92020-07-27 21:26:48 -07002785 c.Properties.ApexesProvidingSharedLibs = append(
2786 c.Properties.ApexesProvidingSharedLibs, an)
2787 }
2788 }
2789 }
2790
2791 // Note: the order of libs in this list is not important because
2792 // they merely serve as Make dependencies and do not affect this lib itself.
Colin Cross370173e2020-07-29 12:48:33 -07002793 c.Properties.AndroidMkSharedLibs = append(
2794 c.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002795 // Record BaseLibName for snapshots.
2796 c.Properties.SnapshotSharedLibs = append(c.Properties.SnapshotSharedLibs, BaseLibName(depName))
Colin Cross6e511a92020-07-27 21:26:48 -07002797 case libDepTag.static():
2798 if libDepTag.wholeStatic {
2799 c.Properties.AndroidMkWholeStaticLibs = append(
2800 c.Properties.AndroidMkWholeStaticLibs, makeLibName)
2801 } else {
2802 c.Properties.AndroidMkStaticLibs = append(
2803 c.Properties.AndroidMkStaticLibs, makeLibName)
2804 }
Justin Yun5e035862021-06-29 20:50:37 +09002805 // Record BaseLibName for snapshots.
2806 c.Properties.SnapshotStaticLibs = append(c.Properties.SnapshotStaticLibs, BaseLibName(depName))
Colin Cross6e511a92020-07-27 21:26:48 -07002807 }
Jiyong Park1ad8e162020-12-01 23:40:09 +09002808 } else if !c.IsStubs() {
2809 // Stubs lib doesn't link to the runtime lib, object, crt, etc. dependencies.
2810
Colin Cross6e511a92020-07-27 21:26:48 -07002811 switch depTag {
2812 case runtimeDepTag:
2813 c.Properties.AndroidMkRuntimeLibs = append(
Ivan Lozanoc08897c2021-04-02 12:41:32 -04002814 c.Properties.AndroidMkRuntimeLibs, MakeLibName(ctx, c, ccDep, depName)+libDepTag.makeSuffix)
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002815 // Record BaseLibName for snapshots.
2816 c.Properties.SnapshotRuntimeLibs = append(c.Properties.SnapshotRuntimeLibs, BaseLibName(depName))
Colin Cross6e511a92020-07-27 21:26:48 -07002817 case objDepTag:
2818 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
2819 case CrtBeginDepTag:
Colin Crossc465efd2021-06-11 18:00:04 -07002820 depPaths.CrtBegin = append(depPaths.CrtBegin, linkFile.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07002821 case CrtEndDepTag:
Colin Crossc465efd2021-06-11 18:00:04 -07002822 depPaths.CrtEnd = append(depPaths.CrtEnd, linkFile.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07002823 case dynamicLinkerDepTag:
2824 depPaths.DynamicLinker = linkFile
2825 }
Jiyong Park27b188b2017-07-18 13:23:39 +09002826 }
Colin Crossca860ac2016-01-04 14:34:37 -08002827 })
2828
Jeff Gaston294356f2017-09-27 17:05:30 -07002829 // use the ordered dependencies as this module's dependencies
Colin Cross0de8a1e2020-09-18 14:15:30 -07002830 orderedStaticPaths, transitiveStaticLibs := orderStaticModuleDeps(directStaticDeps, directSharedDeps)
2831 depPaths.TranstiveStaticLibrariesForOrdering = transitiveStaticLibs
2832 depPaths.StaticLibs = append(depPaths.StaticLibs, orderedStaticPaths...)
Jeff Gaston294356f2017-09-27 17:05:30 -07002833
Colin Crossdd84e052017-05-17 13:44:16 -07002834 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07002835 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Jiyong Park74955042019-10-22 20:19:51 +09002836 depPaths.IncludeDirs = android.FirstUniquePaths(depPaths.IncludeDirs)
2837 depPaths.SystemIncludeDirs = android.FirstUniquePaths(depPaths.SystemIncludeDirs)
Inseob Kimd110f872019-12-06 13:15:38 +09002838 depPaths.GeneratedDeps = android.FirstUniquePaths(depPaths.GeneratedDeps)
Jiyong Park74955042019-10-22 20:19:51 +09002839 depPaths.ReexportedDirs = android.FirstUniquePaths(depPaths.ReexportedDirs)
2840 depPaths.ReexportedSystemDirs = android.FirstUniquePaths(depPaths.ReexportedSystemDirs)
Colin Crossb6715442017-10-24 11:13:31 -07002841 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Inseob Kim69378442019-06-03 19:10:47 +09002842 depPaths.ReexportedDeps = android.FirstUniquePaths(depPaths.ReexportedDeps)
Inseob Kimd110f872019-12-06 13:15:38 +09002843 depPaths.ReexportedGeneratedHeaders = android.FirstUniquePaths(depPaths.ReexportedGeneratedHeaders)
Dan Willemsenfe92c962017-08-29 12:28:37 -07002844
2845 if c.sabi != nil {
Inseob Kim69378442019-06-03 19:10:47 +09002846 c.sabi.Properties.ReexportedIncludes = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludes)
Dan Willemsenfe92c962017-08-29 12:28:37 -07002847 }
Colin Crossdd84e052017-05-17 13:44:16 -07002848
Colin Crossca860ac2016-01-04 14:34:37 -08002849 return depPaths
2850}
2851
Jiyong Park7d55b612021-06-11 17:22:09 +09002852// ChooseStubOrImpl determines whether a given dependency should be redirected to the stub variant
2853// of the dependency or not, and returns the SharedLibraryInfo and FlagExporterInfo for the right
2854// dependency. The stub variant is selected when the dependency crosses a boundary where each side
2855// has different level of updatability. For example, if a library foo in an APEX depends on a
2856// library bar which provides stable interface and exists in the platform, foo uses the stub variant
2857// of bar. If bar doesn't provide a stable interface (i.e. buildStubs() == false) or is in the
2858// same APEX as foo, the non-stub variant of bar is used.
2859func ChooseStubOrImpl(ctx android.ModuleContext, dep android.Module) (SharedLibraryInfo, FlagExporterInfo) {
2860 depName := ctx.OtherModuleName(dep)
2861 depTag := ctx.OtherModuleDependencyTag(dep)
2862 libDepTag, ok := depTag.(libraryDependencyTag)
2863 if !ok || !libDepTag.shared() {
2864 panic(fmt.Errorf("Unexpected dependency tag: %T", depTag))
2865 }
2866
2867 thisModule, ok := ctx.Module().(android.ApexModule)
2868 if !ok {
2869 panic(fmt.Errorf("Not an APEX module: %q", ctx.ModuleName()))
2870 }
2871
2872 useVndk := false
2873 bootstrap := false
2874 if linkable, ok := ctx.Module().(LinkableInterface); !ok {
2875 panic(fmt.Errorf("Not a Linkable module: %q", ctx.ModuleName()))
2876 } else {
2877 useVndk = linkable.UseVndk()
2878 bootstrap = linkable.Bootstrap()
2879 }
2880
2881 sharedLibraryInfo := ctx.OtherModuleProvider(dep, SharedLibraryInfoProvider).(SharedLibraryInfo)
2882 depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
2883 sharedLibraryStubsInfo := ctx.OtherModuleProvider(dep, SharedLibraryStubsProvider).(SharedLibraryStubsInfo)
2884 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2885
2886 if !libDepTag.explicitlyVersioned && len(sharedLibraryStubsInfo.SharedStubLibraries) > 0 {
2887 useStubs := false
2888
2889 if lib := moduleLibraryInterface(dep); lib.buildStubs() && useVndk { // LLNDK
2890 if !apexInfo.IsForPlatform() {
2891 // For platform libraries, use current version of LLNDK
2892 // If this is for use_vendor apex we will apply the same rules
2893 // of apex sdk enforcement below to choose right version.
2894 useStubs = true
2895 }
Jiyong Park9477c262021-06-22 20:23:05 +09002896 } else if apexInfo.IsForPlatform() || apexInfo.UsePlatformApis {
2897 // If not building for APEX or the containing APEX allows the use of
2898 // platform APIs, use stubs only when it is from an APEX (and not from
2899 // platform) However, for host, ramdisk, vendor_ramdisk, recovery or
2900 // bootstrap modules, always link to non-stub variant
Jiyong Park7d55b612021-06-11 17:22:09 +09002901 useStubs = dep.(android.ApexModule).NotInPlatform() && !bootstrap
2902 if useStubs {
2903 // Another exception: if this module is a test for an APEX, then
2904 // it is linked with the non-stub variant of a module in the APEX
2905 // as if this is part of the APEX.
2906 testFor := ctx.Provider(android.ApexTestForInfoProvider).(android.ApexTestForInfo)
2907 for _, apexContents := range testFor.ApexContents {
2908 if apexContents.DirectlyInApex(depName) {
2909 useStubs = false
2910 break
2911 }
2912 }
2913 }
2914 if useStubs {
2915 // Yet another exception: If this module and the dependency are
2916 // available to the same APEXes then skip stubs between their
2917 // platform variants. This complements the test_for case above,
2918 // which avoids the stubs on a direct APEX library dependency, by
2919 // avoiding stubs for indirect test dependencies as well.
2920 //
2921 // TODO(b/183882457): This doesn't work if the two libraries have
2922 // only partially overlapping apex_available. For that test_for
2923 // modules would need to be split into APEX variants and resolved
2924 // separately for each APEX they have access to.
2925 if android.AvailableToSameApexes(thisModule, dep.(android.ApexModule)) {
2926 useStubs = false
2927 }
2928 }
2929 } else {
2930 // If building for APEX, use stubs when the parent is in any APEX that
2931 // the child is not in.
2932 useStubs = !android.DirectlyInAllApexes(apexInfo, depName)
2933 }
2934
2935 // when to use (unspecified) stubs, use the latest one.
2936 if useStubs {
2937 stubs := sharedLibraryStubsInfo.SharedStubLibraries
2938 toUse := stubs[len(stubs)-1]
2939 sharedLibraryInfo = toUse.SharedLibraryInfo
2940 depExporterInfo = toUse.FlagExporterInfo
2941 }
2942 }
2943 return sharedLibraryInfo, depExporterInfo
2944}
2945
Colin Cross0de8a1e2020-09-18 14:15:30 -07002946// orderStaticModuleDeps rearranges the order of the static library dependencies of the module
2947// to match the topological order of the dependency tree, including any static analogues of
2948// direct shared libraries. It returns the ordered static dependencies, and an android.DepSet
2949// of the transitive dependencies.
2950func orderStaticModuleDeps(staticDeps []StaticLibraryInfo, sharedDeps []SharedLibraryInfo) (ordered android.Paths, transitive *android.DepSet) {
2951 transitiveStaticLibsBuilder := android.NewDepSetBuilder(android.TOPOLOGICAL)
2952 var staticPaths android.Paths
2953 for _, staticDep := range staticDeps {
2954 staticPaths = append(staticPaths, staticDep.StaticLibrary)
2955 transitiveStaticLibsBuilder.Transitive(staticDep.TransitiveStaticLibrariesForOrdering)
2956 }
2957 for _, sharedDep := range sharedDeps {
Liz Kammeref6dfea2021-06-08 15:37:09 -04002958 if sharedDep.TransitiveStaticLibrariesForOrdering != nil {
2959 transitiveStaticLibsBuilder.Transitive(sharedDep.TransitiveStaticLibrariesForOrdering)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002960 }
2961 }
2962 transitiveStaticLibs := transitiveStaticLibsBuilder.Build()
2963
2964 orderedTransitiveStaticLibs := transitiveStaticLibs.ToList()
2965
2966 // reorder the dependencies based on transitive dependencies
2967 staticPaths = android.FirstUniquePaths(staticPaths)
2968 _, orderedStaticPaths := android.FilterPathList(orderedTransitiveStaticLibs, staticPaths)
2969
2970 if len(orderedStaticPaths) != len(staticPaths) {
2971 missing, _ := android.FilterPathList(staticPaths, orderedStaticPaths)
2972 panic(fmt.Errorf("expected %d ordered static paths , got %d, missing %q %q %q", len(staticPaths), len(orderedStaticPaths), missing, orderedStaticPaths, staticPaths))
2973 }
2974
2975 return orderedStaticPaths, transitiveStaticLibs
2976}
2977
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002978// BaseLibName trims known prefixes and suffixes
2979func BaseLibName(depName string) string {
Colin Cross6e511a92020-07-27 21:26:48 -07002980 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
2981 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Paul Duffind23c7262020-12-11 18:13:08 +00002982 libName = android.RemoveOptionalPrebuiltPrefix(libName)
Colin Cross6e511a92020-07-27 21:26:48 -07002983 return libName
2984}
2985
Ivan Lozanoc08897c2021-04-02 12:41:32 -04002986func MakeLibName(ctx android.ModuleContext, c LinkableInterface, ccDep LinkableInterface, depName string) string {
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002987 libName := BaseLibName(depName)
Colin Cross127bb8b2020-12-16 16:46:01 -08002988 ccDepModule, _ := ccDep.(*Module)
2989 isLLndk := ccDepModule != nil && ccDepModule.IsLlndk()
Justin Yuncbca3732021-02-03 19:24:13 +09002990 nonSystemVariantsExist := ccDep.HasNonSystemVariants() || isLLndk
Colin Cross6e511a92020-07-27 21:26:48 -07002991
Justin Yuncbca3732021-02-03 19:24:13 +09002992 if ccDepModule != nil {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04002993 // TODO(ivanlozano) Support snapshots for Rust-produced C library variants.
Colin Cross6e511a92020-07-27 21:26:48 -07002994 // Use base module name for snapshots when exporting to Makefile.
Ivan Lozanod1dec542021-05-26 15:33:11 -04002995 if snapshotPrebuilt, ok := ccDepModule.linker.(SnapshotInterface); ok {
Justin Yuncbca3732021-02-03 19:24:13 +09002996 baseName := ccDepModule.BaseModuleName()
Colin Cross6e511a92020-07-27 21:26:48 -07002997
Ivan Lozanod1dec542021-05-26 15:33:11 -04002998 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix()
Colin Cross6e511a92020-07-27 21:26:48 -07002999 }
3000 }
3001
Yifan Hong60e0cfb2020-10-21 15:17:56 -07003002 if ctx.DeviceConfig().VndkUseCoreVariant() && ccDep.IsVndk() && !ccDep.MustUseVendorVariant() &&
3003 !c.InRamdisk() && !c.InVendorRamdisk() && !c.InRecovery() {
Colin Cross6e511a92020-07-27 21:26:48 -07003004 // The vendor module is a no-vendor-variant VNDK library. Depend on the
3005 // core module instead.
3006 return libName
Ivan Lozanoc08897c2021-04-02 12:41:32 -04003007 } else if ccDep.UseVndk() && nonSystemVariantsExist {
Justin Yuncbca3732021-02-03 19:24:13 +09003008 // The vendor and product modules in Make will have been renamed to not conflict with the
3009 // core module, so update the dependency name here accordingly.
Ivan Lozanoc08897c2021-04-02 12:41:32 -04003010 return libName + ccDep.SubName()
Colin Cross6e511a92020-07-27 21:26:48 -07003011 } else if ccDep.InRamdisk() && !ccDep.OnlyInRamdisk() {
3012 return libName + ramdiskSuffix
Yifan Hong60e0cfb2020-10-21 15:17:56 -07003013 } else if ccDep.InVendorRamdisk() && !ccDep.OnlyInVendorRamdisk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -05003014 return libName + VendorRamdiskSuffix
Colin Cross6e511a92020-07-27 21:26:48 -07003015 } else if ccDep.InRecovery() && !ccDep.OnlyInRecovery() {
3016 return libName + recoverySuffix
Ivan Lozanof9e21722020-12-02 09:00:51 -05003017 } else if ccDep.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross6e511a92020-07-27 21:26:48 -07003018 return libName + nativeBridgeSuffix
3019 } else {
3020 return libName
3021 }
3022}
3023
Colin Crossca860ac2016-01-04 14:34:37 -08003024func (c *Module) InstallInData() bool {
3025 if c.installer == nil {
3026 return false
3027 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07003028 return c.installer.inData()
3029}
3030
3031func (c *Module) InstallInSanitizerDir() bool {
3032 if c.installer == nil {
3033 return false
3034 }
3035 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07003036 return true
3037 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07003038 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08003039}
3040
Yifan Hong1b3348d2020-01-21 15:53:22 -08003041func (c *Module) InstallInRamdisk() bool {
3042 return c.InRamdisk()
3043}
3044
Yifan Hong60e0cfb2020-10-21 15:17:56 -07003045func (c *Module) InstallInVendorRamdisk() bool {
3046 return c.InVendorRamdisk()
3047}
3048
Jiyong Parkf9332f12018-02-01 00:54:12 +09003049func (c *Module) InstallInRecovery() bool {
Ivan Lozano52767be2019-10-18 14:49:46 -07003050 return c.InRecovery()
Jiyong Parkf9332f12018-02-01 00:54:12 +09003051}
3052
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01003053func (c *Module) MakeUninstallable() {
Martin Stjernholmbf37d162020-03-31 16:05:34 +01003054 if c.installer == nil {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01003055 c.ModuleBase.MakeUninstallable()
Martin Stjernholmbf37d162020-03-31 16:05:34 +01003056 return
3057 }
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01003058 c.installer.makeUninstallable(c)
Martin Stjernholmbf37d162020-03-31 16:05:34 +01003059}
3060
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07003061func (c *Module) HostToolPath() android.OptionalPath {
3062 if c.installer == nil {
3063 return android.OptionalPath{}
3064 }
3065 return c.installer.hostToolPath()
3066}
3067
Nan Zhangd4e641b2017-07-12 12:55:28 -07003068func (c *Module) IntermPathForModuleOut() android.OptionalPath {
3069 return c.outputFile
3070}
3071
Colin Cross41955e82019-05-29 14:40:35 -07003072func (c *Module) OutputFiles(tag string) (android.Paths, error) {
3073 switch tag {
3074 case "":
3075 if c.outputFile.Valid() {
3076 return android.Paths{c.outputFile.Path()}, nil
3077 }
3078 return android.Paths{}, nil
3079 default:
3080 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07003081 }
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07003082}
3083
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00003084func (c *Module) static() bool {
3085 if static, ok := c.linker.(interface {
3086 static() bool
3087 }); ok {
3088 return static.static()
3089 }
3090 return false
3091}
3092
Jiyong Park379de2f2018-12-19 02:47:14 +09003093func (c *Module) staticBinary() bool {
3094 if static, ok := c.linker.(interface {
3095 staticBinary() bool
3096 }); ok {
3097 return static.staticBinary()
3098 }
3099 return false
3100}
3101
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003102func (c *Module) testBinary() bool {
3103 if test, ok := c.linker.(interface {
3104 testBinary() bool
3105 }); ok {
3106 return test.testBinary()
3107 }
3108 return false
3109}
3110
Ivan Lozano3968d8f2020-12-14 11:27:52 -05003111// Header returns true if the module is a header-only variant. (See cc/library.go header()).
3112func (c *Module) Header() bool {
Jiyong Park1d1119f2019-07-29 21:27:18 +09003113 if h, ok := c.linker.(interface {
3114 header() bool
3115 }); ok {
3116 return h.header()
3117 }
3118 return false
3119}
3120
Ivan Lozanod7586b62021-04-01 09:49:36 -04003121func (c *Module) Binary() bool {
Inseob Kim7f283f42020-06-01 21:53:49 +09003122 if b, ok := c.linker.(interface {
3123 binary() bool
3124 }); ok {
3125 return b.binary()
3126 }
3127 return false
3128}
3129
Justin Yun5e035862021-06-29 20:50:37 +09003130func (c *Module) StaticExecutable() bool {
3131 if b, ok := c.linker.(*binaryDecorator); ok {
3132 return b.static()
3133 }
3134 return false
3135}
3136
Ivan Lozanod7586b62021-04-01 09:49:36 -04003137func (c *Module) Object() bool {
Inseob Kim1042d292020-06-01 23:23:05 +09003138 if o, ok := c.linker.(interface {
3139 object() bool
3140 }); ok {
3141 return o.object()
3142 }
3143 return false
3144}
3145
Ivan Lozanof9e21722020-12-02 09:00:51 -05003146func GetMakeLinkType(actx android.ModuleContext, c LinkableInterface) string {
Ivan Lozano52767be2019-10-18 14:49:46 -07003147 if c.UseVndk() {
Colin Cross127bb8b2020-12-16 16:46:01 -08003148 if c.IsLlndk() {
3149 if !c.IsLlndkPublic() {
Ivan Lozanof9e21722020-12-02 09:00:51 -05003150 return "native:vndk_private"
Colin Crossb60190a2018-09-04 16:28:17 -07003151 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003152 return "native:vndk"
Colin Crossb60190a2018-09-04 16:28:17 -07003153 }
Ivan Lozanof9e21722020-12-02 09:00:51 -05003154 if c.IsVndk() && !c.IsVndkExt() {
Colin Cross127bb8b2020-12-16 16:46:01 -08003155 if c.IsVndkPrivate() {
Ivan Lozanof9e21722020-12-02 09:00:51 -05003156 return "native:vndk_private"
Jooyung Han38002912019-05-16 04:01:54 +09003157 }
Ivan Lozanof9e21722020-12-02 09:00:51 -05003158 return "native:vndk"
Jooyung Han38002912019-05-16 04:01:54 +09003159 }
Ivan Lozanof9e21722020-12-02 09:00:51 -05003160 if c.InProduct() {
Justin Yun5f7f7e82019-11-18 19:52:14 +09003161 return "native:product"
3162 }
Jooyung Han38002912019-05-16 04:01:54 +09003163 return "native:vendor"
Yifan Hong1b3348d2020-01-21 15:53:22 -08003164 } else if c.InRamdisk() {
3165 return "native:ramdisk"
Yifan Hong60e0cfb2020-10-21 15:17:56 -07003166 } else if c.InVendorRamdisk() {
3167 return "native:vendor_ramdisk"
Ivan Lozano52767be2019-10-18 14:49:46 -07003168 } else if c.InRecovery() {
Colin Crossb60190a2018-09-04 16:28:17 -07003169 return "native:recovery"
Ivan Lozanof9e21722020-12-02 09:00:51 -05003170 } else if c.Target().Os == android.Android && c.SdkVersion() != "" {
Colin Crossb60190a2018-09-04 16:28:17 -07003171 return "native:ndk:none:none"
3172 // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
3173 //family, link := getNdkStlFamilyAndLinkType(c)
3174 //return fmt.Sprintf("native:ndk:%s:%s", family, link)
Ivan Lozano52767be2019-10-18 14:49:46 -07003175 } else if actx.DeviceConfig().VndkUseCoreVariant() && !c.MustUseVendorVariant() {
Vic Yangefd249e2018-11-12 20:19:56 -08003176 return "native:platform_vndk"
Colin Crossb60190a2018-09-04 16:28:17 -07003177 } else {
3178 return "native:platform"
3179 }
3180}
3181
Jiyong Park9d452992018-10-03 00:38:19 +09003182// Overrides ApexModule.IsInstallabeToApex()
Roland Levillainf89cd092019-07-29 16:22:59 +01003183// Only shared/runtime libraries and "test_per_src" tests are installable to APEX.
Jiyong Park9d452992018-10-03 00:38:19 +09003184func (c *Module) IsInstallableToApex() bool {
Colin Cross31076b32020-10-23 17:22:06 -07003185 if lib := c.library; lib != nil {
Jiyong Park73c54ee2019-10-22 20:31:18 +09003186 // Stub libs and prebuilt libs in a versioned SDK are not
3187 // installable to APEX even though they are shared libs.
Colin Cross31076b32020-10-23 17:22:06 -07003188 return lib.shared() && !lib.buildStubs() && c.ContainingSdk().Unversioned()
Roland Levillainf89cd092019-07-29 16:22:59 +01003189 } else if _, ok := c.linker.(testPerSrc); ok {
3190 return true
Jiyong Park9d452992018-10-03 00:38:19 +09003191 }
3192 return false
3193}
3194
Jiyong Parka90ca002019-10-07 15:47:24 +09003195func (c *Module) AvailableFor(what string) bool {
3196 if linker, ok := c.linker.(interface {
3197 availableFor(string) bool
3198 }); ok {
3199 return c.ApexModuleBase.AvailableFor(what) || linker.availableFor(what)
3200 } else {
3201 return c.ApexModuleBase.AvailableFor(what)
3202 }
3203}
3204
Jiyong Park62304bb2020-04-13 16:19:48 +09003205func (c *Module) TestFor() []string {
Jiyong Park46a512f2020-12-04 18:02:13 +09003206 return c.Properties.Test_for
Jiyong Park62304bb2020-04-13 16:19:48 +09003207}
3208
Colin Crossaede88c2020-08-11 12:17:01 -07003209func (c *Module) UniqueApexVariations() bool {
3210 if u, ok := c.compiler.(interface {
3211 uniqueApexVariations() bool
3212 }); ok {
3213 return u.uniqueApexVariations()
3214 } else {
3215 return false
3216 }
3217}
3218
Paul Duffin0cb37b92020-03-04 14:52:46 +00003219func (c *Module) EverInstallable() bool {
3220 return c.installer != nil &&
3221 // Check to see whether the module is actually ever installable.
3222 c.installer.everInstallable()
3223}
3224
Ivan Lozanod7586b62021-04-01 09:49:36 -04003225func (c *Module) PreventInstall() bool {
3226 return c.Properties.PreventInstall
3227}
3228
3229func (c *Module) Installable() *bool {
3230 return c.Properties.Installable
3231}
3232
3233func installable(c LinkableInterface, apexInfo android.ApexInfo) bool {
Paul Duffin0cb37b92020-03-04 14:52:46 +00003234 ret := c.EverInstallable() &&
3235 // Check to see whether the module has been configured to not be installed.
Ivan Lozanod7586b62021-04-01 09:49:36 -04003236 proptools.BoolDefault(c.Installable(), true) &&
3237 !c.PreventInstall() && c.OutputFile().Valid()
Jiyong Parkfe9a4302020-01-07 16:59:44 +09003238
3239 // The platform variant doesn't need further condition. Apex variants however might not
3240 // be installable because it will likely to be included in the APEX and won't appear
3241 // in the system partition.
Colin Cross56a83212020-09-15 18:30:11 -07003242 if apexInfo.IsForPlatform() {
Jiyong Parkfe9a4302020-01-07 16:59:44 +09003243 return ret
3244 }
3245
3246 // Special case for modules that are configured to be installed to /data, which includes
3247 // test modules. For these modules, both APEX and non-APEX variants are considered as
3248 // installable. This is because even the APEX variants won't be included in the APEX, but
3249 // will anyway be installed to /data/*.
3250 // See b/146995717
3251 if c.InstallInData() {
3252 return ret
3253 }
3254
3255 return false
Inseob Kim1f086e22019-05-09 13:29:15 +09003256}
3257
Logan Chien41eabe62019-04-10 13:33:58 +08003258func (c *Module) AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer) {
3259 if c.linker != nil {
3260 if library, ok := c.linker.(*libraryDecorator); ok {
3261 library.androidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
3262 }
3263 }
3264}
3265
Jiyong Park45bf82e2020-12-15 22:29:02 +09003266var _ android.ApexModule = (*Module)(nil)
3267
3268// Implements android.ApexModule
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09003269func (c *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07003270 depTag := ctx.OtherModuleDependencyTag(dep)
3271 libDepTag, isLibDepTag := depTag.(libraryDependencyTag)
3272
3273 if cc, ok := dep.(*Module); ok {
3274 if cc.HasStubsVariants() {
3275 if isLibDepTag && libDepTag.shared() {
3276 // dynamic dep to a stubs lib crosses APEX boundary
3277 return false
Jiyong Parkd7536ba2020-01-16 17:14:23 +09003278 }
Colin Cross6e511a92020-07-27 21:26:48 -07003279 if IsRuntimeDepTag(depTag) {
3280 // runtime dep to a stubs lib also crosses APEX boundary
Jiyong Parkd7536ba2020-01-16 17:14:23 +09003281 return false
3282 }
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09003283 }
Zhijun Heec285872021-04-24 10:47:08 -07003284 if cc.IsLlndk() {
3285 return false
3286 }
Colin Crossaac32222020-07-29 12:51:56 -07003287 if isLibDepTag && c.static() && libDepTag.shared() {
Colin Cross6e511a92020-07-27 21:26:48 -07003288 // shared_lib dependency from a static lib is considered as crossing
3289 // the APEX boundary because the dependency doesn't actually is
3290 // linked; the dependency is used only during the compilation phase.
3291 return false
3292 }
Jiyong Parke3867542020-12-03 17:28:25 +09003293
3294 if isLibDepTag && libDepTag.excludeInApex {
3295 return false
3296 }
Colin Cross6e511a92020-07-27 21:26:48 -07003297 }
Colin Crossc1b36442021-05-06 13:42:48 -07003298 if depTag == stubImplDepTag {
3299 // We don't track from an implementation library to its stubs.
Jiyong Park7d95a512020-05-10 15:16:24 +09003300 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09003301 }
Jiyong Park12177fc2021-01-05 14:37:15 +09003302 if depTag == staticVariantTag {
3303 // This dependency is for optimization (reuse *.o from the static lib). It doesn't
3304 // actually mean that the static lib (and its dependencies) are copied into the
3305 // APEX.
3306 return false
3307 }
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09003308 return true
3309}
3310
Jiyong Park45bf82e2020-12-15 22:29:02 +09003311// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003312func (c *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3313 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003314 // We ignore libclang_rt.* prebuilt libs since they declare sdk_version: 14(b/121358700)
3315 if strings.HasPrefix(ctx.OtherModuleName(c), "libclang_rt") {
3316 return nil
3317 }
3318 // b/154569636: set min_sdk_version correctly for toolchain_libraries
3319 if c.ToolchainLibrary() {
3320 return nil
3321 }
3322 // We don't check for prebuilt modules
3323 if _, ok := c.linker.(prebuiltLinkerInterface); ok {
3324 return nil
3325 }
3326 minSdkVersion := c.MinSdkVersion()
3327 if minSdkVersion == "apex_inherit" {
3328 return nil
3329 }
3330 if minSdkVersion == "" {
3331 // JNI libs within APK-in-APEX fall into here
3332 // Those are okay to set sdk_version instead
3333 // We don't have to check if this is a SDK variant because
3334 // non-SDK variant resets sdk_version, which works too.
3335 minSdkVersion = c.SdkVersion()
3336 }
Dan Albertc8060532020-07-22 22:32:17 -07003337 if minSdkVersion == "" {
3338 return fmt.Errorf("neither min_sdk_version nor sdk_version specificed")
3339 }
3340 // Not using nativeApiLevelFromUser because the context here is not
3341 // necessarily a native context.
3342 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
Jooyung Han749dc692020-04-15 11:03:39 +09003343 if err != nil {
3344 return err
3345 }
Dan Albertc8060532020-07-22 22:32:17 -07003346
3347 if ver.GreaterThan(sdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +09003348 return fmt.Errorf("newer SDK(%v)", ver)
3349 }
3350 return nil
3351}
3352
Paul Duffinb5769c12021-05-12 16:16:51 +01003353// Implements android.ApexModule
3354func (c *Module) AlwaysRequiresPlatformApexVariant() bool {
3355 // stub libraries and native bridge libraries are always available to platform
3356 return c.IsStubs() || c.Target().NativeBridge == android.NativeBridgeEnabled
3357}
3358
Colin Cross2ba19d92015-05-07 15:44:20 -07003359//
Colin Crosscfad1192015-11-02 16:43:11 -08003360// Defaults
3361//
Colin Crossca860ac2016-01-04 14:34:37 -08003362type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07003363 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07003364 android.DefaultsModuleBase
Liz Kammer3cf52112021-03-31 15:42:03 -04003365 // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel
3366 // target. This is primarily useful for modules that were architecture specific and instead are
3367 // handled in Bazel as a select().
3368 android.BazelModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +09003369 android.ApexModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08003370}
3371
Patrice Arrudac249c712019-03-19 17:00:29 -07003372// cc_defaults provides a set of properties that can be inherited by other cc
3373// modules. A module can use the properties from a cc_defaults using
3374// `defaults: ["<:default_module_name>"]`. Properties of both modules are
3375// merged (when possible) by prepending the default module's values to the
3376// depending module's values.
Colin Cross36242852017-06-23 15:06:31 -07003377func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07003378 return DefaultsFactory()
3379}
3380
Colin Cross36242852017-06-23 15:06:31 -07003381func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08003382 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08003383
Colin Cross36242852017-06-23 15:06:31 -07003384 module.AddProperties(props...)
3385 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08003386 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07003387 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08003388 &BaseCompilerProperties{},
3389 &BaseLinkerProperties{},
Paul Duffina37832a2019-07-18 12:31:26 +01003390 &ObjectLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07003391 &LibraryProperties{},
Colin Crosse1bb5d02019-09-24 14:55:04 -07003392 &StaticProperties{},
3393 &SharedProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07003394 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08003395 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07003396 &TestProperties{},
3397 &TestBinaryProperties{},
Colin Cross43287652020-06-30 10:15:07 -07003398 &BenchmarkProperties{},
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -07003399 &FuzzProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08003400 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08003401 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07003402 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07003403 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07003404 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08003405 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08003406 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09003407 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07003408 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07003409 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08003410 &android.ProtoProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -04003411 // RustBindgenProperties is included here so that cc_defaults can be used for rust_bindgen modules.
3412 &RustBindgenClangProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07003413 )
Colin Crosscfad1192015-11-02 16:43:11 -08003414
Liz Kammer3cf52112021-03-31 15:42:03 -04003415 // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module.
3416 android.InitBazelModule(module)
Jooyung Hancc372c52019-09-25 15:18:44 +09003417 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07003418
3419 return module
Colin Crosscfad1192015-11-02 16:43:11 -08003420}
3421
Jiyong Park2286afd2020-06-16 21:58:53 +09003422func (c *Module) IsSdkVariant() bool {
Lukacs T. Berki2063a0d2021-06-17 09:32:36 +02003423 return c.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +09003424}
3425
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003426func kytheExtractAllFactory() android.Singleton {
3427 return &kytheExtractAllSingleton{}
3428}
3429
3430type kytheExtractAllSingleton struct {
3431}
3432
3433func (ks *kytheExtractAllSingleton) GenerateBuildActions(ctx android.SingletonContext) {
3434 var xrefTargets android.Paths
3435 ctx.VisitAllModules(func(module android.Module) {
3436 if ccModule, ok := module.(xref); ok {
3437 xrefTargets = append(xrefTargets, ccModule.XrefCcFiles()...)
3438 }
3439 })
3440 // TODO(asmundak): Perhaps emit a rule to output a warning if there were no xrefTargets
3441 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07003442 ctx.Phony("xref_cxx", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003443 }
3444}
3445
Colin Cross06a931b2015-10-28 17:23:31 -07003446var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07003447var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08003448var BoolPtr = proptools.BoolPtr
3449var String = proptools.String
3450var StringPtr = proptools.StringPtr