blob: f12c517e90a19aac73929453685b88dfd6beef62 [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 Gascóa70425f2022-05-20 13:05:34 +000022 "regexp"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090023 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024 "strings"
25
Jiyong Park48ca7dc2018-10-10 14:01:00 +090026 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080027 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090028 "github.com/google/blueprint/proptools"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070029
30 "android/soong/android"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040031 "android/soong/bazel"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070033 "android/soong/cc"
34 prebuilt_etc "android/soong/etc"
Jiyong Park12a719c2021-01-07 15:31:24 +090035 "android/soong/filesystem"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070036 "android/soong/java"
37 "android/soong/python"
Jiyong Park99644e92020-11-17 22:21:02 +090038 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070039 "android/soong/sh"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090040)
41
Jiyong Park8e6d52f2020-11-19 14:37:47 +090042func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000043 registerApexBuildComponents(android.InitRegistrationContext)
44}
Jiyong Park8e6d52f2020-11-19 14:37:47 +090045
Paul Duffin667893c2021-03-09 22:34:13 +000046func registerApexBuildComponents(ctx android.RegistrationContext) {
47 ctx.RegisterModuleType("apex", BundleFactory)
48 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
49 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
50 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
51 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
52 ctx.RegisterModuleType("override_apex", overrideApexFactory)
53 ctx.RegisterModuleType("apex_set", apexSetFactory)
54
Paul Duffin5dda3e32021-05-05 14:13:27 +010055 ctx.PreArchMutators(registerPreArchMutators)
Paul Duffin667893c2021-03-09 22:34:13 +000056 ctx.PreDepsMutators(RegisterPreDepsMutators)
57 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090058}
59
Paul Duffin5dda3e32021-05-05 14:13:27 +010060func registerPreArchMutators(ctx android.RegisterMutatorsContext) {
61 ctx.TopDown("prebuilt_apex_module_creator", prebuiltApexModuleCreatorMutator).Parallel()
62}
63
Jiyong Park8e6d52f2020-11-19 14:37:47 +090064func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
65 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
66 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
67}
68
69func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Paul Duffin949abc02020-12-08 10:34:30 +000070 ctx.TopDown("apex_info", apexInfoMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090071 ctx.BottomUp("apex_unique", apexUniqueVariationsMutator).Parallel()
72 ctx.BottomUp("apex_test_for_deps", apexTestForDepsMutator).Parallel()
73 ctx.BottomUp("apex_test_for", apexTestForMutator).Parallel()
Paul Duffin28bf7ee2021-05-12 16:41:35 +010074 // Run mark_platform_availability before the apexMutator as the apexMutator needs to know whether
75 // it should create a platform variant.
76 ctx.BottomUp("mark_platform_availability", markPlatformAvailability).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090077 ctx.BottomUp("apex", apexMutator).Parallel()
78 ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).Parallel()
79 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Spandan Das66773252022-01-15 00:23:18 +000080 // Register after apex_info mutator so that it can use ApexVariationName
81 ctx.TopDown("apex_strict_updatability_lint", apexStrictUpdatibilityLintMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090082}
83
84type apexBundleProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090085 // Json manifest file describing meta info of this APEX bundle. Refer to
86 // system/apex/proto/apex_manifest.proto for the schema. Default: "apex_manifest.json"
Jiyong Park8e6d52f2020-11-19 14:37:47 +090087 Manifest *string `android:"path"`
88
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090089 // AndroidManifest.xml file used for the zip container of this APEX bundle. If unspecified,
90 // a default one is automatically generated.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090091 AndroidManifest *string `android:"path"`
92
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090093 // Canonical name of this APEX bundle. Used to determine the path to the activated APEX on
94 // device (/apex/<apex_name>). If unspecified, follows the name property.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090095 Apex_name *string
96
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090097 // Determines the file contexts file for setting the security contexts to files in this APEX
98 // bundle. For platform APEXes, this should points to a file under /system/sepolicy Default:
99 // /system/sepolicy/apex/<module_name>_file_contexts.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900100 File_contexts *string `android:"path"`
101
Jiyong Park038e8522021-12-13 23:56:35 +0900102 // Path to the canned fs config file for customizing file's uid/gid/mod/capabilities. The
103 // format is /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where path_or_glob is a
104 // path or glob pattern for a file or set of files, uid/gid are numerial values of user ID
105 // and group ID, mode is octal value for the file mode, and cap is hexadecimal value for the
106 // capability. If this property is not set, or a file is missing in the file, default config
107 // is used.
108 Canned_fs_config *string `android:"path"`
109
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900110 ApexNativeDependencies
111
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900112 Multilib apexMultilibProperties
113
Sundong Ahn80c04892021-11-23 00:57:19 +0000114 // List of sh binaries that are embedded inside this APEX bundle.
115 Sh_binaries []string
116
Paul Duffin3abc1742021-03-15 19:32:23 +0000117 // List of platform_compat_config files that are embedded inside this APEX bundle.
118 Compat_configs []string
119
Jiyong Park12a719c2021-01-07 15:31:24 +0900120 // List of filesystem images that are embedded inside this APEX bundle.
121 Filesystems []string
122
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900123 // The minimum SDK version that this APEX must support at minimum. This is usually set to
124 // the SDK version that the APEX was first introduced.
125 Min_sdk_version *string
126
127 // Whether this APEX is considered updatable or not. When set to true, this will enforce
128 // additional rules for making sure that the APEX is truly updatable. To be updatable,
129 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000130 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900131 Updatable *bool
132
Jiyong Parkf4020582021-11-29 12:37:10 +0900133 // Marks that this APEX is designed to be updatable in the future, although it's not
134 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
135 // updatable APEXes. Currently, this disables the size optimization, so that the size of
136 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
137 // false.
138 Future_updatable *bool
139
Jiyong Park1bc84122021-06-22 20:23:05 +0900140 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
141 // false`. Default is false.
142 Platform_apis *bool
143
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900144 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
145 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900146 Installable *bool
147
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900148 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
149 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
150 Use_vndk_as_stable *bool
151
Daniel Norman6cfb37af2021-11-16 20:28:29 +0000152 // Whether this is multi-installed APEX should skip installing symbol files.
153 // Multi-installed APEXes share the same apex_name and are installed at the same time.
154 // Default is false.
155 //
156 // Should be set to true for all multi-installed APEXes except the singular
157 // default version within the multi-installed group.
158 // Only the default version can install symbol files in $(PRODUCT_OUT}/apex,
159 // or else conflicting build rules may be created.
160 Multi_install_skip_symbol_files *bool
161
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900162 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
163 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
164 // container. When set to zip, contents are stored in a zip container directly. This type is
165 // mostly for host-side debugging. When set to both, the two types are both built. Default
166 // is 'image'.
167 Payload_type *string
168
Huang Jianan13cac632021-08-02 15:02:17 +0800169 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
170 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900171 Payload_fs_type *string
172
173 // For telling the APEX to ignore special handling for system libraries such as bionic.
174 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900175 Ignore_system_library_special_case *bool
176
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100177 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100178 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100179 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900180
181 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
182 // used in tests.
183 Test_only_unsigned_payload *bool
184
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000185 // Whenever apex should be compressed, regardless of product flag used. Should be only
186 // used in tests.
187 Test_only_force_compression *bool
188
Jooyung Han09c11ad2021-10-27 03:45:31 +0900189 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
190 // with the tool to sign payload contents.
191 Custom_sign_tool *string
192
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100193 // Canonical name of this APEX bundle. Used to determine the path to the
194 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
195 // apex mutator variations. For override_apex modules, this is the name of the
196 // overridden base module.
197 ApexVariationName string `blueprint:"mutated"`
198
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900199 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900200
201 // List of sanitizer names that this APEX is enabled for
202 SanitizerNames []string `blueprint:"mutated"`
203
204 PreventInstall bool `blueprint:"mutated"`
205
206 HideFromMake bool `blueprint:"mutated"`
207
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900208 // Internal package method for this APEX. When payload_type is image, this can be either
209 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
210 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900211 ApexType apexPackaging `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900212}
213
214type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900215 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900216 Native_shared_libs []string
217
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900218 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900219 Jni_libs []string
220
Jiyong Park99644e92020-11-17 22:21:02 +0900221 // List of rust dyn libraries
222 Rust_dyn_libs []string
223
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900224 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900225 Binaries []string
226
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900227 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900228 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900229
230 // List of filesystem images that are embedded inside this APEX bundle.
231 Filesystems []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900232}
233
234type apexMultilibProperties struct {
235 // Native dependencies whose compile_multilib is "first"
236 First ApexNativeDependencies
237
238 // Native dependencies whose compile_multilib is "both"
239 Both ApexNativeDependencies
240
241 // Native dependencies whose compile_multilib is "prefer32"
242 Prefer32 ApexNativeDependencies
243
244 // Native dependencies whose compile_multilib is "32"
245 Lib32 ApexNativeDependencies
246
247 // Native dependencies whose compile_multilib is "64"
248 Lib64 ApexNativeDependencies
249}
250
251type apexTargetBundleProperties struct {
252 Target struct {
253 // Multilib properties only for android.
254 Android struct {
255 Multilib apexMultilibProperties
256 }
257
258 // Multilib properties only for host.
259 Host struct {
260 Multilib apexMultilibProperties
261 }
262
263 // Multilib properties only for host linux_bionic.
264 Linux_bionic struct {
265 Multilib apexMultilibProperties
266 }
267
268 // Multilib properties only for host linux_glibc.
269 Linux_glibc struct {
270 Multilib apexMultilibProperties
271 }
272 }
273}
274
Jiyong Park59140302020-12-14 18:44:04 +0900275type apexArchBundleProperties struct {
276 Arch struct {
277 Arm struct {
278 ApexNativeDependencies
279 }
280 Arm64 struct {
281 ApexNativeDependencies
282 }
283 X86 struct {
284 ApexNativeDependencies
285 }
286 X86_64 struct {
287 ApexNativeDependencies
288 }
289 }
290}
291
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900292// These properties can be used in override_apex to override the corresponding properties in the
293// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900294type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900295 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900296 Apps []string
297
Daniel Norman5a3ce132021-08-26 15:44:43 -0700298 // List of prebuilt files that are embedded inside this APEX bundle.
299 Prebuilts []string
300
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900301 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900302 Rros []string
303
markchien7c803b82021-08-26 22:10:06 +0800304 // List of BPF programs inside this APEX bundle.
305 Bpfs []string
306
Remi NGUYEN VANbe901722022-03-02 21:00:33 +0900307 // List of bootclasspath fragments that are embedded inside this APEX bundle.
308 Bootclasspath_fragments []string
309
310 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
311 Systemserverclasspath_fragments []string
312
313 // List of java libraries that are embedded inside this APEX bundle.
314 Java_libs []string
315
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900316 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
317 // Soong). This does not completely prevent installation of the overridden binaries, but if
318 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
319 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900320 Overrides []string
321
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900322 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900323 Logging_parent string
324
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900325 // Apex Container package name. Override value for attribute package:name in
326 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900327 Package_name string
328
329 // A txt file containing list of files that are allowed to be included in this APEX.
330 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700331
332 // Name of the apex_key module that provides the private key to sign this APEX bundle.
333 Key *string
334
335 // Specifies the certificate and the private key to sign the zip container of this APEX. If
336 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
337 // as the certificate and the private key, respectively. If this is ":module", then the
338 // certificate and the private key are provided from the android_app_certificate module
339 // named "module".
340 Certificate *string
Oriol Prieto Gasco2c4a9632021-10-14 15:33:41 -0400341
342 // Whether this APEX can be compressed or not. Setting this property to false means this
343 // APEX will never be compressed. When set to true, APEX will be compressed if other
344 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
345 // Default: false.
346 Compressible *bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900347}
348
349type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900350 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900351 android.ModuleBase
352 android.DefaultableModuleBase
353 android.OverridableModuleBase
354 android.SdkBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400355 android.BazelModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900356
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900357 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900358 properties apexBundleProperties
359 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900360 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900361 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900362 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900363
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900364 ///////////////////////////////////////////////////////////////////////////////////////////
365 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900366
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900367 // Keys for apex_paylaod.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800368 publicKeyFile android.Path
369 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900370
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900371 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800372 containerCertificateFile android.Path
373 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900374
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900375 // Flags for special variants of APEX
376 testApex bool
377 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900378
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900379 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
380 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900381 primaryApexType bool
382
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900383 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900384 suffix string
385
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900386 // File system type of apex_payload.img
387 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900388
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900389 // Whether to create symlink to the system file instead of having a file inside the apex or
390 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900391 linkToSystemLib bool
392
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900393 // List of files to be included in this APEX. This is filled in the first part of
394 // GenerateAndroidBuildActions.
395 filesInfo []apexFile
396
397 // List of other module names that should be installed when this APEX gets installed.
398 requiredDeps []string
399
400 ///////////////////////////////////////////////////////////////////////////////////////////
401 // Outputs (final and intermediates)
402
403 // Processed apex manifest in JSONson format (for Q)
404 manifestJsonOut android.WritablePath
405
406 // Processed apex manifest in PB format (for R+)
407 manifestPbOut android.WritablePath
408
409 // Processed file_contexts files
410 fileContexts android.WritablePath
411
Bob Badourde6a0872022-04-01 18:00:00 +0000412 // Path to notice file in html.gz format.
413 htmlGzNotice android.WritablePath
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900414
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900415 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900416 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900417 outputFile android.WritablePath
418
Jooyung Hana6d36672022-02-24 13:58:07 +0900419 // The built uncompressed .apex file.
420 outputApexFile android.WritablePath
421
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900422 // The built APEX file in app bundle format. This file is not directly installed to the
423 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
424 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
425 // system) to be merged into a single app bundle file that Play accepts. See
426 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
427 bundleModuleFile android.WritablePath
428
Colin Cross6340ea52021-11-04 12:01:18 -0700429 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900430 installDir android.InstallPath
431
Colin Cross6340ea52021-11-04 12:01:18 -0700432 // Path where this APEX was installed.
433 installedFile android.InstallPath
434
435 // Installed locations of symlinks for backward compatibility.
436 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900437
438 // Text file having the list of individual files that are included in this APEX. Used for
439 // debugging purpose.
440 installedFilesFile android.WritablePath
441
442 // List of module names that this APEX is including (to be shown via *-deps-info target).
443 // Used for debugging purpose.
444 android.ApexBundleDepsInfo
445
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900446 // Optional list of lint report zip files for apexes that contain java or app modules
447 lintReports android.Paths
448
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900449 prebuiltFileToDelete string
sophiezc80a2b32020-11-12 16:39:19 +0000450
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000451 isCompressed bool
452
sophiezc80a2b32020-11-12 16:39:19 +0000453 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700454 nativeApisUsedByModuleFile android.ModuleOutPath
455 nativeApisBackedByModuleFile android.ModuleOutPath
456 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800457
458 // Collect the module directory for IDE info in java/jdeps.go.
459 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900460}
461
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900462// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900463type apexFileClass int
464
Jooyung Han72bd2f82019-10-23 16:46:38 +0900465const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900466 app apexFileClass = iota
467 appSet
468 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900469 goBinary
470 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900471 nativeExecutable
472 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900473 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900474 pyBinary
475 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900476)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900477
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900478// apexFile represents a file in an APEX bundle. This is created during the first half of
479// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
480// of the function, this is used to create commands that copies the files into a staging directory,
481// where they are packaged into the APEX file. This struct is also used for creating Make modules
482// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900483type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900484 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000485 builtFile android.Path
486 installDir string
487 customStem string
488 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900489
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900490 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
491 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
492 // suffix>]
493 androidMkModuleName string // becomes LOCAL_MODULE
494 class apexFileClass // becomes LOCAL_MODULE_CLASS
495 moduleDir string // becomes LOCAL_PATH
496 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
497 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
498 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
499 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900500
501 jacocoReportClassesFile android.Path // only for javalibs and apps
502 lintDepSets java.LintDepSets // only for javalibs and apps
503 certificate java.Certificate // only for apps
504 overriddenPackageName string // only for apps
505
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900506 transitiveDep bool
507 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900508
Jiyong Park57621b22021-01-20 20:33:11 +0900509 multilib string
510
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900511 // TODO(jiyong): remove this
512 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900513}
514
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900515// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900516func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
517 ret := apexFile{
518 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900519 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900520 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900521 class: class,
522 module: module,
523 }
524 if module != nil {
525 ret.moduleDir = ctx.OtherModuleDir(module)
526 ret.requiredModuleNames = module.RequiredModuleNames()
527 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
528 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900529 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900530 }
531 return ret
532}
533
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900534func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900535 return af.builtFile != nil && af.builtFile.String() != ""
536}
537
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900538// apexRelativePath returns the relative path of the given path from the install directory of this
539// apexFile.
540// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900541func (af *apexFile) apexRelativePath(path string) string {
542 return filepath.Join(af.installDir, path)
543}
544
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900545// path returns path of this apex file relative to the APEX root
546func (af *apexFile) path() string {
547 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900548}
549
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900550// stem returns the base filename of this apex file
551func (af *apexFile) stem() string {
552 if af.customStem != "" {
553 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900554 }
555 return af.builtFile.Base()
556}
557
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900558// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
559func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900560 var ret []string
561 for _, symlink := range af.symlinks {
562 ret = append(ret, af.apexRelativePath(symlink))
563 }
564 return ret
565}
566
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900567// availableToPlatform tests whether this apexFile is from a module that can be installed to the
568// platform.
569func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900570 if af.module == nil {
571 return false
572 }
573 if am, ok := af.module.(android.ApexModule); ok {
574 return am.AvailableFor(android.AvailableToPlatform)
575 }
576 return false
577}
578
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900579////////////////////////////////////////////////////////////////////////////////////////////////////
580// Mutators
581//
582// Brief description about mutators for APEX. The following three mutators are the most important
583// ones.
584//
585// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
586// to the (direct) dependencies of this APEX bundle.
587//
Paul Duffin949abc02020-12-08 10:34:30 +0000588// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900589// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
590// modules are marked as being included in the APEX via BuildForApex().
591//
Paul Duffin949abc02020-12-08 10:34:30 +0000592// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
593// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900594
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900595type dependencyTag struct {
596 blueprint.BaseDependencyTag
597 name string
598
599 // Determines if the dependent will be part of the APEX payload. Can be false for the
600 // dependencies to the signing key module, etc.
601 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000602
603 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
604 // replacement. This is needed because some prebuilt modules do not provide all the information
605 // needed by the apex.
606 sourceOnly bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900607}
608
Paul Duffin8c535da2021-03-17 14:51:03 +0000609func (d dependencyTag) ReplaceSourceWithPrebuilt() bool {
610 return !d.sourceOnly
611}
612
613var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
614
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900615var (
Paul Duffin0b817782021-03-17 15:02:19 +0000616 androidAppTag = dependencyTag{name: "androidApp", payload: true}
617 bpfTag = dependencyTag{name: "bpf", payload: true}
618 certificateTag = dependencyTag{name: "certificate"}
619 executableTag = dependencyTag{name: "executable", payload: true}
620 fsTag = dependencyTag{name: "filesystem", payload: true}
Paul Duffin94f19632021-04-20 12:40:07 +0100621 bcpfTag = dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true}
satayev333a1732021-05-17 21:35:26 +0100622 sscpfTag = dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true}
Paul Duffin1b29e002021-03-16 15:06:54 +0000623 compatConfigTag = dependencyTag{name: "compatConfig", payload: true, sourceOnly: true}
Paul Duffin0b817782021-03-17 15:02:19 +0000624 javaLibTag = dependencyTag{name: "javaLib", payload: true}
625 jniLibTag = dependencyTag{name: "jniLib", payload: true}
626 keyTag = dependencyTag{name: "key"}
627 prebuiltTag = dependencyTag{name: "prebuilt", payload: true}
628 rroTag = dependencyTag{name: "rro", payload: true}
629 sharedLibTag = dependencyTag{name: "sharedLib", payload: true}
630 testForTag = dependencyTag{name: "test for"}
631 testTag = dependencyTag{name: "test", payload: true}
Sundong Ahn80c04892021-11-23 00:57:19 +0000632 shBinaryTag = dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900633)
634
635// TODO(jiyong): shorten this function signature
636func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900637 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900638 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900639 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900640
641 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900642 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900643 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
644 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900645 }
646
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900647 // Use *FarVariation* to be able to depend on modules having conflicting variations with
648 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
649 // 'arm' or 'arm64' for native shared libs.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900650 ctx.AddFarVariationDependencies(binVariations, executableTag, nativeModules.Binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900651 ctx.AddFarVariationDependencies(binVariations, testTag, nativeModules.Tests...)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900652 ctx.AddFarVariationDependencies(libVariations, jniLibTag, nativeModules.Jni_libs...)
653 ctx.AddFarVariationDependencies(libVariations, sharedLibTag, nativeModules.Native_shared_libs...)
Jiyong Park99644e92020-11-17 22:21:02 +0900654 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag, nativeModules.Rust_dyn_libs...)
Jiyong Park06711462021-02-15 17:54:43 +0900655 ctx.AddFarVariationDependencies(target.Variations(), fsTag, nativeModules.Filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900656}
657
658func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900659 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900660 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
661 } else {
662 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
663 if ctx.Os().Bionic() {
664 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
665 } else {
666 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
667 }
668 }
669}
670
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900671// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
672// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
673func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
674 deviceConfig := ctx.DeviceConfig()
675 if a.vndkApex {
676 return cc.VendorVariationPrefix + a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900677 }
678
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900679 var prefix string
680 var vndkVersion string
681 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000682 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900683 prefix = cc.VendorVariationPrefix
684 vndkVersion = deviceConfig.VndkVersion()
685 } else if a.ProductSpecific() {
686 prefix = cc.ProductVariationPrefix
687 vndkVersion = deviceConfig.ProductVndkVersion()
688 }
689 }
690 if vndkVersion == "current" {
691 vndkVersion = deviceConfig.PlatformVndkVersion()
692 }
693 if vndkVersion != "" {
694 return prefix + vndkVersion
695 }
696
697 return android.CoreVariation // The usual case
698}
699
700func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900701 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
702 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
703 // each target os/architectures, appropriate dependencies are selected by their
704 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900705 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900706 imageVariation := a.getImageVariation(ctx)
707
708 a.combineProperties(ctx)
709
710 has32BitTarget := false
711 for _, target := range targets {
712 if target.Arch.ArchType.Multilib == "lib32" {
713 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000714 }
715 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900716 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900717 // Don't include artifacts for the host cross targets because there is no way for us
718 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900719 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900720 continue
721 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000722
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900723 var depsList []ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000724
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900725 // Add native modules targeting both ABIs. When multilib.* is omitted for
726 // native_shared_libs/jni_libs/tests, it implies multilib.both
727 depsList = append(depsList, a.properties.Multilib.Both)
728 depsList = append(depsList, ApexNativeDependencies{
729 Native_shared_libs: a.properties.Native_shared_libs,
730 Tests: a.properties.Tests,
731 Jni_libs: a.properties.Jni_libs,
732 Binaries: nil,
733 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900734
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900735 // Add native modules targeting the first ABI When multilib.* is omitted for
736 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900737 isPrimaryAbi := i == 0
738 if isPrimaryAbi {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900739 depsList = append(depsList, a.properties.Multilib.First)
740 depsList = append(depsList, ApexNativeDependencies{
741 Native_shared_libs: nil,
742 Tests: nil,
743 Jni_libs: nil,
744 Binaries: a.properties.Binaries,
745 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900746 }
747
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900748 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900749 switch target.Arch.ArchType.Multilib {
750 case "lib32":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900751 depsList = append(depsList, a.properties.Multilib.Lib32)
752 depsList = append(depsList, a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900753 case "lib64":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900754 depsList = append(depsList, a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900755 if !has32BitTarget {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900756 depsList = append(depsList, a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900757 }
758 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900759
Jiyong Park59140302020-12-14 18:44:04 +0900760 // Add native modules targeting a specific arch variant
761 switch target.Arch.ArchType {
762 case android.Arm:
763 depsList = append(depsList, a.archProperties.Arch.Arm.ApexNativeDependencies)
764 case android.Arm64:
765 depsList = append(depsList, a.archProperties.Arch.Arm64.ApexNativeDependencies)
766 case android.X86:
767 depsList = append(depsList, a.archProperties.Arch.X86.ApexNativeDependencies)
768 case android.X86_64:
769 depsList = append(depsList, a.archProperties.Arch.X86_64.ApexNativeDependencies)
770 default:
771 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
772 }
773
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900774 for _, d := range depsList {
775 addDependenciesForNativeModules(ctx, d, target, imageVariation)
776 }
Sundong Ahn80c04892021-11-23 00:57:19 +0000777 ctx.AddFarVariationDependencies([]blueprint.Variation{
778 {Mutator: "os", Variation: target.OsVariation()},
779 {Mutator: "arch", Variation: target.ArchVariation()},
780 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900781 }
782
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900783 // Common-arch dependencies come next
784 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Jiyong Park12a719c2021-01-07 15:31:24 +0900785 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000786 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100787}
788
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900789// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900790func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
791 if a.overridableProperties.Allowed_files != nil {
792 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100793 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900794
795 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
796 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800797 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900798 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.overridableProperties.Rros...)
Remi NGUYEN VANbe901722022-03-02 21:00:33 +0900799 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.overridableProperties.Bootclasspath_fragments...)
800 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.overridableProperties.Systemserverclasspath_fragments...)
801 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.overridableProperties.Java_libs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700802 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
803 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
804 // regardless of the TARGET_PREFER_* setting. See b/144532908
805 arches := ctx.DeviceConfig().Arches()
806 if len(arches) != 0 {
807 archForPrebuiltEtc := arches[0]
808 for _, arch := range arches {
809 // Prefer 64-bit arch if there is any
810 if arch.ArchType.Multilib == "lib64" {
811 archForPrebuiltEtc = arch
812 break
813 }
814 }
815 ctx.AddFarVariationDependencies([]blueprint.Variation{
816 {Mutator: "os", Variation: ctx.Os().String()},
817 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
818 }, prebuiltTag, prebuilts...)
819 }
820 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700821
822 // Dependencies for signing
823 if String(a.overridableProperties.Key) == "" {
824 ctx.PropertyErrorf("key", "missing")
825 return
826 }
827 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
828
829 cert := android.SrcIsModule(a.getCertString(ctx))
830 if cert != "" {
831 ctx.AddDependency(ctx.Module(), certificateTag, cert)
832 // empty cert is not an error. Cert and private keys will be directly found under
833 // PRODUCT_DEFAULT_DEV_CERTIFICATE
834 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100835}
836
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900837type ApexBundleInfo struct {
838 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100839}
840
Paul Duffin949abc02020-12-08 10:34:30 +0000841var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900842
Paul Duffina7d6a892020-12-07 17:39:59 +0000843var _ ApexInfoMutator = (*apexBundle)(nil)
844
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100845func (a *apexBundle) ApexVariationName() string {
846 return a.properties.ApexVariationName
847}
848
Paul Duffina7d6a892020-12-07 17:39:59 +0000849// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900850// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
851// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
852// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
853// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000854//
855// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
856// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
857// The apexMutator uses that list to create module variants for the apexes to which it belongs.
858// The relationship between module variants and apexes is not one-to-one as variants will be
859// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000860func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900861
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900862 // The VNDK APEX is special. For the APEX, the membership is described in a very different
863 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
864 // libraries are self-identified by their vndk.enabled properties. There is no need to run
865 // this mutator for the APEX as nothing will be collected. So, let's return fast.
866 if a.vndkApex {
867 return
868 }
869
870 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
871 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
872 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
873 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
874 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900875 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
876 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +0900877 if proptools.Bool(a.properties.Use_vndk_as_stable) {
878 if !useVndk {
879 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
880 }
881 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
882 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
883 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
884 }
885 })
886 if mctx.Failed() {
887 return
888 }
Jooyung Handf78e212020-07-22 15:54:47 +0900889 }
890
Colin Cross56a83212020-09-15 18:30:11 -0700891 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900892 am, ok := child.(android.ApexModule)
893 if !ok || !am.CanHaveApexVariants() {
894 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +0900895 }
Paul Duffin573989d2021-03-17 13:25:29 +0000896 depTag := mctx.OtherModuleDependencyTag(child)
897
898 // Check to see if the tag always requires that the child module has an apex variant for every
899 // apex variant of the parent module. If it does not then it is still possible for something
900 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
901 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
902 return true
903 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000904 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900905 return false
906 }
Jooyung Handf78e212020-07-22 15:54:47 +0900907 if excludeVndkLibs {
908 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
909 return false
910 }
911 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900912 // By default, all the transitive dependencies are collected, unless filtered out
913 // above.
Colin Cross56a83212020-09-15 18:30:11 -0700914 return true
915 }
916
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900917 // Records whether a certain module is included in this apexBundle via direct dependency or
918 // inndirect dependency.
919 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -0700920 mctx.WalkDeps(func(child, parent android.Module) bool {
921 if !continueApexDepsWalk(child, parent) {
922 return false
923 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900924 // If the parent is apexBundle, this child is directly depended.
925 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900926 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -0700927 contents[depName] = contents[depName].Add(directDep)
928 return true
929 })
930
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900931 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +0900932 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -0700933 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
934 Contents: apexContents,
935 })
936
Jooyung Haned124c32021-01-26 11:43:46 +0900937 minSdkVersion := a.minSdkVersion(mctx)
938 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
939 if minSdkVersion.IsNone() {
940 minSdkVersion = android.FutureApiLevel
941 }
942
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900943 // This is the main part of this mutator. Mark the collected dependencies that they need to
944 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +0900945
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100946 apexVariationName := proptools.StringDefault(a.properties.Apex_name, mctx.ModuleName()) // could be com.android.foo
947 a.properties.ApexVariationName = apexVariationName
Colin Cross56a83212020-09-15 18:30:11 -0700948 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100949 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +0900950 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -0700951 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +0900952 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100953 InApexVariants: []string{apexVariationName},
954 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -0700955 ApexContents: []*android.ApexContents{apexContents},
956 }
Colin Cross56a83212020-09-15 18:30:11 -0700957 mctx.WalkDeps(func(child, parent android.Module) bool {
958 if !continueApexDepsWalk(child, parent) {
959 return false
960 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900961 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +0900962 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +0900963 })
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900964}
965
Paul Duffina7d6a892020-12-07 17:39:59 +0000966type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100967 // ApexVariationName returns the name of the APEX variation to use in the apex
968 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
969 ApexVariationName() string
970
Paul Duffina7d6a892020-12-07 17:39:59 +0000971 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
972 // depended upon by an apex and which require an apex specific variant.
973 ApexInfoMutator(android.TopDownMutatorContext)
974}
975
976// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
977// specific variant to modules that support the ApexInfoMutator.
Spandan Das91250b12022-05-06 22:12:55 +0000978// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +0000979func apexInfoMutator(mctx android.TopDownMutatorContext) {
980 if !mctx.Module().Enabled() {
981 return
982 }
983
984 if a, ok := mctx.Module().(ApexInfoMutator); ok {
985 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +0000986 }
Spandan Das91250b12022-05-06 22:12:55 +0000987 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +0000988}
989
Spandan Das66773252022-01-15 00:23:18 +0000990// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
991// This check is enforced for updatable modules
992func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
993 if !mctx.Module().Enabled() {
994 return
995 }
Spandan Das08c911f2022-01-21 22:07:26 +0000996 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +0000997 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +0000998 // b/208656169 Do not propagate strict updatability linting to libcore/
999 // These libs are available on the classpath during compilation
1000 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1001 // Only skip libraries defined in libcore root, not subdirectories
1002 if mctx.OtherModuleDir(child) == "libcore" {
1003 // Do not traverse transitive deps of libcore/ libs
1004 return false
1005 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001006 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1007 return false
1008 }
Spandan Das66773252022-01-15 00:23:18 +00001009 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1010 lintable.SetStrictUpdatabilityLinting(true)
1011 }
1012 // visit transitive deps
1013 return true
1014 })
1015 }
1016}
1017
Spandan Das91250b12022-05-06 22:12:55 +00001018// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1019func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1020 if !mctx.Module().Enabled() {
1021 return
1022 }
1023 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1024 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1025 mctx.VisitDirectDeps(func(module android.Module) {
1026 // ignore android_test_app
1027 if app, ok := module.(*java.AndroidApp); ok {
1028 app.SetUpdatable(true)
1029 }
1030 })
1031 }
1032}
1033
Spandan Das08c911f2022-01-21 22:07:26 +00001034// TODO: b/215736885 Whittle the denylist
1035// Transitive deps of certain mainline modules baseline NewApi errors
1036// Skip these mainline modules for now
1037var (
1038 skipStrictUpdatabilityLintAllowlist = []string{
1039 "com.android.art",
1040 "com.android.art.debug",
1041 "com.android.conscrypt",
1042 "com.android.media",
1043 // test apexes
1044 "test_com.android.art",
1045 "test_com.android.conscrypt",
1046 "test_com.android.media",
1047 "test_jitzygote_com.android.art",
1048 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001049
1050 // TODO: b/215736885 Remove this list
1051 skipLintJavalibAllowlist = []string{
1052 "conscrypt.module.platform.api.stubs",
1053 "conscrypt.module.public.api.stubs",
1054 "conscrypt.module.public.api.stubs.system",
1055 "conscrypt.module.public.api.stubs.module_lib",
1056 "framework-media.stubs",
1057 "framework-media.stubs.system",
1058 "framework-media.stubs.module_lib",
1059 }
Spandan Das08c911f2022-01-21 22:07:26 +00001060)
1061
1062func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1063 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1064}
1065
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001066// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1067// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1068// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001069func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1070 if !mctx.Module().Enabled() {
1071 return
1072 }
1073 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001074 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1075 }
1076}
1077
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001078// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1079// the apex in order to retrieve its contents later.
1080// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001081func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1082 if !mctx.Module().Enabled() {
1083 return
1084 }
Colin Cross56a83212020-09-15 18:30:11 -07001085 if am, ok := mctx.Module().(android.ApexModule); ok {
1086 if testFor := am.TestFor(); len(testFor) > 0 {
1087 mctx.AddFarVariationDependencies([]blueprint.Variation{
1088 {Mutator: "os", Variation: am.Target().OsVariation()},
1089 {"arch", "common"},
1090 }, testForTag, testFor...)
1091 }
1092 }
1093}
1094
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001095// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001096func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1097 if !mctx.Module().Enabled() {
1098 return
1099 }
Colin Cross56a83212020-09-15 18:30:11 -07001100 if _, ok := mctx.Module().(android.ApexModule); ok {
1101 var contents []*android.ApexContents
1102 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1103 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1104 contents = append(contents, abInfo.Contents)
1105 }
1106 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1107 ApexContents: contents,
1108 })
Colin Crossaede88c2020-08-11 12:17:01 -07001109 }
1110}
1111
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001112// markPlatformAvailability marks whether or not a module can be available to platform. A module
1113// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1114// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1115// be) available to platform
1116// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001117func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1118 // Host and recovery are not considered as platform
1119 if mctx.Host() || mctx.Module().InstallInRecovery() {
1120 return
1121 }
1122
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001123 am, ok := mctx.Module().(android.ApexModule)
1124 if !ok {
1125 return
1126 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001127
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001128 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001129
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001130 // If any of the dep is not available to platform, this module is also considered as being
1131 // not available to platform even if it has "//apex_available:platform"
1132 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001133 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001134 // if the dependency crosses apex boundary, don't consider it
1135 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001136 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001137 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1138 availableToPlatform = false
1139 // TODO(b/154889534) trigger an error when 'am' has
1140 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001141 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001142 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001143
Paul Duffinb5769c12021-05-12 16:16:51 +01001144 // Exception 1: check to see if the module always requires it.
1145 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001146 availableToPlatform = true
1147 }
1148
1149 // Exception 2: bootstrap bionic libraries are also always available to platform
1150 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1151 availableToPlatform = true
1152 }
1153
1154 if !availableToPlatform {
1155 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001156 }
1157}
1158
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001159// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001160// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001161func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001162 if !mctx.Module().Enabled() {
1163 return
1164 }
Colin Cross56a83212020-09-15 18:30:11 -07001165
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001166 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001167 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001168 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001169 return
1170 }
1171
1172 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001173 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1174 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001175 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001176 if strings.HasPrefix(apexBundleName, "com.android.art") {
1177 // Create an alias from the platform variant. This is done to make
1178 // test_for dependencies work for modules that are split by the APEX
1179 // mutator, since test_for dependencies always go to the platform variant.
1180 // This doesn't happen for normal APEXes that are disjunct, so only do
1181 // this for the overlapping ART APEXes.
1182 // TODO(b/183882457): Remove this if the test_for functionality is
1183 // refactored to depend on the proper APEX variants instead of platform.
1184 mctx.CreateAliasVariation("", apexBundleName)
1185 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001186 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1187 apexBundleName := o.GetOverriddenModuleName()
1188 if apexBundleName == "" {
1189 mctx.ModuleErrorf("base property is not set")
1190 return
1191 }
1192 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001193 if strings.HasPrefix(apexBundleName, "com.android.art") {
1194 // TODO(b/183882457): See note for CreateAliasVariation above.
1195 mctx.CreateAliasVariation("", apexBundleName)
1196 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001197 }
1198}
Sundong Ahne9b55722019-09-06 17:37:42 +09001199
Paul Duffin6717d882021-06-15 19:09:41 +01001200// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1201// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001202func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001203 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001204 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001205 return !a.vndkApex
1206 }
1207
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001208 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001209}
1210
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001211// See android.UpdateDirectlyInAnyApex
1212// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001213func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1214 if !mctx.Module().Enabled() {
1215 return
1216 }
1217 if am, ok := mctx.Module().(android.ApexModule); ok {
1218 android.UpdateDirectlyInAnyApex(mctx, am)
1219 }
1220}
1221
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001222// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001223type apexPackaging int
1224
1225const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001226 // imageApex is a packaging method where contents are included in a filesystem image which
1227 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001228 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001229
1230 // zipApex is a packaging method where contents are directly included in the zip container.
1231 // This is used for host-side testing - because the contents are easily accessible by
1232 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001233 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001234
1235 // flattendApex is a packaging method where contents are not included in the APEX file, but
1236 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1237 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001238 flattenedApex
1239)
1240
1241const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001242 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001243 imageApexSuffix = ".apex"
1244 imageCapexSuffix = ".capex"
1245 zipApexSuffix = ".zipapex"
1246 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001247
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001248 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001249 imageApexType = "image"
1250 zipApexType = "zip"
1251 flattenedApexType = "flattened"
1252
Dan Willemsen47e1a752021-10-16 18:36:13 -07001253 ext4FsType = "ext4"
1254 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001255 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001256)
1257
1258// The suffix for the output "file", not the module
1259func (a apexPackaging) suffix() string {
1260 switch a {
1261 case imageApex:
1262 return imageApexSuffix
1263 case zipApex:
1264 return zipApexSuffix
1265 default:
1266 panic(fmt.Errorf("unknown APEX type %d", a))
1267 }
1268}
1269
1270func (a apexPackaging) name() string {
1271 switch a {
1272 case imageApex:
1273 return imageApexType
1274 case zipApex:
1275 return zipApexType
1276 default:
1277 panic(fmt.Errorf("unknown APEX type %d", a))
1278 }
1279}
1280
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001281// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1282// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001283func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001284 if !mctx.Module().Enabled() {
1285 return
1286 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001287 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001288 var variants []string
1289 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1290 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001291 // This is the normal case. Note that both image and flattend APEXes are
1292 // created. The image type is installed to the system partition, while the
1293 // flattened APEX is (optionally) installed to the system_ext partition.
1294 // This is mostly for GSI which has to support wide range of devices. If GSI
1295 // is installed on a newer (APEX-capable) device, the image APEX in the
1296 // system will be used. However, if the same GSI is installed on an old
1297 // device which can't support image APEX, the flattened APEX in the
1298 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001299 variants = append(variants, imageApexType, flattenedApexType)
1300 case "zip":
1301 variants = append(variants, zipApexType)
1302 case "both":
1303 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1304 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001305 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001306 return
1307 }
1308
1309 modules := mctx.CreateLocalVariations(variants...)
1310
1311 for i, v := range variants {
1312 switch v {
1313 case imageApexType:
1314 modules[i].(*apexBundle).properties.ApexType = imageApex
1315 case zipApexType:
1316 modules[i].(*apexBundle).properties.ApexType = zipApex
1317 case flattenedApexType:
1318 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001319 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001320 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001321 modules[i].(*apexBundle).MakeAsSystemExt()
1322 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001323 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001324 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001325 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001326 // payload_type is forcibly overridden to "image"
1327 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001328 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001329 }
1330}
1331
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001332var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001333
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001334// Implements android.DepInInSameApex
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001335func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1336 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001337 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001338 return true
1339}
1340
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001341var _ android.OutputFileProducer = (*apexBundle)(nil)
1342
1343// Implements android.OutputFileProducer
1344func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1345 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001346 case "", android.DefaultDistTag:
1347 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001348 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001349 case imageApexSuffix:
1350 // uncompressed one
1351 if a.outputApexFile != nil {
1352 return android.Paths{a.outputApexFile}, nil
1353 }
1354 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001355 default:
1356 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1357 }
1358}
1359
1360var _ cc.Coverage = (*apexBundle)(nil)
1361
1362// Implements cc.Coverage
1363func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1364 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1365}
1366
1367// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001368func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001369 a.properties.PreventInstall = true
1370}
1371
1372// Implements cc.Coverage
1373func (a *apexBundle) HideFromMake() {
1374 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001375 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1376 // TODO(ccross): untangle these
1377 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001378}
1379
1380// Implements cc.Coverage
1381func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1382 a.properties.IsCoverageVariant = coverage
1383}
1384
1385// Implements cc.Coverage
1386func (a *apexBundle) EnableCoverageIfNeeded() {}
1387
1388var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1389
Oriol Prieto Gasco2c4a9632021-10-14 15:33:41 -04001390// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001391func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001392 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001393}
1394
Jiyong Parkf4020582021-11-29 12:37:10 +09001395func (a *apexBundle) FutureUpdatable() bool {
1396 return proptools.BoolDefault(a.properties.Future_updatable, false)
1397}
1398
Jiyong Park1bc84122021-06-22 20:23:05 +09001399func (a *apexBundle) UsePlatformApis() bool {
1400 return proptools.BoolDefault(a.properties.Platform_apis, false)
1401}
1402
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001403// getCertString returns the name of the cert that should be used to sign this APEX. This is
1404// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001405func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001406 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001407 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1408 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1409 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001410 if a.vndkApex {
1411 moduleName = vndkApexName
1412 }
1413 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001414 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001415 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001416 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001417 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001418}
1419
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001420// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001421func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001422 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001423}
1424
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001425// See the generate_hashtree property
1426func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001427 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001428}
1429
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001430// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001431func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1432 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1433}
1434
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001435// See the test_only_force_compression property
1436func (a *apexBundle) testOnlyShouldForceCompression() bool {
1437 return proptools.Bool(a.properties.Test_only_force_compression)
1438}
1439
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001440// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1441// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1442// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001443
Jiyong Parkf97782b2019-02-13 20:28:58 +09001444func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1445 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1446 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1447 }
1448}
1449
Jiyong Park388ef3f2019-01-28 19:47:32 +09001450func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001451 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1452 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001453 }
1454
1455 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +09001456 globalSanitizerNames := []string{}
1457 if a.Host() {
1458 globalSanitizerNames = ctx.Config().SanitizeHost()
1459 } else {
1460 arches := ctx.Config().SanitizeDeviceArch()
1461 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
1462 globalSanitizerNames = ctx.Config().SanitizeDevice()
1463 }
1464 }
1465 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001466}
1467
Jooyung Han8ce8db92020-05-15 19:05:05 +09001468func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001469 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1470 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001471 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001472 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001473 for _, target := range ctx.MultiTargets() {
1474 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001475 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Crossfc0df952022-02-10 11:41:18 -08001476 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001477 Tests: nil,
1478 Jni_libs: nil,
1479 Binaries: nil,
1480 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001481 break
1482 }
1483 }
1484 }
1485}
1486
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001487// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1488// returned apexFile saves information about the Soong module that will be used for creating the
1489// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001490func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001491 // Decide the APEX-local directory by the multilib of the library In the future, we may
1492 // query this to the module.
1493 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001494 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001495 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001496 case "lib32":
1497 dirInApex = "lib"
1498 case "lib64":
1499 dirInApex = "lib64"
1500 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001501 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001502 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001503 }
Jooyung Han35155c42020-02-06 17:33:20 +09001504 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park1833cef2019-12-13 13:28:36 +09001505 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001506 // Special case for Bionic libs and other libs installed with them. This is to
1507 // prevent those libs from being included in the search path
1508 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1509 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1510 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1511 // will be loaded into the default linker namespace (aka "platform" namespace). If
1512 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1513 // be loaded again into the runtime linker namespace, which will result in double
1514 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001515 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001516 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001517
Jiyong Parkf653b052019-11-18 15:39:01 +09001518 fileToCopy := ccMod.OutputFile().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001519 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1520 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001521}
1522
Jiyong Park1833cef2019-12-13 13:28:36 +09001523func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001524 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001525 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001526 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001527 }
Jooyung Han35155c42020-02-06 17:33:20 +09001528 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Parkf653b052019-11-18 15:39:01 +09001529 fileToCopy := cc.OutputFile().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001530 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1531 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001532 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001533 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001534 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001535}
1536
Jiyong Park99644e92020-11-17 22:21:02 +09001537func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1538 dirInApex := "bin"
1539 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1540 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1541 }
1542 fileToCopy := rustm.OutputFile().Path()
1543 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1544 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1545 return af
1546}
1547
1548func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1549 // Decide the APEX-local directory by the multilib of the library
1550 // In the future, we may query this to the module.
1551 var dirInApex string
1552 switch rustm.Arch().ArchType.Multilib {
1553 case "lib32":
1554 dirInApex = "lib"
1555 case "lib64":
1556 dirInApex = "lib64"
1557 }
1558 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1559 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1560 }
1561 fileToCopy := rustm.OutputFile().Path()
1562 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1563 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1564}
1565
Jiyong Park1833cef2019-12-13 13:28:36 +09001566func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.Module) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001567 dirInApex := "bin"
1568 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001569 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001570}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001571
Jiyong Park1833cef2019-12-13 13:28:36 +09001572func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001573 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001574 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001575 // NB: Since go binaries are static we don't need the module for anything here, which is
1576 // good since the go tool is a blueprint.Module not an android.Module like we would
1577 // normally use.
Jiyong Park1833cef2019-12-13 13:28:36 +09001578 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001579}
1580
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001581func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001582 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001583 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1584 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1585 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001586 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001587 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001588 af.symlinks = sh.Symlinks()
1589 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001590}
1591
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001592func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001593 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001594 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001595 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001596}
1597
atrost6e126252020-01-27 17:01:16 +00001598func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1599 dirInApex := filepath.Join("etc", config.SubDir())
1600 fileToCopy := config.CompatConfig()
1601 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1602}
1603
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001604// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1605// way.
1606type javaModule interface {
1607 android.Module
1608 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001609 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001610 JacocoReportClassesFile() android.Path
1611 LintDepSets() java.LintDepSets
1612 Stem() string
1613}
1614
1615var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001616var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001617var _ javaModule = (*java.SdkLibrary)(nil)
1618var _ javaModule = (*java.DexImport)(nil)
1619var _ javaModule = (*java.SdkLibraryImport)(nil)
1620
Paul Duffin190fdef2021-04-26 10:33:59 +01001621// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001622func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001623 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001624}
1625
1626// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1627func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001628 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001629 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001630 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1631 af.lintDepSets = module.LintDepSets()
1632 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001633 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1634 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1635 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1636 }
1637 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001638 return af
1639}
1640
1641// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1642// the same way.
1643type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001644 android.Module
1645 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001646 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001647 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001648 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001649 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001650 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001651 LintDepSets() java.LintDepSets
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001652}
1653
1654var _ androidApp = (*java.AndroidApp)(nil)
1655var _ androidApp = (*java.AndroidAppImport)(nil)
1656
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00001657func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1658 buildId := ctx.Config().BuildId()
1659
1660 // The build ID is used as a suffix for a filename, so ensure that
1661 // the set of characters being used are sanitized.
1662 // - any word character: [a-zA-Z0-9_]
1663 // - dots: .
1664 // - dashes: -
1665 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1666 if !validRegex.MatchString(buildId) {
1667 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1668 }
1669 return buildId
1670}
Jingwen Chen11cca672022-03-25 02:57:49 +00001671
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001672func apexFileForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001673 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001674 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001675 appDir = "priv-app"
1676 }
Jingwen Chen11cca672022-03-25 02:57:49 +00001677
1678 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1679 // so that PackageManager correctly invalidates the existing installed apk
1680 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00001681 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001682 fileToCopy := aapp.OutputFile()
Jingwen Chen11cca672022-03-25 02:57:49 +00001683
Yo Chiange8128052020-07-23 20:09:18 +08001684 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001685 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001686 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001687 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001688
1689 if app, ok := aapp.(interface {
1690 OverriddenManifestPackageName() string
1691 }); ok {
1692 af.overriddenPackageName = app.OverriddenManifestPackageName()
1693 }
Jiyong Park618922e2020-01-08 13:35:43 +09001694 return af
Dario Frenicde2a032019-10-27 00:29:22 +01001695}
1696
Jiyong Park69aeba92020-04-24 21:16:36 +09001697func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1698 rroDir := "overlay"
1699 dirInApex := filepath.Join(rroDir, rro.Theme())
1700 fileToCopy := rro.OutputFile()
1701 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1702 af.certificate = rro.Certificate()
1703
1704 if a, ok := rro.(interface {
1705 OverriddenManifestPackageName() string
1706 }); ok {
1707 af.overriddenPackageName = a.OverriddenManifestPackageName()
1708 }
1709 return af
1710}
1711
Ken Chenfad7f9d2021-11-10 22:02:57 +08001712func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1713 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001714 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1715}
1716
Jiyong Park12a719c2021-01-07 15:31:24 +09001717func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1718 dirInApex := filepath.Join("etc", "fs")
1719 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1720}
1721
Paul Duffin064b70c2020-11-02 17:32:38 +00001722// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001723// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1724// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1725// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001726func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001727 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001728 am, ok := child.(android.ApexModule)
1729 if !ok || !am.CanHaveApexVariants() {
1730 return false
1731 }
1732
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001733 // Filter-out unwanted depedendencies
1734 depTag := ctx.OtherModuleDependencyTag(child)
1735 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1736 return false
1737 }
1738 if dt, ok := depTag.(dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001739 return false
1740 }
1741
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001742 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001743 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001744
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001745 // Visit actually
1746 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001747 })
1748}
1749
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001750// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1751type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001752
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001753const (
1754 ext4 fsType = iota
1755 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001756 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001757)
Artur Satayev849f8442020-04-28 14:57:42 +01001758
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001759func (f fsType) string() string {
1760 switch f {
1761 case ext4:
1762 return ext4FsType
1763 case f2fs:
1764 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001765 case erofs:
1766 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001767 default:
1768 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001769 }
1770}
1771
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001772// Creates build rules for an APEX. It consists of the following major steps:
1773//
1774// 1) do some validity checks such as apex_available, min_sdk_version, etc.
1775// 2) traverse the dependency tree to collect apexFile structs from them.
1776// 3) some fields in apexBundle struct are configured
1777// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001778func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001779 ////////////////////////////////////////////////////////////////////////////////////////////
1780 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Jiyong Park0f80c182020-01-31 02:49:53 +09001781 a.checkApexAvailability(ctx)
Jooyung Han548640b2020-04-27 12:10:30 +09001782 a.checkUpdatable(ctx)
satayevb3fd4112021-12-02 13:59:35 +00001783 a.CheckMinSdkVersion(ctx)
Jiyong Park7d95a512020-05-10 15:16:24 +09001784 a.checkStaticLinkingToStubLibraries(ctx)
Jiyong Park192600a2021-08-03 07:52:17 +00001785 a.checkStaticExecutables(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001786 if len(a.properties.Tests) > 0 && !a.testApex {
1787 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
1788 return
1789 }
Jiyong Park678c8812020-02-07 17:25:49 +09001790
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001791 ////////////////////////////////////////////////////////////////////////////////////////////
1792 // 2) traverse the dependency tree to collect apexFile structs from them.
1793
1794 // all the files that will be included in this APEX
1795 var filesInfo []apexFile
Alex Lightfc0bd7c2019-01-29 18:31:59 -08001796
Jooyung Hane1633032019-08-01 17:41:43 +09001797 // native lib dependencies
1798 var provideNativeLibs []string
1799 var requireNativeLibs []string
1800
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001801 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1802
braleeb0c1f0c2021-06-07 22:49:13 +08001803 // Collect the module directory for IDE info in java/jdeps.go.
1804 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
1805
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001806 // TODO(jiyong): do this using WalkPayloadDeps
1807 // TODO(jiyong): make this clean!!!
Alex Light778127a2019-02-27 14:19:50 -08001808 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +01001809 depTag := ctx.OtherModuleDependencyTag(child)
Paul Duffindddd5462020-04-07 15:25:44 +01001810 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1811 return false
1812 }
Dan Willemsen47e1a752021-10-16 18:36:13 -07001813 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
1814 return false
1815 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001816 depName := ctx.OtherModuleName(child)
Jiyong Parkf653b052019-11-18 15:39:01 +09001817 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001818 switch depTag {
Jooyung Han643adc42020-02-27 13:50:06 +09001819 case sharedLibTag, jniLibTag:
1820 isJniLib := depTag == jniLibTag
Jooyung Hanfaa2d5f2020-02-06 17:42:40 +09001821 if c, ok := child.(*cc.Module); ok {
Jooyung Han643adc42020-02-27 13:50:06 +09001822 fi := apexFileForNativeLibrary(ctx, c, handleSpecialLibs)
1823 fi.isJniLib = isJniLib
1824 filesInfo = append(filesInfo, fi)
Jooyung Han45a96772020-06-15 14:59:42 +09001825 // Collect the list of stub-providing libs except:
1826 // - VNDK libs are only for vendors
1827 // - bootstrap bionic libs are treated as provided by system
1828 if c.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(c.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001829 provideNativeLibs = append(provideNativeLibs, fi.stem())
Jiyong Parkf1493cc2020-05-29 21:29:20 +09001830 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001831 return true // track transitive dependencies
Jiyong Parkf2cc1b72020-12-09 00:20:45 +09001832 } else if r, ok := child.(*rust.Module); ok {
1833 fi := apexFileForRustLibrary(ctx, r)
Benjamin Brittain9edc3752021-11-30 13:38:13 -05001834 fi.isJniLib = isJniLib
Jiyong Parkf2cc1b72020-12-09 00:20:45 +09001835 filesInfo = append(filesInfo, fi)
Jiyong Park34d5c332022-02-24 18:02:44 +09001836 return true // track transitive dependencies
Jiyong Parkff1458f2018-10-12 21:49:38 +09001837 } else {
Jooyung Han643adc42020-02-27 13:50:06 +09001838 propertyName := "native_shared_libs"
1839 if isJniLib {
1840 propertyName = "jni_libs"
1841 }
1842 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001843 }
1844 case executableTag:
1845 if cc, ok := child.(*cc.Module); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001846 filesInfo = append(filesInfo, apexFileForExecutable(ctx, cc))
Jiyong Parkf653b052019-11-18 15:39:01 +09001847 return true // track transitive dependencies
Alex Light778127a2019-02-27 14:19:50 -08001848 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
Jiyong Park1833cef2019-12-13 13:28:36 +09001849 filesInfo = append(filesInfo, apexFileForPyBinary(ctx, py))
Alex Light778127a2019-02-27 14:19:50 -08001850 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
Jiyong Parkf653b052019-11-18 15:39:01 +09001851 filesInfo = append(filesInfo, apexFileForGoBinary(ctx, depName, gb))
Jiyong Park99644e92020-11-17 22:21:02 +09001852 } else if rust, ok := child.(*rust.Module); ok {
1853 filesInfo = append(filesInfo, apexFileForRustExecutable(ctx, rust))
1854 return true // track transitive dependencies
Jiyong Parkff1458f2018-10-12 21:49:38 +09001855 } else {
Sundong Ahn80c04892021-11-23 00:57:19 +00001856 ctx.PropertyErrorf("binaries", "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
1857 }
1858 case shBinaryTag:
1859 if sh, ok := child.(*sh.ShBinary); ok {
1860 filesInfo = append(filesInfo, apexFileForShBinary(ctx, sh))
1861 } else {
1862 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001863 }
Paul Duffin94f19632021-04-20 12:40:07 +01001864 case bcpfTag:
Paul Duffina1d60252021-01-21 18:13:43 +00001865 {
Jiakai Zhang6decef92022-01-12 17:56:19 +00001866 bcpfModule, ok := child.(*java.BootclasspathFragmentModule)
1867 if !ok {
Paul Duffincc33ec82021-04-25 23:14:55 +01001868 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
Paul Duffina1d60252021-01-21 18:13:43 +00001869 return false
1870 }
Paul Duffin4d101b62021-03-24 15:42:20 +00001871
Paul Duffincc33ec82021-04-25 23:14:55 +01001872 filesToAdd := apexBootclasspathFragmentFiles(ctx, child)
1873 filesInfo = append(filesInfo, filesToAdd...)
Jiakai Zhang6decef92022-01-12 17:56:19 +00001874 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
1875 a.requiredDeps = append(a.requiredDeps, makeModuleName)
1876 }
Paul Duffin4d101b62021-03-24 15:42:20 +00001877 return true
Paul Duffina1d60252021-01-21 18:13:43 +00001878 }
satayev333a1732021-05-17 21:35:26 +01001879 case sscpfTag:
1880 {
1881 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
1882 ctx.PropertyErrorf("systemserverclasspath_fragments", "%q is not a systemserverclasspath_fragment module", depName)
1883 return false
1884 }
satayevb98371c2021-06-15 16:49:50 +01001885 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
1886 filesInfo = append(filesInfo, *af)
1887 }
satayev333a1732021-05-17 21:35:26 +01001888 return true
1889 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001890 case javaLibTag:
Jiyong Park77acec62020-06-01 21:39:15 +09001891 switch child.(type) {
Bill Peckhama41a6962021-01-11 10:58:54 -08001892 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001893 af := apexFileForJavaModule(ctx, child.(javaModule))
1894 if !af.ok() {
Jooyung Han58f26ab2019-12-18 15:34:32 +09001895 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1896 return false
1897 }
1898 filesInfo = append(filesInfo, af)
Jooyung Han58f26ab2019-12-18 15:34:32 +09001899 return true // track transitive dependencies
Jiyong Park77acec62020-06-01 21:39:15 +09001900 default:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001901 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001902 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001903 case androidAppTag:
Jiyong Parkf653b052019-11-18 15:39:01 +09001904 if ap, ok := child.(*java.AndroidApp); ok {
Jooyung Han39ee1192020-03-23 20:21:11 +09001905 filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
Jiyong Parkf653b052019-11-18 15:39:01 +09001906 return true // track transitive dependencies
1907 } else if ap, ok := child.(*java.AndroidAppImport); ok {
Jooyung Han39ee1192020-03-23 20:21:11 +09001908 filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
Dario Freni6f3937c2019-12-20 22:58:03 +00001909 } else if ap, ok := child.(*java.AndroidTestHelperApp); ok {
Jooyung Han39ee1192020-03-23 20:21:11 +09001910 filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
Sasha Smundak18d98bc2020-05-27 16:36:07 -07001911 } else if ap, ok := child.(*java.AndroidAppSet); ok {
1912 appDir := "app"
1913 if ap.Privileged() {
1914 appDir = "priv-app"
1915 }
Jingwen Chen11cca672022-03-25 02:57:49 +00001916 // TODO(b/224589412, b/226559955): Ensure that the dirname is
1917 // suffixed so that PackageManager correctly invalidates the
1918 // existing installed apk in favour of the new APK-in-APEX.
1919 // See bugs for more information.
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00001920 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
Jingwen Chen11cca672022-03-25 02:57:49 +00001921 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07001922 af.certificate = java.PresignedCertificate
1923 filesInfo = append(filesInfo, af)
Jiyong Parkf653b052019-11-18 15:39:01 +09001924 } else {
1925 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1926 }
Jiyong Park69aeba92020-04-24 21:16:36 +09001927 case rroTag:
1928 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
1929 filesInfo = append(filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
1930 } else {
1931 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
1932 }
markchien2f59ec92020-09-02 16:23:38 +08001933 case bpfTag:
1934 if bpfProgram, ok := child.(bpf.BpfModule); ok {
1935 filesToCopy, _ := bpfProgram.OutputFiles("")
Ken Chenfad7f9d2021-11-10 22:02:57 +08001936 apex_sub_dir := bpfProgram.SubDir()
markchien2f59ec92020-09-02 16:23:38 +08001937 for _, bpfFile := range filesToCopy {
Ken Chenfad7f9d2021-11-10 22:02:57 +08001938 filesInfo = append(filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
markchien2f59ec92020-09-02 16:23:38 +08001939 }
1940 } else {
1941 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
1942 }
Jiyong Park12a719c2021-01-07 15:31:24 +09001943 case fsTag:
1944 if fs, ok := child.(filesystem.Filesystem); ok {
1945 filesInfo = append(filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
1946 } else {
1947 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
1948 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001949 case prebuiltTag:
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001950 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001951 filesInfo = append(filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
Jiyong Parkff1458f2018-10-12 21:49:38 +09001952 } else {
Paul Duffin1bc21dc2021-03-15 19:43:17 +00001953 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
Jiyong Parkff1458f2018-10-12 21:49:38 +09001954 }
Paul Duffin0b817782021-03-17 15:02:19 +00001955 case compatConfigTag:
Paul Duffin3abc1742021-03-15 19:32:23 +00001956 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
1957 filesInfo = append(filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
1958 } else {
1959 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
1960 }
Roland Levillain630846d2019-06-26 12:48:34 +01001961 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001962 if ccTest, ok := child.(*cc.Module); ok {
1963 if ccTest.IsTestPerSrcAllTestsVariation() {
1964 // Multiple-output test module (where `test_per_src: true`).
1965 //
1966 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1967 // We do not add this variation to `filesInfo`, as it has no output;
1968 // however, we do add the other variations of this module as indirect
1969 // dependencies (see below).
Roland Levillain9b5fde92019-06-28 15:41:19 +01001970 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001971 // Single-output test module (where `test_per_src: false`).
Jiyong Park1833cef2019-12-13 13:28:36 +09001972 af := apexFileForExecutable(ctx, ccTest)
Jiyong Parkf653b052019-11-18 15:39:01 +09001973 af.class = nativeTest
1974 filesInfo = append(filesInfo, af)
Roland Levillain9b5fde92019-06-28 15:41:19 +01001975 }
Jiyong Parkaf9539f2020-05-04 10:31:32 +09001976 return true // track transitive dependencies
Roland Levillain630846d2019-06-26 12:48:34 +01001977 } else {
1978 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1979 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001980 case keyTag:
1981 if key, ok := child.(*apexKey); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001982 a.privateKeyFile = key.privateKeyFile
1983 a.publicKeyFile = key.publicKeyFile
Jiyong Parkff1458f2018-10-12 21:49:38 +09001984 } else {
1985 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001986 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001987 return false
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001988 case certificateTag:
1989 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001990 a.containerCertificateFile = dep.Certificate.Pem
1991 a.containerPrivateKeyFile = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001992 } else {
1993 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1994 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001995 case android.PrebuiltDepTag:
1996 // If the prebuilt is force disabled, remember to delete the prebuilt file
1997 // that might have been installed in the previous builds
Jiyong Park10e926b2020-07-16 21:38:56 +09001998 if prebuilt, ok := child.(prebuilt); ok && prebuilt.isForceDisabled() {
Jiyong Park03b68dd2019-07-26 23:20:40 +09001999 a.prebuiltFileToDelete = prebuilt.InstallFilename()
2000 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002001 }
Jooyung Han8aee2042019-10-29 05:08:31 +09002002 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002003 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09002004 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01002005 // We cannot use a switch statement on `depTag` here as the checked
2006 // tags used below are private (e.g. `cc.sharedDepTag`).
Jiyong Park52cd06f2019-11-11 10:14:32 +09002007 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002008 if cc, ok := child.(*cc.Module); ok {
Jooyung Handf78e212020-07-22 15:54:47 +09002009 if cc.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && cc.IsVndk() {
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002010 requireNativeLibs = append(requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002011 return false
2012 }
Jiyong Parkf1493cc2020-05-29 21:29:20 +09002013 af := apexFileForNativeLibrary(ctx, cc, handleSpecialLibs)
2014 af.transitiveDep = true
Martin Stjernholmf2635ec2020-12-16 01:01:59 +00002015
2016 // Always track transitive dependencies for host.
2017 if a.Host() {
2018 filesInfo = append(filesInfo, af)
2019 return true
2020 }
2021
Colin Cross56a83212020-09-15 18:30:11 -07002022 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
Martin Stjernholmf2635ec2020-12-16 01:01:59 +00002023 if !abInfo.Contents.DirectlyInApex(depName) && (cc.IsStubs() || cc.HasStubsVariants()) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002024 // If the dependency is a stubs lib, don't include it in this APEX,
2025 // but make sure that the lib is installed on the device.
2026 // In case no APEX is having the lib, the lib is installed to the system
2027 // partition.
2028 //
2029 // Always include if we are a host-apex however since those won't have any
2030 // system libraries.
Colin Cross56a83212020-09-15 18:30:11 -07002031 if !am.DirectlyInAnyApex() {
Jooyung Hanefb184e2020-06-25 17:14:25 +09002032 // we need a module name for Make
Steven Moreland2c4000c2021-04-27 02:08:49 +00002033 name := cc.ImplementationModuleNameForMake(ctx) + cc.Properties.SubName
Jooyung Hanefb184e2020-06-25 17:14:25 +09002034 if !android.InList(name, a.requiredDeps) {
2035 a.requiredDeps = append(a.requiredDeps, name)
2036 }
Roland Levillainf89cd092019-07-29 16:22:59 +01002037 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002038 requireNativeLibs = append(requireNativeLibs, af.stem())
Roland Levillainf89cd092019-07-29 16:22:59 +01002039 // Don't track further
2040 return false
2041 }
Jiyong Parke3867542020-12-03 17:28:25 +09002042
2043 // If the dep is not considered to be in the same
2044 // apex, don't add it to filesInfo so that it is not
2045 // included in this APEX.
2046 // TODO(jiyong): move this to at the top of the
2047 // else-if clause for the indirect dependencies.
2048 // Currently, that's impossible because we would
2049 // like to record requiredNativeLibs even when
Martin Stjernholmf2635ec2020-12-16 01:01:59 +00002050 // DepIsInSameAPex is false. We also shouldn't do
2051 // this for host.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002052 //
2053 // TODO(jiyong): explain why the same module is passed in twice.
2054 // Switching the first am to parent breaks lots of tests.
2055 if !android.IsDepInSameApex(ctx, am, am) {
Jiyong Parke3867542020-12-03 17:28:25 +09002056 return false
2057 }
2058
Jiyong Parkf653b052019-11-18 15:39:01 +09002059 filesInfo = append(filesInfo, af)
2060 return true // track transitive dependencies
Jiyong Parkf2cc1b72020-12-09 00:20:45 +09002061 } else if rm, ok := child.(*rust.Module); ok {
2062 af := apexFileForRustLibrary(ctx, rm)
2063 af.transitiveDep = true
2064 filesInfo = append(filesInfo, af)
2065 return true // track transitive dependencies
Jiyong Park25fc6a92018-11-18 18:02:45 +09002066 }
Roland Levillainf89cd092019-07-29 16:22:59 +01002067 } else if cc.IsTestPerSrcDepTag(depTag) {
2068 if cc, ok := child.(*cc.Module); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09002069 af := apexFileForExecutable(ctx, cc)
Roland Levillainf89cd092019-07-29 16:22:59 +01002070 // Handle modules created as `test_per_src` variations of a single test module:
2071 // use the name of the generated test binary (`fileToCopy`) instead of the name
2072 // of the original test module (`depName`, shared by all `test_per_src`
2073 // variations of that module).
Yo Chiange8128052020-07-23 20:09:18 +08002074 af.androidMkModuleName = filepath.Base(af.builtFile.String())
Jiyong Park7cd10e32020-01-14 09:22:18 +09002075 // these are not considered transitive dep
2076 af.transitiveDep = false
Jiyong Parkf653b052019-11-18 15:39:01 +09002077 filesInfo = append(filesInfo, af)
2078 return true // track transitive dependencies
Roland Levillainf89cd092019-07-29 16:22:59 +01002079 }
Jiyong Park1ad8e162020-12-01 23:40:09 +09002080 } else if cc.IsHeaderDepTag(depTag) {
2081 // nothing
Jiyong Park52cd06f2019-11-11 10:14:32 +09002082 } else if java.IsJniDepTag(depTag) {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002083 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2084 return false
Jiyong Parke3833882020-02-17 17:28:10 +09002085 } else if java.IsXmlPermissionsFileDepTag(depTag) {
Jaewoong Jung4b79e982020-06-01 10:45:49 -07002086 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09002087 filesInfo = append(filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2088 }
Jiyong Park99644e92020-11-17 22:21:02 +09002089 } else if rust.IsDylibDepTag(depTag) {
2090 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2091 af := apexFileForRustLibrary(ctx, rustm)
2092 af.transitiveDep = true
2093 filesInfo = append(filesInfo, af)
2094 return true // track transitive dependencies
2095 }
Jiyong Park94e22fd2021-04-08 18:19:15 +09002096 } else if rust.IsRlibDepTag(depTag) {
2097 // Rlib is statically linked, but it might have shared lib
2098 // dependencies. Track them.
2099 return true
Paul Duffin65898052021-04-20 22:47:03 +01002100 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
Paul Duffin94f19632021-04-20 12:40:07 +01002101 // Add the contents of the bootclasspath fragment to the apex.
Paul Duffin4d101b62021-03-24 15:42:20 +00002102 switch child.(type) {
2103 case *java.Library, *java.SdkLibrary:
Paul Duffincc33ec82021-04-25 23:14:55 +01002104 javaModule := child.(javaModule)
Paul Duffin190fdef2021-04-26 10:33:59 +01002105 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
Paul Duffin4d101b62021-03-24 15:42:20 +00002106 if !af.ok() {
Paul Duffin94f19632021-04-20 12:40:07 +01002107 ctx.PropertyErrorf("bootclasspath_fragments", "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
Paul Duffin4d101b62021-03-24 15:42:20 +00002108 return false
2109 }
2110 filesInfo = append(filesInfo, af)
2111 return true // track transitive dependencies
2112 default:
Paul Duffin94f19632021-04-20 12:40:07 +01002113 ctx.PropertyErrorf("bootclasspath_fragments", "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
Paul Duffin4d101b62021-03-24 15:42:20 +00002114 }
satayev333a1732021-05-17 21:35:26 +01002115 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2116 // Add the contents of the systemserverclasspath fragment to the apex.
2117 switch child.(type) {
2118 case *java.Library, *java.SdkLibrary:
2119 af := apexFileForJavaModule(ctx, child.(javaModule))
2120 filesInfo = append(filesInfo, af)
2121 return true // track transitive dependencies
2122 default:
2123 ctx.PropertyErrorf("systemserverclasspath_fragments", "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2124 }
Colin Cross56a83212020-09-15 18:30:11 -07002125 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2126 // nothing
Dan Willemsen47450072021-10-19 20:24:49 -07002127 } else if depTag == android.DarwinUniversalVariantTag {
2128 // nothing
Jooyung Han9c80bae2019-08-20 17:30:57 +09002129 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Jiyong Park1c7e9622020-05-07 16:12:13 +09002130 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002131 }
2132 }
2133 }
2134 return false
2135 })
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002136 if a.privateKeyFile == nil {
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07002137 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002138 return
2139 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002140
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002141 // Remove duplicates in filesInfo
Jiyong Park8fd61922018-11-08 02:50:25 +09002142 removeDup := func(filesInfo []apexFile) []apexFile {
Jiyong Park7cd10e32020-01-14 09:22:18 +09002143 encountered := make(map[string]apexFile)
Jiyong Park8fd61922018-11-08 02:50:25 +09002144 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09002145 dest := filepath.Join(f.installDir, f.builtFile.Base())
Jiyong Park7cd10e32020-01-14 09:22:18 +09002146 if e, ok := encountered[dest]; !ok {
2147 encountered[dest] = f
2148 } else {
2149 // If a module is directly included and also transitively depended on
2150 // consider it as directly included.
2151 e.transitiveDep = e.transitiveDep && f.transitiveDep
2152 encountered[dest] = e
Jiyong Park8fd61922018-11-08 02:50:25 +09002153 }
2154 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002155 var result []apexFile
2156 for _, v := range encountered {
2157 result = append(result, v)
2158 }
Jiyong Park8fd61922018-11-08 02:50:25 +09002159 return result
2160 }
2161 filesInfo = removeDup(filesInfo)
2162
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002163 // Sort to have consistent build rules
Jiyong Park8fd61922018-11-08 02:50:25 +09002164 sort.Slice(filesInfo, func(i, j int) bool {
Paul Duffin56060292021-05-15 19:34:05 +01002165 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2166 // changes.
2167 return filesInfo[i].path() < filesInfo[j].path()
Jiyong Park8fd61922018-11-08 02:50:25 +09002168 })
2169
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002170 ////////////////////////////////////////////////////////////////////////////////////////////
2171 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002172 a.installDir = android.PathForModuleInstall(ctx, "apex")
2173 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002174
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002175 // Set suffix and primaryApexType depending on the ApexType
Martin Stjernholmcb3ff1e2021-05-25 00:28:27 +01002176 buildFlattenedAsDefault := ctx.Config().FlattenApex()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002177 switch a.properties.ApexType {
2178 case imageApex:
2179 if buildFlattenedAsDefault {
2180 a.suffix = imageApexSuffix
2181 } else {
2182 a.suffix = ""
2183 a.primaryApexType = true
2184
2185 if ctx.Config().InstallExtraFlattenedApexes() {
2186 a.requiredDeps = append(a.requiredDeps, a.Name()+flattenedSuffix)
2187 }
2188 }
2189 case zipApex:
2190 if proptools.String(a.properties.Payload_type) == "zip" {
2191 a.suffix = ""
2192 a.primaryApexType = true
2193 } else {
2194 a.suffix = zipApexSuffix
2195 }
2196 case flattenedApex:
2197 if buildFlattenedAsDefault {
2198 a.suffix = ""
2199 a.primaryApexType = true
2200 } else {
2201 a.suffix = flattenedSuffix
2202 }
2203 }
2204
Theotime Combes4ba38c12020-06-12 12:46:59 +00002205 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2206 case ext4FsType:
2207 a.payloadFsType = ext4
2208 case f2fsFsType:
2209 a.payloadFsType = f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08002210 case erofsFsType:
2211 a.payloadFsType = erofs
Theotime Combes4ba38c12020-06-12 12:46:59 +00002212 default:
Huang Jianan13cac632021-08-02 15:02:17 +08002213 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
Theotime Combes4ba38c12020-06-12 12:46:59 +00002214 }
2215
Jiyong Park7cd10e32020-01-14 09:22:18 +09002216 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2217 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2218 // the same library in the system partition, thus effectively sharing the same libraries
2219 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2220 // in the APEX.
Steven Moreland2c4000c2021-04-27 02:08:49 +00002221 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
Jooyung Han54aca7b2019-11-20 02:26:02 +09002222
Jooyung Han85d61762020-06-24 23:50:26 +09002223 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2224 // So we can't link them to /system/lib libs which are core variants.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002225 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
Jooyung Han85d61762020-06-24 23:50:26 +09002226 a.linkToSystemLib = false
2227 }
2228
Jiyong Park4da07972021-01-05 21:01:11 +09002229 forced := ctx.Config().ForceApexSymlinkOptimization()
Jiyong Parkf4020582021-11-29 12:37:10 +09002230 updatable := a.Updatable() || a.FutureUpdatable()
Jiyong Park4da07972021-01-05 21:01:11 +09002231
Jiyong Park9d677202020-02-19 16:29:35 +09002232 // We don't need the optimization for updatable APEXes, as it might give false signal
Jiyong Park4da07972021-01-05 21:01:11 +09002233 // to the system health when the APEXes are still bundled (b/149805758).
Jiyong Parkf4020582021-11-29 12:37:10 +09002234 if !forced && updatable && a.properties.ApexType == imageApex {
Jiyong Park9d677202020-02-19 16:29:35 +09002235 a.linkToSystemLib = false
2236 }
2237
Jiyong Park638d30e2020-02-26 18:27:19 +09002238 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2239 if ctx.Host() {
2240 a.linkToSystemLib = false
2241 }
2242
Colin Cross6340ea52021-11-04 12:01:18 -07002243 if a.properties.ApexType != zipApex {
2244 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2245 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002246
2247 ////////////////////////////////////////////////////////////////////////////////////////////
2248 // 4) generate the build rules to create the APEX. This is done in builder.go.
2249 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002250 if a.properties.ApexType == flattenedApex {
2251 a.buildFlattenedApex(ctx)
2252 } else {
2253 a.buildUnflattenedApex(ctx)
2254 }
Jiyong Park956305c2020-01-09 12:32:06 +09002255 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002256 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002257
2258 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2259 if a.installable() {
2260 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2261 // along with other ordinary files. (Note that this is done by apexer for
2262 // non-flattened APEXes)
2263 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2264
2265 // Place the public key as apex_pubkey. This is also done by apexer for
2266 // non-flattened APEXes case.
2267 // TODO(jiyong): Why do we need this CP rule?
2268 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2269 ctx.Build(pctx, android.BuildParams{
2270 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002271 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002272 Output: copiedPubkey,
2273 })
2274 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2275 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002276}
2277
Paul Duffincc33ec82021-04-25 23:14:55 +01002278// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2279// the bootclasspath_fragment contributes to the apex.
2280func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2281 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2282 var filesToAdd []apexFile
2283
2284 // Add the boot image files, e.g. .art, .oat and .vdex files.
Jiakai Zhang6decef92022-01-12 17:56:19 +00002285 if bootclasspathFragmentInfo.ShouldInstallBootImageInApex() {
2286 for arch, files := range bootclasspathFragmentInfo.AndroidBootImageFilesByArchType() {
2287 dirInApex := filepath.Join("javalib", arch.String())
2288 for _, f := range files {
2289 androidMkModuleName := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
2290 // TODO(b/177892522) - consider passing in the bootclasspath fragment module here instead of nil
2291 af := newApexFile(ctx, f, androidMkModuleName, dirInApex, etc, nil)
2292 filesToAdd = append(filesToAdd, af)
2293 }
Paul Duffincc33ec82021-04-25 23:14:55 +01002294 }
2295 }
2296
satayev3db35472021-05-06 23:59:58 +01002297 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002298 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2299 filesToAdd = append(filesToAdd, *af)
2300 }
satayev3db35472021-05-06 23:59:58 +01002301
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002302 if pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex(); pathInApex != "" {
2303 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2304 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2305
2306 if pathOnHost != nil {
2307 // We need to copy the profile to a temporary path with the right filename because the apexer
2308 // will take the filename as is.
2309 ctx.Build(pctx, android.BuildParams{
2310 Rule: android.Cp,
2311 Input: pathOnHost,
2312 Output: tempPath,
2313 })
2314 } else {
2315 // At this point, the boot image profile cannot be generated. It is probably because the boot
2316 // image profile source file does not exist on the branch, or it is not available for the
2317 // current build target.
2318 // However, we cannot enforce the boot image profile to be generated because some build
2319 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2320 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2321 // only if the APEX is being built.
2322 ctx.Build(pctx, android.BuildParams{
2323 Rule: android.ErrorRule,
2324 Output: tempPath,
2325 Args: map[string]string{
2326 "error": "Boot image profile cannot be generated",
2327 },
2328 })
2329 }
2330
2331 androidMkModuleName := filepath.Base(pathInApex)
2332 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2333 filesToAdd = append(filesToAdd, af)
2334 }
2335
Paul Duffincc33ec82021-04-25 23:14:55 +01002336 return filesToAdd
2337}
2338
satayevb98371c2021-06-15 16:49:50 +01002339// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2340// the module contributes to the apex; or nil if the proto config was not generated.
2341func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2342 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2343 if !info.ClasspathFragmentProtoGenerated {
2344 return nil
2345 }
2346 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2347 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2348 return &af
satayev14e49132021-05-17 21:03:07 +01002349}
2350
Paul Duffincc33ec82021-04-25 23:14:55 +01002351// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2352// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002353func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2354 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2355
2356 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2357 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002358 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2359 if err != nil {
2360 ctx.ModuleErrorf("%s", err)
2361 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002362
2363 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2364 // bootclasspath_fragment.
2365 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2366 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002367}
2368
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002369///////////////////////////////////////////////////////////////////////////////////////////////////
2370// Factory functions
2371//
2372
2373func newApexBundle() *apexBundle {
2374 module := &apexBundle{}
2375
2376 module.AddProperties(&module.properties)
2377 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002378 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002379 module.AddProperties(&module.overridableProperties)
2380
2381 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2382 android.InitDefaultableModule(module)
2383 android.InitSdkAwareModule(module)
2384 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002385 android.InitBazelModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002386 return module
2387}
2388
Paul Duffineb8051d2021-10-18 17:49:39 +01002389func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002390 bundle := newApexBundle()
2391 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002392 return bundle
2393}
2394
2395// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2396// certain compatibility checks such as apex_available are not done for apex_test.
2397func testApexBundleFactory() android.Module {
2398 bundle := newApexBundle()
2399 bundle.testApex = true
2400 return bundle
2401}
2402
2403// apex packages other modules into an APEX file which is a packaging format for system-level
2404// components like binaries, shared libraries, etc.
2405func BundleFactory() android.Module {
2406 return newApexBundle()
2407}
2408
2409type Defaults struct {
2410 android.ModuleBase
2411 android.DefaultsModuleBase
2412}
2413
2414// apex_defaults provides defaultable properties to other apex modules.
2415func defaultsFactory() android.Module {
2416 return DefaultsFactory()
2417}
2418
2419func DefaultsFactory(props ...interface{}) android.Module {
2420 module := &Defaults{}
2421
2422 module.AddProperties(props...)
2423 module.AddProperties(
2424 &apexBundleProperties{},
2425 &apexTargetBundleProperties{},
2426 &overridableProperties{},
2427 )
2428
2429 android.InitDefaultsModule(module)
2430 return module
2431}
2432
2433type OverrideApex struct {
2434 android.ModuleBase
2435 android.OverrideModuleBase
2436}
2437
2438func (o *OverrideApex) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2439 // All the overrides happen in the base module.
2440}
2441
2442// override_apex is used to create an apex module based on another apex module by overriding some of
2443// its properties.
2444func overrideApexFactory() android.Module {
2445 m := &OverrideApex{}
2446
2447 m.AddProperties(&overridableProperties{})
2448
2449 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2450 android.InitOverrideModule(m)
2451 return m
2452}
2453
2454///////////////////////////////////////////////////////////////////////////////////////////////////
2455// Vality check routines
2456//
2457// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2458// certain conditions are not met.
2459//
2460// TODO(jiyong): move these checks to a separate go file.
2461
satayevad991492021-12-03 18:58:32 +00002462var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2463
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002464// Entures that min_sdk_version of the included modules are equal or less than the min_sdk_version
2465// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002466func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002467 if a.testApex || a.vndkApex {
2468 return
2469 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002470 // apexBundle::minSdkVersion reports its own errors.
2471 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002472 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002473}
2474
Albert Martin55ccba22022-03-21 20:11:16 +00002475// Returns apex's min_sdk_version string value, honoring overrides
2476func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2477 // Only override the minSdkVersion value on Apexes which already specify
2478 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2479 // min_sdk_version value is lower than the one to override with.
2480 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2481 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2482 originalMinApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.properties.Min_sdk_version))
2483 isMinSdkSet := a.properties.Min_sdk_version != nil
2484 isOverrideValueHigher := overrideApiLevel.CompareTo(originalMinApiLevel) > 0
2485 if overrideMinSdkValue != "" && isMinSdkSet && isOverrideValueHigher {
2486 return overrideMinSdkValue
2487 }
2488
2489 return proptools.String(a.properties.Min_sdk_version)
2490}
2491
2492// Returns apex's min_sdk_version SdkSpec, honoring overrides
satayevad991492021-12-03 18:58:32 +00002493func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2494 return android.SdkSpec{
2495 Kind: android.SdkNone,
2496 ApiLevel: a.minSdkVersion(ctx),
Albert Martin55ccba22022-03-21 20:11:16 +00002497 Raw: a.minSdkVersionValue(ctx),
satayevad991492021-12-03 18:58:32 +00002498 }
2499}
2500
Albert Martin55ccba22022-03-21 20:11:16 +00002501// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00002502func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martin55ccba22022-03-21 20:11:16 +00002503 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
2504}
2505
2506// Construct ApiLevel object from min_sdk_version string value
2507func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
2508 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09002509 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002510 }
Albert Martin55ccba22022-03-21 20:11:16 +00002511 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002512 if err != nil {
2513 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
2514 return android.NoneApiLevel
2515 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002516 return apiLevel
2517}
2518
2519// Ensures that a lib providing stub isn't statically linked
2520func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
2521 // Practically, we only care about regular APEXes on the device.
2522 if ctx.Host() || a.testApex || a.vndkApex {
2523 return
2524 }
2525
2526 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2527
2528 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
2529 if ccm, ok := to.(*cc.Module); ok {
2530 apexName := ctx.ModuleName()
2531 fromName := ctx.OtherModuleName(from)
2532 toName := ctx.OtherModuleName(to)
2533
2534 // If `to` is not actually in the same APEX as `from` then it does not need
2535 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002536 //
2537 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002538 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2539 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2540 return false
2541 }
2542
2543 // The dynamic linker and crash_dump tool in the runtime APEX is the only
2544 // exception to this rule. It can't make the static dependencies dynamic
2545 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09002546 // Same rule should be applied to linkerconfig, because it should be executed
2547 // only with static linked libraries before linker is available with ld.config.txt
2548 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002549 return false
2550 }
2551
2552 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
2553 if isStubLibraryFromOtherApex && !externalDep {
2554 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
2555 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
2556 }
2557
2558 }
2559 return true
2560 })
2561}
2562
satayevb98371c2021-06-15 16:49:50 +01002563// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002564func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
2565 if a.Updatable() {
Albert Martin55ccba22022-03-21 20:11:16 +00002566 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002567 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
2568 }
Jiyong Park1bc84122021-06-22 20:23:05 +09002569 if a.UsePlatformApis() {
2570 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
2571 }
Daniel Norman69109112021-12-02 12:52:42 -08002572 if a.SocSpecific() || a.DeviceSpecific() {
2573 ctx.PropertyErrorf("updatable", "vendor APEXes are not updatable")
2574 }
Jiyong Parkf4020582021-11-29 12:37:10 +09002575 if a.FutureUpdatable() {
2576 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
2577 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002578 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01002579 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002580 }
2581}
2582
satayevb98371c2021-06-15 16:49:50 +01002583// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
2584func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
2585 ctx.VisitDirectDeps(func(module android.Module) {
2586 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
2587 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2588 if !info.ClasspathFragmentProtoGenerated {
2589 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
2590 }
2591 }
2592 })
2593}
2594
2595// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01002596func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002597 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
2598 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01002599 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
2600 tag := ctx.OtherModuleDependencyTag(module)
2601 switch tag {
2602 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09002603 if m, ok := module.(interface {
2604 CheckStableSdkVersion(ctx android.BaseModuleContext) error
2605 }); ok {
2606 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01002607 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
2608 }
2609 }
2610 }
2611 })
2612}
2613
satayevb98371c2021-06-15 16:49:50 +01002614// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002615func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
2616 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
2617 if ctx.Host() || a.testApex || a.vndkApex {
2618 return
2619 }
2620
2621 // Because APEXes targeting other than system/system_ext partitions can't set
2622 // apex_available, we skip checks for these APEXes
2623 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2624 return
2625 }
2626
2627 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
2628 // Requiring them and their transitive depencies with apex_available is not right
2629 // because they just add noise.
2630 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
2631 return
2632 }
2633
2634 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
2635 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2636 if externalDep {
2637 return false
2638 }
2639
2640 apexName := ctx.ModuleName()
2641 fromName := ctx.OtherModuleName(from)
2642 toName := ctx.OtherModuleName(to)
2643
2644 // If `to` is not actually in the same APEX as `from` then it does not need
2645 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002646 //
2647 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002648 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2649 // As soon as the dependency graph crosses the APEX boundary, don't go
2650 // further.
2651 return false
2652 }
2653
2654 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
2655 return true
2656 }
Jiyong Park767dbd92021-03-04 13:03:10 +09002657 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
2658 "\n\nDependency path:%s\n\n"+
2659 "Consider adding %q to 'apex_available' property of %q",
2660 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002661 // Visit this module's dependencies to check and report any issues with their availability.
2662 return true
2663 })
2664}
2665
Jiyong Park192600a2021-08-03 07:52:17 +00002666// checkStaticExecutable ensures that executables in an APEX are not static.
2667func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09002668 // No need to run this for host APEXes
2669 if ctx.Host() {
2670 return
2671 }
2672
Jiyong Park192600a2021-08-03 07:52:17 +00002673 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
2674 if ctx.OtherModuleDependencyTag(module) != executableTag {
2675 return
2676 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09002677
2678 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00002679 apex := a.ApexVariationName()
2680 exec := ctx.OtherModuleName(module)
2681 if isStaticExecutableAllowed(apex, exec) {
2682 return
2683 }
2684 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
2685 }
2686 })
2687}
2688
2689// A small list of exceptions where static executables are allowed in APEXes.
2690func isStaticExecutableAllowed(apex string, exec string) bool {
2691 m := map[string][]string{
2692 "com.android.runtime": []string{
2693 "linker",
2694 "linkerconfig",
2695 },
2696 }
2697 execNames, ok := m[apex]
2698 return ok && android.InList(exec, execNames)
2699}
2700
braleeb0c1f0c2021-06-07 22:49:13 +08002701// Collect information for opening IDE project files in java/jdeps.go.
2702func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09002703 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Java_libs...)
2704 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Bootclasspath_fragments...)
2705 dpInfo.Deps = append(dpInfo.Deps, a.overridableProperties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08002706 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
2707}
2708
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002709var (
2710 apexAvailBaseline = makeApexAvailableBaseline()
2711 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
2712)
2713
Colin Cross440e0d02020-06-11 11:32:11 -07002714func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002715 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00002716 moduleName = normalizeModuleName(moduleName)
2717
Colin Cross440e0d02020-06-11 11:32:11 -07002718 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00002719 return true
2720 }
2721
2722 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07002723 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00002724 return true
2725 }
2726
2727 return false
2728}
2729
2730func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09002731 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
2732 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00002733 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09002734 if strings.HasPrefix(moduleName, "libclang_rt.") {
2735 // This module has many arch variants that depend on the product being built.
2736 // We don't want to list them all
2737 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002738 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09002739 if strings.HasPrefix(moduleName, "androidx.") {
2740 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
2741 moduleName = "androidx"
2742 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00002743 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002744}
2745
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002746// Transform the map of apex -> modules to module -> apexes.
2747func invertApexBaseline(m map[string][]string) map[string][]string {
2748 r := make(map[string][]string)
2749 for apex, modules := range m {
2750 for _, module := range modules {
2751 r[module] = append(r[module], apex)
2752 }
2753 }
2754 return r
2755}
2756
2757// Retrieve the baseline of apexes to which the supplied module belongs.
2758func BaselineApexAvailable(moduleName string) []string {
2759 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
2760}
2761
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002762// This is a map from apex to modules, which overrides the apex_available setting for that
2763// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002764// TODO(b/147364041): remove this
2765func makeApexAvailableBaseline() map[string][]string {
2766 // The "Module separator"s below are employed to minimize merge conflicts.
2767 m := make(map[string][]string)
2768 //
2769 // Module separator
2770 //
2771 m["com.android.appsearch"] = []string{
2772 "icing-java-proto-lite",
2773 "libprotobuf-java-lite",
2774 }
2775 //
2776 // Module separator
2777 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00002778 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07002779 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00002780 }
2781 //
2782 // Module separator
2783 //
Etienne Ruffieux16512672021-12-15 15:49:04 +00002784 m["com.android.bluetooth"] = []string{
William Escandecfbdc3b2022-06-03 19:10:26 -07002785 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002786 }
2787 //
2788 // Module separator
2789 //
2790 m["com.android.cellbroadcast"] = []string{"CellBroadcastApp", "CellBroadcastServiceModule"}
2791 //
2792 // Module separator
2793 //
2794 m["com.android.extservices"] = []string{
2795 "error_prone_annotations",
2796 "ExtServices-core",
2797 "ExtServices",
2798 "libtextclassifier-java",
2799 "libz_current",
2800 "textclassifier-statsd",
2801 "TextClassifierNotificationLibNoManifest",
2802 "TextClassifierServiceLibNoManifest",
2803 }
2804 //
2805 // Module separator
2806 //
2807 m["com.android.neuralnetworks"] = []string{
2808 "android.hardware.neuralnetworks@1.0",
2809 "android.hardware.neuralnetworks@1.1",
2810 "android.hardware.neuralnetworks@1.2",
2811 "android.hardware.neuralnetworks@1.3",
2812 "android.hidl.allocator@1.0",
2813 "android.hidl.memory.token@1.0",
2814 "android.hidl.memory@1.0",
2815 "android.hidl.safe_union@1.0",
2816 "libarect",
2817 "libbuildversion",
2818 "libmath",
2819 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002820 }
2821 //
2822 // Module separator
2823 //
2824 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08002825 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002826 }
2827 //
2828 // Module separator
2829 //
2830 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08002831 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002832 }
2833 //
2834 // Module separator
2835 //
2836 m["com.android.mediaprovider"] = []string{
2837 "MediaProvider",
2838 "MediaProviderGoogle",
2839 "fmtlib_ndk",
2840 "libbase_ndk",
2841 "libfuse",
2842 "libfuse_jni",
2843 }
2844 //
2845 // Module separator
2846 //
2847 m["com.android.permission"] = []string{
2848 "car-ui-lib",
2849 "iconloader",
2850 "kotlin-annotations",
2851 "kotlin-stdlib",
2852 "kotlin-stdlib-jdk7",
2853 "kotlin-stdlib-jdk8",
2854 "kotlinx-coroutines-android",
2855 "kotlinx-coroutines-android-nodeps",
2856 "kotlinx-coroutines-core",
2857 "kotlinx-coroutines-core-nodeps",
2858 "permissioncontroller-statsd",
2859 "GooglePermissionController",
2860 "PermissionController",
2861 "SettingsLibActionBarShadow",
2862 "SettingsLibAppPreference",
2863 "SettingsLibBarChartPreference",
2864 "SettingsLibLayoutPreference",
2865 "SettingsLibProgressBar",
2866 "SettingsLibSearchWidget",
2867 "SettingsLibSettingsTheme",
2868 "SettingsLibRestrictedLockUtils",
2869 "SettingsLibHelpUtils",
2870 }
2871 //
2872 // Module separator
2873 //
2874 m["com.android.runtime"] = []string{
2875 "bionic_libc_platform_headers",
2876 "libarm-optimized-routines-math",
2877 "libc_aeabi",
2878 "libc_bionic",
2879 "libc_bionic_ndk",
2880 "libc_bootstrap",
2881 "libc_common",
2882 "libc_common_shared",
2883 "libc_common_static",
2884 "libc_dns",
2885 "libc_dynamic_dispatch",
2886 "libc_fortify",
2887 "libc_freebsd",
2888 "libc_freebsd_large_stack",
2889 "libc_gdtoa",
2890 "libc_init_dynamic",
2891 "libc_init_static",
2892 "libc_jemalloc_wrapper",
2893 "libc_netbsd",
2894 "libc_nomalloc",
2895 "libc_nopthread",
2896 "libc_openbsd",
2897 "libc_openbsd_large_stack",
2898 "libc_openbsd_ndk",
2899 "libc_pthread",
2900 "libc_static_dispatch",
2901 "libc_syscalls",
2902 "libc_tzcode",
2903 "libc_unwind_static",
2904 "libdebuggerd",
2905 "libdebuggerd_common_headers",
2906 "libdebuggerd_handler_core",
2907 "libdebuggerd_handler_fallback",
2908 "libdl_static",
2909 "libjemalloc5",
2910 "liblinker_main",
2911 "liblinker_malloc",
2912 "liblz4",
2913 "liblzma",
2914 "libprocinfo",
2915 "libpropertyinfoparser",
2916 "libscudo",
2917 "libstdc++",
2918 "libsystemproperties",
2919 "libtombstoned_client_static",
2920 "libunwindstack",
2921 "libz",
2922 "libziparchive",
2923 }
2924 //
2925 // Module separator
2926 //
2927 m["com.android.tethering"] = []string{
2928 "android.hardware.tetheroffload.config-V1.0-java",
2929 "android.hardware.tetheroffload.control-V1.0-java",
2930 "android.hidl.base-V1.0-java",
2931 "libcgrouprc",
2932 "libcgrouprc_format",
2933 "libtetherutilsjni",
2934 "libvndksupport",
2935 "net-utils-framework-common",
2936 "netd_aidl_interface-V3-java",
2937 "netlink-client",
2938 "networkstack-aidl-interfaces-java",
2939 "tethering-aidl-interfaces-java",
2940 "TetheringApiCurrentLib",
2941 }
2942 //
2943 // Module separator
2944 //
2945 m["com.android.wifi"] = []string{
2946 "PlatformProperties",
2947 "android.hardware.wifi-V1.0-java",
2948 "android.hardware.wifi-V1.0-java-constants",
2949 "android.hardware.wifi-V1.1-java",
2950 "android.hardware.wifi-V1.2-java",
2951 "android.hardware.wifi-V1.3-java",
2952 "android.hardware.wifi-V1.4-java",
2953 "android.hardware.wifi.hostapd-V1.0-java",
2954 "android.hardware.wifi.hostapd-V1.1-java",
2955 "android.hardware.wifi.hostapd-V1.2-java",
2956 "android.hardware.wifi.supplicant-V1.0-java",
2957 "android.hardware.wifi.supplicant-V1.1-java",
2958 "android.hardware.wifi.supplicant-V1.2-java",
2959 "android.hardware.wifi.supplicant-V1.3-java",
2960 "android.hidl.base-V1.0-java",
2961 "android.hidl.manager-V1.0-java",
2962 "android.hidl.manager-V1.1-java",
2963 "android.hidl.manager-V1.2-java",
2964 "bouncycastle-unbundled",
2965 "dnsresolver_aidl_interface-V2-java",
2966 "error_prone_annotations",
2967 "framework-wifi-pre-jarjar",
2968 "framework-wifi-util-lib",
2969 "ipmemorystore-aidl-interfaces-V3-java",
2970 "ipmemorystore-aidl-interfaces-java",
2971 "ksoap2",
2972 "libnanohttpd",
2973 "libwifi-jni",
2974 "net-utils-services-common",
2975 "netd_aidl_interface-V2-java",
2976 "netd_aidl_interface-unstable-java",
2977 "netd_event_listener_interface-java",
2978 "netlink-client",
2979 "networkstack-client",
2980 "services.net",
2981 "wifi-lite-protos",
2982 "wifi-nano-protos",
2983 "wifi-service-pre-jarjar",
2984 "wifi-service-resources",
2985 }
2986 //
2987 // Module separator
2988 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09002989 m["com.android.os.statsd"] = []string{
2990 "libstatssocket",
2991 }
2992 //
2993 // Module separator
2994 //
2995 m[android.AvailableToAnyApex] = []string{
2996 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx/extras support libraries
2997 "androidx",
2998 "androidx-constraintlayout_constraintlayout",
2999 "androidx-constraintlayout_constraintlayout-nodeps",
3000 "androidx-constraintlayout_constraintlayout-solver",
3001 "androidx-constraintlayout_constraintlayout-solver-nodeps",
3002 "com.google.android.material_material",
3003 "com.google.android.material_material-nodeps",
3004
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003005 "libclang_rt",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003006 "libprofile-clang-extras",
3007 "libprofile-clang-extras_ndk",
3008 "libprofile-extras",
3009 "libprofile-extras_ndk",
Ryan Prichardb35a85e2021-01-13 19:18:53 -08003010 "libunwind",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003011 }
3012 return m
3013}
3014
3015func init() {
Spandan Das440ff962021-11-12 00:01:37 +00003016 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3017 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003018}
3019
Spandan Das440ff962021-11-12 00:01:37 +00003020func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3021 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3022 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003023 permittedPackagesRule := android.NeverAllow().
Spandan Das440ff962021-11-12 00:01:37 +00003024 With("name", jar).
3025 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3026 Because(jar +
3027 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssonddf8c1b2021-12-23 15:05:38 +00003028 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003029 " 1. If the offending code is from a statically linked library, consider " +
3030 "removing that dependency and using an alternative already in the " +
3031 "bootclasspath, or perhaps a shared library." +
3032 " 2. Move the offending code into an allowed package.\n" +
3033 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3034 "health implications of bundling that code, particularly if the offending jar " +
3035 "is part of the bootclasspath.")
Spandan Das440ff962021-11-12 00:01:37 +00003036
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003037 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003038 }
3039 return rules
3040}
3041
Anton Hanssonddf8c1b2021-12-23 15:05:38 +00003042// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003043// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Das440ff962021-11-12 00:01:37 +00003044func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003045 return map[string][]string{
Spandan Das440ff962021-11-12 00:01:37 +00003046 "conscrypt": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003047 "android.net.ssl",
3048 "com.android.org.conscrypt",
3049 },
Spandan Das440ff962021-11-12 00:01:37 +00003050 "updatable-media": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003051 "android.media",
3052 },
3053 }
3054}
3055
Anton Hanssonddf8c1b2021-12-23 15:05:38 +00003056// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003057// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Das440ff962021-11-12 00:01:37 +00003058func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003059 return map[string][]string{
Spandan Das440ff962021-11-12 00:01:37 +00003060 "framework-mediaprovider": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003061 "android.provider",
3062 },
Spandan Das440ff962021-11-12 00:01:37 +00003063 "framework-permission": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003064 "android.permission",
3065 "android.app.role",
3066 "com.android.permission",
3067 "com.android.role",
3068 },
Spandan Das440ff962021-11-12 00:01:37 +00003069 "framework-sdkextensions": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003070 "android.os.ext",
3071 },
Spandan Das440ff962021-11-12 00:01:37 +00003072 "framework-statsd": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003073 "android.app",
3074 "android.os",
3075 "android.util",
3076 "com.android.internal.statsd",
3077 "com.android.server.stats",
3078 },
Spandan Das440ff962021-11-12 00:01:37 +00003079 "framework-wifi": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003080 "com.android.server.wifi",
3081 "com.android.wifi.x",
3082 "android.hardware.wifi",
3083 "android.net.wifi",
3084 },
Spandan Das440ff962021-11-12 00:01:37 +00003085 "framework-tethering": []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003086 "android.net",
3087 },
3088 }
3089}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003090
3091// For Bazel / bp2build
3092
3093type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003094 Manifest bazel.LabelAttribute
3095 Android_manifest bazel.LabelAttribute
3096 File_contexts bazel.LabelAttribute
3097 Key bazel.LabelAttribute
3098 Certificate bazel.LabelAttribute
3099 Min_sdk_version *string
3100 Updatable bazel.BoolAttribute
3101 Installable bazel.BoolAttribute
3102 Binaries bazel.LabelListAttribute
3103 Prebuilts bazel.LabelListAttribute
3104 Native_shared_libs_32 bazel.LabelListAttribute
3105 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003106 Compressible bazel.BoolAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003107}
3108
3109type convertedNativeSharedLibs struct {
3110 Native_shared_libs_32 bazel.LabelListAttribute
3111 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003112}
3113
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003114// ConvertWithBp2build performs bp2build conversion of an apex
3115func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
3116 // We do not convert apex_test modules at this time
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003117 if ctx.ModuleType() != "apex" {
3118 return
3119 }
3120
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003121 var manifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003122 if a.properties.Manifest != nil {
3123 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Manifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003124 }
3125
3126 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003127 if a.properties.AndroidManifest != nil {
3128 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003129 }
3130
3131 var fileContextsLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003132 if a.properties.File_contexts != nil {
3133 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003134 }
3135
Albert Martin55ccba22022-03-21 20:11:16 +00003136 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3137 // given it's coming via config, we probably don't want to put it in here.
Liz Kammer46fb7ab2021-12-01 10:09:34 -05003138 var minSdkVersion *string
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003139 if a.properties.Min_sdk_version != nil {
3140 minSdkVersion = a.properties.Min_sdk_version
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003141 }
3142
3143 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003144 if a.overridableProperties.Key != nil {
3145 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003146 }
3147
3148 var certificateLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003149 if a.overridableProperties.Certificate != nil {
3150 certificateLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Certificate))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003151 }
3152
Yu Liu4ae55d12022-01-05 17:17:23 -08003153 nativeSharedLibs := &convertedNativeSharedLibs{
3154 Native_shared_libs_32: bazel.LabelListAttribute{},
3155 Native_shared_libs_64: bazel.LabelListAttribute{},
3156 }
3157 compileMultilib := "both"
3158 if a.CompileMultilib() != nil {
3159 compileMultilib = *a.CompileMultilib()
3160 }
3161
3162 // properties.Native_shared_libs is treated as "both"
3163 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3164 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3165 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3166 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3167 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003168
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003169 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003170 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3171 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3172
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003173 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003174 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003175
3176 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003177 if a.properties.Updatable != nil {
3178 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003179 }
3180
3181 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003182 if a.properties.Installable != nil {
3183 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003184 }
3185
Wei Lif034cb42022-01-19 15:54:31 -08003186 var compressibleAttribute bazel.BoolAttribute
3187 if a.overridableProperties.Compressible != nil {
3188 compressibleAttribute.Value = a.overridableProperties.Compressible
3189 }
3190
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003191 attrs := &bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003192 Manifest: manifestLabelAttribute,
3193 Android_manifest: androidManifestLabelAttribute,
3194 File_contexts: fileContextsLabelAttribute,
3195 Min_sdk_version: minSdkVersion,
3196 Key: keyLabelAttribute,
3197 Certificate: certificateLabelAttribute,
3198 Updatable: updatableAttribute,
3199 Installable: installableAttribute,
3200 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3201 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3202 Binaries: binariesLabelListAttribute,
3203 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003204 Compressible: compressibleAttribute,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003205 }
3206
3207 props := bazel.BazelTargetModuleProperties{
3208 Rule_class: "apex",
3209 Bzl_load_location: "//build/bazel/rules:apex.bzl",
3210 }
3211
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003212 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: a.Name()}, attrs)
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003213}
Yu Liu4ae55d12022-01-05 17:17:23 -08003214
3215// The following conversions are based on this table where the rows are the compile_multilib
3216// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3217// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3218// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3219// should not be compiled.
3220// multib/compile_multilib, 32, 64, both, first
3221// 32, 32/32, none/none, 32/32, none/32
3222// 64, none/none, 64/none, 64/none, 64/none
3223// both, 32/32, 64/none, 32&64/32, 64/32
3224// first, 32/32, 64/none, 64/32, 64/32
3225
3226func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3227 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3228 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3229 switch compileMultilb {
3230 case "both", "32":
3231 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3232 case "first":
3233 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3234 case "64":
3235 // Incompatible, ignore
3236 default:
3237 invalidCompileMultilib(ctx, compileMultilb)
3238 }
3239}
3240
3241func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3242 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3243 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3244 switch compileMultilb {
3245 case "both", "64", "first":
3246 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3247 case "32":
3248 // Incompatible, ignore
3249 default:
3250 invalidCompileMultilib(ctx, compileMultilb)
3251 }
3252}
3253
3254func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3255 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3256 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3257 switch compileMultilb {
3258 case "both":
3259 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3260 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3261 case "first":
3262 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3263 case "32":
3264 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3265 case "64":
3266 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3267 default:
3268 invalidCompileMultilib(ctx, compileMultilb)
3269 }
3270}
3271
3272func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3273 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3274 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3275 switch compileMultilb {
3276 case "both", "first":
3277 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3278 case "32":
3279 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3280 case "64":
3281 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3282 default:
3283 invalidCompileMultilib(ctx, compileMultilb)
3284 }
3285}
3286
3287func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3288 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3289 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3290}
3291
3292func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3293 list := bazel.LabelListAttribute{}
3294 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3295 nativeSharedLibs.Native_shared_libs_32.Append(list)
3296}
3297
3298func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3299 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3300 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3301}
3302
3303func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3304 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3305 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3306}
3307
3308func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3309 labelListAttr *bazel.LabelListAttribute) {
3310 list := bazel.LabelListAttribute{}
3311 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3312 labelListAttr.Append(list)
3313}
3314
3315func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3316 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3317}