blob: 69eea031f94e7653b07e8049c38a8fb9f2ecfc0d [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090015// package apex implements build rules for creating the APEX files which are container for
16// lower-level system components. See https://source.android.com/devices/tech/ota/apex
Jiyong Park48ca7dc2018-10-10 14:01:00 +090017package apex
18
19import (
20 "fmt"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090021 "path/filepath"
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +000022 "regexp"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090023 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024 "strings"
25
Yu Liu4c212ce2022-10-14 12:20:20 -070026 "android/soong/bazel/cquery"
27
Jiyong Park48ca7dc2018-10-10 14:01:00 +090028 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080029 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030 "github.com/google/blueprint/proptools"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070031
32 "android/soong/android"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040033 "android/soong/bazel"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 "android/soong/cc"
36 prebuilt_etc "android/soong/etc"
Jiyong Park12a719c2021-01-07 15:31:24 +090037 "android/soong/filesystem"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/java"
Inseob Kim5eb7ee92022-04-27 10:30:34 +090039 "android/soong/multitree"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040 "android/soong/python"
Jiyong Park99644e92020-11-17 22:21:02 +090041 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042 "android/soong/sh"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090043)
44
Jiyong Park8e6d52f2020-11-19 14:37:47 +090045func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000046 registerApexBuildComponents(android.InitRegistrationContext)
47}
Jiyong Park8e6d52f2020-11-19 14:37:47 +090048
Paul Duffin667893c2021-03-09 22:34:13 +000049func registerApexBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("apex", BundleFactory)
Yu Liu4c212ce2022-10-14 12:20:20 -070051 ctx.RegisterModuleType("apex_test", TestApexBundleFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000052 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Cole Faust912bc882023-03-08 12:29:50 -080053 ctx.RegisterModuleType("apex_defaults", DefaultsFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000054 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Wei Li1c66fc72022-05-09 23:59:14 -070055 ctx.RegisterModuleType("override_apex", OverrideApexFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000056 ctx.RegisterModuleType("apex_set", apexSetFactory)
57
Paul Duffin5dda3e32021-05-05 14:13:27 +010058 ctx.PreArchMutators(registerPreArchMutators)
Paul Duffin667893c2021-03-09 22:34:13 +000059 ctx.PreDepsMutators(RegisterPreDepsMutators)
60 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090061}
62
Paul Duffin5dda3e32021-05-05 14:13:27 +010063func registerPreArchMutators(ctx android.RegisterMutatorsContext) {
64 ctx.TopDown("prebuilt_apex_module_creator", prebuiltApexModuleCreatorMutator).Parallel()
65}
66
Jiyong Park8e6d52f2020-11-19 14:37:47 +090067func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
68 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
69 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
70}
71
72func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Paul Duffin949abc02020-12-08 10:34:30 +000073 ctx.TopDown("apex_info", apexInfoMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090074 ctx.BottomUp("apex_unique", apexUniqueVariationsMutator).Parallel()
75 ctx.BottomUp("apex_test_for_deps", apexTestForDepsMutator).Parallel()
76 ctx.BottomUp("apex_test_for", apexTestForMutator).Parallel()
Paul Duffin28bf7ee2021-05-12 16:41:35 +010077 // Run mark_platform_availability before the apexMutator as the apexMutator needs to know whether
78 // it should create a platform variant.
79 ctx.BottomUp("mark_platform_availability", markPlatformAvailability).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090080 ctx.BottomUp("apex", apexMutator).Parallel()
81 ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).Parallel()
82 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Dennis Shene2ed70c2023-01-11 14:15:43 +000083 ctx.BottomUp("apex_dcla_deps", apexDCLADepsMutator).Parallel()
Spandan Das66773252022-01-15 00:23:18 +000084 // Register after apex_info mutator so that it can use ApexVariationName
85 ctx.TopDown("apex_strict_updatability_lint", apexStrictUpdatibilityLintMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090086}
87
88type apexBundleProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090089 // Json manifest file describing meta info of this APEX bundle. Refer to
90 // system/apex/proto/apex_manifest.proto for the schema. Default: "apex_manifest.json"
Jiyong Park8e6d52f2020-11-19 14:37:47 +090091 Manifest *string `android:"path"`
92
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090093 // AndroidManifest.xml file used for the zip container of this APEX bundle. If unspecified,
94 // a default one is automatically generated.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090095 AndroidManifest *string `android:"path"`
96
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090097 // Determines the file contexts file for setting the security contexts to files in this APEX
98 // bundle. For platform APEXes, this should points to a file under /system/sepolicy Default:
99 // /system/sepolicy/apex/<module_name>_file_contexts.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900100 File_contexts *string `android:"path"`
101
Jooyung Hanaf730952023-02-28 14:13:38 +0900102 // By default, file_contexts is amended by force-labelling / and /apex_manifest.pb as system_file
103 // to avoid mistakes. When set as true, no force-labelling.
104 Use_file_contexts_as_is *bool
105
Jingwen Chendea7a642023-03-28 11:30:50 +0000106 // Path to the canned fs config file for customizing file's
107 // uid/gid/mod/capabilities. The content of this file is appended to the
108 // default config, so that the custom entries are preferred. The format is
109 // /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where
110 // path_or_glob is a path or glob pattern for a file or set of files,
111 // uid/gid are numerial values of user ID and group ID, mode is octal value
112 // for the file mode, and cap is hexadecimal value for the capability.
Jiyong Park038e8522021-12-13 23:56:35 +0900113 Canned_fs_config *string `android:"path"`
114
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900115 ApexNativeDependencies
116
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900117 Multilib apexMultilibProperties
118
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000119 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
120 Rros []string
121
Anton Hanssone7545852023-02-24 11:06:07 +0000122 // List of bootclasspath fragments that are embedded inside this APEX bundle.
123 Bootclasspath_fragments []string
124
125 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
126 Systemserverclasspath_fragments []string
127
128 // List of java libraries that are embedded inside this APEX bundle.
129 Java_libs []string
130
Sundong Ahn80c04892021-11-23 00:57:19 +0000131 // List of sh binaries that are embedded inside this APEX bundle.
132 Sh_binaries []string
133
Paul Duffin3abc1742021-03-15 19:32:23 +0000134 // List of platform_compat_config files that are embedded inside this APEX bundle.
135 Compat_configs []string
136
Jiyong Park12a719c2021-01-07 15:31:24 +0900137 // List of filesystem images that are embedded inside this APEX bundle.
138 Filesystems []string
139
Liz Kammerbd58e742023-05-11 15:58:13 +0000140 // The minimum SDK version that this APEX must support at minimum. This is usually set to
141 // the SDK version that the APEX was first introduced.
142 Min_sdk_version *string
143
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900144 // Whether this APEX is considered updatable or not. When set to true, this will enforce
145 // additional rules for making sure that the APEX is truly updatable. To be updatable,
146 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000147 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900148 Updatable *bool
149
Jiyong Parkf4020582021-11-29 12:37:10 +0900150 // Marks that this APEX is designed to be updatable in the future, although it's not
151 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
152 // updatable APEXes. Currently, this disables the size optimization, so that the size of
153 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
154 // false.
155 Future_updatable *bool
156
Jiyong Park1bc84122021-06-22 20:23:05 +0900157 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
158 // false`. Default is false.
159 Platform_apis *bool
160
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900161 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
162 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900163 Installable *bool
164
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900165 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
166 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
167 Use_vndk_as_stable *bool
168
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900169 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
170 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
171 // container. When set to zip, contents are stored in a zip container directly. This type is
172 // mostly for host-side debugging. When set to both, the two types are both built. Default
173 // is 'image'.
174 Payload_type *string
175
Huang Jianan13cac632021-08-02 15:02:17 +0800176 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
177 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900178 Payload_fs_type *string
179
180 // For telling the APEX to ignore special handling for system libraries such as bionic.
181 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900182 Ignore_system_library_special_case *bool
183
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100184 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100185 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100186 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900187
188 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
189 // used in tests.
190 Test_only_unsigned_payload *bool
191
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000192 // Whenever apex should be compressed, regardless of product flag used. Should be only
193 // used in tests.
194 Test_only_force_compression *bool
195
Jooyung Han09c11ad2021-10-27 03:45:31 +0900196 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
197 // with the tool to sign payload contents.
198 Custom_sign_tool *string
199
Dennis Shenaf41bc12022-08-03 16:46:43 +0000200 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
201 // in a special way that include the digest of the lib file under /lib(64)?
202 Dynamic_common_lib_apex *bool
203
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100204 // Canonical name of this APEX bundle. Used to determine the path to the
205 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
206 // apex mutator variations. For override_apex modules, this is the name of the
207 // overridden base module.
208 ApexVariationName string `blueprint:"mutated"`
209
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900210 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900211
212 // List of sanitizer names that this APEX is enabled for
213 SanitizerNames []string `blueprint:"mutated"`
214
215 PreventInstall bool `blueprint:"mutated"`
216
217 HideFromMake bool `blueprint:"mutated"`
218
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900219 // Internal package method for this APEX. When payload_type is image, this can be either
220 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
221 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900222 ApexType apexPackaging `blueprint:"mutated"`
Sam Delmericoca816532023-06-02 14:09:50 -0400223
224 // Name that dependencies can specify in their apex_available properties to refer to this module.
225 // If not specified, this defaults to Soong module name.
226 Apex_available_name *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900227}
228
229type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900230 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900231 Native_shared_libs []string
232
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900233 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900234 Jni_libs []string
235
Colin Cross70572ed2022-11-02 13:14:20 -0700236 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900237 Rust_dyn_libs []string
238
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900239 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900240 Binaries []string
241
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900242 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900243 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900244
245 // List of filesystem images that are embedded inside this APEX bundle.
246 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700247
248 // List of native libraries to exclude from this APEX.
249 Exclude_native_shared_libs []string
250
251 // List of JNI libraries to exclude from this APEX.
252 Exclude_jni_libs []string
253
254 // List of rust dyn libraries to exclude from this APEX.
255 Exclude_rust_dyn_libs []string
256
257 // List of native executables to exclude from this APEX.
258 Exclude_binaries []string
259
260 // List of native tests to exclude from this APEX.
261 Exclude_tests []string
262
263 // List of filesystem images to exclude from this APEX bundle.
264 Exclude_filesystems []string
265}
266
267// Merge combines another ApexNativeDependencies into this one
268func (a *ApexNativeDependencies) Merge(b ApexNativeDependencies) {
269 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs...)
270 a.Jni_libs = append(a.Jni_libs, b.Jni_libs...)
271 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
272 a.Binaries = append(a.Binaries, b.Binaries...)
273 a.Tests = append(a.Tests, b.Tests...)
274 a.Filesystems = append(a.Filesystems, b.Filesystems...)
275
276 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
277 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
278 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
279 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
280 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
281 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900282}
283
284type apexMultilibProperties struct {
285 // Native dependencies whose compile_multilib is "first"
286 First ApexNativeDependencies
287
288 // Native dependencies whose compile_multilib is "both"
289 Both ApexNativeDependencies
290
291 // Native dependencies whose compile_multilib is "prefer32"
292 Prefer32 ApexNativeDependencies
293
294 // Native dependencies whose compile_multilib is "32"
295 Lib32 ApexNativeDependencies
296
297 // Native dependencies whose compile_multilib is "64"
298 Lib64 ApexNativeDependencies
299}
300
301type apexTargetBundleProperties struct {
302 Target struct {
303 // Multilib properties only for android.
304 Android struct {
305 Multilib apexMultilibProperties
306 }
307
308 // Multilib properties only for host.
309 Host struct {
310 Multilib apexMultilibProperties
311 }
312
313 // Multilib properties only for host linux_bionic.
314 Linux_bionic struct {
315 Multilib apexMultilibProperties
316 }
317
318 // Multilib properties only for host linux_glibc.
319 Linux_glibc struct {
320 Multilib apexMultilibProperties
321 }
322 }
323}
324
Jiyong Park59140302020-12-14 18:44:04 +0900325type apexArchBundleProperties struct {
326 Arch struct {
327 Arm struct {
328 ApexNativeDependencies
329 }
330 Arm64 struct {
331 ApexNativeDependencies
332 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700333 Riscv64 struct {
334 ApexNativeDependencies
335 }
Jiyong Park59140302020-12-14 18:44:04 +0900336 X86 struct {
337 ApexNativeDependencies
338 }
339 X86_64 struct {
340 ApexNativeDependencies
341 }
342 }
343}
344
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900345// These properties can be used in override_apex to override the corresponding properties in the
346// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900347type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900348 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900349 Apps []string
350
Daniel Norman5a3ce132021-08-26 15:44:43 -0700351 // List of prebuilt files that are embedded inside this APEX bundle.
352 Prebuilts []string
353
markchien7c803b82021-08-26 22:10:06 +0800354 // List of BPF programs inside this APEX bundle.
355 Bpfs []string
356
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900357 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
358 // Soong). This does not completely prevent installation of the overridden binaries, but if
359 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
360 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900361 Overrides []string
362
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900363 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900364 Logging_parent string
365
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900366 // Apex Container package name. Override value for attribute package:name in
367 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900368 Package_name string
369
370 // A txt file containing list of files that are allowed to be included in this APEX.
371 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700372
373 // Name of the apex_key module that provides the private key to sign this APEX bundle.
374 Key *string
375
376 // Specifies the certificate and the private key to sign the zip container of this APEX. If
377 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
378 // as the certificate and the private key, respectively. If this is ":module", then the
379 // certificate and the private key are provided from the android_app_certificate module
380 // named "module".
381 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400382
383 // Whether this APEX can be compressed or not. Setting this property to false means this
384 // APEX will never be compressed. When set to true, APEX will be compressed if other
385 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
386 // Default: false.
387 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000388
389 // Trim against a specific Dynamic Common Lib APEX
390 Trim_against *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900391}
392
393type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900394 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900395 android.ModuleBase
396 android.DefaultableModuleBase
397 android.OverridableModuleBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400398 android.BazelModuleBase
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900399 multitree.ExportableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900400
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900401 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900402 properties apexBundleProperties
403 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900404 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900405 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900406 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900407
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900408 ///////////////////////////////////////////////////////////////////////////////////////////
409 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900410
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +0100411 // Keys for apex_payload.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800412 publicKeyFile android.Path
413 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900414
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900415 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800416 containerCertificateFile android.Path
417 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900418
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900419 // Flags for special variants of APEX
420 testApex bool
421 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900422
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900423 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
424 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900425 primaryApexType bool
426
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900427 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900428 suffix string
429
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900430 // File system type of apex_payload.img
431 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900432
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900433 // Whether to create symlink to the system file instead of having a file inside the apex or
434 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900435 linkToSystemLib bool
436
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900437 // List of files to be included in this APEX. This is filled in the first part of
438 // GenerateAndroidBuildActions.
439 filesInfo []apexFile
440
Jingwen Chen29743c82023-01-25 17:49:46 +0000441 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
442 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900443
444 ///////////////////////////////////////////////////////////////////////////////////////////
445 // Outputs (final and intermediates)
446
447 // Processed apex manifest in JSONson format (for Q)
448 manifestJsonOut android.WritablePath
449
450 // Processed apex manifest in PB format (for R+)
451 manifestPbOut android.WritablePath
452
453 // Processed file_contexts files
454 fileContexts android.WritablePath
455
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900456 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900457 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900458 outputFile android.WritablePath
459
Jooyung Hana6d36672022-02-24 13:58:07 +0900460 // The built uncompressed .apex file.
461 outputApexFile android.WritablePath
462
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900463 // The built APEX file in app bundle format. This file is not directly installed to the
464 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
465 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
466 // system) to be merged into a single app bundle file that Play accepts. See
467 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
468 bundleModuleFile android.WritablePath
469
Colin Cross6340ea52021-11-04 12:01:18 -0700470 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900471 installDir android.InstallPath
472
Colin Cross6340ea52021-11-04 12:01:18 -0700473 // Path where this APEX was installed.
474 installedFile android.InstallPath
475
476 // Installed locations of symlinks for backward compatibility.
477 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900478
479 // Text file having the list of individual files that are included in this APEX. Used for
480 // debugging purpose.
481 installedFilesFile android.WritablePath
482
483 // List of module names that this APEX is including (to be shown via *-deps-info target).
484 // Used for debugging purpose.
485 android.ApexBundleDepsInfo
486
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900487 // Optional list of lint report zip files for apexes that contain java or app modules
488 lintReports android.Paths
489
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000490 isCompressed bool
491
sophiezc80a2b32020-11-12 16:39:19 +0000492 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700493 nativeApisUsedByModuleFile android.ModuleOutPath
494 nativeApisBackedByModuleFile android.ModuleOutPath
495 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800496
497 // Collect the module directory for IDE info in java/jdeps.go.
498 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900499}
500
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900501// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900502type apexFileClass int
503
Jooyung Han72bd2f82019-10-23 16:46:38 +0900504const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900505 app apexFileClass = iota
506 appSet
507 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900508 goBinary
509 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900510 nativeExecutable
511 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900512 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900513 pyBinary
514 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900515)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900516
Jingwen Chen2d37b642023-03-14 16:11:38 +0000517var (
518 classes = map[string]apexFileClass{
519 "app": app,
520 "appSet": appSet,
521 "etc": etc,
522 "goBinary": goBinary,
523 "javaSharedLib": javaSharedLib,
524 "nativeExecutable": nativeExecutable,
525 "nativeSharedLib": nativeSharedLib,
526 "nativeTest": nativeTest,
527 "pyBinary": pyBinary,
528 "shBinary": shBinary,
529 }
530)
531
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900532// apexFile represents a file in an APEX bundle. This is created during the first half of
533// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
534// of the function, this is used to create commands that copies the files into a staging directory,
535// where they are packaged into the APEX file. This struct is also used for creating Make modules
536// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900537type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900538 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000539 builtFile android.Path
540 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900541 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000542 customStem string
543 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900544
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900545 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
546 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
547 // suffix>]
548 androidMkModuleName string // becomes LOCAL_MODULE
549 class apexFileClass // becomes LOCAL_MODULE_CLASS
550 moduleDir string // becomes LOCAL_PATH
551 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
552 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
553 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
554 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900555
556 jacocoReportClassesFile android.Path // only for javalibs and apps
557 lintDepSets java.LintDepSets // only for javalibs and apps
558 certificate java.Certificate // only for apps
559 overriddenPackageName string // only for apps
560
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900561 transitiveDep bool
562 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900563
Jiyong Park57621b22021-01-20 20:33:11 +0900564 multilib string
565
Jingwen Chen2d37b642023-03-14 16:11:38 +0000566 isBazelPrebuilt bool
567 unstrippedBuiltFile android.Path
568 arch string
569
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900570 // TODO(jiyong): remove this
571 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900572}
573
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900574// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900575func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
576 ret := apexFile{
577 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900578 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900579 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900580 class: class,
581 module: module,
582 }
583 if module != nil {
584 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900585 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900586 ret.requiredModuleNames = module.RequiredModuleNames()
587 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
588 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900589 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900590 }
591 return ret
592}
593
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900594func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900595 return af.builtFile != nil && af.builtFile.String() != ""
596}
597
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900598// apexRelativePath returns the relative path of the given path from the install directory of this
599// apexFile.
600// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900601func (af *apexFile) apexRelativePath(path string) string {
602 return filepath.Join(af.installDir, path)
603}
604
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900605// path returns path of this apex file relative to the APEX root
606func (af *apexFile) path() string {
607 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900608}
609
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900610// stem returns the base filename of this apex file
611func (af *apexFile) stem() string {
612 if af.customStem != "" {
613 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900614 }
615 return af.builtFile.Base()
616}
617
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900618// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
619func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900620 var ret []string
621 for _, symlink := range af.symlinks {
622 ret = append(ret, af.apexRelativePath(symlink))
623 }
624 return ret
625}
626
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900627// availableToPlatform tests whether this apexFile is from a module that can be installed to the
628// platform.
629func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900630 if af.module == nil {
631 return false
632 }
633 if am, ok := af.module.(android.ApexModule); ok {
634 return am.AvailableFor(android.AvailableToPlatform)
635 }
636 return false
637}
638
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900639////////////////////////////////////////////////////////////////////////////////////////////////////
640// Mutators
641//
642// Brief description about mutators for APEX. The following three mutators are the most important
643// ones.
644//
645// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
646// to the (direct) dependencies of this APEX bundle.
647//
Paul Duffin949abc02020-12-08 10:34:30 +0000648// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900649// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
650// modules are marked as being included in the APEX via BuildForApex().
651//
Paul Duffin949abc02020-12-08 10:34:30 +0000652// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
653// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900654
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900655type dependencyTag struct {
656 blueprint.BaseDependencyTag
657 name string
658
659 // Determines if the dependent will be part of the APEX payload. Can be false for the
660 // dependencies to the signing key module, etc.
661 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000662
663 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
664 // replacement. This is needed because some prebuilt modules do not provide all the information
665 // needed by the apex.
666 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000667
668 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
669 // also be added as exported members of that SDK.
670 memberType android.SdkMemberType
671}
672
673func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
674 return d.memberType
675}
676
677func (d *dependencyTag) ExportMember() bool {
678 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900679}
680
Paul Duffin520917a2022-05-13 13:01:59 +0000681func (d *dependencyTag) String() string {
682 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
683}
684
685func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000686 return !d.sourceOnly
687}
688
689var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000690var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000691
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900692var (
Paul Duffin520917a2022-05-13 13:01:59 +0000693 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
694 bpfTag = &dependencyTag{name: "bpf", payload: true}
695 certificateTag = &dependencyTag{name: "certificate"}
Dennis Shene2ed70c2023-01-11 14:15:43 +0000696 dclaTag = &dependencyTag{name: "dcla"}
Paul Duffin520917a2022-05-13 13:01:59 +0000697 executableTag = &dependencyTag{name: "executable", payload: true}
698 fsTag = &dependencyTag{name: "filesystem", payload: true}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000699 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
700 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType}
Paul Duffinfcf79852022-07-20 14:18:24 +0000701 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000702 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
703 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
704 keyTag = &dependencyTag{name: "key"}
705 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
706 rroTag = &dependencyTag{name: "rro", payload: true}
707 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
708 testForTag = &dependencyTag{name: "test for"}
709 testTag = &dependencyTag{name: "test", payload: true}
710 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900711)
712
713// TODO(jiyong): shorten this function signature
714func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900715 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900716 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900717 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900718
719 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900720 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900721 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
722 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900723 }
724
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900725 // Use *FarVariation* to be able to depend on modules having conflicting variations with
726 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
727 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700728 ctx.AddFarVariationDependencies(binVariations, executableTag,
729 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
730 ctx.AddFarVariationDependencies(binVariations, testTag,
731 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
732 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
733 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
734 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
735 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
736 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
737 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
738 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
739 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900740}
741
742func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900743 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900744 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
745 } else {
746 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
747 if ctx.Os().Bionic() {
748 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
749 } else {
750 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
751 }
752 }
753}
754
Jooyung Hand045ebc2022-12-06 15:23:57 +0900755// getImageVariationPair returns a pair for the image variation name as its
756// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
757// suffix indicates the vndk version when it's vendor or product.
758// getImageVariation can simply join the result of this function to get the
759// image variation name.
760func (a *apexBundle) getImageVariationPair(deviceConfig android.DeviceConfig) (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900761 if a.vndkApex {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900762 return cc.VendorVariationPrefix, a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900763 }
764
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900765 var prefix string
766 var vndkVersion string
767 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000768 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900769 prefix = cc.VendorVariationPrefix
770 vndkVersion = deviceConfig.VndkVersion()
771 } else if a.ProductSpecific() {
772 prefix = cc.ProductVariationPrefix
773 vndkVersion = deviceConfig.ProductVndkVersion()
774 }
775 }
776 if vndkVersion == "current" {
777 vndkVersion = deviceConfig.PlatformVndkVersion()
778 }
779 if vndkVersion != "" {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900780 return prefix, vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900781 }
782
Jooyung Hand045ebc2022-12-06 15:23:57 +0900783 return android.CoreVariation, "" // The usual case
784}
785
786// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
787// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
788func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
789 prefix, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig())
790 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900791}
792
793func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900794 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
795 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
796 // each target os/architectures, appropriate dependencies are selected by their
797 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900798 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900799 imageVariation := a.getImageVariation(ctx)
800
801 a.combineProperties(ctx)
802
803 has32BitTarget := false
804 for _, target := range targets {
805 if target.Arch.ArchType.Multilib == "lib32" {
806 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000807 }
808 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900809 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900810 // Don't include artifacts for the host cross targets because there is no way for us
811 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900812 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900813 continue
814 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000815
Colin Cross70572ed2022-11-02 13:14:20 -0700816 var deps ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000817
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900818 // Add native modules targeting both ABIs. When multilib.* is omitted for
819 // native_shared_libs/jni_libs/tests, it implies multilib.both
Colin Cross70572ed2022-11-02 13:14:20 -0700820 deps.Merge(a.properties.Multilib.Both)
821 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900822 Native_shared_libs: a.properties.Native_shared_libs,
823 Tests: a.properties.Tests,
824 Jni_libs: a.properties.Jni_libs,
825 Binaries: nil,
826 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900827
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900828 // Add native modules targeting the first ABI When multilib.* is omitted for
829 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900830 isPrimaryAbi := i == 0
831 if isPrimaryAbi {
Colin Cross70572ed2022-11-02 13:14:20 -0700832 deps.Merge(a.properties.Multilib.First)
833 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900834 Native_shared_libs: nil,
835 Tests: nil,
836 Jni_libs: nil,
837 Binaries: a.properties.Binaries,
838 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900839 }
840
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900841 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900842 switch target.Arch.ArchType.Multilib {
843 case "lib32":
Colin Cross70572ed2022-11-02 13:14:20 -0700844 deps.Merge(a.properties.Multilib.Lib32)
845 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900846 case "lib64":
Colin Cross70572ed2022-11-02 13:14:20 -0700847 deps.Merge(a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900848 if !has32BitTarget {
Colin Cross70572ed2022-11-02 13:14:20 -0700849 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900850 }
851 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900852
Jiyong Park59140302020-12-14 18:44:04 +0900853 // Add native modules targeting a specific arch variant
854 switch target.Arch.ArchType {
855 case android.Arm:
Colin Cross70572ed2022-11-02 13:14:20 -0700856 deps.Merge(a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900857 case android.Arm64:
Colin Cross70572ed2022-11-02 13:14:20 -0700858 deps.Merge(a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700859 case android.Riscv64:
Colin Cross70572ed2022-11-02 13:14:20 -0700860 deps.Merge(a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900861 case android.X86:
Colin Cross70572ed2022-11-02 13:14:20 -0700862 deps.Merge(a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900863 case android.X86_64:
Colin Cross70572ed2022-11-02 13:14:20 -0700864 deps.Merge(a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900865 default:
866 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
867 }
868
Colin Cross70572ed2022-11-02 13:14:20 -0700869 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Sundong Ahn80c04892021-11-23 00:57:19 +0000870 ctx.AddFarVariationDependencies([]blueprint.Variation{
871 {Mutator: "os", Variation: target.OsVariation()},
872 {Mutator: "arch", Variation: target.ArchVariation()},
873 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900874 }
875
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900876 // Common-arch dependencies come next
877 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000878 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.properties.Rros...)
Anton Hanssone7545852023-02-24 11:06:07 +0000879 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.properties.Bootclasspath_fragments...)
880 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.properties.Systemserverclasspath_fragments...)
881 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
Jiyong Park12a719c2021-01-07 15:31:24 +0900882 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000883 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100884}
885
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900886// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900887func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
888 if a.overridableProperties.Allowed_files != nil {
889 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100890 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900891
892 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
893 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800894 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700895 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
896 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
897 // regardless of the TARGET_PREFER_* setting. See b/144532908
898 arches := ctx.DeviceConfig().Arches()
899 if len(arches) != 0 {
900 archForPrebuiltEtc := arches[0]
901 for _, arch := range arches {
902 // Prefer 64-bit arch if there is any
903 if arch.ArchType.Multilib == "lib64" {
904 archForPrebuiltEtc = arch
905 break
906 }
907 }
908 ctx.AddFarVariationDependencies([]blueprint.Variation{
909 {Mutator: "os", Variation: ctx.Os().String()},
910 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
911 }, prebuiltTag, prebuilts...)
912 }
913 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700914
915 // Dependencies for signing
916 if String(a.overridableProperties.Key) == "" {
917 ctx.PropertyErrorf("key", "missing")
918 return
919 }
920 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
921
922 cert := android.SrcIsModule(a.getCertString(ctx))
923 if cert != "" {
924 ctx.AddDependency(ctx.Module(), certificateTag, cert)
925 // empty cert is not an error. Cert and private keys will be directly found under
926 // PRODUCT_DEFAULT_DEV_CERTIFICATE
927 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100928}
929
Dennis Shene2ed70c2023-01-11 14:15:43 +0000930func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
931 if !mctx.Config().ApexTrimEnabled() {
932 return
933 }
934 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
935 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
936 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
937 } else if o, ok := mctx.Module().(*OverrideApex); ok {
938 for _, p := range o.GetProperties() {
939 properties, ok := p.(*overridableProperties)
940 if !ok {
941 continue
942 }
943 if properties.Trim_against != nil {
944 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
945 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
946 }
947 }
948 }
949}
950
951type DCLAInfo struct {
952 ProvidedLibs []string
953}
954
955var DCLAInfoProvider = blueprint.NewMutatorProvider(DCLAInfo{}, "apex_info")
956
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900957type ApexBundleInfo struct {
958 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100959}
960
Paul Duffin949abc02020-12-08 10:34:30 +0000961var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900962
Paul Duffina7d6a892020-12-07 17:39:59 +0000963var _ ApexInfoMutator = (*apexBundle)(nil)
964
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100965func (a *apexBundle) ApexVariationName() string {
966 return a.properties.ApexVariationName
967}
968
Paul Duffina7d6a892020-12-07 17:39:59 +0000969// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900970// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
971// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
972// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
973// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000974//
975// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
976// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
977// The apexMutator uses that list to create module variants for the apexes to which it belongs.
978// The relationship between module variants and apexes is not one-to-one as variants will be
979// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000980func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900981
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900982 // The VNDK APEX is special. For the APEX, the membership is described in a very different
983 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
984 // libraries are self-identified by their vndk.enabled properties. There is no need to run
985 // this mutator for the APEX as nothing will be collected. So, let's return fast.
986 if a.vndkApex {
987 return
988 }
989
990 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
991 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
992 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
993 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
994 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900995 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
996 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +0900997 if proptools.Bool(a.properties.Use_vndk_as_stable) {
998 if !useVndk {
999 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
1000 }
Jooyung Han02873da2023-03-22 17:41:03 +09001001 if a.minSdkVersionValue(mctx) != "" {
1002 mctx.PropertyErrorf("use_vndk_as_stable", "not supported when min_sdk_version is set")
1003 }
Jooyung Hanc5a96762022-02-04 11:54:50 +09001004 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
1005 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
1006 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
1007 }
1008 })
1009 if mctx.Failed() {
1010 return
1011 }
Jooyung Handf78e212020-07-22 15:54:47 +09001012 }
1013
Colin Cross56a83212020-09-15 18:30:11 -07001014 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001015 am, ok := child.(android.ApexModule)
1016 if !ok || !am.CanHaveApexVariants() {
1017 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +09001018 }
Paul Duffin573989d2021-03-17 13:25:29 +00001019 depTag := mctx.OtherModuleDependencyTag(child)
1020
1021 // Check to see if the tag always requires that the child module has an apex variant for every
1022 // apex variant of the parent module. If it does not then it is still possible for something
1023 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
1024 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
1025 return true
1026 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001027 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001028 return false
1029 }
Jooyung Handf78e212020-07-22 15:54:47 +09001030 if excludeVndkLibs {
1031 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
1032 return false
1033 }
1034 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001035 // By default, all the transitive dependencies are collected, unless filtered out
1036 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001037 return true
1038 }
1039
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001040 // Records whether a certain module is included in this apexBundle via direct dependency or
1041 // inndirect dependency.
1042 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001043 mctx.WalkDeps(func(child, parent android.Module) bool {
1044 if !continueApexDepsWalk(child, parent) {
1045 return false
1046 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001047 // If the parent is apexBundle, this child is directly depended.
1048 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001049 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001050 contents[depName] = contents[depName].Add(directDep)
1051 return true
1052 })
1053
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001054 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001055 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -07001056 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
1057 Contents: apexContents,
1058 })
1059
Jooyung Haned124c32021-01-26 11:43:46 +09001060 minSdkVersion := a.minSdkVersion(mctx)
1061 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1062 if minSdkVersion.IsNone() {
1063 minSdkVersion = android.FutureApiLevel
1064 }
1065
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001066 // This is the main part of this mutator. Mark the collected dependencies that they need to
1067 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001068
Jooyung Han63dff462023-02-09 00:11:27 +00001069 apexVariationName := mctx.ModuleName() // could be com.android.foo
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001070 a.properties.ApexVariationName = apexVariationName
Spandan Dase8173a82023-04-12 17:14:11 +00001071 testApexes := []string{}
1072 if a.testApex {
1073 testApexes = []string{apexVariationName}
1074 }
Colin Cross56a83212020-09-15 18:30:11 -07001075 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001076 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001077 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001078 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001079 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001080 InApexVariants: []string{apexVariationName},
1081 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001082 ApexContents: []*android.ApexContents{apexContents},
Spandan Dase8173a82023-04-12 17:14:11 +00001083 TestApexes: testApexes,
Colin Cross56a83212020-09-15 18:30:11 -07001084 }
Colin Cross56a83212020-09-15 18:30:11 -07001085 mctx.WalkDeps(func(child, parent android.Module) bool {
1086 if !continueApexDepsWalk(child, parent) {
1087 return false
1088 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001089 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001090 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001091 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001092
1093 if a.dynamic_common_lib_apex() {
1094 mctx.SetProvider(DCLAInfoProvider, DCLAInfo{
1095 ProvidedLibs: a.properties.Native_shared_libs,
1096 })
1097 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001098}
1099
Paul Duffina7d6a892020-12-07 17:39:59 +00001100type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001101 // ApexVariationName returns the name of the APEX variation to use in the apex
1102 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1103 ApexVariationName() string
1104
Paul Duffina7d6a892020-12-07 17:39:59 +00001105 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1106 // depended upon by an apex and which require an apex specific variant.
1107 ApexInfoMutator(android.TopDownMutatorContext)
1108}
1109
1110// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1111// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001112// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001113func apexInfoMutator(mctx android.TopDownMutatorContext) {
1114 if !mctx.Module().Enabled() {
1115 return
1116 }
1117
1118 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1119 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001120 }
Spandan Das42e89502022-05-06 22:12:55 +00001121 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001122}
1123
Spandan Das66773252022-01-15 00:23:18 +00001124// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
1125// This check is enforced for updatable modules
1126func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
1127 if !mctx.Module().Enabled() {
1128 return
1129 }
Spandan Das08c911f2022-01-21 22:07:26 +00001130 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +00001131 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +00001132 // b/208656169 Do not propagate strict updatability linting to libcore/
1133 // These libs are available on the classpath during compilation
1134 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1135 // Only skip libraries defined in libcore root, not subdirectories
1136 if mctx.OtherModuleDir(child) == "libcore" {
1137 // Do not traverse transitive deps of libcore/ libs
1138 return false
1139 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001140 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1141 return false
1142 }
Spandan Das66773252022-01-15 00:23:18 +00001143 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1144 lintable.SetStrictUpdatabilityLinting(true)
1145 }
1146 // visit transitive deps
1147 return true
1148 })
1149 }
1150}
1151
Spandan Das42e89502022-05-06 22:12:55 +00001152// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1153func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1154 if !mctx.Module().Enabled() {
1155 return
1156 }
1157 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1158 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1159 mctx.VisitDirectDeps(func(module android.Module) {
1160 // ignore android_test_app
1161 if app, ok := module.(*java.AndroidApp); ok {
1162 app.SetUpdatable(true)
1163 }
1164 })
1165 }
1166}
1167
Spandan Das08c911f2022-01-21 22:07:26 +00001168// TODO: b/215736885 Whittle the denylist
1169// Transitive deps of certain mainline modules baseline NewApi errors
1170// Skip these mainline modules for now
1171var (
1172 skipStrictUpdatabilityLintAllowlist = []string{
1173 "com.android.art",
1174 "com.android.art.debug",
1175 "com.android.conscrypt",
1176 "com.android.media",
1177 // test apexes
1178 "test_com.android.art",
1179 "test_com.android.conscrypt",
1180 "test_com.android.media",
1181 "test_jitzygote_com.android.art",
1182 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001183
1184 // TODO: b/215736885 Remove this list
1185 skipLintJavalibAllowlist = []string{
1186 "conscrypt.module.platform.api.stubs",
1187 "conscrypt.module.public.api.stubs",
1188 "conscrypt.module.public.api.stubs.system",
1189 "conscrypt.module.public.api.stubs.module_lib",
1190 "framework-media.stubs",
1191 "framework-media.stubs.system",
1192 "framework-media.stubs.module_lib",
1193 }
Spandan Das08c911f2022-01-21 22:07:26 +00001194)
1195
1196func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1197 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1198}
1199
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001200// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1201// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1202// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001203func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1204 if !mctx.Module().Enabled() {
1205 return
1206 }
1207 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001208 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1209 }
1210}
1211
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001212// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1213// the apex in order to retrieve its contents later.
1214// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001215func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1216 if !mctx.Module().Enabled() {
1217 return
1218 }
Colin Cross56a83212020-09-15 18:30:11 -07001219 if am, ok := mctx.Module().(android.ApexModule); ok {
1220 if testFor := am.TestFor(); len(testFor) > 0 {
1221 mctx.AddFarVariationDependencies([]blueprint.Variation{
1222 {Mutator: "os", Variation: am.Target().OsVariation()},
1223 {"arch", "common"},
1224 }, testForTag, testFor...)
1225 }
1226 }
1227}
1228
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001229// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001230func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1231 if !mctx.Module().Enabled() {
1232 return
1233 }
Colin Cross56a83212020-09-15 18:30:11 -07001234 if _, ok := mctx.Module().(android.ApexModule); ok {
1235 var contents []*android.ApexContents
1236 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1237 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1238 contents = append(contents, abInfo.Contents)
1239 }
1240 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1241 ApexContents: contents,
1242 })
Colin Crossaede88c2020-08-11 12:17:01 -07001243 }
1244}
1245
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001246// markPlatformAvailability marks whether or not a module can be available to platform. A module
1247// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1248// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1249// be) available to platform
1250// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001251func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1252 // Host and recovery are not considered as platform
1253 if mctx.Host() || mctx.Module().InstallInRecovery() {
1254 return
1255 }
1256
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001257 am, ok := mctx.Module().(android.ApexModule)
1258 if !ok {
1259 return
1260 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001261
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001262 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001263
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001264 // If any of the dep is not available to platform, this module is also considered as being
1265 // not available to platform even if it has "//apex_available:platform"
1266 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001267 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001268 // if the dependency crosses apex boundary, don't consider it
1269 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001270 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001271 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1272 availableToPlatform = false
1273 // TODO(b/154889534) trigger an error when 'am' has
1274 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001275 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001276 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001277
Paul Duffinb5769c12021-05-12 16:16:51 +01001278 // Exception 1: check to see if the module always requires it.
1279 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001280 availableToPlatform = true
1281 }
1282
1283 // Exception 2: bootstrap bionic libraries are also always available to platform
1284 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1285 availableToPlatform = true
1286 }
1287
1288 if !availableToPlatform {
1289 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001290 }
1291}
1292
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001293// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001294// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001295func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001296 if !mctx.Module().Enabled() {
1297 return
1298 }
Colin Cross56a83212020-09-15 18:30:11 -07001299
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001300 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001301 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001302 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001303 return
1304 }
1305
1306 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001307 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1308 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001309 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001310 if strings.HasPrefix(apexBundleName, "com.android.art") {
1311 // Create an alias from the platform variant. This is done to make
1312 // test_for dependencies work for modules that are split by the APEX
1313 // mutator, since test_for dependencies always go to the platform variant.
1314 // This doesn't happen for normal APEXes that are disjunct, so only do
1315 // this for the overlapping ART APEXes.
1316 // TODO(b/183882457): Remove this if the test_for functionality is
1317 // refactored to depend on the proper APEX variants instead of platform.
1318 mctx.CreateAliasVariation("", apexBundleName)
1319 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001320 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1321 apexBundleName := o.GetOverriddenModuleName()
1322 if apexBundleName == "" {
1323 mctx.ModuleErrorf("base property is not set")
1324 return
1325 }
1326 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001327 if strings.HasPrefix(apexBundleName, "com.android.art") {
1328 // TODO(b/183882457): See note for CreateAliasVariation above.
1329 mctx.CreateAliasVariation("", apexBundleName)
1330 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001331 }
1332}
Sundong Ahne9b55722019-09-06 17:37:42 +09001333
Paul Duffin6717d882021-06-15 19:09:41 +01001334// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1335// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001336func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001337 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001338 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001339 return !a.vndkApex
1340 }
1341
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001342 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001343}
1344
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001345// See android.UpdateDirectlyInAnyApex
1346// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001347func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1348 if !mctx.Module().Enabled() {
1349 return
1350 }
1351 if am, ok := mctx.Module().(android.ApexModule); ok {
1352 android.UpdateDirectlyInAnyApex(mctx, am)
1353 }
1354}
1355
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001356// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001357type apexPackaging int
1358
1359const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001360 // imageApex is a packaging method where contents are included in a filesystem image which
1361 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001362 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001363
1364 // zipApex is a packaging method where contents are directly included in the zip container.
1365 // This is used for host-side testing - because the contents are easily accessible by
1366 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001367 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001368
1369 // flattendApex is a packaging method where contents are not included in the APEX file, but
1370 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1371 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001372 flattenedApex
1373)
1374
1375const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001376 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001377 imageApexSuffix = ".apex"
1378 imageCapexSuffix = ".capex"
1379 zipApexSuffix = ".zipapex"
1380 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001381
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001382 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001383 imageApexType = "image"
1384 zipApexType = "zip"
1385 flattenedApexType = "flattened"
1386
Dan Willemsen47e1a752021-10-16 18:36:13 -07001387 ext4FsType = "ext4"
1388 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001389 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001390)
1391
1392// The suffix for the output "file", not the module
1393func (a apexPackaging) suffix() string {
1394 switch a {
1395 case imageApex:
1396 return imageApexSuffix
1397 case zipApex:
1398 return zipApexSuffix
1399 default:
1400 panic(fmt.Errorf("unknown APEX type %d", a))
1401 }
1402}
1403
1404func (a apexPackaging) name() string {
1405 switch a {
1406 case imageApex:
1407 return imageApexType
1408 case zipApex:
1409 return zipApexType
1410 default:
1411 panic(fmt.Errorf("unknown APEX type %d", a))
1412 }
1413}
1414
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001415// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1416// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001417func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001418 if !mctx.Module().Enabled() {
1419 return
1420 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001421 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001422 var variants []string
1423 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1424 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001425 // This is the normal case. Note that both image and flattend APEXes are
1426 // created. The image type is installed to the system partition, while the
1427 // flattened APEX is (optionally) installed to the system_ext partition.
1428 // This is mostly for GSI which has to support wide range of devices. If GSI
1429 // is installed on a newer (APEX-capable) device, the image APEX in the
1430 // system will be used. However, if the same GSI is installed on an old
1431 // device which can't support image APEX, the flattened APEX in the
1432 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001433 variants = append(variants, imageApexType, flattenedApexType)
1434 case "zip":
1435 variants = append(variants, zipApexType)
1436 case "both":
1437 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1438 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001439 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001440 return
1441 }
1442
1443 modules := mctx.CreateLocalVariations(variants...)
1444
1445 for i, v := range variants {
1446 switch v {
1447 case imageApexType:
1448 modules[i].(*apexBundle).properties.ApexType = imageApex
1449 case zipApexType:
1450 modules[i].(*apexBundle).properties.ApexType = zipApex
1451 case flattenedApexType:
1452 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001453 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001454 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001455 modules[i].(*apexBundle).MakeAsSystemExt()
1456 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001457 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001458 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001459 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001460 // payload_type is forcibly overridden to "image"
1461 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001462 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001463 }
1464}
1465
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001466var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001467
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001468// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001469func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001470 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001471 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001472 return true
1473}
1474
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001475var _ android.OutputFileProducer = (*apexBundle)(nil)
1476
1477// Implements android.OutputFileProducer
1478func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1479 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001480 case "", android.DefaultDistTag:
1481 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001482 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001483 case imageApexSuffix:
1484 // uncompressed one
1485 if a.outputApexFile != nil {
1486 return android.Paths{a.outputApexFile}, nil
1487 }
1488 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001489 default:
1490 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1491 }
1492}
1493
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001494var _ multitree.Exportable = (*apexBundle)(nil)
1495
1496func (a *apexBundle) Exportable() bool {
1497 if a.properties.ApexType == flattenedApex {
1498 return false
1499 }
1500 return true
1501}
1502
1503func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1504 ret := make(map[string]android.Paths)
1505 ret["apex"] = android.Paths{a.outputFile}
1506 return ret
1507}
1508
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001509var _ cc.Coverage = (*apexBundle)(nil)
1510
1511// Implements cc.Coverage
1512func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1513 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1514}
1515
1516// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001517func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001518 a.properties.PreventInstall = true
1519}
1520
1521// Implements cc.Coverage
1522func (a *apexBundle) HideFromMake() {
1523 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001524 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1525 // TODO(ccross): untangle these
1526 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001527}
1528
1529// Implements cc.Coverage
1530func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1531 a.properties.IsCoverageVariant = coverage
1532}
1533
1534// Implements cc.Coverage
1535func (a *apexBundle) EnableCoverageIfNeeded() {}
1536
1537var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1538
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001539// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001540func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001541 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001542}
1543
Jiyong Parkf4020582021-11-29 12:37:10 +09001544func (a *apexBundle) FutureUpdatable() bool {
1545 return proptools.BoolDefault(a.properties.Future_updatable, false)
1546}
1547
Jiyong Park1bc84122021-06-22 20:23:05 +09001548func (a *apexBundle) UsePlatformApis() bool {
1549 return proptools.BoolDefault(a.properties.Platform_apis, false)
1550}
1551
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001552// getCertString returns the name of the cert that should be used to sign this APEX. This is
1553// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001554func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001555 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001556 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1557 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1558 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001559 if a.vndkApex {
1560 moduleName = vndkApexName
1561 }
1562 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001563 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001564 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001565 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001566 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001567}
1568
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001569// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001570func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001571 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001572}
1573
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001574// See the generate_hashtree property
1575func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001576 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001577}
1578
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001579// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001580func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1581 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1582}
1583
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001584// See the test_only_force_compression property
1585func (a *apexBundle) testOnlyShouldForceCompression() bool {
1586 return proptools.Bool(a.properties.Test_only_force_compression)
1587}
1588
Dennis Shenaf41bc12022-08-03 16:46:43 +00001589// See the dynamic_common_lib_apex property
1590func (a *apexBundle) dynamic_common_lib_apex() bool {
1591 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1592}
1593
Dennis Shene2ed70c2023-01-11 14:15:43 +00001594// See the list of libs to trim
1595func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1596 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1597 if len(dclaModules) > 1 {
1598 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1599 }
1600 if len(dclaModules) > 0 {
1601 DCLAInfo := ctx.OtherModuleProvider(dclaModules[0], DCLAInfoProvider).(DCLAInfo)
1602 return DCLAInfo.ProvidedLibs
1603 }
1604 return []string{}
1605}
1606
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001607// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1608// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1609// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001610
Jiyong Parkf97782b2019-02-13 20:28:58 +09001611func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1612 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1613 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1614 }
1615}
1616
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001617func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001618 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1619 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001620 }
1621
1622 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001623 var globalSanitizerNames []string
Jiyong Park388ef3f2019-01-28 19:47:32 +09001624 if a.Host() {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001625 globalSanitizerNames = config.SanitizeHost()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001626 } else {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001627 arches := config.SanitizeDeviceArch()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001628 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001629 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001630 }
1631 }
1632 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001633}
1634
Jooyung Han8ce8db92020-05-15 19:05:05 +09001635func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001636 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1637 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001638 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001639 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001640 for _, target := range ctx.MultiTargets() {
1641 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001642 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001643 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001644 Tests: nil,
1645 Jni_libs: nil,
1646 Binaries: nil,
1647 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001648 break
1649 }
1650 }
1651 }
1652}
1653
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001654// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1655// returned apexFile saves information about the Soong module that will be used for creating the
1656// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001657func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001658 // Decide the APEX-local directory by the multilib of the library In the future, we may
1659 // query this to the module.
1660 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001661 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001662 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001663 case "lib32":
1664 dirInApex = "lib"
1665 case "lib64":
1666 dirInApex = "lib64"
1667 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001668 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001669 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001670 }
Jiyong Park1833cef2019-12-13 13:28:36 +09001671 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001672 // Special case for Bionic libs and other libs installed with them. This is to
1673 // prevent those libs from being included in the search path
1674 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1675 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1676 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1677 // will be loaded into the default linker namespace (aka "platform" namespace). If
1678 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1679 // be loaded again into the runtime linker namespace, which will result in double
1680 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001681 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001682 }
Florian Mayer95cd6db2023-03-23 17:48:07 -07001683 // This needs to go after the runtime APEX handling because otherwise we would get
1684 // weird paths like lib64/rel_install_path/bionic rather than
1685 // lib64/bionic/rel_install_path.
1686 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001687
Colin Cross1d487152022-10-03 19:14:46 -07001688 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001689 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1690 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001691}
1692
Jiyong Park1833cef2019-12-13 13:28:36 +09001693func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001694 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001695 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001696 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001697 }
Jooyung Han35155c42020-02-06 17:33:20 +09001698 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001699 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001700 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1701 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001702 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001703 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001704 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001705}
1706
Jiyong Park99644e92020-11-17 22:21:02 +09001707func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1708 dirInApex := "bin"
1709 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1710 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1711 }
Colin Cross1d487152022-10-03 19:14:46 -07001712 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001713 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1714 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1715 return af
1716}
1717
1718func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1719 // Decide the APEX-local directory by the multilib of the library
1720 // In the future, we may query this to the module.
1721 var dirInApex string
1722 switch rustm.Arch().ArchType.Multilib {
1723 case "lib32":
1724 dirInApex = "lib"
1725 case "lib64":
1726 dirInApex = "lib64"
1727 }
1728 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1729 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1730 }
Colin Cross1d487152022-10-03 19:14:46 -07001731 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001732 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1733 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1734}
1735
Cole Faust4d247e62023-01-23 10:14:58 -08001736func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.PythonBinaryModule) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001737 dirInApex := "bin"
1738 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001739 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001740}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001741
Jiyong Park1833cef2019-12-13 13:28:36 +09001742func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001743 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001744 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001745 // NB: Since go binaries are static we don't need the module for anything here, which is
1746 // good since the go tool is a blueprint.Module not an android.Module like we would
1747 // normally use.
Jingwen Chen2d37b642023-03-14 16:11:38 +00001748 //
Jiyong Park1833cef2019-12-13 13:28:36 +09001749 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001750}
1751
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001752func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001753 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001754 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1755 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1756 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001757 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001758 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001759 af.symlinks = sh.Symlinks()
1760 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001761}
1762
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001763func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001764 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001765 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001766 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001767}
1768
atrost6e126252020-01-27 17:01:16 +00001769func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1770 dirInApex := filepath.Join("etc", config.SubDir())
1771 fileToCopy := config.CompatConfig()
1772 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1773}
1774
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001775// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1776// way.
1777type javaModule interface {
1778 android.Module
1779 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001780 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001781 JacocoReportClassesFile() android.Path
1782 LintDepSets() java.LintDepSets
1783 Stem() string
1784}
1785
1786var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001787var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001788var _ javaModule = (*java.SdkLibrary)(nil)
1789var _ javaModule = (*java.DexImport)(nil)
1790var _ javaModule = (*java.SdkLibraryImport)(nil)
1791
Paul Duffin190fdef2021-04-26 10:33:59 +01001792// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001793func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001794 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001795}
1796
1797// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1798func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001799 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001800 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001801 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1802 af.lintDepSets = module.LintDepSets()
1803 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001804 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1805 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1806 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1807 }
1808 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001809 return af
1810}
1811
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001812func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1813 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001814 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001815 dirInApex := "javalib"
1816 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1817 af.customStem = module.Stem() + ".jar.prof"
1818 return &af
1819 }
1820 }
1821 return nil
1822}
1823
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001824// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1825// the same way.
1826type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001827 android.Module
1828 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001829 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001830 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001831 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001832 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001833 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001834 LintDepSets() java.LintDepSets
Andrei Onea580636b2022-08-17 16:53:46 +00001835 PrivAppAllowlist() android.OptionalPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001836}
1837
1838var _ androidApp = (*java.AndroidApp)(nil)
1839var _ androidApp = (*java.AndroidAppImport)(nil)
1840
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001841func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1842 buildId := ctx.Config().BuildId()
1843
1844 // The build ID is used as a suffix for a filename, so ensure that
1845 // the set of characters being used are sanitized.
1846 // - any word character: [a-zA-Z0-9_]
1847 // - dots: .
1848 // - dashes: -
1849 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1850 if !validRegex.MatchString(buildId) {
1851 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1852 }
1853 return buildId
1854}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001855
Andrei Onea580636b2022-08-17 16:53:46 +00001856func apexFilesForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) []apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001857 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001858 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001859 appDir = "priv-app"
1860 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001861
1862 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1863 // so that PackageManager correctly invalidates the existing installed apk
1864 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001865 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001866 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001867
Yo Chiange8128052020-07-23 20:09:18 +08001868 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001869 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001870 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001871 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001872
1873 if app, ok := aapp.(interface {
1874 OverriddenManifestPackageName() string
1875 }); ok {
1876 af.overriddenPackageName = app.OverriddenManifestPackageName()
1877 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04001878
1879 apexFiles := []apexFile{}
Andrei Onea580636b2022-08-17 16:53:46 +00001880
1881 if allowlist := aapp.PrivAppAllowlist(); allowlist.Valid() {
1882 dirInApex := filepath.Join("etc", "permissions")
Sam Delmericob1daccd2023-05-25 14:45:30 -04001883 privAppAllowlist := newApexFile(ctx, allowlist.Path(), aapp.BaseModuleName()+"_privapp", dirInApex, etc, aapp)
Andrei Onea580636b2022-08-17 16:53:46 +00001884 apexFiles = append(apexFiles, privAppAllowlist)
1885 }
1886
Sam Delmericob1daccd2023-05-25 14:45:30 -04001887 apexFiles = append(apexFiles, af)
1888
Andrei Onea580636b2022-08-17 16:53:46 +00001889 return apexFiles
Dario Frenicde2a032019-10-27 00:29:22 +01001890}
1891
Jiyong Park69aeba92020-04-24 21:16:36 +09001892func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1893 rroDir := "overlay"
1894 dirInApex := filepath.Join(rroDir, rro.Theme())
1895 fileToCopy := rro.OutputFile()
1896 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1897 af.certificate = rro.Certificate()
1898
1899 if a, ok := rro.(interface {
1900 OverriddenManifestPackageName() string
1901 }); ok {
1902 af.overriddenPackageName = a.OverriddenManifestPackageName()
1903 }
1904 return af
1905}
1906
Ken Chenfad7f9d2021-11-10 22:02:57 +08001907func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1908 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001909 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1910}
1911
Jiyong Park12a719c2021-01-07 15:31:24 +09001912func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1913 dirInApex := filepath.Join("etc", "fs")
1914 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1915}
1916
Paul Duffin064b70c2020-11-02 17:32:38 +00001917// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001918// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1919// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1920// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001921func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001922 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001923 am, ok := child.(android.ApexModule)
1924 if !ok || !am.CanHaveApexVariants() {
1925 return false
1926 }
1927
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001928 // Filter-out unwanted depedendencies
1929 depTag := ctx.OtherModuleDependencyTag(child)
1930 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1931 return false
1932 }
Paul Duffin520917a2022-05-13 13:01:59 +00001933 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001934 return false
1935 }
1936
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001937 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001938 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001939
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001940 // Visit actually
1941 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001942 })
1943}
1944
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001945// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1946type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001947
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001948const (
1949 ext4 fsType = iota
1950 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001951 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001952)
Artur Satayev849f8442020-04-28 14:57:42 +01001953
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001954func (f fsType) string() string {
1955 switch f {
1956 case ext4:
1957 return ext4FsType
1958 case f2fs:
1959 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001960 case erofs:
1961 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001962 default:
1963 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001964 }
1965}
1966
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001967var _ android.MixedBuildBuildable = (*apexBundle)(nil)
1968
1969func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
Jingwen Chenbad41822023-03-23 03:04:00 +00001970 return a.properties.ApexType == imageApex
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001971}
1972
1973func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
1974 bazelCtx := ctx.Config().BazelContext
1975 bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
1976}
1977
Jingwen Chen889f2f22022-12-16 08:16:01 +00001978// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
1979// override_apex module overriding this apexBundle. An apexBundle can be
1980// overridden by different override_apex modules (e.g. Google or Go variants),
1981// which is handled by the overrides mutators.
1982func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
Jingwen Chen889f2f22022-12-16 08:16:01 +00001983 return a.BazelModuleBase.GetBazelLabel(ctx, a)
1984}
1985
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001986func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
1987 if !a.commonBuildActions(ctx) {
1988 return
1989 }
1990
1991 a.setApexTypeAndSuffix(ctx)
1992 a.setPayloadFsType(ctx)
1993 a.setSystemLibLink(ctx)
1994
1995 if a.properties.ApexType != zipApex {
1996 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
1997 }
1998
1999 bazelCtx := ctx.Config().BazelContext
2000 outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx))
2001 if err != nil {
2002 ctx.ModuleErrorf(err.Error())
2003 return
2004 }
2005 a.installDir = android.PathForModuleInstall(ctx, "apex")
Jingwen Chen94098e82023-01-10 14:50:42 +00002006
2007 // Set the output file to .apex or .capex depending on the compression configuration.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002008 a.setCompression(ctx)
Jingwen Chen94098e82023-01-10 14:50:42 +00002009 if a.isCompressed {
Cole Faustb0bfa072023-04-03 14:28:36 -07002010 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedCompressedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002011 } else {
Cole Faustb0bfa072023-04-03 14:28:36 -07002012 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002013 }
2014 a.outputFile = a.outputApexFile
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002015
Sam Delmerico4ed95e22023-02-03 18:12:15 -05002016 if len(outputs.TidyFiles) > 0 {
2017 tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles)
2018 a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles)
2019 }
2020
Liz Kammer0e255ef2022-11-04 16:07:04 -04002021 // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer
2022 // interface if these were set in a provider rather than the module itself
Wei Li32dcdf92022-10-26 22:30:48 -07002023 a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0])
2024 a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1])
2025 a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0])
2026 a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1])
Liz Kammer0e255ef2022-11-04 16:07:04 -04002027
Jingwen Chen29743c82023-01-25 17:49:46 +00002028 // Ensure ApexMkInfo.install_to_system make module names are installed as
2029 // part of a bundled build.
2030 a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...)
Vinh Tranb6803a52022-12-14 11:34:54 -05002031
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002032 apexType := a.properties.ApexType
2033 switch apexType {
2034 case imageApex:
Liz Kammer303978d2022-11-04 16:12:43 -04002035 a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile)
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002036 a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex))
Wei Licc73a052022-11-07 14:25:34 -08002037 a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs))
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002038 // TODO(b/239084755): Generate the java api using.xml file from Bazel.
Jingwen Chen1ec77852022-11-07 14:36:12 +00002039 a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex))
Wei Li78c07de2022-11-08 16:01:05 -08002040 a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002041 installSuffix := imageApexSuffix
2042 if a.isCompressed {
2043 installSuffix = imageCapexSuffix
2044 }
2045 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
2046 a.compatSymlinks.Paths()...)
2047 default:
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002048 panic(fmt.Errorf("internal error: unexpected apex_type for the ProcessBazelQueryResponse: %v", a.properties.ApexType))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002049 }
2050
Jingwen Chen2d37b642023-03-14 16:11:38 +00002051 // filesInfo in mixed mode must retrieve all information about the apex's
2052 // contents completely from the Starlark providers. It should never rely on
2053 // Android.bp information, as they might not exist for fully migrated
2054 // dependencies.
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002055 //
2056 // Prevent accidental writes to filesInfo in the earlier parts Soong by
2057 // asserting it to be nil.
2058 if a.filesInfo != nil {
Jingwen Chen2d37b642023-03-14 16:11:38 +00002059 panic(
2060 fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel. " +
2061 "Did something else set filesInfo before this line of code?"))
2062 }
2063 for _, f := range outputs.PayloadFilesInfo {
2064 fileInfo := apexFile{
2065 isBazelPrebuilt: true,
2066
2067 builtFile: android.PathForBazelOut(ctx, f["built_file"]),
2068 unstrippedBuiltFile: android.PathForBazelOut(ctx, f["unstripped_built_file"]),
2069 androidMkModuleName: f["make_module_name"],
2070 installDir: f["install_dir"],
2071 class: classes[f["class"]],
2072 customStem: f["basename"],
2073 moduleDir: f["package"],
2074 }
2075
2076 arch := f["arch"]
2077 fileInfo.arch = arch
2078 if len(arch) > 0 {
2079 fileInfo.multilib = "lib32"
2080 if strings.HasSuffix(arch, "64") {
2081 fileInfo.multilib = "lib64"
2082 }
2083 }
2084
2085 a.filesInfo = append(a.filesInfo, fileInfo)
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002086 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002087}
2088
2089func (a *apexBundle) setCompression(ctx android.ModuleContext) {
2090 if a.properties.ApexType != imageApex {
2091 a.isCompressed = false
2092 } else if a.testOnlyShouldForceCompression() {
2093 a.isCompressed = true
2094 } else {
2095 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
2096 }
2097}
2098
2099func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
2100 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2101 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2102 // the same library in the system partition, thus effectively sharing the same libraries
2103 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2104 // in the APEX.
2105 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
2106
2107 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2108 // So we can't link them to /system/lib libs which are core variants.
2109 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2110 a.linkToSystemLib = false
2111 }
2112
2113 forced := ctx.Config().ForceApexSymlinkOptimization()
2114 updatable := a.Updatable() || a.FutureUpdatable()
2115
2116 // We don't need the optimization for updatable APEXes, as it might give false signal
2117 // to the system health when the APEXes are still bundled (b/149805758).
2118 if !forced && updatable && a.properties.ApexType == imageApex {
2119 a.linkToSystemLib = false
2120 }
2121
2122 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2123 if ctx.Host() {
2124 a.linkToSystemLib = false
2125 }
2126}
2127
2128func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
2129 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2130 case ext4FsType:
2131 a.payloadFsType = ext4
2132 case f2fsFsType:
2133 a.payloadFsType = f2fs
2134 case erofsFsType:
2135 a.payloadFsType = erofs
2136 default:
2137 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
2138 }
2139}
2140
2141func (a *apexBundle) setApexTypeAndSuffix(ctx android.ModuleContext) {
2142 // Set suffix and primaryApexType depending on the ApexType
2143 buildFlattenedAsDefault := ctx.Config().FlattenApex()
2144 switch a.properties.ApexType {
2145 case imageApex:
2146 if buildFlattenedAsDefault {
2147 a.suffix = imageApexSuffix
2148 } else {
2149 a.suffix = ""
2150 a.primaryApexType = true
2151
2152 if ctx.Config().InstallExtraFlattenedApexes() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002153 a.makeModulesToInstall = append(a.makeModulesToInstall, a.Name()+flattenedSuffix)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002154 }
2155 }
2156 case zipApex:
2157 if proptools.String(a.properties.Payload_type) == "zip" {
2158 a.suffix = ""
2159 a.primaryApexType = true
2160 } else {
2161 a.suffix = zipApexSuffix
2162 }
2163 case flattenedApex:
2164 if buildFlattenedAsDefault {
2165 a.suffix = ""
2166 a.primaryApexType = true
2167 } else {
2168 a.suffix = flattenedSuffix
2169 }
2170 }
2171}
2172
2173func (a apexBundle) isCompressable() bool {
2174 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2175}
2176
2177func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2178 a.checkApexAvailability(ctx)
2179 a.checkUpdatable(ctx)
2180 a.CheckMinSdkVersion(ctx)
2181 a.checkStaticLinkingToStubLibraries(ctx)
2182 a.checkStaticExecutables(ctx)
2183 if len(a.properties.Tests) > 0 && !a.testApex {
2184 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2185 return false
2186 }
2187 return true
2188}
2189
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002190type visitorContext struct {
2191 // all the files that will be included in this APEX
2192 filesInfo []apexFile
2193
2194 // native lib dependencies
2195 provideNativeLibs []string
2196 requireNativeLibs []string
2197
2198 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002199
2200 // if true, raise error on duplicate apexFile
2201 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002202}
2203
Jooyung Han862c0d62022-12-21 10:15:37 +09002204func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002205 encountered := make(map[string]apexFile)
2206 for _, f := range vctx.filesInfo {
2207 dest := filepath.Join(f.installDir, f.builtFile.Base())
2208 if e, ok := encountered[dest]; !ok {
2209 encountered[dest] = f
2210 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002211 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2212 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2213 dest, e.builtFile, f.builtFile)
2214 return
2215 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002216 // If a module is directly included and also transitively depended on
2217 // consider it as directly included.
2218 e.transitiveDep = e.transitiveDep && f.transitiveDep
2219 encountered[dest] = e
2220 }
2221 }
2222 vctx.filesInfo = vctx.filesInfo[:0]
2223 for _, v := range encountered {
2224 vctx.filesInfo = append(vctx.filesInfo, v)
2225 }
2226 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2227 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2228 // changes.
2229 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2230 })
2231}
2232
2233func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2234 depTag := ctx.OtherModuleDependencyTag(child)
2235 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2236 return false
2237 }
2238 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2239 return false
2240 }
2241 depName := ctx.OtherModuleName(child)
2242 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2243 switch depTag {
2244 case sharedLibTag, jniLibTag:
2245 isJniLib := depTag == jniLibTag
2246 switch ch := child.(type) {
2247 case *cc.Module:
2248 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2249 fi.isJniLib = isJniLib
2250 vctx.filesInfo = append(vctx.filesInfo, fi)
2251 // Collect the list of stub-providing libs except:
2252 // - VNDK libs are only for vendors
2253 // - bootstrap bionic libs are treated as provided by system
2254 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2255 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2256 }
2257 return true // track transitive dependencies
2258 case *rust.Module:
2259 fi := apexFileForRustLibrary(ctx, ch)
2260 fi.isJniLib = isJniLib
2261 vctx.filesInfo = append(vctx.filesInfo, fi)
2262 return true // track transitive dependencies
2263 default:
2264 propertyName := "native_shared_libs"
2265 if isJniLib {
2266 propertyName = "jni_libs"
2267 }
2268 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2269 }
2270 case executableTag:
2271 switch ch := child.(type) {
2272 case *cc.Module:
2273 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2274 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002275 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002276 if ch.HostToolPath().Valid() {
2277 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2278 }
2279 case bootstrap.GoBinaryTool:
2280 if a.Host() {
2281 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2282 }
2283 case *rust.Module:
2284 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2285 return true // track transitive dependencies
2286 default:
2287 ctx.PropertyErrorf("binaries",
2288 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2289 }
2290 case shBinaryTag:
2291 if csh, ok := child.(*sh.ShBinary); ok {
2292 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2293 } else {
2294 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2295 }
2296 case bcpfTag:
Jiakai Zhangb47cacc2023-05-10 16:40:18 +01002297 _, ok := child.(*java.BootclasspathFragmentModule)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002298 if !ok {
2299 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2300 return false
2301 }
2302
2303 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002304 return true
2305 case sscpfTag:
2306 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2307 ctx.PropertyErrorf("systemserverclasspath_fragments",
2308 "%q is not a systemserverclasspath_fragment module", depName)
2309 return false
2310 }
2311 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2312 vctx.filesInfo = append(vctx.filesInfo, *af)
2313 }
2314 return true
2315 case javaLibTag:
2316 switch child.(type) {
2317 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2318 af := apexFileForJavaModule(ctx, child.(javaModule))
2319 if !af.ok() {
2320 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2321 return false
2322 }
2323 vctx.filesInfo = append(vctx.filesInfo, af)
2324 return true // track transitive dependencies
2325 default:
2326 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2327 }
2328 case androidAppTag:
2329 switch ap := child.(type) {
2330 case *java.AndroidApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002331 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002332 return true // track transitive dependencies
2333 case *java.AndroidAppImport:
Andrei Onea580636b2022-08-17 16:53:46 +00002334 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002335 case *java.AndroidTestHelperApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002336 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002337 case *java.AndroidAppSet:
2338 appDir := "app"
2339 if ap.Privileged() {
2340 appDir = "priv-app"
2341 }
2342 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2343 // suffixed so that PackageManager correctly invalidates the
2344 // existing installed apk in favour of the new APK-in-APEX.
2345 // See bugs for more information.
2346 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2347 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2348 af.certificate = java.PresignedCertificate
2349 vctx.filesInfo = append(vctx.filesInfo, af)
2350 default:
2351 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2352 }
2353 case rroTag:
2354 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2355 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2356 } else {
2357 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2358 }
2359 case bpfTag:
2360 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2361 filesToCopy, _ := bpfProgram.OutputFiles("")
2362 apex_sub_dir := bpfProgram.SubDir()
2363 for _, bpfFile := range filesToCopy {
2364 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2365 }
2366 } else {
2367 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2368 }
2369 case fsTag:
2370 if fs, ok := child.(filesystem.Filesystem); ok {
2371 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2372 } else {
2373 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2374 }
2375 case prebuiltTag:
2376 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2377 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2378 } else {
2379 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2380 }
2381 case compatConfigTag:
2382 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2383 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2384 } else {
2385 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2386 }
2387 case testTag:
2388 if ccTest, ok := child.(*cc.Module); ok {
2389 if ccTest.IsTestPerSrcAllTestsVariation() {
2390 // Multiple-output test module (where `test_per_src: true`).
2391 //
2392 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2393 // We do not add this variation to `filesInfo`, as it has no output;
2394 // however, we do add the other variations of this module as indirect
2395 // dependencies (see below).
2396 } else {
2397 // Single-output test module (where `test_per_src: false`).
2398 af := apexFileForExecutable(ctx, ccTest)
2399 af.class = nativeTest
2400 vctx.filesInfo = append(vctx.filesInfo, af)
2401 }
2402 return true // track transitive dependencies
2403 } else {
2404 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2405 }
2406 case keyTag:
2407 if key, ok := child.(*apexKey); ok {
2408 a.privateKeyFile = key.privateKeyFile
2409 a.publicKeyFile = key.publicKeyFile
2410 } else {
2411 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2412 }
2413 case certificateTag:
2414 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2415 a.containerCertificateFile = dep.Certificate.Pem
2416 a.containerPrivateKeyFile = dep.Certificate.Key
2417 } else {
2418 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2419 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002420 }
2421 return false
2422 }
2423
2424 if a.vndkApex {
2425 return false
2426 }
2427
2428 // indirect dependencies
2429 am, ok := child.(android.ApexModule)
2430 if !ok {
2431 return false
2432 }
2433 // We cannot use a switch statement on `depTag` here as the checked
2434 // tags used below are private (e.g. `cc.sharedDepTag`).
2435 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2436 if ch, ok := child.(*cc.Module); ok {
2437 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2438 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2439 return false
2440 }
2441 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2442 af.transitiveDep = true
2443
2444 // Always track transitive dependencies for host.
2445 if a.Host() {
2446 vctx.filesInfo = append(vctx.filesInfo, af)
2447 return true
2448 }
2449
2450 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2451 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2452 // If the dependency is a stubs lib, don't include it in this APEX,
2453 // but make sure that the lib is installed on the device.
2454 // In case no APEX is having the lib, the lib is installed to the system
2455 // partition.
2456 //
2457 // Always include if we are a host-apex however since those won't have any
2458 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002459 //
2460 // Skip the dependency in unbundled builds where the device image is not
2461 // being built.
2462 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002463 // we need a module name for Make
2464 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002465 if !android.InList(name, a.makeModulesToInstall) {
2466 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002467 }
2468 }
2469 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2470 // Don't track further
2471 return false
2472 }
2473
2474 // If the dep is not considered to be in the same
2475 // apex, don't add it to filesInfo so that it is not
2476 // included in this APEX.
2477 // TODO(jiyong): move this to at the top of the
2478 // else-if clause for the indirect dependencies.
2479 // Currently, that's impossible because we would
2480 // like to record requiredNativeLibs even when
2481 // DepIsInSameAPex is false. We also shouldn't do
2482 // this for host.
2483 //
2484 // TODO(jiyong): explain why the same module is passed in twice.
2485 // Switching the first am to parent breaks lots of tests.
2486 if !android.IsDepInSameApex(ctx, am, am) {
2487 return false
2488 }
2489
2490 vctx.filesInfo = append(vctx.filesInfo, af)
2491 return true // track transitive dependencies
2492 } else if rm, ok := child.(*rust.Module); ok {
2493 af := apexFileForRustLibrary(ctx, rm)
2494 af.transitiveDep = true
2495 vctx.filesInfo = append(vctx.filesInfo, af)
2496 return true // track transitive dependencies
2497 }
2498 } else if cc.IsTestPerSrcDepTag(depTag) {
2499 if ch, ok := child.(*cc.Module); ok {
2500 af := apexFileForExecutable(ctx, ch)
2501 // Handle modules created as `test_per_src` variations of a single test module:
2502 // use the name of the generated test binary (`fileToCopy`) instead of the name
2503 // of the original test module (`depName`, shared by all `test_per_src`
2504 // variations of that module).
2505 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2506 // these are not considered transitive dep
2507 af.transitiveDep = false
2508 vctx.filesInfo = append(vctx.filesInfo, af)
2509 return true // track transitive dependencies
2510 }
2511 } else if cc.IsHeaderDepTag(depTag) {
2512 // nothing
2513 } else if java.IsJniDepTag(depTag) {
2514 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2515 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2516 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2517 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2518 }
2519 } else if rust.IsDylibDepTag(depTag) {
2520 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2521 af := apexFileForRustLibrary(ctx, rustm)
2522 af.transitiveDep = true
2523 vctx.filesInfo = append(vctx.filesInfo, af)
2524 return true // track transitive dependencies
2525 }
2526 } else if rust.IsRlibDepTag(depTag) {
2527 // Rlib is statically linked, but it might have shared lib
2528 // dependencies. Track them.
2529 return true
2530 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2531 // Add the contents of the bootclasspath fragment to the apex.
2532 switch child.(type) {
2533 case *java.Library, *java.SdkLibrary:
2534 javaModule := child.(javaModule)
2535 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2536 if !af.ok() {
2537 ctx.PropertyErrorf("bootclasspath_fragments",
2538 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2539 return false
2540 }
2541 vctx.filesInfo = append(vctx.filesInfo, af)
2542 return true // track transitive dependencies
2543 default:
2544 ctx.PropertyErrorf("bootclasspath_fragments",
2545 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2546 }
2547 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2548 // Add the contents of the systemserverclasspath fragment to the apex.
2549 switch child.(type) {
2550 case *java.Library, *java.SdkLibrary:
2551 af := apexFileForJavaModule(ctx, child.(javaModule))
2552 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002553 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2554 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2555 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002556 return true // track transitive dependencies
2557 default:
2558 ctx.PropertyErrorf("systemserverclasspath_fragments",
2559 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2560 }
2561 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2562 // nothing
2563 } else if depTag == android.DarwinUniversalVariantTag {
2564 // nothing
2565 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2566 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2567 }
2568 return false
2569}
2570
Jooyung Han862c0d62022-12-21 10:15:37 +09002571func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2572 // TODO(b/263308293) remove this
2573 if a.properties.IsCoverageVariant {
2574 return false
2575 }
2576 // TODO(b/263308515) remove this
2577 if a.testApex {
2578 return false
2579 }
2580 // TODO(b/263309864) remove this
2581 if a.Host() {
2582 return false
2583 }
2584 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2585 return false
2586 }
2587 return true
2588}
2589
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002590// Creates build rules for an APEX. It consists of the following major steps:
2591//
2592// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2593// 2) traverse the dependency tree to collect apexFile structs from them.
2594// 3) some fields in apexBundle struct are configured
2595// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002596func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002597 ////////////////////////////////////////////////////////////////////////////////////////////
2598 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002599 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002600 return
2601 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002602 ////////////////////////////////////////////////////////////////////////////////////////////
2603 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002604 // Collect the module directory for IDE info in java/jdeps.go.
2605 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2606
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002607 // TODO(jiyong): do this using WalkPayloadDeps
2608 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002609 vctx := visitorContext{
2610 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2611 checkDuplicate: a.shouldCheckDuplicate(ctx),
2612 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002613 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002614 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002615 if a.privateKeyFile == nil {
Nicolas Geoffray036ff9a2023-05-15 10:46:38 +01002616 if ctx.Config().AllowMissingDependencies() {
2617 // TODO(b/266099037): a better approach for slim manifests.
2618 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2619 // Create placeholder paths for later stages that expect to see those paths,
2620 // though they won't be used.
2621 var unusedPath = android.PathForModuleOut(ctx, "nonexistentprivatekey")
2622 ctx.Build(pctx, android.BuildParams{
2623 Rule: android.ErrorRule,
2624 Output: unusedPath,
2625 Args: map[string]string{
2626 "error": "Private key not available",
2627 },
2628 })
2629 a.privateKeyFile = unusedPath
2630 } else {
2631 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
2632 return
2633 }
2634 }
2635
2636 if a.publicKeyFile == nil {
2637 if ctx.Config().AllowMissingDependencies() {
2638 // TODO(b/266099037): a better approach for slim manifests.
2639 ctx.AddMissingDependencies([]string{String(a.overridableProperties.Key)})
2640 // Create placeholder paths for later stages that expect to see those paths,
2641 // though they won't be used.
2642 var unusedPath = android.PathForModuleOut(ctx, "nonexistentpublickey")
2643 ctx.Build(pctx, android.BuildParams{
2644 Rule: android.ErrorRule,
2645 Output: unusedPath,
2646 Args: map[string]string{
2647 "error": "Public key not available",
2648 },
2649 })
2650 a.publicKeyFile = unusedPath
2651 } else {
2652 ctx.PropertyErrorf("key", "public_key for %q could not be found", String(a.overridableProperties.Key))
2653 return
2654 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002655 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002656
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002657 ////////////////////////////////////////////////////////////////////////////////////////////
2658 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002659 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002660 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002661
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002662 a.setApexTypeAndSuffix(ctx)
2663 a.setPayloadFsType(ctx)
2664 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002665 if a.properties.ApexType != zipApex {
2666 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2667 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002668
2669 ////////////////////////////////////////////////////////////////////////////////////////////
2670 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002671 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002672 if a.properties.ApexType == flattenedApex {
2673 a.buildFlattenedApex(ctx)
2674 } else {
2675 a.buildUnflattenedApex(ctx)
2676 }
Jiyong Park956305c2020-01-09 12:32:06 +09002677 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002678 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002679
2680 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2681 if a.installable() {
2682 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2683 // along with other ordinary files. (Note that this is done by apexer for
2684 // non-flattened APEXes)
2685 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2686
2687 // Place the public key as apex_pubkey. This is also done by apexer for
2688 // non-flattened APEXes case.
2689 // TODO(jiyong): Why do we need this CP rule?
2690 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2691 ctx.Build(pctx, android.BuildParams{
2692 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002693 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002694 Output: copiedPubkey,
2695 })
2696 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2697 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002698}
2699
Paul Duffincc33ec82021-04-25 23:14:55 +01002700// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2701// the bootclasspath_fragment contributes to the apex.
2702func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2703 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2704 var filesToAdd []apexFile
2705
satayev3db35472021-05-06 23:59:58 +01002706 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002707 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2708 filesToAdd = append(filesToAdd, *af)
2709 }
satayev3db35472021-05-06 23:59:58 +01002710
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002711 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
Jiakai Zhangbc698cd2023-05-08 16:28:38 +00002712 if pathInApex != "" {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002713 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2714 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2715
2716 if pathOnHost != nil {
2717 // We need to copy the profile to a temporary path with the right filename because the apexer
2718 // will take the filename as is.
2719 ctx.Build(pctx, android.BuildParams{
2720 Rule: android.Cp,
2721 Input: pathOnHost,
2722 Output: tempPath,
2723 })
2724 } else {
2725 // At this point, the boot image profile cannot be generated. It is probably because the boot
2726 // image profile source file does not exist on the branch, or it is not available for the
2727 // current build target.
2728 // However, we cannot enforce the boot image profile to be generated because some build
2729 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2730 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2731 // only if the APEX is being built.
2732 ctx.Build(pctx, android.BuildParams{
2733 Rule: android.ErrorRule,
2734 Output: tempPath,
2735 Args: map[string]string{
2736 "error": "Boot image profile cannot be generated",
2737 },
2738 })
2739 }
2740
2741 androidMkModuleName := filepath.Base(pathInApex)
2742 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2743 filesToAdd = append(filesToAdd, af)
2744 }
2745
Paul Duffincc33ec82021-04-25 23:14:55 +01002746 return filesToAdd
2747}
2748
satayevb98371c2021-06-15 16:49:50 +01002749// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2750// the module contributes to the apex; or nil if the proto config was not generated.
2751func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2752 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2753 if !info.ClasspathFragmentProtoGenerated {
2754 return nil
2755 }
2756 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2757 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2758 return &af
satayev14e49132021-05-17 21:03:07 +01002759}
2760
Paul Duffincc33ec82021-04-25 23:14:55 +01002761// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2762// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002763func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2764 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2765
2766 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2767 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002768 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2769 if err != nil {
2770 ctx.ModuleErrorf("%s", err)
2771 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002772
2773 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2774 // bootclasspath_fragment.
2775 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2776 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002777}
2778
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002779///////////////////////////////////////////////////////////////////////////////////////////////////
2780// Factory functions
2781//
2782
2783func newApexBundle() *apexBundle {
2784 module := &apexBundle{}
2785
2786 module.AddProperties(&module.properties)
2787 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002788 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002789 module.AddProperties(&module.overridableProperties)
2790
2791 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2792 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002793 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002794 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002795 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002796 return module
2797}
2798
Paul Duffineb8051d2021-10-18 17:49:39 +01002799func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002800 bundle := newApexBundle()
2801 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002802 return bundle
2803}
2804
2805// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2806// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002807func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002808 bundle := newApexBundle()
2809 bundle.testApex = true
2810 return bundle
2811}
2812
2813// apex packages other modules into an APEX file which is a packaging format for system-level
2814// components like binaries, shared libraries, etc.
2815func BundleFactory() android.Module {
2816 return newApexBundle()
2817}
2818
2819type Defaults struct {
2820 android.ModuleBase
2821 android.DefaultsModuleBase
2822}
2823
2824// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002825func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002826 module := &Defaults{}
2827
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002828 module.AddProperties(
2829 &apexBundleProperties{},
2830 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002831 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002832 &overridableProperties{},
2833 )
2834
2835 android.InitDefaultsModule(module)
2836 return module
2837}
2838
2839type OverrideApex struct {
2840 android.ModuleBase
2841 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002842 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002843}
2844
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002845func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002846 // All the overrides happen in the base module.
2847}
2848
2849// override_apex is used to create an apex module based on another apex module by overriding some of
2850// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002851func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002852 m := &OverrideApex{}
2853
2854 m.AddProperties(&overridableProperties{})
2855
2856 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2857 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002858 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002859 return m
2860}
2861
Wei Li1c66fc72022-05-09 23:59:14 -07002862func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2863 if ctx.ModuleType() != "override_apex" {
2864 return
2865 }
2866
2867 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2868 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2869 if !baseApexExists {
2870 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2871 }
2872
2873 a, baseModuleIsApex := baseModule.(*apexBundle)
2874 if !baseModuleIsApex {
2875 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2876 }
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002877 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
Wei Li1c66fc72022-05-09 23:59:14 -07002878
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002879 // We just want the name, not module reference.
2880 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2881 attrs.Base_apex_name = &baseApexName
2882
Wei Li1c66fc72022-05-09 23:59:14 -07002883 for _, p := range o.GetProperties() {
2884 overridableProperties, ok := p.(*overridableProperties)
2885 if !ok {
2886 continue
2887 }
Wei Li40f98732022-05-20 22:08:11 -07002888
2889 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2890 // After it is converted in convertWithBp2build(baseApex, ctx),
2891 // the attrs.Manifest.Value.Label is the file path relative to the directory
2892 // of base apex. So the following code converts it to a label that looks like
2893 // <package of base apex>:<path of manifest file> if base apex and override
2894 // apex are not in the same package.
2895 baseApexPackage := ctx.OtherModuleDir(a)
2896 overrideApexPackage := ctx.ModuleDir()
2897 if baseApexPackage != overrideApexPackage {
2898 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2899 }
2900
Wei Li1c66fc72022-05-09 23:59:14 -07002901 // Key
2902 if overridableProperties.Key != nil {
2903 attrs.Key = bazel.LabelAttribute{}
2904 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2905 }
2906
2907 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002908 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002909 // If overridableProperties.Certificate is nil, clear this out as
2910 // well with zeroed structs, so the override_apex does not use the
2911 // base apex's certificate.
2912 attrs.Certificate = bazel.LabelAttribute{}
2913 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002914 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002915 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002916 }
2917
2918 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002919 if overridableProperties.Prebuilts != nil {
2920 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2921 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2922 }
Wei Li1c66fc72022-05-09 23:59:14 -07002923
2924 // Compressible
2925 if overridableProperties.Compressible != nil {
2926 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2927 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002928
2929 // Package name
2930 //
2931 // e.g. com.android.adbd's package name is com.android.adbd, but
2932 // com.google.android.adbd overrides the package name to com.google.android.adbd
2933 //
2934 // TODO: this can be overridden from the product configuration, see
2935 // getOverrideManifestPackageName and
2936 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2937 //
2938 // Instead of generating the BUILD files differently based on the product config
2939 // at the point of conversion, this should be handled by the BUILD file loading
2940 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2941 if overridableProperties.Package_name != "" {
2942 attrs.Package_name = &overridableProperties.Package_name
2943 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002944
2945 // Logging parent
2946 if overridableProperties.Logging_parent != "" {
2947 attrs.Logging_parent = &overridableProperties.Logging_parent
2948 }
Wei Li1c66fc72022-05-09 23:59:14 -07002949 }
2950
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002951 commonAttrs.Name = o.Name()
2952
2953 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07002954}
2955
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002956///////////////////////////////////////////////////////////////////////////////////////////////////
2957// Vality check routines
2958//
2959// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2960// certain conditions are not met.
2961//
2962// TODO(jiyong): move these checks to a separate go file.
2963
satayevad991492021-12-03 18:58:32 +00002964var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2965
Spandan Dasa5f39a12022-08-05 02:35:52 +00002966// Ensures that min_sdk_version of the included modules are equal or less than the min_sdk_version
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002967// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002968func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002969 if a.testApex || a.vndkApex {
2970 return
2971 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002972 // apexBundle::minSdkVersion reports its own errors.
2973 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002974 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002975}
2976
Albert Martineefabcf2022-03-21 20:11:16 +00002977// Returns apex's min_sdk_version string value, honoring overrides
2978func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2979 // Only override the minSdkVersion value on Apexes which already specify
2980 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2981 // min_sdk_version value is lower than the one to override with.
Liz Kammerbd58e742023-05-11 15:58:13 +00002982 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.properties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002983 if minApiLevel.IsNone() {
2984 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002985 }
2986
Colin Cross56534df2022-10-04 09:58:58 -07002987 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2988 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2989 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2990 minApiLevel = overrideApiLevel
2991 }
2992
2993 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002994}
2995
2996// Returns apex's min_sdk_version SdkSpec, honoring overrides
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002997func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2998 return a.minSdkVersion(ctx)
satayevad991492021-12-03 18:58:32 +00002999}
3000
Albert Martineefabcf2022-03-21 20:11:16 +00003001// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00003002func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00003003 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
3004}
3005
3006// Construct ApiLevel object from min_sdk_version string value
3007func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
3008 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09003009 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003010 }
Albert Martineefabcf2022-03-21 20:11:16 +00003011 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003012 if err != nil {
3013 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
3014 return android.NoneApiLevel
3015 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003016 return apiLevel
3017}
3018
3019// Ensures that a lib providing stub isn't statically linked
3020func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
3021 // Practically, we only care about regular APEXes on the device.
3022 if ctx.Host() || a.testApex || a.vndkApex {
3023 return
3024 }
3025
3026 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
3027
3028 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3029 if ccm, ok := to.(*cc.Module); ok {
3030 apexName := ctx.ModuleName()
3031 fromName := ctx.OtherModuleName(from)
3032 toName := ctx.OtherModuleName(to)
3033
3034 // If `to` is not actually in the same APEX as `from` then it does not need
3035 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003036 //
3037 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003038 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3039 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3040 return false
3041 }
3042
3043 // The dynamic linker and crash_dump tool in the runtime APEX is the only
3044 // exception to this rule. It can't make the static dependencies dynamic
3045 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09003046 // Same rule should be applied to linkerconfig, because it should be executed
3047 // only with static linked libraries before linker is available with ld.config.txt
3048 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003049 return false
3050 }
3051
3052 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
3053 if isStubLibraryFromOtherApex && !externalDep {
3054 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
3055 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
3056 }
3057
3058 }
3059 return true
3060 })
3061}
3062
satayevb98371c2021-06-15 16:49:50 +01003063// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003064func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
3065 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00003066 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003067 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
3068 }
Jiyong Park1bc84122021-06-22 20:23:05 +09003069 if a.UsePlatformApis() {
3070 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
3071 }
Jooyung Handfc864c2023-03-20 18:19:07 +09003072 if proptools.Bool(a.properties.Use_vndk_as_stable) {
3073 ctx.PropertyErrorf("use_vndk_as_stable", "updatable APEXes can't use external VNDK libs")
Daniel Norman69109112021-12-02 12:52:42 -08003074 }
Jiyong Parkf4020582021-11-29 12:37:10 +09003075 if a.FutureUpdatable() {
3076 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
3077 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003078 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01003079 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003080 }
3081}
3082
satayevb98371c2021-06-15 16:49:50 +01003083// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
3084func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
3085 ctx.VisitDirectDeps(func(module android.Module) {
3086 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
3087 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
3088 if !info.ClasspathFragmentProtoGenerated {
3089 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
3090 }
3091 }
3092 })
3093}
3094
3095// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003096func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003097 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3098 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003099 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3100 tag := ctx.OtherModuleDependencyTag(module)
3101 switch tag {
3102 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003103 if m, ok := module.(interface {
3104 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3105 }); ok {
3106 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003107 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3108 }
3109 }
3110 }
3111 })
3112}
3113
satayevb98371c2021-06-15 16:49:50 +01003114// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003115func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3116 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3117 if ctx.Host() || a.testApex || a.vndkApex {
3118 return
3119 }
3120
3121 // Because APEXes targeting other than system/system_ext partitions can't set
3122 // apex_available, we skip checks for these APEXes
3123 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3124 return
3125 }
3126
3127 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3128 // Requiring them and their transitive depencies with apex_available is not right
3129 // because they just add noise.
3130 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3131 return
3132 }
3133
3134 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3135 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3136 if externalDep {
3137 return false
3138 }
3139
3140 apexName := ctx.ModuleName()
Sam Delmericoca816532023-06-02 14:09:50 -04003141 for _, props := range ctx.Module().GetProperties() {
3142 if apexProps, ok := props.(*apexBundleProperties); ok {
3143 if apexProps.Apex_available_name != nil {
3144 apexName = *apexProps.Apex_available_name
3145 }
3146 }
3147 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003148 fromName := ctx.OtherModuleName(from)
3149 toName := ctx.OtherModuleName(to)
3150
3151 // If `to` is not actually in the same APEX as `from` then it does not need
3152 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003153 //
3154 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003155 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3156 // As soon as the dependency graph crosses the APEX boundary, don't go
3157 // further.
3158 return false
3159 }
3160
3161 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3162 return true
3163 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003164 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3165 "\n\nDependency path:%s\n\n"+
3166 "Consider adding %q to 'apex_available' property of %q",
3167 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003168 // Visit this module's dependencies to check and report any issues with their availability.
3169 return true
3170 })
3171}
3172
Jiyong Park192600a2021-08-03 07:52:17 +00003173// checkStaticExecutable ensures that executables in an APEX are not static.
3174func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003175 // No need to run this for host APEXes
3176 if ctx.Host() {
3177 return
3178 }
3179
Jiyong Park192600a2021-08-03 07:52:17 +00003180 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3181 if ctx.OtherModuleDependencyTag(module) != executableTag {
3182 return
3183 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003184
3185 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003186 apex := a.ApexVariationName()
3187 exec := ctx.OtherModuleName(module)
3188 if isStaticExecutableAllowed(apex, exec) {
3189 return
3190 }
3191 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3192 }
3193 })
3194}
3195
3196// A small list of exceptions where static executables are allowed in APEXes.
3197func isStaticExecutableAllowed(apex string, exec string) bool {
3198 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003199 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003200 "linker",
3201 "linkerconfig",
3202 },
3203 }
3204 execNames, ok := m[apex]
3205 return ok && android.InList(exec, execNames)
3206}
3207
braleeb0c1f0c2021-06-07 22:49:13 +08003208// Collect information for opening IDE project files in java/jdeps.go.
3209func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00003210 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
3211 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
3212 dpInfo.Deps = append(dpInfo.Deps, a.properties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003213 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3214}
3215
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003216var (
3217 apexAvailBaseline = makeApexAvailableBaseline()
3218 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3219)
3220
Colin Cross440e0d02020-06-11 11:32:11 -07003221func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003222 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003223 moduleName = normalizeModuleName(moduleName)
3224
Colin Cross440e0d02020-06-11 11:32:11 -07003225 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003226 return true
3227 }
3228
3229 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003230 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003231 return true
3232 }
3233
3234 return false
3235}
3236
3237func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003238 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3239 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003240 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003241 if strings.HasPrefix(moduleName, "libclang_rt.") {
3242 // This module has many arch variants that depend on the product being built.
3243 // We don't want to list them all
3244 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003245 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003246 if strings.HasPrefix(moduleName, "androidx.") {
3247 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3248 moduleName = "androidx"
3249 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003250 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003251}
3252
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003253// Transform the map of apex -> modules to module -> apexes.
3254func invertApexBaseline(m map[string][]string) map[string][]string {
3255 r := make(map[string][]string)
3256 for apex, modules := range m {
3257 for _, module := range modules {
3258 r[module] = append(r[module], apex)
3259 }
3260 }
3261 return r
3262}
3263
3264// Retrieve the baseline of apexes to which the supplied module belongs.
3265func BaselineApexAvailable(moduleName string) []string {
3266 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3267}
3268
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003269// This is a map from apex to modules, which overrides the apex_available setting for that
3270// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003271// TODO(b/147364041): remove this
3272func makeApexAvailableBaseline() map[string][]string {
3273 // The "Module separator"s below are employed to minimize merge conflicts.
3274 m := make(map[string][]string)
3275 //
3276 // Module separator
3277 //
3278 m["com.android.appsearch"] = []string{
3279 "icing-java-proto-lite",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003280 }
3281 //
3282 // Module separator
3283 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003284 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003285 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003286 }
3287 //
3288 // Module separator
3289 //
Spandan Das072f7bc2023-05-05 21:06:23 +00003290 m["com.android.cellbroadcast"] = []string{}
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003291 //
3292 // Module separator
3293 //
3294 m["com.android.extservices"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003295 "ExtServices-core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003296 "libtextclassifier-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003297 "textclassifier-statsd",
3298 "TextClassifierNotificationLibNoManifest",
3299 "TextClassifierServiceLibNoManifest",
3300 }
3301 //
3302 // Module separator
3303 //
3304 m["com.android.neuralnetworks"] = []string{
3305 "android.hardware.neuralnetworks@1.0",
3306 "android.hardware.neuralnetworks@1.1",
3307 "android.hardware.neuralnetworks@1.2",
3308 "android.hardware.neuralnetworks@1.3",
3309 "android.hidl.allocator@1.0",
3310 "android.hidl.memory.token@1.0",
3311 "android.hidl.memory@1.0",
3312 "android.hidl.safe_union@1.0",
3313 "libarect",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003314 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003315 }
3316 //
3317 // Module separator
3318 //
3319 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003320 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003321 }
3322 //
3323 // Module separator
3324 //
3325 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003326 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003327 }
3328 //
3329 // Module separator
3330 //
3331 m["com.android.mediaprovider"] = []string{
3332 "MediaProvider",
3333 "MediaProviderGoogle",
3334 "fmtlib_ndk",
3335 "libbase_ndk",
3336 "libfuse",
3337 "libfuse_jni",
3338 }
3339 //
3340 // Module separator
3341 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003342 m["com.android.runtime"] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003343 "libc_aeabi",
3344 "libc_bionic",
3345 "libc_bionic_ndk",
3346 "libc_bootstrap",
3347 "libc_common",
3348 "libc_common_shared",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003349 "libc_dns",
3350 "libc_dynamic_dispatch",
3351 "libc_fortify",
3352 "libc_freebsd",
3353 "libc_freebsd_large_stack",
3354 "libc_gdtoa",
3355 "libc_init_dynamic",
3356 "libc_init_static",
3357 "libc_jemalloc_wrapper",
3358 "libc_netbsd",
3359 "libc_nomalloc",
3360 "libc_nopthread",
3361 "libc_openbsd",
3362 "libc_openbsd_large_stack",
3363 "libc_openbsd_ndk",
3364 "libc_pthread",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003365 "libc_syscalls",
3366 "libc_tzcode",
3367 "libc_unwind_static",
3368 "libdebuggerd",
3369 "libdebuggerd_common_headers",
3370 "libdebuggerd_handler_core",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003371 "libdl_static",
3372 "libjemalloc5",
3373 "liblinker_main",
3374 "liblinker_malloc",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003375 "liblzma",
3376 "libprocinfo",
3377 "libpropertyinfoparser",
3378 "libscudo",
3379 "libstdc++",
3380 "libsystemproperties",
3381 "libtombstoned_client_static",
3382 "libunwindstack",
3383 "libz",
3384 "libziparchive",
3385 }
3386 //
3387 // Module separator
3388 //
3389 m["com.android.tethering"] = []string{
3390 "android.hardware.tetheroffload.config-V1.0-java",
3391 "android.hardware.tetheroffload.control-V1.0-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003392 "net-utils-framework-common",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003393 }
3394 //
3395 // Module separator
3396 //
3397 m["com.android.wifi"] = []string{
3398 "PlatformProperties",
3399 "android.hardware.wifi-V1.0-java",
3400 "android.hardware.wifi-V1.0-java-constants",
3401 "android.hardware.wifi-V1.1-java",
3402 "android.hardware.wifi-V1.2-java",
3403 "android.hardware.wifi-V1.3-java",
3404 "android.hardware.wifi-V1.4-java",
3405 "android.hardware.wifi.hostapd-V1.0-java",
3406 "android.hardware.wifi.hostapd-V1.1-java",
3407 "android.hardware.wifi.hostapd-V1.2-java",
3408 "android.hardware.wifi.supplicant-V1.0-java",
3409 "android.hardware.wifi.supplicant-V1.1-java",
3410 "android.hardware.wifi.supplicant-V1.2-java",
3411 "android.hardware.wifi.supplicant-V1.3-java",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003412 "bouncycastle-unbundled",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003413 "framework-wifi-util-lib",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003414 "ksoap2",
3415 "libnanohttpd",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003416 "wifi-lite-protos",
3417 "wifi-nano-protos",
3418 "wifi-service-pre-jarjar",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003419 }
3420 //
3421 // Module separator
3422 //
3423 m[android.AvailableToAnyApex] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003424 "libprofile-clang-extras",
3425 "libprofile-clang-extras_ndk",
3426 "libprofile-extras",
3427 "libprofile-extras_ndk",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003428 }
3429 return m
3430}
3431
3432func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003433 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3434 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003435}
3436
Spandan Dasf14e2542021-11-12 00:01:37 +00003437func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3438 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3439 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003440 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003441 With("name", jar).
3442 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3443 Because(jar +
3444 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003445 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003446 " 1. If the offending code is from a statically linked library, consider " +
3447 "removing that dependency and using an alternative already in the " +
3448 "bootclasspath, or perhaps a shared library." +
3449 " 2. Move the offending code into an allowed package.\n" +
3450 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3451 "health implications of bundling that code, particularly if the offending jar " +
3452 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003453
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003454 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003455 }
3456 return rules
3457}
3458
Anton Hanssone1b18362021-12-23 15:05:38 +00003459// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003460// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003461func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003462 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003463 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003464 "android.net.ssl",
3465 "com.android.org.conscrypt",
3466 },
Wei Li40f98732022-05-20 22:08:11 -07003467 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003468 "android.media",
3469 },
3470 }
3471}
3472
Anton Hanssone1b18362021-12-23 15:05:38 +00003473// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003474// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003475func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003476 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003477 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003478 "android.provider",
3479 },
Wei Li40f98732022-05-20 22:08:11 -07003480 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003481 "android.permission",
3482 "android.app.role",
3483 "com.android.permission",
3484 "com.android.role",
3485 },
Wei Li40f98732022-05-20 22:08:11 -07003486 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003487 "android.os.ext",
3488 },
Wei Li40f98732022-05-20 22:08:11 -07003489 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003490 "android.app",
3491 "android.os",
3492 "android.util",
3493 "com.android.internal.statsd",
3494 "com.android.server.stats",
3495 },
Wei Li40f98732022-05-20 22:08:11 -07003496 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003497 "com.android.server.wifi",
3498 "com.android.wifi.x",
3499 "android.hardware.wifi",
3500 "android.net.wifi",
3501 },
Wei Li40f98732022-05-20 22:08:11 -07003502 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003503 "android.net",
3504 },
3505 }
3506}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003507
3508// For Bazel / bp2build
3509
3510type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003511 Manifest bazel.LabelAttribute
3512 Android_manifest bazel.LabelAttribute
3513 File_contexts bazel.LabelAttribute
Jingwen Chena8623da2023-03-28 13:05:02 +00003514 Canned_fs_config bazel.LabelAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003515 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003516 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3517 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003518 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003519 Updatable bazel.BoolAttribute
3520 Installable bazel.BoolAttribute
3521 Binaries bazel.LabelListAttribute
3522 Prebuilts bazel.LabelListAttribute
3523 Native_shared_libs_32 bazel.LabelListAttribute
3524 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003525 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003526 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003527 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003528 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003529 Base_apex_name *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003530}
3531
3532type convertedNativeSharedLibs struct {
3533 Native_shared_libs_32 bazel.LabelListAttribute
3534 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003535}
3536
Liz Kammerb83b7b02022-12-21 14:53:41 -05003537const (
3538 minSdkVersionPropName = "Min_sdk_version"
3539)
3540
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003541// ConvertWithBp2build performs bp2build conversion of an apex
3542func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003543 // We only convert apex and apex_test modules at this time
3544 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003545 return
3546 }
3547
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003548 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
3549 commonAttrs.Name = a.Name()
Yu Liu4c212ce2022-10-14 12:20:20 -07003550 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003551}
3552
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003553func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties, android.CommonAttributes) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003554 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003555 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003556
3557 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003558 if a.properties.AndroidManifest != nil {
3559 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003560 }
3561
3562 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003563 if a.properties.File_contexts == nil {
3564 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3565 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3566 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3567 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003568 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003569 } else {
3570 // File_contexts is a file
3571 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003572 }
3573
Jingwen Chena8623da2023-03-28 13:05:02 +00003574 var cannedFsConfigAttribute bazel.LabelAttribute
3575 if a.properties.Canned_fs_config != nil {
3576 cannedFsConfigAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Canned_fs_config))
3577 }
3578
Cole Faust912bc882023-03-08 12:29:50 -08003579 productVariableProps := android.ProductVariableProperties(ctx, a)
Albert Martineefabcf2022-03-21 20:11:16 +00003580 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3581 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003582 var minSdkVersion bazel.StringAttribute
Liz Kammerbd58e742023-05-11 15:58:13 +00003583 if a.properties.Min_sdk_version != nil {
3584 minSdkVersion.SetValue(*a.properties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003585 }
3586 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3587 for c, p := range props {
3588 if val, ok := p.(*string); ok {
3589 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3590 }
3591 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003592 }
3593
3594 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003595 if a.overridableProperties.Key != nil {
3596 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003597 }
3598
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003599 // Certificate
3600 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003601
Yu Liu4ae55d12022-01-05 17:17:23 -08003602 nativeSharedLibs := &convertedNativeSharedLibs{
3603 Native_shared_libs_32: bazel.LabelListAttribute{},
3604 Native_shared_libs_64: bazel.LabelListAttribute{},
3605 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003606
3607 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3608 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3609 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3610 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3611 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003612 if a.CompileMultilib() != nil {
3613 compileMultilib = *a.CompileMultilib()
3614 }
3615
3616 // properties.Native_shared_libs is treated as "both"
3617 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3618 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3619 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3620 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3621 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003622
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003623 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003624 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3625 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3626
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003627 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003628 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003629
Yu Liu4c212ce2022-10-14 12:20:20 -07003630 var testsAttrs bazel.LabelListAttribute
3631 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3632 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3633 testsAttrs = bazel.MakeLabelListAttribute(tests)
3634 }
3635
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003636 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003637 if a.properties.Updatable != nil {
3638 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003639 }
3640
3641 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003642 if a.properties.Installable != nil {
3643 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003644 }
3645
Wei Lif034cb42022-01-19 15:54:31 -08003646 var compressibleAttribute bazel.BoolAttribute
3647 if a.overridableProperties.Compressible != nil {
3648 compressibleAttribute.Value = a.overridableProperties.Compressible
3649 }
3650
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003651 var packageName *string
3652 if a.overridableProperties.Package_name != "" {
3653 packageName = &a.overridableProperties.Package_name
3654 }
3655
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003656 var loggingParent *string
3657 if a.overridableProperties.Logging_parent != "" {
3658 loggingParent = &a.overridableProperties.Logging_parent
3659 }
3660
Wei Li1c66fc72022-05-09 23:59:14 -07003661 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003662 Manifest: manifestLabelAttribute,
3663 Android_manifest: androidManifestLabelAttribute,
3664 File_contexts: fileContextsLabelAttribute,
Jingwen Chena8623da2023-03-28 13:05:02 +00003665 Canned_fs_config: cannedFsConfigAttribute,
Yu Liu4ae55d12022-01-05 17:17:23 -08003666 Min_sdk_version: minSdkVersion,
3667 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003668 Certificate: certificate,
3669 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003670 Updatable: updatableAttribute,
3671 Installable: installableAttribute,
3672 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3673 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3674 Binaries: binariesLabelListAttribute,
3675 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003676 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003677 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003678 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003679 Tests: testsAttrs,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003680 }
3681
3682 props := bazel.BazelTargetModuleProperties{
3683 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003684 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003685 }
3686
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003687 commonAttrs := android.CommonAttributes{}
3688 if a.testApex {
3689 commonAttrs.Testonly = proptools.BoolPtr(true)
Spandan Dasa43ae132023-05-08 18:33:16 +00003690 // Set the api_domain of the test apex
3691 attrs.Base_apex_name = proptools.StringPtr(cc.GetApiDomain(a.Name()))
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003692 }
3693
3694 return attrs, props, commonAttrs
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003695}
Yu Liu4ae55d12022-01-05 17:17:23 -08003696
3697// The following conversions are based on this table where the rows are the compile_multilib
3698// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3699// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3700// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3701// should not be compiled.
3702// multib/compile_multilib, 32, 64, both, first
3703// 32, 32/32, none/none, 32/32, none/32
3704// 64, none/none, 64/none, 64/none, 64/none
3705// both, 32/32, 64/none, 32&64/32, 64/32
3706// first, 32/32, 64/none, 64/32, 64/32
3707
3708func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3709 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3710 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3711 switch compileMultilb {
3712 case "both", "32":
3713 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3714 case "first":
3715 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3716 case "64":
3717 // Incompatible, ignore
3718 default:
3719 invalidCompileMultilib(ctx, compileMultilb)
3720 }
3721}
3722
3723func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3724 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3725 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3726 switch compileMultilb {
3727 case "both", "64", "first":
3728 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3729 case "32":
3730 // Incompatible, ignore
3731 default:
3732 invalidCompileMultilib(ctx, compileMultilb)
3733 }
3734}
3735
3736func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3737 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3738 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3739 switch compileMultilb {
3740 case "both":
3741 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3742 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3743 case "first":
3744 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3745 case "32":
3746 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3747 case "64":
3748 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3749 default:
3750 invalidCompileMultilib(ctx, compileMultilb)
3751 }
3752}
3753
3754func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3755 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3756 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3757 switch compileMultilb {
3758 case "both", "first":
3759 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3760 case "32":
3761 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3762 case "64":
3763 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3764 default:
3765 invalidCompileMultilib(ctx, compileMultilb)
3766 }
3767}
3768
3769func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3770 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3771 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3772}
3773
3774func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3775 list := bazel.LabelListAttribute{}
3776 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3777 nativeSharedLibs.Native_shared_libs_32.Append(list)
3778}
3779
3780func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3781 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3782 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3783}
3784
3785func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3786 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3787 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3788}
3789
3790func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3791 labelListAttr *bazel.LabelListAttribute) {
3792 list := bazel.LabelListAttribute{}
3793 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3794 labelListAttr.Append(list)
3795}
3796
3797func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3798 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3799}
Spandan Dasf57a9662023-04-12 19:05:49 +00003800
3801func (a *apexBundle) IsTestApex() bool {
3802 return a.testApex
3803}