blob: 587f63fe113ae7983c5f9368b2bdb8464a48242d [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090015// package apex implements build rules for creating the APEX files which are container for
16// lower-level system components. See https://source.android.com/devices/tech/ota/apex
Jiyong Park48ca7dc2018-10-10 14:01:00 +090017package apex
18
19import (
20 "fmt"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090021 "path/filepath"
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +000022 "regexp"
Colin Crossb614cd42024-10-11 12:52:21 -070023 "slices"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090024 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090025 "strings"
26
Jiyong Park48ca7dc2018-10-10 14:01:00 +090027 "github.com/google/blueprint"
Colin Crossb614cd42024-10-11 12:52:21 -070028 "github.com/google/blueprint/depset"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029 "github.com/google/blueprint/proptools"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070030
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070033 "android/soong/cc"
Spandan Dasfbcd5fe2024-09-30 22:30:39 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Park12a719c2021-01-07 15:31:24 +090036 "android/soong/filesystem"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090038 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070039 "android/soong/sh"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090040)
41
Jiyong Park8e6d52f2020-11-19 14:37:47 +090042func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000043 registerApexBuildComponents(android.InitRegistrationContext)
44}
Jiyong Park8e6d52f2020-11-19 14:37:47 +090045
Paul Duffin667893c2021-03-09 22:34:13 +000046func registerApexBuildComponents(ctx android.RegistrationContext) {
47 ctx.RegisterModuleType("apex", BundleFactory)
Yu Liu4c212ce2022-10-14 12:20:20 -070048 ctx.RegisterModuleType("apex_test", TestApexBundleFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000049 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Cole Faust912bc882023-03-08 12:29:50 -080050 ctx.RegisterModuleType("apex_defaults", DefaultsFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000051 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Wei Li1c66fc72022-05-09 23:59:14 -070052 ctx.RegisterModuleType("override_apex", OverrideApexFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000053 ctx.RegisterModuleType("apex_set", apexSetFactory)
54
55 ctx.PreDepsMutators(RegisterPreDepsMutators)
56 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090057}
58
59func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070060 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).UsesReverseDependencies()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090061}
62
63func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070064 ctx.TopDown("apex_info", apexInfoMutator)
65 ctx.BottomUp("apex_unique", apexUniqueVariationsMutator)
66 ctx.BottomUp("apex_test_for_deps", apexTestForDepsMutator)
67 ctx.BottomUp("apex_test_for", apexTestForMutator)
Paul Duffin28bf7ee2021-05-12 16:41:35 +010068 // Run mark_platform_availability before the apexMutator as the apexMutator needs to know whether
69 // it should create a platform variant.
Colin Cross8a962802024-10-09 15:29:27 -070070 ctx.BottomUp("mark_platform_availability", markPlatformAvailability)
Colin Cross7c035062024-03-28 12:18:42 -070071 ctx.Transition("apex", &apexTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070072 ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).MutatesDependencies()
73 ctx.BottomUp("apex_dcla_deps", apexDCLADepsMutator)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090074}
75
76type apexBundleProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090077 // Json manifest file describing meta info of this APEX bundle. Refer to
78 // system/apex/proto/apex_manifest.proto for the schema. Default: "apex_manifest.json"
Jiyong Park8e6d52f2020-11-19 14:37:47 +090079 Manifest *string `android:"path"`
80
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090081 // AndroidManifest.xml file used for the zip container of this APEX bundle. If unspecified,
82 // a default one is automatically generated.
Inseob Kimb1142342024-07-23 13:39:54 +090083 AndroidManifest proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +090084
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090085 // Determines the file contexts file for setting the security contexts to files in this APEX
86 // bundle. For platform APEXes, this should points to a file under /system/sepolicy Default:
87 // /system/sepolicy/apex/<module_name>_file_contexts.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090088 File_contexts *string `android:"path"`
89
Jooyung Hanaf730952023-02-28 14:13:38 +090090 // By default, file_contexts is amended by force-labelling / and /apex_manifest.pb as system_file
91 // to avoid mistakes. When set as true, no force-labelling.
92 Use_file_contexts_as_is *bool
93
Jingwen Chendea7a642023-03-28 11:30:50 +000094 // Path to the canned fs config file for customizing file's
95 // uid/gid/mod/capabilities. The content of this file is appended to the
96 // default config, so that the custom entries are preferred. The format is
97 // /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where
98 // path_or_glob is a path or glob pattern for a file or set of files,
99 // uid/gid are numerial values of user ID and group ID, mode is octal value
100 // for the file mode, and cap is hexadecimal value for the capability.
Inseob Kimb1142342024-07-23 13:39:54 +0900101 Canned_fs_config proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park038e8522021-12-13 23:56:35 +0900102
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900103 ApexNativeDependencies
104
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900105 Multilib apexMultilibProperties
106
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000107 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
108 Rros []string
109
Anton Hanssone7545852023-02-24 11:06:07 +0000110 // List of bootclasspath fragments that are embedded inside this APEX bundle.
111 Bootclasspath_fragments []string
112
113 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
Inseob Kimb1142342024-07-23 13:39:54 +0900114 Systemserverclasspath_fragments proptools.Configurable[[]string]
115 ResolvedSystemserverclasspathFragments []string `blueprint:"mutated"`
Anton Hanssone7545852023-02-24 11:06:07 +0000116
117 // List of java libraries that are embedded inside this APEX bundle.
118 Java_libs []string
119
Sundong Ahn80c04892021-11-23 00:57:19 +0000120 // List of sh binaries that are embedded inside this APEX bundle.
121 Sh_binaries []string
122
Paul Duffin3abc1742021-03-15 19:32:23 +0000123 // List of platform_compat_config files that are embedded inside this APEX bundle.
124 Compat_configs []string
125
Jiyong Park12a719c2021-01-07 15:31:24 +0900126 // List of filesystem images that are embedded inside this APEX bundle.
127 Filesystems []string
128
Jooyung Hana8bd72a2023-11-02 11:56:48 +0900129 // List of module names which we don't want to add as transitive deps. This can be used as
130 // a workaround when the current implementation collects more than necessary. For example,
131 // Rust binaries with prefer_rlib:true add unnecessary dependencies.
132 Unwanted_transitive_deps []string
133
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900134 // Whether this APEX is considered updatable or not. When set to true, this will enforce
135 // additional rules for making sure that the APEX is truly updatable. To be updatable,
136 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000137 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900138 Updatable *bool
139
Jiyong Parkf4020582021-11-29 12:37:10 +0900140 // Marks that this APEX is designed to be updatable in the future, although it's not
141 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
142 // updatable APEXes. Currently, this disables the size optimization, so that the size of
143 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
144 // false.
145 Future_updatable *bool
146
Jiyong Park1bc84122021-06-22 20:23:05 +0900147 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
148 // false`. Default is false.
149 Platform_apis *bool
150
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900151 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
152 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900153 Installable *bool
154
Jooyung Hanb19a0dd2024-07-02 16:47:31 +0900155 // Deprecated. Do not use. TODO(b/350644693) remove this after removing all usage
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900156 Use_vndk_as_stable *bool
157
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900158 // The type of filesystem to use. Either 'ext4', 'f2fs' or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900159 Payload_fs_type *string
160
161 // For telling the APEX to ignore special handling for system libraries such as bionic.
162 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900163 Ignore_system_library_special_case *bool
164
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100165 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100166 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100167 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900168
169 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
170 // used in tests.
171 Test_only_unsigned_payload *bool
172
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000173 // Whenever apex should be compressed, regardless of product flag used. Should be only
174 // used in tests.
175 Test_only_force_compression *bool
176
Jooyung Han09c11ad2021-10-27 03:45:31 +0900177 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
178 // with the tool to sign payload contents.
179 Custom_sign_tool *string
180
Dennis Shenaf41bc12022-08-03 16:46:43 +0000181 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
182 // in a special way that include the digest of the lib file under /lib(64)?
183 Dynamic_common_lib_apex *bool
184
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100185 // Canonical name of this APEX bundle. Used to determine the path to the
186 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
187 // apex mutator variations. For override_apex modules, this is the name of the
188 // overridden base module.
189 ApexVariationName string `blueprint:"mutated"`
190
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900191 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900192
193 // List of sanitizer names that this APEX is enabled for
194 SanitizerNames []string `blueprint:"mutated"`
195
196 PreventInstall bool `blueprint:"mutated"`
197
198 HideFromMake bool `blueprint:"mutated"`
199
Sam Delmericoca816532023-06-02 14:09:50 -0400200 // Name that dependencies can specify in their apex_available properties to refer to this module.
Sam Delmericoc3df1132023-06-06 12:14:23 -0400201 // If not specified, this defaults to Soong module name. This must be the name of a Soong module.
Sam Delmericoca816532023-06-02 14:09:50 -0400202 Apex_available_name *string
Sam Delmerico6d65a0f2023-06-05 15:55:57 -0400203
204 // Variant version of the mainline module. Must be an integer between 0-9
205 Variant_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900206}
207
208type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900209 // List of native libraries that are embedded inside this APEX.
Cole Faustac92f3e2024-08-20 13:26:52 -0700210 Native_shared_libs proptools.Configurable[[]string]
211
212 // List of JNI libraries that are embedded inside this APEX.
Jihoon Kang371a0372024-10-01 16:44:41 +0000213 Jni_libs proptools.Configurable[[]string]
Cole Faustac92f3e2024-08-20 13:26:52 -0700214
215 // List of rust dyn libraries that are embedded inside this APEX.
216 Rust_dyn_libs []string
217
218 // List of native executables that are embedded inside this APEX.
219 Binaries proptools.Configurable[[]string]
220
221 // List of native tests that are embedded inside this APEX.
222 Tests []string
223
224 // List of filesystem images that are embedded inside this APEX bundle.
225 Filesystems []string
226
227 // List of prebuilt_etcs that are embedded inside this APEX bundle.
228 Prebuilts proptools.Configurable[[]string]
229
230 // List of native libraries to exclude from this APEX.
231 Exclude_native_shared_libs []string
232
233 // List of JNI libraries to exclude from this APEX.
234 Exclude_jni_libs []string
235
236 // List of rust dyn libraries to exclude from this APEX.
237 Exclude_rust_dyn_libs []string
238
239 // List of native executables to exclude from this APEX.
240 Exclude_binaries []string
241
242 // List of native tests to exclude from this APEX.
243 Exclude_tests []string
244
245 // List of filesystem images to exclude from this APEX bundle.
246 Exclude_filesystems []string
247
248 // List of prebuilt_etcs to exclude from this APEX bundle.
249 Exclude_prebuilts []string
250}
251
252type ResolvedApexNativeDependencies struct {
253 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900254 Native_shared_libs []string
255
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900256 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900257 Jni_libs []string
258
Colin Cross70572ed2022-11-02 13:14:20 -0700259 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900260 Rust_dyn_libs []string
261
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900262 // List of native executables that are embedded inside this APEX.
Cole Faustac92f3e2024-08-20 13:26:52 -0700263 Binaries []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900264
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900265 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900266 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900267
268 // List of filesystem images that are embedded inside this APEX bundle.
269 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700270
Alice Wang4fab2dc2023-10-20 12:05:08 +0000271 // List of prebuilt_etcs that are embedded inside this APEX bundle.
Cole Faustac92f3e2024-08-20 13:26:52 -0700272 Prebuilts []string
Alice Wang4fab2dc2023-10-20 12:05:08 +0000273
Colin Cross70572ed2022-11-02 13:14:20 -0700274 // List of native libraries to exclude from this APEX.
275 Exclude_native_shared_libs []string
276
277 // List of JNI libraries to exclude from this APEX.
278 Exclude_jni_libs []string
279
280 // List of rust dyn libraries to exclude from this APEX.
281 Exclude_rust_dyn_libs []string
282
283 // List of native executables to exclude from this APEX.
284 Exclude_binaries []string
285
286 // List of native tests to exclude from this APEX.
287 Exclude_tests []string
288
289 // List of filesystem images to exclude from this APEX bundle.
290 Exclude_filesystems []string
Alice Wang4fab2dc2023-10-20 12:05:08 +0000291
292 // List of prebuilt_etcs to exclude from this APEX bundle.
293 Exclude_prebuilts []string
Colin Cross70572ed2022-11-02 13:14:20 -0700294}
295
296// Merge combines another ApexNativeDependencies into this one
Colin Crossb2388e32024-10-07 15:05:23 -0700297func (a *ResolvedApexNativeDependencies) Merge(ctx android.BaseModuleContext, b ApexNativeDependencies) {
Cole Faustac92f3e2024-08-20 13:26:52 -0700298 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs.GetOrDefault(ctx, nil)...)
Jihoon Kang371a0372024-10-01 16:44:41 +0000299 a.Jni_libs = append(a.Jni_libs, b.Jni_libs.GetOrDefault(ctx, nil)...)
Colin Cross70572ed2022-11-02 13:14:20 -0700300 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
Cole Faustac92f3e2024-08-20 13:26:52 -0700301 a.Binaries = append(a.Binaries, b.Binaries.GetOrDefault(ctx, nil)...)
Colin Cross70572ed2022-11-02 13:14:20 -0700302 a.Tests = append(a.Tests, b.Tests...)
303 a.Filesystems = append(a.Filesystems, b.Filesystems...)
Cole Faustac92f3e2024-08-20 13:26:52 -0700304 a.Prebuilts = append(a.Prebuilts, b.Prebuilts.GetOrDefault(ctx, nil)...)
Colin Cross70572ed2022-11-02 13:14:20 -0700305
306 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
307 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
308 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
309 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
310 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
311 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Alice Wang4fab2dc2023-10-20 12:05:08 +0000312 a.Exclude_prebuilts = append(a.Exclude_prebuilts, b.Exclude_prebuilts...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900313}
314
315type apexMultilibProperties struct {
316 // Native dependencies whose compile_multilib is "first"
317 First ApexNativeDependencies
318
319 // Native dependencies whose compile_multilib is "both"
320 Both ApexNativeDependencies
321
322 // Native dependencies whose compile_multilib is "prefer32"
323 Prefer32 ApexNativeDependencies
324
325 // Native dependencies whose compile_multilib is "32"
326 Lib32 ApexNativeDependencies
327
328 // Native dependencies whose compile_multilib is "64"
329 Lib64 ApexNativeDependencies
330}
331
332type apexTargetBundleProperties struct {
333 Target struct {
334 // Multilib properties only for android.
335 Android struct {
336 Multilib apexMultilibProperties
337 }
338
339 // Multilib properties only for host.
340 Host struct {
341 Multilib apexMultilibProperties
342 }
343
344 // Multilib properties only for host linux_bionic.
345 Linux_bionic struct {
346 Multilib apexMultilibProperties
347 }
348
349 // Multilib properties only for host linux_glibc.
350 Linux_glibc struct {
351 Multilib apexMultilibProperties
352 }
353 }
354}
355
Jiyong Park59140302020-12-14 18:44:04 +0900356type apexArchBundleProperties struct {
357 Arch struct {
358 Arm struct {
359 ApexNativeDependencies
360 }
361 Arm64 struct {
362 ApexNativeDependencies
363 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700364 Riscv64 struct {
365 ApexNativeDependencies
366 }
Jiyong Park59140302020-12-14 18:44:04 +0900367 X86 struct {
368 ApexNativeDependencies
369 }
370 X86_64 struct {
371 ApexNativeDependencies
372 }
373 }
374}
375
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900376// These properties can be used in override_apex to override the corresponding properties in the
377// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900378type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900379 // List of APKs that are embedded inside this APEX.
Inseob Kimd23e0d32024-07-23 16:12:33 +0900380 Apps proptools.Configurable[[]string]
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900381
Daniel Norman5a3ce132021-08-26 15:44:43 -0700382 // List of prebuilt files that are embedded inside this APEX bundle.
Inseob Kimd23e0d32024-07-23 16:12:33 +0900383 Prebuilts proptools.Configurable[[]string]
Daniel Norman5a3ce132021-08-26 15:44:43 -0700384
markchien7c803b82021-08-26 22:10:06 +0800385 // List of BPF programs inside this APEX bundle.
386 Bpfs []string
387
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900388 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
389 // Soong). This does not completely prevent installation of the overridden binaries, but if
390 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
391 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900392 Overrides []string
393
Jesse Melhuishec60e252024-03-29 19:08:20 +0000394 Multilib apexMultilibProperties
395
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900396 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900397 Logging_parent string
398
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900399 // Apex Container package name. Override value for attribute package:name in
400 // AndroidManifest.xml
Cole Faust12f6ec92024-10-03 13:32:43 -0700401 Package_name proptools.Configurable[string]
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900402
403 // A txt file containing list of files that are allowed to be included in this APEX.
404 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700405
406 // Name of the apex_key module that provides the private key to sign this APEX bundle.
407 Key *string
408
409 // Specifies the certificate and the private key to sign the zip container of this APEX. If
410 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
411 // as the certificate and the private key, respectively. If this is ":module", then the
412 // certificate and the private key are provided from the android_app_certificate module
413 // named "module".
414 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400415
416 // Whether this APEX can be compressed or not. Setting this property to false means this
417 // APEX will never be compressed. When set to true, APEX will be compressed if other
418 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
419 // Default: false.
420 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000421
422 // Trim against a specific Dynamic Common Lib APEX
423 Trim_against *string
Spandan Das50801e22024-05-13 18:29:45 +0000424
425 // The minimum SDK version that this APEX must support at minimum. This is usually set to
426 // the SDK version that the APEX was first introduced.
427 Min_sdk_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900428}
429
430type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900431 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900432 android.ModuleBase
433 android.DefaultableModuleBase
434 android.OverridableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900435
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900436 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900437 properties apexBundleProperties
438 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900439 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900440 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900441 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900442
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900443 ///////////////////////////////////////////////////////////////////////////////////////////
444 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900445
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +0100446 // Keys for apex_payload.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800447 publicKeyFile android.Path
448 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900449
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900450 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800451 containerCertificateFile android.Path
452 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900453
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900454 // Flags for special variants of APEX
455 testApex bool
456 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900457
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900458 // File system type of apex_payload.img
459 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900460
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900461 // Whether to create symlink to the system file instead of having a file inside the apex or
462 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900463 linkToSystemLib bool
464
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900465 // List of files to be included in this APEX. This is filled in the first part of
466 // GenerateAndroidBuildActions.
467 filesInfo []apexFile
468
Colin Crossb614cd42024-10-11 12:52:21 -0700469 // List of files that were excluded by the unwanted_transitive_deps property.
470 unwantedTransitiveFilesInfo []apexFile
471
472 // List of files that were excluded due to conflicts with other variants of the same module.
473 duplicateTransitiveFilesInfo []apexFile
474
Jingwen Chen29743c82023-01-25 17:49:46 +0000475 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
476 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900477
478 ///////////////////////////////////////////////////////////////////////////////////////////
479 // Outputs (final and intermediates)
480
481 // Processed apex manifest in JSONson format (for Q)
482 manifestJsonOut android.WritablePath
483
484 // Processed apex manifest in PB format (for R+)
485 manifestPbOut android.WritablePath
486
487 // Processed file_contexts files
488 fileContexts android.WritablePath
489
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900490 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900491 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900492 outputFile android.WritablePath
493
Jooyung Hana6d36672022-02-24 13:58:07 +0900494 // The built uncompressed .apex file.
495 outputApexFile android.WritablePath
496
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900497 // The built APEX file in app bundle format. This file is not directly installed to the
498 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
499 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
500 // system) to be merged into a single app bundle file that Play accepts. See
501 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
502 bundleModuleFile android.WritablePath
503
Colin Cross6340ea52021-11-04 12:01:18 -0700504 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900505 installDir android.InstallPath
506
Colin Cross6340ea52021-11-04 12:01:18 -0700507 // Path where this APEX was installed.
508 installedFile android.InstallPath
509
Jooyung Han286957d2023-10-30 16:17:56 +0900510 // fragment for this apex for apexkeys.txt
511 apexKeysPath android.WritablePath
512
Colin Cross6340ea52021-11-04 12:01:18 -0700513 // Installed locations of symlinks for backward compatibility.
514 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900515
516 // Text file having the list of individual files that are included in this APEX. Used for
517 // debugging purpose.
518 installedFilesFile android.WritablePath
519
520 // List of module names that this APEX is including (to be shown via *-deps-info target).
521 // Used for debugging purpose.
522 android.ApexBundleDepsInfo
523
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900524 // Optional list of lint report zip files for apexes that contain java or app modules
525 lintReports android.Paths
526
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000527 isCompressed bool
528
sophiezc80a2b32020-11-12 16:39:19 +0000529 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700530 nativeApisUsedByModuleFile android.ModuleOutPath
531 nativeApisBackedByModuleFile android.ModuleOutPath
532 javaApisUsedByModuleFile android.ModuleOutPath
Yu Liueae7b362023-11-16 17:05:47 -0800533
534 aconfigFiles []android.Path
Cole Faust43ddd082024-06-17 12:32:40 -0700535
536 // Required modules, filled out during GenerateAndroidBuildActions and used in AndroidMk
537 required []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900538}
539
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900540// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900541type apexFileClass int
542
Jooyung Han72bd2f82019-10-23 16:46:38 +0900543const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900544 app apexFileClass = iota
545 appSet
546 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900547 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900548 nativeExecutable
549 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900550 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900551 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900552)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900553
Jingwen Chen2d37b642023-03-14 16:11:38 +0000554var (
555 classes = map[string]apexFileClass{
556 "app": app,
557 "appSet": appSet,
558 "etc": etc,
Jingwen Chen2d37b642023-03-14 16:11:38 +0000559 "javaSharedLib": javaSharedLib,
560 "nativeExecutable": nativeExecutable,
561 "nativeSharedLib": nativeSharedLib,
562 "nativeTest": nativeTest,
Jingwen Chen2d37b642023-03-14 16:11:38 +0000563 "shBinary": shBinary,
564 }
565)
566
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900567// apexFile represents a file in an APEX bundle. This is created during the first half of
568// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
569// of the function, this is used to create commands that copies the files into a staging directory,
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900570// where they are packaged into the APEX file.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900571type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900572 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000573 builtFile android.Path
574 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900575 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000576 customStem string
577 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900578
Colin Crossa6182ab2024-08-21 10:47:44 -0700579 checkbuildTarget android.Path
580
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900581 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
582 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
583 // suffix>]
584 androidMkModuleName string // becomes LOCAL_MODULE
585 class apexFileClass // becomes LOCAL_MODULE_CLASS
586 moduleDir string // becomes LOCAL_PATH
587 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
588 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
589 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
590 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900591
592 jacocoReportClassesFile android.Path // only for javalibs and apps
Colin Crossb79aa8f2024-09-25 15:41:01 -0700593 lintInfo *java.LintInfo // only for javalibs and apps
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900594 certificate java.Certificate // only for apps
595 overriddenPackageName string // only for apps
596
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900597 transitiveDep bool
598 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900599
Jiyong Park57621b22021-01-20 20:33:11 +0900600 multilib string
601
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900602 // TODO(jiyong): remove this
603 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900604}
605
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900606// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900607func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
608 ret := apexFile{
609 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900610 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900611 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900612 class: class,
613 module: module,
614 }
615 if module != nil {
Colin Crossa6182ab2024-08-21 10:47:44 -0700616 if installFilesInfo, ok := android.OtherModuleProvider(ctx, module, android.InstallFilesProvider); ok {
617 ret.checkbuildTarget = installFilesInfo.CheckbuildTarget
618 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900619 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900620 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Cole Faust43ddd082024-06-17 12:32:40 -0700621 ret.requiredModuleNames = module.RequiredModuleNames(ctx)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900622 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
623 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900624 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900625 }
626 return ret
627}
628
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900629func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900630 return af.builtFile != nil && af.builtFile.String() != ""
631}
632
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900633// apexRelativePath returns the relative path of the given path from the install directory of this
634// apexFile.
635// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900636func (af *apexFile) apexRelativePath(path string) string {
637 return filepath.Join(af.installDir, path)
638}
639
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900640// path returns path of this apex file relative to the APEX root
641func (af *apexFile) path() string {
642 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900643}
644
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900645// stem returns the base filename of this apex file
646func (af *apexFile) stem() string {
647 if af.customStem != "" {
648 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900649 }
650 return af.builtFile.Base()
651}
652
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900653// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
654func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900655 var ret []string
656 for _, symlink := range af.symlinks {
657 ret = append(ret, af.apexRelativePath(symlink))
658 }
659 return ret
660}
661
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900662// availableToPlatform tests whether this apexFile is from a module that can be installed to the
663// platform.
664func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900665 if af.module == nil {
666 return false
667 }
668 if am, ok := af.module.(android.ApexModule); ok {
669 return am.AvailableFor(android.AvailableToPlatform)
670 }
671 return false
672}
673
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900674////////////////////////////////////////////////////////////////////////////////////////////////////
675// Mutators
676//
677// Brief description about mutators for APEX. The following three mutators are the most important
678// ones.
679//
680// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
681// to the (direct) dependencies of this APEX bundle.
682//
Paul Duffin949abc02020-12-08 10:34:30 +0000683// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900684// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
685// modules are marked as being included in the APEX via BuildForApex().
686//
Paul Duffin949abc02020-12-08 10:34:30 +0000687// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
688// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900689
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900690type dependencyTag struct {
691 blueprint.BaseDependencyTag
692 name string
693
694 // Determines if the dependent will be part of the APEX payload. Can be false for the
695 // dependencies to the signing key module, etc.
696 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000697
698 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
699 // replacement. This is needed because some prebuilt modules do not provide all the information
700 // needed by the apex.
701 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000702
703 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
704 // also be added as exported members of that SDK.
705 memberType android.SdkMemberType
Spandan Das746161d2024-08-21 22:47:53 +0000706
707 installable bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000708}
709
710func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
711 return d.memberType
712}
713
714func (d *dependencyTag) ExportMember() bool {
715 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900716}
717
Paul Duffin520917a2022-05-13 13:01:59 +0000718func (d *dependencyTag) String() string {
719 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
720}
721
722func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000723 return !d.sourceOnly
724}
725
Spandan Das746161d2024-08-21 22:47:53 +0000726func (d *dependencyTag) InstallDepNeeded() bool {
727 return d.installable
728}
729
Paul Duffin8c535da2021-03-17 14:51:03 +0000730var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000731var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000732
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900733var (
Spandan Das746161d2024-08-21 22:47:53 +0000734 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
735 bpfTag = &dependencyTag{name: "bpf", payload: true}
736 certificateTag = &dependencyTag{name: "certificate"}
737 dclaTag = &dependencyTag{name: "dcla"}
738 executableTag = &dependencyTag{name: "executable", payload: true}
739 fsTag = &dependencyTag{name: "filesystem", payload: true}
740 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
741 // The dexpreopt artifacts of apex system server jars are installed onto system image.
742 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType, installable: true}
Paul Duffinfcf79852022-07-20 14:18:24 +0000743 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000744 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
745 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
746 keyTag = &dependencyTag{name: "key"}
747 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
748 rroTag = &dependencyTag{name: "rro", payload: true}
749 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
750 testForTag = &dependencyTag{name: "test for"}
751 testTag = &dependencyTag{name: "test", payload: true}
752 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900753)
754
755// TODO(jiyong): shorten this function signature
Cole Faustac92f3e2024-08-20 13:26:52 -0700756func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ResolvedApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900757 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900758 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Ivan Lozano0a468a42024-05-13 21:03:34 -0400759 rustLibVariations := append(
760 target.Variations(), []blueprint.Variation{
761 {Mutator: "rust_libraries", Variation: "dylib"},
Ivan Lozano0a468a42024-05-13 21:03:34 -0400762 }...,
763 )
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900764
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900765 // Append "image" variation
766 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
767 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
768 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900769
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900770 // Use *FarVariation* to be able to depend on modules having conflicting variations with
771 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
772 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700773 ctx.AddFarVariationDependencies(binVariations, executableTag,
Cole Faustac92f3e2024-08-20 13:26:52 -0700774 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
Colin Cross70572ed2022-11-02 13:14:20 -0700775 ctx.AddFarVariationDependencies(binVariations, testTag,
776 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
777 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
778 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
779 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
780 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
781 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
782 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
783 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
784 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Alice Wang4fab2dc2023-10-20 12:05:08 +0000785 ctx.AddFarVariationDependencies(target.Variations(), prebuiltTag,
Cole Faustac92f3e2024-08-20 13:26:52 -0700786 android.RemoveListFromList(nativeModules.Prebuilts, nativeModules.Exclude_prebuilts)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900787}
788
789func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900790 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900791}
792
Jooyung Hand045ebc2022-12-06 15:23:57 +0900793// getImageVariationPair returns a pair for the image variation name as its
794// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900795// suffix indicates the vndk version for vendor/product if vndk is enabled.
Jooyung Hand045ebc2022-12-06 15:23:57 +0900796// getImageVariation can simply join the result of this function to get the
797// image variation name.
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900798func (a *apexBundle) getImageVariationPair() (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900799 if a.vndkApex {
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900800 return cc.VendorVariationPrefix, a.vndkVersion()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900801 }
802
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900803 prefix := android.CoreVariation
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900804 if a.SocSpecific() || a.DeviceSpecific() {
Jihoon Kang47e91842024-06-19 00:51:16 +0000805 prefix = android.VendorVariation
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900806 } else if a.ProductSpecific() {
Jihoon Kang47e91842024-06-19 00:51:16 +0000807 prefix = android.ProductVariation
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900808 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900809
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900810 return prefix, ""
Jooyung Hand045ebc2022-12-06 15:23:57 +0900811}
812
813// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
814// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900815func (a *apexBundle) getImageVariation() string {
816 prefix, vndkVersion := a.getImageVariationPair()
Jooyung Hand045ebc2022-12-06 15:23:57 +0900817 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900818}
819
820func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900821 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
822 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
823 // each target os/architectures, appropriate dependencies are selected by their
824 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900825 targets := ctx.MultiTargets()
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900826 imageVariation := a.getImageVariation()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900827
828 a.combineProperties(ctx)
829
830 has32BitTarget := false
831 for _, target := range targets {
832 if target.Arch.ArchType.Multilib == "lib32" {
833 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000834 }
835 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900836 for i, target := range targets {
Cole Faustac92f3e2024-08-20 13:26:52 -0700837 var deps ResolvedApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000838
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900839 // Add native modules targeting both ABIs. When multilib.* is omitted for
840 // native_shared_libs/jni_libs/tests, it implies multilib.both
Inseob Kimd23e0d32024-07-23 16:12:33 +0900841 deps.Merge(ctx, a.properties.Multilib.Both)
842 deps.Merge(ctx, ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900843 Native_shared_libs: a.properties.Native_shared_libs,
Colin Cross49f1a8f2024-10-23 13:04:15 -0700844 Rust_dyn_libs: a.properties.Rust_dyn_libs,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900845 Tests: a.properties.Tests,
846 Jni_libs: a.properties.Jni_libs,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900847 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900848
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900849 // Add native modules targeting the first ABI When multilib.* is omitted for
850 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900851 isPrimaryAbi := i == 0
852 if isPrimaryAbi {
Inseob Kimd23e0d32024-07-23 16:12:33 +0900853 deps.Merge(ctx, a.properties.Multilib.First)
854 deps.Merge(ctx, ApexNativeDependencies{
Cole Faustac92f3e2024-08-20 13:26:52 -0700855 Native_shared_libs: proptools.NewConfigurable[[]string](nil, nil),
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900856 Tests: nil,
Jihoon Kang371a0372024-10-01 16:44:41 +0000857 Jni_libs: proptools.NewConfigurable[[]string](nil, nil),
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900858 Binaries: a.properties.Binaries,
859 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900860 }
861
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900862 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900863 switch target.Arch.ArchType.Multilib {
864 case "lib32":
Inseob Kimd23e0d32024-07-23 16:12:33 +0900865 deps.Merge(ctx, a.properties.Multilib.Lib32)
866 deps.Merge(ctx, a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900867 case "lib64":
Inseob Kimd23e0d32024-07-23 16:12:33 +0900868 deps.Merge(ctx, a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900869 if !has32BitTarget {
Inseob Kimd23e0d32024-07-23 16:12:33 +0900870 deps.Merge(ctx, a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900871 }
872 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900873
Jiyong Park59140302020-12-14 18:44:04 +0900874 // Add native modules targeting a specific arch variant
875 switch target.Arch.ArchType {
876 case android.Arm:
Inseob Kimd23e0d32024-07-23 16:12:33 +0900877 deps.Merge(ctx, a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900878 case android.Arm64:
Inseob Kimd23e0d32024-07-23 16:12:33 +0900879 deps.Merge(ctx, a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700880 case android.Riscv64:
Inseob Kimd23e0d32024-07-23 16:12:33 +0900881 deps.Merge(ctx, a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900882 case android.X86:
Inseob Kimd23e0d32024-07-23 16:12:33 +0900883 deps.Merge(ctx, a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900884 case android.X86_64:
Inseob Kimd23e0d32024-07-23 16:12:33 +0900885 deps.Merge(ctx, a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900886 default:
887 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
888 }
889
Colin Cross70572ed2022-11-02 13:14:20 -0700890 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Riya Thakur654461c2024-02-27 07:21:05 +0000891 if isPrimaryAbi {
892 ctx.AddFarVariationDependencies([]blueprint.Variation{
893 {Mutator: "os", Variation: target.OsVariation()},
894 {Mutator: "arch", Variation: target.ArchVariation()},
895 }, shBinaryTag, a.properties.Sh_binaries...)
896 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900897 }
898
Cole Faustbf1d92a2024-07-29 12:24:25 -0700899 a.properties.ResolvedSystemserverclasspathFragments = a.properties.Systemserverclasspath_fragments.GetOrDefault(ctx, nil)
Inseob Kimb1142342024-07-23 13:39:54 +0900900
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900901 // Common-arch dependencies come next
902 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000903 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.properties.Rros...)
Anton Hanssone7545852023-02-24 11:06:07 +0000904 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.properties.Bootclasspath_fragments...)
Inseob Kimb1142342024-07-23 13:39:54 +0900905 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.properties.ResolvedSystemserverclasspathFragments...)
Anton Hanssone7545852023-02-24 11:06:07 +0000906 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
Jiyong Park12a719c2021-01-07 15:31:24 +0900907 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000908 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100909}
910
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900911// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900912func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
913 if a.overridableProperties.Allowed_files != nil {
914 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100915 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900916
917 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Cole Faustbf1d92a2024-07-29 12:24:25 -0700918 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps.GetOrDefault(ctx, nil)...)
markchien7c803b82021-08-26 22:10:06 +0800919 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Cole Faustbf1d92a2024-07-29 12:24:25 -0700920 if prebuilts := a.overridableProperties.Prebuilts.GetOrDefault(ctx, nil); len(prebuilts) > 0 {
Daniel Norman5a3ce132021-08-26 15:44:43 -0700921 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
922 // regardless of the TARGET_PREFER_* setting. See b/144532908
923 arches := ctx.DeviceConfig().Arches()
924 if len(arches) != 0 {
925 archForPrebuiltEtc := arches[0]
926 for _, arch := range arches {
927 // Prefer 64-bit arch if there is any
928 if arch.ArchType.Multilib == "lib64" {
929 archForPrebuiltEtc = arch
930 break
931 }
932 }
933 ctx.AddFarVariationDependencies([]blueprint.Variation{
934 {Mutator: "os", Variation: ctx.Os().String()},
935 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
936 }, prebuiltTag, prebuilts...)
937 }
938 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700939
940 // Dependencies for signing
941 if String(a.overridableProperties.Key) == "" {
942 ctx.PropertyErrorf("key", "missing")
943 return
944 }
945 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
946
947 cert := android.SrcIsModule(a.getCertString(ctx))
948 if cert != "" {
949 ctx.AddDependency(ctx.Module(), certificateTag, cert)
950 // empty cert is not an error. Cert and private keys will be directly found under
951 // PRODUCT_DEFAULT_DEV_CERTIFICATE
952 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100953}
954
Dennis Shene2ed70c2023-01-11 14:15:43 +0000955func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
956 if !mctx.Config().ApexTrimEnabled() {
957 return
958 }
959 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
960 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
961 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
962 } else if o, ok := mctx.Module().(*OverrideApex); ok {
963 for _, p := range o.GetProperties() {
964 properties, ok := p.(*overridableProperties)
965 if !ok {
966 continue
967 }
968 if properties.Trim_against != nil {
969 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
970 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
971 }
972 }
973 }
974}
975
976type DCLAInfo struct {
977 ProvidedLibs []string
978}
979
Colin Crossbc7d76c2023-12-12 16:39:03 -0800980var DCLAInfoProvider = blueprint.NewMutatorProvider[DCLAInfo]("apex_info")
Dennis Shene2ed70c2023-01-11 14:15:43 +0000981
Paul Duffina7d6a892020-12-07 17:39:59 +0000982var _ ApexInfoMutator = (*apexBundle)(nil)
983
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100984func (a *apexBundle) ApexVariationName() string {
985 return a.properties.ApexVariationName
986}
987
Paul Duffina7d6a892020-12-07 17:39:59 +0000988// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900989// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
990// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
991// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
992// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000993//
994// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
995// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
996// The apexMutator uses that list to create module variants for the apexes to which it belongs.
997// The relationship between module variants and apexes is not one-to-one as variants will be
998// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000999func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +09001000
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001001 // The VNDK APEX is special. For the APEX, the membership is described in a very different
1002 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
1003 // libraries are self-identified by their vndk.enabled properties. There is no need to run
1004 // this mutator for the APEX as nothing will be collected. So, let's return fast.
1005 if a.vndkApex {
1006 return
1007 }
1008
Colin Cross56a83212020-09-15 18:30:11 -07001009 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001010 am, ok := child.(android.ApexModule)
1011 if !ok || !am.CanHaveApexVariants() {
1012 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +09001013 }
Paul Duffin573989d2021-03-17 13:25:29 +00001014 depTag := mctx.OtherModuleDependencyTag(child)
1015
1016 // Check to see if the tag always requires that the child module has an apex variant for every
1017 // apex variant of the parent module. If it does not then it is still possible for something
1018 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
1019 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
1020 return true
1021 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001022 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001023 return false
1024 }
Kiyoung Kimcbe2ba02023-09-07 16:00:04 +09001025
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001026 // By default, all the transitive dependencies are collected, unless filtered out
1027 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001028 return true
1029 }
1030
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001031 // Records whether a certain module is included in this apexBundle via direct dependency or
1032 // inndirect dependency.
1033 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001034 mctx.WalkDeps(func(child, parent android.Module) bool {
1035 if !continueApexDepsWalk(child, parent) {
1036 return false
1037 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001038 // If the parent is apexBundle, this child is directly depended.
1039 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001040 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001041 contents[depName] = contents[depName].Add(directDep)
1042 return true
1043 })
1044
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001045 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001046 apexContents := android.NewApexContents(contents)
Spandan Dasf5e03f12024-01-25 19:25:42 +00001047 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{
Colin Cross56a83212020-09-15 18:30:11 -07001048 Contents: apexContents,
1049 })
1050
Jooyung Haned124c32021-01-26 11:43:46 +09001051 minSdkVersion := a.minSdkVersion(mctx)
1052 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1053 if minSdkVersion.IsNone() {
1054 minSdkVersion = android.FutureApiLevel
1055 }
1056
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001057 // This is the main part of this mutator. Mark the collected dependencies that they need to
1058 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001059
Jooyung Han63dff462023-02-09 00:11:27 +00001060 apexVariationName := mctx.ModuleName() // could be com.android.foo
Spandan Das50801e22024-05-13 18:29:45 +00001061 if overridable, ok := mctx.Module().(android.OverridableModule); ok && overridable.GetOverriddenBy() != "" {
1062 // use the overridden name com.mycompany.android.foo
1063 apexVariationName = overridable.GetOverriddenBy()
1064 }
1065
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001066 a.properties.ApexVariationName = apexVariationName
Spandan Dase8173a82023-04-12 17:14:11 +00001067 testApexes := []string{}
1068 if a.testApex {
1069 testApexes = []string{apexVariationName}
1070 }
Colin Cross56a83212020-09-15 18:30:11 -07001071 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001072 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001073 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001074 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001075 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001076 InApexVariants: []string{apexVariationName},
1077 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001078 ApexContents: []*android.ApexContents{apexContents},
Spandan Dase8173a82023-04-12 17:14:11 +00001079 TestApexes: testApexes,
Spandan Das33bbeb22024-06-18 23:28:25 +00001080 BaseApexName: mctx.ModuleName(),
Spandan Das003452f2024-09-06 00:56:25 +00001081 ApexAvailableName: proptools.String(a.properties.Apex_available_name),
Colin Cross56a83212020-09-15 18:30:11 -07001082 }
Colin Cross56a83212020-09-15 18:30:11 -07001083 mctx.WalkDeps(func(child, parent android.Module) bool {
1084 if !continueApexDepsWalk(child, parent) {
1085 return false
1086 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001087 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001088 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001089 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001090
1091 if a.dynamic_common_lib_apex() {
Colin Cross40213022023-12-13 15:19:49 -08001092 android.SetProvider(mctx, DCLAInfoProvider, DCLAInfo{
Cole Faustac92f3e2024-08-20 13:26:52 -07001093 ProvidedLibs: a.properties.Native_shared_libs.GetOrDefault(mctx, nil),
Dennis Shene2ed70c2023-01-11 14:15:43 +00001094 })
1095 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001096}
1097
Paul Duffina7d6a892020-12-07 17:39:59 +00001098type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001099 // ApexVariationName returns the name of the APEX variation to use in the apex
1100 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1101 ApexVariationName() string
1102
Paul Duffina7d6a892020-12-07 17:39:59 +00001103 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1104 // depended upon by an apex and which require an apex specific variant.
1105 ApexInfoMutator(android.TopDownMutatorContext)
1106}
1107
1108// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1109// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001110// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001111func apexInfoMutator(mctx android.TopDownMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001112 if !mctx.Module().Enabled(mctx) {
Paul Duffina7d6a892020-12-07 17:39:59 +00001113 return
1114 }
1115
1116 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1117 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001118 }
Colin Cross7c035062024-03-28 12:18:42 -07001119
1120 if am, ok := mctx.Module().(android.ApexModule); ok {
1121 android.ApexInfoMutator(mctx, am)
1122 }
Spandan Das42e89502022-05-06 22:12:55 +00001123}
1124
Spandan Das08c911f2022-01-21 22:07:26 +00001125// TODO: b/215736885 Whittle the denylist
1126// Transitive deps of certain mainline modules baseline NewApi errors
1127// Skip these mainline modules for now
1128var (
1129 skipStrictUpdatabilityLintAllowlist = []string{
Cole Fauste17f93a2024-01-18 11:25:59 -08001130 // go/keep-sorted start
1131 "PackageManagerTestApex",
1132 "com.android.adservices",
1133 "com.android.appsearch",
Spandan Das08c911f2022-01-21 22:07:26 +00001134 "com.android.art",
1135 "com.android.art.debug",
Cole Fauste17f93a2024-01-18 11:25:59 -08001136 "com.android.btservices",
1137 "com.android.cellbroadcast",
1138 "com.android.configinfrastructure",
Spandan Das08c911f2022-01-21 22:07:26 +00001139 "com.android.conscrypt",
Cole Fauste17f93a2024-01-18 11:25:59 -08001140 "com.android.extservices",
1141 "com.android.extservices_tplus",
1142 "com.android.healthfitness",
1143 "com.android.ipsec",
Spandan Das08c911f2022-01-21 22:07:26 +00001144 "com.android.media",
Cole Fauste17f93a2024-01-18 11:25:59 -08001145 "com.android.mediaprovider",
1146 "com.android.ondevicepersonalization",
1147 "com.android.os.statsd",
1148 "com.android.permission",
Yisroel Forta154796c2024-02-13 00:16:36 +00001149 "com.android.profiling",
Cole Fauste17f93a2024-01-18 11:25:59 -08001150 "com.android.rkpd",
1151 "com.android.scheduling",
1152 "com.android.tethering",
1153 "com.android.uwb",
1154 "com.android.wifi",
Spandan Das08c911f2022-01-21 22:07:26 +00001155 "test_com.android.art",
Cole Fauste17f93a2024-01-18 11:25:59 -08001156 "test_com.android.cellbroadcast",
Spandan Das08c911f2022-01-21 22:07:26 +00001157 "test_com.android.conscrypt",
Cole Fauste17f93a2024-01-18 11:25:59 -08001158 "test_com.android.extservices",
1159 "test_com.android.ipsec",
Spandan Das08c911f2022-01-21 22:07:26 +00001160 "test_com.android.media",
Cole Fauste17f93a2024-01-18 11:25:59 -08001161 "test_com.android.mediaprovider",
1162 "test_com.android.os.statsd",
1163 "test_com.android.permission",
1164 "test_com.android.wifi",
Dmitrii Ishcheikinf8821072024-06-05 15:20:48 +00001165 "test_imgdiag_com.android.art",
Spandan Das08c911f2022-01-21 22:07:26 +00001166 "test_jitzygote_com.android.art",
Cole Fauste17f93a2024-01-18 11:25:59 -08001167 // go/keep-sorted end
Spandan Das08c911f2022-01-21 22:07:26 +00001168 }
1169)
1170
Colin Cross87427352024-09-25 15:41:19 -07001171func (a *apexBundle) checkStrictUpdatabilityLinting(mctx android.ModuleContext) bool {
Spandan Das50801e22024-05-13 18:29:45 +00001172 // The allowlist contains the base apex name, so use that instead of the ApexVariationName
1173 return a.Updatable() && !android.InList(mctx.ModuleName(), skipStrictUpdatabilityLintAllowlist)
Spandan Das08c911f2022-01-21 22:07:26 +00001174}
1175
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001176// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1177// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1178// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001179func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001180 if !mctx.Module().Enabled(mctx) {
Colin Crossaede88c2020-08-11 12:17:01 -07001181 return
1182 }
1183 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001184 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1185 }
1186}
1187
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001188// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1189// the apex in order to retrieve its contents later.
1190// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001191func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001192 if !mctx.Module().Enabled(mctx) {
Colin Cross56a83212020-09-15 18:30:11 -07001193 return
1194 }
Colin Cross56a83212020-09-15 18:30:11 -07001195 if am, ok := mctx.Module().(android.ApexModule); ok {
1196 if testFor := am.TestFor(); len(testFor) > 0 {
1197 mctx.AddFarVariationDependencies([]blueprint.Variation{
1198 {Mutator: "os", Variation: am.Target().OsVariation()},
1199 {"arch", "common"},
1200 }, testForTag, testFor...)
1201 }
1202 }
1203}
1204
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001205// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001206func apexTestForMutator(mctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001207 if !mctx.Module().Enabled(mctx) {
Colin Cross56a83212020-09-15 18:30:11 -07001208 return
1209 }
Colin Cross56a83212020-09-15 18:30:11 -07001210 if _, ok := mctx.Module().(android.ApexModule); ok {
1211 var contents []*android.ApexContents
1212 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
Spandan Dasf5e03f12024-01-25 19:25:42 +00001213 abInfo, _ := android.OtherModuleProvider(mctx, testFor, android.ApexBundleInfoProvider)
Colin Cross56a83212020-09-15 18:30:11 -07001214 contents = append(contents, abInfo.Contents)
1215 }
Colin Cross40213022023-12-13 15:19:49 -08001216 android.SetProvider(mctx, android.ApexTestForInfoProvider, android.ApexTestForInfo{
Colin Cross56a83212020-09-15 18:30:11 -07001217 ApexContents: contents,
1218 })
Colin Crossaede88c2020-08-11 12:17:01 -07001219 }
1220}
1221
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001222// markPlatformAvailability marks whether or not a module can be available to platform. A module
1223// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1224// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1225// be) available to platform
1226// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001227func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
Jooyung Han8d4a1f02023-08-23 13:54:08 +09001228 // Recovery is not considered as platform
1229 if mctx.Module().InstallInRecovery() {
Jiyong Park89e850a2020-04-07 16:37:39 +09001230 return
1231 }
1232
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001233 am, ok := mctx.Module().(android.ApexModule)
1234 if !ok {
1235 return
1236 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001237
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001238 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001239
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001240 // If any of the dep is not available to platform, this module is also considered as being
1241 // not available to platform even if it has "//apex_available:platform"
1242 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001243 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001244 // if the dependency crosses apex boundary, don't consider it
1245 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001246 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001247 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1248 availableToPlatform = false
1249 // TODO(b/154889534) trigger an error when 'am' has
1250 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001251 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001252 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001253
Paul Duffinb5769c12021-05-12 16:16:51 +01001254 // Exception 1: check to see if the module always requires it.
1255 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001256 availableToPlatform = true
1257 }
1258
1259 // Exception 2: bootstrap bionic libraries are also always available to platform
1260 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1261 availableToPlatform = true
1262 }
1263
1264 if !availableToPlatform {
1265 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001266 }
1267}
1268
Colin Cross7c035062024-03-28 12:18:42 -07001269type apexTransitionMutator struct{}
Colin Cross56a83212020-09-15 18:30:11 -07001270
Colin Cross7c035062024-03-28 12:18:42 -07001271func (a *apexTransitionMutator) Split(ctx android.BaseModuleContext) []string {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001272 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Colin Cross7c035062024-03-28 12:18:42 -07001273 if ai, ok := ctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
Spandan Das50801e22024-05-13 18:29:45 +00001274 if overridable, ok := ctx.Module().(android.OverridableModule); ok && overridable.GetOverriddenBy() != "" {
1275 return []string{overridable.GetOverriddenBy()}
Jiyong Park5d790c32019-11-15 18:40:32 +09001276 }
Spandan Das50801e22024-05-13 18:29:45 +00001277 return []string{ai.ApexVariationName()}
1278 } else if _, ok := ctx.Module().(*OverrideApex); ok {
1279 return []string{ctx.ModuleName()}
Colin Cross7c035062024-03-28 12:18:42 -07001280 }
1281 return []string{""}
1282}
1283
1284func (a *apexTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1285 return sourceVariation
1286}
1287
1288func (a *apexTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1289 if am, ok := ctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
1290 return android.IncomingApexTransition(ctx, incomingVariation)
1291 } else if ai, ok := ctx.Module().(ApexInfoMutator); ok {
Spandan Das50801e22024-05-13 18:29:45 +00001292 if overridable, ok := ctx.Module().(android.OverridableModule); ok && overridable.GetOverriddenBy() != "" {
1293 return overridable.GetOverriddenBy()
1294 }
Colin Cross7c035062024-03-28 12:18:42 -07001295 return ai.ApexVariationName()
Spandan Das50801e22024-05-13 18:29:45 +00001296 } else if _, ok := ctx.Module().(*OverrideApex); ok {
1297 return ctx.Module().Name()
Colin Cross7c035062024-03-28 12:18:42 -07001298 }
1299
1300 return ""
1301}
1302
1303func (a *apexTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
1304 if am, ok := ctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
1305 android.MutateApexTransition(ctx, variation)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001306 }
1307}
Sundong Ahne9b55722019-09-06 17:37:42 +09001308
Paul Duffin6717d882021-06-15 19:09:41 +01001309// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1310// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001311func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001312 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001313 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001314 return !a.vndkApex
1315 }
1316
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001317 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001318}
1319
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001320// See android.UpdateDirectlyInAnyApex
1321// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001322func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
Colin Cross56a83212020-09-15 18:30:11 -07001323 if am, ok := mctx.Module().(android.ApexModule); ok {
1324 android.UpdateDirectlyInAnyApex(mctx, am)
1325 }
1326}
1327
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001328const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001329 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001330 imageApexSuffix = ".apex"
1331 imageCapexSuffix = ".capex"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001332
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001333 // variant names each of which is for a packaging method
Jooyung Haneec1b3f2023-06-20 16:25:59 +09001334 imageApexType = "image"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001335
Dan Willemsen47e1a752021-10-16 18:36:13 -07001336 ext4FsType = "ext4"
1337 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001338 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001339)
1340
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001341var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001342
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001343// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001344func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001345 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001346 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001347 return true
1348}
1349
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001350func (a *apexBundle) Exportable() bool {
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001351 return true
1352}
1353
1354func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1355 ret := make(map[string]android.Paths)
1356 ret["apex"] = android.Paths{a.outputFile}
1357 return ret
1358}
1359
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001360var _ cc.Coverage = (*apexBundle)(nil)
1361
1362// Implements cc.Coverage
Colin Crosse1a85552024-06-14 12:17:37 -07001363func (a *apexBundle) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Han8d4a1f02023-08-23 13:54:08 +09001364 return ctx.DeviceConfig().NativeCoverageEnabled()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001365}
1366
1367// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001368func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001369 a.properties.PreventInstall = true
1370}
1371
1372// Implements cc.Coverage
1373func (a *apexBundle) HideFromMake() {
1374 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001375 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1376 // TODO(ccross): untangle these
1377 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001378}
1379
1380// Implements cc.Coverage
1381func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1382 a.properties.IsCoverageVariant = coverage
1383}
1384
1385// Implements cc.Coverage
1386func (a *apexBundle) EnableCoverageIfNeeded() {}
1387
1388var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1389
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001390// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001391func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001392 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001393}
1394
Jiyong Parkf4020582021-11-29 12:37:10 +09001395func (a *apexBundle) FutureUpdatable() bool {
1396 return proptools.BoolDefault(a.properties.Future_updatable, false)
1397}
1398
Jiyong Park1bc84122021-06-22 20:23:05 +09001399func (a *apexBundle) UsePlatformApis() bool {
1400 return proptools.BoolDefault(a.properties.Platform_apis, false)
1401}
1402
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001403// getCertString returns the name of the cert that should be used to sign this APEX. This is
1404// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001405func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001406 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001407 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1408 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1409 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001410 if a.vndkApex {
1411 moduleName = vndkApexName
1412 }
1413 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001414 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001415 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001416 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001417 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001418}
1419
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001420// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001421func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001422 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001423}
1424
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001425// See the generate_hashtree property
1426func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001427 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001428}
1429
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001430// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001431func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1432 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1433}
1434
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001435// See the test_only_force_compression property
1436func (a *apexBundle) testOnlyShouldForceCompression() bool {
1437 return proptools.Bool(a.properties.Test_only_force_compression)
1438}
1439
Dennis Shenaf41bc12022-08-03 16:46:43 +00001440// See the dynamic_common_lib_apex property
1441func (a *apexBundle) dynamic_common_lib_apex() bool {
1442 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1443}
1444
Dennis Shene2ed70c2023-01-11 14:15:43 +00001445// See the list of libs to trim
1446func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1447 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1448 if len(dclaModules) > 1 {
1449 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1450 }
1451 if len(dclaModules) > 0 {
Colin Cross313aa542023-12-13 13:47:44 -08001452 DCLAInfo, _ := android.OtherModuleProvider(ctx, dclaModules[0], DCLAInfoProvider)
Dennis Shene2ed70c2023-01-11 14:15:43 +00001453 return DCLAInfo.ProvidedLibs
1454 }
1455 return []string{}
1456}
1457
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001458// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1459// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1460// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001461
Jiyong Parkf97782b2019-02-13 20:28:58 +09001462func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1463 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1464 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1465 }
1466}
1467
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001468func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001469 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1470 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001471 }
1472
1473 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001474 var globalSanitizerNames []string
Jooyung Han8d4a1f02023-08-23 13:54:08 +09001475 arches := config.SanitizeDeviceArch()
1476 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
1477 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001478 }
1479 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001480}
1481
Jooyung Han8ce8db92020-05-15 19:05:05 +09001482func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001483 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1484 // Keep only the mechanism here.
Jooyung Han8d4a1f02023-08-23 13:54:08 +09001485 if sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Kiyoung Kim4e765b12024-04-04 17:33:42 +09001486 imageVariation := a.getImageVariation()
Jooyung Han8ce8db92020-05-15 19:05:05 +09001487 for _, target := range ctx.MultiTargets() {
1488 if target.Arch.ArchType.Multilib == "lib64" {
Cole Faustac92f3e2024-08-20 13:26:52 -07001489 addDependenciesForNativeModules(ctx, ResolvedApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001490 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001491 Tests: nil,
1492 Jni_libs: nil,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001493 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001494 break
1495 }
1496 }
1497 }
1498}
1499
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001500// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1501// returned apexFile saves information about the Soong module that will be used for creating the
1502// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001503func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001504 // Decide the APEX-local directory by the multilib of the library In the future, we may
1505 // query this to the module.
1506 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001507 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001508 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001509 case "lib32":
1510 dirInApex = "lib"
1511 case "lib64":
1512 dirInApex = "lib64"
1513 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001514 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001515 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001516 }
Jiyong Park1833cef2019-12-13 13:28:36 +09001517 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001518 // Special case for Bionic libs and other libs installed with them. This is to
1519 // prevent those libs from being included in the search path
1520 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1521 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1522 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1523 // will be loaded into the default linker namespace (aka "platform" namespace). If
1524 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1525 // be loaded again into the runtime linker namespace, which will result in double
1526 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001527 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001528 }
Florian Mayer95cd6db2023-03-23 17:48:07 -07001529 // This needs to go after the runtime APEX handling because otherwise we would get
1530 // weird paths like lib64/rel_install_path/bionic rather than
1531 // lib64/bionic/rel_install_path.
1532 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001533
Colin Cross1d487152022-10-03 19:14:46 -07001534 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001535 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1536 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001537}
1538
Jiyong Park1833cef2019-12-13 13:28:36 +09001539func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001540 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001541 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001542 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001543 }
Jooyung Han35155c42020-02-06 17:33:20 +09001544 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001545 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001546 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1547 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001548 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001549 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001550 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001551}
1552
Jiyong Park99644e92020-11-17 22:21:02 +09001553func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1554 dirInApex := "bin"
1555 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1556 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1557 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09001558 dirInApex = filepath.Join(dirInApex, rustm.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001559 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001560 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1561 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1562 return af
1563}
1564
1565func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1566 // Decide the APEX-local directory by the multilib of the library
1567 // In the future, we may query this to the module.
1568 var dirInApex string
1569 switch rustm.Arch().ArchType.Multilib {
1570 case "lib32":
1571 dirInApex = "lib"
1572 case "lib64":
1573 dirInApex = "lib64"
1574 }
1575 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1576 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1577 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09001578 dirInApex = filepath.Join(dirInApex, rustm.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001579 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001580 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1581 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1582}
1583
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001584func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001585 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001586 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1587 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1588 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001589 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001590 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001591 af.symlinks = sh.Symlinks()
1592 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001593}
1594
Thiébaud Weksteen00e8b312024-03-18 14:06:00 +11001595func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, outputFile android.Path) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001596 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Cole Faust7c991b42024-05-15 11:17:55 -07001597 makeModuleName := strings.ReplaceAll(filepath.Join(dirInApex, outputFile.Base()), "/", "_")
1598 return newApexFile(ctx, outputFile, makeModuleName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001599}
1600
atrost6e126252020-01-27 17:01:16 +00001601func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1602 dirInApex := filepath.Join("etc", config.SubDir())
1603 fileToCopy := config.CompatConfig()
1604 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1605}
1606
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001607// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1608// way.
1609type javaModule interface {
1610 android.Module
1611 BaseModuleName() string
Spandan Das59a4a2b2024-01-09 21:35:56 +00001612 DexJarBuildPath(ctx android.ModuleErrorfContext) java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001613 JacocoReportClassesFile() android.Path
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001614 Stem() string
1615}
1616
1617var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001618var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001619var _ javaModule = (*java.SdkLibrary)(nil)
1620var _ javaModule = (*java.DexImport)(nil)
1621var _ javaModule = (*java.SdkLibraryImport)(nil)
1622
Paul Duffin190fdef2021-04-26 10:33:59 +01001623// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Justin Yun613bdc52024-06-12 21:32:10 +09001624func apexFileForJavaModule(ctx android.ModuleContext, module javaModule) apexFile {
Spandan Das59a4a2b2024-01-09 21:35:56 +00001625 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath(ctx).PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001626}
1627
1628// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
Justin Yun613bdc52024-06-12 21:32:10 +09001629func apexFileForJavaModuleWithFile(ctx android.ModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001630 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001631 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001632 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
Colin Crossb79aa8f2024-09-25 15:41:01 -07001633 if lintInfo, ok := android.OtherModuleProvider(ctx, module, java.LintProvider); ok {
1634 af.lintInfo = lintInfo
1635 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001636 af.customStem = module.Stem() + ".jar"
Jihoon Kanga3a05462024-04-05 00:36:44 +00001637 // TODO: b/338641779 - Remove special casing of sdkLibrary once bcpf and sscpf depends
1638 // on the implementation library
1639 if sdkLib, ok := module.(*java.SdkLibrary); ok {
1640 for _, install := range sdkLib.BuiltInstalledForApex() {
1641 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1642 }
1643 } else if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001644 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1645 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1646 }
1647 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001648 return af
1649}
1650
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001651func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1652 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001653 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001654 dirInApex := "javalib"
1655 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1656 af.customStem = module.Stem() + ".jar.prof"
1657 return &af
1658 }
1659 }
1660 return nil
1661}
1662
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001663// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1664// the same way.
1665type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001666 android.Module
1667 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001668 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001669 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001670 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001671 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001672 BaseModuleName() string
Andrei Onea580636b2022-08-17 16:53:46 +00001673 PrivAppAllowlist() android.OptionalPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001674}
1675
1676var _ androidApp = (*java.AndroidApp)(nil)
1677var _ androidApp = (*java.AndroidAppImport)(nil)
1678
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001679func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1680 buildId := ctx.Config().BuildId()
1681
1682 // The build ID is used as a suffix for a filename, so ensure that
1683 // the set of characters being used are sanitized.
1684 // - any word character: [a-zA-Z0-9_]
1685 // - dots: .
1686 // - dashes: -
1687 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1688 if !validRegex.MatchString(buildId) {
1689 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1690 }
1691 return buildId
1692}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001693
Andrei Onea580636b2022-08-17 16:53:46 +00001694func apexFilesForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) []apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001695 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001696 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001697 appDir = "priv-app"
1698 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001699
1700 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1701 // so that PackageManager correctly invalidates the existing installed apk
1702 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001703 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001704 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001705
Yo Chiange8128052020-07-23 20:09:18 +08001706 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001707 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Crossb79aa8f2024-09-25 15:41:01 -07001708 if lintInfo, ok := android.OtherModuleProvider(ctx, aapp, java.LintProvider); ok {
1709 af.lintInfo = lintInfo
1710 }
Colin Cross503c1d02020-01-28 14:00:53 -08001711 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001712
1713 if app, ok := aapp.(interface {
1714 OverriddenManifestPackageName() string
1715 }); ok {
1716 af.overriddenPackageName = app.OverriddenManifestPackageName()
1717 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04001718
1719 apexFiles := []apexFile{}
Andrei Onea580636b2022-08-17 16:53:46 +00001720
1721 if allowlist := aapp.PrivAppAllowlist(); allowlist.Valid() {
1722 dirInApex := filepath.Join("etc", "permissions")
Sam Delmericob1daccd2023-05-25 14:45:30 -04001723 privAppAllowlist := newApexFile(ctx, allowlist.Path(), aapp.BaseModuleName()+"_privapp", dirInApex, etc, aapp)
Andrei Onea580636b2022-08-17 16:53:46 +00001724 apexFiles = append(apexFiles, privAppAllowlist)
1725 }
1726
Sam Delmericob1daccd2023-05-25 14:45:30 -04001727 apexFiles = append(apexFiles, af)
1728
Andrei Onea580636b2022-08-17 16:53:46 +00001729 return apexFiles
Dario Frenicde2a032019-10-27 00:29:22 +01001730}
1731
Jiyong Park69aeba92020-04-24 21:16:36 +09001732func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1733 rroDir := "overlay"
1734 dirInApex := filepath.Join(rroDir, rro.Theme())
1735 fileToCopy := rro.OutputFile()
1736 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1737 af.certificate = rro.Certificate()
1738
1739 if a, ok := rro.(interface {
1740 OverriddenManifestPackageName() string
1741 }); ok {
1742 af.overriddenPackageName = a.OverriddenManifestPackageName()
1743 }
1744 return af
1745}
1746
Ken Chenfad7f9d2021-11-10 22:02:57 +08001747func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1748 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001749 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1750}
1751
Jiyong Park12a719c2021-01-07 15:31:24 +09001752func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1753 dirInApex := filepath.Join("etc", "fs")
1754 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1755}
1756
Paul Duffin064b70c2020-11-02 17:32:38 +00001757// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001758// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1759// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1760// modules. This is used in check* functions below.
Colin Cross8bf14fc2024-09-25 16:41:31 -07001761func (a *apexBundle) WalkPayloadDeps(ctx android.BaseModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001762 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001763 am, ok := child.(android.ApexModule)
1764 if !ok || !am.CanHaveApexVariants() {
1765 return false
1766 }
1767
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001768 // Filter-out unwanted depedendencies
1769 depTag := ctx.OtherModuleDependencyTag(child)
1770 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1771 return false
1772 }
Paul Duffin520917a2022-05-13 13:01:59 +00001773 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001774 return false
1775 }
Jiyong Park8bcf3c62024-03-18 18:37:10 +09001776 if depTag == android.RequiredDepTag {
1777 return false
1778 }
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001779
Colin Cross313aa542023-12-13 13:47:44 -08001780 ai, _ := android.OtherModuleProvider(ctx, child, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09001781 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001782
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001783 // Visit actually
1784 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001785 })
1786}
1787
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001788// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1789type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001790
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001791const (
1792 ext4 fsType = iota
1793 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001794 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001795)
Artur Satayev849f8442020-04-28 14:57:42 +01001796
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001797func (f fsType) string() string {
1798 switch f {
1799 case ext4:
1800 return ext4FsType
1801 case f2fs:
1802 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001803 case erofs:
1804 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001805 default:
1806 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001807 }
1808}
1809
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001810func (a *apexBundle) setCompression(ctx android.ModuleContext) {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001811 if a.testOnlyShouldForceCompression() {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001812 a.isCompressed = true
1813 } else {
1814 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
1815 }
1816}
1817
1818func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
1819 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
1820 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
1821 // the same library in the system partition, thus effectively sharing the same libraries
1822 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
1823 // in the APEX.
1824 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
1825
1826 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
1827 // So we can't link them to /system/lib libs which are core variants.
1828 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
1829 a.linkToSystemLib = false
1830 }
1831
1832 forced := ctx.Config().ForceApexSymlinkOptimization()
1833 updatable := a.Updatable() || a.FutureUpdatable()
1834
1835 // We don't need the optimization for updatable APEXes, as it might give false signal
1836 // to the system health when the APEXes are still bundled (b/149805758).
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001837 if !forced && updatable {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001838 a.linkToSystemLib = false
1839 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001840}
1841
1842func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
1843 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
1844 case ext4FsType:
1845 a.payloadFsType = ext4
1846 case f2fsFsType:
1847 a.payloadFsType = f2fs
1848 case erofsFsType:
1849 a.payloadFsType = erofs
1850 default:
1851 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
1852 }
1853}
1854
Jooyung Haneec1b3f2023-06-20 16:25:59 +09001855func (a *apexBundle) isCompressable() bool {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001856 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
1857}
1858
1859func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
1860 a.checkApexAvailability(ctx)
1861 a.checkUpdatable(ctx)
1862 a.CheckMinSdkVersion(ctx)
1863 a.checkStaticLinkingToStubLibraries(ctx)
1864 a.checkStaticExecutables(ctx)
Colin Cross99939e92024-10-01 16:02:46 -07001865 a.enforceAppUpdatability(ctx)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001866 if len(a.properties.Tests) > 0 && !a.testApex {
1867 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
1868 return false
1869 }
1870 return true
1871}
1872
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001873type visitorContext struct {
1874 // all the files that will be included in this APEX
1875 filesInfo []apexFile
1876
1877 // native lib dependencies
1878 provideNativeLibs []string
1879 requireNativeLibs []string
1880
1881 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09001882
1883 // if true, raise error on duplicate apexFile
1884 checkDuplicate bool
Jooyung Hana8bd72a2023-11-02 11:56:48 +09001885
1886 // visitor skips these from this list of module names
1887 unwantedTransitiveDeps []string
Colin Crossb614cd42024-10-11 12:52:21 -07001888
1889 // unwantedTransitiveFilesInfo contains files that would have been in the apex
1890 // except that they were listed in unwantedTransitiveDeps.
1891 unwantedTransitiveFilesInfo []apexFile
1892
1893 // duplicateTransitiveFilesInfo contains files that would ahve been in the apex
1894 // except that another variant of the same module was already in the apex.
1895 duplicateTransitiveFilesInfo []apexFile
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001896}
1897
Jooyung Han862c0d62022-12-21 10:15:37 +09001898func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001899 encountered := make(map[string]apexFile)
1900 for _, f := range vctx.filesInfo {
Jooyung Hana8bd72a2023-11-02 11:56:48 +09001901 // Skips unwanted transitive deps. This happens, for example, with Rust binaries with prefer_rlib:true.
1902 // TODO(b/295593640)
1903 // Needs additional verification for the resulting APEX to ensure that skipped artifacts don't make problems.
1904 // For example, DT_NEEDED modules should be found within the APEX unless they are marked in `requiredNativeLibs`.
1905 if f.transitiveDep && f.module != nil && android.InList(mctx.OtherModuleName(f.module), vctx.unwantedTransitiveDeps) {
Colin Crossb614cd42024-10-11 12:52:21 -07001906 vctx.unwantedTransitiveFilesInfo = append(vctx.unwantedTransitiveFilesInfo, f)
Jooyung Hana8bd72a2023-11-02 11:56:48 +09001907 continue
1908 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001909 dest := filepath.Join(f.installDir, f.builtFile.Base())
1910 if e, ok := encountered[dest]; !ok {
1911 encountered[dest] = f
1912 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09001913 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
1914 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
1915 dest, e.builtFile, f.builtFile)
1916 return
Colin Crossb614cd42024-10-11 12:52:21 -07001917 } else {
1918 vctx.duplicateTransitiveFilesInfo = append(vctx.duplicateTransitiveFilesInfo, f)
Jooyung Han862c0d62022-12-21 10:15:37 +09001919 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001920 // If a module is directly included and also transitively depended on
1921 // consider it as directly included.
1922 e.transitiveDep = e.transitiveDep && f.transitiveDep
Jiakai Zhang9c60c172023-09-05 15:19:21 +01001923 // If a module is added as both a JNI library and a regular shared library, consider it as a
1924 // JNI library.
1925 e.isJniLib = e.isJniLib || f.isJniLib
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001926 encountered[dest] = e
1927 }
1928 }
1929 vctx.filesInfo = vctx.filesInfo[:0]
1930 for _, v := range encountered {
1931 vctx.filesInfo = append(vctx.filesInfo, v)
1932 }
Colin Crossb614cd42024-10-11 12:52:21 -07001933
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001934 sort.Slice(vctx.filesInfo, func(i, j int) bool {
1935 // Sort by destination path so as to ensure consistent ordering even if the source of the files
1936 // changes.
1937 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
1938 })
1939}
1940
Spandan Dasfbcd5fe2024-09-30 22:30:39 +00001941// enforcePartitionTagOnApexSystemServerJar checks that the partition tags of an apex system server jar matches
1942// the partition tags of the top-level apex.
1943// e.g. if the top-level apex sets system_ext_specific to true, the javalib must set this property to true as well.
1944// This check ensures that the dexpreopt artifacts of the apex system server jar is installed in the same partition
1945// as the apex.
1946func (a *apexBundle) enforcePartitionTagOnApexSystemServerJar(ctx android.ModuleContext) {
1947 global := dexpreopt.GetGlobalConfig(ctx)
1948 ctx.VisitDirectDepsWithTag(sscpfTag, func(child android.Module) {
1949 info, ok := android.OtherModuleProvider(ctx, child, java.LibraryNameToPartitionInfoProvider)
1950 if !ok {
1951 ctx.ModuleErrorf("Could not find partition info of apex system server jars.")
1952 }
1953 apexPartition := ctx.Module().PartitionTag(ctx.DeviceConfig())
1954 for javalib, javalibPartition := range info.LibraryNameToPartition {
1955 if !global.AllApexSystemServerJars(ctx).ContainsJar(javalib) {
1956 continue // not an apex system server jar
1957 }
1958 if apexPartition != javalibPartition {
1959 ctx.ModuleErrorf(`
1960%s is an apex systemserver jar, but its partition does not match the partition of its containing apex. Expected %s, Got %s`,
1961 javalib, apexPartition, javalibPartition)
1962 }
1963 }
1964 })
1965}
1966
Colin Cross648daea2024-09-12 14:35:29 -07001967func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent android.Module) bool {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001968 depTag := ctx.OtherModuleDependencyTag(child)
1969 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1970 return false
1971 }
Colin Cross648daea2024-09-12 14:35:29 -07001972 if !child.Enabled(ctx) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001973 return false
1974 }
1975 depName := ctx.OtherModuleName(child)
1976 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
1977 switch depTag {
1978 case sharedLibTag, jniLibTag:
1979 isJniLib := depTag == jniLibTag
Jooyung Han20348752023-12-05 15:23:56 +09001980 propertyName := "native_shared_libs"
1981 if isJniLib {
1982 propertyName = "jni_libs"
1983 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001984 switch ch := child.(type) {
1985 case *cc.Module:
Jooyung Han20348752023-12-05 15:23:56 +09001986 if ch.IsStubs() {
1987 ctx.PropertyErrorf(propertyName, "%q is a stub. Remove it from the list.", depName)
1988 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001989 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
1990 fi.isJniLib = isJniLib
1991 vctx.filesInfo = append(vctx.filesInfo, fi)
1992 // Collect the list of stub-providing libs except:
1993 // - VNDK libs are only for vendors
1994 // - bootstrap bionic libs are treated as provided by system
1995 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
1996 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
1997 }
1998 return true // track transitive dependencies
1999 case *rust.Module:
2000 fi := apexFileForRustLibrary(ctx, ch)
2001 fi.isJniLib = isJniLib
2002 vctx.filesInfo = append(vctx.filesInfo, fi)
2003 return true // track transitive dependencies
2004 default:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002005 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2006 }
2007 case executableTag:
2008 switch ch := child.(type) {
2009 case *cc.Module:
2010 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2011 return true // track transitive dependencies
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002012 case *rust.Module:
2013 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2014 return true // track transitive dependencies
2015 default:
2016 ctx.PropertyErrorf("binaries",
2017 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2018 }
2019 case shBinaryTag:
2020 if csh, ok := child.(*sh.ShBinary); ok {
2021 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2022 } else {
2023 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2024 }
2025 case bcpfTag:
Jiakai Zhangb47cacc2023-05-10 16:40:18 +01002026 _, ok := child.(*java.BootclasspathFragmentModule)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002027 if !ok {
2028 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2029 return false
2030 }
2031
2032 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002033 return true
2034 case sscpfTag:
2035 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2036 ctx.PropertyErrorf("systemserverclasspath_fragments",
2037 "%q is not a systemserverclasspath_fragment module", depName)
2038 return false
2039 }
2040 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2041 vctx.filesInfo = append(vctx.filesInfo, *af)
2042 }
2043 return true
2044 case javaLibTag:
2045 switch child.(type) {
2046 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2047 af := apexFileForJavaModule(ctx, child.(javaModule))
2048 if !af.ok() {
2049 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2050 return false
2051 }
2052 vctx.filesInfo = append(vctx.filesInfo, af)
2053 return true // track transitive dependencies
2054 default:
2055 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2056 }
2057 case androidAppTag:
2058 switch ap := child.(type) {
2059 case *java.AndroidApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002060 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002061 return true // track transitive dependencies
2062 case *java.AndroidAppImport:
Andrei Onea580636b2022-08-17 16:53:46 +00002063 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002064 case *java.AndroidTestHelperApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002065 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002066 case *java.AndroidAppSet:
2067 appDir := "app"
2068 if ap.Privileged() {
2069 appDir = "priv-app"
2070 }
2071 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2072 // suffixed so that PackageManager correctly invalidates the
2073 // existing installed apk in favour of the new APK-in-APEX.
2074 // See bugs for more information.
2075 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2076 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2077 af.certificate = java.PresignedCertificate
2078 vctx.filesInfo = append(vctx.filesInfo, af)
2079 default:
2080 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2081 }
2082 case rroTag:
2083 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2084 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2085 } else {
2086 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2087 }
2088 case bpfTag:
2089 if bpfProgram, ok := child.(bpf.BpfModule); ok {
mrziwange6c85812024-05-22 14:36:09 -07002090 filesToCopy := android.OutputFilesForModule(ctx, bpfProgram, "")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002091 apex_sub_dir := bpfProgram.SubDir()
2092 for _, bpfFile := range filesToCopy {
2093 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2094 }
2095 } else {
2096 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2097 }
2098 case fsTag:
2099 if fs, ok := child.(filesystem.Filesystem); ok {
2100 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2101 } else {
2102 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2103 }
2104 case prebuiltTag:
2105 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
mrziwange2346b82024-06-10 15:09:45 -07002106 filesToCopy := android.OutputFilesForModule(ctx, prebuilt, "")
Thiébaud Weksteen00e8b312024-03-18 14:06:00 +11002107 for _, etcFile := range filesToCopy {
2108 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, etcFile))
2109 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002110 } else {
2111 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2112 }
2113 case compatConfigTag:
2114 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2115 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2116 } else {
2117 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2118 }
2119 case testTag:
2120 if ccTest, ok := child.(*cc.Module); ok {
Colin Cross3a02c7b2024-05-21 13:46:22 -07002121 af := apexFileForExecutable(ctx, ccTest)
2122 af.class = nativeTest
2123 vctx.filesInfo = append(vctx.filesInfo, af)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002124 return true // track transitive dependencies
2125 } else {
2126 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2127 }
2128 case keyTag:
2129 if key, ok := child.(*apexKey); ok {
2130 a.privateKeyFile = key.privateKeyFile
2131 a.publicKeyFile = key.publicKeyFile
2132 } else {
2133 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2134 }
2135 case certificateTag:
2136 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2137 a.containerCertificateFile = dep.Certificate.Pem
2138 a.containerPrivateKeyFile = dep.Certificate.Key
2139 } else {
2140 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2141 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002142 }
2143 return false
2144 }
2145
2146 if a.vndkApex {
2147 return false
2148 }
2149
2150 // indirect dependencies
2151 am, ok := child.(android.ApexModule)
2152 if !ok {
2153 return false
2154 }
2155 // We cannot use a switch statement on `depTag` here as the checked
2156 // tags used below are private (e.g. `cc.sharedDepTag`).
2157 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2158 if ch, ok := child.(*cc.Module); ok {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002159 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2160 af.transitiveDep = true
2161
Spandan Dasf5e03f12024-01-25 19:25:42 +00002162 abInfo, _ := android.ModuleProvider(ctx, android.ApexBundleInfoProvider)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002163 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2164 // If the dependency is a stubs lib, don't include it in this APEX,
2165 // but make sure that the lib is installed on the device.
2166 // In case no APEX is having the lib, the lib is installed to the system
2167 // partition.
2168 //
2169 // Always include if we are a host-apex however since those won't have any
2170 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002171 //
2172 // Skip the dependency in unbundled builds where the device image is not
2173 // being built.
2174 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002175 // we need a module name for Make
2176 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002177 if !android.InList(name, a.makeModulesToInstall) {
2178 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002179 }
2180 }
2181 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2182 // Don't track further
2183 return false
2184 }
2185
2186 // If the dep is not considered to be in the same
2187 // apex, don't add it to filesInfo so that it is not
2188 // included in this APEX.
2189 // TODO(jiyong): move this to at the top of the
2190 // else-if clause for the indirect dependencies.
2191 // Currently, that's impossible because we would
2192 // like to record requiredNativeLibs even when
2193 // DepIsInSameAPex is false. We also shouldn't do
2194 // this for host.
2195 //
2196 // TODO(jiyong): explain why the same module is passed in twice.
2197 // Switching the first am to parent breaks lots of tests.
2198 if !android.IsDepInSameApex(ctx, am, am) {
2199 return false
2200 }
2201
2202 vctx.filesInfo = append(vctx.filesInfo, af)
2203 return true // track transitive dependencies
2204 } else if rm, ok := child.(*rust.Module); ok {
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00002205 if !android.IsDepInSameApex(ctx, am, am) {
2206 return false
2207 }
2208
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002209 af := apexFileForRustLibrary(ctx, rm)
2210 af.transitiveDep = true
2211 vctx.filesInfo = append(vctx.filesInfo, af)
2212 return true // track transitive dependencies
2213 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002214 } else if cc.IsHeaderDepTag(depTag) {
2215 // nothing
2216 } else if java.IsJniDepTag(depTag) {
2217 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2218 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2219 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
mrziwange2346b82024-06-10 15:09:45 -07002220 filesToCopy := android.OutputFilesForModule(ctx, prebuilt, "")
Thiébaud Weksteen00e8b312024-03-18 14:06:00 +11002221 for _, etcFile := range filesToCopy {
2222 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, etcFile))
2223 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002224 }
2225 } else if rust.IsDylibDepTag(depTag) {
2226 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00002227 if !android.IsDepInSameApex(ctx, am, am) {
2228 return false
2229 }
2230
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002231 af := apexFileForRustLibrary(ctx, rustm)
2232 af.transitiveDep = true
2233 vctx.filesInfo = append(vctx.filesInfo, af)
2234 return true // track transitive dependencies
2235 }
2236 } else if rust.IsRlibDepTag(depTag) {
2237 // Rlib is statically linked, but it might have shared lib
2238 // dependencies. Track them.
2239 return true
2240 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2241 // Add the contents of the bootclasspath fragment to the apex.
2242 switch child.(type) {
2243 case *java.Library, *java.SdkLibrary:
2244 javaModule := child.(javaModule)
2245 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2246 if !af.ok() {
2247 ctx.PropertyErrorf("bootclasspath_fragments",
2248 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2249 return false
2250 }
2251 vctx.filesInfo = append(vctx.filesInfo, af)
2252 return true // track transitive dependencies
2253 default:
2254 ctx.PropertyErrorf("bootclasspath_fragments",
2255 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2256 }
2257 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2258 // Add the contents of the systemserverclasspath fragment to the apex.
2259 switch child.(type) {
2260 case *java.Library, *java.SdkLibrary:
2261 af := apexFileForJavaModule(ctx, child.(javaModule))
2262 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002263 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2264 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2265 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002266 return true // track transitive dependencies
2267 default:
2268 ctx.PropertyErrorf("systemserverclasspath_fragments",
2269 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2270 }
2271 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2272 // nothing
2273 } else if depTag == android.DarwinUniversalVariantTag {
2274 // nothing
Jiyong Park8bcf3c62024-03-18 18:37:10 +09002275 } else if depTag == android.RequiredDepTag {
2276 // nothing
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002277 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2278 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2279 }
2280 return false
2281}
2282
Jooyung Han862c0d62022-12-21 10:15:37 +09002283func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2284 // TODO(b/263308293) remove this
2285 if a.properties.IsCoverageVariant {
2286 return false
2287 }
Jooyung Han8d4a1f02023-08-23 13:54:08 +09002288 if ctx.DeviceConfig().DeviceArch() == "" {
Jooyung Han862c0d62022-12-21 10:15:37 +09002289 return false
2290 }
2291 return true
2292}
2293
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002294// Creates build rules for an APEX. It consists of the following major steps:
2295//
2296// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2297// 2) traverse the dependency tree to collect apexFile structs from them.
2298// 3) some fields in apexBundle struct are configured
2299// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002300func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002301 ////////////////////////////////////////////////////////////////////////////////////////////
2302 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002303 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002304 return
2305 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002306 ////////////////////////////////////////////////////////////////////////////////////////////
2307 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002308
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002309 // TODO(jiyong): do this using WalkPayloadDeps
2310 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002311 vctx := visitorContext{
Jooyung Hana8bd72a2023-11-02 11:56:48 +09002312 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2313 checkDuplicate: a.shouldCheckDuplicate(ctx),
2314 unwantedTransitiveDeps: a.properties.Unwanted_transitive_deps,
Jooyung Han862c0d62022-12-21 10:15:37 +09002315 }
Colin Cross648daea2024-09-12 14:35:29 -07002316 ctx.WalkDeps(func(child, parent android.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002317 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002318 if a.privateKeyFile == nil {
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +01002319 if ctx.Config().AllowMissingDependencies() {
2320 // TODO(b/266099037): a better approach for slim manifests.
2321 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2322 // Create placeholder paths for later stages that expect to see those paths,
2323 // though they won't be used.
2324 var unusedPath = android.PathForModuleOut(ctx, "nonexistentprivatekey")
2325 ctx.Build(pctx, android.BuildParams{
2326 Rule: android.ErrorRule,
2327 Output: unusedPath,
2328 Args: map[string]string{
2329 "error": "Private key not available",
2330 },
2331 })
2332 a.privateKeyFile = unusedPath
2333 } else {
2334 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
2335 return
2336 }
2337 }
2338
2339 if a.publicKeyFile == nil {
2340 if ctx.Config().AllowMissingDependencies() {
2341 // TODO(b/266099037): a better approach for slim manifests.
2342 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2343 // Create placeholder paths for later stages that expect to see those paths,
2344 // though they won't be used.
2345 var unusedPath = android.PathForModuleOut(ctx, "nonexistentpublickey")
2346 ctx.Build(pctx, android.BuildParams{
2347 Rule: android.ErrorRule,
2348 Output: unusedPath,
2349 Args: map[string]string{
2350 "error": "Public key not available",
2351 },
2352 })
2353 a.publicKeyFile = unusedPath
2354 } else {
2355 ctx.PropertyErrorf("key", "public_key for %q could not be found", String(a.overridableProperties.Key))
2356 return
2357 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002358 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002359
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002360 ////////////////////////////////////////////////////////////////////////////////////////////
2361 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002362 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002363 a.filesInfo = vctx.filesInfo
Colin Crossb614cd42024-10-11 12:52:21 -07002364 a.unwantedTransitiveFilesInfo = vctx.unwantedTransitiveFilesInfo
2365 a.duplicateTransitiveFilesInfo = vctx.duplicateTransitiveFilesInfo
Alex Light5098a612018-11-29 17:12:15 -08002366
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002367 a.setPayloadFsType(ctx)
2368 a.setSystemLibLink(ctx)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09002369 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002370
2371 ////////////////////////////////////////////////////////////////////////////////////////////
Jooyung Hana3fddf42024-09-03 13:22:21 +09002372 // 3.a) some artifacts are generated from the collected files
2373 a.filesInfo = append(a.filesInfo, a.buildAconfigFiles(ctx)...)
2374
2375 ////////////////////////////////////////////////////////////////////////////////////////////
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002376 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002377 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09002378 a.buildApex(ctx)
Jiyong Park956305c2020-01-09 12:32:06 +09002379 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002380 a.buildLintReports(ctx)
Spandan Dasda739a32023-12-13 00:06:32 +00002381
2382 // Set a provider for dexpreopt of bootjars
2383 a.provideApexExportsInfo(ctx)
Spandan Dasa747d2e2024-03-11 21:37:25 +00002384
2385 a.providePrebuiltInfo(ctx)
Cole Faust43ddd082024-06-17 12:32:40 -07002386
2387 a.required = a.RequiredModuleNames(ctx)
Kiyoung Kimfaf6af32024-08-12 11:15:19 +09002388 a.required = append(a.required, a.VintfFragmentModuleNames(ctx)...)
mrziwange7ec89e2024-06-13 12:05:18 -07002389
2390 a.setOutputFiles(ctx)
Spandan Dasfbcd5fe2024-09-30 22:30:39 +00002391 a.enforcePartitionTagOnApexSystemServerJar(ctx)
Colin Crossb614cd42024-10-11 12:52:21 -07002392
2393 a.verifyNativeImplementationLibs(ctx)
Spandan Dasa747d2e2024-03-11 21:37:25 +00002394}
2395
Spandan Dasa747d2e2024-03-11 21:37:25 +00002396// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
2397// with information about whether source or prebuilt of an apex was used during the build.
2398func (a *apexBundle) providePrebuiltInfo(ctx android.ModuleContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +00002399 info := android.PrebuiltInfo{
Spandan Dasa747d2e2024-03-11 21:37:25 +00002400 Name: a.Name(),
2401 Is_prebuilt: false,
2402 }
Spandan Das3490dfd2024-03-11 21:37:25 +00002403 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
Spandan Dasda739a32023-12-13 00:06:32 +00002404}
2405
2406// Set a provider containing information about the jars and .prof provided by the apex
2407// Apexes built from source retrieve this information by visiting `bootclasspath_fragments`
2408// Used by dex_bootjars to generate the boot image
2409func (a *apexBundle) provideApexExportsInfo(ctx android.ModuleContext) {
2410 ctx.VisitDirectDepsWithTag(bcpfTag, func(child android.Module) {
2411 if info, ok := android.OtherModuleProvider(ctx, child, java.BootclasspathFragmentApexContentInfoProvider); ok {
2412 exports := android.ApexExportsInfo{
Spandan Das5be63332023-12-13 00:06:32 +00002413 ApexName: a.ApexVariationName(),
2414 ProfilePathOnHost: info.ProfilePathOnHost(),
2415 LibraryNameToDexJarPathOnHost: info.DexBootJarPathMap(),
Spandan Dasda739a32023-12-13 00:06:32 +00002416 }
Spandan Dasa41b8ec2023-12-22 00:05:04 +00002417 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +00002418 }
2419 })
Jooyung Han01a3ee22019-11-02 02:52:25 +09002420}
2421
mrziwange7ec89e2024-06-13 12:05:18 -07002422// Set output files to outputFiles property, which is later used to set the
2423// OutputFilesProvider
2424func (a *apexBundle) setOutputFiles(ctx android.ModuleContext) {
2425 // default dist path
2426 ctx.SetOutputFiles(android.Paths{a.outputFile}, "")
2427 ctx.SetOutputFiles(android.Paths{a.outputFile}, android.DefaultDistTag)
2428 // uncompressed one
2429 if a.outputApexFile != nil {
2430 ctx.SetOutputFiles(android.Paths{a.outputApexFile}, imageApexSuffix)
2431 }
2432}
2433
Colin Cross99939e92024-10-01 16:02:46 -07002434// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
2435func (a *apexBundle) enforceAppUpdatability(mctx android.ModuleContext) {
2436 if !a.Enabled(mctx) {
2437 return
2438 }
2439 if a.Updatable() {
2440 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
2441 mctx.VisitDirectDeps(func(module android.Module) {
2442 if appInfo, ok := android.OtherModuleProvider(mctx, module, java.AppInfoProvider); ok {
2443 // ignore android_test_app
2444 if !appInfo.TestHelperApp && !appInfo.Updatable {
2445 mctx.ModuleErrorf("app dependency %s must have updatable: true", mctx.OtherModuleName(module))
2446 }
2447 }
2448 })
2449 }
2450}
2451
Paul Duffincc33ec82021-04-25 23:14:55 +01002452// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2453// the bootclasspath_fragment contributes to the apex.
2454func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
Colin Cross313aa542023-12-13 13:47:44 -08002455 bootclasspathFragmentInfo, _ := android.OtherModuleProvider(ctx, module, java.BootclasspathFragmentApexContentInfoProvider)
Paul Duffincc33ec82021-04-25 23:14:55 +01002456 var filesToAdd []apexFile
2457
satayev3db35472021-05-06 23:59:58 +01002458 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002459 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2460 filesToAdd = append(filesToAdd, *af)
2461 }
satayev3db35472021-05-06 23:59:58 +01002462
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002463 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
Jiakai Zhangbc698cd2023-05-08 16:28:38 +00002464 if pathInApex != "" {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002465 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2466 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2467
2468 if pathOnHost != nil {
2469 // We need to copy the profile to a temporary path with the right filename because the apexer
2470 // will take the filename as is.
2471 ctx.Build(pctx, android.BuildParams{
2472 Rule: android.Cp,
2473 Input: pathOnHost,
2474 Output: tempPath,
2475 })
2476 } else {
2477 // At this point, the boot image profile cannot be generated. It is probably because the boot
2478 // image profile source file does not exist on the branch, or it is not available for the
2479 // current build target.
2480 // However, we cannot enforce the boot image profile to be generated because some build
2481 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2482 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2483 // only if the APEX is being built.
2484 ctx.Build(pctx, android.BuildParams{
2485 Rule: android.ErrorRule,
2486 Output: tempPath,
2487 Args: map[string]string{
2488 "error": "Boot image profile cannot be generated",
2489 },
2490 })
2491 }
2492
2493 androidMkModuleName := filepath.Base(pathInApex)
2494 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2495 filesToAdd = append(filesToAdd, af)
2496 }
2497
Paul Duffincc33ec82021-04-25 23:14:55 +01002498 return filesToAdd
2499}
2500
satayevb98371c2021-06-15 16:49:50 +01002501// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2502// the module contributes to the apex; or nil if the proto config was not generated.
2503func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
Colin Cross313aa542023-12-13 13:47:44 -08002504 info, _ := android.OtherModuleProvider(ctx, module, java.ClasspathFragmentProtoContentInfoProvider)
satayevb98371c2021-06-15 16:49:50 +01002505 if !info.ClasspathFragmentProtoGenerated {
2506 return nil
2507 }
2508 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2509 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2510 return &af
satayev14e49132021-05-17 21:03:07 +01002511}
2512
Paul Duffincc33ec82021-04-25 23:14:55 +01002513// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2514// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002515func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
Colin Cross313aa542023-12-13 13:47:44 -08002516 bootclasspathFragmentInfo, _ := android.OtherModuleProvider(ctx, fragmentModule, java.BootclasspathFragmentApexContentInfoProvider)
Paul Duffin190fdef2021-04-26 10:33:59 +01002517
2518 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2519 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002520 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2521 if err != nil {
2522 ctx.ModuleErrorf("%s", err)
2523 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002524
2525 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2526 // bootclasspath_fragment.
2527 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2528 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002529}
2530
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002531///////////////////////////////////////////////////////////////////////////////////////////////////
2532// Factory functions
2533//
2534
2535func newApexBundle() *apexBundle {
2536 module := &apexBundle{}
2537
2538 module.AddProperties(&module.properties)
2539 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002540 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002541 module.AddProperties(&module.overridableProperties)
2542
Jooyung Han8d4a1f02023-08-23 13:54:08 +09002543 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002544 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002545 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
2546 return module
2547}
2548
Paul Duffineb8051d2021-10-18 17:49:39 +01002549func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002550 bundle := newApexBundle()
2551 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002552 return bundle
2553}
2554
2555// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2556// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002557func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002558 bundle := newApexBundle()
2559 bundle.testApex = true
2560 return bundle
2561}
2562
2563// apex packages other modules into an APEX file which is a packaging format for system-level
2564// components like binaries, shared libraries, etc.
2565func BundleFactory() android.Module {
2566 return newApexBundle()
2567}
2568
2569type Defaults struct {
2570 android.ModuleBase
2571 android.DefaultsModuleBase
2572}
2573
2574// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002575func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002576 module := &Defaults{}
2577
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002578 module.AddProperties(
2579 &apexBundleProperties{},
2580 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002581 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002582 &overridableProperties{},
2583 )
2584
2585 android.InitDefaultsModule(module)
2586 return module
2587}
2588
2589type OverrideApex struct {
2590 android.ModuleBase
2591 android.OverrideModuleBase
2592}
2593
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002594func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002595 // All the overrides happen in the base module.
2596}
2597
2598// override_apex is used to create an apex module based on another apex module by overriding some of
2599// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002600func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002601 m := &OverrideApex{}
2602
2603 m.AddProperties(&overridableProperties{})
2604
2605 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2606 android.InitOverrideModule(m)
2607 return m
2608}
2609
2610///////////////////////////////////////////////////////////////////////////////////////////////////
2611// Vality check routines
2612//
2613// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2614// certain conditions are not met.
2615//
2616// TODO(jiyong): move these checks to a separate go file.
2617
satayevad991492021-12-03 18:58:32 +00002618var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2619
Spandan Dasa5f39a12022-08-05 02:35:52 +00002620// Ensures that min_sdk_version of the included modules are equal or less than the min_sdk_version
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002621// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002622func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002623 if a.testApex || a.vndkApex {
2624 return
2625 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002626 // apexBundle::minSdkVersion reports its own errors.
2627 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002628 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002629}
2630
Albert Martineefabcf2022-03-21 20:11:16 +00002631// Returns apex's min_sdk_version string value, honoring overrides
2632func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2633 // Only override the minSdkVersion value on Apexes which already specify
2634 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2635 // min_sdk_version value is lower than the one to override with.
Spandan Das50801e22024-05-13 18:29:45 +00002636 minApiLevel := android.MinSdkVersionFromValue(ctx, proptools.String(a.overridableProperties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002637 if minApiLevel.IsNone() {
2638 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002639 }
2640
Colin Cross56534df2022-10-04 09:58:58 -07002641 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
Sam Delmerico0e0d96e2023-08-18 22:43:28 +00002642 overrideApiLevel := android.MinSdkVersionFromValue(ctx, overrideMinSdkValue)
Colin Cross56534df2022-10-04 09:58:58 -07002643 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2644 minApiLevel = overrideApiLevel
2645 }
2646
2647 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002648}
2649
2650// Returns apex's min_sdk_version SdkSpec, honoring overrides
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002651func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2652 return a.minSdkVersion(ctx)
satayevad991492021-12-03 18:58:32 +00002653}
2654
Albert Martineefabcf2022-03-21 20:11:16 +00002655// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00002656func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Sam Delmerico0e0d96e2023-08-18 22:43:28 +00002657 return android.MinSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002658}
2659
2660// Ensures that a lib providing stub isn't statically linked
2661func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
2662 // Practically, we only care about regular APEXes on the device.
Jooyung Han8d4a1f02023-08-23 13:54:08 +09002663 if a.testApex || a.vndkApex {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002664 return
2665 }
2666
Spandan Dasf5e03f12024-01-25 19:25:42 +00002667 abInfo, _ := android.ModuleProvider(ctx, android.ApexBundleInfoProvider)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002668
Colin Cross8bf14fc2024-09-25 16:41:31 -07002669 a.WalkPayloadDeps(ctx, func(ctx android.BaseModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002670 if ccm, ok := to.(*cc.Module); ok {
2671 apexName := ctx.ModuleName()
2672 fromName := ctx.OtherModuleName(from)
2673 toName := ctx.OtherModuleName(to)
2674
2675 // If `to` is not actually in the same APEX as `from` then it does not need
2676 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002677 //
2678 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002679 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2680 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2681 return false
2682 }
2683
2684 // The dynamic linker and crash_dump tool in the runtime APEX is the only
2685 // exception to this rule. It can't make the static dependencies dynamic
2686 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09002687 // Same rule should be applied to linkerconfig, because it should be executed
2688 // only with static linked libraries before linker is available with ld.config.txt
2689 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002690 return false
2691 }
2692
2693 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
2694 if isStubLibraryFromOtherApex && !externalDep {
2695 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
2696 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
2697 }
2698
2699 }
2700 return true
2701 })
2702}
2703
satayevb98371c2021-06-15 16:49:50 +01002704// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002705func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
2706 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00002707 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002708 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
2709 }
Spandan Das32c93a32024-07-17 20:31:58 +00002710 if a.minSdkVersion(ctx).IsCurrent() {
Spandan Dasca1d63e2024-07-01 22:53:49 +00002711 ctx.PropertyErrorf("updatable", "updatable APEXes should not set min_sdk_version to current. Please use a finalized API level or a recognized in-development codename")
2712 }
Jiyong Park1bc84122021-06-22 20:23:05 +09002713 if a.UsePlatformApis() {
2714 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
2715 }
Jiyong Parkf4020582021-11-29 12:37:10 +09002716 if a.FutureUpdatable() {
2717 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
2718 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002719 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01002720 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002721 }
2722}
2723
satayevb98371c2021-06-15 16:49:50 +01002724// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
2725func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
2726 ctx.VisitDirectDeps(func(module android.Module) {
2727 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
Colin Cross313aa542023-12-13 13:47:44 -08002728 info, _ := android.OtherModuleProvider(ctx, module, java.ClasspathFragmentProtoContentInfoProvider)
satayevb98371c2021-06-15 16:49:50 +01002729 if !info.ClasspathFragmentProtoGenerated {
2730 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
2731 }
2732 }
2733 })
2734}
2735
2736// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01002737func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002738 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
2739 // java's checkLinkType guarantees correct usage for transitive deps
Colin Cross648daea2024-09-12 14:35:29 -07002740 ctx.VisitDirectDeps(func(module android.Module) {
Artur Satayev8cf899a2020-04-15 17:29:42 +01002741 tag := ctx.OtherModuleDependencyTag(module)
2742 switch tag {
2743 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09002744 if m, ok := module.(interface {
2745 CheckStableSdkVersion(ctx android.BaseModuleContext) error
2746 }); ok {
2747 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01002748 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
2749 }
2750 }
2751 }
2752 })
2753}
2754
satayevb98371c2021-06-15 16:49:50 +01002755// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002756func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
2757 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
Jooyung Han8d4a1f02023-08-23 13:54:08 +09002758 if a.testApex || a.vndkApex {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002759 return
2760 }
2761
2762 // Because APEXes targeting other than system/system_ext partitions can't set
2763 // apex_available, we skip checks for these APEXes
2764 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2765 return
2766 }
2767
Chan Wang490a6f92024-09-23 11:52:00 +00002768 // Temporarily bypass /product APEXes with a specific prefix.
2769 // TODO: b/352818241 - Remove this after APEX availability is enforced for /product APEXes.
2770 if a.ProductSpecific() && strings.HasPrefix(a.ApexVariationName(), "com.sdv.") {
2771 return
2772 }
2773
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002774 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
2775 // Requiring them and their transitive depencies with apex_available is not right
2776 // because they just add noise.
2777 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
2778 return
2779 }
2780
Colin Cross8bf14fc2024-09-25 16:41:31 -07002781 a.WalkPayloadDeps(ctx, func(ctx android.BaseModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002782 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2783 if externalDep {
2784 return false
2785 }
2786
2787 apexName := ctx.ModuleName()
Sam Delmericoca816532023-06-02 14:09:50 -04002788 for _, props := range ctx.Module().GetProperties() {
2789 if apexProps, ok := props.(*apexBundleProperties); ok {
2790 if apexProps.Apex_available_name != nil {
2791 apexName = *apexProps.Apex_available_name
2792 }
2793 }
2794 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002795 fromName := ctx.OtherModuleName(from)
2796 toName := ctx.OtherModuleName(to)
2797
2798 // If `to` is not actually in the same APEX as `from` then it does not need
2799 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002800 //
2801 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002802 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2803 // As soon as the dependency graph crosses the APEX boundary, don't go
2804 // further.
2805 return false
2806 }
2807
Jooyung Hana89a58a2024-08-29 13:00:33 +09002808 if to.AvailableFor(apexName) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002809 return true
2810 }
Jooyung Han9a419e22024-08-16 17:14:21 +09002811
2812 // Let's give some hint for apex_available
2813 hint := fmt.Sprintf("%q", apexName)
2814
2815 if strings.HasPrefix(apexName, "com.") && !strings.HasPrefix(apexName, "com.android.") && strings.Count(apexName, ".") >= 2 {
2816 // In case of a partner APEX, prefix format might be an option.
2817 components := strings.Split(apexName, ".")
2818 components[len(components)-1] = "*"
2819 hint += fmt.Sprintf(" or %q", strings.Join(components, "."))
2820 }
2821
Jiyong Park767dbd92021-03-04 13:03:10 +09002822 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
2823 "\n\nDependency path:%s\n\n"+
Jooyung Han9a419e22024-08-16 17:14:21 +09002824 "Consider adding %s to 'apex_available' property of %q",
2825 fromName, toName, ctx.GetPathString(true), hint, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002826 // Visit this module's dependencies to check and report any issues with their availability.
2827 return true
2828 })
2829}
2830
Jiyong Park192600a2021-08-03 07:52:17 +00002831// checkStaticExecutable ensures that executables in an APEX are not static.
2832func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Colin Cross648daea2024-09-12 14:35:29 -07002833 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park192600a2021-08-03 07:52:17 +00002834 if ctx.OtherModuleDependencyTag(module) != executableTag {
2835 return
2836 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09002837
2838 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00002839 apex := a.ApexVariationName()
2840 exec := ctx.OtherModuleName(module)
2841 if isStaticExecutableAllowed(apex, exec) {
2842 return
2843 }
2844 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
2845 }
2846 })
2847}
2848
2849// A small list of exceptions where static executables are allowed in APEXes.
2850func isStaticExecutableAllowed(apex string, exec string) bool {
2851 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07002852 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00002853 "linker",
2854 "linkerconfig",
2855 },
2856 }
2857 execNames, ok := m[apex]
2858 return ok && android.InList(exec, execNames)
2859}
2860
braleeb0c1f0c2021-06-07 22:49:13 +08002861// Collect information for opening IDE project files in java/jdeps.go.
Cole Faustb36d31d2024-08-27 16:04:28 -07002862func (a *apexBundle) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00002863 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
2864 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
Inseob Kimb1142342024-07-23 13:39:54 +09002865 dpInfo.Deps = append(dpInfo.Deps, a.properties.ResolvedSystemserverclasspathFragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08002866}
2867
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002868func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00002869 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
2870 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002871}
2872
Spandan Dasf14e2542021-11-12 00:01:37 +00002873func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
2874 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
2875 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002876 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00002877 With("name", jar).
2878 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
2879 Because(jar +
2880 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00002881 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00002882 " 1. If the offending code is from a statically linked library, consider " +
2883 "removing that dependency and using an alternative already in the " +
2884 "bootclasspath, or perhaps a shared library." +
2885 " 2. Move the offending code into an allowed package.\n" +
2886 " 3. Jarjar the offending code. Please be mindful of the potential system " +
2887 "health implications of bundling that code, particularly if the offending jar " +
2888 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00002889
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002890 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002891 }
2892 return rules
2893}
2894
Anton Hanssone1b18362021-12-23 15:05:38 +00002895// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002896// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00002897func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002898 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07002899 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002900 "android.net.ssl",
2901 "com.android.org.conscrypt",
2902 },
Wei Li40f98732022-05-20 22:08:11 -07002903 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002904 "android.media",
2905 },
2906 }
2907}
2908
Anton Hanssone1b18362021-12-23 15:05:38 +00002909// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002910// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00002911func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002912 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07002913 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002914 "android.provider",
2915 },
Wei Li40f98732022-05-20 22:08:11 -07002916 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002917 "android.permission",
2918 "android.app.role",
2919 "com.android.permission",
2920 "com.android.role",
2921 },
Wei Li40f98732022-05-20 22:08:11 -07002922 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002923 "android.os.ext",
2924 },
Wei Li40f98732022-05-20 22:08:11 -07002925 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002926 "android.app",
2927 "android.os",
2928 "android.util",
2929 "com.android.internal.statsd",
2930 "com.android.server.stats",
2931 },
Wei Li40f98732022-05-20 22:08:11 -07002932 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002933 "com.android.server.wifi",
2934 "com.android.wifi.x",
2935 "android.hardware.wifi",
2936 "android.net.wifi",
2937 },
Wei Li40f98732022-05-20 22:08:11 -07002938 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002939 "android.net",
2940 },
2941 }
2942}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04002943
Spandan Dasf57a9662023-04-12 19:05:49 +00002944func (a *apexBundle) IsTestApex() bool {
2945 return a.testApex
2946}
Colin Crossb614cd42024-10-11 12:52:21 -07002947
2948// verifyNativeImplementationLibs compares the list of transitive implementation libraries used to link native
2949// libraries in the apex against the list of implementation libraries in the apex, ensuring that none of the
2950// libraries in the apex have references to private APIs from outside the apex.
2951func (a *apexBundle) verifyNativeImplementationLibs(ctx android.ModuleContext) {
2952 var directImplementationLibs android.Paths
2953 var transitiveImplementationLibs []depset.DepSet[android.Path]
2954
2955 if a.properties.IsCoverageVariant {
2956 return
2957 }
2958
2959 if a.testApex {
2960 return
2961 }
2962
2963 if a.UsePlatformApis() {
2964 return
2965 }
2966
2967 checkApexTag := func(tag blueprint.DependencyTag) bool {
2968 switch tag {
2969 case sharedLibTag, jniLibTag, executableTag, androidAppTag:
2970 return true
2971 default:
2972 return false
2973 }
2974 }
2975
2976 checkTransitiveTag := func(tag blueprint.DependencyTag) bool {
2977 switch {
2978 case cc.IsSharedDepTag(tag), java.IsJniDepTag(tag), rust.IsRlibDepTag(tag), rust.IsDylibDepTag(tag), checkApexTag(tag):
2979 return true
2980 default:
2981 return false
2982 }
2983 }
2984
2985 var appEmbeddedJNILibs android.Paths
2986 ctx.VisitDirectDeps(func(dep android.Module) {
2987 tag := ctx.OtherModuleDependencyTag(dep)
2988 if !checkApexTag(tag) {
2989 return
2990 }
2991 if tag == sharedLibTag || tag == jniLibTag {
2992 outputFile := android.OutputFileForModule(ctx, dep, "")
2993 directImplementationLibs = append(directImplementationLibs, outputFile)
2994 }
2995 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
2996 transitiveImplementationLibs = append(transitiveImplementationLibs, info.ImplementationDeps)
2997 }
2998 if info, ok := android.OtherModuleProvider(ctx, dep, java.AppInfoProvider); ok {
2999 appEmbeddedJNILibs = append(appEmbeddedJNILibs, info.EmbeddedJNILibs...)
3000 }
3001 })
3002
3003 depSet := depset.New(depset.PREORDER, directImplementationLibs, transitiveImplementationLibs)
3004 allImplementationLibs := depSet.ToList()
3005
3006 allFileInfos := slices.Concat(a.filesInfo, a.unwantedTransitiveFilesInfo, a.duplicateTransitiveFilesInfo)
3007
3008 for _, lib := range allImplementationLibs {
3009 inApex := slices.ContainsFunc(allFileInfos, func(fi apexFile) bool {
3010 return fi.builtFile == lib
3011 })
3012 inApkInApex := slices.Contains(appEmbeddedJNILibs, lib)
3013
3014 if !inApex && !inApkInApex {
3015 ctx.ModuleErrorf("library in apex transitively linked against implementation library %q not in apex", lib)
3016 var depPath []android.Module
3017 ctx.WalkDeps(func(child, parent android.Module) bool {
3018 if depPath != nil {
3019 return false
3020 }
3021
3022 tag := ctx.OtherModuleDependencyTag(child)
3023
3024 if parent == ctx.Module() {
3025 if !checkApexTag(tag) {
3026 return false
3027 }
3028 }
3029
3030 if checkTransitiveTag(tag) {
3031 if android.OutputFileForModule(ctx, child, "") == lib {
3032 depPath = ctx.GetWalkPath()
3033 }
3034 return true
3035 }
3036
3037 return false
3038 })
3039 if depPath != nil {
3040 ctx.ModuleErrorf("dependency path:")
3041 for _, m := range depPath {
3042 ctx.ModuleErrorf(" %s", ctx.OtherModuleName(m))
3043 }
3044 return
3045 }
3046 }
3047 }
3048}