blob: e8747d624a6f6eeefe9b6a763d0aa8efbec2960d [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.
225 // If not specified, this defaults to Soong module name.
226 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
2154
2155 if ctx.Config().InstallExtraFlattenedApexes() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002156 a.makeModulesToInstall = append(a.makeModulesToInstall, a.Name()+flattenedSuffix)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002157 }
2158 }
2159 case zipApex:
2160 if proptools.String(a.properties.Payload_type) == "zip" {
2161 a.suffix = ""
2162 a.primaryApexType = true
2163 } else {
2164 a.suffix = zipApexSuffix
2165 }
2166 case flattenedApex:
2167 if buildFlattenedAsDefault {
2168 a.suffix = ""
2169 a.primaryApexType = true
2170 } else {
2171 a.suffix = flattenedSuffix
2172 }
2173 }
2174}
2175
2176func (a apexBundle) isCompressable() bool {
2177 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2178}
2179
2180func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2181 a.checkApexAvailability(ctx)
2182 a.checkUpdatable(ctx)
2183 a.CheckMinSdkVersion(ctx)
2184 a.checkStaticLinkingToStubLibraries(ctx)
2185 a.checkStaticExecutables(ctx)
2186 if len(a.properties.Tests) > 0 && !a.testApex {
2187 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2188 return false
2189 }
2190 return true
2191}
2192
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002193type visitorContext struct {
2194 // all the files that will be included in this APEX
2195 filesInfo []apexFile
2196
2197 // native lib dependencies
2198 provideNativeLibs []string
2199 requireNativeLibs []string
2200
2201 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002202
2203 // if true, raise error on duplicate apexFile
2204 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002205}
2206
Jooyung Han862c0d62022-12-21 10:15:37 +09002207func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002208 encountered := make(map[string]apexFile)
2209 for _, f := range vctx.filesInfo {
2210 dest := filepath.Join(f.installDir, f.builtFile.Base())
2211 if e, ok := encountered[dest]; !ok {
2212 encountered[dest] = f
2213 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002214 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2215 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2216 dest, e.builtFile, f.builtFile)
2217 return
2218 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002219 // If a module is directly included and also transitively depended on
2220 // consider it as directly included.
2221 e.transitiveDep = e.transitiveDep && f.transitiveDep
2222 encountered[dest] = e
2223 }
2224 }
2225 vctx.filesInfo = vctx.filesInfo[:0]
2226 for _, v := range encountered {
2227 vctx.filesInfo = append(vctx.filesInfo, v)
2228 }
2229 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2230 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2231 // changes.
2232 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2233 })
2234}
2235
2236func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2237 depTag := ctx.OtherModuleDependencyTag(child)
2238 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2239 return false
2240 }
2241 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2242 return false
2243 }
2244 depName := ctx.OtherModuleName(child)
2245 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2246 switch depTag {
2247 case sharedLibTag, jniLibTag:
2248 isJniLib := depTag == jniLibTag
2249 switch ch := child.(type) {
2250 case *cc.Module:
2251 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2252 fi.isJniLib = isJniLib
2253 vctx.filesInfo = append(vctx.filesInfo, fi)
2254 // Collect the list of stub-providing libs except:
2255 // - VNDK libs are only for vendors
2256 // - bootstrap bionic libs are treated as provided by system
2257 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2258 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2259 }
2260 return true // track transitive dependencies
2261 case *rust.Module:
2262 fi := apexFileForRustLibrary(ctx, ch)
2263 fi.isJniLib = isJniLib
2264 vctx.filesInfo = append(vctx.filesInfo, fi)
2265 return true // track transitive dependencies
2266 default:
2267 propertyName := "native_shared_libs"
2268 if isJniLib {
2269 propertyName = "jni_libs"
2270 }
2271 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2272 }
2273 case executableTag:
2274 switch ch := child.(type) {
2275 case *cc.Module:
2276 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2277 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002278 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002279 if ch.HostToolPath().Valid() {
2280 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2281 }
2282 case bootstrap.GoBinaryTool:
2283 if a.Host() {
2284 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2285 }
2286 case *rust.Module:
2287 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2288 return true // track transitive dependencies
2289 default:
2290 ctx.PropertyErrorf("binaries",
2291 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2292 }
2293 case shBinaryTag:
2294 if csh, ok := child.(*sh.ShBinary); ok {
2295 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2296 } else {
2297 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2298 }
2299 case bcpfTag:
Jiakai Zhangb47cacc2023-05-10 16:40:18 +01002300 _, ok := child.(*java.BootclasspathFragmentModule)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002301 if !ok {
2302 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2303 return false
2304 }
2305
2306 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002307 return true
2308 case sscpfTag:
2309 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2310 ctx.PropertyErrorf("systemserverclasspath_fragments",
2311 "%q is not a systemserverclasspath_fragment module", depName)
2312 return false
2313 }
2314 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2315 vctx.filesInfo = append(vctx.filesInfo, *af)
2316 }
2317 return true
2318 case javaLibTag:
2319 switch child.(type) {
2320 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2321 af := apexFileForJavaModule(ctx, child.(javaModule))
2322 if !af.ok() {
2323 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2324 return false
2325 }
2326 vctx.filesInfo = append(vctx.filesInfo, af)
2327 return true // track transitive dependencies
2328 default:
2329 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2330 }
2331 case androidAppTag:
2332 switch ap := child.(type) {
2333 case *java.AndroidApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002334 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002335 return true // track transitive dependencies
2336 case *java.AndroidAppImport:
Andrei Onea580636b2022-08-17 16:53:46 +00002337 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002338 case *java.AndroidTestHelperApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002339 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002340 case *java.AndroidAppSet:
2341 appDir := "app"
2342 if ap.Privileged() {
2343 appDir = "priv-app"
2344 }
2345 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2346 // suffixed so that PackageManager correctly invalidates the
2347 // existing installed apk in favour of the new APK-in-APEX.
2348 // See bugs for more information.
2349 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2350 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2351 af.certificate = java.PresignedCertificate
2352 vctx.filesInfo = append(vctx.filesInfo, af)
2353 default:
2354 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2355 }
2356 case rroTag:
2357 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2358 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2359 } else {
2360 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2361 }
2362 case bpfTag:
2363 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2364 filesToCopy, _ := bpfProgram.OutputFiles("")
2365 apex_sub_dir := bpfProgram.SubDir()
2366 for _, bpfFile := range filesToCopy {
2367 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2368 }
2369 } else {
2370 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2371 }
2372 case fsTag:
2373 if fs, ok := child.(filesystem.Filesystem); ok {
2374 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2375 } else {
2376 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2377 }
2378 case prebuiltTag:
2379 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2380 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2381 } else {
2382 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2383 }
2384 case compatConfigTag:
2385 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2386 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2387 } else {
2388 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2389 }
2390 case testTag:
2391 if ccTest, ok := child.(*cc.Module); ok {
2392 if ccTest.IsTestPerSrcAllTestsVariation() {
2393 // Multiple-output test module (where `test_per_src: true`).
2394 //
2395 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2396 // We do not add this variation to `filesInfo`, as it has no output;
2397 // however, we do add the other variations of this module as indirect
2398 // dependencies (see below).
2399 } else {
2400 // Single-output test module (where `test_per_src: false`).
2401 af := apexFileForExecutable(ctx, ccTest)
2402 af.class = nativeTest
2403 vctx.filesInfo = append(vctx.filesInfo, af)
2404 }
2405 return true // track transitive dependencies
2406 } else {
2407 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2408 }
2409 case keyTag:
2410 if key, ok := child.(*apexKey); ok {
2411 a.privateKeyFile = key.privateKeyFile
2412 a.publicKeyFile = key.publicKeyFile
2413 } else {
2414 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2415 }
2416 case certificateTag:
2417 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2418 a.containerCertificateFile = dep.Certificate.Pem
2419 a.containerPrivateKeyFile = dep.Certificate.Key
2420 } else {
2421 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2422 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002423 }
2424 return false
2425 }
2426
2427 if a.vndkApex {
2428 return false
2429 }
2430
2431 // indirect dependencies
2432 am, ok := child.(android.ApexModule)
2433 if !ok {
2434 return false
2435 }
2436 // We cannot use a switch statement on `depTag` here as the checked
2437 // tags used below are private (e.g. `cc.sharedDepTag`).
2438 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2439 if ch, ok := child.(*cc.Module); ok {
2440 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2441 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2442 return false
2443 }
2444 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2445 af.transitiveDep = true
2446
2447 // Always track transitive dependencies for host.
2448 if a.Host() {
2449 vctx.filesInfo = append(vctx.filesInfo, af)
2450 return true
2451 }
2452
2453 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2454 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2455 // If the dependency is a stubs lib, don't include it in this APEX,
2456 // but make sure that the lib is installed on the device.
2457 // In case no APEX is having the lib, the lib is installed to the system
2458 // partition.
2459 //
2460 // Always include if we are a host-apex however since those won't have any
2461 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002462 //
2463 // Skip the dependency in unbundled builds where the device image is not
2464 // being built.
2465 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002466 // we need a module name for Make
2467 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002468 if !android.InList(name, a.makeModulesToInstall) {
2469 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002470 }
2471 }
2472 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2473 // Don't track further
2474 return false
2475 }
2476
2477 // If the dep is not considered to be in the same
2478 // apex, don't add it to filesInfo so that it is not
2479 // included in this APEX.
2480 // TODO(jiyong): move this to at the top of the
2481 // else-if clause for the indirect dependencies.
2482 // Currently, that's impossible because we would
2483 // like to record requiredNativeLibs even when
2484 // DepIsInSameAPex is false. We also shouldn't do
2485 // this for host.
2486 //
2487 // TODO(jiyong): explain why the same module is passed in twice.
2488 // Switching the first am to parent breaks lots of tests.
2489 if !android.IsDepInSameApex(ctx, am, am) {
2490 return false
2491 }
2492
2493 vctx.filesInfo = append(vctx.filesInfo, af)
2494 return true // track transitive dependencies
2495 } else if rm, ok := child.(*rust.Module); ok {
2496 af := apexFileForRustLibrary(ctx, rm)
2497 af.transitiveDep = true
2498 vctx.filesInfo = append(vctx.filesInfo, af)
2499 return true // track transitive dependencies
2500 }
2501 } else if cc.IsTestPerSrcDepTag(depTag) {
2502 if ch, ok := child.(*cc.Module); ok {
2503 af := apexFileForExecutable(ctx, ch)
2504 // Handle modules created as `test_per_src` variations of a single test module:
2505 // use the name of the generated test binary (`fileToCopy`) instead of the name
2506 // of the original test module (`depName`, shared by all `test_per_src`
2507 // variations of that module).
2508 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2509 // these are not considered transitive dep
2510 af.transitiveDep = false
2511 vctx.filesInfo = append(vctx.filesInfo, af)
2512 return true // track transitive dependencies
2513 }
2514 } else if cc.IsHeaderDepTag(depTag) {
2515 // nothing
2516 } else if java.IsJniDepTag(depTag) {
2517 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2518 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2519 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2520 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2521 }
2522 } else if rust.IsDylibDepTag(depTag) {
2523 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2524 af := apexFileForRustLibrary(ctx, rustm)
2525 af.transitiveDep = true
2526 vctx.filesInfo = append(vctx.filesInfo, af)
2527 return true // track transitive dependencies
2528 }
2529 } else if rust.IsRlibDepTag(depTag) {
2530 // Rlib is statically linked, but it might have shared lib
2531 // dependencies. Track them.
2532 return true
2533 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2534 // Add the contents of the bootclasspath fragment to the apex.
2535 switch child.(type) {
2536 case *java.Library, *java.SdkLibrary:
2537 javaModule := child.(javaModule)
2538 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2539 if !af.ok() {
2540 ctx.PropertyErrorf("bootclasspath_fragments",
2541 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2542 return false
2543 }
2544 vctx.filesInfo = append(vctx.filesInfo, af)
2545 return true // track transitive dependencies
2546 default:
2547 ctx.PropertyErrorf("bootclasspath_fragments",
2548 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2549 }
2550 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2551 // Add the contents of the systemserverclasspath fragment to the apex.
2552 switch child.(type) {
2553 case *java.Library, *java.SdkLibrary:
2554 af := apexFileForJavaModule(ctx, child.(javaModule))
2555 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002556 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2557 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2558 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002559 return true // track transitive dependencies
2560 default:
2561 ctx.PropertyErrorf("systemserverclasspath_fragments",
2562 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2563 }
2564 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2565 // nothing
2566 } else if depTag == android.DarwinUniversalVariantTag {
2567 // nothing
2568 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2569 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2570 }
2571 return false
2572}
2573
Jooyung Han862c0d62022-12-21 10:15:37 +09002574func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2575 // TODO(b/263308293) remove this
2576 if a.properties.IsCoverageVariant {
2577 return false
2578 }
2579 // TODO(b/263308515) remove this
2580 if a.testApex {
2581 return false
2582 }
2583 // TODO(b/263309864) remove this
2584 if a.Host() {
2585 return false
2586 }
2587 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2588 return false
2589 }
2590 return true
2591}
2592
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002593// Creates build rules for an APEX. It consists of the following major steps:
2594//
2595// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2596// 2) traverse the dependency tree to collect apexFile structs from them.
2597// 3) some fields in apexBundle struct are configured
2598// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002599func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002600 ////////////////////////////////////////////////////////////////////////////////////////////
2601 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002602 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002603 return
2604 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002605 ////////////////////////////////////////////////////////////////////////////////////////////
2606 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002607 // Collect the module directory for IDE info in java/jdeps.go.
2608 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2609
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002610 // TODO(jiyong): do this using WalkPayloadDeps
2611 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002612 vctx := visitorContext{
2613 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2614 checkDuplicate: a.shouldCheckDuplicate(ctx),
2615 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002616 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002617 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002618 if a.privateKeyFile == nil {
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +01002619 if ctx.Config().AllowMissingDependencies() {
2620 // TODO(b/266099037): a better approach for slim manifests.
2621 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2622 // Create placeholder paths for later stages that expect to see those paths,
2623 // though they won't be used.
2624 var unusedPath = android.PathForModuleOut(ctx, "nonexistentprivatekey")
2625 ctx.Build(pctx, android.BuildParams{
2626 Rule: android.ErrorRule,
2627 Output: unusedPath,
2628 Args: map[string]string{
2629 "error": "Private key not available",
2630 },
2631 })
2632 a.privateKeyFile = unusedPath
2633 } else {
2634 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
2635 return
2636 }
2637 }
2638
2639 if a.publicKeyFile == nil {
2640 if ctx.Config().AllowMissingDependencies() {
2641 // TODO(b/266099037): a better approach for slim manifests.
2642 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2643 // Create placeholder paths for later stages that expect to see those paths,
2644 // though they won't be used.
2645 var unusedPath = android.PathForModuleOut(ctx, "nonexistentpublickey")
2646 ctx.Build(pctx, android.BuildParams{
2647 Rule: android.ErrorRule,
2648 Output: unusedPath,
2649 Args: map[string]string{
2650 "error": "Public key not available",
2651 },
2652 })
2653 a.publicKeyFile = unusedPath
2654 } else {
2655 ctx.PropertyErrorf("key", "public_key for %q could not be found", String(a.overridableProperties.Key))
2656 return
2657 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002658 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002659
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002660 ////////////////////////////////////////////////////////////////////////////////////////////
2661 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002662 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002663 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002664
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002665 a.setApexTypeAndSuffix(ctx)
2666 a.setPayloadFsType(ctx)
2667 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002668 if a.properties.ApexType != zipApex {
2669 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2670 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002671
2672 ////////////////////////////////////////////////////////////////////////////////////////////
2673 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002674 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002675 if a.properties.ApexType == flattenedApex {
2676 a.buildFlattenedApex(ctx)
2677 } else {
2678 a.buildUnflattenedApex(ctx)
2679 }
Jiyong Park956305c2020-01-09 12:32:06 +09002680 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002681 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002682
2683 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2684 if a.installable() {
2685 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2686 // along with other ordinary files. (Note that this is done by apexer for
2687 // non-flattened APEXes)
2688 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2689
2690 // Place the public key as apex_pubkey. This is also done by apexer for
2691 // non-flattened APEXes case.
2692 // TODO(jiyong): Why do we need this CP rule?
2693 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2694 ctx.Build(pctx, android.BuildParams{
2695 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002696 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002697 Output: copiedPubkey,
2698 })
2699 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2700 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002701}
2702
Paul Duffincc33ec82021-04-25 23:14:55 +01002703// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2704// the bootclasspath_fragment contributes to the apex.
2705func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2706 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2707 var filesToAdd []apexFile
2708
satayev3db35472021-05-06 23:59:58 +01002709 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002710 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2711 filesToAdd = append(filesToAdd, *af)
2712 }
satayev3db35472021-05-06 23:59:58 +01002713
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002714 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
Jiakai Zhangbc698cd2023-05-08 16:28:38 +00002715 if pathInApex != "" {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002716 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2717 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2718
2719 if pathOnHost != nil {
2720 // We need to copy the profile to a temporary path with the right filename because the apexer
2721 // will take the filename as is.
2722 ctx.Build(pctx, android.BuildParams{
2723 Rule: android.Cp,
2724 Input: pathOnHost,
2725 Output: tempPath,
2726 })
2727 } else {
2728 // At this point, the boot image profile cannot be generated. It is probably because the boot
2729 // image profile source file does not exist on the branch, or it is not available for the
2730 // current build target.
2731 // However, we cannot enforce the boot image profile to be generated because some build
2732 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2733 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2734 // only if the APEX is being built.
2735 ctx.Build(pctx, android.BuildParams{
2736 Rule: android.ErrorRule,
2737 Output: tempPath,
2738 Args: map[string]string{
2739 "error": "Boot image profile cannot be generated",
2740 },
2741 })
2742 }
2743
2744 androidMkModuleName := filepath.Base(pathInApex)
2745 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2746 filesToAdd = append(filesToAdd, af)
2747 }
2748
Paul Duffincc33ec82021-04-25 23:14:55 +01002749 return filesToAdd
2750}
2751
satayevb98371c2021-06-15 16:49:50 +01002752// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2753// the module contributes to the apex; or nil if the proto config was not generated.
2754func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2755 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2756 if !info.ClasspathFragmentProtoGenerated {
2757 return nil
2758 }
2759 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2760 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2761 return &af
satayev14e49132021-05-17 21:03:07 +01002762}
2763
Paul Duffincc33ec82021-04-25 23:14:55 +01002764// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2765// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002766func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2767 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2768
2769 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2770 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002771 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2772 if err != nil {
2773 ctx.ModuleErrorf("%s", err)
2774 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002775
2776 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2777 // bootclasspath_fragment.
2778 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2779 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002780}
2781
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002782///////////////////////////////////////////////////////////////////////////////////////////////////
2783// Factory functions
2784//
2785
2786func newApexBundle() *apexBundle {
2787 module := &apexBundle{}
2788
2789 module.AddProperties(&module.properties)
2790 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002791 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002792 module.AddProperties(&module.overridableProperties)
2793
2794 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2795 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002796 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002797 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002798 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002799 return module
2800}
2801
Paul Duffineb8051d2021-10-18 17:49:39 +01002802func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002803 bundle := newApexBundle()
2804 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002805 return bundle
2806}
2807
2808// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2809// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002810func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002811 bundle := newApexBundle()
2812 bundle.testApex = true
2813 return bundle
2814}
2815
2816// apex packages other modules into an APEX file which is a packaging format for system-level
2817// components like binaries, shared libraries, etc.
2818func BundleFactory() android.Module {
2819 return newApexBundle()
2820}
2821
2822type Defaults struct {
2823 android.ModuleBase
2824 android.DefaultsModuleBase
2825}
2826
2827// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002828func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002829 module := &Defaults{}
2830
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002831 module.AddProperties(
2832 &apexBundleProperties{},
2833 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002834 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002835 &overridableProperties{},
2836 )
2837
2838 android.InitDefaultsModule(module)
2839 return module
2840}
2841
2842type OverrideApex struct {
2843 android.ModuleBase
2844 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002845 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002846}
2847
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002848func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002849 // All the overrides happen in the base module.
2850}
2851
2852// override_apex is used to create an apex module based on another apex module by overriding some of
2853// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002854func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002855 m := &OverrideApex{}
2856
2857 m.AddProperties(&overridableProperties{})
2858
2859 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2860 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002861 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002862 return m
2863}
2864
Wei Li1c66fc72022-05-09 23:59:14 -07002865func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2866 if ctx.ModuleType() != "override_apex" {
2867 return
2868 }
2869
2870 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2871 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2872 if !baseApexExists {
2873 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2874 }
2875
2876 a, baseModuleIsApex := baseModule.(*apexBundle)
2877 if !baseModuleIsApex {
2878 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2879 }
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002880 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
Wei Li1c66fc72022-05-09 23:59:14 -07002881
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002882 // We just want the name, not module reference.
2883 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2884 attrs.Base_apex_name = &baseApexName
2885
Wei Li1c66fc72022-05-09 23:59:14 -07002886 for _, p := range o.GetProperties() {
2887 overridableProperties, ok := p.(*overridableProperties)
2888 if !ok {
2889 continue
2890 }
Wei Li40f98732022-05-20 22:08:11 -07002891
2892 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2893 // After it is converted in convertWithBp2build(baseApex, ctx),
2894 // the attrs.Manifest.Value.Label is the file path relative to the directory
2895 // of base apex. So the following code converts it to a label that looks like
2896 // <package of base apex>:<path of manifest file> if base apex and override
2897 // apex are not in the same package.
2898 baseApexPackage := ctx.OtherModuleDir(a)
2899 overrideApexPackage := ctx.ModuleDir()
2900 if baseApexPackage != overrideApexPackage {
2901 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2902 }
2903
Wei Li1c66fc72022-05-09 23:59:14 -07002904 // Key
2905 if overridableProperties.Key != nil {
2906 attrs.Key = bazel.LabelAttribute{}
2907 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2908 }
2909
2910 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002911 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002912 // If overridableProperties.Certificate is nil, clear this out as
2913 // well with zeroed structs, so the override_apex does not use the
2914 // base apex's certificate.
2915 attrs.Certificate = bazel.LabelAttribute{}
2916 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002917 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002918 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002919 }
2920
2921 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002922 if overridableProperties.Prebuilts != nil {
2923 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2924 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2925 }
Wei Li1c66fc72022-05-09 23:59:14 -07002926
2927 // Compressible
2928 if overridableProperties.Compressible != nil {
2929 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2930 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002931
2932 // Package name
2933 //
2934 // e.g. com.android.adbd's package name is com.android.adbd, but
2935 // com.google.android.adbd overrides the package name to com.google.android.adbd
2936 //
2937 // TODO: this can be overridden from the product configuration, see
2938 // getOverrideManifestPackageName and
2939 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2940 //
2941 // Instead of generating the BUILD files differently based on the product config
2942 // at the point of conversion, this should be handled by the BUILD file loading
2943 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2944 if overridableProperties.Package_name != "" {
2945 attrs.Package_name = &overridableProperties.Package_name
2946 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002947
2948 // Logging parent
2949 if overridableProperties.Logging_parent != "" {
2950 attrs.Logging_parent = &overridableProperties.Logging_parent
2951 }
Wei Li1c66fc72022-05-09 23:59:14 -07002952 }
2953
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002954 commonAttrs.Name = o.Name()
2955
2956 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07002957}
2958
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002959///////////////////////////////////////////////////////////////////////////////////////////////////
2960// Vality check routines
2961//
2962// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2963// certain conditions are not met.
2964//
2965// TODO(jiyong): move these checks to a separate go file.
2966
satayevad991492021-12-03 18:58:32 +00002967var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2968
Spandan Dasa5f39a12022-08-05 02:35:52 +00002969// 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 +09002970// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002971func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002972 if a.testApex || a.vndkApex {
2973 return
2974 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002975 // apexBundle::minSdkVersion reports its own errors.
2976 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002977 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002978}
2979
Albert Martineefabcf2022-03-21 20:11:16 +00002980// Returns apex's min_sdk_version string value, honoring overrides
2981func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2982 // Only override the minSdkVersion value on Apexes which already specify
2983 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2984 // min_sdk_version value is lower than the one to override with.
Liz Kammerbd58e742023-05-11 15:58:13 +00002985 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.properties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002986 if minApiLevel.IsNone() {
2987 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002988 }
2989
Colin Cross56534df2022-10-04 09:58:58 -07002990 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2991 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2992 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2993 minApiLevel = overrideApiLevel
2994 }
2995
2996 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002997}
2998
2999// Returns apex's min_sdk_version SdkSpec, honoring overrides
Spandan Das8c9ae7e2023-03-03 21:20:36 +00003000func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
3001 return a.minSdkVersion(ctx)
satayevad991492021-12-03 18:58:32 +00003002}
3003
Albert Martineefabcf2022-03-21 20:11:16 +00003004// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00003005func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00003006 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
3007}
3008
3009// Construct ApiLevel object from min_sdk_version string value
3010func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
3011 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09003012 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003013 }
Albert Martineefabcf2022-03-21 20:11:16 +00003014 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003015 if err != nil {
3016 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
3017 return android.NoneApiLevel
3018 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003019 return apiLevel
3020}
3021
3022// Ensures that a lib providing stub isn't statically linked
3023func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
3024 // Practically, we only care about regular APEXes on the device.
3025 if ctx.Host() || a.testApex || a.vndkApex {
3026 return
3027 }
3028
3029 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
3030
3031 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3032 if ccm, ok := to.(*cc.Module); ok {
3033 apexName := ctx.ModuleName()
3034 fromName := ctx.OtherModuleName(from)
3035 toName := ctx.OtherModuleName(to)
3036
3037 // If `to` is not actually in the same APEX as `from` then it does not need
3038 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003039 //
3040 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003041 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3042 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3043 return false
3044 }
3045
3046 // The dynamic linker and crash_dump tool in the runtime APEX is the only
3047 // exception to this rule. It can't make the static dependencies dynamic
3048 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09003049 // Same rule should be applied to linkerconfig, because it should be executed
3050 // only with static linked libraries before linker is available with ld.config.txt
3051 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003052 return false
3053 }
3054
3055 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
3056 if isStubLibraryFromOtherApex && !externalDep {
3057 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
3058 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
3059 }
3060
3061 }
3062 return true
3063 })
3064}
3065
satayevb98371c2021-06-15 16:49:50 +01003066// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003067func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
3068 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00003069 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003070 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
3071 }
Jiyong Park1bc84122021-06-22 20:23:05 +09003072 if a.UsePlatformApis() {
3073 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
3074 }
Jooyung Handfc864c2023-03-20 18:19:07 +09003075 if proptools.Bool(a.properties.Use_vndk_as_stable) {
3076 ctx.PropertyErrorf("use_vndk_as_stable", "updatable APEXes can't use external VNDK libs")
Daniel Norman69109112021-12-02 12:52:42 -08003077 }
Jiyong Parkf4020582021-11-29 12:37:10 +09003078 if a.FutureUpdatable() {
3079 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
3080 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003081 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01003082 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003083 }
3084}
3085
satayevb98371c2021-06-15 16:49:50 +01003086// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
3087func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
3088 ctx.VisitDirectDeps(func(module android.Module) {
3089 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
3090 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
3091 if !info.ClasspathFragmentProtoGenerated {
3092 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
3093 }
3094 }
3095 })
3096}
3097
3098// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003099func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003100 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3101 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003102 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3103 tag := ctx.OtherModuleDependencyTag(module)
3104 switch tag {
3105 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003106 if m, ok := module.(interface {
3107 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3108 }); ok {
3109 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003110 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3111 }
3112 }
3113 }
3114 })
3115}
3116
satayevb98371c2021-06-15 16:49:50 +01003117// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003118func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3119 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3120 if ctx.Host() || a.testApex || a.vndkApex {
3121 return
3122 }
3123
3124 // Because APEXes targeting other than system/system_ext partitions can't set
3125 // apex_available, we skip checks for these APEXes
3126 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3127 return
3128 }
3129
3130 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3131 // Requiring them and their transitive depencies with apex_available is not right
3132 // because they just add noise.
3133 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3134 return
3135 }
3136
3137 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3138 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3139 if externalDep {
3140 return false
3141 }
3142
3143 apexName := ctx.ModuleName()
Sam Delmericoca816532023-06-02 14:09:50 -04003144 for _, props := range ctx.Module().GetProperties() {
3145 if apexProps, ok := props.(*apexBundleProperties); ok {
3146 if apexProps.Apex_available_name != nil {
3147 apexName = *apexProps.Apex_available_name
3148 }
3149 }
3150 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003151 fromName := ctx.OtherModuleName(from)
3152 toName := ctx.OtherModuleName(to)
3153
3154 // If `to` is not actually in the same APEX as `from` then it does not need
3155 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003156 //
3157 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003158 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3159 // As soon as the dependency graph crosses the APEX boundary, don't go
3160 // further.
3161 return false
3162 }
3163
3164 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3165 return true
3166 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003167 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3168 "\n\nDependency path:%s\n\n"+
3169 "Consider adding %q to 'apex_available' property of %q",
3170 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003171 // Visit this module's dependencies to check and report any issues with their availability.
3172 return true
3173 })
3174}
3175
Jiyong Park192600a2021-08-03 07:52:17 +00003176// checkStaticExecutable ensures that executables in an APEX are not static.
3177func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003178 // No need to run this for host APEXes
3179 if ctx.Host() {
3180 return
3181 }
3182
Jiyong Park192600a2021-08-03 07:52:17 +00003183 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3184 if ctx.OtherModuleDependencyTag(module) != executableTag {
3185 return
3186 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003187
3188 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003189 apex := a.ApexVariationName()
3190 exec := ctx.OtherModuleName(module)
3191 if isStaticExecutableAllowed(apex, exec) {
3192 return
3193 }
3194 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3195 }
3196 })
3197}
3198
3199// A small list of exceptions where static executables are allowed in APEXes.
3200func isStaticExecutableAllowed(apex string, exec string) bool {
3201 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003202 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003203 "linker",
3204 "linkerconfig",
3205 },
3206 }
3207 execNames, ok := m[apex]
3208 return ok && android.InList(exec, execNames)
3209}
3210
braleeb0c1f0c2021-06-07 22:49:13 +08003211// Collect information for opening IDE project files in java/jdeps.go.
3212func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00003213 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
3214 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
3215 dpInfo.Deps = append(dpInfo.Deps, a.properties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003216 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3217}
3218
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003219var (
3220 apexAvailBaseline = makeApexAvailableBaseline()
3221 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3222)
3223
Colin Cross440e0d02020-06-11 11:32:11 -07003224func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003225 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003226 moduleName = normalizeModuleName(moduleName)
3227
Colin Cross440e0d02020-06-11 11:32:11 -07003228 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003229 return true
3230 }
3231
3232 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003233 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003234 return true
3235 }
3236
3237 return false
3238}
3239
3240func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003241 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3242 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003243 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003244 if strings.HasPrefix(moduleName, "libclang_rt.") {
3245 // This module has many arch variants that depend on the product being built.
3246 // We don't want to list them all
3247 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003248 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003249 if strings.HasPrefix(moduleName, "androidx.") {
3250 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3251 moduleName = "androidx"
3252 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003253 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003254}
3255
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003256// Transform the map of apex -> modules to module -> apexes.
3257func invertApexBaseline(m map[string][]string) map[string][]string {
3258 r := make(map[string][]string)
3259 for apex, modules := range m {
3260 for _, module := range modules {
3261 r[module] = append(r[module], apex)
3262 }
3263 }
3264 return r
3265}
3266
3267// Retrieve the baseline of apexes to which the supplied module belongs.
3268func BaselineApexAvailable(moduleName string) []string {
3269 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3270}
3271
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003272// This is a map from apex to modules, which overrides the apex_available setting for that
3273// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003274// TODO(b/147364041): remove this
3275func makeApexAvailableBaseline() map[string][]string {
3276 // The "Module separator"s below are employed to minimize merge conflicts.
3277 m := make(map[string][]string)
3278 //
3279 // Module separator
3280 //
3281 m["com.android.appsearch"] = []string{
3282 "icing-java-proto-lite",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003283 }
3284 //
3285 // Module separator
3286 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003287 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003288 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003289 }
3290 //
3291 // Module separator
3292 //
Spandan Das072f7bc2023-05-05 21:06:23 +00003293 m["com.android.cellbroadcast"] = []string{}
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003294 //
3295 // Module separator
3296 //
3297 m["com.android.extservices"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003298 "ExtServices-core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003299 "libtextclassifier-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003300 "textclassifier-statsd",
3301 "TextClassifierNotificationLibNoManifest",
3302 "TextClassifierServiceLibNoManifest",
3303 }
3304 //
3305 // Module separator
3306 //
3307 m["com.android.neuralnetworks"] = []string{
3308 "android.hardware.neuralnetworks@1.0",
3309 "android.hardware.neuralnetworks@1.1",
3310 "android.hardware.neuralnetworks@1.2",
3311 "android.hardware.neuralnetworks@1.3",
3312 "android.hidl.allocator@1.0",
3313 "android.hidl.memory.token@1.0",
3314 "android.hidl.memory@1.0",
3315 "android.hidl.safe_union@1.0",
3316 "libarect",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003317 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003318 }
3319 //
3320 // Module separator
3321 //
3322 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003323 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003324 }
3325 //
3326 // Module separator
3327 //
3328 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003329 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003330 }
3331 //
3332 // Module separator
3333 //
3334 m["com.android.mediaprovider"] = []string{
3335 "MediaProvider",
3336 "MediaProviderGoogle",
3337 "fmtlib_ndk",
3338 "libbase_ndk",
3339 "libfuse",
3340 "libfuse_jni",
3341 }
3342 //
3343 // Module separator
3344 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003345 m["com.android.runtime"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003346 "libc_aeabi",
3347 "libc_bionic",
3348 "libc_bionic_ndk",
3349 "libc_bootstrap",
3350 "libc_common",
3351 "libc_common_shared",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003352 "libc_dns",
3353 "libc_dynamic_dispatch",
3354 "libc_fortify",
3355 "libc_freebsd",
3356 "libc_freebsd_large_stack",
3357 "libc_gdtoa",
3358 "libc_init_dynamic",
3359 "libc_init_static",
3360 "libc_jemalloc_wrapper",
3361 "libc_netbsd",
3362 "libc_nomalloc",
3363 "libc_nopthread",
3364 "libc_openbsd",
3365 "libc_openbsd_large_stack",
3366 "libc_openbsd_ndk",
3367 "libc_pthread",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003368 "libc_syscalls",
3369 "libc_tzcode",
3370 "libc_unwind_static",
3371 "libdebuggerd",
3372 "libdebuggerd_common_headers",
3373 "libdebuggerd_handler_core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003374 "libdl_static",
3375 "libjemalloc5",
3376 "liblinker_main",
3377 "liblinker_malloc",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003378 "liblzma",
3379 "libprocinfo",
3380 "libpropertyinfoparser",
3381 "libscudo",
3382 "libstdc++",
3383 "libsystemproperties",
3384 "libtombstoned_client_static",
3385 "libunwindstack",
3386 "libz",
3387 "libziparchive",
3388 }
3389 //
3390 // Module separator
3391 //
3392 m["com.android.tethering"] = []string{
3393 "android.hardware.tetheroffload.config-V1.0-java",
3394 "android.hardware.tetheroffload.control-V1.0-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003395 "net-utils-framework-common",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003396 }
3397 //
3398 // Module separator
3399 //
3400 m["com.android.wifi"] = []string{
3401 "PlatformProperties",
3402 "android.hardware.wifi-V1.0-java",
3403 "android.hardware.wifi-V1.0-java-constants",
3404 "android.hardware.wifi-V1.1-java",
3405 "android.hardware.wifi-V1.2-java",
3406 "android.hardware.wifi-V1.3-java",
3407 "android.hardware.wifi-V1.4-java",
3408 "android.hardware.wifi.hostapd-V1.0-java",
3409 "android.hardware.wifi.hostapd-V1.1-java",
3410 "android.hardware.wifi.hostapd-V1.2-java",
3411 "android.hardware.wifi.supplicant-V1.0-java",
3412 "android.hardware.wifi.supplicant-V1.1-java",
3413 "android.hardware.wifi.supplicant-V1.2-java",
3414 "android.hardware.wifi.supplicant-V1.3-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003415 "bouncycastle-unbundled",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003416 "framework-wifi-util-lib",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003417 "ksoap2",
3418 "libnanohttpd",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003419 "wifi-lite-protos",
3420 "wifi-nano-protos",
3421 "wifi-service-pre-jarjar",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003422 }
3423 //
3424 // Module separator
3425 //
3426 m[android.AvailableToAnyApex] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003427 "libprofile-clang-extras",
3428 "libprofile-clang-extras_ndk",
3429 "libprofile-extras",
3430 "libprofile-extras_ndk",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003431 }
3432 return m
3433}
3434
3435func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003436 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3437 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003438}
3439
Spandan Dasf14e2542021-11-12 00:01:37 +00003440func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3441 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3442 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003443 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003444 With("name", jar).
3445 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3446 Because(jar +
3447 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003448 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003449 " 1. If the offending code is from a statically linked library, consider " +
3450 "removing that dependency and using an alternative already in the " +
3451 "bootclasspath, or perhaps a shared library." +
3452 " 2. Move the offending code into an allowed package.\n" +
3453 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3454 "health implications of bundling that code, particularly if the offending jar " +
3455 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003456
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003457 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003458 }
3459 return rules
3460}
3461
Anton Hanssone1b18362021-12-23 15:05:38 +00003462// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003463// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003464func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003465 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003466 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003467 "android.net.ssl",
3468 "com.android.org.conscrypt",
3469 },
Wei Li40f98732022-05-20 22:08:11 -07003470 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003471 "android.media",
3472 },
3473 }
3474}
3475
Anton Hanssone1b18362021-12-23 15:05:38 +00003476// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003477// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003478func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003479 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003480 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003481 "android.provider",
3482 },
Wei Li40f98732022-05-20 22:08:11 -07003483 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003484 "android.permission",
3485 "android.app.role",
3486 "com.android.permission",
3487 "com.android.role",
3488 },
Wei Li40f98732022-05-20 22:08:11 -07003489 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003490 "android.os.ext",
3491 },
Wei Li40f98732022-05-20 22:08:11 -07003492 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003493 "android.app",
3494 "android.os",
3495 "android.util",
3496 "com.android.internal.statsd",
3497 "com.android.server.stats",
3498 },
Wei Li40f98732022-05-20 22:08:11 -07003499 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003500 "com.android.server.wifi",
3501 "com.android.wifi.x",
3502 "android.hardware.wifi",
3503 "android.net.wifi",
3504 },
Wei Li40f98732022-05-20 22:08:11 -07003505 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003506 "android.net",
3507 },
3508 }
3509}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003510
3511// For Bazel / bp2build
3512
3513type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003514 Manifest bazel.LabelAttribute
3515 Android_manifest bazel.LabelAttribute
3516 File_contexts bazel.LabelAttribute
Jingwen Chena8623da2023-03-28 13:05:02 +00003517 Canned_fs_config bazel.LabelAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003518 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003519 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3520 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003521 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003522 Updatable bazel.BoolAttribute
3523 Installable bazel.BoolAttribute
3524 Binaries bazel.LabelListAttribute
3525 Prebuilts bazel.LabelListAttribute
3526 Native_shared_libs_32 bazel.LabelListAttribute
3527 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003528 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003529 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003530 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003531 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003532 Base_apex_name *string
Sam Delmericoe91698a2023-06-06 11:30:31 -04003533 Apex_available_name *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003534}
3535
3536type convertedNativeSharedLibs struct {
3537 Native_shared_libs_32 bazel.LabelListAttribute
3538 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003539}
3540
Liz Kammerb83b7b02022-12-21 14:53:41 -05003541const (
3542 minSdkVersionPropName = "Min_sdk_version"
3543)
3544
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003545// ConvertWithBp2build performs bp2build conversion of an apex
3546func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003547 // We only convert apex and apex_test modules at this time
3548 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003549 return
3550 }
3551
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003552 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
3553 commonAttrs.Name = a.Name()
Yu Liu4c212ce2022-10-14 12:20:20 -07003554 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003555}
3556
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003557func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties, android.CommonAttributes) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003558 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003559 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003560
3561 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003562 if a.properties.AndroidManifest != nil {
3563 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003564 }
3565
3566 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003567 if a.properties.File_contexts == nil {
3568 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3569 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3570 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3571 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003572 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003573 } else {
3574 // File_contexts is a file
3575 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003576 }
3577
Jingwen Chena8623da2023-03-28 13:05:02 +00003578 var cannedFsConfigAttribute bazel.LabelAttribute
3579 if a.properties.Canned_fs_config != nil {
3580 cannedFsConfigAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Canned_fs_config))
3581 }
3582
Cole Faust912bc882023-03-08 12:29:50 -08003583 productVariableProps := android.ProductVariableProperties(ctx, a)
Albert Martineefabcf2022-03-21 20:11:16 +00003584 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3585 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003586 var minSdkVersion bazel.StringAttribute
Liz Kammerbd58e742023-05-11 15:58:13 +00003587 if a.properties.Min_sdk_version != nil {
3588 minSdkVersion.SetValue(*a.properties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003589 }
3590 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3591 for c, p := range props {
3592 if val, ok := p.(*string); ok {
3593 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3594 }
3595 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003596 }
3597
3598 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003599 if a.overridableProperties.Key != nil {
3600 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003601 }
3602
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003603 // Certificate
3604 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003605
Yu Liu4ae55d12022-01-05 17:17:23 -08003606 nativeSharedLibs := &convertedNativeSharedLibs{
3607 Native_shared_libs_32: bazel.LabelListAttribute{},
3608 Native_shared_libs_64: bazel.LabelListAttribute{},
3609 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003610
3611 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3612 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3613 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3614 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3615 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003616 if a.CompileMultilib() != nil {
3617 compileMultilib = *a.CompileMultilib()
3618 }
3619
3620 // properties.Native_shared_libs is treated as "both"
3621 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3622 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3623 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3624 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3625 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003626
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003627 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003628 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3629 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3630
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003631 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003632 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003633
Yu Liu4c212ce2022-10-14 12:20:20 -07003634 var testsAttrs bazel.LabelListAttribute
3635 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3636 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3637 testsAttrs = bazel.MakeLabelListAttribute(tests)
3638 }
3639
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003640 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003641 if a.properties.Updatable != nil {
3642 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003643 }
3644
3645 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003646 if a.properties.Installable != nil {
3647 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003648 }
3649
Wei Lif034cb42022-01-19 15:54:31 -08003650 var compressibleAttribute bazel.BoolAttribute
3651 if a.overridableProperties.Compressible != nil {
3652 compressibleAttribute.Value = a.overridableProperties.Compressible
3653 }
3654
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003655 var packageName *string
3656 if a.overridableProperties.Package_name != "" {
3657 packageName = &a.overridableProperties.Package_name
3658 }
3659
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003660 var loggingParent *string
3661 if a.overridableProperties.Logging_parent != "" {
3662 loggingParent = &a.overridableProperties.Logging_parent
3663 }
3664
Wei Li1c66fc72022-05-09 23:59:14 -07003665 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003666 Manifest: manifestLabelAttribute,
3667 Android_manifest: androidManifestLabelAttribute,
3668 File_contexts: fileContextsLabelAttribute,
Jingwen Chena8623da2023-03-28 13:05:02 +00003669 Canned_fs_config: cannedFsConfigAttribute,
Yu Liu4ae55d12022-01-05 17:17:23 -08003670 Min_sdk_version: minSdkVersion,
3671 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003672 Certificate: certificate,
3673 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003674 Updatable: updatableAttribute,
3675 Installable: installableAttribute,
3676 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3677 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3678 Binaries: binariesLabelListAttribute,
3679 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003680 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003681 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003682 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003683 Tests: testsAttrs,
Sam Delmericoe91698a2023-06-06 11:30:31 -04003684 Apex_available_name: a.properties.Apex_available_name,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003685 }
3686
3687 props := bazel.BazelTargetModuleProperties{
3688 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003689 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003690 }
3691
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003692 commonAttrs := android.CommonAttributes{}
3693 if a.testApex {
3694 commonAttrs.Testonly = proptools.BoolPtr(true)
Spandan Dasa43ae132023-05-08 18:33:16 +00003695 // Set the api_domain of the test apex
3696 attrs.Base_apex_name = proptools.StringPtr(cc.GetApiDomain(a.Name()))
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003697 }
3698
3699 return attrs, props, commonAttrs
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003700}
Yu Liu4ae55d12022-01-05 17:17:23 -08003701
3702// The following conversions are based on this table where the rows are the compile_multilib
3703// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3704// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3705// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3706// should not be compiled.
3707// multib/compile_multilib, 32, 64, both, first
3708// 32, 32/32, none/none, 32/32, none/32
3709// 64, none/none, 64/none, 64/none, 64/none
3710// both, 32/32, 64/none, 32&64/32, 64/32
3711// first, 32/32, 64/none, 64/32, 64/32
3712
3713func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3714 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3715 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3716 switch compileMultilb {
3717 case "both", "32":
3718 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3719 case "first":
3720 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3721 case "64":
3722 // Incompatible, ignore
3723 default:
3724 invalidCompileMultilib(ctx, compileMultilb)
3725 }
3726}
3727
3728func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3729 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3730 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3731 switch compileMultilb {
3732 case "both", "64", "first":
3733 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3734 case "32":
3735 // Incompatible, ignore
3736 default:
3737 invalidCompileMultilib(ctx, compileMultilb)
3738 }
3739}
3740
3741func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3742 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3743 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3744 switch compileMultilb {
3745 case "both":
3746 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3747 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3748 case "first":
3749 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3750 case "32":
3751 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3752 case "64":
3753 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3754 default:
3755 invalidCompileMultilib(ctx, compileMultilb)
3756 }
3757}
3758
3759func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3760 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3761 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3762 switch compileMultilb {
3763 case "both", "first":
3764 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3765 case "32":
3766 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3767 case "64":
3768 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3769 default:
3770 invalidCompileMultilib(ctx, compileMultilb)
3771 }
3772}
3773
3774func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3775 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3776 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3777}
3778
3779func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3780 list := bazel.LabelListAttribute{}
3781 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3782 nativeSharedLibs.Native_shared_libs_32.Append(list)
3783}
3784
3785func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3786 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3787 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3788}
3789
3790func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3791 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3792 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3793}
3794
3795func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3796 labelListAttr *bazel.LabelListAttribute) {
3797 list := bazel.LabelListAttribute{}
3798 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3799 labelListAttr.Append(list)
3800}
3801
3802func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3803 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3804}
Spandan Dasf57a9662023-04-12 19:05:49 +00003805
3806func (a *apexBundle) IsTestApex() bool {
3807 return a.testApex
3808}