blob: b77568dd364e3c3bc5b302080b947aa0854c2fe8 [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"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090023 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024 "strings"
25
Yu Liu4c212ce2022-10-14 12:20:20 -070026 "android/soong/bazel/cquery"
27
Jiyong Park48ca7dc2018-10-10 14:01:00 +090028 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080029 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030 "github.com/google/blueprint/proptools"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070031
32 "android/soong/android"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040033 "android/soong/bazel"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 "android/soong/cc"
36 prebuilt_etc "android/soong/etc"
Jiyong Park12a719c2021-01-07 15:31:24 +090037 "android/soong/filesystem"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/java"
Inseob Kim5eb7ee92022-04-27 10:30:34 +090039 "android/soong/multitree"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040 "android/soong/python"
Jiyong Park99644e92020-11-17 22:21:02 +090041 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042 "android/soong/sh"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090043)
44
Jiyong Park8e6d52f2020-11-19 14:37:47 +090045func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000046 registerApexBuildComponents(android.InitRegistrationContext)
47}
Jiyong Park8e6d52f2020-11-19 14:37:47 +090048
Paul Duffin667893c2021-03-09 22:34:13 +000049func registerApexBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("apex", BundleFactory)
Yu Liu4c212ce2022-10-14 12:20:20 -070051 ctx.RegisterModuleType("apex_test", TestApexBundleFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000052 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
53 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
54 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Wei Li1c66fc72022-05-09 23:59:14 -070055 ctx.RegisterModuleType("override_apex", OverrideApexFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000056 ctx.RegisterModuleType("apex_set", apexSetFactory)
57
Paul Duffin5dda3e32021-05-05 14:13:27 +010058 ctx.PreArchMutators(registerPreArchMutators)
Paul Duffin667893c2021-03-09 22:34:13 +000059 ctx.PreDepsMutators(RegisterPreDepsMutators)
60 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090061}
62
Paul Duffin5dda3e32021-05-05 14:13:27 +010063func registerPreArchMutators(ctx android.RegisterMutatorsContext) {
64 ctx.TopDown("prebuilt_apex_module_creator", prebuiltApexModuleCreatorMutator).Parallel()
65}
66
Jiyong Park8e6d52f2020-11-19 14:37:47 +090067func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
68 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
69 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
70}
71
72func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Paul Duffin949abc02020-12-08 10:34:30 +000073 ctx.TopDown("apex_info", apexInfoMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090074 ctx.BottomUp("apex_unique", apexUniqueVariationsMutator).Parallel()
75 ctx.BottomUp("apex_test_for_deps", apexTestForDepsMutator).Parallel()
76 ctx.BottomUp("apex_test_for", apexTestForMutator).Parallel()
Paul Duffin28bf7ee2021-05-12 16:41:35 +010077 // Run mark_platform_availability before the apexMutator as the apexMutator needs to know whether
78 // it should create a platform variant.
79 ctx.BottomUp("mark_platform_availability", markPlatformAvailability).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090080 ctx.BottomUp("apex", apexMutator).Parallel()
81 ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).Parallel()
82 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Dennis Shene2ed70c2023-01-11 14:15:43 +000083 ctx.BottomUp("apex_dcla_deps", apexDCLADepsMutator).Parallel()
Spandan Das66773252022-01-15 00:23:18 +000084 // Register after apex_info mutator so that it can use ApexVariationName
85 ctx.TopDown("apex_strict_updatability_lint", apexStrictUpdatibilityLintMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090086}
87
88type apexBundleProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090089 // Json manifest file describing meta info of this APEX bundle. Refer to
90 // system/apex/proto/apex_manifest.proto for the schema. Default: "apex_manifest.json"
Jiyong Park8e6d52f2020-11-19 14:37:47 +090091 Manifest *string `android:"path"`
92
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090093 // AndroidManifest.xml file used for the zip container of this APEX bundle. If unspecified,
94 // a default one is automatically generated.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090095 AndroidManifest *string `android:"path"`
96
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090097 // Determines the file contexts file for setting the security contexts to files in this APEX
98 // bundle. For platform APEXes, this should points to a file under /system/sepolicy Default:
99 // /system/sepolicy/apex/<module_name>_file_contexts.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900100 File_contexts *string `android:"path"`
101
Jiyong Park038e8522021-12-13 23:56:35 +0900102 // Path to the canned fs config file for customizing file's uid/gid/mod/capabilities. The
103 // format is /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where path_or_glob is a
104 // path or glob pattern for a file or set of files, uid/gid are numerial values of user ID
105 // and group ID, mode is octal value for the file mode, and cap is hexadecimal value for the
106 // capability. If this property is not set, or a file is missing in the file, default config
107 // is used.
108 Canned_fs_config *string `android:"path"`
109
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900110 ApexNativeDependencies
111
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900112 Multilib apexMultilibProperties
113
Sundong Ahn80c04892021-11-23 00:57:19 +0000114 // List of sh binaries that are embedded inside this APEX bundle.
115 Sh_binaries []string
116
Paul Duffin3abc1742021-03-15 19:32:23 +0000117 // List of platform_compat_config files that are embedded inside this APEX bundle.
118 Compat_configs []string
119
Jiyong Park12a719c2021-01-07 15:31:24 +0900120 // List of filesystem images that are embedded inside this APEX bundle.
121 Filesystems []string
122
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900123 // Whether this APEX is considered updatable or not. When set to true, this will enforce
124 // additional rules for making sure that the APEX is truly updatable. To be updatable,
125 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000126 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900127 Updatable *bool
128
Jiyong Parkf4020582021-11-29 12:37:10 +0900129 // Marks that this APEX is designed to be updatable in the future, although it's not
130 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
131 // updatable APEXes. Currently, this disables the size optimization, so that the size of
132 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
133 // false.
134 Future_updatable *bool
135
Jiyong Park1bc84122021-06-22 20:23:05 +0900136 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
137 // false`. Default is false.
138 Platform_apis *bool
139
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900140 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
141 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900142 Installable *bool
143
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900144 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
145 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
146 Use_vndk_as_stable *bool
147
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900148 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
149 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
150 // container. When set to zip, contents are stored in a zip container directly. This type is
151 // mostly for host-side debugging. When set to both, the two types are both built. Default
152 // is 'image'.
153 Payload_type *string
154
Huang Jianan13cac632021-08-02 15:02:17 +0800155 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
156 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900157 Payload_fs_type *string
158
159 // For telling the APEX to ignore special handling for system libraries such as bionic.
160 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900161 Ignore_system_library_special_case *bool
162
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100163 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100164 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100165 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900166
167 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
168 // used in tests.
169 Test_only_unsigned_payload *bool
170
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000171 // Whenever apex should be compressed, regardless of product flag used. Should be only
172 // used in tests.
173 Test_only_force_compression *bool
174
Jooyung Han09c11ad2021-10-27 03:45:31 +0900175 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
176 // with the tool to sign payload contents.
177 Custom_sign_tool *string
178
Dennis Shenaf41bc12022-08-03 16:46:43 +0000179 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
180 // in a special way that include the digest of the lib file under /lib(64)?
181 Dynamic_common_lib_apex *bool
182
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100183 // Canonical name of this APEX bundle. Used to determine the path to the
184 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
185 // apex mutator variations. For override_apex modules, this is the name of the
186 // overridden base module.
187 ApexVariationName string `blueprint:"mutated"`
188
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900189 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900190
191 // List of sanitizer names that this APEX is enabled for
192 SanitizerNames []string `blueprint:"mutated"`
193
194 PreventInstall bool `blueprint:"mutated"`
195
196 HideFromMake bool `blueprint:"mutated"`
197
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900198 // Internal package method for this APEX. When payload_type is image, this can be either
199 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
200 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900201 ApexType apexPackaging `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900202}
203
204type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900205 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900206 Native_shared_libs []string
207
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900208 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900209 Jni_libs []string
210
Colin Cross70572ed2022-11-02 13:14:20 -0700211 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900212 Rust_dyn_libs []string
213
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900214 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900215 Binaries []string
216
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900217 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900218 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900219
220 // List of filesystem images that are embedded inside this APEX bundle.
221 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700222
223 // List of native libraries to exclude from this APEX.
224 Exclude_native_shared_libs []string
225
226 // List of JNI libraries to exclude from this APEX.
227 Exclude_jni_libs []string
228
229 // List of rust dyn libraries to exclude from this APEX.
230 Exclude_rust_dyn_libs []string
231
232 // List of native executables to exclude from this APEX.
233 Exclude_binaries []string
234
235 // List of native tests to exclude from this APEX.
236 Exclude_tests []string
237
238 // List of filesystem images to exclude from this APEX bundle.
239 Exclude_filesystems []string
240}
241
242// Merge combines another ApexNativeDependencies into this one
243func (a *ApexNativeDependencies) Merge(b ApexNativeDependencies) {
244 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs...)
245 a.Jni_libs = append(a.Jni_libs, b.Jni_libs...)
246 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
247 a.Binaries = append(a.Binaries, b.Binaries...)
248 a.Tests = append(a.Tests, b.Tests...)
249 a.Filesystems = append(a.Filesystems, b.Filesystems...)
250
251 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
252 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
253 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
254 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
255 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
256 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900257}
258
259type apexMultilibProperties struct {
260 // Native dependencies whose compile_multilib is "first"
261 First ApexNativeDependencies
262
263 // Native dependencies whose compile_multilib is "both"
264 Both ApexNativeDependencies
265
266 // Native dependencies whose compile_multilib is "prefer32"
267 Prefer32 ApexNativeDependencies
268
269 // Native dependencies whose compile_multilib is "32"
270 Lib32 ApexNativeDependencies
271
272 // Native dependencies whose compile_multilib is "64"
273 Lib64 ApexNativeDependencies
274}
275
276type apexTargetBundleProperties struct {
277 Target struct {
278 // Multilib properties only for android.
279 Android struct {
280 Multilib apexMultilibProperties
281 }
282
283 // Multilib properties only for host.
284 Host struct {
285 Multilib apexMultilibProperties
286 }
287
288 // Multilib properties only for host linux_bionic.
289 Linux_bionic struct {
290 Multilib apexMultilibProperties
291 }
292
293 // Multilib properties only for host linux_glibc.
294 Linux_glibc struct {
295 Multilib apexMultilibProperties
296 }
297 }
298}
299
Jiyong Park59140302020-12-14 18:44:04 +0900300type apexArchBundleProperties struct {
301 Arch struct {
302 Arm struct {
303 ApexNativeDependencies
304 }
305 Arm64 struct {
306 ApexNativeDependencies
307 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700308 Riscv64 struct {
309 ApexNativeDependencies
310 }
Jiyong Park59140302020-12-14 18:44:04 +0900311 X86 struct {
312 ApexNativeDependencies
313 }
314 X86_64 struct {
315 ApexNativeDependencies
316 }
317 }
318}
319
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900320// These properties can be used in override_apex to override the corresponding properties in the
321// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900322type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900323 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900324 Apps []string
325
Daniel Norman5a3ce132021-08-26 15:44:43 -0700326 // List of prebuilt files that are embedded inside this APEX bundle.
327 Prebuilts []string
328
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900329 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900330 Rros []string
331
markchien7c803b82021-08-26 22:10:06 +0800332 // List of BPF programs inside this APEX bundle.
333 Bpfs []string
334
Remi NGUYEN VANbe901722022-03-02 21:00:33 +0900335 // List of bootclasspath fragments that are embedded inside this APEX bundle.
336 Bootclasspath_fragments []string
337
338 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
339 Systemserverclasspath_fragments []string
340
341 // List of java libraries that are embedded inside this APEX bundle.
342 Java_libs []string
343
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900344 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
345 // Soong). This does not completely prevent installation of the overridden binaries, but if
346 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
347 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900348 Overrides []string
349
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900350 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900351 Logging_parent string
352
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900353 // Apex Container package name. Override value for attribute package:name in
354 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900355 Package_name string
356
357 // A txt file containing list of files that are allowed to be included in this APEX.
358 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700359
360 // Name of the apex_key module that provides the private key to sign this APEX bundle.
361 Key *string
362
363 // Specifies the certificate and the private key to sign the zip container of this APEX. If
364 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
365 // as the certificate and the private key, respectively. If this is ":module", then the
366 // certificate and the private key are provided from the android_app_certificate module
367 // named "module".
368 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400369
370 // Whether this APEX can be compressed or not. Setting this property to false means this
371 // APEX will never be compressed. When set to true, APEX will be compressed if other
372 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
373 // Default: false.
374 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000375
376 // Trim against a specific Dynamic Common Lib APEX
377 Trim_against *string
zhidou133c55b2023-01-31 19:34:10 +0000378
379 // The minimum SDK version that this APEX must support at minimum. This is usually set to
380 // the SDK version that the APEX was first introduced.
381 Min_sdk_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900382}
383
384type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900385 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900386 android.ModuleBase
387 android.DefaultableModuleBase
388 android.OverridableModuleBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400389 android.BazelModuleBase
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900390 multitree.ExportableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900391
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900392 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900393 properties apexBundleProperties
394 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900395 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900396 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900397 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900398
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900399 ///////////////////////////////////////////////////////////////////////////////////////////
400 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900401
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900402 // Keys for apex_paylaod.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800403 publicKeyFile android.Path
404 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900405
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900406 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800407 containerCertificateFile android.Path
408 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900409
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900410 // Flags for special variants of APEX
411 testApex bool
412 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900413
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900414 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
415 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900416 primaryApexType bool
417
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900418 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900419 suffix string
420
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900421 // File system type of apex_payload.img
422 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900423
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900424 // Whether to create symlink to the system file instead of having a file inside the apex or
425 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900426 linkToSystemLib bool
427
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900428 // List of files to be included in this APEX. This is filled in the first part of
429 // GenerateAndroidBuildActions.
430 filesInfo []apexFile
431
Jingwen Chen29743c82023-01-25 17:49:46 +0000432 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
433 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900434
435 ///////////////////////////////////////////////////////////////////////////////////////////
436 // Outputs (final and intermediates)
437
438 // Processed apex manifest in JSONson format (for Q)
439 manifestJsonOut android.WritablePath
440
441 // Processed apex manifest in PB format (for R+)
442 manifestPbOut android.WritablePath
443
444 // Processed file_contexts files
445 fileContexts android.WritablePath
446
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900447 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900448 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900449 outputFile android.WritablePath
450
Jooyung Hana6d36672022-02-24 13:58:07 +0900451 // The built uncompressed .apex file.
452 outputApexFile android.WritablePath
453
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900454 // The built APEX file in app bundle format. This file is not directly installed to the
455 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
456 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
457 // system) to be merged into a single app bundle file that Play accepts. See
458 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
459 bundleModuleFile android.WritablePath
460
Colin Cross6340ea52021-11-04 12:01:18 -0700461 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900462 installDir android.InstallPath
463
Colin Cross6340ea52021-11-04 12:01:18 -0700464 // Path where this APEX was installed.
465 installedFile android.InstallPath
466
467 // Installed locations of symlinks for backward compatibility.
468 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900469
470 // Text file having the list of individual files that are included in this APEX. Used for
471 // debugging purpose.
472 installedFilesFile android.WritablePath
473
474 // List of module names that this APEX is including (to be shown via *-deps-info target).
475 // Used for debugging purpose.
476 android.ApexBundleDepsInfo
477
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900478 // Optional list of lint report zip files for apexes that contain java or app modules
479 lintReports android.Paths
480
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000481 isCompressed bool
482
sophiezc80a2b32020-11-12 16:39:19 +0000483 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700484 nativeApisUsedByModuleFile android.ModuleOutPath
485 nativeApisBackedByModuleFile android.ModuleOutPath
486 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800487
488 // Collect the module directory for IDE info in java/jdeps.go.
489 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900490}
491
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900492// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900493type apexFileClass int
494
Jooyung Han72bd2f82019-10-23 16:46:38 +0900495const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900496 app apexFileClass = iota
497 appSet
498 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900499 goBinary
500 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900501 nativeExecutable
502 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900503 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900504 pyBinary
505 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900506)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900507
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900508// apexFile represents a file in an APEX bundle. This is created during the first half of
509// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
510// of the function, this is used to create commands that copies the files into a staging directory,
511// where they are packaged into the APEX file. This struct is also used for creating Make modules
512// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900513type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900514 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000515 builtFile android.Path
516 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900517 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000518 customStem string
519 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900520
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900521 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
522 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
523 // suffix>]
524 androidMkModuleName string // becomes LOCAL_MODULE
525 class apexFileClass // becomes LOCAL_MODULE_CLASS
526 moduleDir string // becomes LOCAL_PATH
527 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
528 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
529 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
530 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900531
532 jacocoReportClassesFile android.Path // only for javalibs and apps
533 lintDepSets java.LintDepSets // only for javalibs and apps
534 certificate java.Certificate // only for apps
535 overriddenPackageName string // only for apps
536
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900537 transitiveDep bool
538 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900539
Jiyong Park57621b22021-01-20 20:33:11 +0900540 multilib string
541
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900542 // TODO(jiyong): remove this
543 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900544}
545
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900546// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900547func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
548 ret := apexFile{
549 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900550 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900551 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900552 class: class,
553 module: module,
554 }
555 if module != nil {
556 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900557 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900558 ret.requiredModuleNames = module.RequiredModuleNames()
559 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
560 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900561 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900562 }
563 return ret
564}
565
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900566func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900567 return af.builtFile != nil && af.builtFile.String() != ""
568}
569
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900570// apexRelativePath returns the relative path of the given path from the install directory of this
571// apexFile.
572// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900573func (af *apexFile) apexRelativePath(path string) string {
574 return filepath.Join(af.installDir, path)
575}
576
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900577// path returns path of this apex file relative to the APEX root
578func (af *apexFile) path() string {
579 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900580}
581
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900582// stem returns the base filename of this apex file
583func (af *apexFile) stem() string {
584 if af.customStem != "" {
585 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900586 }
587 return af.builtFile.Base()
588}
589
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900590// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
591func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900592 var ret []string
593 for _, symlink := range af.symlinks {
594 ret = append(ret, af.apexRelativePath(symlink))
595 }
596 return ret
597}
598
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900599// availableToPlatform tests whether this apexFile is from a module that can be installed to the
600// platform.
601func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900602 if af.module == nil {
603 return false
604 }
605 if am, ok := af.module.(android.ApexModule); ok {
606 return am.AvailableFor(android.AvailableToPlatform)
607 }
608 return false
609}
610
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900611////////////////////////////////////////////////////////////////////////////////////////////////////
612// Mutators
613//
614// Brief description about mutators for APEX. The following three mutators are the most important
615// ones.
616//
617// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
618// to the (direct) dependencies of this APEX bundle.
619//
Paul Duffin949abc02020-12-08 10:34:30 +0000620// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900621// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
622// modules are marked as being included in the APEX via BuildForApex().
623//
Paul Duffin949abc02020-12-08 10:34:30 +0000624// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
625// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900626
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900627type dependencyTag struct {
628 blueprint.BaseDependencyTag
629 name string
630
631 // Determines if the dependent will be part of the APEX payload. Can be false for the
632 // dependencies to the signing key module, etc.
633 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000634
635 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
636 // replacement. This is needed because some prebuilt modules do not provide all the information
637 // needed by the apex.
638 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000639
640 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
641 // also be added as exported members of that SDK.
642 memberType android.SdkMemberType
643}
644
645func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
646 return d.memberType
647}
648
649func (d *dependencyTag) ExportMember() bool {
650 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900651}
652
Paul Duffin520917a2022-05-13 13:01:59 +0000653func (d *dependencyTag) String() string {
654 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
655}
656
657func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000658 return !d.sourceOnly
659}
660
661var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000662var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000663
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900664var (
Paul Duffin520917a2022-05-13 13:01:59 +0000665 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
666 bpfTag = &dependencyTag{name: "bpf", payload: true}
667 certificateTag = &dependencyTag{name: "certificate"}
Dennis Shene2ed70c2023-01-11 14:15:43 +0000668 dclaTag = &dependencyTag{name: "dcla"}
Paul Duffin520917a2022-05-13 13:01:59 +0000669 executableTag = &dependencyTag{name: "executable", payload: true}
670 fsTag = &dependencyTag{name: "filesystem", payload: true}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000671 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
672 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType}
Paul Duffinfcf79852022-07-20 14:18:24 +0000673 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000674 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
675 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
676 keyTag = &dependencyTag{name: "key"}
677 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
678 rroTag = &dependencyTag{name: "rro", payload: true}
679 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
680 testForTag = &dependencyTag{name: "test for"}
681 testTag = &dependencyTag{name: "test", payload: true}
682 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900683)
684
685// TODO(jiyong): shorten this function signature
686func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900687 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900688 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900689 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900690
691 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900692 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900693 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
694 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900695 }
696
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900697 // Use *FarVariation* to be able to depend on modules having conflicting variations with
698 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
699 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700700 ctx.AddFarVariationDependencies(binVariations, executableTag,
701 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
702 ctx.AddFarVariationDependencies(binVariations, testTag,
703 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
704 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
705 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
706 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
707 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
708 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
709 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
710 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
711 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900712}
713
714func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900715 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900716 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
717 } else {
718 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
719 if ctx.Os().Bionic() {
720 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
721 } else {
722 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
723 }
724 }
725}
726
Jooyung Hand045ebc2022-12-06 15:23:57 +0900727// getImageVariationPair returns a pair for the image variation name as its
728// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
729// suffix indicates the vndk version when it's vendor or product.
730// getImageVariation can simply join the result of this function to get the
731// image variation name.
732func (a *apexBundle) getImageVariationPair(deviceConfig android.DeviceConfig) (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900733 if a.vndkApex {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900734 return cc.VendorVariationPrefix, a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900735 }
736
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900737 var prefix string
738 var vndkVersion string
739 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000740 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900741 prefix = cc.VendorVariationPrefix
742 vndkVersion = deviceConfig.VndkVersion()
743 } else if a.ProductSpecific() {
744 prefix = cc.ProductVariationPrefix
745 vndkVersion = deviceConfig.ProductVndkVersion()
746 }
747 }
748 if vndkVersion == "current" {
749 vndkVersion = deviceConfig.PlatformVndkVersion()
750 }
751 if vndkVersion != "" {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900752 return prefix, vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900753 }
754
Jooyung Hand045ebc2022-12-06 15:23:57 +0900755 return android.CoreVariation, "" // The usual case
756}
757
758// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
759// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
760func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
761 prefix, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig())
762 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900763}
764
765func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900766 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
767 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
768 // each target os/architectures, appropriate dependencies are selected by their
769 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900770 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900771 imageVariation := a.getImageVariation(ctx)
772
773 a.combineProperties(ctx)
774
775 has32BitTarget := false
776 for _, target := range targets {
777 if target.Arch.ArchType.Multilib == "lib32" {
778 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000779 }
780 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900781 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900782 // Don't include artifacts for the host cross targets because there is no way for us
783 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900784 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900785 continue
786 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000787
Colin Cross70572ed2022-11-02 13:14:20 -0700788 var deps ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000789
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900790 // Add native modules targeting both ABIs. When multilib.* is omitted for
791 // native_shared_libs/jni_libs/tests, it implies multilib.both
Colin Cross70572ed2022-11-02 13:14:20 -0700792 deps.Merge(a.properties.Multilib.Both)
793 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900794 Native_shared_libs: a.properties.Native_shared_libs,
795 Tests: a.properties.Tests,
796 Jni_libs: a.properties.Jni_libs,
797 Binaries: nil,
798 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900799
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900800 // Add native modules targeting the first ABI When multilib.* is omitted for
801 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900802 isPrimaryAbi := i == 0
803 if isPrimaryAbi {
Colin Cross70572ed2022-11-02 13:14:20 -0700804 deps.Merge(a.properties.Multilib.First)
805 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900806 Native_shared_libs: nil,
807 Tests: nil,
808 Jni_libs: nil,
809 Binaries: a.properties.Binaries,
810 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900811 }
812
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900813 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900814 switch target.Arch.ArchType.Multilib {
815 case "lib32":
Colin Cross70572ed2022-11-02 13:14:20 -0700816 deps.Merge(a.properties.Multilib.Lib32)
817 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900818 case "lib64":
Colin Cross70572ed2022-11-02 13:14:20 -0700819 deps.Merge(a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900820 if !has32BitTarget {
Colin Cross70572ed2022-11-02 13:14:20 -0700821 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900822 }
823 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900824
Jiyong Park59140302020-12-14 18:44:04 +0900825 // Add native modules targeting a specific arch variant
826 switch target.Arch.ArchType {
827 case android.Arm:
Colin Cross70572ed2022-11-02 13:14:20 -0700828 deps.Merge(a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900829 case android.Arm64:
Colin Cross70572ed2022-11-02 13:14:20 -0700830 deps.Merge(a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700831 case android.Riscv64:
Colin Cross70572ed2022-11-02 13:14:20 -0700832 deps.Merge(a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900833 case android.X86:
Colin Cross70572ed2022-11-02 13:14:20 -0700834 deps.Merge(a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900835 case android.X86_64:
Colin Cross70572ed2022-11-02 13:14:20 -0700836 deps.Merge(a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900837 default:
838 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
839 }
840
Colin Cross70572ed2022-11-02 13:14:20 -0700841 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Sundong Ahn80c04892021-11-23 00:57:19 +0000842 ctx.AddFarVariationDependencies([]blueprint.Variation{
843 {Mutator: "os", Variation: target.OsVariation()},
844 {Mutator: "arch", Variation: target.ArchVariation()},
845 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900846 }
847
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900848 // Common-arch dependencies come next
849 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Jiyong Park12a719c2021-01-07 15:31:24 +0900850 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000851 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100852}
853
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900854// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900855func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
856 if a.overridableProperties.Allowed_files != nil {
857 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100858 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900859
860 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
861 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800862 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900863 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.overridableProperties.Rros...)
Remi NGUYEN VANbe901722022-03-02 21:00:33 +0900864 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.overridableProperties.Bootclasspath_fragments...)
865 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.overridableProperties.Systemserverclasspath_fragments...)
866 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.overridableProperties.Java_libs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700867 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
868 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
869 // regardless of the TARGET_PREFER_* setting. See b/144532908
870 arches := ctx.DeviceConfig().Arches()
871 if len(arches) != 0 {
872 archForPrebuiltEtc := arches[0]
873 for _, arch := range arches {
874 // Prefer 64-bit arch if there is any
875 if arch.ArchType.Multilib == "lib64" {
876 archForPrebuiltEtc = arch
877 break
878 }
879 }
880 ctx.AddFarVariationDependencies([]blueprint.Variation{
881 {Mutator: "os", Variation: ctx.Os().String()},
882 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
883 }, prebuiltTag, prebuilts...)
884 }
885 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700886
887 // Dependencies for signing
888 if String(a.overridableProperties.Key) == "" {
889 ctx.PropertyErrorf("key", "missing")
890 return
891 }
892 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
893
894 cert := android.SrcIsModule(a.getCertString(ctx))
895 if cert != "" {
896 ctx.AddDependency(ctx.Module(), certificateTag, cert)
897 // empty cert is not an error. Cert and private keys will be directly found under
898 // PRODUCT_DEFAULT_DEV_CERTIFICATE
899 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100900}
901
Dennis Shene2ed70c2023-01-11 14:15:43 +0000902func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
903 if !mctx.Config().ApexTrimEnabled() {
904 return
905 }
906 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
907 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
908 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
909 } else if o, ok := mctx.Module().(*OverrideApex); ok {
910 for _, p := range o.GetProperties() {
911 properties, ok := p.(*overridableProperties)
912 if !ok {
913 continue
914 }
915 if properties.Trim_against != nil {
916 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
917 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
918 }
919 }
920 }
921}
922
923type DCLAInfo struct {
924 ProvidedLibs []string
925}
926
927var DCLAInfoProvider = blueprint.NewMutatorProvider(DCLAInfo{}, "apex_info")
928
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900929type ApexBundleInfo struct {
930 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100931}
932
Paul Duffin949abc02020-12-08 10:34:30 +0000933var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900934
Paul Duffina7d6a892020-12-07 17:39:59 +0000935var _ ApexInfoMutator = (*apexBundle)(nil)
936
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100937func (a *apexBundle) ApexVariationName() string {
938 return a.properties.ApexVariationName
939}
940
Paul Duffina7d6a892020-12-07 17:39:59 +0000941// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900942// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
943// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
944// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
945// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000946//
947// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
948// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
949// The apexMutator uses that list to create module variants for the apexes to which it belongs.
950// The relationship between module variants and apexes is not one-to-one as variants will be
951// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000952func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900953
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900954 // The VNDK APEX is special. For the APEX, the membership is described in a very different
955 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
956 // libraries are self-identified by their vndk.enabled properties. There is no need to run
957 // this mutator for the APEX as nothing will be collected. So, let's return fast.
958 if a.vndkApex {
959 return
960 }
961
962 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
963 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
964 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
965 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
966 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900967 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
968 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +0900969 if proptools.Bool(a.properties.Use_vndk_as_stable) {
970 if !useVndk {
971 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
972 }
973 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
974 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
975 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
976 }
977 })
978 if mctx.Failed() {
979 return
980 }
Jooyung Handf78e212020-07-22 15:54:47 +0900981 }
982
Colin Cross56a83212020-09-15 18:30:11 -0700983 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900984 am, ok := child.(android.ApexModule)
985 if !ok || !am.CanHaveApexVariants() {
986 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +0900987 }
Paul Duffin573989d2021-03-17 13:25:29 +0000988 depTag := mctx.OtherModuleDependencyTag(child)
989
990 // Check to see if the tag always requires that the child module has an apex variant for every
991 // apex variant of the parent module. If it does not then it is still possible for something
992 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
993 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
994 return true
995 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000996 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900997 return false
998 }
Jooyung Handf78e212020-07-22 15:54:47 +0900999 if excludeVndkLibs {
1000 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
1001 return false
1002 }
1003 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001004 // By default, all the transitive dependencies are collected, unless filtered out
1005 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001006 return true
1007 }
1008
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001009 // Records whether a certain module is included in this apexBundle via direct dependency or
1010 // inndirect dependency.
1011 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001012 mctx.WalkDeps(func(child, parent android.Module) bool {
1013 if !continueApexDepsWalk(child, parent) {
1014 return false
1015 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001016 // If the parent is apexBundle, this child is directly depended.
1017 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001018 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001019 contents[depName] = contents[depName].Add(directDep)
1020 return true
1021 })
1022
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001023 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001024 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -07001025 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
1026 Contents: apexContents,
1027 })
1028
Jooyung Haned124c32021-01-26 11:43:46 +09001029 minSdkVersion := a.minSdkVersion(mctx)
1030 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1031 if minSdkVersion.IsNone() {
1032 minSdkVersion = android.FutureApiLevel
1033 }
1034
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001035 // This is the main part of this mutator. Mark the collected dependencies that they need to
1036 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001037
Jooyung Han63dff462023-02-09 00:11:27 +00001038 apexVariationName := mctx.ModuleName() // could be com.android.foo
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001039 a.properties.ApexVariationName = apexVariationName
Colin Cross56a83212020-09-15 18:30:11 -07001040 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001041 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001042 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001043 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001044 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001045 InApexVariants: []string{apexVariationName},
1046 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001047 ApexContents: []*android.ApexContents{apexContents},
1048 }
Colin Cross56a83212020-09-15 18:30:11 -07001049 mctx.WalkDeps(func(child, parent android.Module) bool {
1050 if !continueApexDepsWalk(child, parent) {
1051 return false
1052 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001053 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001054 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001055 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001056
1057 if a.dynamic_common_lib_apex() {
1058 mctx.SetProvider(DCLAInfoProvider, DCLAInfo{
1059 ProvidedLibs: a.properties.Native_shared_libs,
1060 })
1061 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001062}
1063
Paul Duffina7d6a892020-12-07 17:39:59 +00001064type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001065 // ApexVariationName returns the name of the APEX variation to use in the apex
1066 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1067 ApexVariationName() string
1068
Paul Duffina7d6a892020-12-07 17:39:59 +00001069 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1070 // depended upon by an apex and which require an apex specific variant.
1071 ApexInfoMutator(android.TopDownMutatorContext)
1072}
1073
1074// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1075// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001076// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001077func apexInfoMutator(mctx android.TopDownMutatorContext) {
1078 if !mctx.Module().Enabled() {
1079 return
1080 }
1081
1082 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1083 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001084 }
Spandan Das42e89502022-05-06 22:12:55 +00001085 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001086}
1087
Spandan Das66773252022-01-15 00:23:18 +00001088// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
1089// This check is enforced for updatable modules
1090func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
1091 if !mctx.Module().Enabled() {
1092 return
1093 }
Spandan Das08c911f2022-01-21 22:07:26 +00001094 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +00001095 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +00001096 // b/208656169 Do not propagate strict updatability linting to libcore/
1097 // These libs are available on the classpath during compilation
1098 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1099 // Only skip libraries defined in libcore root, not subdirectories
1100 if mctx.OtherModuleDir(child) == "libcore" {
1101 // Do not traverse transitive deps of libcore/ libs
1102 return false
1103 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001104 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1105 return false
1106 }
Spandan Das66773252022-01-15 00:23:18 +00001107 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1108 lintable.SetStrictUpdatabilityLinting(true)
1109 }
1110 // visit transitive deps
1111 return true
1112 })
1113 }
1114}
1115
Spandan Das42e89502022-05-06 22:12:55 +00001116// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1117func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1118 if !mctx.Module().Enabled() {
1119 return
1120 }
1121 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1122 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1123 mctx.VisitDirectDeps(func(module android.Module) {
1124 // ignore android_test_app
1125 if app, ok := module.(*java.AndroidApp); ok {
1126 app.SetUpdatable(true)
1127 }
1128 })
1129 }
1130}
1131
Spandan Das08c911f2022-01-21 22:07:26 +00001132// TODO: b/215736885 Whittle the denylist
1133// Transitive deps of certain mainline modules baseline NewApi errors
1134// Skip these mainline modules for now
1135var (
1136 skipStrictUpdatabilityLintAllowlist = []string{
1137 "com.android.art",
1138 "com.android.art.debug",
1139 "com.android.conscrypt",
1140 "com.android.media",
1141 // test apexes
1142 "test_com.android.art",
1143 "test_com.android.conscrypt",
1144 "test_com.android.media",
1145 "test_jitzygote_com.android.art",
1146 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001147
1148 // TODO: b/215736885 Remove this list
1149 skipLintJavalibAllowlist = []string{
1150 "conscrypt.module.platform.api.stubs",
1151 "conscrypt.module.public.api.stubs",
1152 "conscrypt.module.public.api.stubs.system",
1153 "conscrypt.module.public.api.stubs.module_lib",
1154 "framework-media.stubs",
1155 "framework-media.stubs.system",
1156 "framework-media.stubs.module_lib",
1157 }
Spandan Das08c911f2022-01-21 22:07:26 +00001158)
1159
1160func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1161 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1162}
1163
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001164// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1165// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1166// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001167func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1168 if !mctx.Module().Enabled() {
1169 return
1170 }
1171 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001172 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1173 }
1174}
1175
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001176// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1177// the apex in order to retrieve its contents later.
1178// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001179func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1180 if !mctx.Module().Enabled() {
1181 return
1182 }
Colin Cross56a83212020-09-15 18:30:11 -07001183 if am, ok := mctx.Module().(android.ApexModule); ok {
1184 if testFor := am.TestFor(); len(testFor) > 0 {
1185 mctx.AddFarVariationDependencies([]blueprint.Variation{
1186 {Mutator: "os", Variation: am.Target().OsVariation()},
1187 {"arch", "common"},
1188 }, testForTag, testFor...)
1189 }
1190 }
1191}
1192
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001193// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001194func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1195 if !mctx.Module().Enabled() {
1196 return
1197 }
Colin Cross56a83212020-09-15 18:30:11 -07001198 if _, ok := mctx.Module().(android.ApexModule); ok {
1199 var contents []*android.ApexContents
1200 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1201 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1202 contents = append(contents, abInfo.Contents)
1203 }
1204 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1205 ApexContents: contents,
1206 })
Colin Crossaede88c2020-08-11 12:17:01 -07001207 }
1208}
1209
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001210// markPlatformAvailability marks whether or not a module can be available to platform. A module
1211// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1212// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1213// be) available to platform
1214// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001215func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1216 // Host and recovery are not considered as platform
1217 if mctx.Host() || mctx.Module().InstallInRecovery() {
1218 return
1219 }
1220
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001221 am, ok := mctx.Module().(android.ApexModule)
1222 if !ok {
1223 return
1224 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001225
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001226 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001227
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001228 // If any of the dep is not available to platform, this module is also considered as being
1229 // not available to platform even if it has "//apex_available:platform"
1230 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001231 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001232 // if the dependency crosses apex boundary, don't consider it
1233 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001234 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001235 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1236 availableToPlatform = false
1237 // TODO(b/154889534) trigger an error when 'am' has
1238 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001239 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001240 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001241
Paul Duffinb5769c12021-05-12 16:16:51 +01001242 // Exception 1: check to see if the module always requires it.
1243 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001244 availableToPlatform = true
1245 }
1246
1247 // Exception 2: bootstrap bionic libraries are also always available to platform
1248 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1249 availableToPlatform = true
1250 }
1251
1252 if !availableToPlatform {
1253 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001254 }
1255}
1256
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001257// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001258// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001259func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001260 if !mctx.Module().Enabled() {
1261 return
1262 }
Colin Cross56a83212020-09-15 18:30:11 -07001263
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001264 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001265 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001266 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001267 return
1268 }
1269
1270 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001271 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1272 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001273 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001274 if strings.HasPrefix(apexBundleName, "com.android.art") {
1275 // Create an alias from the platform variant. This is done to make
1276 // test_for dependencies work for modules that are split by the APEX
1277 // mutator, since test_for dependencies always go to the platform variant.
1278 // This doesn't happen for normal APEXes that are disjunct, so only do
1279 // this for the overlapping ART APEXes.
1280 // TODO(b/183882457): Remove this if the test_for functionality is
1281 // refactored to depend on the proper APEX variants instead of platform.
1282 mctx.CreateAliasVariation("", apexBundleName)
1283 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001284 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1285 apexBundleName := o.GetOverriddenModuleName()
1286 if apexBundleName == "" {
1287 mctx.ModuleErrorf("base property is not set")
1288 return
1289 }
1290 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001291 if strings.HasPrefix(apexBundleName, "com.android.art") {
1292 // TODO(b/183882457): See note for CreateAliasVariation above.
1293 mctx.CreateAliasVariation("", apexBundleName)
1294 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001295 }
1296}
Sundong Ahne9b55722019-09-06 17:37:42 +09001297
Paul Duffin6717d882021-06-15 19:09:41 +01001298// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1299// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001300func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001301 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001302 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001303 return !a.vndkApex
1304 }
1305
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001306 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001307}
1308
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001309// See android.UpdateDirectlyInAnyApex
1310// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001311func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1312 if !mctx.Module().Enabled() {
1313 return
1314 }
1315 if am, ok := mctx.Module().(android.ApexModule); ok {
1316 android.UpdateDirectlyInAnyApex(mctx, am)
1317 }
1318}
1319
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001320// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001321type apexPackaging int
1322
1323const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001324 // imageApex is a packaging method where contents are included in a filesystem image which
1325 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001326 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001327
1328 // zipApex is a packaging method where contents are directly included in the zip container.
1329 // This is used for host-side testing - because the contents are easily accessible by
1330 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001331 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001332
1333 // flattendApex is a packaging method where contents are not included in the APEX file, but
1334 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1335 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001336 flattenedApex
1337)
1338
1339const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001340 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001341 imageApexSuffix = ".apex"
1342 imageCapexSuffix = ".capex"
1343 zipApexSuffix = ".zipapex"
1344 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001345
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001346 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001347 imageApexType = "image"
1348 zipApexType = "zip"
1349 flattenedApexType = "flattened"
1350
Dan Willemsen47e1a752021-10-16 18:36:13 -07001351 ext4FsType = "ext4"
1352 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001353 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001354)
1355
1356// The suffix for the output "file", not the module
1357func (a apexPackaging) suffix() string {
1358 switch a {
1359 case imageApex:
1360 return imageApexSuffix
1361 case zipApex:
1362 return zipApexSuffix
1363 default:
1364 panic(fmt.Errorf("unknown APEX type %d", a))
1365 }
1366}
1367
1368func (a apexPackaging) name() string {
1369 switch a {
1370 case imageApex:
1371 return imageApexType
1372 case zipApex:
1373 return zipApexType
1374 default:
1375 panic(fmt.Errorf("unknown APEX type %d", a))
1376 }
1377}
1378
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001379// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1380// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001381func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001382 if !mctx.Module().Enabled() {
1383 return
1384 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001385 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001386 var variants []string
1387 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1388 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001389 // This is the normal case. Note that both image and flattend APEXes are
1390 // created. The image type is installed to the system partition, while the
1391 // flattened APEX is (optionally) installed to the system_ext partition.
1392 // This is mostly for GSI which has to support wide range of devices. If GSI
1393 // is installed on a newer (APEX-capable) device, the image APEX in the
1394 // system will be used. However, if the same GSI is installed on an old
1395 // device which can't support image APEX, the flattened APEX in the
1396 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001397 variants = append(variants, imageApexType, flattenedApexType)
1398 case "zip":
1399 variants = append(variants, zipApexType)
1400 case "both":
1401 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1402 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001403 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001404 return
1405 }
1406
1407 modules := mctx.CreateLocalVariations(variants...)
1408
1409 for i, v := range variants {
1410 switch v {
1411 case imageApexType:
1412 modules[i].(*apexBundle).properties.ApexType = imageApex
1413 case zipApexType:
1414 modules[i].(*apexBundle).properties.ApexType = zipApex
1415 case flattenedApexType:
1416 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001417 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001418 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001419 modules[i].(*apexBundle).MakeAsSystemExt()
1420 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001421 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001422 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001423 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001424 // payload_type is forcibly overridden to "image"
1425 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001426 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001427 }
1428}
1429
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001430var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001431
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001432// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001433func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001434 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001435 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001436 return true
1437}
1438
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001439var _ android.OutputFileProducer = (*apexBundle)(nil)
1440
1441// Implements android.OutputFileProducer
1442func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1443 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001444 case "", android.DefaultDistTag:
1445 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001446 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001447 case imageApexSuffix:
1448 // uncompressed one
1449 if a.outputApexFile != nil {
1450 return android.Paths{a.outputApexFile}, nil
1451 }
1452 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001453 default:
1454 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1455 }
1456}
1457
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001458var _ multitree.Exportable = (*apexBundle)(nil)
1459
1460func (a *apexBundle) Exportable() bool {
1461 if a.properties.ApexType == flattenedApex {
1462 return false
1463 }
1464 return true
1465}
1466
1467func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1468 ret := make(map[string]android.Paths)
1469 ret["apex"] = android.Paths{a.outputFile}
1470 return ret
1471}
1472
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001473var _ cc.Coverage = (*apexBundle)(nil)
1474
1475// Implements cc.Coverage
1476func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1477 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1478}
1479
1480// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001481func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001482 a.properties.PreventInstall = true
1483}
1484
1485// Implements cc.Coverage
1486func (a *apexBundle) HideFromMake() {
1487 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001488 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1489 // TODO(ccross): untangle these
1490 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001491}
1492
1493// Implements cc.Coverage
1494func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1495 a.properties.IsCoverageVariant = coverage
1496}
1497
1498// Implements cc.Coverage
1499func (a *apexBundle) EnableCoverageIfNeeded() {}
1500
1501var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1502
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001503// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001504func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001505 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001506}
1507
Jiyong Parkf4020582021-11-29 12:37:10 +09001508func (a *apexBundle) FutureUpdatable() bool {
1509 return proptools.BoolDefault(a.properties.Future_updatable, false)
1510}
1511
Jiyong Park1bc84122021-06-22 20:23:05 +09001512func (a *apexBundle) UsePlatformApis() bool {
1513 return proptools.BoolDefault(a.properties.Platform_apis, false)
1514}
1515
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001516// getCertString returns the name of the cert that should be used to sign this APEX. This is
1517// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001518func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001519 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001520 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1521 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1522 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001523 if a.vndkApex {
1524 moduleName = vndkApexName
1525 }
1526 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001527 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001528 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001529 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001530 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001531}
1532
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001533// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001534func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001535 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001536}
1537
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001538// See the generate_hashtree property
1539func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001540 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001541}
1542
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001543// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001544func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1545 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1546}
1547
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001548// See the test_only_force_compression property
1549func (a *apexBundle) testOnlyShouldForceCompression() bool {
1550 return proptools.Bool(a.properties.Test_only_force_compression)
1551}
1552
Dennis Shenaf41bc12022-08-03 16:46:43 +00001553// See the dynamic_common_lib_apex property
1554func (a *apexBundle) dynamic_common_lib_apex() bool {
1555 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1556}
1557
Dennis Shene2ed70c2023-01-11 14:15:43 +00001558// See the list of libs to trim
1559func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1560 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1561 if len(dclaModules) > 1 {
1562 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1563 }
1564 if len(dclaModules) > 0 {
1565 DCLAInfo := ctx.OtherModuleProvider(dclaModules[0], DCLAInfoProvider).(DCLAInfo)
1566 return DCLAInfo.ProvidedLibs
1567 }
1568 return []string{}
1569}
1570
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001571// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1572// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1573// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001574
Jiyong Parkf97782b2019-02-13 20:28:58 +09001575func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1576 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1577 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1578 }
1579}
1580
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001581func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001582 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1583 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001584 }
1585
1586 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001587 var globalSanitizerNames []string
Jiyong Park388ef3f2019-01-28 19:47:32 +09001588 if a.Host() {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001589 globalSanitizerNames = config.SanitizeHost()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001590 } else {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001591 arches := config.SanitizeDeviceArch()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001592 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001593 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001594 }
1595 }
1596 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001597}
1598
Jooyung Han8ce8db92020-05-15 19:05:05 +09001599func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001600 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1601 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001602 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001603 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604 for _, target := range ctx.MultiTargets() {
1605 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001606 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001607 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001608 Tests: nil,
1609 Jni_libs: nil,
1610 Binaries: nil,
1611 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001612 break
1613 }
1614 }
1615 }
1616}
1617
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001618// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1619// returned apexFile saves information about the Soong module that will be used for creating the
1620// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001621func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001622 // Decide the APEX-local directory by the multilib of the library In the future, we may
1623 // query this to the module.
1624 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001625 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001626 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001627 case "lib32":
1628 dirInApex = "lib"
1629 case "lib64":
1630 dirInApex = "lib64"
1631 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001632 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001633 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001634 }
Jooyung Han35155c42020-02-06 17:33:20 +09001635 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park1833cef2019-12-13 13:28:36 +09001636 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001637 // Special case for Bionic libs and other libs installed with them. This is to
1638 // prevent those libs from being included in the search path
1639 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1640 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1641 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1642 // will be loaded into the default linker namespace (aka "platform" namespace). If
1643 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1644 // be loaded again into the runtime linker namespace, which will result in double
1645 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001646 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001647 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001648
Colin Cross1d487152022-10-03 19:14:46 -07001649 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001650 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1651 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001652}
1653
Jiyong Park1833cef2019-12-13 13:28:36 +09001654func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001655 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001656 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001657 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001658 }
Jooyung Han35155c42020-02-06 17:33:20 +09001659 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001660 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001661 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1662 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001663 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001664 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001665 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001666}
1667
Jiyong Park99644e92020-11-17 22:21:02 +09001668func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1669 dirInApex := "bin"
1670 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1671 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1672 }
Colin Cross1d487152022-10-03 19:14:46 -07001673 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001674 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1675 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1676 return af
1677}
1678
1679func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1680 // Decide the APEX-local directory by the multilib of the library
1681 // In the future, we may query this to the module.
1682 var dirInApex string
1683 switch rustm.Arch().ArchType.Multilib {
1684 case "lib32":
1685 dirInApex = "lib"
1686 case "lib64":
1687 dirInApex = "lib64"
1688 }
1689 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1690 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1691 }
Colin Cross1d487152022-10-03 19:14:46 -07001692 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001693 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1694 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1695}
1696
Cole Faust4d247e62023-01-23 10:14:58 -08001697func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.PythonBinaryModule) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001698 dirInApex := "bin"
1699 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001700 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001701}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001702
Jiyong Park1833cef2019-12-13 13:28:36 +09001703func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001704 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001705 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001706 // NB: Since go binaries are static we don't need the module for anything here, which is
1707 // good since the go tool is a blueprint.Module not an android.Module like we would
1708 // normally use.
Jiyong Park1833cef2019-12-13 13:28:36 +09001709 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001710}
1711
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001712func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001713 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001714 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1715 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1716 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001717 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001718 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001719 af.symlinks = sh.Symlinks()
1720 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001721}
1722
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001723func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001724 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001725 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001726 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001727}
1728
atrost6e126252020-01-27 17:01:16 +00001729func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1730 dirInApex := filepath.Join("etc", config.SubDir())
1731 fileToCopy := config.CompatConfig()
1732 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1733}
1734
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001735// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1736// way.
1737type javaModule interface {
1738 android.Module
1739 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001740 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001741 JacocoReportClassesFile() android.Path
1742 LintDepSets() java.LintDepSets
1743 Stem() string
1744}
1745
1746var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001747var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001748var _ javaModule = (*java.SdkLibrary)(nil)
1749var _ javaModule = (*java.DexImport)(nil)
1750var _ javaModule = (*java.SdkLibraryImport)(nil)
1751
Paul Duffin190fdef2021-04-26 10:33:59 +01001752// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001753func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001754 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001755}
1756
1757// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1758func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001759 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001760 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001761 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1762 af.lintDepSets = module.LintDepSets()
1763 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001764 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1765 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1766 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1767 }
1768 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001769 return af
1770}
1771
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001772func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1773 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001774 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001775 dirInApex := "javalib"
1776 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1777 af.customStem = module.Stem() + ".jar.prof"
1778 return &af
1779 }
1780 }
1781 return nil
1782}
1783
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001784// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1785// the same way.
1786type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001787 android.Module
1788 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001789 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001790 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001791 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001792 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001793 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001794 LintDepSets() java.LintDepSets
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001795}
1796
1797var _ androidApp = (*java.AndroidApp)(nil)
1798var _ androidApp = (*java.AndroidAppImport)(nil)
1799
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001800func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1801 buildId := ctx.Config().BuildId()
1802
1803 // The build ID is used as a suffix for a filename, so ensure that
1804 // the set of characters being used are sanitized.
1805 // - any word character: [a-zA-Z0-9_]
1806 // - dots: .
1807 // - dashes: -
1808 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1809 if !validRegex.MatchString(buildId) {
1810 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1811 }
1812 return buildId
1813}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001814
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001815func apexFileForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001816 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001817 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001818 appDir = "priv-app"
1819 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001820
1821 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1822 // so that PackageManager correctly invalidates the existing installed apk
1823 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001824 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001825 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001826
Yo Chiange8128052020-07-23 20:09:18 +08001827 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001828 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001829 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001830 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001831
1832 if app, ok := aapp.(interface {
1833 OverriddenManifestPackageName() string
1834 }); ok {
1835 af.overriddenPackageName = app.OverriddenManifestPackageName()
1836 }
Jiyong Park618922e2020-01-08 13:35:43 +09001837 return af
Dario Frenicde2a032019-10-27 00:29:22 +01001838}
1839
Jiyong Park69aeba92020-04-24 21:16:36 +09001840func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1841 rroDir := "overlay"
1842 dirInApex := filepath.Join(rroDir, rro.Theme())
1843 fileToCopy := rro.OutputFile()
1844 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1845 af.certificate = rro.Certificate()
1846
1847 if a, ok := rro.(interface {
1848 OverriddenManifestPackageName() string
1849 }); ok {
1850 af.overriddenPackageName = a.OverriddenManifestPackageName()
1851 }
1852 return af
1853}
1854
Ken Chenfad7f9d2021-11-10 22:02:57 +08001855func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1856 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001857 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1858}
1859
Jiyong Park12a719c2021-01-07 15:31:24 +09001860func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1861 dirInApex := filepath.Join("etc", "fs")
1862 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1863}
1864
Paul Duffin064b70c2020-11-02 17:32:38 +00001865// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001866// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1867// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1868// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001869func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001870 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001871 am, ok := child.(android.ApexModule)
1872 if !ok || !am.CanHaveApexVariants() {
1873 return false
1874 }
1875
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001876 // Filter-out unwanted depedendencies
1877 depTag := ctx.OtherModuleDependencyTag(child)
1878 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1879 return false
1880 }
Paul Duffin520917a2022-05-13 13:01:59 +00001881 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001882 return false
1883 }
1884
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001885 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001886 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001887
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001888 // Visit actually
1889 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001890 })
1891}
1892
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001893// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1894type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001895
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001896const (
1897 ext4 fsType = iota
1898 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001899 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001900)
Artur Satayev849f8442020-04-28 14:57:42 +01001901
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001902func (f fsType) string() string {
1903 switch f {
1904 case ext4:
1905 return ext4FsType
1906 case f2fs:
1907 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001908 case erofs:
1909 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001910 default:
1911 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001912 }
1913}
1914
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001915var _ android.MixedBuildBuildable = (*apexBundle)(nil)
1916
1917func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
1918 return ctx.ModuleType() == "apex" && a.properties.ApexType == imageApex
1919}
1920
1921func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
1922 bazelCtx := ctx.Config().BazelContext
1923 bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
1924}
1925
Jingwen Chen889f2f22022-12-16 08:16:01 +00001926// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
1927// override_apex module overriding this apexBundle. An apexBundle can be
1928// overridden by different override_apex modules (e.g. Google or Go variants),
1929// which is handled by the overrides mutators.
1930func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
1931 if _, ok := ctx.Module().(android.OverridableModule); ok {
1932 return android.MaybeBp2buildLabelOfOverridingModule(ctx)
1933 }
1934 return a.BazelModuleBase.GetBazelLabel(ctx, a)
1935}
1936
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001937func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
1938 if !a.commonBuildActions(ctx) {
1939 return
1940 }
1941
1942 a.setApexTypeAndSuffix(ctx)
1943 a.setPayloadFsType(ctx)
1944 a.setSystemLibLink(ctx)
1945
1946 if a.properties.ApexType != zipApex {
1947 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
1948 }
1949
1950 bazelCtx := ctx.Config().BazelContext
1951 outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx))
1952 if err != nil {
1953 ctx.ModuleErrorf(err.Error())
1954 return
1955 }
1956 a.installDir = android.PathForModuleInstall(ctx, "apex")
Jingwen Chen94098e82023-01-10 14:50:42 +00001957
1958 // Set the output file to .apex or .capex depending on the compression configuration.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001959 a.setCompression(ctx)
Jingwen Chen94098e82023-01-10 14:50:42 +00001960 if a.isCompressed {
1961 a.outputApexFile = android.PathForBazelOut(ctx, outputs.SignedCompressedOutput)
1962 } else {
1963 a.outputApexFile = android.PathForBazelOut(ctx, outputs.SignedOutput)
1964 }
1965 a.outputFile = a.outputApexFile
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001966
Sam Delmerico4ed95e22023-02-03 18:12:15 -05001967 if len(outputs.TidyFiles) > 0 {
1968 tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles)
1969 a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles)
1970 }
1971
Liz Kammer0e255ef2022-11-04 16:07:04 -04001972 // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer
1973 // interface if these were set in a provider rather than the module itself
Wei Li32dcdf92022-10-26 22:30:48 -07001974 a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0])
1975 a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1])
1976 a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0])
1977 a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1])
Liz Kammer0e255ef2022-11-04 16:07:04 -04001978
Jingwen Chen29743c82023-01-25 17:49:46 +00001979 // Ensure ApexMkInfo.install_to_system make module names are installed as
1980 // part of a bundled build.
1981 a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...)
Vinh Tranb6803a52022-12-14 11:34:54 -05001982
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001983 apexType := a.properties.ApexType
1984 switch apexType {
1985 case imageApex:
Liz Kammer303978d2022-11-04 16:12:43 -04001986 a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile)
Jingwen Chen0c9a2762022-11-04 09:40:47 +00001987 a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex))
Wei Licc73a052022-11-07 14:25:34 -08001988 a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs))
Jingwen Chen0c9a2762022-11-04 09:40:47 +00001989 // TODO(b/239084755): Generate the java api using.xml file from Bazel.
Jingwen Chen1ec77852022-11-07 14:36:12 +00001990 a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex))
Wei Li78c07de2022-11-08 16:01:05 -08001991 a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001992 installSuffix := imageApexSuffix
1993 if a.isCompressed {
1994 installSuffix = imageCapexSuffix
1995 }
1996 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
1997 a.compatSymlinks.Paths()...)
1998 default:
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00001999 panic(fmt.Errorf("internal error: unexpected apex_type for the ProcessBazelQueryResponse: %v", a.properties.ApexType))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002000 }
2001
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002002 // filesInfo is not set in mixed mode, because all information about the
2003 // apex's contents should completely come from the Starlark providers.
2004 //
2005 // Prevent accidental writes to filesInfo in the earlier parts Soong by
2006 // asserting it to be nil.
2007 if a.filesInfo != nil {
2008 panic(fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel."))
2009 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002010}
2011
2012func (a *apexBundle) setCompression(ctx android.ModuleContext) {
2013 if a.properties.ApexType != imageApex {
2014 a.isCompressed = false
2015 } else if a.testOnlyShouldForceCompression() {
2016 a.isCompressed = true
2017 } else {
2018 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
2019 }
2020}
2021
2022func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
2023 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2024 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2025 // the same library in the system partition, thus effectively sharing the same libraries
2026 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2027 // in the APEX.
2028 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
2029
2030 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2031 // So we can't link them to /system/lib libs which are core variants.
2032 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2033 a.linkToSystemLib = false
2034 }
2035
2036 forced := ctx.Config().ForceApexSymlinkOptimization()
2037 updatable := a.Updatable() || a.FutureUpdatable()
2038
2039 // We don't need the optimization for updatable APEXes, as it might give false signal
2040 // to the system health when the APEXes are still bundled (b/149805758).
2041 if !forced && updatable && a.properties.ApexType == imageApex {
2042 a.linkToSystemLib = false
2043 }
2044
2045 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2046 if ctx.Host() {
2047 a.linkToSystemLib = false
2048 }
2049}
2050
2051func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
2052 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2053 case ext4FsType:
2054 a.payloadFsType = ext4
2055 case f2fsFsType:
2056 a.payloadFsType = f2fs
2057 case erofsFsType:
2058 a.payloadFsType = erofs
2059 default:
2060 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
2061 }
2062}
2063
2064func (a *apexBundle) setApexTypeAndSuffix(ctx android.ModuleContext) {
2065 // Set suffix and primaryApexType depending on the ApexType
2066 buildFlattenedAsDefault := ctx.Config().FlattenApex()
2067 switch a.properties.ApexType {
2068 case imageApex:
2069 if buildFlattenedAsDefault {
2070 a.suffix = imageApexSuffix
2071 } else {
2072 a.suffix = ""
2073 a.primaryApexType = true
2074
2075 if ctx.Config().InstallExtraFlattenedApexes() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002076 a.makeModulesToInstall = append(a.makeModulesToInstall, a.Name()+flattenedSuffix)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002077 }
2078 }
2079 case zipApex:
2080 if proptools.String(a.properties.Payload_type) == "zip" {
2081 a.suffix = ""
2082 a.primaryApexType = true
2083 } else {
2084 a.suffix = zipApexSuffix
2085 }
2086 case flattenedApex:
2087 if buildFlattenedAsDefault {
2088 a.suffix = ""
2089 a.primaryApexType = true
2090 } else {
2091 a.suffix = flattenedSuffix
2092 }
2093 }
2094}
2095
2096func (a apexBundle) isCompressable() bool {
2097 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2098}
2099
2100func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2101 a.checkApexAvailability(ctx)
2102 a.checkUpdatable(ctx)
2103 a.CheckMinSdkVersion(ctx)
2104 a.checkStaticLinkingToStubLibraries(ctx)
2105 a.checkStaticExecutables(ctx)
2106 if len(a.properties.Tests) > 0 && !a.testApex {
2107 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2108 return false
2109 }
2110 return true
2111}
2112
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002113type visitorContext struct {
2114 // all the files that will be included in this APEX
2115 filesInfo []apexFile
2116
2117 // native lib dependencies
2118 provideNativeLibs []string
2119 requireNativeLibs []string
2120
2121 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002122
2123 // if true, raise error on duplicate apexFile
2124 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002125}
2126
Jooyung Han862c0d62022-12-21 10:15:37 +09002127func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002128 encountered := make(map[string]apexFile)
2129 for _, f := range vctx.filesInfo {
2130 dest := filepath.Join(f.installDir, f.builtFile.Base())
2131 if e, ok := encountered[dest]; !ok {
2132 encountered[dest] = f
2133 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002134 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2135 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2136 dest, e.builtFile, f.builtFile)
2137 return
2138 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002139 // If a module is directly included and also transitively depended on
2140 // consider it as directly included.
2141 e.transitiveDep = e.transitiveDep && f.transitiveDep
2142 encountered[dest] = e
2143 }
2144 }
2145 vctx.filesInfo = vctx.filesInfo[:0]
2146 for _, v := range encountered {
2147 vctx.filesInfo = append(vctx.filesInfo, v)
2148 }
2149 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2150 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2151 // changes.
2152 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2153 })
2154}
2155
2156func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2157 depTag := ctx.OtherModuleDependencyTag(child)
2158 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2159 return false
2160 }
2161 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2162 return false
2163 }
2164 depName := ctx.OtherModuleName(child)
2165 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2166 switch depTag {
2167 case sharedLibTag, jniLibTag:
2168 isJniLib := depTag == jniLibTag
2169 switch ch := child.(type) {
2170 case *cc.Module:
2171 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2172 fi.isJniLib = isJniLib
2173 vctx.filesInfo = append(vctx.filesInfo, fi)
2174 // Collect the list of stub-providing libs except:
2175 // - VNDK libs are only for vendors
2176 // - bootstrap bionic libs are treated as provided by system
2177 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2178 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2179 }
2180 return true // track transitive dependencies
2181 case *rust.Module:
2182 fi := apexFileForRustLibrary(ctx, ch)
2183 fi.isJniLib = isJniLib
2184 vctx.filesInfo = append(vctx.filesInfo, fi)
2185 return true // track transitive dependencies
2186 default:
2187 propertyName := "native_shared_libs"
2188 if isJniLib {
2189 propertyName = "jni_libs"
2190 }
2191 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2192 }
2193 case executableTag:
2194 switch ch := child.(type) {
2195 case *cc.Module:
2196 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2197 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002198 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002199 if ch.HostToolPath().Valid() {
2200 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2201 }
2202 case bootstrap.GoBinaryTool:
2203 if a.Host() {
2204 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2205 }
2206 case *rust.Module:
2207 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2208 return true // track transitive dependencies
2209 default:
2210 ctx.PropertyErrorf("binaries",
2211 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2212 }
2213 case shBinaryTag:
2214 if csh, ok := child.(*sh.ShBinary); ok {
2215 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2216 } else {
2217 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2218 }
2219 case bcpfTag:
2220 bcpfModule, ok := child.(*java.BootclasspathFragmentModule)
2221 if !ok {
2222 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2223 return false
2224 }
2225
2226 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
2227 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002228 a.makeModulesToInstall = append(a.makeModulesToInstall, makeModuleName)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002229 }
2230 return true
2231 case sscpfTag:
2232 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2233 ctx.PropertyErrorf("systemserverclasspath_fragments",
2234 "%q is not a systemserverclasspath_fragment module", depName)
2235 return false
2236 }
2237 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2238 vctx.filesInfo = append(vctx.filesInfo, *af)
2239 }
2240 return true
2241 case javaLibTag:
2242 switch child.(type) {
2243 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2244 af := apexFileForJavaModule(ctx, child.(javaModule))
2245 if !af.ok() {
2246 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2247 return false
2248 }
2249 vctx.filesInfo = append(vctx.filesInfo, af)
2250 return true // track transitive dependencies
2251 default:
2252 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2253 }
2254 case androidAppTag:
2255 switch ap := child.(type) {
2256 case *java.AndroidApp:
2257 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2258 return true // track transitive dependencies
2259 case *java.AndroidAppImport:
2260 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2261 case *java.AndroidTestHelperApp:
2262 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2263 case *java.AndroidAppSet:
2264 appDir := "app"
2265 if ap.Privileged() {
2266 appDir = "priv-app"
2267 }
2268 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2269 // suffixed so that PackageManager correctly invalidates the
2270 // existing installed apk in favour of the new APK-in-APEX.
2271 // See bugs for more information.
2272 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2273 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2274 af.certificate = java.PresignedCertificate
2275 vctx.filesInfo = append(vctx.filesInfo, af)
2276 default:
2277 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2278 }
2279 case rroTag:
2280 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2281 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2282 } else {
2283 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2284 }
2285 case bpfTag:
2286 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2287 filesToCopy, _ := bpfProgram.OutputFiles("")
2288 apex_sub_dir := bpfProgram.SubDir()
2289 for _, bpfFile := range filesToCopy {
2290 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2291 }
2292 } else {
2293 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2294 }
2295 case fsTag:
2296 if fs, ok := child.(filesystem.Filesystem); ok {
2297 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2298 } else {
2299 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2300 }
2301 case prebuiltTag:
2302 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2303 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2304 } else {
2305 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2306 }
2307 case compatConfigTag:
2308 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2309 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2310 } else {
2311 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2312 }
2313 case testTag:
2314 if ccTest, ok := child.(*cc.Module); ok {
2315 if ccTest.IsTestPerSrcAllTestsVariation() {
2316 // Multiple-output test module (where `test_per_src: true`).
2317 //
2318 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2319 // We do not add this variation to `filesInfo`, as it has no output;
2320 // however, we do add the other variations of this module as indirect
2321 // dependencies (see below).
2322 } else {
2323 // Single-output test module (where `test_per_src: false`).
2324 af := apexFileForExecutable(ctx, ccTest)
2325 af.class = nativeTest
2326 vctx.filesInfo = append(vctx.filesInfo, af)
2327 }
2328 return true // track transitive dependencies
2329 } else {
2330 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2331 }
2332 case keyTag:
2333 if key, ok := child.(*apexKey); ok {
2334 a.privateKeyFile = key.privateKeyFile
2335 a.publicKeyFile = key.publicKeyFile
2336 } else {
2337 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2338 }
2339 case certificateTag:
2340 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2341 a.containerCertificateFile = dep.Certificate.Pem
2342 a.containerPrivateKeyFile = dep.Certificate.Key
2343 } else {
2344 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2345 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002346 }
2347 return false
2348 }
2349
2350 if a.vndkApex {
2351 return false
2352 }
2353
2354 // indirect dependencies
2355 am, ok := child.(android.ApexModule)
2356 if !ok {
2357 return false
2358 }
2359 // We cannot use a switch statement on `depTag` here as the checked
2360 // tags used below are private (e.g. `cc.sharedDepTag`).
2361 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2362 if ch, ok := child.(*cc.Module); ok {
2363 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2364 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2365 return false
2366 }
2367 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2368 af.transitiveDep = true
2369
2370 // Always track transitive dependencies for host.
2371 if a.Host() {
2372 vctx.filesInfo = append(vctx.filesInfo, af)
2373 return true
2374 }
2375
2376 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2377 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2378 // If the dependency is a stubs lib, don't include it in this APEX,
2379 // but make sure that the lib is installed on the device.
2380 // In case no APEX is having the lib, the lib is installed to the system
2381 // partition.
2382 //
2383 // Always include if we are a host-apex however since those won't have any
2384 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002385 //
2386 // Skip the dependency in unbundled builds where the device image is not
2387 // being built.
2388 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002389 // we need a module name for Make
2390 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002391 if !android.InList(name, a.makeModulesToInstall) {
2392 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002393 }
2394 }
2395 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2396 // Don't track further
2397 return false
2398 }
2399
2400 // If the dep is not considered to be in the same
2401 // apex, don't add it to filesInfo so that it is not
2402 // included in this APEX.
2403 // TODO(jiyong): move this to at the top of the
2404 // else-if clause for the indirect dependencies.
2405 // Currently, that's impossible because we would
2406 // like to record requiredNativeLibs even when
2407 // DepIsInSameAPex is false. We also shouldn't do
2408 // this for host.
2409 //
2410 // TODO(jiyong): explain why the same module is passed in twice.
2411 // Switching the first am to parent breaks lots of tests.
2412 if !android.IsDepInSameApex(ctx, am, am) {
2413 return false
2414 }
2415
2416 vctx.filesInfo = append(vctx.filesInfo, af)
2417 return true // track transitive dependencies
2418 } else if rm, ok := child.(*rust.Module); ok {
2419 af := apexFileForRustLibrary(ctx, rm)
2420 af.transitiveDep = true
2421 vctx.filesInfo = append(vctx.filesInfo, af)
2422 return true // track transitive dependencies
2423 }
2424 } else if cc.IsTestPerSrcDepTag(depTag) {
2425 if ch, ok := child.(*cc.Module); ok {
2426 af := apexFileForExecutable(ctx, ch)
2427 // Handle modules created as `test_per_src` variations of a single test module:
2428 // use the name of the generated test binary (`fileToCopy`) instead of the name
2429 // of the original test module (`depName`, shared by all `test_per_src`
2430 // variations of that module).
2431 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2432 // these are not considered transitive dep
2433 af.transitiveDep = false
2434 vctx.filesInfo = append(vctx.filesInfo, af)
2435 return true // track transitive dependencies
2436 }
2437 } else if cc.IsHeaderDepTag(depTag) {
2438 // nothing
2439 } else if java.IsJniDepTag(depTag) {
2440 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2441 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2442 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2443 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2444 }
2445 } else if rust.IsDylibDepTag(depTag) {
2446 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2447 af := apexFileForRustLibrary(ctx, rustm)
2448 af.transitiveDep = true
2449 vctx.filesInfo = append(vctx.filesInfo, af)
2450 return true // track transitive dependencies
2451 }
2452 } else if rust.IsRlibDepTag(depTag) {
2453 // Rlib is statically linked, but it might have shared lib
2454 // dependencies. Track them.
2455 return true
2456 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2457 // Add the contents of the bootclasspath fragment to the apex.
2458 switch child.(type) {
2459 case *java.Library, *java.SdkLibrary:
2460 javaModule := child.(javaModule)
2461 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2462 if !af.ok() {
2463 ctx.PropertyErrorf("bootclasspath_fragments",
2464 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2465 return false
2466 }
2467 vctx.filesInfo = append(vctx.filesInfo, af)
2468 return true // track transitive dependencies
2469 default:
2470 ctx.PropertyErrorf("bootclasspath_fragments",
2471 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2472 }
2473 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2474 // Add the contents of the systemserverclasspath fragment to the apex.
2475 switch child.(type) {
2476 case *java.Library, *java.SdkLibrary:
2477 af := apexFileForJavaModule(ctx, child.(javaModule))
2478 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002479 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2480 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2481 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002482 return true // track transitive dependencies
2483 default:
2484 ctx.PropertyErrorf("systemserverclasspath_fragments",
2485 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2486 }
2487 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2488 // nothing
2489 } else if depTag == android.DarwinUniversalVariantTag {
2490 // nothing
2491 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2492 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2493 }
2494 return false
2495}
2496
Jooyung Han862c0d62022-12-21 10:15:37 +09002497func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2498 // TODO(b/263308293) remove this
2499 if a.properties.IsCoverageVariant {
2500 return false
2501 }
2502 // TODO(b/263308515) remove this
2503 if a.testApex {
2504 return false
2505 }
2506 // TODO(b/263309864) remove this
2507 if a.Host() {
2508 return false
2509 }
2510 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2511 return false
2512 }
2513 return true
2514}
2515
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002516// Creates build rules for an APEX. It consists of the following major steps:
2517//
2518// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2519// 2) traverse the dependency tree to collect apexFile structs from them.
2520// 3) some fields in apexBundle struct are configured
2521// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002522func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002523 ////////////////////////////////////////////////////////////////////////////////////////////
2524 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002525 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002526 return
2527 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002528 ////////////////////////////////////////////////////////////////////////////////////////////
2529 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002530 // Collect the module directory for IDE info in java/jdeps.go.
2531 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2532
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002533 // TODO(jiyong): do this using WalkPayloadDeps
2534 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002535 vctx := visitorContext{
2536 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2537 checkDuplicate: a.shouldCheckDuplicate(ctx),
2538 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002539 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002540 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002541 if a.privateKeyFile == nil {
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07002542 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002543 return
2544 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002545
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002546 ////////////////////////////////////////////////////////////////////////////////////////////
2547 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002548 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002549 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002550
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002551 a.setApexTypeAndSuffix(ctx)
2552 a.setPayloadFsType(ctx)
2553 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002554 if a.properties.ApexType != zipApex {
2555 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2556 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002557
2558 ////////////////////////////////////////////////////////////////////////////////////////////
2559 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002560 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002561 if a.properties.ApexType == flattenedApex {
2562 a.buildFlattenedApex(ctx)
2563 } else {
2564 a.buildUnflattenedApex(ctx)
2565 }
Jiyong Park956305c2020-01-09 12:32:06 +09002566 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002567 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002568
2569 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2570 if a.installable() {
2571 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2572 // along with other ordinary files. (Note that this is done by apexer for
2573 // non-flattened APEXes)
2574 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2575
2576 // Place the public key as apex_pubkey. This is also done by apexer for
2577 // non-flattened APEXes case.
2578 // TODO(jiyong): Why do we need this CP rule?
2579 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2580 ctx.Build(pctx, android.BuildParams{
2581 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002582 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002583 Output: copiedPubkey,
2584 })
2585 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2586 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002587}
2588
Paul Duffincc33ec82021-04-25 23:14:55 +01002589// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2590// the bootclasspath_fragment contributes to the apex.
2591func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2592 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2593 var filesToAdd []apexFile
2594
2595 // Add the boot image files, e.g. .art, .oat and .vdex files.
Jiakai Zhang6decef92022-01-12 17:56:19 +00002596 if bootclasspathFragmentInfo.ShouldInstallBootImageInApex() {
2597 for arch, files := range bootclasspathFragmentInfo.AndroidBootImageFilesByArchType() {
2598 dirInApex := filepath.Join("javalib", arch.String())
2599 for _, f := range files {
2600 androidMkModuleName := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
2601 // TODO(b/177892522) - consider passing in the bootclasspath fragment module here instead of nil
2602 af := newApexFile(ctx, f, androidMkModuleName, dirInApex, etc, nil)
2603 filesToAdd = append(filesToAdd, af)
2604 }
Paul Duffincc33ec82021-04-25 23:14:55 +01002605 }
2606 }
2607
satayev3db35472021-05-06 23:59:58 +01002608 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002609 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2610 filesToAdd = append(filesToAdd, *af)
2611 }
satayev3db35472021-05-06 23:59:58 +01002612
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002613 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
2614 if pathInApex != "" && !java.SkipDexpreoptBootJars(ctx) {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002615 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2616 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2617
2618 if pathOnHost != nil {
2619 // We need to copy the profile to a temporary path with the right filename because the apexer
2620 // will take the filename as is.
2621 ctx.Build(pctx, android.BuildParams{
2622 Rule: android.Cp,
2623 Input: pathOnHost,
2624 Output: tempPath,
2625 })
2626 } else {
2627 // At this point, the boot image profile cannot be generated. It is probably because the boot
2628 // image profile source file does not exist on the branch, or it is not available for the
2629 // current build target.
2630 // However, we cannot enforce the boot image profile to be generated because some build
2631 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2632 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2633 // only if the APEX is being built.
2634 ctx.Build(pctx, android.BuildParams{
2635 Rule: android.ErrorRule,
2636 Output: tempPath,
2637 Args: map[string]string{
2638 "error": "Boot image profile cannot be generated",
2639 },
2640 })
2641 }
2642
2643 androidMkModuleName := filepath.Base(pathInApex)
2644 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2645 filesToAdd = append(filesToAdd, af)
2646 }
2647
Paul Duffincc33ec82021-04-25 23:14:55 +01002648 return filesToAdd
2649}
2650
satayevb98371c2021-06-15 16:49:50 +01002651// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2652// the module contributes to the apex; or nil if the proto config was not generated.
2653func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2654 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2655 if !info.ClasspathFragmentProtoGenerated {
2656 return nil
2657 }
2658 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2659 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2660 return &af
satayev14e49132021-05-17 21:03:07 +01002661}
2662
Paul Duffincc33ec82021-04-25 23:14:55 +01002663// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2664// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002665func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2666 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2667
2668 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2669 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002670 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2671 if err != nil {
2672 ctx.ModuleErrorf("%s", err)
2673 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002674
2675 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2676 // bootclasspath_fragment.
2677 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2678 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002679}
2680
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002681///////////////////////////////////////////////////////////////////////////////////////////////////
2682// Factory functions
2683//
2684
2685func newApexBundle() *apexBundle {
2686 module := &apexBundle{}
2687
2688 module.AddProperties(&module.properties)
2689 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002690 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002691 module.AddProperties(&module.overridableProperties)
2692
2693 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2694 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002695 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002696 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002697 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002698 return module
2699}
2700
Paul Duffineb8051d2021-10-18 17:49:39 +01002701func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002702 bundle := newApexBundle()
2703 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002704 return bundle
2705}
2706
2707// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2708// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002709func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002710 bundle := newApexBundle()
2711 bundle.testApex = true
2712 return bundle
2713}
2714
2715// apex packages other modules into an APEX file which is a packaging format for system-level
2716// components like binaries, shared libraries, etc.
2717func BundleFactory() android.Module {
2718 return newApexBundle()
2719}
2720
2721type Defaults struct {
2722 android.ModuleBase
2723 android.DefaultsModuleBase
2724}
2725
2726// apex_defaults provides defaultable properties to other apex modules.
2727func defaultsFactory() android.Module {
2728 return DefaultsFactory()
2729}
2730
2731func DefaultsFactory(props ...interface{}) android.Module {
2732 module := &Defaults{}
2733
2734 module.AddProperties(props...)
2735 module.AddProperties(
2736 &apexBundleProperties{},
2737 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002738 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002739 &overridableProperties{},
2740 )
2741
2742 android.InitDefaultsModule(module)
2743 return module
2744}
2745
2746type OverrideApex struct {
2747 android.ModuleBase
2748 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002749 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002750}
2751
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002752func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002753 // All the overrides happen in the base module.
2754}
2755
2756// override_apex is used to create an apex module based on another apex module by overriding some of
2757// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002758func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002759 m := &OverrideApex{}
2760
2761 m.AddProperties(&overridableProperties{})
2762
2763 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2764 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002765 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002766 return m
2767}
2768
Wei Li1c66fc72022-05-09 23:59:14 -07002769func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2770 if ctx.ModuleType() != "override_apex" {
2771 return
2772 }
2773
2774 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2775 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2776 if !baseApexExists {
2777 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2778 }
2779
2780 a, baseModuleIsApex := baseModule.(*apexBundle)
2781 if !baseModuleIsApex {
2782 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2783 }
2784 attrs, props := convertWithBp2build(a, ctx)
2785
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002786 // We just want the name, not module reference.
2787 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2788 attrs.Base_apex_name = &baseApexName
2789
Wei Li1c66fc72022-05-09 23:59:14 -07002790 for _, p := range o.GetProperties() {
2791 overridableProperties, ok := p.(*overridableProperties)
2792 if !ok {
2793 continue
2794 }
Wei Li40f98732022-05-20 22:08:11 -07002795
2796 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2797 // After it is converted in convertWithBp2build(baseApex, ctx),
2798 // the attrs.Manifest.Value.Label is the file path relative to the directory
2799 // of base apex. So the following code converts it to a label that looks like
2800 // <package of base apex>:<path of manifest file> if base apex and override
2801 // apex are not in the same package.
2802 baseApexPackage := ctx.OtherModuleDir(a)
2803 overrideApexPackage := ctx.ModuleDir()
2804 if baseApexPackage != overrideApexPackage {
2805 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2806 }
2807
Wei Li1c66fc72022-05-09 23:59:14 -07002808 // Key
2809 if overridableProperties.Key != nil {
2810 attrs.Key = bazel.LabelAttribute{}
2811 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2812 }
2813
2814 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002815 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002816 // If overridableProperties.Certificate is nil, clear this out as
2817 // well with zeroed structs, so the override_apex does not use the
2818 // base apex's certificate.
2819 attrs.Certificate = bazel.LabelAttribute{}
2820 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002821 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002822 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002823 }
2824
2825 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002826 if overridableProperties.Prebuilts != nil {
2827 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2828 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2829 }
Wei Li1c66fc72022-05-09 23:59:14 -07002830
2831 // Compressible
2832 if overridableProperties.Compressible != nil {
2833 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2834 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002835
2836 // Package name
2837 //
2838 // e.g. com.android.adbd's package name is com.android.adbd, but
2839 // com.google.android.adbd overrides the package name to com.google.android.adbd
2840 //
2841 // TODO: this can be overridden from the product configuration, see
2842 // getOverrideManifestPackageName and
2843 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2844 //
2845 // Instead of generating the BUILD files differently based on the product config
2846 // at the point of conversion, this should be handled by the BUILD file loading
2847 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2848 if overridableProperties.Package_name != "" {
2849 attrs.Package_name = &overridableProperties.Package_name
2850 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002851
2852 // Logging parent
2853 if overridableProperties.Logging_parent != "" {
2854 attrs.Logging_parent = &overridableProperties.Logging_parent
2855 }
Wei Li1c66fc72022-05-09 23:59:14 -07002856 }
2857
2858 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: o.Name()}, &attrs)
2859}
2860
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002861///////////////////////////////////////////////////////////////////////////////////////////////////
2862// Vality check routines
2863//
2864// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2865// certain conditions are not met.
2866//
2867// TODO(jiyong): move these checks to a separate go file.
2868
satayevad991492021-12-03 18:58:32 +00002869var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2870
Spandan Dasa5f39a12022-08-05 02:35:52 +00002871// 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 +09002872// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002873func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002874 if a.testApex || a.vndkApex {
2875 return
2876 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002877 // apexBundle::minSdkVersion reports its own errors.
2878 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002879 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002880}
2881
Albert Martineefabcf2022-03-21 20:11:16 +00002882// Returns apex's min_sdk_version string value, honoring overrides
2883func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2884 // Only override the minSdkVersion value on Apexes which already specify
2885 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2886 // min_sdk_version value is lower than the one to override with.
zhidou133c55b2023-01-31 19:34:10 +00002887 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.overridableProperties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002888 if minApiLevel.IsNone() {
2889 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002890 }
2891
Colin Cross56534df2022-10-04 09:58:58 -07002892 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2893 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2894 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2895 minApiLevel = overrideApiLevel
2896 }
2897
2898 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002899}
2900
2901// Returns apex's min_sdk_version SdkSpec, honoring overrides
satayevad991492021-12-03 18:58:32 +00002902func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2903 return android.SdkSpec{
2904 Kind: android.SdkNone,
2905 ApiLevel: a.minSdkVersion(ctx),
Albert Martineefabcf2022-03-21 20:11:16 +00002906 Raw: a.minSdkVersionValue(ctx),
satayevad991492021-12-03 18:58:32 +00002907 }
2908}
2909
Albert Martineefabcf2022-03-21 20:11:16 +00002910// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00002911func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00002912 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
2913}
2914
2915// Construct ApiLevel object from min_sdk_version string value
2916func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
2917 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09002918 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002919 }
Albert Martineefabcf2022-03-21 20:11:16 +00002920 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002921 if err != nil {
2922 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
2923 return android.NoneApiLevel
2924 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002925 return apiLevel
2926}
2927
2928// Ensures that a lib providing stub isn't statically linked
2929func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
2930 // Practically, we only care about regular APEXes on the device.
2931 if ctx.Host() || a.testApex || a.vndkApex {
2932 return
2933 }
2934
2935 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2936
2937 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
2938 if ccm, ok := to.(*cc.Module); ok {
2939 apexName := ctx.ModuleName()
2940 fromName := ctx.OtherModuleName(from)
2941 toName := ctx.OtherModuleName(to)
2942
2943 // If `to` is not actually in the same APEX as `from` then it does not need
2944 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002945 //
2946 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002947 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2948 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2949 return false
2950 }
2951
2952 // The dynamic linker and crash_dump tool in the runtime APEX is the only
2953 // exception to this rule. It can't make the static dependencies dynamic
2954 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09002955 // Same rule should be applied to linkerconfig, because it should be executed
2956 // only with static linked libraries before linker is available with ld.config.txt
2957 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002958 return false
2959 }
2960
2961 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
2962 if isStubLibraryFromOtherApex && !externalDep {
2963 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
2964 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
2965 }
2966
2967 }
2968 return true
2969 })
2970}
2971
satayevb98371c2021-06-15 16:49:50 +01002972// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002973func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
2974 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00002975 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002976 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
2977 }
Jiyong Park1bc84122021-06-22 20:23:05 +09002978 if a.UsePlatformApis() {
2979 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
2980 }
Daniel Norman69109112021-12-02 12:52:42 -08002981 if a.SocSpecific() || a.DeviceSpecific() {
2982 ctx.PropertyErrorf("updatable", "vendor APEXes are not updatable")
2983 }
Jiyong Parkf4020582021-11-29 12:37:10 +09002984 if a.FutureUpdatable() {
2985 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
2986 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002987 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01002988 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002989 }
2990}
2991
satayevb98371c2021-06-15 16:49:50 +01002992// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
2993func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
2994 ctx.VisitDirectDeps(func(module android.Module) {
2995 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
2996 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2997 if !info.ClasspathFragmentProtoGenerated {
2998 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
2999 }
3000 }
3001 })
3002}
3003
3004// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003005func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003006 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3007 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003008 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3009 tag := ctx.OtherModuleDependencyTag(module)
3010 switch tag {
3011 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003012 if m, ok := module.(interface {
3013 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3014 }); ok {
3015 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003016 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3017 }
3018 }
3019 }
3020 })
3021}
3022
satayevb98371c2021-06-15 16:49:50 +01003023// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003024func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3025 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3026 if ctx.Host() || a.testApex || a.vndkApex {
3027 return
3028 }
3029
3030 // Because APEXes targeting other than system/system_ext partitions can't set
3031 // apex_available, we skip checks for these APEXes
3032 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3033 return
3034 }
3035
3036 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3037 // Requiring them and their transitive depencies with apex_available is not right
3038 // because they just add noise.
3039 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3040 return
3041 }
3042
3043 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3044 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3045 if externalDep {
3046 return false
3047 }
3048
3049 apexName := ctx.ModuleName()
3050 fromName := ctx.OtherModuleName(from)
3051 toName := ctx.OtherModuleName(to)
3052
3053 // If `to` is not actually in the same APEX as `from` then it does not need
3054 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003055 //
3056 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003057 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3058 // As soon as the dependency graph crosses the APEX boundary, don't go
3059 // further.
3060 return false
3061 }
3062
3063 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3064 return true
3065 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003066 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3067 "\n\nDependency path:%s\n\n"+
3068 "Consider adding %q to 'apex_available' property of %q",
3069 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003070 // Visit this module's dependencies to check and report any issues with their availability.
3071 return true
3072 })
3073}
3074
Jiyong Park192600a2021-08-03 07:52:17 +00003075// checkStaticExecutable ensures that executables in an APEX are not static.
3076func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003077 // No need to run this for host APEXes
3078 if ctx.Host() {
3079 return
3080 }
3081
Jiyong Park192600a2021-08-03 07:52:17 +00003082 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3083 if ctx.OtherModuleDependencyTag(module) != executableTag {
3084 return
3085 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003086
3087 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003088 apex := a.ApexVariationName()
3089 exec := ctx.OtherModuleName(module)
3090 if isStaticExecutableAllowed(apex, exec) {
3091 return
3092 }
3093 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3094 }
3095 })
3096}
3097
3098// A small list of exceptions where static executables are allowed in APEXes.
3099func isStaticExecutableAllowed(apex string, exec string) bool {
3100 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003101 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003102 "linker",
3103 "linkerconfig",
3104 },
3105 }
3106 execNames, ok := m[apex]
3107 return ok && android.InList(exec, execNames)
3108}
3109
braleeb0c1f0c2021-06-07 22:49:13 +08003110// Collect information for opening IDE project files in java/jdeps.go.
3111func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09003112 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Java_libs...)
3113 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Bootclasspath_fragments...)
3114 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003115 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3116}
3117
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003118var (
3119 apexAvailBaseline = makeApexAvailableBaseline()
3120 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3121)
3122
Colin Cross440e0d02020-06-11 11:32:11 -07003123func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003124 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003125 moduleName = normalizeModuleName(moduleName)
3126
Colin Cross440e0d02020-06-11 11:32:11 -07003127 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003128 return true
3129 }
3130
3131 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003132 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003133 return true
3134 }
3135
3136 return false
3137}
3138
3139func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003140 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3141 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003142 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003143 if strings.HasPrefix(moduleName, "libclang_rt.") {
3144 // This module has many arch variants that depend on the product being built.
3145 // We don't want to list them all
3146 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003147 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003148 if strings.HasPrefix(moduleName, "androidx.") {
3149 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3150 moduleName = "androidx"
3151 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003152 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003153}
3154
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003155// Transform the map of apex -> modules to module -> apexes.
3156func invertApexBaseline(m map[string][]string) map[string][]string {
3157 r := make(map[string][]string)
3158 for apex, modules := range m {
3159 for _, module := range modules {
3160 r[module] = append(r[module], apex)
3161 }
3162 }
3163 return r
3164}
3165
3166// Retrieve the baseline of apexes to which the supplied module belongs.
3167func BaselineApexAvailable(moduleName string) []string {
3168 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3169}
3170
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003171// This is a map from apex to modules, which overrides the apex_available setting for that
3172// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003173// TODO(b/147364041): remove this
3174func makeApexAvailableBaseline() map[string][]string {
3175 // The "Module separator"s below are employed to minimize merge conflicts.
3176 m := make(map[string][]string)
3177 //
3178 // Module separator
3179 //
3180 m["com.android.appsearch"] = []string{
3181 "icing-java-proto-lite",
3182 "libprotobuf-java-lite",
3183 }
3184 //
3185 // Module separator
3186 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003187 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003188 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003189 }
3190 //
3191 // Module separator
3192 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003193 m["com.android.cellbroadcast"] = []string{"CellBroadcastApp", "CellBroadcastServiceModule"}
3194 //
3195 // Module separator
3196 //
3197 m["com.android.extservices"] = []string{
3198 "error_prone_annotations",
3199 "ExtServices-core",
3200 "ExtServices",
3201 "libtextclassifier-java",
3202 "libz_current",
3203 "textclassifier-statsd",
3204 "TextClassifierNotificationLibNoManifest",
3205 "TextClassifierServiceLibNoManifest",
3206 }
3207 //
3208 // Module separator
3209 //
3210 m["com.android.neuralnetworks"] = []string{
3211 "android.hardware.neuralnetworks@1.0",
3212 "android.hardware.neuralnetworks@1.1",
3213 "android.hardware.neuralnetworks@1.2",
3214 "android.hardware.neuralnetworks@1.3",
3215 "android.hidl.allocator@1.0",
3216 "android.hidl.memory.token@1.0",
3217 "android.hidl.memory@1.0",
3218 "android.hidl.safe_union@1.0",
3219 "libarect",
3220 "libbuildversion",
3221 "libmath",
3222 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003223 }
3224 //
3225 // Module separator
3226 //
3227 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003228 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003229 }
3230 //
3231 // Module separator
3232 //
3233 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003234 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003235 }
3236 //
3237 // Module separator
3238 //
3239 m["com.android.mediaprovider"] = []string{
3240 "MediaProvider",
3241 "MediaProviderGoogle",
3242 "fmtlib_ndk",
3243 "libbase_ndk",
3244 "libfuse",
3245 "libfuse_jni",
3246 }
3247 //
3248 // Module separator
3249 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003250 m["com.android.runtime"] = []string{
3251 "bionic_libc_platform_headers",
3252 "libarm-optimized-routines-math",
3253 "libc_aeabi",
3254 "libc_bionic",
3255 "libc_bionic_ndk",
3256 "libc_bootstrap",
3257 "libc_common",
3258 "libc_common_shared",
3259 "libc_common_static",
3260 "libc_dns",
3261 "libc_dynamic_dispatch",
3262 "libc_fortify",
3263 "libc_freebsd",
3264 "libc_freebsd_large_stack",
3265 "libc_gdtoa",
3266 "libc_init_dynamic",
3267 "libc_init_static",
3268 "libc_jemalloc_wrapper",
3269 "libc_netbsd",
3270 "libc_nomalloc",
3271 "libc_nopthread",
3272 "libc_openbsd",
3273 "libc_openbsd_large_stack",
3274 "libc_openbsd_ndk",
3275 "libc_pthread",
3276 "libc_static_dispatch",
3277 "libc_syscalls",
3278 "libc_tzcode",
3279 "libc_unwind_static",
3280 "libdebuggerd",
3281 "libdebuggerd_common_headers",
3282 "libdebuggerd_handler_core",
3283 "libdebuggerd_handler_fallback",
3284 "libdl_static",
3285 "libjemalloc5",
3286 "liblinker_main",
3287 "liblinker_malloc",
3288 "liblz4",
3289 "liblzma",
3290 "libprocinfo",
3291 "libpropertyinfoparser",
3292 "libscudo",
3293 "libstdc++",
3294 "libsystemproperties",
3295 "libtombstoned_client_static",
3296 "libunwindstack",
3297 "libz",
3298 "libziparchive",
3299 }
3300 //
3301 // Module separator
3302 //
3303 m["com.android.tethering"] = []string{
3304 "android.hardware.tetheroffload.config-V1.0-java",
3305 "android.hardware.tetheroffload.control-V1.0-java",
3306 "android.hidl.base-V1.0-java",
3307 "libcgrouprc",
3308 "libcgrouprc_format",
3309 "libtetherutilsjni",
3310 "libvndksupport",
3311 "net-utils-framework-common",
3312 "netd_aidl_interface-V3-java",
3313 "netlink-client",
3314 "networkstack-aidl-interfaces-java",
3315 "tethering-aidl-interfaces-java",
3316 "TetheringApiCurrentLib",
3317 }
3318 //
3319 // Module separator
3320 //
3321 m["com.android.wifi"] = []string{
3322 "PlatformProperties",
3323 "android.hardware.wifi-V1.0-java",
3324 "android.hardware.wifi-V1.0-java-constants",
3325 "android.hardware.wifi-V1.1-java",
3326 "android.hardware.wifi-V1.2-java",
3327 "android.hardware.wifi-V1.3-java",
3328 "android.hardware.wifi-V1.4-java",
3329 "android.hardware.wifi.hostapd-V1.0-java",
3330 "android.hardware.wifi.hostapd-V1.1-java",
3331 "android.hardware.wifi.hostapd-V1.2-java",
3332 "android.hardware.wifi.supplicant-V1.0-java",
3333 "android.hardware.wifi.supplicant-V1.1-java",
3334 "android.hardware.wifi.supplicant-V1.2-java",
3335 "android.hardware.wifi.supplicant-V1.3-java",
3336 "android.hidl.base-V1.0-java",
3337 "android.hidl.manager-V1.0-java",
3338 "android.hidl.manager-V1.1-java",
3339 "android.hidl.manager-V1.2-java",
3340 "bouncycastle-unbundled",
3341 "dnsresolver_aidl_interface-V2-java",
3342 "error_prone_annotations",
3343 "framework-wifi-pre-jarjar",
3344 "framework-wifi-util-lib",
3345 "ipmemorystore-aidl-interfaces-V3-java",
3346 "ipmemorystore-aidl-interfaces-java",
3347 "ksoap2",
3348 "libnanohttpd",
3349 "libwifi-jni",
3350 "net-utils-services-common",
3351 "netd_aidl_interface-V2-java",
3352 "netd_aidl_interface-unstable-java",
3353 "netd_event_listener_interface-java",
3354 "netlink-client",
3355 "networkstack-client",
3356 "services.net",
3357 "wifi-lite-protos",
3358 "wifi-nano-protos",
3359 "wifi-service-pre-jarjar",
3360 "wifi-service-resources",
3361 }
3362 //
3363 // Module separator
3364 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003365 m["com.android.os.statsd"] = []string{
3366 "libstatssocket",
3367 }
3368 //
3369 // Module separator
3370 //
3371 m[android.AvailableToAnyApex] = []string{
3372 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx/extras support libraries
3373 "androidx",
3374 "androidx-constraintlayout_constraintlayout",
3375 "androidx-constraintlayout_constraintlayout-nodeps",
3376 "androidx-constraintlayout_constraintlayout-solver",
3377 "androidx-constraintlayout_constraintlayout-solver-nodeps",
3378 "com.google.android.material_material",
3379 "com.google.android.material_material-nodeps",
3380
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003381 "libclang_rt",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003382 "libprofile-clang-extras",
3383 "libprofile-clang-extras_ndk",
3384 "libprofile-extras",
3385 "libprofile-extras_ndk",
Ryan Prichardb35a85e2021-01-13 19:18:53 -08003386 "libunwind",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003387 }
3388 return m
3389}
3390
3391func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003392 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3393 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003394}
3395
Spandan Dasf14e2542021-11-12 00:01:37 +00003396func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3397 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3398 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003399 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003400 With("name", jar).
3401 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3402 Because(jar +
3403 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003404 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003405 " 1. If the offending code is from a statically linked library, consider " +
3406 "removing that dependency and using an alternative already in the " +
3407 "bootclasspath, or perhaps a shared library." +
3408 " 2. Move the offending code into an allowed package.\n" +
3409 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3410 "health implications of bundling that code, particularly if the offending jar " +
3411 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003412
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003413 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003414 }
3415 return rules
3416}
3417
Anton Hanssone1b18362021-12-23 15:05:38 +00003418// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003419// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003420func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003421 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003422 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003423 "android.net.ssl",
3424 "com.android.org.conscrypt",
3425 },
Wei Li40f98732022-05-20 22:08:11 -07003426 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003427 "android.media",
3428 },
3429 }
3430}
3431
Anton Hanssone1b18362021-12-23 15:05:38 +00003432// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003433// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003434func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003435 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003436 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003437 "android.provider",
3438 },
Wei Li40f98732022-05-20 22:08:11 -07003439 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003440 "android.permission",
3441 "android.app.role",
3442 "com.android.permission",
3443 "com.android.role",
3444 },
Wei Li40f98732022-05-20 22:08:11 -07003445 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003446 "android.os.ext",
3447 },
Wei Li40f98732022-05-20 22:08:11 -07003448 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003449 "android.app",
3450 "android.os",
3451 "android.util",
3452 "com.android.internal.statsd",
3453 "com.android.server.stats",
3454 },
Wei Li40f98732022-05-20 22:08:11 -07003455 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003456 "com.android.server.wifi",
3457 "com.android.wifi.x",
3458 "android.hardware.wifi",
3459 "android.net.wifi",
3460 },
Wei Li40f98732022-05-20 22:08:11 -07003461 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003462 "android.net",
3463 },
3464 }
3465}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003466
3467// For Bazel / bp2build
3468
3469type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003470 Manifest bazel.LabelAttribute
3471 Android_manifest bazel.LabelAttribute
3472 File_contexts bazel.LabelAttribute
3473 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003474 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3475 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003476 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003477 Updatable bazel.BoolAttribute
3478 Installable bazel.BoolAttribute
3479 Binaries bazel.LabelListAttribute
3480 Prebuilts bazel.LabelListAttribute
3481 Native_shared_libs_32 bazel.LabelListAttribute
3482 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003483 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003484 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003485 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003486 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003487 Base_apex_name *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003488}
3489
3490type convertedNativeSharedLibs struct {
3491 Native_shared_libs_32 bazel.LabelListAttribute
3492 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003493}
3494
Liz Kammerb83b7b02022-12-21 14:53:41 -05003495const (
3496 minSdkVersionPropName = "Min_sdk_version"
3497)
3498
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003499// ConvertWithBp2build performs bp2build conversion of an apex
3500func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003501 // We only convert apex and apex_test modules at this time
3502 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003503 return
3504 }
3505
Wei Li1c66fc72022-05-09 23:59:14 -07003506 attrs, props := convertWithBp2build(a, ctx)
Yu Liu4c212ce2022-10-14 12:20:20 -07003507 commonAttrs := android.CommonAttributes{
3508 Name: a.Name(),
3509 }
3510 if a.testApex {
3511 commonAttrs.Testonly = proptools.BoolPtr(a.testApex)
3512 }
3513 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003514}
3515
3516func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003517 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003518 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003519
3520 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003521 if a.properties.AndroidManifest != nil {
3522 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003523 }
3524
3525 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003526 if a.properties.File_contexts == nil {
3527 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3528 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3529 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3530 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003531 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003532 } else {
3533 // File_contexts is a file
3534 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003535 }
3536
Liz Kammerb83b7b02022-12-21 14:53:41 -05003537 productVariableProps := android.ProductVariableProperties(ctx)
Albert Martineefabcf2022-03-21 20:11:16 +00003538 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3539 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003540 var minSdkVersion bazel.StringAttribute
zhidou133c55b2023-01-31 19:34:10 +00003541 if a.overridableProperties.Min_sdk_version != nil {
3542 minSdkVersion.SetValue(*a.overridableProperties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003543 }
3544 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3545 for c, p := range props {
3546 if val, ok := p.(*string); ok {
3547 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3548 }
3549 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003550 }
3551
3552 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003553 if a.overridableProperties.Key != nil {
3554 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003555 }
3556
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003557 // Certificate
3558 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003559
Yu Liu4ae55d12022-01-05 17:17:23 -08003560 nativeSharedLibs := &convertedNativeSharedLibs{
3561 Native_shared_libs_32: bazel.LabelListAttribute{},
3562 Native_shared_libs_64: bazel.LabelListAttribute{},
3563 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003564
3565 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3566 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3567 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3568 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3569 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003570 if a.CompileMultilib() != nil {
3571 compileMultilib = *a.CompileMultilib()
3572 }
3573
3574 // properties.Native_shared_libs is treated as "both"
3575 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3576 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3577 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3578 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3579 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003580
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003581 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003582 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3583 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3584
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003585 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003586 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003587
Yu Liu4c212ce2022-10-14 12:20:20 -07003588 var testsAttrs bazel.LabelListAttribute
3589 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3590 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3591 testsAttrs = bazel.MakeLabelListAttribute(tests)
3592 }
3593
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003594 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003595 if a.properties.Updatable != nil {
3596 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003597 }
3598
3599 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003600 if a.properties.Installable != nil {
3601 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003602 }
3603
Wei Lif034cb42022-01-19 15:54:31 -08003604 var compressibleAttribute bazel.BoolAttribute
3605 if a.overridableProperties.Compressible != nil {
3606 compressibleAttribute.Value = a.overridableProperties.Compressible
3607 }
3608
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003609 var packageName *string
3610 if a.overridableProperties.Package_name != "" {
3611 packageName = &a.overridableProperties.Package_name
3612 }
3613
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003614 var loggingParent *string
3615 if a.overridableProperties.Logging_parent != "" {
3616 loggingParent = &a.overridableProperties.Logging_parent
3617 }
3618
Wei Li1c66fc72022-05-09 23:59:14 -07003619 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003620 Manifest: manifestLabelAttribute,
3621 Android_manifest: androidManifestLabelAttribute,
3622 File_contexts: fileContextsLabelAttribute,
3623 Min_sdk_version: minSdkVersion,
3624 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003625 Certificate: certificate,
3626 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003627 Updatable: updatableAttribute,
3628 Installable: installableAttribute,
3629 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3630 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3631 Binaries: binariesLabelListAttribute,
3632 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003633 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003634 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003635 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003636 Tests: testsAttrs,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003637 }
3638
3639 props := bazel.BazelTargetModuleProperties{
3640 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003641 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003642 }
3643
Wei Li1c66fc72022-05-09 23:59:14 -07003644 return attrs, props
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003645}
Yu Liu4ae55d12022-01-05 17:17:23 -08003646
3647// The following conversions are based on this table where the rows are the compile_multilib
3648// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3649// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3650// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3651// should not be compiled.
3652// multib/compile_multilib, 32, 64, both, first
3653// 32, 32/32, none/none, 32/32, none/32
3654// 64, none/none, 64/none, 64/none, 64/none
3655// both, 32/32, 64/none, 32&64/32, 64/32
3656// first, 32/32, 64/none, 64/32, 64/32
3657
3658func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3659 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3660 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3661 switch compileMultilb {
3662 case "both", "32":
3663 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3664 case "first":
3665 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3666 case "64":
3667 // Incompatible, ignore
3668 default:
3669 invalidCompileMultilib(ctx, compileMultilb)
3670 }
3671}
3672
3673func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3674 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3675 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3676 switch compileMultilb {
3677 case "both", "64", "first":
3678 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3679 case "32":
3680 // Incompatible, ignore
3681 default:
3682 invalidCompileMultilib(ctx, compileMultilb)
3683 }
3684}
3685
3686func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3687 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3688 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3689 switch compileMultilb {
3690 case "both":
3691 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3692 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3693 case "first":
3694 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3695 case "32":
3696 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3697 case "64":
3698 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3699 default:
3700 invalidCompileMultilib(ctx, compileMultilb)
3701 }
3702}
3703
3704func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3705 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3706 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3707 switch compileMultilb {
3708 case "both", "first":
3709 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3710 case "32":
3711 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3712 case "64":
3713 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3714 default:
3715 invalidCompileMultilib(ctx, compileMultilb)
3716 }
3717}
3718
3719func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3720 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3721 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3722}
3723
3724func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3725 list := bazel.LabelListAttribute{}
3726 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3727 nativeSharedLibs.Native_shared_libs_32.Append(list)
3728}
3729
3730func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3731 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3732 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3733}
3734
3735func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3736 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3737 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3738}
3739
3740func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3741 labelListAttr *bazel.LabelListAttribute) {
3742 list := bazel.LabelListAttribute{}
3743 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3744 labelListAttr.Append(list)
3745}
3746
3747func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3748 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3749}