blob: 51c67d0044b352cd03555b8dd54974d27c8bb0aa [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)
Cole Faust912bc882023-03-08 12:29:50 -080053 ctx.RegisterModuleType("apex_defaults", DefaultsFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000054 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
Jooyung Hanaf730952023-02-28 14:13:38 +0900102 // By default, file_contexts is amended by force-labelling / and /apex_manifest.pb as system_file
103 // to avoid mistakes. When set as true, no force-labelling.
104 Use_file_contexts_as_is *bool
105
Jingwen Chendea7a642023-03-28 11:30:50 +0000106 // Path to the canned fs config file for customizing file's
107 // uid/gid/mod/capabilities. The content of this file is appended to the
108 // default config, so that the custom entries are preferred. The format is
109 // /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where
110 // path_or_glob is a path or glob pattern for a file or set of files,
111 // uid/gid are numerial values of user ID and group ID, mode is octal value
112 // for the file mode, and cap is hexadecimal value for the capability.
Jiyong Park038e8522021-12-13 23:56:35 +0900113 Canned_fs_config *string `android:"path"`
114
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900115 ApexNativeDependencies
116
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900117 Multilib apexMultilibProperties
118
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000119 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
120 Rros []string
121
Anton Hanssone7545852023-02-24 11:06:07 +0000122 // List of bootclasspath fragments that are embedded inside this APEX bundle.
123 Bootclasspath_fragments []string
124
125 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
126 Systemserverclasspath_fragments []string
127
128 // List of java libraries that are embedded inside this APEX bundle.
129 Java_libs []string
130
Sundong Ahn80c04892021-11-23 00:57:19 +0000131 // List of sh binaries that are embedded inside this APEX bundle.
132 Sh_binaries []string
133
Paul Duffin3abc1742021-03-15 19:32:23 +0000134 // List of platform_compat_config files that are embedded inside this APEX bundle.
135 Compat_configs []string
136
Jiyong Park12a719c2021-01-07 15:31:24 +0900137 // List of filesystem images that are embedded inside this APEX bundle.
138 Filesystems []string
139
Liz Kammerbd58e742023-05-11 15:58:13 +0000140 // The minimum SDK version that this APEX must support at minimum. This is usually set to
141 // the SDK version that the APEX was first introduced.
142 Min_sdk_version *string
143
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900144 // Whether this APEX is considered updatable or not. When set to true, this will enforce
145 // additional rules for making sure that the APEX is truly updatable. To be updatable,
146 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000147 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900148 Updatable *bool
149
Jiyong Parkf4020582021-11-29 12:37:10 +0900150 // Marks that this APEX is designed to be updatable in the future, although it's not
151 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
152 // updatable APEXes. Currently, this disables the size optimization, so that the size of
153 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
154 // false.
155 Future_updatable *bool
156
Jiyong Park1bc84122021-06-22 20:23:05 +0900157 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
158 // false`. Default is false.
159 Platform_apis *bool
160
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900161 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
162 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900163 Installable *bool
164
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900165 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
166 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
167 Use_vndk_as_stable *bool
168
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900169 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
170 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
171 // container. When set to zip, contents are stored in a zip container directly. This type is
172 // mostly for host-side debugging. When set to both, the two types are both built. Default
173 // is 'image'.
174 Payload_type *string
175
Huang Jianan13cac632021-08-02 15:02:17 +0800176 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
177 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900178 Payload_fs_type *string
179
180 // For telling the APEX to ignore special handling for system libraries such as bionic.
181 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900182 Ignore_system_library_special_case *bool
183
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100184 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100185 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100186 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900187
188 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
189 // used in tests.
190 Test_only_unsigned_payload *bool
191
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000192 // Whenever apex should be compressed, regardless of product flag used. Should be only
193 // used in tests.
194 Test_only_force_compression *bool
195
Jooyung Han09c11ad2021-10-27 03:45:31 +0900196 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
197 // with the tool to sign payload contents.
198 Custom_sign_tool *string
199
Dennis Shenaf41bc12022-08-03 16:46:43 +0000200 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
201 // in a special way that include the digest of the lib file under /lib(64)?
202 Dynamic_common_lib_apex *bool
203
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100204 // Canonical name of this APEX bundle. Used to determine the path to the
205 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
206 // apex mutator variations. For override_apex modules, this is the name of the
207 // overridden base module.
208 ApexVariationName string `blueprint:"mutated"`
209
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900210 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900211
212 // List of sanitizer names that this APEX is enabled for
213 SanitizerNames []string `blueprint:"mutated"`
214
215 PreventInstall bool `blueprint:"mutated"`
216
217 HideFromMake bool `blueprint:"mutated"`
218
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900219 // Internal package method for this APEX. When payload_type is image, this can be either
220 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
221 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900222 ApexType apexPackaging `blueprint:"mutated"`
Sam Delmericoca816532023-06-02 14:09:50 -0400223
224 // Name that dependencies can specify in their apex_available properties to refer to this module.
Sam Delmericoc3df1132023-06-06 12:14:23 -0400225 // If not specified, this defaults to Soong module name. This must be the name of a Soong module.
Sam Delmericoca816532023-06-02 14:09:50 -0400226 Apex_available_name *string
Sam Delmerico6d65a0f2023-06-05 15:55:57 -0400227
228 // Variant version of the mainline module. Must be an integer between 0-9
229 Variant_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900230}
231
232type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900233 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900234 Native_shared_libs []string
235
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900236 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900237 Jni_libs []string
238
Colin Cross70572ed2022-11-02 13:14:20 -0700239 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900240 Rust_dyn_libs []string
241
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900242 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900243 Binaries []string
244
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900245 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900246 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900247
248 // List of filesystem images that are embedded inside this APEX bundle.
249 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700250
251 // List of native libraries to exclude from this APEX.
252 Exclude_native_shared_libs []string
253
254 // List of JNI libraries to exclude from this APEX.
255 Exclude_jni_libs []string
256
257 // List of rust dyn libraries to exclude from this APEX.
258 Exclude_rust_dyn_libs []string
259
260 // List of native executables to exclude from this APEX.
261 Exclude_binaries []string
262
263 // List of native tests to exclude from this APEX.
264 Exclude_tests []string
265
266 // List of filesystem images to exclude from this APEX bundle.
267 Exclude_filesystems []string
268}
269
270// Merge combines another ApexNativeDependencies into this one
271func (a *ApexNativeDependencies) Merge(b ApexNativeDependencies) {
272 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs...)
273 a.Jni_libs = append(a.Jni_libs, b.Jni_libs...)
274 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
275 a.Binaries = append(a.Binaries, b.Binaries...)
276 a.Tests = append(a.Tests, b.Tests...)
277 a.Filesystems = append(a.Filesystems, b.Filesystems...)
278
279 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
280 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
281 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
282 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
283 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
284 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900285}
286
287type apexMultilibProperties struct {
288 // Native dependencies whose compile_multilib is "first"
289 First ApexNativeDependencies
290
291 // Native dependencies whose compile_multilib is "both"
292 Both ApexNativeDependencies
293
294 // Native dependencies whose compile_multilib is "prefer32"
295 Prefer32 ApexNativeDependencies
296
297 // Native dependencies whose compile_multilib is "32"
298 Lib32 ApexNativeDependencies
299
300 // Native dependencies whose compile_multilib is "64"
301 Lib64 ApexNativeDependencies
302}
303
304type apexTargetBundleProperties struct {
305 Target struct {
306 // Multilib properties only for android.
307 Android struct {
308 Multilib apexMultilibProperties
309 }
310
311 // Multilib properties only for host.
312 Host struct {
313 Multilib apexMultilibProperties
314 }
315
316 // Multilib properties only for host linux_bionic.
317 Linux_bionic struct {
318 Multilib apexMultilibProperties
319 }
320
321 // Multilib properties only for host linux_glibc.
322 Linux_glibc struct {
323 Multilib apexMultilibProperties
324 }
325 }
326}
327
Jiyong Park59140302020-12-14 18:44:04 +0900328type apexArchBundleProperties struct {
329 Arch struct {
330 Arm struct {
331 ApexNativeDependencies
332 }
333 Arm64 struct {
334 ApexNativeDependencies
335 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700336 Riscv64 struct {
337 ApexNativeDependencies
338 }
Jiyong Park59140302020-12-14 18:44:04 +0900339 X86 struct {
340 ApexNativeDependencies
341 }
342 X86_64 struct {
343 ApexNativeDependencies
344 }
345 }
346}
347
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900348// These properties can be used in override_apex to override the corresponding properties in the
349// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900350type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900351 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900352 Apps []string
353
Daniel Norman5a3ce132021-08-26 15:44:43 -0700354 // List of prebuilt files that are embedded inside this APEX bundle.
355 Prebuilts []string
356
markchien7c803b82021-08-26 22:10:06 +0800357 // List of BPF programs inside this APEX bundle.
358 Bpfs []string
359
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900360 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
361 // Soong). This does not completely prevent installation of the overridden binaries, but if
362 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
363 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900364 Overrides []string
365
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900366 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900367 Logging_parent string
368
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900369 // Apex Container package name. Override value for attribute package:name in
370 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900371 Package_name string
372
373 // A txt file containing list of files that are allowed to be included in this APEX.
374 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700375
376 // Name of the apex_key module that provides the private key to sign this APEX bundle.
377 Key *string
378
379 // Specifies the certificate and the private key to sign the zip container of this APEX. If
380 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
381 // as the certificate and the private key, respectively. If this is ":module", then the
382 // certificate and the private key are provided from the android_app_certificate module
383 // named "module".
384 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400385
386 // Whether this APEX can be compressed or not. Setting this property to false means this
387 // APEX will never be compressed. When set to true, APEX will be compressed if other
388 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
389 // Default: false.
390 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000391
392 // Trim against a specific Dynamic Common Lib APEX
393 Trim_against *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900394}
395
396type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900397 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900398 android.ModuleBase
399 android.DefaultableModuleBase
400 android.OverridableModuleBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400401 android.BazelModuleBase
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900402 multitree.ExportableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900403
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900404 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900405 properties apexBundleProperties
406 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900407 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900408 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900409 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900410
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900411 ///////////////////////////////////////////////////////////////////////////////////////////
412 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900413
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +0100414 // Keys for apex_payload.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800415 publicKeyFile android.Path
416 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900417
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900418 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800419 containerCertificateFile android.Path
420 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900421
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900422 // Flags for special variants of APEX
423 testApex bool
424 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900425
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900426 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
427 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900428 primaryApexType bool
429
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900430 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900431 suffix string
432
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900433 // File system type of apex_payload.img
434 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900435
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900436 // Whether to create symlink to the system file instead of having a file inside the apex or
437 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900438 linkToSystemLib bool
439
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900440 // List of files to be included in this APEX. This is filled in the first part of
441 // GenerateAndroidBuildActions.
442 filesInfo []apexFile
443
Jingwen Chen29743c82023-01-25 17:49:46 +0000444 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
445 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900446
447 ///////////////////////////////////////////////////////////////////////////////////////////
448 // Outputs (final and intermediates)
449
450 // Processed apex manifest in JSONson format (for Q)
451 manifestJsonOut android.WritablePath
452
453 // Processed apex manifest in PB format (for R+)
454 manifestPbOut android.WritablePath
455
456 // Processed file_contexts files
457 fileContexts android.WritablePath
458
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900459 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900460 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900461 outputFile android.WritablePath
462
Jooyung Hana6d36672022-02-24 13:58:07 +0900463 // The built uncompressed .apex file.
464 outputApexFile android.WritablePath
465
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900466 // The built APEX file in app bundle format. This file is not directly installed to the
467 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
468 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
469 // system) to be merged into a single app bundle file that Play accepts. See
470 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
471 bundleModuleFile android.WritablePath
472
Colin Cross6340ea52021-11-04 12:01:18 -0700473 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900474 installDir android.InstallPath
475
Colin Cross6340ea52021-11-04 12:01:18 -0700476 // Path where this APEX was installed.
477 installedFile android.InstallPath
478
479 // Installed locations of symlinks for backward compatibility.
480 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900481
482 // Text file having the list of individual files that are included in this APEX. Used for
483 // debugging purpose.
484 installedFilesFile android.WritablePath
485
486 // List of module names that this APEX is including (to be shown via *-deps-info target).
487 // Used for debugging purpose.
488 android.ApexBundleDepsInfo
489
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900490 // Optional list of lint report zip files for apexes that contain java or app modules
491 lintReports android.Paths
492
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000493 isCompressed bool
494
sophiezc80a2b32020-11-12 16:39:19 +0000495 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700496 nativeApisUsedByModuleFile android.ModuleOutPath
497 nativeApisBackedByModuleFile android.ModuleOutPath
498 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800499
500 // Collect the module directory for IDE info in java/jdeps.go.
501 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900502}
503
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900504// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900505type apexFileClass int
506
Jooyung Han72bd2f82019-10-23 16:46:38 +0900507const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900508 app apexFileClass = iota
509 appSet
510 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900511 goBinary
512 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900513 nativeExecutable
514 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900515 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900516 pyBinary
517 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900518)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900519
Jingwen Chen2d37b642023-03-14 16:11:38 +0000520var (
521 classes = map[string]apexFileClass{
522 "app": app,
523 "appSet": appSet,
524 "etc": etc,
525 "goBinary": goBinary,
526 "javaSharedLib": javaSharedLib,
527 "nativeExecutable": nativeExecutable,
528 "nativeSharedLib": nativeSharedLib,
529 "nativeTest": nativeTest,
530 "pyBinary": pyBinary,
531 "shBinary": shBinary,
532 }
533)
534
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900535// apexFile represents a file in an APEX bundle. This is created during the first half of
536// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
537// of the function, this is used to create commands that copies the files into a staging directory,
538// where they are packaged into the APEX file. This struct is also used for creating Make modules
539// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900540type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900541 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000542 builtFile android.Path
543 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900544 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000545 customStem string
546 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900547
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900548 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
549 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
550 // suffix>]
551 androidMkModuleName string // becomes LOCAL_MODULE
552 class apexFileClass // becomes LOCAL_MODULE_CLASS
553 moduleDir string // becomes LOCAL_PATH
554 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
555 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
556 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
557 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900558
559 jacocoReportClassesFile android.Path // only for javalibs and apps
560 lintDepSets java.LintDepSets // only for javalibs and apps
561 certificate java.Certificate // only for apps
562 overriddenPackageName string // only for apps
563
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900564 transitiveDep bool
565 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900566
Jiyong Park57621b22021-01-20 20:33:11 +0900567 multilib string
568
Jingwen Chen2d37b642023-03-14 16:11:38 +0000569 isBazelPrebuilt bool
570 unstrippedBuiltFile android.Path
571 arch string
572
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900573 // TODO(jiyong): remove this
574 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900575}
576
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900577// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900578func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
579 ret := apexFile{
580 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900581 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900582 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900583 class: class,
584 module: module,
585 }
586 if module != nil {
587 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900588 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900589 ret.requiredModuleNames = module.RequiredModuleNames()
590 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
591 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900592 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900593 }
594 return ret
595}
596
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900597func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900598 return af.builtFile != nil && af.builtFile.String() != ""
599}
600
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900601// apexRelativePath returns the relative path of the given path from the install directory of this
602// apexFile.
603// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900604func (af *apexFile) apexRelativePath(path string) string {
605 return filepath.Join(af.installDir, path)
606}
607
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900608// path returns path of this apex file relative to the APEX root
609func (af *apexFile) path() string {
610 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900611}
612
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900613// stem returns the base filename of this apex file
614func (af *apexFile) stem() string {
615 if af.customStem != "" {
616 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900617 }
618 return af.builtFile.Base()
619}
620
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900621// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
622func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900623 var ret []string
624 for _, symlink := range af.symlinks {
625 ret = append(ret, af.apexRelativePath(symlink))
626 }
627 return ret
628}
629
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900630// availableToPlatform tests whether this apexFile is from a module that can be installed to the
631// platform.
632func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900633 if af.module == nil {
634 return false
635 }
636 if am, ok := af.module.(android.ApexModule); ok {
637 return am.AvailableFor(android.AvailableToPlatform)
638 }
639 return false
640}
641
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900642////////////////////////////////////////////////////////////////////////////////////////////////////
643// Mutators
644//
645// Brief description about mutators for APEX. The following three mutators are the most important
646// ones.
647//
648// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
649// to the (direct) dependencies of this APEX bundle.
650//
Paul Duffin949abc02020-12-08 10:34:30 +0000651// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900652// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
653// modules are marked as being included in the APEX via BuildForApex().
654//
Paul Duffin949abc02020-12-08 10:34:30 +0000655// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
656// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900657
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900658type dependencyTag struct {
659 blueprint.BaseDependencyTag
660 name string
661
662 // Determines if the dependent will be part of the APEX payload. Can be false for the
663 // dependencies to the signing key module, etc.
664 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000665
666 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
667 // replacement. This is needed because some prebuilt modules do not provide all the information
668 // needed by the apex.
669 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000670
671 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
672 // also be added as exported members of that SDK.
673 memberType android.SdkMemberType
674}
675
676func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
677 return d.memberType
678}
679
680func (d *dependencyTag) ExportMember() bool {
681 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900682}
683
Paul Duffin520917a2022-05-13 13:01:59 +0000684func (d *dependencyTag) String() string {
685 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
686}
687
688func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000689 return !d.sourceOnly
690}
691
692var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000693var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000694
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900695var (
Paul Duffin520917a2022-05-13 13:01:59 +0000696 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
697 bpfTag = &dependencyTag{name: "bpf", payload: true}
698 certificateTag = &dependencyTag{name: "certificate"}
Dennis Shene2ed70c2023-01-11 14:15:43 +0000699 dclaTag = &dependencyTag{name: "dcla"}
Paul Duffin520917a2022-05-13 13:01:59 +0000700 executableTag = &dependencyTag{name: "executable", payload: true}
701 fsTag = &dependencyTag{name: "filesystem", payload: true}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000702 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
703 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType}
Paul Duffinfcf79852022-07-20 14:18:24 +0000704 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000705 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
706 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
707 keyTag = &dependencyTag{name: "key"}
708 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
709 rroTag = &dependencyTag{name: "rro", payload: true}
710 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
711 testForTag = &dependencyTag{name: "test for"}
712 testTag = &dependencyTag{name: "test", payload: true}
713 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900714)
715
716// TODO(jiyong): shorten this function signature
717func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900718 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900719 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900720 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900721
722 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900723 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900724 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
725 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900726 }
727
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900728 // Use *FarVariation* to be able to depend on modules having conflicting variations with
729 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
730 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700731 ctx.AddFarVariationDependencies(binVariations, executableTag,
732 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
733 ctx.AddFarVariationDependencies(binVariations, testTag,
734 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
735 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
736 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
737 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
738 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
739 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
740 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
741 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
742 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900743}
744
745func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900746 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900747 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
748 } else {
749 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
750 if ctx.Os().Bionic() {
751 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
752 } else {
753 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
754 }
755 }
756}
757
Jooyung Hand045ebc2022-12-06 15:23:57 +0900758// getImageVariationPair returns a pair for the image variation name as its
759// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
760// suffix indicates the vndk version when it's vendor or product.
761// getImageVariation can simply join the result of this function to get the
762// image variation name.
763func (a *apexBundle) getImageVariationPair(deviceConfig android.DeviceConfig) (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900764 if a.vndkApex {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900765 return cc.VendorVariationPrefix, a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900766 }
767
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900768 var prefix string
769 var vndkVersion string
770 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000771 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900772 prefix = cc.VendorVariationPrefix
773 vndkVersion = deviceConfig.VndkVersion()
774 } else if a.ProductSpecific() {
775 prefix = cc.ProductVariationPrefix
776 vndkVersion = deviceConfig.ProductVndkVersion()
777 }
778 }
779 if vndkVersion == "current" {
780 vndkVersion = deviceConfig.PlatformVndkVersion()
781 }
782 if vndkVersion != "" {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900783 return prefix, vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900784 }
785
Jooyung Hand045ebc2022-12-06 15:23:57 +0900786 return android.CoreVariation, "" // The usual case
787}
788
789// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
790// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
791func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
792 prefix, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig())
793 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900794}
795
796func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900797 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
798 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
799 // each target os/architectures, appropriate dependencies are selected by their
800 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900801 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900802 imageVariation := a.getImageVariation(ctx)
803
804 a.combineProperties(ctx)
805
806 has32BitTarget := false
807 for _, target := range targets {
808 if target.Arch.ArchType.Multilib == "lib32" {
809 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000810 }
811 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900812 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900813 // Don't include artifacts for the host cross targets because there is no way for us
814 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900815 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900816 continue
817 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000818
Colin Cross70572ed2022-11-02 13:14:20 -0700819 var deps ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000820
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900821 // Add native modules targeting both ABIs. When multilib.* is omitted for
822 // native_shared_libs/jni_libs/tests, it implies multilib.both
Colin Cross70572ed2022-11-02 13:14:20 -0700823 deps.Merge(a.properties.Multilib.Both)
824 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900825 Native_shared_libs: a.properties.Native_shared_libs,
826 Tests: a.properties.Tests,
827 Jni_libs: a.properties.Jni_libs,
828 Binaries: nil,
829 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900830
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900831 // Add native modules targeting the first ABI When multilib.* is omitted for
832 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900833 isPrimaryAbi := i == 0
834 if isPrimaryAbi {
Colin Cross70572ed2022-11-02 13:14:20 -0700835 deps.Merge(a.properties.Multilib.First)
836 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900837 Native_shared_libs: nil,
838 Tests: nil,
839 Jni_libs: nil,
840 Binaries: a.properties.Binaries,
841 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900842 }
843
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900844 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900845 switch target.Arch.ArchType.Multilib {
846 case "lib32":
Colin Cross70572ed2022-11-02 13:14:20 -0700847 deps.Merge(a.properties.Multilib.Lib32)
848 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900849 case "lib64":
Colin Cross70572ed2022-11-02 13:14:20 -0700850 deps.Merge(a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900851 if !has32BitTarget {
Colin Cross70572ed2022-11-02 13:14:20 -0700852 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900853 }
854 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900855
Jiyong Park59140302020-12-14 18:44:04 +0900856 // Add native modules targeting a specific arch variant
857 switch target.Arch.ArchType {
858 case android.Arm:
Colin Cross70572ed2022-11-02 13:14:20 -0700859 deps.Merge(a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900860 case android.Arm64:
Colin Cross70572ed2022-11-02 13:14:20 -0700861 deps.Merge(a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700862 case android.Riscv64:
Colin Cross70572ed2022-11-02 13:14:20 -0700863 deps.Merge(a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900864 case android.X86:
Colin Cross70572ed2022-11-02 13:14:20 -0700865 deps.Merge(a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900866 case android.X86_64:
Colin Cross70572ed2022-11-02 13:14:20 -0700867 deps.Merge(a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900868 default:
869 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
870 }
871
Colin Cross70572ed2022-11-02 13:14:20 -0700872 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Sundong Ahn80c04892021-11-23 00:57:19 +0000873 ctx.AddFarVariationDependencies([]blueprint.Variation{
874 {Mutator: "os", Variation: target.OsVariation()},
875 {Mutator: "arch", Variation: target.ArchVariation()},
876 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900877 }
878
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900879 // Common-arch dependencies come next
880 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000881 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.properties.Rros...)
Anton Hanssone7545852023-02-24 11:06:07 +0000882 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.properties.Bootclasspath_fragments...)
883 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.properties.Systemserverclasspath_fragments...)
884 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
Jiyong Park12a719c2021-01-07 15:31:24 +0900885 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000886 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100887}
888
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900889// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900890func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
891 if a.overridableProperties.Allowed_files != nil {
892 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100893 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900894
895 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
896 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800897 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700898 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
899 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
900 // regardless of the TARGET_PREFER_* setting. See b/144532908
901 arches := ctx.DeviceConfig().Arches()
902 if len(arches) != 0 {
903 archForPrebuiltEtc := arches[0]
904 for _, arch := range arches {
905 // Prefer 64-bit arch if there is any
906 if arch.ArchType.Multilib == "lib64" {
907 archForPrebuiltEtc = arch
908 break
909 }
910 }
911 ctx.AddFarVariationDependencies([]blueprint.Variation{
912 {Mutator: "os", Variation: ctx.Os().String()},
913 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
914 }, prebuiltTag, prebuilts...)
915 }
916 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700917
918 // Dependencies for signing
919 if String(a.overridableProperties.Key) == "" {
920 ctx.PropertyErrorf("key", "missing")
921 return
922 }
923 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
924
925 cert := android.SrcIsModule(a.getCertString(ctx))
926 if cert != "" {
927 ctx.AddDependency(ctx.Module(), certificateTag, cert)
928 // empty cert is not an error. Cert and private keys will be directly found under
929 // PRODUCT_DEFAULT_DEV_CERTIFICATE
930 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100931}
932
Dennis Shene2ed70c2023-01-11 14:15:43 +0000933func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
934 if !mctx.Config().ApexTrimEnabled() {
935 return
936 }
937 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
938 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
939 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
940 } else if o, ok := mctx.Module().(*OverrideApex); ok {
941 for _, p := range o.GetProperties() {
942 properties, ok := p.(*overridableProperties)
943 if !ok {
944 continue
945 }
946 if properties.Trim_against != nil {
947 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
948 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
949 }
950 }
951 }
952}
953
954type DCLAInfo struct {
955 ProvidedLibs []string
956}
957
958var DCLAInfoProvider = blueprint.NewMutatorProvider(DCLAInfo{}, "apex_info")
959
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900960type ApexBundleInfo struct {
961 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100962}
963
Paul Duffin949abc02020-12-08 10:34:30 +0000964var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900965
Paul Duffina7d6a892020-12-07 17:39:59 +0000966var _ ApexInfoMutator = (*apexBundle)(nil)
967
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100968func (a *apexBundle) ApexVariationName() string {
969 return a.properties.ApexVariationName
970}
971
Paul Duffina7d6a892020-12-07 17:39:59 +0000972// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900973// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
974// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
975// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
976// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000977//
978// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
979// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
980// The apexMutator uses that list to create module variants for the apexes to which it belongs.
981// The relationship between module variants and apexes is not one-to-one as variants will be
982// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000983func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900984
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900985 // The VNDK APEX is special. For the APEX, the membership is described in a very different
986 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
987 // libraries are self-identified by their vndk.enabled properties. There is no need to run
988 // this mutator for the APEX as nothing will be collected. So, let's return fast.
989 if a.vndkApex {
990 return
991 }
992
993 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
994 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
995 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
996 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
997 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900998 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
999 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +09001000 if proptools.Bool(a.properties.Use_vndk_as_stable) {
1001 if !useVndk {
1002 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
1003 }
Jooyung Han02873da2023-03-22 17:41:03 +09001004 if a.minSdkVersionValue(mctx) != "" {
1005 mctx.PropertyErrorf("use_vndk_as_stable", "not supported when min_sdk_version is set")
1006 }
Jooyung Hanc5a96762022-02-04 11:54:50 +09001007 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
1008 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
1009 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
1010 }
1011 })
1012 if mctx.Failed() {
1013 return
1014 }
Jooyung Handf78e212020-07-22 15:54:47 +09001015 }
1016
Colin Cross56a83212020-09-15 18:30:11 -07001017 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001018 am, ok := child.(android.ApexModule)
1019 if !ok || !am.CanHaveApexVariants() {
1020 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +09001021 }
Paul Duffin573989d2021-03-17 13:25:29 +00001022 depTag := mctx.OtherModuleDependencyTag(child)
1023
1024 // Check to see if the tag always requires that the child module has an apex variant for every
1025 // apex variant of the parent module. If it does not then it is still possible for something
1026 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
1027 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
1028 return true
1029 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001030 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001031 return false
1032 }
Jooyung Handf78e212020-07-22 15:54:47 +09001033 if excludeVndkLibs {
1034 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
1035 return false
1036 }
1037 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001038 // By default, all the transitive dependencies are collected, unless filtered out
1039 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001040 return true
1041 }
1042
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001043 // Records whether a certain module is included in this apexBundle via direct dependency or
1044 // inndirect dependency.
1045 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001046 mctx.WalkDeps(func(child, parent android.Module) bool {
1047 if !continueApexDepsWalk(child, parent) {
1048 return false
1049 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001050 // If the parent is apexBundle, this child is directly depended.
1051 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001052 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001053 contents[depName] = contents[depName].Add(directDep)
1054 return true
1055 })
1056
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001057 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001058 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -07001059 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
1060 Contents: apexContents,
1061 })
1062
Jooyung Haned124c32021-01-26 11:43:46 +09001063 minSdkVersion := a.minSdkVersion(mctx)
1064 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1065 if minSdkVersion.IsNone() {
1066 minSdkVersion = android.FutureApiLevel
1067 }
1068
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001069 // This is the main part of this mutator. Mark the collected dependencies that they need to
1070 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001071
Jooyung Han63dff462023-02-09 00:11:27 +00001072 apexVariationName := mctx.ModuleName() // could be com.android.foo
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001073 a.properties.ApexVariationName = apexVariationName
Spandan Dase8173a82023-04-12 17:14:11 +00001074 testApexes := []string{}
1075 if a.testApex {
1076 testApexes = []string{apexVariationName}
1077 }
Colin Cross56a83212020-09-15 18:30:11 -07001078 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001079 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001080 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001081 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001082 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001083 InApexVariants: []string{apexVariationName},
1084 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001085 ApexContents: []*android.ApexContents{apexContents},
Spandan Dase8173a82023-04-12 17:14:11 +00001086 TestApexes: testApexes,
Colin Cross56a83212020-09-15 18:30:11 -07001087 }
Colin Cross56a83212020-09-15 18:30:11 -07001088 mctx.WalkDeps(func(child, parent android.Module) bool {
1089 if !continueApexDepsWalk(child, parent) {
1090 return false
1091 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001092 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001093 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001094 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001095
1096 if a.dynamic_common_lib_apex() {
1097 mctx.SetProvider(DCLAInfoProvider, DCLAInfo{
1098 ProvidedLibs: a.properties.Native_shared_libs,
1099 })
1100 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001101}
1102
Paul Duffina7d6a892020-12-07 17:39:59 +00001103type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001104 // ApexVariationName returns the name of the APEX variation to use in the apex
1105 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1106 ApexVariationName() string
1107
Paul Duffina7d6a892020-12-07 17:39:59 +00001108 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1109 // depended upon by an apex and which require an apex specific variant.
1110 ApexInfoMutator(android.TopDownMutatorContext)
1111}
1112
1113// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1114// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001115// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001116func apexInfoMutator(mctx android.TopDownMutatorContext) {
1117 if !mctx.Module().Enabled() {
1118 return
1119 }
1120
1121 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1122 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001123 }
Spandan Das42e89502022-05-06 22:12:55 +00001124 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001125}
1126
Spandan Das66773252022-01-15 00:23:18 +00001127// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
1128// This check is enforced for updatable modules
1129func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
1130 if !mctx.Module().Enabled() {
1131 return
1132 }
Spandan Das08c911f2022-01-21 22:07:26 +00001133 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +00001134 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +00001135 // b/208656169 Do not propagate strict updatability linting to libcore/
1136 // These libs are available on the classpath during compilation
1137 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1138 // Only skip libraries defined in libcore root, not subdirectories
1139 if mctx.OtherModuleDir(child) == "libcore" {
1140 // Do not traverse transitive deps of libcore/ libs
1141 return false
1142 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001143 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1144 return false
1145 }
Spandan Das66773252022-01-15 00:23:18 +00001146 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1147 lintable.SetStrictUpdatabilityLinting(true)
1148 }
1149 // visit transitive deps
1150 return true
1151 })
1152 }
1153}
1154
Spandan Das42e89502022-05-06 22:12:55 +00001155// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1156func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1157 if !mctx.Module().Enabled() {
1158 return
1159 }
1160 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1161 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1162 mctx.VisitDirectDeps(func(module android.Module) {
1163 // ignore android_test_app
1164 if app, ok := module.(*java.AndroidApp); ok {
1165 app.SetUpdatable(true)
1166 }
1167 })
1168 }
1169}
1170
Spandan Das08c911f2022-01-21 22:07:26 +00001171// TODO: b/215736885 Whittle the denylist
1172// Transitive deps of certain mainline modules baseline NewApi errors
1173// Skip these mainline modules for now
1174var (
1175 skipStrictUpdatabilityLintAllowlist = []string{
1176 "com.android.art",
1177 "com.android.art.debug",
1178 "com.android.conscrypt",
1179 "com.android.media",
1180 // test apexes
1181 "test_com.android.art",
1182 "test_com.android.conscrypt",
1183 "test_com.android.media",
1184 "test_jitzygote_com.android.art",
1185 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001186
1187 // TODO: b/215736885 Remove this list
1188 skipLintJavalibAllowlist = []string{
1189 "conscrypt.module.platform.api.stubs",
1190 "conscrypt.module.public.api.stubs",
1191 "conscrypt.module.public.api.stubs.system",
1192 "conscrypt.module.public.api.stubs.module_lib",
1193 "framework-media.stubs",
1194 "framework-media.stubs.system",
1195 "framework-media.stubs.module_lib",
1196 }
Spandan Das08c911f2022-01-21 22:07:26 +00001197)
1198
1199func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1200 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1201}
1202
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001203// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1204// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1205// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001206func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1207 if !mctx.Module().Enabled() {
1208 return
1209 }
1210 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001211 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1212 }
1213}
1214
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001215// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1216// the apex in order to retrieve its contents later.
1217// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001218func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1219 if !mctx.Module().Enabled() {
1220 return
1221 }
Colin Cross56a83212020-09-15 18:30:11 -07001222 if am, ok := mctx.Module().(android.ApexModule); ok {
1223 if testFor := am.TestFor(); len(testFor) > 0 {
1224 mctx.AddFarVariationDependencies([]blueprint.Variation{
1225 {Mutator: "os", Variation: am.Target().OsVariation()},
1226 {"arch", "common"},
1227 }, testForTag, testFor...)
1228 }
1229 }
1230}
1231
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001232// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001233func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1234 if !mctx.Module().Enabled() {
1235 return
1236 }
Colin Cross56a83212020-09-15 18:30:11 -07001237 if _, ok := mctx.Module().(android.ApexModule); ok {
1238 var contents []*android.ApexContents
1239 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1240 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1241 contents = append(contents, abInfo.Contents)
1242 }
1243 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1244 ApexContents: contents,
1245 })
Colin Crossaede88c2020-08-11 12:17:01 -07001246 }
1247}
1248
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001249// markPlatformAvailability marks whether or not a module can be available to platform. A module
1250// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1251// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1252// be) available to platform
1253// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001254func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1255 // Host and recovery are not considered as platform
1256 if mctx.Host() || mctx.Module().InstallInRecovery() {
1257 return
1258 }
1259
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001260 am, ok := mctx.Module().(android.ApexModule)
1261 if !ok {
1262 return
1263 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001264
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001265 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001266
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001267 // If any of the dep is not available to platform, this module is also considered as being
1268 // not available to platform even if it has "//apex_available:platform"
1269 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001270 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001271 // if the dependency crosses apex boundary, don't consider it
1272 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001273 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001274 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1275 availableToPlatform = false
1276 // TODO(b/154889534) trigger an error when 'am' has
1277 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001278 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001279 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001280
Paul Duffinb5769c12021-05-12 16:16:51 +01001281 // Exception 1: check to see if the module always requires it.
1282 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001283 availableToPlatform = true
1284 }
1285
1286 // Exception 2: bootstrap bionic libraries are also always available to platform
1287 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1288 availableToPlatform = true
1289 }
1290
1291 if !availableToPlatform {
1292 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001293 }
1294}
1295
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001296// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001297// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001298func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001299 if !mctx.Module().Enabled() {
1300 return
1301 }
Colin Cross56a83212020-09-15 18:30:11 -07001302
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001303 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001304 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001305 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001306 return
1307 }
1308
1309 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001310 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1311 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001312 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001313 if strings.HasPrefix(apexBundleName, "com.android.art") {
1314 // Create an alias from the platform variant. This is done to make
1315 // test_for dependencies work for modules that are split by the APEX
1316 // mutator, since test_for dependencies always go to the platform variant.
1317 // This doesn't happen for normal APEXes that are disjunct, so only do
1318 // this for the overlapping ART APEXes.
1319 // TODO(b/183882457): Remove this if the test_for functionality is
1320 // refactored to depend on the proper APEX variants instead of platform.
1321 mctx.CreateAliasVariation("", apexBundleName)
1322 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001323 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1324 apexBundleName := o.GetOverriddenModuleName()
1325 if apexBundleName == "" {
1326 mctx.ModuleErrorf("base property is not set")
1327 return
1328 }
1329 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001330 if strings.HasPrefix(apexBundleName, "com.android.art") {
1331 // TODO(b/183882457): See note for CreateAliasVariation above.
1332 mctx.CreateAliasVariation("", apexBundleName)
1333 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001334 }
1335}
Sundong Ahne9b55722019-09-06 17:37:42 +09001336
Paul Duffin6717d882021-06-15 19:09:41 +01001337// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1338// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001339func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001340 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001341 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001342 return !a.vndkApex
1343 }
1344
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001345 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001346}
1347
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001348// See android.UpdateDirectlyInAnyApex
1349// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001350func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1351 if !mctx.Module().Enabled() {
1352 return
1353 }
1354 if am, ok := mctx.Module().(android.ApexModule); ok {
1355 android.UpdateDirectlyInAnyApex(mctx, am)
1356 }
1357}
1358
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001359// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001360type apexPackaging int
1361
1362const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001363 // imageApex is a packaging method where contents are included in a filesystem image which
1364 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001365 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001366
1367 // zipApex is a packaging method where contents are directly included in the zip container.
1368 // This is used for host-side testing - because the contents are easily accessible by
1369 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001370 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001371
1372 // flattendApex is a packaging method where contents are not included in the APEX file, but
1373 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1374 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001375 flattenedApex
1376)
1377
1378const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001379 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001380 imageApexSuffix = ".apex"
1381 imageCapexSuffix = ".capex"
1382 zipApexSuffix = ".zipapex"
1383 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001384
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001385 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001386 imageApexType = "image"
1387 zipApexType = "zip"
1388 flattenedApexType = "flattened"
1389
Dan Willemsen47e1a752021-10-16 18:36:13 -07001390 ext4FsType = "ext4"
1391 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001392 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001393)
1394
1395// The suffix for the output "file", not the module
1396func (a apexPackaging) suffix() string {
1397 switch a {
1398 case imageApex:
1399 return imageApexSuffix
1400 case zipApex:
1401 return zipApexSuffix
1402 default:
1403 panic(fmt.Errorf("unknown APEX type %d", a))
1404 }
1405}
1406
1407func (a apexPackaging) name() string {
1408 switch a {
1409 case imageApex:
1410 return imageApexType
1411 case zipApex:
1412 return zipApexType
1413 default:
1414 panic(fmt.Errorf("unknown APEX type %d", a))
1415 }
1416}
1417
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001418// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1419// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001420func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001421 if !mctx.Module().Enabled() {
1422 return
1423 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001424 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001425 var variants []string
1426 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1427 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001428 // This is the normal case. Note that both image and flattend APEXes are
1429 // created. The image type is installed to the system partition, while the
1430 // flattened APEX is (optionally) installed to the system_ext partition.
1431 // This is mostly for GSI which has to support wide range of devices. If GSI
1432 // is installed on a newer (APEX-capable) device, the image APEX in the
1433 // system will be used. However, if the same GSI is installed on an old
1434 // device which can't support image APEX, the flattened APEX in the
1435 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001436 variants = append(variants, imageApexType, flattenedApexType)
1437 case "zip":
1438 variants = append(variants, zipApexType)
1439 case "both":
1440 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1441 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001442 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001443 return
1444 }
1445
1446 modules := mctx.CreateLocalVariations(variants...)
1447
1448 for i, v := range variants {
1449 switch v {
1450 case imageApexType:
1451 modules[i].(*apexBundle).properties.ApexType = imageApex
1452 case zipApexType:
1453 modules[i].(*apexBundle).properties.ApexType = zipApex
1454 case flattenedApexType:
1455 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001456 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001457 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001458 modules[i].(*apexBundle).MakeAsSystemExt()
1459 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001460 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001461 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001462 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001463 // payload_type is forcibly overridden to "image"
1464 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001465 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001466 }
1467}
1468
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001469var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001470
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001471// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001472func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001473 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001474 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001475 return true
1476}
1477
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001478var _ android.OutputFileProducer = (*apexBundle)(nil)
1479
1480// Implements android.OutputFileProducer
1481func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1482 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001483 case "", android.DefaultDistTag:
1484 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001485 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001486 case imageApexSuffix:
1487 // uncompressed one
1488 if a.outputApexFile != nil {
1489 return android.Paths{a.outputApexFile}, nil
1490 }
1491 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001492 default:
1493 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1494 }
1495}
1496
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001497var _ multitree.Exportable = (*apexBundle)(nil)
1498
1499func (a *apexBundle) Exportable() bool {
1500 if a.properties.ApexType == flattenedApex {
1501 return false
1502 }
1503 return true
1504}
1505
1506func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1507 ret := make(map[string]android.Paths)
1508 ret["apex"] = android.Paths{a.outputFile}
1509 return ret
1510}
1511
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001512var _ cc.Coverage = (*apexBundle)(nil)
1513
1514// Implements cc.Coverage
1515func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1516 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1517}
1518
1519// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001520func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001521 a.properties.PreventInstall = true
1522}
1523
1524// Implements cc.Coverage
1525func (a *apexBundle) HideFromMake() {
1526 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001527 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1528 // TODO(ccross): untangle these
1529 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001530}
1531
1532// Implements cc.Coverage
1533func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1534 a.properties.IsCoverageVariant = coverage
1535}
1536
1537// Implements cc.Coverage
1538func (a *apexBundle) EnableCoverageIfNeeded() {}
1539
1540var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1541
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001542// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001543func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001544 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001545}
1546
Jiyong Parkf4020582021-11-29 12:37:10 +09001547func (a *apexBundle) FutureUpdatable() bool {
1548 return proptools.BoolDefault(a.properties.Future_updatable, false)
1549}
1550
Jiyong Park1bc84122021-06-22 20:23:05 +09001551func (a *apexBundle) UsePlatformApis() bool {
1552 return proptools.BoolDefault(a.properties.Platform_apis, false)
1553}
1554
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001555// getCertString returns the name of the cert that should be used to sign this APEX. This is
1556// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001557func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001558 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001559 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1560 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1561 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001562 if a.vndkApex {
1563 moduleName = vndkApexName
1564 }
1565 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001566 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001567 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001568 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001569 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001570}
1571
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001572// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001573func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001574 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001575}
1576
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001577// See the generate_hashtree property
1578func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001579 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001580}
1581
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001582// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001583func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1584 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1585}
1586
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001587// See the test_only_force_compression property
1588func (a *apexBundle) testOnlyShouldForceCompression() bool {
1589 return proptools.Bool(a.properties.Test_only_force_compression)
1590}
1591
Dennis Shenaf41bc12022-08-03 16:46:43 +00001592// See the dynamic_common_lib_apex property
1593func (a *apexBundle) dynamic_common_lib_apex() bool {
1594 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1595}
1596
Dennis Shene2ed70c2023-01-11 14:15:43 +00001597// See the list of libs to trim
1598func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1599 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1600 if len(dclaModules) > 1 {
1601 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1602 }
1603 if len(dclaModules) > 0 {
1604 DCLAInfo := ctx.OtherModuleProvider(dclaModules[0], DCLAInfoProvider).(DCLAInfo)
1605 return DCLAInfo.ProvidedLibs
1606 }
1607 return []string{}
1608}
1609
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001610// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1611// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1612// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001613
Jiyong Parkf97782b2019-02-13 20:28:58 +09001614func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1615 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1616 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1617 }
1618}
1619
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001620func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001621 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1622 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001623 }
1624
1625 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001626 var globalSanitizerNames []string
Jiyong Park388ef3f2019-01-28 19:47:32 +09001627 if a.Host() {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001628 globalSanitizerNames = config.SanitizeHost()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001629 } else {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001630 arches := config.SanitizeDeviceArch()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001631 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001632 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001633 }
1634 }
1635 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001636}
1637
Jooyung Han8ce8db92020-05-15 19:05:05 +09001638func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001639 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1640 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001641 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001642 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001643 for _, target := range ctx.MultiTargets() {
1644 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001645 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001646 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001647 Tests: nil,
1648 Jni_libs: nil,
1649 Binaries: nil,
1650 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001651 break
1652 }
1653 }
1654 }
1655}
1656
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001657// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1658// returned apexFile saves information about the Soong module that will be used for creating the
1659// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001660func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001661 // Decide the APEX-local directory by the multilib of the library In the future, we may
1662 // query this to the module.
1663 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001664 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001665 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001666 case "lib32":
1667 dirInApex = "lib"
1668 case "lib64":
1669 dirInApex = "lib64"
1670 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001671 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001672 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001673 }
Jiyong Park1833cef2019-12-13 13:28:36 +09001674 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001675 // Special case for Bionic libs and other libs installed with them. This is to
1676 // prevent those libs from being included in the search path
1677 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1678 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1679 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1680 // will be loaded into the default linker namespace (aka "platform" namespace). If
1681 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1682 // be loaded again into the runtime linker namespace, which will result in double
1683 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001684 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001685 }
Florian Mayer95cd6db2023-03-23 17:48:07 -07001686 // This needs to go after the runtime APEX handling because otherwise we would get
1687 // weird paths like lib64/rel_install_path/bionic rather than
1688 // lib64/bionic/rel_install_path.
1689 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001690
Colin Cross1d487152022-10-03 19:14:46 -07001691 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001692 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1693 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001694}
1695
Jiyong Park1833cef2019-12-13 13:28:36 +09001696func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001697 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001698 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001699 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001700 }
Jooyung Han35155c42020-02-06 17:33:20 +09001701 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001702 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001703 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1704 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001705 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001706 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001707 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001708}
1709
Jiyong Park99644e92020-11-17 22:21:02 +09001710func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1711 dirInApex := "bin"
1712 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1713 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1714 }
Colin Cross1d487152022-10-03 19:14:46 -07001715 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001716 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1717 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1718 return af
1719}
1720
1721func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1722 // Decide the APEX-local directory by the multilib of the library
1723 // In the future, we may query this to the module.
1724 var dirInApex string
1725 switch rustm.Arch().ArchType.Multilib {
1726 case "lib32":
1727 dirInApex = "lib"
1728 case "lib64":
1729 dirInApex = "lib64"
1730 }
1731 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1732 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1733 }
Colin Cross1d487152022-10-03 19:14:46 -07001734 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001735 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1736 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1737}
1738
Cole Faust4d247e62023-01-23 10:14:58 -08001739func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.PythonBinaryModule) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001740 dirInApex := "bin"
1741 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001742 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001743}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001744
Jiyong Park1833cef2019-12-13 13:28:36 +09001745func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001746 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001747 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001748 // NB: Since go binaries are static we don't need the module for anything here, which is
1749 // good since the go tool is a blueprint.Module not an android.Module like we would
1750 // normally use.
Jingwen Chen2d37b642023-03-14 16:11:38 +00001751 //
Jiyong Park1833cef2019-12-13 13:28:36 +09001752 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001753}
1754
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001755func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001756 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001757 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1758 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1759 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001760 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001761 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001762 af.symlinks = sh.Symlinks()
1763 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001764}
1765
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001766func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001767 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001768 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001769 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001770}
1771
atrost6e126252020-01-27 17:01:16 +00001772func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1773 dirInApex := filepath.Join("etc", config.SubDir())
1774 fileToCopy := config.CompatConfig()
1775 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1776}
1777
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001778// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1779// way.
1780type javaModule interface {
1781 android.Module
1782 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001783 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001784 JacocoReportClassesFile() android.Path
1785 LintDepSets() java.LintDepSets
1786 Stem() string
1787}
1788
1789var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001790var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001791var _ javaModule = (*java.SdkLibrary)(nil)
1792var _ javaModule = (*java.DexImport)(nil)
1793var _ javaModule = (*java.SdkLibraryImport)(nil)
1794
Paul Duffin190fdef2021-04-26 10:33:59 +01001795// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001796func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001797 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001798}
1799
1800// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1801func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001802 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001803 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001804 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1805 af.lintDepSets = module.LintDepSets()
1806 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001807 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1808 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1809 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1810 }
1811 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001812 return af
1813}
1814
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001815func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1816 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001817 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001818 dirInApex := "javalib"
1819 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1820 af.customStem = module.Stem() + ".jar.prof"
1821 return &af
1822 }
1823 }
1824 return nil
1825}
1826
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001827// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1828// the same way.
1829type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001830 android.Module
1831 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001832 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001833 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001834 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001835 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001836 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001837 LintDepSets() java.LintDepSets
Andrei Onea580636b2022-08-17 16:53:46 +00001838 PrivAppAllowlist() android.OptionalPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001839}
1840
1841var _ androidApp = (*java.AndroidApp)(nil)
1842var _ androidApp = (*java.AndroidAppImport)(nil)
1843
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001844func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1845 buildId := ctx.Config().BuildId()
1846
1847 // The build ID is used as a suffix for a filename, so ensure that
1848 // the set of characters being used are sanitized.
1849 // - any word character: [a-zA-Z0-9_]
1850 // - dots: .
1851 // - dashes: -
1852 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1853 if !validRegex.MatchString(buildId) {
1854 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1855 }
1856 return buildId
1857}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001858
Andrei Onea580636b2022-08-17 16:53:46 +00001859func apexFilesForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) []apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001860 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001861 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001862 appDir = "priv-app"
1863 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001864
1865 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1866 // so that PackageManager correctly invalidates the existing installed apk
1867 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001868 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001869 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001870
Yo Chiange8128052020-07-23 20:09:18 +08001871 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001872 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001873 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001874 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001875
1876 if app, ok := aapp.(interface {
1877 OverriddenManifestPackageName() string
1878 }); ok {
1879 af.overriddenPackageName = app.OverriddenManifestPackageName()
1880 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04001881
1882 apexFiles := []apexFile{}
Andrei Onea580636b2022-08-17 16:53:46 +00001883
1884 if allowlist := aapp.PrivAppAllowlist(); allowlist.Valid() {
1885 dirInApex := filepath.Join("etc", "permissions")
Sam Delmericob1daccd2023-05-25 14:45:30 -04001886 privAppAllowlist := newApexFile(ctx, allowlist.Path(), aapp.BaseModuleName()+"_privapp", dirInApex, etc, aapp)
Andrei Onea580636b2022-08-17 16:53:46 +00001887 apexFiles = append(apexFiles, privAppAllowlist)
1888 }
1889
Sam Delmericob1daccd2023-05-25 14:45:30 -04001890 apexFiles = append(apexFiles, af)
1891
Andrei Onea580636b2022-08-17 16:53:46 +00001892 return apexFiles
Dario Frenicde2a032019-10-27 00:29:22 +01001893}
1894
Jiyong Park69aeba92020-04-24 21:16:36 +09001895func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1896 rroDir := "overlay"
1897 dirInApex := filepath.Join(rroDir, rro.Theme())
1898 fileToCopy := rro.OutputFile()
1899 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1900 af.certificate = rro.Certificate()
1901
1902 if a, ok := rro.(interface {
1903 OverriddenManifestPackageName() string
1904 }); ok {
1905 af.overriddenPackageName = a.OverriddenManifestPackageName()
1906 }
1907 return af
1908}
1909
Ken Chenfad7f9d2021-11-10 22:02:57 +08001910func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1911 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001912 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1913}
1914
Jiyong Park12a719c2021-01-07 15:31:24 +09001915func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1916 dirInApex := filepath.Join("etc", "fs")
1917 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1918}
1919
Paul Duffin064b70c2020-11-02 17:32:38 +00001920// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001921// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1922// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1923// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001924func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001925 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001926 am, ok := child.(android.ApexModule)
1927 if !ok || !am.CanHaveApexVariants() {
1928 return false
1929 }
1930
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001931 // Filter-out unwanted depedendencies
1932 depTag := ctx.OtherModuleDependencyTag(child)
1933 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1934 return false
1935 }
Paul Duffin520917a2022-05-13 13:01:59 +00001936 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001937 return false
1938 }
1939
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001940 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001941 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001942
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001943 // Visit actually
1944 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001945 })
1946}
1947
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001948// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1949type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001950
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001951const (
1952 ext4 fsType = iota
1953 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001954 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001955)
Artur Satayev849f8442020-04-28 14:57:42 +01001956
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001957func (f fsType) string() string {
1958 switch f {
1959 case ext4:
1960 return ext4FsType
1961 case f2fs:
1962 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001963 case erofs:
1964 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001965 default:
1966 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001967 }
1968}
1969
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001970var _ android.MixedBuildBuildable = (*apexBundle)(nil)
1971
1972func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
Jingwen Chenbad41822023-03-23 03:04:00 +00001973 return a.properties.ApexType == imageApex
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001974}
1975
1976func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
1977 bazelCtx := ctx.Config().BazelContext
1978 bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
1979}
1980
Jingwen Chen889f2f22022-12-16 08:16:01 +00001981// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
1982// override_apex module overriding this apexBundle. An apexBundle can be
1983// overridden by different override_apex modules (e.g. Google or Go variants),
1984// which is handled by the overrides mutators.
1985func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
Jingwen Chen889f2f22022-12-16 08:16:01 +00001986 return a.BazelModuleBase.GetBazelLabel(ctx, a)
1987}
1988
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001989func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
1990 if !a.commonBuildActions(ctx) {
1991 return
1992 }
1993
1994 a.setApexTypeAndSuffix(ctx)
1995 a.setPayloadFsType(ctx)
1996 a.setSystemLibLink(ctx)
1997
1998 if a.properties.ApexType != zipApex {
1999 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2000 }
2001
2002 bazelCtx := ctx.Config().BazelContext
2003 outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx))
2004 if err != nil {
2005 ctx.ModuleErrorf(err.Error())
2006 return
2007 }
2008 a.installDir = android.PathForModuleInstall(ctx, "apex")
Jingwen Chen94098e82023-01-10 14:50:42 +00002009
2010 // Set the output file to .apex or .capex depending on the compression configuration.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002011 a.setCompression(ctx)
Jingwen Chen94098e82023-01-10 14:50:42 +00002012 if a.isCompressed {
Cole Faustb0bfa072023-04-03 14:28:36 -07002013 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedCompressedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002014 } else {
Cole Faustb0bfa072023-04-03 14:28:36 -07002015 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002016 }
2017 a.outputFile = a.outputApexFile
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002018
Sam Delmerico4ed95e22023-02-03 18:12:15 -05002019 if len(outputs.TidyFiles) > 0 {
2020 tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles)
2021 a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles)
2022 }
2023
Liz Kammer0e255ef2022-11-04 16:07:04 -04002024 // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer
2025 // interface if these were set in a provider rather than the module itself
Wei Li32dcdf92022-10-26 22:30:48 -07002026 a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0])
2027 a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1])
2028 a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0])
2029 a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1])
Liz Kammer0e255ef2022-11-04 16:07:04 -04002030
Jingwen Chen29743c82023-01-25 17:49:46 +00002031 // Ensure ApexMkInfo.install_to_system make module names are installed as
2032 // part of a bundled build.
2033 a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...)
Vinh Tranb6803a52022-12-14 11:34:54 -05002034
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002035 apexType := a.properties.ApexType
2036 switch apexType {
2037 case imageApex:
Liz Kammer303978d2022-11-04 16:12:43 -04002038 a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile)
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002039 a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex))
Wei Licc73a052022-11-07 14:25:34 -08002040 a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs))
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002041 // TODO(b/239084755): Generate the java api using.xml file from Bazel.
Jingwen Chen1ec77852022-11-07 14:36:12 +00002042 a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex))
Wei Li78c07de2022-11-08 16:01:05 -08002043 a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002044 installSuffix := imageApexSuffix
2045 if a.isCompressed {
2046 installSuffix = imageCapexSuffix
2047 }
2048 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
2049 a.compatSymlinks.Paths()...)
2050 default:
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002051 panic(fmt.Errorf("internal error: unexpected apex_type for the ProcessBazelQueryResponse: %v", a.properties.ApexType))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002052 }
2053
Jingwen Chen2d37b642023-03-14 16:11:38 +00002054 // filesInfo in mixed mode must retrieve all information about the apex's
2055 // contents completely from the Starlark providers. It should never rely on
2056 // Android.bp information, as they might not exist for fully migrated
2057 // dependencies.
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002058 //
2059 // Prevent accidental writes to filesInfo in the earlier parts Soong by
2060 // asserting it to be nil.
2061 if a.filesInfo != nil {
Jingwen Chen2d37b642023-03-14 16:11:38 +00002062 panic(
2063 fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel. " +
2064 "Did something else set filesInfo before this line of code?"))
2065 }
2066 for _, f := range outputs.PayloadFilesInfo {
2067 fileInfo := apexFile{
2068 isBazelPrebuilt: true,
2069
2070 builtFile: android.PathForBazelOut(ctx, f["built_file"]),
2071 unstrippedBuiltFile: android.PathForBazelOut(ctx, f["unstripped_built_file"]),
2072 androidMkModuleName: f["make_module_name"],
2073 installDir: f["install_dir"],
2074 class: classes[f["class"]],
2075 customStem: f["basename"],
2076 moduleDir: f["package"],
2077 }
2078
2079 arch := f["arch"]
2080 fileInfo.arch = arch
2081 if len(arch) > 0 {
2082 fileInfo.multilib = "lib32"
2083 if strings.HasSuffix(arch, "64") {
2084 fileInfo.multilib = "lib64"
2085 }
2086 }
2087
2088 a.filesInfo = append(a.filesInfo, fileInfo)
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002089 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002090}
2091
2092func (a *apexBundle) setCompression(ctx android.ModuleContext) {
2093 if a.properties.ApexType != imageApex {
2094 a.isCompressed = false
2095 } else if a.testOnlyShouldForceCompression() {
2096 a.isCompressed = true
2097 } else {
2098 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
2099 }
2100}
2101
2102func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
2103 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2104 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2105 // the same library in the system partition, thus effectively sharing the same libraries
2106 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2107 // in the APEX.
2108 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
2109
2110 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2111 // So we can't link them to /system/lib libs which are core variants.
2112 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2113 a.linkToSystemLib = false
2114 }
2115
2116 forced := ctx.Config().ForceApexSymlinkOptimization()
2117 updatable := a.Updatable() || a.FutureUpdatable()
2118
2119 // We don't need the optimization for updatable APEXes, as it might give false signal
2120 // to the system health when the APEXes are still bundled (b/149805758).
2121 if !forced && updatable && a.properties.ApexType == imageApex {
2122 a.linkToSystemLib = false
2123 }
2124
2125 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2126 if ctx.Host() {
2127 a.linkToSystemLib = false
2128 }
2129}
2130
2131func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
2132 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2133 case ext4FsType:
2134 a.payloadFsType = ext4
2135 case f2fsFsType:
2136 a.payloadFsType = f2fs
2137 case erofsFsType:
2138 a.payloadFsType = erofs
2139 default:
2140 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
2141 }
2142}
2143
2144func (a *apexBundle) setApexTypeAndSuffix(ctx android.ModuleContext) {
2145 // Set suffix and primaryApexType depending on the ApexType
2146 buildFlattenedAsDefault := ctx.Config().FlattenApex()
2147 switch a.properties.ApexType {
2148 case imageApex:
2149 if buildFlattenedAsDefault {
2150 a.suffix = imageApexSuffix
2151 } else {
2152 a.suffix = ""
2153 a.primaryApexType = true
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002154 }
2155 case zipApex:
2156 if proptools.String(a.properties.Payload_type) == "zip" {
2157 a.suffix = ""
2158 a.primaryApexType = true
2159 } else {
2160 a.suffix = zipApexSuffix
2161 }
2162 case flattenedApex:
2163 if buildFlattenedAsDefault {
2164 a.suffix = ""
2165 a.primaryApexType = true
2166 } else {
2167 a.suffix = flattenedSuffix
2168 }
2169 }
2170}
2171
2172func (a apexBundle) isCompressable() bool {
2173 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2174}
2175
2176func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2177 a.checkApexAvailability(ctx)
2178 a.checkUpdatable(ctx)
2179 a.CheckMinSdkVersion(ctx)
2180 a.checkStaticLinkingToStubLibraries(ctx)
2181 a.checkStaticExecutables(ctx)
2182 if len(a.properties.Tests) > 0 && !a.testApex {
2183 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2184 return false
2185 }
2186 return true
2187}
2188
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002189type visitorContext struct {
2190 // all the files that will be included in this APEX
2191 filesInfo []apexFile
2192
2193 // native lib dependencies
2194 provideNativeLibs []string
2195 requireNativeLibs []string
2196
2197 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002198
2199 // if true, raise error on duplicate apexFile
2200 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002201}
2202
Jooyung Han862c0d62022-12-21 10:15:37 +09002203func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002204 encountered := make(map[string]apexFile)
2205 for _, f := range vctx.filesInfo {
2206 dest := filepath.Join(f.installDir, f.builtFile.Base())
2207 if e, ok := encountered[dest]; !ok {
2208 encountered[dest] = f
2209 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002210 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2211 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2212 dest, e.builtFile, f.builtFile)
2213 return
2214 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002215 // If a module is directly included and also transitively depended on
2216 // consider it as directly included.
2217 e.transitiveDep = e.transitiveDep && f.transitiveDep
2218 encountered[dest] = e
2219 }
2220 }
2221 vctx.filesInfo = vctx.filesInfo[:0]
2222 for _, v := range encountered {
2223 vctx.filesInfo = append(vctx.filesInfo, v)
2224 }
2225 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2226 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2227 // changes.
2228 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2229 })
2230}
2231
2232func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2233 depTag := ctx.OtherModuleDependencyTag(child)
2234 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2235 return false
2236 }
2237 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2238 return false
2239 }
2240 depName := ctx.OtherModuleName(child)
2241 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2242 switch depTag {
2243 case sharedLibTag, jniLibTag:
2244 isJniLib := depTag == jniLibTag
2245 switch ch := child.(type) {
2246 case *cc.Module:
2247 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2248 fi.isJniLib = isJniLib
2249 vctx.filesInfo = append(vctx.filesInfo, fi)
2250 // Collect the list of stub-providing libs except:
2251 // - VNDK libs are only for vendors
2252 // - bootstrap bionic libs are treated as provided by system
2253 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2254 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2255 }
2256 return true // track transitive dependencies
2257 case *rust.Module:
2258 fi := apexFileForRustLibrary(ctx, ch)
2259 fi.isJniLib = isJniLib
2260 vctx.filesInfo = append(vctx.filesInfo, fi)
2261 return true // track transitive dependencies
2262 default:
2263 propertyName := "native_shared_libs"
2264 if isJniLib {
2265 propertyName = "jni_libs"
2266 }
2267 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2268 }
2269 case executableTag:
2270 switch ch := child.(type) {
2271 case *cc.Module:
2272 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2273 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002274 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002275 if ch.HostToolPath().Valid() {
2276 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2277 }
2278 case bootstrap.GoBinaryTool:
2279 if a.Host() {
2280 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2281 }
2282 case *rust.Module:
2283 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2284 return true // track transitive dependencies
2285 default:
2286 ctx.PropertyErrorf("binaries",
2287 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2288 }
2289 case shBinaryTag:
2290 if csh, ok := child.(*sh.ShBinary); ok {
2291 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2292 } else {
2293 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2294 }
2295 case bcpfTag:
Jiakai Zhangb47cacc2023-05-10 16:40:18 +01002296 _, ok := child.(*java.BootclasspathFragmentModule)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002297 if !ok {
2298 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2299 return false
2300 }
2301
2302 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002303 return true
2304 case sscpfTag:
2305 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2306 ctx.PropertyErrorf("systemserverclasspath_fragments",
2307 "%q is not a systemserverclasspath_fragment module", depName)
2308 return false
2309 }
2310 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2311 vctx.filesInfo = append(vctx.filesInfo, *af)
2312 }
2313 return true
2314 case javaLibTag:
2315 switch child.(type) {
2316 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2317 af := apexFileForJavaModule(ctx, child.(javaModule))
2318 if !af.ok() {
2319 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2320 return false
2321 }
2322 vctx.filesInfo = append(vctx.filesInfo, af)
2323 return true // track transitive dependencies
2324 default:
2325 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2326 }
2327 case androidAppTag:
2328 switch ap := child.(type) {
2329 case *java.AndroidApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002330 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002331 return true // track transitive dependencies
2332 case *java.AndroidAppImport:
Andrei Onea580636b2022-08-17 16:53:46 +00002333 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002334 case *java.AndroidTestHelperApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002335 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002336 case *java.AndroidAppSet:
2337 appDir := "app"
2338 if ap.Privileged() {
2339 appDir = "priv-app"
2340 }
2341 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2342 // suffixed so that PackageManager correctly invalidates the
2343 // existing installed apk in favour of the new APK-in-APEX.
2344 // See bugs for more information.
2345 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2346 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2347 af.certificate = java.PresignedCertificate
2348 vctx.filesInfo = append(vctx.filesInfo, af)
2349 default:
2350 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2351 }
2352 case rroTag:
2353 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2354 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2355 } else {
2356 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2357 }
2358 case bpfTag:
2359 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2360 filesToCopy, _ := bpfProgram.OutputFiles("")
2361 apex_sub_dir := bpfProgram.SubDir()
2362 for _, bpfFile := range filesToCopy {
2363 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2364 }
2365 } else {
2366 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2367 }
2368 case fsTag:
2369 if fs, ok := child.(filesystem.Filesystem); ok {
2370 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2371 } else {
2372 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2373 }
2374 case prebuiltTag:
2375 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2376 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2377 } else {
2378 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2379 }
2380 case compatConfigTag:
2381 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2382 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2383 } else {
2384 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2385 }
2386 case testTag:
2387 if ccTest, ok := child.(*cc.Module); ok {
2388 if ccTest.IsTestPerSrcAllTestsVariation() {
2389 // Multiple-output test module (where `test_per_src: true`).
2390 //
2391 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2392 // We do not add this variation to `filesInfo`, as it has no output;
2393 // however, we do add the other variations of this module as indirect
2394 // dependencies (see below).
2395 } else {
2396 // Single-output test module (where `test_per_src: false`).
2397 af := apexFileForExecutable(ctx, ccTest)
2398 af.class = nativeTest
2399 vctx.filesInfo = append(vctx.filesInfo, af)
2400 }
2401 return true // track transitive dependencies
2402 } else {
2403 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2404 }
2405 case keyTag:
2406 if key, ok := child.(*apexKey); ok {
2407 a.privateKeyFile = key.privateKeyFile
2408 a.publicKeyFile = key.publicKeyFile
2409 } else {
2410 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2411 }
2412 case certificateTag:
2413 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2414 a.containerCertificateFile = dep.Certificate.Pem
2415 a.containerPrivateKeyFile = dep.Certificate.Key
2416 } else {
2417 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2418 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002419 }
2420 return false
2421 }
2422
2423 if a.vndkApex {
2424 return false
2425 }
2426
2427 // indirect dependencies
2428 am, ok := child.(android.ApexModule)
2429 if !ok {
2430 return false
2431 }
2432 // We cannot use a switch statement on `depTag` here as the checked
2433 // tags used below are private (e.g. `cc.sharedDepTag`).
2434 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2435 if ch, ok := child.(*cc.Module); ok {
2436 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2437 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2438 return false
2439 }
2440 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2441 af.transitiveDep = true
2442
2443 // Always track transitive dependencies for host.
2444 if a.Host() {
2445 vctx.filesInfo = append(vctx.filesInfo, af)
2446 return true
2447 }
2448
2449 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2450 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2451 // If the dependency is a stubs lib, don't include it in this APEX,
2452 // but make sure that the lib is installed on the device.
2453 // In case no APEX is having the lib, the lib is installed to the system
2454 // partition.
2455 //
2456 // Always include if we are a host-apex however since those won't have any
2457 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002458 //
2459 // Skip the dependency in unbundled builds where the device image is not
2460 // being built.
2461 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002462 // we need a module name for Make
2463 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002464 if !android.InList(name, a.makeModulesToInstall) {
2465 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002466 }
2467 }
2468 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2469 // Don't track further
2470 return false
2471 }
2472
2473 // If the dep is not considered to be in the same
2474 // apex, don't add it to filesInfo so that it is not
2475 // included in this APEX.
2476 // TODO(jiyong): move this to at the top of the
2477 // else-if clause for the indirect dependencies.
2478 // Currently, that's impossible because we would
2479 // like to record requiredNativeLibs even when
2480 // DepIsInSameAPex is false. We also shouldn't do
2481 // this for host.
2482 //
2483 // TODO(jiyong): explain why the same module is passed in twice.
2484 // Switching the first am to parent breaks lots of tests.
2485 if !android.IsDepInSameApex(ctx, am, am) {
2486 return false
2487 }
2488
2489 vctx.filesInfo = append(vctx.filesInfo, af)
2490 return true // track transitive dependencies
2491 } else if rm, ok := child.(*rust.Module); ok {
2492 af := apexFileForRustLibrary(ctx, rm)
2493 af.transitiveDep = true
2494 vctx.filesInfo = append(vctx.filesInfo, af)
2495 return true // track transitive dependencies
2496 }
2497 } else if cc.IsTestPerSrcDepTag(depTag) {
2498 if ch, ok := child.(*cc.Module); ok {
2499 af := apexFileForExecutable(ctx, ch)
2500 // Handle modules created as `test_per_src` variations of a single test module:
2501 // use the name of the generated test binary (`fileToCopy`) instead of the name
2502 // of the original test module (`depName`, shared by all `test_per_src`
2503 // variations of that module).
2504 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2505 // these are not considered transitive dep
2506 af.transitiveDep = false
2507 vctx.filesInfo = append(vctx.filesInfo, af)
2508 return true // track transitive dependencies
2509 }
2510 } else if cc.IsHeaderDepTag(depTag) {
2511 // nothing
2512 } else if java.IsJniDepTag(depTag) {
2513 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2514 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2515 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2516 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2517 }
2518 } else if rust.IsDylibDepTag(depTag) {
2519 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2520 af := apexFileForRustLibrary(ctx, rustm)
2521 af.transitiveDep = true
2522 vctx.filesInfo = append(vctx.filesInfo, af)
2523 return true // track transitive dependencies
2524 }
2525 } else if rust.IsRlibDepTag(depTag) {
2526 // Rlib is statically linked, but it might have shared lib
2527 // dependencies. Track them.
2528 return true
2529 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2530 // Add the contents of the bootclasspath fragment to the apex.
2531 switch child.(type) {
2532 case *java.Library, *java.SdkLibrary:
2533 javaModule := child.(javaModule)
2534 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2535 if !af.ok() {
2536 ctx.PropertyErrorf("bootclasspath_fragments",
2537 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2538 return false
2539 }
2540 vctx.filesInfo = append(vctx.filesInfo, af)
2541 return true // track transitive dependencies
2542 default:
2543 ctx.PropertyErrorf("bootclasspath_fragments",
2544 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2545 }
2546 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2547 // Add the contents of the systemserverclasspath fragment to the apex.
2548 switch child.(type) {
2549 case *java.Library, *java.SdkLibrary:
2550 af := apexFileForJavaModule(ctx, child.(javaModule))
2551 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002552 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2553 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2554 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002555 return true // track transitive dependencies
2556 default:
2557 ctx.PropertyErrorf("systemserverclasspath_fragments",
2558 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2559 }
2560 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2561 // nothing
2562 } else if depTag == android.DarwinUniversalVariantTag {
2563 // nothing
2564 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2565 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2566 }
2567 return false
2568}
2569
Jooyung Han862c0d62022-12-21 10:15:37 +09002570func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2571 // TODO(b/263308293) remove this
2572 if a.properties.IsCoverageVariant {
2573 return false
2574 }
2575 // TODO(b/263308515) remove this
2576 if a.testApex {
2577 return false
2578 }
2579 // TODO(b/263309864) remove this
2580 if a.Host() {
2581 return false
2582 }
2583 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2584 return false
2585 }
2586 return true
2587}
2588
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002589// Creates build rules for an APEX. It consists of the following major steps:
2590//
2591// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2592// 2) traverse the dependency tree to collect apexFile structs from them.
2593// 3) some fields in apexBundle struct are configured
2594// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002595func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002596 ////////////////////////////////////////////////////////////////////////////////////////////
2597 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002598 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002599 return
2600 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002601 ////////////////////////////////////////////////////////////////////////////////////////////
2602 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002603 // Collect the module directory for IDE info in java/jdeps.go.
2604 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2605
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002606 // TODO(jiyong): do this using WalkPayloadDeps
2607 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002608 vctx := visitorContext{
2609 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2610 checkDuplicate: a.shouldCheckDuplicate(ctx),
2611 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002612 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002613 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002614 if a.privateKeyFile == nil {
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +01002615 if ctx.Config().AllowMissingDependencies() {
2616 // TODO(b/266099037): a better approach for slim manifests.
2617 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2618 // Create placeholder paths for later stages that expect to see those paths,
2619 // though they won't be used.
2620 var unusedPath = android.PathForModuleOut(ctx, "nonexistentprivatekey")
2621 ctx.Build(pctx, android.BuildParams{
2622 Rule: android.ErrorRule,
2623 Output: unusedPath,
2624 Args: map[string]string{
2625 "error": "Private key not available",
2626 },
2627 })
2628 a.privateKeyFile = unusedPath
2629 } else {
2630 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
2631 return
2632 }
2633 }
2634
2635 if a.publicKeyFile == nil {
2636 if ctx.Config().AllowMissingDependencies() {
2637 // TODO(b/266099037): a better approach for slim manifests.
2638 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2639 // Create placeholder paths for later stages that expect to see those paths,
2640 // though they won't be used.
2641 var unusedPath = android.PathForModuleOut(ctx, "nonexistentpublickey")
2642 ctx.Build(pctx, android.BuildParams{
2643 Rule: android.ErrorRule,
2644 Output: unusedPath,
2645 Args: map[string]string{
2646 "error": "Public key not available",
2647 },
2648 })
2649 a.publicKeyFile = unusedPath
2650 } else {
2651 ctx.PropertyErrorf("key", "public_key for %q could not be found", String(a.overridableProperties.Key))
2652 return
2653 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002654 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002655
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002656 ////////////////////////////////////////////////////////////////////////////////////////////
2657 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002658 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002659 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002660
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002661 a.setApexTypeAndSuffix(ctx)
2662 a.setPayloadFsType(ctx)
2663 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002664 if a.properties.ApexType != zipApex {
2665 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2666 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002667
2668 ////////////////////////////////////////////////////////////////////////////////////////////
2669 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002670 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002671 if a.properties.ApexType == flattenedApex {
2672 a.buildFlattenedApex(ctx)
2673 } else {
2674 a.buildUnflattenedApex(ctx)
2675 }
Jiyong Park956305c2020-01-09 12:32:06 +09002676 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002677 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002678
2679 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2680 if a.installable() {
2681 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2682 // along with other ordinary files. (Note that this is done by apexer for
2683 // non-flattened APEXes)
2684 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2685
2686 // Place the public key as apex_pubkey. This is also done by apexer for
2687 // non-flattened APEXes case.
2688 // TODO(jiyong): Why do we need this CP rule?
2689 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2690 ctx.Build(pctx, android.BuildParams{
2691 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002692 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002693 Output: copiedPubkey,
2694 })
2695 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2696 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002697}
2698
Paul Duffincc33ec82021-04-25 23:14:55 +01002699// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2700// the bootclasspath_fragment contributes to the apex.
2701func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2702 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2703 var filesToAdd []apexFile
2704
satayev3db35472021-05-06 23:59:58 +01002705 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002706 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2707 filesToAdd = append(filesToAdd, *af)
2708 }
satayev3db35472021-05-06 23:59:58 +01002709
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002710 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
Jiakai Zhangbc698cd2023-05-08 16:28:38 +00002711 if pathInApex != "" {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002712 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2713 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2714
2715 if pathOnHost != nil {
2716 // We need to copy the profile to a temporary path with the right filename because the apexer
2717 // will take the filename as is.
2718 ctx.Build(pctx, android.BuildParams{
2719 Rule: android.Cp,
2720 Input: pathOnHost,
2721 Output: tempPath,
2722 })
2723 } else {
2724 // At this point, the boot image profile cannot be generated. It is probably because the boot
2725 // image profile source file does not exist on the branch, or it is not available for the
2726 // current build target.
2727 // However, we cannot enforce the boot image profile to be generated because some build
2728 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2729 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2730 // only if the APEX is being built.
2731 ctx.Build(pctx, android.BuildParams{
2732 Rule: android.ErrorRule,
2733 Output: tempPath,
2734 Args: map[string]string{
2735 "error": "Boot image profile cannot be generated",
2736 },
2737 })
2738 }
2739
2740 androidMkModuleName := filepath.Base(pathInApex)
2741 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2742 filesToAdd = append(filesToAdd, af)
2743 }
2744
Paul Duffincc33ec82021-04-25 23:14:55 +01002745 return filesToAdd
2746}
2747
satayevb98371c2021-06-15 16:49:50 +01002748// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2749// the module contributes to the apex; or nil if the proto config was not generated.
2750func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2751 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2752 if !info.ClasspathFragmentProtoGenerated {
2753 return nil
2754 }
2755 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2756 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2757 return &af
satayev14e49132021-05-17 21:03:07 +01002758}
2759
Paul Duffincc33ec82021-04-25 23:14:55 +01002760// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2761// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002762func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2763 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2764
2765 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2766 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002767 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2768 if err != nil {
2769 ctx.ModuleErrorf("%s", err)
2770 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002771
2772 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2773 // bootclasspath_fragment.
2774 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2775 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002776}
2777
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002778///////////////////////////////////////////////////////////////////////////////////////////////////
2779// Factory functions
2780//
2781
2782func newApexBundle() *apexBundle {
2783 module := &apexBundle{}
2784
2785 module.AddProperties(&module.properties)
2786 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002787 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002788 module.AddProperties(&module.overridableProperties)
2789
2790 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2791 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002792 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002793 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002794 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002795 return module
2796}
2797
Paul Duffineb8051d2021-10-18 17:49:39 +01002798func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002799 bundle := newApexBundle()
2800 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002801 return bundle
2802}
2803
2804// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2805// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002806func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002807 bundle := newApexBundle()
2808 bundle.testApex = true
2809 return bundle
2810}
2811
2812// apex packages other modules into an APEX file which is a packaging format for system-level
2813// components like binaries, shared libraries, etc.
2814func BundleFactory() android.Module {
2815 return newApexBundle()
2816}
2817
2818type Defaults struct {
2819 android.ModuleBase
2820 android.DefaultsModuleBase
2821}
2822
2823// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002824func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002825 module := &Defaults{}
2826
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002827 module.AddProperties(
2828 &apexBundleProperties{},
2829 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002830 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002831 &overridableProperties{},
2832 )
2833
2834 android.InitDefaultsModule(module)
2835 return module
2836}
2837
2838type OverrideApex struct {
2839 android.ModuleBase
2840 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002841 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002842}
2843
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002844func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002845 // All the overrides happen in the base module.
2846}
2847
2848// override_apex is used to create an apex module based on another apex module by overriding some of
2849// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002850func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002851 m := &OverrideApex{}
2852
2853 m.AddProperties(&overridableProperties{})
2854
2855 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2856 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002857 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002858 return m
2859}
2860
Wei Li1c66fc72022-05-09 23:59:14 -07002861func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2862 if ctx.ModuleType() != "override_apex" {
2863 return
2864 }
2865
2866 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2867 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2868 if !baseApexExists {
2869 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2870 }
2871
2872 a, baseModuleIsApex := baseModule.(*apexBundle)
2873 if !baseModuleIsApex {
2874 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2875 }
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002876 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
Wei Li1c66fc72022-05-09 23:59:14 -07002877
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002878 // We just want the name, not module reference.
2879 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2880 attrs.Base_apex_name = &baseApexName
2881
Wei Li1c66fc72022-05-09 23:59:14 -07002882 for _, p := range o.GetProperties() {
2883 overridableProperties, ok := p.(*overridableProperties)
2884 if !ok {
2885 continue
2886 }
Wei Li40f98732022-05-20 22:08:11 -07002887
2888 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2889 // After it is converted in convertWithBp2build(baseApex, ctx),
2890 // the attrs.Manifest.Value.Label is the file path relative to the directory
2891 // of base apex. So the following code converts it to a label that looks like
2892 // <package of base apex>:<path of manifest file> if base apex and override
2893 // apex are not in the same package.
2894 baseApexPackage := ctx.OtherModuleDir(a)
2895 overrideApexPackage := ctx.ModuleDir()
2896 if baseApexPackage != overrideApexPackage {
2897 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2898 }
2899
Wei Li1c66fc72022-05-09 23:59:14 -07002900 // Key
2901 if overridableProperties.Key != nil {
2902 attrs.Key = bazel.LabelAttribute{}
2903 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2904 }
2905
2906 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002907 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002908 // If overridableProperties.Certificate is nil, clear this out as
2909 // well with zeroed structs, so the override_apex does not use the
2910 // base apex's certificate.
2911 attrs.Certificate = bazel.LabelAttribute{}
2912 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002913 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002914 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002915 }
2916
2917 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002918 if overridableProperties.Prebuilts != nil {
2919 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2920 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2921 }
Wei Li1c66fc72022-05-09 23:59:14 -07002922
2923 // Compressible
2924 if overridableProperties.Compressible != nil {
2925 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2926 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002927
2928 // Package name
2929 //
2930 // e.g. com.android.adbd's package name is com.android.adbd, but
2931 // com.google.android.adbd overrides the package name to com.google.android.adbd
2932 //
2933 // TODO: this can be overridden from the product configuration, see
2934 // getOverrideManifestPackageName and
2935 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2936 //
2937 // Instead of generating the BUILD files differently based on the product config
2938 // at the point of conversion, this should be handled by the BUILD file loading
2939 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2940 if overridableProperties.Package_name != "" {
2941 attrs.Package_name = &overridableProperties.Package_name
2942 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002943
2944 // Logging parent
2945 if overridableProperties.Logging_parent != "" {
2946 attrs.Logging_parent = &overridableProperties.Logging_parent
2947 }
Wei Li1c66fc72022-05-09 23:59:14 -07002948 }
2949
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002950 commonAttrs.Name = o.Name()
2951
2952 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07002953}
2954
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002955///////////////////////////////////////////////////////////////////////////////////////////////////
2956// Vality check routines
2957//
2958// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2959// certain conditions are not met.
2960//
2961// TODO(jiyong): move these checks to a separate go file.
2962
satayevad991492021-12-03 18:58:32 +00002963var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2964
Spandan Dasa5f39a12022-08-05 02:35:52 +00002965// 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 +09002966// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002967func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002968 if a.testApex || a.vndkApex {
2969 return
2970 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002971 // apexBundle::minSdkVersion reports its own errors.
2972 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002973 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002974}
2975
Albert Martineefabcf2022-03-21 20:11:16 +00002976// Returns apex's min_sdk_version string value, honoring overrides
2977func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2978 // Only override the minSdkVersion value on Apexes which already specify
2979 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2980 // min_sdk_version value is lower than the one to override with.
Liz Kammerbd58e742023-05-11 15:58:13 +00002981 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.properties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002982 if minApiLevel.IsNone() {
2983 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002984 }
2985
Colin Cross56534df2022-10-04 09:58:58 -07002986 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2987 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2988 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2989 minApiLevel = overrideApiLevel
2990 }
2991
2992 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002993}
2994
2995// Returns apex's min_sdk_version SdkSpec, honoring overrides
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002996func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2997 return a.minSdkVersion(ctx)
satayevad991492021-12-03 18:58:32 +00002998}
2999
Albert Martineefabcf2022-03-21 20:11:16 +00003000// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00003001func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00003002 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
3003}
3004
3005// Construct ApiLevel object from min_sdk_version string value
3006func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
3007 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09003008 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003009 }
Albert Martineefabcf2022-03-21 20:11:16 +00003010 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003011 if err != nil {
3012 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
3013 return android.NoneApiLevel
3014 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003015 return apiLevel
3016}
3017
3018// Ensures that a lib providing stub isn't statically linked
3019func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
3020 // Practically, we only care about regular APEXes on the device.
3021 if ctx.Host() || a.testApex || a.vndkApex {
3022 return
3023 }
3024
3025 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
3026
3027 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3028 if ccm, ok := to.(*cc.Module); ok {
3029 apexName := ctx.ModuleName()
3030 fromName := ctx.OtherModuleName(from)
3031 toName := ctx.OtherModuleName(to)
3032
3033 // If `to` is not actually in the same APEX as `from` then it does not need
3034 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003035 //
3036 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003037 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3038 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3039 return false
3040 }
3041
3042 // The dynamic linker and crash_dump tool in the runtime APEX is the only
3043 // exception to this rule. It can't make the static dependencies dynamic
3044 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09003045 // Same rule should be applied to linkerconfig, because it should be executed
3046 // only with static linked libraries before linker is available with ld.config.txt
3047 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003048 return false
3049 }
3050
3051 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
3052 if isStubLibraryFromOtherApex && !externalDep {
3053 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
3054 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
3055 }
3056
3057 }
3058 return true
3059 })
3060}
3061
satayevb98371c2021-06-15 16:49:50 +01003062// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003063func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
3064 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00003065 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003066 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
3067 }
Jiyong Park1bc84122021-06-22 20:23:05 +09003068 if a.UsePlatformApis() {
3069 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
3070 }
Jooyung Handfc864c2023-03-20 18:19:07 +09003071 if proptools.Bool(a.properties.Use_vndk_as_stable) {
3072 ctx.PropertyErrorf("use_vndk_as_stable", "updatable APEXes can't use external VNDK libs")
Daniel Norman69109112021-12-02 12:52:42 -08003073 }
Jiyong Parkf4020582021-11-29 12:37:10 +09003074 if a.FutureUpdatable() {
3075 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
3076 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003077 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01003078 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003079 }
3080}
3081
satayevb98371c2021-06-15 16:49:50 +01003082// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
3083func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
3084 ctx.VisitDirectDeps(func(module android.Module) {
3085 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
3086 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
3087 if !info.ClasspathFragmentProtoGenerated {
3088 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
3089 }
3090 }
3091 })
3092}
3093
3094// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003095func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003096 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3097 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003098 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3099 tag := ctx.OtherModuleDependencyTag(module)
3100 switch tag {
3101 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003102 if m, ok := module.(interface {
3103 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3104 }); ok {
3105 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003106 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3107 }
3108 }
3109 }
3110 })
3111}
3112
satayevb98371c2021-06-15 16:49:50 +01003113// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003114func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3115 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3116 if ctx.Host() || a.testApex || a.vndkApex {
3117 return
3118 }
3119
3120 // Because APEXes targeting other than system/system_ext partitions can't set
3121 // apex_available, we skip checks for these APEXes
3122 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3123 return
3124 }
3125
3126 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3127 // Requiring them and their transitive depencies with apex_available is not right
3128 // because they just add noise.
3129 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3130 return
3131 }
3132
3133 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3134 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3135 if externalDep {
3136 return false
3137 }
3138
3139 apexName := ctx.ModuleName()
Sam Delmericoca816532023-06-02 14:09:50 -04003140 for _, props := range ctx.Module().GetProperties() {
3141 if apexProps, ok := props.(*apexBundleProperties); ok {
3142 if apexProps.Apex_available_name != nil {
3143 apexName = *apexProps.Apex_available_name
3144 }
3145 }
3146 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003147 fromName := ctx.OtherModuleName(from)
3148 toName := ctx.OtherModuleName(to)
3149
3150 // If `to` is not actually in the same APEX as `from` then it does not need
3151 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003152 //
3153 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003154 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3155 // As soon as the dependency graph crosses the APEX boundary, don't go
3156 // further.
3157 return false
3158 }
3159
3160 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3161 return true
3162 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003163 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3164 "\n\nDependency path:%s\n\n"+
3165 "Consider adding %q to 'apex_available' property of %q",
3166 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003167 // Visit this module's dependencies to check and report any issues with their availability.
3168 return true
3169 })
3170}
3171
Jiyong Park192600a2021-08-03 07:52:17 +00003172// checkStaticExecutable ensures that executables in an APEX are not static.
3173func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003174 // No need to run this for host APEXes
3175 if ctx.Host() {
3176 return
3177 }
3178
Jiyong Park192600a2021-08-03 07:52:17 +00003179 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3180 if ctx.OtherModuleDependencyTag(module) != executableTag {
3181 return
3182 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003183
3184 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003185 apex := a.ApexVariationName()
3186 exec := ctx.OtherModuleName(module)
3187 if isStaticExecutableAllowed(apex, exec) {
3188 return
3189 }
3190 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3191 }
3192 })
3193}
3194
3195// A small list of exceptions where static executables are allowed in APEXes.
3196func isStaticExecutableAllowed(apex string, exec string) bool {
3197 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003198 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003199 "linker",
3200 "linkerconfig",
3201 },
3202 }
3203 execNames, ok := m[apex]
3204 return ok && android.InList(exec, execNames)
3205}
3206
braleeb0c1f0c2021-06-07 22:49:13 +08003207// Collect information for opening IDE project files in java/jdeps.go.
3208func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00003209 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
3210 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
3211 dpInfo.Deps = append(dpInfo.Deps, a.properties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003212 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3213}
3214
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003215var (
3216 apexAvailBaseline = makeApexAvailableBaseline()
3217 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3218)
3219
Colin Cross440e0d02020-06-11 11:32:11 -07003220func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003221 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003222 moduleName = normalizeModuleName(moduleName)
3223
Colin Cross440e0d02020-06-11 11:32:11 -07003224 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003225 return true
3226 }
3227
3228 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003229 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003230 return true
3231 }
3232
3233 return false
3234}
3235
3236func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003237 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3238 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003239 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003240 if strings.HasPrefix(moduleName, "libclang_rt.") {
3241 // This module has many arch variants that depend on the product being built.
3242 // We don't want to list them all
3243 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003244 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003245 if strings.HasPrefix(moduleName, "androidx.") {
3246 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3247 moduleName = "androidx"
3248 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003249 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003250}
3251
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003252// Transform the map of apex -> modules to module -> apexes.
3253func invertApexBaseline(m map[string][]string) map[string][]string {
3254 r := make(map[string][]string)
3255 for apex, modules := range m {
3256 for _, module := range modules {
3257 r[module] = append(r[module], apex)
3258 }
3259 }
3260 return r
3261}
3262
3263// Retrieve the baseline of apexes to which the supplied module belongs.
3264func BaselineApexAvailable(moduleName string) []string {
3265 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3266}
3267
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003268// This is a map from apex to modules, which overrides the apex_available setting for that
3269// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003270// TODO(b/147364041): remove this
3271func makeApexAvailableBaseline() map[string][]string {
3272 // The "Module separator"s below are employed to minimize merge conflicts.
3273 m := make(map[string][]string)
3274 //
3275 // Module separator
3276 //
3277 m["com.android.appsearch"] = []string{
3278 "icing-java-proto-lite",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003279 }
3280 //
3281 // Module separator
3282 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003283 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003284 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003285 }
3286 //
3287 // Module separator
3288 //
Spandan Das072f7bc2023-05-05 21:06:23 +00003289 m["com.android.cellbroadcast"] = []string{}
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003290 //
3291 // Module separator
3292 //
3293 m["com.android.extservices"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003294 "ExtServices-core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003295 "libtextclassifier-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003296 "textclassifier-statsd",
3297 "TextClassifierNotificationLibNoManifest",
3298 "TextClassifierServiceLibNoManifest",
3299 }
3300 //
3301 // Module separator
3302 //
3303 m["com.android.neuralnetworks"] = []string{
3304 "android.hardware.neuralnetworks@1.0",
3305 "android.hardware.neuralnetworks@1.1",
3306 "android.hardware.neuralnetworks@1.2",
3307 "android.hardware.neuralnetworks@1.3",
3308 "android.hidl.allocator@1.0",
3309 "android.hidl.memory.token@1.0",
3310 "android.hidl.memory@1.0",
3311 "android.hidl.safe_union@1.0",
3312 "libarect",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003313 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003314 }
3315 //
3316 // Module separator
3317 //
3318 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003319 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003320 }
3321 //
3322 // Module separator
3323 //
3324 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003325 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003326 }
3327 //
3328 // Module separator
3329 //
3330 m["com.android.mediaprovider"] = []string{
3331 "MediaProvider",
3332 "MediaProviderGoogle",
3333 "fmtlib_ndk",
3334 "libbase_ndk",
3335 "libfuse",
3336 "libfuse_jni",
3337 }
3338 //
3339 // Module separator
3340 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003341 m["com.android.runtime"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003342 "libc_aeabi",
3343 "libc_bionic",
3344 "libc_bionic_ndk",
3345 "libc_bootstrap",
3346 "libc_common",
3347 "libc_common_shared",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003348 "libc_dns",
3349 "libc_dynamic_dispatch",
3350 "libc_fortify",
3351 "libc_freebsd",
3352 "libc_freebsd_large_stack",
3353 "libc_gdtoa",
3354 "libc_init_dynamic",
3355 "libc_init_static",
3356 "libc_jemalloc_wrapper",
3357 "libc_netbsd",
3358 "libc_nomalloc",
3359 "libc_nopthread",
3360 "libc_openbsd",
3361 "libc_openbsd_large_stack",
3362 "libc_openbsd_ndk",
3363 "libc_pthread",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003364 "libc_syscalls",
3365 "libc_tzcode",
3366 "libc_unwind_static",
3367 "libdebuggerd",
3368 "libdebuggerd_common_headers",
3369 "libdebuggerd_handler_core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003370 "libdl_static",
3371 "libjemalloc5",
3372 "liblinker_main",
3373 "liblinker_malloc",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003374 "liblzma",
3375 "libprocinfo",
3376 "libpropertyinfoparser",
3377 "libscudo",
3378 "libstdc++",
3379 "libsystemproperties",
3380 "libtombstoned_client_static",
3381 "libunwindstack",
3382 "libz",
3383 "libziparchive",
3384 }
3385 //
3386 // Module separator
3387 //
3388 m["com.android.tethering"] = []string{
3389 "android.hardware.tetheroffload.config-V1.0-java",
3390 "android.hardware.tetheroffload.control-V1.0-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003391 "net-utils-framework-common",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003392 }
3393 //
3394 // Module separator
3395 //
3396 m["com.android.wifi"] = []string{
3397 "PlatformProperties",
3398 "android.hardware.wifi-V1.0-java",
3399 "android.hardware.wifi-V1.0-java-constants",
3400 "android.hardware.wifi-V1.1-java",
3401 "android.hardware.wifi-V1.2-java",
3402 "android.hardware.wifi-V1.3-java",
3403 "android.hardware.wifi-V1.4-java",
3404 "android.hardware.wifi.hostapd-V1.0-java",
3405 "android.hardware.wifi.hostapd-V1.1-java",
3406 "android.hardware.wifi.hostapd-V1.2-java",
3407 "android.hardware.wifi.supplicant-V1.0-java",
3408 "android.hardware.wifi.supplicant-V1.1-java",
3409 "android.hardware.wifi.supplicant-V1.2-java",
3410 "android.hardware.wifi.supplicant-V1.3-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003411 "bouncycastle-unbundled",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003412 "framework-wifi-util-lib",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003413 "ksoap2",
3414 "libnanohttpd",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003415 "wifi-lite-protos",
3416 "wifi-nano-protos",
3417 "wifi-service-pre-jarjar",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003418 }
3419 //
3420 // Module separator
3421 //
3422 m[android.AvailableToAnyApex] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003423 "libprofile-clang-extras",
3424 "libprofile-clang-extras_ndk",
3425 "libprofile-extras",
3426 "libprofile-extras_ndk",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003427 }
3428 return m
3429}
3430
3431func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003432 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3433 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003434}
3435
Spandan Dasf14e2542021-11-12 00:01:37 +00003436func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3437 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3438 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003439 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003440 With("name", jar).
3441 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3442 Because(jar +
3443 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003444 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003445 " 1. If the offending code is from a statically linked library, consider " +
3446 "removing that dependency and using an alternative already in the " +
3447 "bootclasspath, or perhaps a shared library." +
3448 " 2. Move the offending code into an allowed package.\n" +
3449 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3450 "health implications of bundling that code, particularly if the offending jar " +
3451 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003452
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003453 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003454 }
3455 return rules
3456}
3457
Anton Hanssone1b18362021-12-23 15:05:38 +00003458// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003459// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003460func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003461 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003462 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003463 "android.net.ssl",
3464 "com.android.org.conscrypt",
3465 },
Wei Li40f98732022-05-20 22:08:11 -07003466 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003467 "android.media",
3468 },
3469 }
3470}
3471
Anton Hanssone1b18362021-12-23 15:05:38 +00003472// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003473// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003474func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003475 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003476 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003477 "android.provider",
3478 },
Wei Li40f98732022-05-20 22:08:11 -07003479 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003480 "android.permission",
3481 "android.app.role",
3482 "com.android.permission",
3483 "com.android.role",
3484 },
Wei Li40f98732022-05-20 22:08:11 -07003485 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003486 "android.os.ext",
3487 },
Wei Li40f98732022-05-20 22:08:11 -07003488 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003489 "android.app",
3490 "android.os",
3491 "android.util",
3492 "com.android.internal.statsd",
3493 "com.android.server.stats",
3494 },
Wei Li40f98732022-05-20 22:08:11 -07003495 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003496 "com.android.server.wifi",
3497 "com.android.wifi.x",
3498 "android.hardware.wifi",
3499 "android.net.wifi",
3500 },
Wei Li40f98732022-05-20 22:08:11 -07003501 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003502 "android.net",
3503 },
3504 }
3505}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003506
3507// For Bazel / bp2build
3508
3509type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003510 Manifest bazel.LabelAttribute
3511 Android_manifest bazel.LabelAttribute
3512 File_contexts bazel.LabelAttribute
Jingwen Chena8623da2023-03-28 13:05:02 +00003513 Canned_fs_config bazel.LabelAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003514 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003515 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3516 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003517 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003518 Updatable bazel.BoolAttribute
3519 Installable bazel.BoolAttribute
3520 Binaries bazel.LabelListAttribute
3521 Prebuilts bazel.LabelListAttribute
3522 Native_shared_libs_32 bazel.LabelListAttribute
3523 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003524 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003525 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003526 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003527 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003528 Base_apex_name *string
Sam Delmericoe91698a2023-06-06 11:30:31 -04003529 Apex_available_name *string
Sam Delmerico743b4c52023-06-06 12:06:53 -04003530 Variant_version *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003531}
3532
3533type convertedNativeSharedLibs struct {
3534 Native_shared_libs_32 bazel.LabelListAttribute
3535 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003536}
3537
Liz Kammerb83b7b02022-12-21 14:53:41 -05003538const (
3539 minSdkVersionPropName = "Min_sdk_version"
3540)
3541
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003542// ConvertWithBp2build performs bp2build conversion of an apex
3543func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003544 // We only convert apex and apex_test modules at this time
3545 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003546 return
3547 }
3548
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003549 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
3550 commonAttrs.Name = a.Name()
Yu Liu4c212ce2022-10-14 12:20:20 -07003551 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003552}
3553
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003554func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties, android.CommonAttributes) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003555 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003556 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003557
3558 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003559 if a.properties.AndroidManifest != nil {
3560 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003561 }
3562
3563 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003564 if a.properties.File_contexts == nil {
3565 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3566 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3567 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3568 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003569 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003570 } else {
3571 // File_contexts is a file
3572 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003573 }
3574
Jingwen Chena8623da2023-03-28 13:05:02 +00003575 var cannedFsConfigAttribute bazel.LabelAttribute
3576 if a.properties.Canned_fs_config != nil {
3577 cannedFsConfigAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Canned_fs_config))
3578 }
3579
Cole Faust912bc882023-03-08 12:29:50 -08003580 productVariableProps := android.ProductVariableProperties(ctx, a)
Albert Martineefabcf2022-03-21 20:11:16 +00003581 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3582 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003583 var minSdkVersion bazel.StringAttribute
Liz Kammerbd58e742023-05-11 15:58:13 +00003584 if a.properties.Min_sdk_version != nil {
3585 minSdkVersion.SetValue(*a.properties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003586 }
3587 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3588 for c, p := range props {
3589 if val, ok := p.(*string); ok {
3590 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3591 }
3592 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003593 }
3594
3595 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003596 if a.overridableProperties.Key != nil {
3597 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003598 }
3599
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003600 // Certificate
3601 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003602
Yu Liu4ae55d12022-01-05 17:17:23 -08003603 nativeSharedLibs := &convertedNativeSharedLibs{
3604 Native_shared_libs_32: bazel.LabelListAttribute{},
3605 Native_shared_libs_64: bazel.LabelListAttribute{},
3606 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003607
3608 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3609 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3610 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3611 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3612 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003613 if a.CompileMultilib() != nil {
3614 compileMultilib = *a.CompileMultilib()
3615 }
3616
3617 // properties.Native_shared_libs is treated as "both"
3618 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3619 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3620 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3621 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3622 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003623
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003624 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003625 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3626 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3627
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003628 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003629 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003630
Yu Liu4c212ce2022-10-14 12:20:20 -07003631 var testsAttrs bazel.LabelListAttribute
3632 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3633 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3634 testsAttrs = bazel.MakeLabelListAttribute(tests)
3635 }
3636
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003637 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003638 if a.properties.Updatable != nil {
3639 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003640 }
3641
3642 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003643 if a.properties.Installable != nil {
3644 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003645 }
3646
Wei Lif034cb42022-01-19 15:54:31 -08003647 var compressibleAttribute bazel.BoolAttribute
3648 if a.overridableProperties.Compressible != nil {
3649 compressibleAttribute.Value = a.overridableProperties.Compressible
3650 }
3651
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003652 var packageName *string
3653 if a.overridableProperties.Package_name != "" {
3654 packageName = &a.overridableProperties.Package_name
3655 }
3656
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003657 var loggingParent *string
3658 if a.overridableProperties.Logging_parent != "" {
3659 loggingParent = &a.overridableProperties.Logging_parent
3660 }
3661
Wei Li1c66fc72022-05-09 23:59:14 -07003662 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003663 Manifest: manifestLabelAttribute,
3664 Android_manifest: androidManifestLabelAttribute,
3665 File_contexts: fileContextsLabelAttribute,
Jingwen Chena8623da2023-03-28 13:05:02 +00003666 Canned_fs_config: cannedFsConfigAttribute,
Yu Liu4ae55d12022-01-05 17:17:23 -08003667 Min_sdk_version: minSdkVersion,
3668 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003669 Certificate: certificate,
3670 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003671 Updatable: updatableAttribute,
3672 Installable: installableAttribute,
3673 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3674 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3675 Binaries: binariesLabelListAttribute,
3676 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003677 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003678 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003679 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003680 Tests: testsAttrs,
Sam Delmericoe91698a2023-06-06 11:30:31 -04003681 Apex_available_name: a.properties.Apex_available_name,
Sam Delmerico743b4c52023-06-06 12:06:53 -04003682 Variant_version: a.properties.Variant_version,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003683 }
3684
3685 props := bazel.BazelTargetModuleProperties{
3686 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003687 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003688 }
3689
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003690 commonAttrs := android.CommonAttributes{}
3691 if a.testApex {
3692 commonAttrs.Testonly = proptools.BoolPtr(true)
Spandan Dasa43ae132023-05-08 18:33:16 +00003693 // Set the api_domain of the test apex
3694 attrs.Base_apex_name = proptools.StringPtr(cc.GetApiDomain(a.Name()))
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003695 }
3696
3697 return attrs, props, commonAttrs
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003698}
Yu Liu4ae55d12022-01-05 17:17:23 -08003699
3700// The following conversions are based on this table where the rows are the compile_multilib
3701// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3702// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3703// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3704// should not be compiled.
3705// multib/compile_multilib, 32, 64, both, first
3706// 32, 32/32, none/none, 32/32, none/32
3707// 64, none/none, 64/none, 64/none, 64/none
3708// both, 32/32, 64/none, 32&64/32, 64/32
3709// first, 32/32, 64/none, 64/32, 64/32
3710
3711func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3712 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3713 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3714 switch compileMultilb {
3715 case "both", "32":
3716 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3717 case "first":
3718 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3719 case "64":
3720 // Incompatible, ignore
3721 default:
3722 invalidCompileMultilib(ctx, compileMultilb)
3723 }
3724}
3725
3726func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3727 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3728 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3729 switch compileMultilb {
3730 case "both", "64", "first":
3731 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3732 case "32":
3733 // Incompatible, ignore
3734 default:
3735 invalidCompileMultilib(ctx, compileMultilb)
3736 }
3737}
3738
3739func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3740 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3741 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3742 switch compileMultilb {
3743 case "both":
3744 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3745 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3746 case "first":
3747 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3748 case "32":
3749 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3750 case "64":
3751 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3752 default:
3753 invalidCompileMultilib(ctx, compileMultilb)
3754 }
3755}
3756
3757func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3758 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3759 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3760 switch compileMultilb {
3761 case "both", "first":
3762 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3763 case "32":
3764 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3765 case "64":
3766 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3767 default:
3768 invalidCompileMultilib(ctx, compileMultilb)
3769 }
3770}
3771
3772func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3773 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3774 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3775}
3776
3777func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3778 list := bazel.LabelListAttribute{}
3779 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3780 nativeSharedLibs.Native_shared_libs_32.Append(list)
3781}
3782
3783func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3784 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3785 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3786}
3787
3788func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3789 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3790 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3791}
3792
3793func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3794 labelListAttr *bazel.LabelListAttribute) {
3795 list := bazel.LabelListAttribute{}
3796 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3797 labelListAttr.Append(list)
3798}
3799
3800func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3801 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3802}
Spandan Dasf57a9662023-04-12 19:05:49 +00003803
3804func (a *apexBundle) IsTestApex() bool {
3805 return a.testApex
3806}