blob: b2ca6c480e5c3b8c6c734af02a042fe74b857a47 [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
Jiyong Park038e8522021-12-13 23:56:35 +0900106 // Path to the canned fs config file for customizing file's uid/gid/mod/capabilities. The
107 // format is /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where path_or_glob is a
108 // path or glob pattern for a file or set of files, uid/gid are numerial values of user ID
109 // and group ID, mode is octal value for the file mode, and cap is hexadecimal value for the
110 // capability. If this property is not set, or a file is missing in the file, default config
111 // is used.
112 Canned_fs_config *string `android:"path"`
113
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900114 ApexNativeDependencies
115
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900116 Multilib apexMultilibProperties
117
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000118 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
119 Rros []string
120
Anton Hanssone7545852023-02-24 11:06:07 +0000121 // List of bootclasspath fragments that are embedded inside this APEX bundle.
122 Bootclasspath_fragments []string
123
124 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
125 Systemserverclasspath_fragments []string
126
127 // List of java libraries that are embedded inside this APEX bundle.
128 Java_libs []string
129
Sundong Ahn80c04892021-11-23 00:57:19 +0000130 // List of sh binaries that are embedded inside this APEX bundle.
131 Sh_binaries []string
132
Paul Duffin3abc1742021-03-15 19:32:23 +0000133 // List of platform_compat_config files that are embedded inside this APEX bundle.
134 Compat_configs []string
135
Jiyong Park12a719c2021-01-07 15:31:24 +0900136 // List of filesystem images that are embedded inside this APEX bundle.
137 Filesystems []string
138
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900139 // Whether this APEX is considered updatable or not. When set to true, this will enforce
140 // additional rules for making sure that the APEX is truly updatable. To be updatable,
141 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000142 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900143 Updatable *bool
144
Jiyong Parkf4020582021-11-29 12:37:10 +0900145 // Marks that this APEX is designed to be updatable in the future, although it's not
146 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
147 // updatable APEXes. Currently, this disables the size optimization, so that the size of
148 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
149 // false.
150 Future_updatable *bool
151
Jiyong Park1bc84122021-06-22 20:23:05 +0900152 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
153 // false`. Default is false.
154 Platform_apis *bool
155
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900156 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
157 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900158 Installable *bool
159
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900160 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
161 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
162 Use_vndk_as_stable *bool
163
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900164 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
165 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
166 // container. When set to zip, contents are stored in a zip container directly. This type is
167 // mostly for host-side debugging. When set to both, the two types are both built. Default
168 // is 'image'.
169 Payload_type *string
170
Huang Jianan13cac632021-08-02 15:02:17 +0800171 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
172 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900173 Payload_fs_type *string
174
175 // For telling the APEX to ignore special handling for system libraries such as bionic.
176 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900177 Ignore_system_library_special_case *bool
178
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100179 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100180 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100181 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900182
183 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
184 // used in tests.
185 Test_only_unsigned_payload *bool
186
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000187 // Whenever apex should be compressed, regardless of product flag used. Should be only
188 // used in tests.
189 Test_only_force_compression *bool
190
Jooyung Han09c11ad2021-10-27 03:45:31 +0900191 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
192 // with the tool to sign payload contents.
193 Custom_sign_tool *string
194
Dennis Shenaf41bc12022-08-03 16:46:43 +0000195 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
196 // in a special way that include the digest of the lib file under /lib(64)?
197 Dynamic_common_lib_apex *bool
198
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100199 // Canonical name of this APEX bundle. Used to determine the path to the
200 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
201 // apex mutator variations. For override_apex modules, this is the name of the
202 // overridden base module.
203 ApexVariationName string `blueprint:"mutated"`
204
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900205 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900206
207 // List of sanitizer names that this APEX is enabled for
208 SanitizerNames []string `blueprint:"mutated"`
209
210 PreventInstall bool `blueprint:"mutated"`
211
212 HideFromMake bool `blueprint:"mutated"`
213
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900214 // Internal package method for this APEX. When payload_type is image, this can be either
215 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
216 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900217 ApexType apexPackaging `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900218}
219
220type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900221 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900222 Native_shared_libs []string
223
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900224 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900225 Jni_libs []string
226
Colin Cross70572ed2022-11-02 13:14:20 -0700227 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900228 Rust_dyn_libs []string
229
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900230 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900231 Binaries []string
232
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900233 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900234 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900235
236 // List of filesystem images that are embedded inside this APEX bundle.
237 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700238
239 // List of native libraries to exclude from this APEX.
240 Exclude_native_shared_libs []string
241
242 // List of JNI libraries to exclude from this APEX.
243 Exclude_jni_libs []string
244
245 // List of rust dyn libraries to exclude from this APEX.
246 Exclude_rust_dyn_libs []string
247
248 // List of native executables to exclude from this APEX.
249 Exclude_binaries []string
250
251 // List of native tests to exclude from this APEX.
252 Exclude_tests []string
253
254 // List of filesystem images to exclude from this APEX bundle.
255 Exclude_filesystems []string
256}
257
258// Merge combines another ApexNativeDependencies into this one
259func (a *ApexNativeDependencies) Merge(b ApexNativeDependencies) {
260 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs...)
261 a.Jni_libs = append(a.Jni_libs, b.Jni_libs...)
262 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
263 a.Binaries = append(a.Binaries, b.Binaries...)
264 a.Tests = append(a.Tests, b.Tests...)
265 a.Filesystems = append(a.Filesystems, b.Filesystems...)
266
267 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
268 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
269 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
270 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
271 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
272 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900273}
274
275type apexMultilibProperties struct {
276 // Native dependencies whose compile_multilib is "first"
277 First ApexNativeDependencies
278
279 // Native dependencies whose compile_multilib is "both"
280 Both ApexNativeDependencies
281
282 // Native dependencies whose compile_multilib is "prefer32"
283 Prefer32 ApexNativeDependencies
284
285 // Native dependencies whose compile_multilib is "32"
286 Lib32 ApexNativeDependencies
287
288 // Native dependencies whose compile_multilib is "64"
289 Lib64 ApexNativeDependencies
290}
291
292type apexTargetBundleProperties struct {
293 Target struct {
294 // Multilib properties only for android.
295 Android struct {
296 Multilib apexMultilibProperties
297 }
298
299 // Multilib properties only for host.
300 Host struct {
301 Multilib apexMultilibProperties
302 }
303
304 // Multilib properties only for host linux_bionic.
305 Linux_bionic struct {
306 Multilib apexMultilibProperties
307 }
308
309 // Multilib properties only for host linux_glibc.
310 Linux_glibc struct {
311 Multilib apexMultilibProperties
312 }
313 }
314}
315
Jiyong Park59140302020-12-14 18:44:04 +0900316type apexArchBundleProperties struct {
317 Arch struct {
318 Arm struct {
319 ApexNativeDependencies
320 }
321 Arm64 struct {
322 ApexNativeDependencies
323 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700324 Riscv64 struct {
325 ApexNativeDependencies
326 }
Jiyong Park59140302020-12-14 18:44:04 +0900327 X86 struct {
328 ApexNativeDependencies
329 }
330 X86_64 struct {
331 ApexNativeDependencies
332 }
333 }
334}
335
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900336// These properties can be used in override_apex to override the corresponding properties in the
337// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900338type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900339 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900340 Apps []string
341
Daniel Norman5a3ce132021-08-26 15:44:43 -0700342 // List of prebuilt files that are embedded inside this APEX bundle.
343 Prebuilts []string
344
markchien7c803b82021-08-26 22:10:06 +0800345 // List of BPF programs inside this APEX bundle.
346 Bpfs []string
347
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900348 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
349 // Soong). This does not completely prevent installation of the overridden binaries, but if
350 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
351 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900352 Overrides []string
353
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900354 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900355 Logging_parent string
356
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900357 // Apex Container package name. Override value for attribute package:name in
358 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900359 Package_name string
360
361 // A txt file containing list of files that are allowed to be included in this APEX.
362 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700363
364 // Name of the apex_key module that provides the private key to sign this APEX bundle.
365 Key *string
366
367 // Specifies the certificate and the private key to sign the zip container of this APEX. If
368 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
369 // as the certificate and the private key, respectively. If this is ":module", then the
370 // certificate and the private key are provided from the android_app_certificate module
371 // named "module".
372 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400373
374 // Whether this APEX can be compressed or not. Setting this property to false means this
375 // APEX will never be compressed. When set to true, APEX will be compressed if other
376 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
377 // Default: false.
378 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000379
380 // Trim against a specific Dynamic Common Lib APEX
381 Trim_against *string
zhidou133c55b2023-01-31 19:34:10 +0000382
383 // The minimum SDK version that this APEX must support at minimum. This is usually set to
384 // the SDK version that the APEX was first introduced.
385 Min_sdk_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900386}
387
388type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900389 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900390 android.ModuleBase
391 android.DefaultableModuleBase
392 android.OverridableModuleBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400393 android.BazelModuleBase
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900394 multitree.ExportableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900395
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900396 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900397 properties apexBundleProperties
398 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900399 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900400 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900401 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900402
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900403 ///////////////////////////////////////////////////////////////////////////////////////////
404 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900405
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900406 // Keys for apex_paylaod.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800407 publicKeyFile android.Path
408 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900409
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900410 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800411 containerCertificateFile android.Path
412 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900413
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900414 // Flags for special variants of APEX
415 testApex bool
416 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900417
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900418 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
419 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900420 primaryApexType bool
421
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900422 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900423 suffix string
424
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900425 // File system type of apex_payload.img
426 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900427
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900428 // Whether to create symlink to the system file instead of having a file inside the apex or
429 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900430 linkToSystemLib bool
431
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900432 // List of files to be included in this APEX. This is filled in the first part of
433 // GenerateAndroidBuildActions.
434 filesInfo []apexFile
435
Jingwen Chen29743c82023-01-25 17:49:46 +0000436 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
437 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900438
439 ///////////////////////////////////////////////////////////////////////////////////////////
440 // Outputs (final and intermediates)
441
442 // Processed apex manifest in JSONson format (for Q)
443 manifestJsonOut android.WritablePath
444
445 // Processed apex manifest in PB format (for R+)
446 manifestPbOut android.WritablePath
447
448 // Processed file_contexts files
449 fileContexts android.WritablePath
450
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900451 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900452 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900453 outputFile android.WritablePath
454
Jooyung Hana6d36672022-02-24 13:58:07 +0900455 // The built uncompressed .apex file.
456 outputApexFile android.WritablePath
457
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900458 // The built APEX file in app bundle format. This file is not directly installed to the
459 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
460 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
461 // system) to be merged into a single app bundle file that Play accepts. See
462 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
463 bundleModuleFile android.WritablePath
464
Colin Cross6340ea52021-11-04 12:01:18 -0700465 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900466 installDir android.InstallPath
467
Colin Cross6340ea52021-11-04 12:01:18 -0700468 // Path where this APEX was installed.
469 installedFile android.InstallPath
470
471 // Installed locations of symlinks for backward compatibility.
472 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900473
474 // Text file having the list of individual files that are included in this APEX. Used for
475 // debugging purpose.
476 installedFilesFile android.WritablePath
477
478 // List of module names that this APEX is including (to be shown via *-deps-info target).
479 // Used for debugging purpose.
480 android.ApexBundleDepsInfo
481
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900482 // Optional list of lint report zip files for apexes that contain java or app modules
483 lintReports android.Paths
484
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000485 isCompressed bool
486
sophiezc80a2b32020-11-12 16:39:19 +0000487 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700488 nativeApisUsedByModuleFile android.ModuleOutPath
489 nativeApisBackedByModuleFile android.ModuleOutPath
490 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800491
492 // Collect the module directory for IDE info in java/jdeps.go.
493 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900494}
495
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900496// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900497type apexFileClass int
498
Jooyung Han72bd2f82019-10-23 16:46:38 +0900499const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900500 app apexFileClass = iota
501 appSet
502 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900503 goBinary
504 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900505 nativeExecutable
506 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900507 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900508 pyBinary
509 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900510)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900511
Jingwen Chen2d37b642023-03-14 16:11:38 +0000512var (
513 classes = map[string]apexFileClass{
514 "app": app,
515 "appSet": appSet,
516 "etc": etc,
517 "goBinary": goBinary,
518 "javaSharedLib": javaSharedLib,
519 "nativeExecutable": nativeExecutable,
520 "nativeSharedLib": nativeSharedLib,
521 "nativeTest": nativeTest,
522 "pyBinary": pyBinary,
523 "shBinary": shBinary,
524 }
525)
526
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900527// apexFile represents a file in an APEX bundle. This is created during the first half of
528// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
529// of the function, this is used to create commands that copies the files into a staging directory,
530// where they are packaged into the APEX file. This struct is also used for creating Make modules
531// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900532type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900533 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000534 builtFile android.Path
535 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900536 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000537 customStem string
538 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900539
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900540 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
541 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
542 // suffix>]
543 androidMkModuleName string // becomes LOCAL_MODULE
544 class apexFileClass // becomes LOCAL_MODULE_CLASS
545 moduleDir string // becomes LOCAL_PATH
546 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
547 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
548 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
549 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900550
551 jacocoReportClassesFile android.Path // only for javalibs and apps
552 lintDepSets java.LintDepSets // only for javalibs and apps
553 certificate java.Certificate // only for apps
554 overriddenPackageName string // only for apps
555
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900556 transitiveDep bool
557 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900558
Jiyong Park57621b22021-01-20 20:33:11 +0900559 multilib string
560
Jingwen Chen2d37b642023-03-14 16:11:38 +0000561 isBazelPrebuilt bool
562 unstrippedBuiltFile android.Path
563 arch string
564
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900565 // TODO(jiyong): remove this
566 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900567}
568
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900569// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900570func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
571 ret := apexFile{
572 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900573 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900574 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900575 class: class,
576 module: module,
577 }
578 if module != nil {
579 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900580 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900581 ret.requiredModuleNames = module.RequiredModuleNames()
582 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
583 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900584 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900585 }
586 return ret
587}
588
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900589func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900590 return af.builtFile != nil && af.builtFile.String() != ""
591}
592
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900593// apexRelativePath returns the relative path of the given path from the install directory of this
594// apexFile.
595// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900596func (af *apexFile) apexRelativePath(path string) string {
597 return filepath.Join(af.installDir, path)
598}
599
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900600// path returns path of this apex file relative to the APEX root
601func (af *apexFile) path() string {
602 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900603}
604
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900605// stem returns the base filename of this apex file
606func (af *apexFile) stem() string {
607 if af.customStem != "" {
608 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900609 }
610 return af.builtFile.Base()
611}
612
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900613// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
614func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900615 var ret []string
616 for _, symlink := range af.symlinks {
617 ret = append(ret, af.apexRelativePath(symlink))
618 }
619 return ret
620}
621
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900622// availableToPlatform tests whether this apexFile is from a module that can be installed to the
623// platform.
624func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900625 if af.module == nil {
626 return false
627 }
628 if am, ok := af.module.(android.ApexModule); ok {
629 return am.AvailableFor(android.AvailableToPlatform)
630 }
631 return false
632}
633
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900634////////////////////////////////////////////////////////////////////////////////////////////////////
635// Mutators
636//
637// Brief description about mutators for APEX. The following three mutators are the most important
638// ones.
639//
640// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
641// to the (direct) dependencies of this APEX bundle.
642//
Paul Duffin949abc02020-12-08 10:34:30 +0000643// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900644// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
645// modules are marked as being included in the APEX via BuildForApex().
646//
Paul Duffin949abc02020-12-08 10:34:30 +0000647// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
648// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900649
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900650type dependencyTag struct {
651 blueprint.BaseDependencyTag
652 name string
653
654 // Determines if the dependent will be part of the APEX payload. Can be false for the
655 // dependencies to the signing key module, etc.
656 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000657
658 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
659 // replacement. This is needed because some prebuilt modules do not provide all the information
660 // needed by the apex.
661 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000662
663 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
664 // also be added as exported members of that SDK.
665 memberType android.SdkMemberType
666}
667
668func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
669 return d.memberType
670}
671
672func (d *dependencyTag) ExportMember() bool {
673 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900674}
675
Paul Duffin520917a2022-05-13 13:01:59 +0000676func (d *dependencyTag) String() string {
677 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
678}
679
680func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000681 return !d.sourceOnly
682}
683
684var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000685var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000686
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900687var (
Paul Duffin520917a2022-05-13 13:01:59 +0000688 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
689 bpfTag = &dependencyTag{name: "bpf", payload: true}
690 certificateTag = &dependencyTag{name: "certificate"}
Dennis Shene2ed70c2023-01-11 14:15:43 +0000691 dclaTag = &dependencyTag{name: "dcla"}
Paul Duffin520917a2022-05-13 13:01:59 +0000692 executableTag = &dependencyTag{name: "executable", payload: true}
693 fsTag = &dependencyTag{name: "filesystem", payload: true}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000694 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
695 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType}
Paul Duffinfcf79852022-07-20 14:18:24 +0000696 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000697 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
698 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
699 keyTag = &dependencyTag{name: "key"}
700 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
701 rroTag = &dependencyTag{name: "rro", payload: true}
702 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
703 testForTag = &dependencyTag{name: "test for"}
704 testTag = &dependencyTag{name: "test", payload: true}
705 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900706)
707
708// TODO(jiyong): shorten this function signature
709func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900710 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900711 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900712 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900713
714 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900715 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900716 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
717 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900718 }
719
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900720 // Use *FarVariation* to be able to depend on modules having conflicting variations with
721 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
722 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700723 ctx.AddFarVariationDependencies(binVariations, executableTag,
724 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
725 ctx.AddFarVariationDependencies(binVariations, testTag,
726 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
727 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
728 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
729 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
730 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
731 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
732 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
733 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
734 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900735}
736
737func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900738 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900739 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
740 } else {
741 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
742 if ctx.Os().Bionic() {
743 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
744 } else {
745 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
746 }
747 }
748}
749
Jooyung Hand045ebc2022-12-06 15:23:57 +0900750// getImageVariationPair returns a pair for the image variation name as its
751// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
752// suffix indicates the vndk version when it's vendor or product.
753// getImageVariation can simply join the result of this function to get the
754// image variation name.
755func (a *apexBundle) getImageVariationPair(deviceConfig android.DeviceConfig) (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900756 if a.vndkApex {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900757 return cc.VendorVariationPrefix, a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900758 }
759
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900760 var prefix string
761 var vndkVersion string
762 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000763 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900764 prefix = cc.VendorVariationPrefix
765 vndkVersion = deviceConfig.VndkVersion()
766 } else if a.ProductSpecific() {
767 prefix = cc.ProductVariationPrefix
768 vndkVersion = deviceConfig.ProductVndkVersion()
769 }
770 }
771 if vndkVersion == "current" {
772 vndkVersion = deviceConfig.PlatformVndkVersion()
773 }
774 if vndkVersion != "" {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900775 return prefix, vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900776 }
777
Jooyung Hand045ebc2022-12-06 15:23:57 +0900778 return android.CoreVariation, "" // The usual case
779}
780
781// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
782// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
783func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
784 prefix, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig())
785 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900786}
787
788func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900789 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
790 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
791 // each target os/architectures, appropriate dependencies are selected by their
792 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900793 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900794 imageVariation := a.getImageVariation(ctx)
795
796 a.combineProperties(ctx)
797
798 has32BitTarget := false
799 for _, target := range targets {
800 if target.Arch.ArchType.Multilib == "lib32" {
801 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000802 }
803 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900804 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900805 // Don't include artifacts for the host cross targets because there is no way for us
806 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900807 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900808 continue
809 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000810
Colin Cross70572ed2022-11-02 13:14:20 -0700811 var deps ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000812
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900813 // Add native modules targeting both ABIs. When multilib.* is omitted for
814 // native_shared_libs/jni_libs/tests, it implies multilib.both
Colin Cross70572ed2022-11-02 13:14:20 -0700815 deps.Merge(a.properties.Multilib.Both)
816 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900817 Native_shared_libs: a.properties.Native_shared_libs,
818 Tests: a.properties.Tests,
819 Jni_libs: a.properties.Jni_libs,
820 Binaries: nil,
821 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900822
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900823 // Add native modules targeting the first ABI When multilib.* is omitted for
824 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900825 isPrimaryAbi := i == 0
826 if isPrimaryAbi {
Colin Cross70572ed2022-11-02 13:14:20 -0700827 deps.Merge(a.properties.Multilib.First)
828 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900829 Native_shared_libs: nil,
830 Tests: nil,
831 Jni_libs: nil,
832 Binaries: a.properties.Binaries,
833 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900834 }
835
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900836 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900837 switch target.Arch.ArchType.Multilib {
838 case "lib32":
Colin Cross70572ed2022-11-02 13:14:20 -0700839 deps.Merge(a.properties.Multilib.Lib32)
840 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900841 case "lib64":
Colin Cross70572ed2022-11-02 13:14:20 -0700842 deps.Merge(a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900843 if !has32BitTarget {
Colin Cross70572ed2022-11-02 13:14:20 -0700844 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900845 }
846 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900847
Jiyong Park59140302020-12-14 18:44:04 +0900848 // Add native modules targeting a specific arch variant
849 switch target.Arch.ArchType {
850 case android.Arm:
Colin Cross70572ed2022-11-02 13:14:20 -0700851 deps.Merge(a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900852 case android.Arm64:
Colin Cross70572ed2022-11-02 13:14:20 -0700853 deps.Merge(a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700854 case android.Riscv64:
Colin Cross70572ed2022-11-02 13:14:20 -0700855 deps.Merge(a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900856 case android.X86:
Colin Cross70572ed2022-11-02 13:14:20 -0700857 deps.Merge(a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900858 case android.X86_64:
Colin Cross70572ed2022-11-02 13:14:20 -0700859 deps.Merge(a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900860 default:
861 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
862 }
863
Colin Cross70572ed2022-11-02 13:14:20 -0700864 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Sundong Ahn80c04892021-11-23 00:57:19 +0000865 ctx.AddFarVariationDependencies([]blueprint.Variation{
866 {Mutator: "os", Variation: target.OsVariation()},
867 {Mutator: "arch", Variation: target.ArchVariation()},
868 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900869 }
870
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900871 // Common-arch dependencies come next
872 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000873 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.properties.Rros...)
Anton Hanssone7545852023-02-24 11:06:07 +0000874 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.properties.Bootclasspath_fragments...)
875 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.properties.Systemserverclasspath_fragments...)
876 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
Jiyong Park12a719c2021-01-07 15:31:24 +0900877 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000878 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100879}
880
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900881// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900882func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
883 if a.overridableProperties.Allowed_files != nil {
884 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100885 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900886
887 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
888 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800889 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700890 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
891 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
892 // regardless of the TARGET_PREFER_* setting. See b/144532908
893 arches := ctx.DeviceConfig().Arches()
894 if len(arches) != 0 {
895 archForPrebuiltEtc := arches[0]
896 for _, arch := range arches {
897 // Prefer 64-bit arch if there is any
898 if arch.ArchType.Multilib == "lib64" {
899 archForPrebuiltEtc = arch
900 break
901 }
902 }
903 ctx.AddFarVariationDependencies([]blueprint.Variation{
904 {Mutator: "os", Variation: ctx.Os().String()},
905 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
906 }, prebuiltTag, prebuilts...)
907 }
908 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700909
910 // Dependencies for signing
911 if String(a.overridableProperties.Key) == "" {
912 ctx.PropertyErrorf("key", "missing")
913 return
914 }
915 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
916
917 cert := android.SrcIsModule(a.getCertString(ctx))
918 if cert != "" {
919 ctx.AddDependency(ctx.Module(), certificateTag, cert)
920 // empty cert is not an error. Cert and private keys will be directly found under
921 // PRODUCT_DEFAULT_DEV_CERTIFICATE
922 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100923}
924
Dennis Shene2ed70c2023-01-11 14:15:43 +0000925func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
926 if !mctx.Config().ApexTrimEnabled() {
927 return
928 }
929 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
930 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
931 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
932 } else if o, ok := mctx.Module().(*OverrideApex); ok {
933 for _, p := range o.GetProperties() {
934 properties, ok := p.(*overridableProperties)
935 if !ok {
936 continue
937 }
938 if properties.Trim_against != nil {
939 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
940 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
941 }
942 }
943 }
944}
945
946type DCLAInfo struct {
947 ProvidedLibs []string
948}
949
950var DCLAInfoProvider = blueprint.NewMutatorProvider(DCLAInfo{}, "apex_info")
951
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900952type ApexBundleInfo struct {
953 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100954}
955
Paul Duffin949abc02020-12-08 10:34:30 +0000956var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900957
Paul Duffina7d6a892020-12-07 17:39:59 +0000958var _ ApexInfoMutator = (*apexBundle)(nil)
959
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100960func (a *apexBundle) ApexVariationName() string {
961 return a.properties.ApexVariationName
962}
963
Paul Duffina7d6a892020-12-07 17:39:59 +0000964// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900965// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
966// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
967// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
968// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000969//
970// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
971// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
972// The apexMutator uses that list to create module variants for the apexes to which it belongs.
973// The relationship between module variants and apexes is not one-to-one as variants will be
974// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000975func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900976
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900977 // The VNDK APEX is special. For the APEX, the membership is described in a very different
978 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
979 // libraries are self-identified by their vndk.enabled properties. There is no need to run
980 // this mutator for the APEX as nothing will be collected. So, let's return fast.
981 if a.vndkApex {
982 return
983 }
984
985 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
986 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
987 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
988 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
989 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900990 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
991 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +0900992 if proptools.Bool(a.properties.Use_vndk_as_stable) {
993 if !useVndk {
994 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
995 }
996 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
997 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
998 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
999 }
1000 })
1001 if mctx.Failed() {
1002 return
1003 }
Jooyung Handf78e212020-07-22 15:54:47 +09001004 }
1005
Colin Cross56a83212020-09-15 18:30:11 -07001006 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001007 am, ok := child.(android.ApexModule)
1008 if !ok || !am.CanHaveApexVariants() {
1009 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +09001010 }
Paul Duffin573989d2021-03-17 13:25:29 +00001011 depTag := mctx.OtherModuleDependencyTag(child)
1012
1013 // Check to see if the tag always requires that the child module has an apex variant for every
1014 // apex variant of the parent module. If it does not then it is still possible for something
1015 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
1016 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
1017 return true
1018 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001019 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001020 return false
1021 }
Jooyung Handf78e212020-07-22 15:54:47 +09001022 if excludeVndkLibs {
1023 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
1024 return false
1025 }
1026 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001027 // By default, all the transitive dependencies are collected, unless filtered out
1028 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001029 return true
1030 }
1031
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001032 // Records whether a certain module is included in this apexBundle via direct dependency or
1033 // inndirect dependency.
1034 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001035 mctx.WalkDeps(func(child, parent android.Module) bool {
1036 if !continueApexDepsWalk(child, parent) {
1037 return false
1038 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001039 // If the parent is apexBundle, this child is directly depended.
1040 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001041 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001042 contents[depName] = contents[depName].Add(directDep)
1043 return true
1044 })
1045
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001046 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001047 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -07001048 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
1049 Contents: apexContents,
1050 })
1051
Jooyung Haned124c32021-01-26 11:43:46 +09001052 minSdkVersion := a.minSdkVersion(mctx)
1053 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1054 if minSdkVersion.IsNone() {
1055 minSdkVersion = android.FutureApiLevel
1056 }
1057
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001058 // This is the main part of this mutator. Mark the collected dependencies that they need to
1059 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001060
Jooyung Han63dff462023-02-09 00:11:27 +00001061 apexVariationName := mctx.ModuleName() // could be com.android.foo
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001062 a.properties.ApexVariationName = apexVariationName
Colin Cross56a83212020-09-15 18:30:11 -07001063 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001064 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001065 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001066 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001067 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001068 InApexVariants: []string{apexVariationName},
1069 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001070 ApexContents: []*android.ApexContents{apexContents},
1071 }
Colin Cross56a83212020-09-15 18:30:11 -07001072 mctx.WalkDeps(func(child, parent android.Module) bool {
1073 if !continueApexDepsWalk(child, parent) {
1074 return false
1075 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001076 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001077 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001078 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001079
1080 if a.dynamic_common_lib_apex() {
1081 mctx.SetProvider(DCLAInfoProvider, DCLAInfo{
1082 ProvidedLibs: a.properties.Native_shared_libs,
1083 })
1084 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001085}
1086
Paul Duffina7d6a892020-12-07 17:39:59 +00001087type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001088 // ApexVariationName returns the name of the APEX variation to use in the apex
1089 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1090 ApexVariationName() string
1091
Paul Duffina7d6a892020-12-07 17:39:59 +00001092 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1093 // depended upon by an apex and which require an apex specific variant.
1094 ApexInfoMutator(android.TopDownMutatorContext)
1095}
1096
1097// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1098// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001099// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001100func apexInfoMutator(mctx android.TopDownMutatorContext) {
1101 if !mctx.Module().Enabled() {
1102 return
1103 }
1104
1105 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1106 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001107 }
Spandan Das42e89502022-05-06 22:12:55 +00001108 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001109}
1110
Spandan Das66773252022-01-15 00:23:18 +00001111// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
1112// This check is enforced for updatable modules
1113func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
1114 if !mctx.Module().Enabled() {
1115 return
1116 }
Spandan Das08c911f2022-01-21 22:07:26 +00001117 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +00001118 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +00001119 // b/208656169 Do not propagate strict updatability linting to libcore/
1120 // These libs are available on the classpath during compilation
1121 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1122 // Only skip libraries defined in libcore root, not subdirectories
1123 if mctx.OtherModuleDir(child) == "libcore" {
1124 // Do not traverse transitive deps of libcore/ libs
1125 return false
1126 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001127 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1128 return false
1129 }
Spandan Das66773252022-01-15 00:23:18 +00001130 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1131 lintable.SetStrictUpdatabilityLinting(true)
1132 }
1133 // visit transitive deps
1134 return true
1135 })
1136 }
1137}
1138
Spandan Das42e89502022-05-06 22:12:55 +00001139// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1140func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1141 if !mctx.Module().Enabled() {
1142 return
1143 }
1144 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1145 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1146 mctx.VisitDirectDeps(func(module android.Module) {
1147 // ignore android_test_app
1148 if app, ok := module.(*java.AndroidApp); ok {
1149 app.SetUpdatable(true)
1150 }
1151 })
1152 }
1153}
1154
Spandan Das08c911f2022-01-21 22:07:26 +00001155// TODO: b/215736885 Whittle the denylist
1156// Transitive deps of certain mainline modules baseline NewApi errors
1157// Skip these mainline modules for now
1158var (
1159 skipStrictUpdatabilityLintAllowlist = []string{
1160 "com.android.art",
1161 "com.android.art.debug",
1162 "com.android.conscrypt",
1163 "com.android.media",
1164 // test apexes
1165 "test_com.android.art",
1166 "test_com.android.conscrypt",
1167 "test_com.android.media",
1168 "test_jitzygote_com.android.art",
1169 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001170
1171 // TODO: b/215736885 Remove this list
1172 skipLintJavalibAllowlist = []string{
1173 "conscrypt.module.platform.api.stubs",
1174 "conscrypt.module.public.api.stubs",
1175 "conscrypt.module.public.api.stubs.system",
1176 "conscrypt.module.public.api.stubs.module_lib",
1177 "framework-media.stubs",
1178 "framework-media.stubs.system",
1179 "framework-media.stubs.module_lib",
1180 }
Spandan Das08c911f2022-01-21 22:07:26 +00001181)
1182
1183func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1184 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1185}
1186
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001187// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1188// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1189// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001190func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1191 if !mctx.Module().Enabled() {
1192 return
1193 }
1194 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001195 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1196 }
1197}
1198
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001199// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1200// the apex in order to retrieve its contents later.
1201// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001202func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1203 if !mctx.Module().Enabled() {
1204 return
1205 }
Colin Cross56a83212020-09-15 18:30:11 -07001206 if am, ok := mctx.Module().(android.ApexModule); ok {
1207 if testFor := am.TestFor(); len(testFor) > 0 {
1208 mctx.AddFarVariationDependencies([]blueprint.Variation{
1209 {Mutator: "os", Variation: am.Target().OsVariation()},
1210 {"arch", "common"},
1211 }, testForTag, testFor...)
1212 }
1213 }
1214}
1215
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001216// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001217func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1218 if !mctx.Module().Enabled() {
1219 return
1220 }
Colin Cross56a83212020-09-15 18:30:11 -07001221 if _, ok := mctx.Module().(android.ApexModule); ok {
1222 var contents []*android.ApexContents
1223 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1224 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1225 contents = append(contents, abInfo.Contents)
1226 }
1227 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1228 ApexContents: contents,
1229 })
Colin Crossaede88c2020-08-11 12:17:01 -07001230 }
1231}
1232
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001233// markPlatformAvailability marks whether or not a module can be available to platform. A module
1234// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1235// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1236// be) available to platform
1237// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001238func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1239 // Host and recovery are not considered as platform
1240 if mctx.Host() || mctx.Module().InstallInRecovery() {
1241 return
1242 }
1243
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001244 am, ok := mctx.Module().(android.ApexModule)
1245 if !ok {
1246 return
1247 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001248
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001249 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001250
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001251 // If any of the dep is not available to platform, this module is also considered as being
1252 // not available to platform even if it has "//apex_available:platform"
1253 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001254 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001255 // if the dependency crosses apex boundary, don't consider it
1256 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001257 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001258 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1259 availableToPlatform = false
1260 // TODO(b/154889534) trigger an error when 'am' has
1261 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001262 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001263 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001264
Paul Duffinb5769c12021-05-12 16:16:51 +01001265 // Exception 1: check to see if the module always requires it.
1266 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001267 availableToPlatform = true
1268 }
1269
1270 // Exception 2: bootstrap bionic libraries are also always available to platform
1271 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1272 availableToPlatform = true
1273 }
1274
1275 if !availableToPlatform {
1276 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001277 }
1278}
1279
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001280// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001281// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001282func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001283 if !mctx.Module().Enabled() {
1284 return
1285 }
Colin Cross56a83212020-09-15 18:30:11 -07001286
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001287 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001288 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001289 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001290 return
1291 }
1292
1293 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001294 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1295 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001296 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001297 if strings.HasPrefix(apexBundleName, "com.android.art") {
1298 // Create an alias from the platform variant. This is done to make
1299 // test_for dependencies work for modules that are split by the APEX
1300 // mutator, since test_for dependencies always go to the platform variant.
1301 // This doesn't happen for normal APEXes that are disjunct, so only do
1302 // this for the overlapping ART APEXes.
1303 // TODO(b/183882457): Remove this if the test_for functionality is
1304 // refactored to depend on the proper APEX variants instead of platform.
1305 mctx.CreateAliasVariation("", apexBundleName)
1306 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001307 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1308 apexBundleName := o.GetOverriddenModuleName()
1309 if apexBundleName == "" {
1310 mctx.ModuleErrorf("base property is not set")
1311 return
1312 }
1313 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001314 if strings.HasPrefix(apexBundleName, "com.android.art") {
1315 // TODO(b/183882457): See note for CreateAliasVariation above.
1316 mctx.CreateAliasVariation("", apexBundleName)
1317 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001318 }
1319}
Sundong Ahne9b55722019-09-06 17:37:42 +09001320
Paul Duffin6717d882021-06-15 19:09:41 +01001321// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1322// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001323func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001324 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001325 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001326 return !a.vndkApex
1327 }
1328
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001329 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001330}
1331
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001332// See android.UpdateDirectlyInAnyApex
1333// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001334func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1335 if !mctx.Module().Enabled() {
1336 return
1337 }
1338 if am, ok := mctx.Module().(android.ApexModule); ok {
1339 android.UpdateDirectlyInAnyApex(mctx, am)
1340 }
1341}
1342
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001343// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001344type apexPackaging int
1345
1346const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001347 // imageApex is a packaging method where contents are included in a filesystem image which
1348 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001349 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001350
1351 // zipApex is a packaging method where contents are directly included in the zip container.
1352 // This is used for host-side testing - because the contents are easily accessible by
1353 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001354 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001355
1356 // flattendApex is a packaging method where contents are not included in the APEX file, but
1357 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1358 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001359 flattenedApex
1360)
1361
1362const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001363 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001364 imageApexSuffix = ".apex"
1365 imageCapexSuffix = ".capex"
1366 zipApexSuffix = ".zipapex"
1367 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001368
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001369 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001370 imageApexType = "image"
1371 zipApexType = "zip"
1372 flattenedApexType = "flattened"
1373
Dan Willemsen47e1a752021-10-16 18:36:13 -07001374 ext4FsType = "ext4"
1375 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001376 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001377)
1378
1379// The suffix for the output "file", not the module
1380func (a apexPackaging) suffix() string {
1381 switch a {
1382 case imageApex:
1383 return imageApexSuffix
1384 case zipApex:
1385 return zipApexSuffix
1386 default:
1387 panic(fmt.Errorf("unknown APEX type %d", a))
1388 }
1389}
1390
1391func (a apexPackaging) name() string {
1392 switch a {
1393 case imageApex:
1394 return imageApexType
1395 case zipApex:
1396 return zipApexType
1397 default:
1398 panic(fmt.Errorf("unknown APEX type %d", a))
1399 }
1400}
1401
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001402// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1403// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001404func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001405 if !mctx.Module().Enabled() {
1406 return
1407 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001408 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001409 var variants []string
1410 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1411 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001412 // This is the normal case. Note that both image and flattend APEXes are
1413 // created. The image type is installed to the system partition, while the
1414 // flattened APEX is (optionally) installed to the system_ext partition.
1415 // This is mostly for GSI which has to support wide range of devices. If GSI
1416 // is installed on a newer (APEX-capable) device, the image APEX in the
1417 // system will be used. However, if the same GSI is installed on an old
1418 // device which can't support image APEX, the flattened APEX in the
1419 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001420 variants = append(variants, imageApexType, flattenedApexType)
1421 case "zip":
1422 variants = append(variants, zipApexType)
1423 case "both":
1424 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1425 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001426 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001427 return
1428 }
1429
1430 modules := mctx.CreateLocalVariations(variants...)
1431
1432 for i, v := range variants {
1433 switch v {
1434 case imageApexType:
1435 modules[i].(*apexBundle).properties.ApexType = imageApex
1436 case zipApexType:
1437 modules[i].(*apexBundle).properties.ApexType = zipApex
1438 case flattenedApexType:
1439 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001440 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001441 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001442 modules[i].(*apexBundle).MakeAsSystemExt()
1443 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001444 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001445 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001446 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001447 // payload_type is forcibly overridden to "image"
1448 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001449 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001450 }
1451}
1452
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001453var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001454
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001455// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001456func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001457 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001458 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001459 return true
1460}
1461
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001462var _ android.OutputFileProducer = (*apexBundle)(nil)
1463
1464// Implements android.OutputFileProducer
1465func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1466 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001467 case "", android.DefaultDistTag:
1468 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001469 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001470 case imageApexSuffix:
1471 // uncompressed one
1472 if a.outputApexFile != nil {
1473 return android.Paths{a.outputApexFile}, nil
1474 }
1475 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001476 default:
1477 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1478 }
1479}
1480
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001481var _ multitree.Exportable = (*apexBundle)(nil)
1482
1483func (a *apexBundle) Exportable() bool {
1484 if a.properties.ApexType == flattenedApex {
1485 return false
1486 }
1487 return true
1488}
1489
1490func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1491 ret := make(map[string]android.Paths)
1492 ret["apex"] = android.Paths{a.outputFile}
1493 return ret
1494}
1495
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001496var _ cc.Coverage = (*apexBundle)(nil)
1497
1498// Implements cc.Coverage
1499func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1500 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1501}
1502
1503// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001504func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001505 a.properties.PreventInstall = true
1506}
1507
1508// Implements cc.Coverage
1509func (a *apexBundle) HideFromMake() {
1510 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001511 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1512 // TODO(ccross): untangle these
1513 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001514}
1515
1516// Implements cc.Coverage
1517func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1518 a.properties.IsCoverageVariant = coverage
1519}
1520
1521// Implements cc.Coverage
1522func (a *apexBundle) EnableCoverageIfNeeded() {}
1523
1524var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1525
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001526// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001527func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001528 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001529}
1530
Jiyong Parkf4020582021-11-29 12:37:10 +09001531func (a *apexBundle) FutureUpdatable() bool {
1532 return proptools.BoolDefault(a.properties.Future_updatable, false)
1533}
1534
Jiyong Park1bc84122021-06-22 20:23:05 +09001535func (a *apexBundle) UsePlatformApis() bool {
1536 return proptools.BoolDefault(a.properties.Platform_apis, false)
1537}
1538
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001539// getCertString returns the name of the cert that should be used to sign this APEX. This is
1540// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001541func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001542 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001543 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1544 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1545 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001546 if a.vndkApex {
1547 moduleName = vndkApexName
1548 }
1549 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001550 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001551 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001552 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001553 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001554}
1555
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001556// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001557func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001558 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001559}
1560
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001561// See the generate_hashtree property
1562func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001563 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001564}
1565
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001566// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001567func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1568 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1569}
1570
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001571// See the test_only_force_compression property
1572func (a *apexBundle) testOnlyShouldForceCompression() bool {
1573 return proptools.Bool(a.properties.Test_only_force_compression)
1574}
1575
Dennis Shenaf41bc12022-08-03 16:46:43 +00001576// See the dynamic_common_lib_apex property
1577func (a *apexBundle) dynamic_common_lib_apex() bool {
1578 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1579}
1580
Dennis Shene2ed70c2023-01-11 14:15:43 +00001581// See the list of libs to trim
1582func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1583 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1584 if len(dclaModules) > 1 {
1585 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1586 }
1587 if len(dclaModules) > 0 {
1588 DCLAInfo := ctx.OtherModuleProvider(dclaModules[0], DCLAInfoProvider).(DCLAInfo)
1589 return DCLAInfo.ProvidedLibs
1590 }
1591 return []string{}
1592}
1593
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001594// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1595// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1596// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001597
Jiyong Parkf97782b2019-02-13 20:28:58 +09001598func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1599 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1600 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1601 }
1602}
1603
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001604func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001605 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1606 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001607 }
1608
1609 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001610 var globalSanitizerNames []string
Jiyong Park388ef3f2019-01-28 19:47:32 +09001611 if a.Host() {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001612 globalSanitizerNames = config.SanitizeHost()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001613 } else {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001614 arches := config.SanitizeDeviceArch()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001615 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001616 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001617 }
1618 }
1619 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001620}
1621
Jooyung Han8ce8db92020-05-15 19:05:05 +09001622func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001623 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1624 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001625 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001626 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001627 for _, target := range ctx.MultiTargets() {
1628 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001629 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001630 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001631 Tests: nil,
1632 Jni_libs: nil,
1633 Binaries: nil,
1634 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001635 break
1636 }
1637 }
1638 }
1639}
1640
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001641// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1642// returned apexFile saves information about the Soong module that will be used for creating the
1643// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001644func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001645 // Decide the APEX-local directory by the multilib of the library In the future, we may
1646 // query this to the module.
1647 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001648 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001649 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001650 case "lib32":
1651 dirInApex = "lib"
1652 case "lib64":
1653 dirInApex = "lib64"
1654 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001655 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001656 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001657 }
Jooyung Han35155c42020-02-06 17:33:20 +09001658 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park1833cef2019-12-13 13:28:36 +09001659 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001660 // Special case for Bionic libs and other libs installed with them. This is to
1661 // prevent those libs from being included in the search path
1662 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1663 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1664 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1665 // will be loaded into the default linker namespace (aka "platform" namespace). If
1666 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1667 // be loaded again into the runtime linker namespace, which will result in double
1668 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001669 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001671
Colin Cross1d487152022-10-03 19:14:46 -07001672 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001673 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1674 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001675}
1676
Jiyong Park1833cef2019-12-13 13:28:36 +09001677func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001678 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001679 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001680 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001681 }
Jooyung Han35155c42020-02-06 17:33:20 +09001682 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001683 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001684 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1685 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001686 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001687 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001688 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001689}
1690
Jiyong Park99644e92020-11-17 22:21:02 +09001691func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1692 dirInApex := "bin"
1693 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1694 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1695 }
Colin Cross1d487152022-10-03 19:14:46 -07001696 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001697 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1698 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1699 return af
1700}
1701
1702func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1703 // Decide the APEX-local directory by the multilib of the library
1704 // In the future, we may query this to the module.
1705 var dirInApex string
1706 switch rustm.Arch().ArchType.Multilib {
1707 case "lib32":
1708 dirInApex = "lib"
1709 case "lib64":
1710 dirInApex = "lib64"
1711 }
1712 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1713 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1714 }
Colin Cross1d487152022-10-03 19:14:46 -07001715 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001716 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1717 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1718}
1719
Cole Faust4d247e62023-01-23 10:14:58 -08001720func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.PythonBinaryModule) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001721 dirInApex := "bin"
1722 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001723 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001724}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001725
Jiyong Park1833cef2019-12-13 13:28:36 +09001726func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001727 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001728 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001729 // NB: Since go binaries are static we don't need the module for anything here, which is
1730 // good since the go tool is a blueprint.Module not an android.Module like we would
1731 // normally use.
Jingwen Chen2d37b642023-03-14 16:11:38 +00001732 //
Jiyong Park1833cef2019-12-13 13:28:36 +09001733 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001734}
1735
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001736func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001737 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001738 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1739 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1740 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001741 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001742 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001743 af.symlinks = sh.Symlinks()
1744 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001745}
1746
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001747func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001748 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001749 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001750 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001751}
1752
atrost6e126252020-01-27 17:01:16 +00001753func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1754 dirInApex := filepath.Join("etc", config.SubDir())
1755 fileToCopy := config.CompatConfig()
1756 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1757}
1758
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001759// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1760// way.
1761type javaModule interface {
1762 android.Module
1763 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001764 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001765 JacocoReportClassesFile() android.Path
1766 LintDepSets() java.LintDepSets
1767 Stem() string
1768}
1769
1770var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001771var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001772var _ javaModule = (*java.SdkLibrary)(nil)
1773var _ javaModule = (*java.DexImport)(nil)
1774var _ javaModule = (*java.SdkLibraryImport)(nil)
1775
Paul Duffin190fdef2021-04-26 10:33:59 +01001776// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001777func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001778 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001779}
1780
1781// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1782func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001783 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001784 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001785 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1786 af.lintDepSets = module.LintDepSets()
1787 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001788 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1789 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1790 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1791 }
1792 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001793 return af
1794}
1795
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001796func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1797 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001798 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001799 dirInApex := "javalib"
1800 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1801 af.customStem = module.Stem() + ".jar.prof"
1802 return &af
1803 }
1804 }
1805 return nil
1806}
1807
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001808// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1809// the same way.
1810type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001811 android.Module
1812 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001813 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001814 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001815 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001816 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001817 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001818 LintDepSets() java.LintDepSets
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001819}
1820
1821var _ androidApp = (*java.AndroidApp)(nil)
1822var _ androidApp = (*java.AndroidAppImport)(nil)
1823
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001824func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1825 buildId := ctx.Config().BuildId()
1826
1827 // The build ID is used as a suffix for a filename, so ensure that
1828 // the set of characters being used are sanitized.
1829 // - any word character: [a-zA-Z0-9_]
1830 // - dots: .
1831 // - dashes: -
1832 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1833 if !validRegex.MatchString(buildId) {
1834 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1835 }
1836 return buildId
1837}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001838
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001839func apexFileForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001840 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001841 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001842 appDir = "priv-app"
1843 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001844
1845 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1846 // so that PackageManager correctly invalidates the existing installed apk
1847 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001848 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001849 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001850
Yo Chiange8128052020-07-23 20:09:18 +08001851 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001852 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001853 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001854 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001855
1856 if app, ok := aapp.(interface {
1857 OverriddenManifestPackageName() string
1858 }); ok {
1859 af.overriddenPackageName = app.OverriddenManifestPackageName()
1860 }
Jiyong Park618922e2020-01-08 13:35:43 +09001861 return af
Dario Frenicde2a032019-10-27 00:29:22 +01001862}
1863
Jiyong Park69aeba92020-04-24 21:16:36 +09001864func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1865 rroDir := "overlay"
1866 dirInApex := filepath.Join(rroDir, rro.Theme())
1867 fileToCopy := rro.OutputFile()
1868 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1869 af.certificate = rro.Certificate()
1870
1871 if a, ok := rro.(interface {
1872 OverriddenManifestPackageName() string
1873 }); ok {
1874 af.overriddenPackageName = a.OverriddenManifestPackageName()
1875 }
1876 return af
1877}
1878
Ken Chenfad7f9d2021-11-10 22:02:57 +08001879func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1880 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001881 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1882}
1883
Jiyong Park12a719c2021-01-07 15:31:24 +09001884func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1885 dirInApex := filepath.Join("etc", "fs")
1886 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1887}
1888
Paul Duffin064b70c2020-11-02 17:32:38 +00001889// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001890// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1891// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1892// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001893func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001894 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001895 am, ok := child.(android.ApexModule)
1896 if !ok || !am.CanHaveApexVariants() {
1897 return false
1898 }
1899
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001900 // Filter-out unwanted depedendencies
1901 depTag := ctx.OtherModuleDependencyTag(child)
1902 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1903 return false
1904 }
Paul Duffin520917a2022-05-13 13:01:59 +00001905 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001906 return false
1907 }
1908
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001909 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001910 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001911
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001912 // Visit actually
1913 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001914 })
1915}
1916
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001917// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1918type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001919
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001920const (
1921 ext4 fsType = iota
1922 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001923 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001924)
Artur Satayev849f8442020-04-28 14:57:42 +01001925
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001926func (f fsType) string() string {
1927 switch f {
1928 case ext4:
1929 return ext4FsType
1930 case f2fs:
1931 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001932 case erofs:
1933 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001934 default:
1935 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001936 }
1937}
1938
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001939var _ android.MixedBuildBuildable = (*apexBundle)(nil)
1940
1941func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
1942 return ctx.ModuleType() == "apex" && a.properties.ApexType == imageApex
1943}
1944
1945func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
1946 bazelCtx := ctx.Config().BazelContext
1947 bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
1948}
1949
Jingwen Chen889f2f22022-12-16 08:16:01 +00001950// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
1951// override_apex module overriding this apexBundle. An apexBundle can be
1952// overridden by different override_apex modules (e.g. Google or Go variants),
1953// which is handled by the overrides mutators.
1954func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
1955 if _, ok := ctx.Module().(android.OverridableModule); ok {
1956 return android.MaybeBp2buildLabelOfOverridingModule(ctx)
1957 }
1958 return a.BazelModuleBase.GetBazelLabel(ctx, a)
1959}
1960
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001961func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
1962 if !a.commonBuildActions(ctx) {
1963 return
1964 }
1965
1966 a.setApexTypeAndSuffix(ctx)
1967 a.setPayloadFsType(ctx)
1968 a.setSystemLibLink(ctx)
1969
1970 if a.properties.ApexType != zipApex {
1971 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
1972 }
1973
1974 bazelCtx := ctx.Config().BazelContext
1975 outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx))
1976 if err != nil {
1977 ctx.ModuleErrorf(err.Error())
1978 return
1979 }
1980 a.installDir = android.PathForModuleInstall(ctx, "apex")
Jingwen Chen94098e82023-01-10 14:50:42 +00001981
1982 // Set the output file to .apex or .capex depending on the compression configuration.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001983 a.setCompression(ctx)
Jingwen Chen94098e82023-01-10 14:50:42 +00001984 if a.isCompressed {
1985 a.outputApexFile = android.PathForBazelOut(ctx, outputs.SignedCompressedOutput)
1986 } else {
1987 a.outputApexFile = android.PathForBazelOut(ctx, outputs.SignedOutput)
1988 }
1989 a.outputFile = a.outputApexFile
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001990
Sam Delmerico4ed95e22023-02-03 18:12:15 -05001991 if len(outputs.TidyFiles) > 0 {
1992 tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles)
1993 a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles)
1994 }
1995
Liz Kammer0e255ef2022-11-04 16:07:04 -04001996 // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer
1997 // interface if these were set in a provider rather than the module itself
Wei Li32dcdf92022-10-26 22:30:48 -07001998 a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0])
1999 a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1])
2000 a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0])
2001 a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1])
Liz Kammer0e255ef2022-11-04 16:07:04 -04002002
Jingwen Chen29743c82023-01-25 17:49:46 +00002003 // Ensure ApexMkInfo.install_to_system make module names are installed as
2004 // part of a bundled build.
2005 a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...)
Vinh Tranb6803a52022-12-14 11:34:54 -05002006
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002007 apexType := a.properties.ApexType
2008 switch apexType {
2009 case imageApex:
Liz Kammer303978d2022-11-04 16:12:43 -04002010 a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile)
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002011 a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex))
Wei Licc73a052022-11-07 14:25:34 -08002012 a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs))
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002013 // TODO(b/239084755): Generate the java api using.xml file from Bazel.
Jingwen Chen1ec77852022-11-07 14:36:12 +00002014 a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex))
Wei Li78c07de2022-11-08 16:01:05 -08002015 a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002016 installSuffix := imageApexSuffix
2017 if a.isCompressed {
2018 installSuffix = imageCapexSuffix
2019 }
2020 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
2021 a.compatSymlinks.Paths()...)
2022 default:
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002023 panic(fmt.Errorf("internal error: unexpected apex_type for the ProcessBazelQueryResponse: %v", a.properties.ApexType))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002024 }
2025
Jingwen Chen2d37b642023-03-14 16:11:38 +00002026 // filesInfo in mixed mode must retrieve all information about the apex's
2027 // contents completely from the Starlark providers. It should never rely on
2028 // Android.bp information, as they might not exist for fully migrated
2029 // dependencies.
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002030 //
2031 // Prevent accidental writes to filesInfo in the earlier parts Soong by
2032 // asserting it to be nil.
2033 if a.filesInfo != nil {
Jingwen Chen2d37b642023-03-14 16:11:38 +00002034 panic(
2035 fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel. " +
2036 "Did something else set filesInfo before this line of code?"))
2037 }
2038 for _, f := range outputs.PayloadFilesInfo {
2039 fileInfo := apexFile{
2040 isBazelPrebuilt: true,
2041
2042 builtFile: android.PathForBazelOut(ctx, f["built_file"]),
2043 unstrippedBuiltFile: android.PathForBazelOut(ctx, f["unstripped_built_file"]),
2044 androidMkModuleName: f["make_module_name"],
2045 installDir: f["install_dir"],
2046 class: classes[f["class"]],
2047 customStem: f["basename"],
2048 moduleDir: f["package"],
2049 }
2050
2051 arch := f["arch"]
2052 fileInfo.arch = arch
2053 if len(arch) > 0 {
2054 fileInfo.multilib = "lib32"
2055 if strings.HasSuffix(arch, "64") {
2056 fileInfo.multilib = "lib64"
2057 }
2058 }
2059
2060 a.filesInfo = append(a.filesInfo, fileInfo)
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002061 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002062}
2063
2064func (a *apexBundle) setCompression(ctx android.ModuleContext) {
2065 if a.properties.ApexType != imageApex {
2066 a.isCompressed = false
2067 } else if a.testOnlyShouldForceCompression() {
2068 a.isCompressed = true
2069 } else {
2070 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
2071 }
2072}
2073
2074func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
2075 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2076 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2077 // the same library in the system partition, thus effectively sharing the same libraries
2078 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2079 // in the APEX.
2080 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
2081
2082 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2083 // So we can't link them to /system/lib libs which are core variants.
2084 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2085 a.linkToSystemLib = false
2086 }
2087
2088 forced := ctx.Config().ForceApexSymlinkOptimization()
2089 updatable := a.Updatable() || a.FutureUpdatable()
2090
2091 // We don't need the optimization for updatable APEXes, as it might give false signal
2092 // to the system health when the APEXes are still bundled (b/149805758).
2093 if !forced && updatable && a.properties.ApexType == imageApex {
2094 a.linkToSystemLib = false
2095 }
2096
2097 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2098 if ctx.Host() {
2099 a.linkToSystemLib = false
2100 }
2101}
2102
2103func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
2104 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2105 case ext4FsType:
2106 a.payloadFsType = ext4
2107 case f2fsFsType:
2108 a.payloadFsType = f2fs
2109 case erofsFsType:
2110 a.payloadFsType = erofs
2111 default:
2112 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
2113 }
2114}
2115
2116func (a *apexBundle) setApexTypeAndSuffix(ctx android.ModuleContext) {
2117 // Set suffix and primaryApexType depending on the ApexType
2118 buildFlattenedAsDefault := ctx.Config().FlattenApex()
2119 switch a.properties.ApexType {
2120 case imageApex:
2121 if buildFlattenedAsDefault {
2122 a.suffix = imageApexSuffix
2123 } else {
2124 a.suffix = ""
2125 a.primaryApexType = true
2126
2127 if ctx.Config().InstallExtraFlattenedApexes() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002128 a.makeModulesToInstall = append(a.makeModulesToInstall, a.Name()+flattenedSuffix)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002129 }
2130 }
2131 case zipApex:
2132 if proptools.String(a.properties.Payload_type) == "zip" {
2133 a.suffix = ""
2134 a.primaryApexType = true
2135 } else {
2136 a.suffix = zipApexSuffix
2137 }
2138 case flattenedApex:
2139 if buildFlattenedAsDefault {
2140 a.suffix = ""
2141 a.primaryApexType = true
2142 } else {
2143 a.suffix = flattenedSuffix
2144 }
2145 }
2146}
2147
2148func (a apexBundle) isCompressable() bool {
2149 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2150}
2151
2152func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2153 a.checkApexAvailability(ctx)
2154 a.checkUpdatable(ctx)
2155 a.CheckMinSdkVersion(ctx)
2156 a.checkStaticLinkingToStubLibraries(ctx)
2157 a.checkStaticExecutables(ctx)
2158 if len(a.properties.Tests) > 0 && !a.testApex {
2159 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2160 return false
2161 }
2162 return true
2163}
2164
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002165type visitorContext struct {
2166 // all the files that will be included in this APEX
2167 filesInfo []apexFile
2168
2169 // native lib dependencies
2170 provideNativeLibs []string
2171 requireNativeLibs []string
2172
2173 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002174
2175 // if true, raise error on duplicate apexFile
2176 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002177}
2178
Jooyung Han862c0d62022-12-21 10:15:37 +09002179func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002180 encountered := make(map[string]apexFile)
2181 for _, f := range vctx.filesInfo {
2182 dest := filepath.Join(f.installDir, f.builtFile.Base())
2183 if e, ok := encountered[dest]; !ok {
2184 encountered[dest] = f
2185 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002186 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2187 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2188 dest, e.builtFile, f.builtFile)
2189 return
2190 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002191 // If a module is directly included and also transitively depended on
2192 // consider it as directly included.
2193 e.transitiveDep = e.transitiveDep && f.transitiveDep
2194 encountered[dest] = e
2195 }
2196 }
2197 vctx.filesInfo = vctx.filesInfo[:0]
2198 for _, v := range encountered {
2199 vctx.filesInfo = append(vctx.filesInfo, v)
2200 }
2201 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2202 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2203 // changes.
2204 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2205 })
2206}
2207
2208func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2209 depTag := ctx.OtherModuleDependencyTag(child)
2210 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2211 return false
2212 }
2213 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2214 return false
2215 }
2216 depName := ctx.OtherModuleName(child)
2217 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2218 switch depTag {
2219 case sharedLibTag, jniLibTag:
2220 isJniLib := depTag == jniLibTag
2221 switch ch := child.(type) {
2222 case *cc.Module:
2223 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2224 fi.isJniLib = isJniLib
2225 vctx.filesInfo = append(vctx.filesInfo, fi)
2226 // Collect the list of stub-providing libs except:
2227 // - VNDK libs are only for vendors
2228 // - bootstrap bionic libs are treated as provided by system
2229 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2230 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2231 }
2232 return true // track transitive dependencies
2233 case *rust.Module:
2234 fi := apexFileForRustLibrary(ctx, ch)
2235 fi.isJniLib = isJniLib
2236 vctx.filesInfo = append(vctx.filesInfo, fi)
2237 return true // track transitive dependencies
2238 default:
2239 propertyName := "native_shared_libs"
2240 if isJniLib {
2241 propertyName = "jni_libs"
2242 }
2243 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2244 }
2245 case executableTag:
2246 switch ch := child.(type) {
2247 case *cc.Module:
2248 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2249 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002250 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002251 if ch.HostToolPath().Valid() {
2252 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2253 }
2254 case bootstrap.GoBinaryTool:
2255 if a.Host() {
2256 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2257 }
2258 case *rust.Module:
2259 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2260 return true // track transitive dependencies
2261 default:
2262 ctx.PropertyErrorf("binaries",
2263 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2264 }
2265 case shBinaryTag:
2266 if csh, ok := child.(*sh.ShBinary); ok {
2267 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2268 } else {
2269 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2270 }
2271 case bcpfTag:
2272 bcpfModule, ok := child.(*java.BootclasspathFragmentModule)
2273 if !ok {
2274 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2275 return false
2276 }
2277
2278 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
2279 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002280 a.makeModulesToInstall = append(a.makeModulesToInstall, makeModuleName)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002281 }
2282 return true
2283 case sscpfTag:
2284 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2285 ctx.PropertyErrorf("systemserverclasspath_fragments",
2286 "%q is not a systemserverclasspath_fragment module", depName)
2287 return false
2288 }
2289 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2290 vctx.filesInfo = append(vctx.filesInfo, *af)
2291 }
2292 return true
2293 case javaLibTag:
2294 switch child.(type) {
2295 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2296 af := apexFileForJavaModule(ctx, child.(javaModule))
2297 if !af.ok() {
2298 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2299 return false
2300 }
2301 vctx.filesInfo = append(vctx.filesInfo, af)
2302 return true // track transitive dependencies
2303 default:
2304 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2305 }
2306 case androidAppTag:
2307 switch ap := child.(type) {
2308 case *java.AndroidApp:
2309 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2310 return true // track transitive dependencies
2311 case *java.AndroidAppImport:
2312 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2313 case *java.AndroidTestHelperApp:
2314 vctx.filesInfo = append(vctx.filesInfo, apexFileForAndroidApp(ctx, ap))
2315 case *java.AndroidAppSet:
2316 appDir := "app"
2317 if ap.Privileged() {
2318 appDir = "priv-app"
2319 }
2320 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2321 // suffixed so that PackageManager correctly invalidates the
2322 // existing installed apk in favour of the new APK-in-APEX.
2323 // See bugs for more information.
2324 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2325 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2326 af.certificate = java.PresignedCertificate
2327 vctx.filesInfo = append(vctx.filesInfo, af)
2328 default:
2329 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2330 }
2331 case rroTag:
2332 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2333 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2334 } else {
2335 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2336 }
2337 case bpfTag:
2338 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2339 filesToCopy, _ := bpfProgram.OutputFiles("")
2340 apex_sub_dir := bpfProgram.SubDir()
2341 for _, bpfFile := range filesToCopy {
2342 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2343 }
2344 } else {
2345 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2346 }
2347 case fsTag:
2348 if fs, ok := child.(filesystem.Filesystem); ok {
2349 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2350 } else {
2351 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2352 }
2353 case prebuiltTag:
2354 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2355 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2356 } else {
2357 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2358 }
2359 case compatConfigTag:
2360 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2361 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2362 } else {
2363 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2364 }
2365 case testTag:
2366 if ccTest, ok := child.(*cc.Module); ok {
2367 if ccTest.IsTestPerSrcAllTestsVariation() {
2368 // Multiple-output test module (where `test_per_src: true`).
2369 //
2370 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2371 // We do not add this variation to `filesInfo`, as it has no output;
2372 // however, we do add the other variations of this module as indirect
2373 // dependencies (see below).
2374 } else {
2375 // Single-output test module (where `test_per_src: false`).
2376 af := apexFileForExecutable(ctx, ccTest)
2377 af.class = nativeTest
2378 vctx.filesInfo = append(vctx.filesInfo, af)
2379 }
2380 return true // track transitive dependencies
2381 } else {
2382 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2383 }
2384 case keyTag:
2385 if key, ok := child.(*apexKey); ok {
2386 a.privateKeyFile = key.privateKeyFile
2387 a.publicKeyFile = key.publicKeyFile
2388 } else {
2389 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2390 }
2391 case certificateTag:
2392 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2393 a.containerCertificateFile = dep.Certificate.Pem
2394 a.containerPrivateKeyFile = dep.Certificate.Key
2395 } else {
2396 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2397 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002398 }
2399 return false
2400 }
2401
2402 if a.vndkApex {
2403 return false
2404 }
2405
2406 // indirect dependencies
2407 am, ok := child.(android.ApexModule)
2408 if !ok {
2409 return false
2410 }
2411 // We cannot use a switch statement on `depTag` here as the checked
2412 // tags used below are private (e.g. `cc.sharedDepTag`).
2413 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2414 if ch, ok := child.(*cc.Module); ok {
2415 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2416 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2417 return false
2418 }
2419 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2420 af.transitiveDep = true
2421
2422 // Always track transitive dependencies for host.
2423 if a.Host() {
2424 vctx.filesInfo = append(vctx.filesInfo, af)
2425 return true
2426 }
2427
2428 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2429 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2430 // If the dependency is a stubs lib, don't include it in this APEX,
2431 // but make sure that the lib is installed on the device.
2432 // In case no APEX is having the lib, the lib is installed to the system
2433 // partition.
2434 //
2435 // Always include if we are a host-apex however since those won't have any
2436 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002437 //
2438 // Skip the dependency in unbundled builds where the device image is not
2439 // being built.
2440 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002441 // we need a module name for Make
2442 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002443 if !android.InList(name, a.makeModulesToInstall) {
2444 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002445 }
2446 }
2447 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2448 // Don't track further
2449 return false
2450 }
2451
2452 // If the dep is not considered to be in the same
2453 // apex, don't add it to filesInfo so that it is not
2454 // included in this APEX.
2455 // TODO(jiyong): move this to at the top of the
2456 // else-if clause for the indirect dependencies.
2457 // Currently, that's impossible because we would
2458 // like to record requiredNativeLibs even when
2459 // DepIsInSameAPex is false. We also shouldn't do
2460 // this for host.
2461 //
2462 // TODO(jiyong): explain why the same module is passed in twice.
2463 // Switching the first am to parent breaks lots of tests.
2464 if !android.IsDepInSameApex(ctx, am, am) {
2465 return false
2466 }
2467
2468 vctx.filesInfo = append(vctx.filesInfo, af)
2469 return true // track transitive dependencies
2470 } else if rm, ok := child.(*rust.Module); ok {
2471 af := apexFileForRustLibrary(ctx, rm)
2472 af.transitiveDep = true
2473 vctx.filesInfo = append(vctx.filesInfo, af)
2474 return true // track transitive dependencies
2475 }
2476 } else if cc.IsTestPerSrcDepTag(depTag) {
2477 if ch, ok := child.(*cc.Module); ok {
2478 af := apexFileForExecutable(ctx, ch)
2479 // Handle modules created as `test_per_src` variations of a single test module:
2480 // use the name of the generated test binary (`fileToCopy`) instead of the name
2481 // of the original test module (`depName`, shared by all `test_per_src`
2482 // variations of that module).
2483 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2484 // these are not considered transitive dep
2485 af.transitiveDep = false
2486 vctx.filesInfo = append(vctx.filesInfo, af)
2487 return true // track transitive dependencies
2488 }
2489 } else if cc.IsHeaderDepTag(depTag) {
2490 // nothing
2491 } else if java.IsJniDepTag(depTag) {
2492 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2493 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2494 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2495 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2496 }
2497 } else if rust.IsDylibDepTag(depTag) {
2498 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2499 af := apexFileForRustLibrary(ctx, rustm)
2500 af.transitiveDep = true
2501 vctx.filesInfo = append(vctx.filesInfo, af)
2502 return true // track transitive dependencies
2503 }
2504 } else if rust.IsRlibDepTag(depTag) {
2505 // Rlib is statically linked, but it might have shared lib
2506 // dependencies. Track them.
2507 return true
2508 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2509 // Add the contents of the bootclasspath fragment to the apex.
2510 switch child.(type) {
2511 case *java.Library, *java.SdkLibrary:
2512 javaModule := child.(javaModule)
2513 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2514 if !af.ok() {
2515 ctx.PropertyErrorf("bootclasspath_fragments",
2516 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2517 return false
2518 }
2519 vctx.filesInfo = append(vctx.filesInfo, af)
2520 return true // track transitive dependencies
2521 default:
2522 ctx.PropertyErrorf("bootclasspath_fragments",
2523 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2524 }
2525 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2526 // Add the contents of the systemserverclasspath fragment to the apex.
2527 switch child.(type) {
2528 case *java.Library, *java.SdkLibrary:
2529 af := apexFileForJavaModule(ctx, child.(javaModule))
2530 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002531 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2532 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2533 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002534 return true // track transitive dependencies
2535 default:
2536 ctx.PropertyErrorf("systemserverclasspath_fragments",
2537 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2538 }
2539 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2540 // nothing
2541 } else if depTag == android.DarwinUniversalVariantTag {
2542 // nothing
2543 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2544 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2545 }
2546 return false
2547}
2548
Jooyung Han862c0d62022-12-21 10:15:37 +09002549func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2550 // TODO(b/263308293) remove this
2551 if a.properties.IsCoverageVariant {
2552 return false
2553 }
2554 // TODO(b/263308515) remove this
2555 if a.testApex {
2556 return false
2557 }
2558 // TODO(b/263309864) remove this
2559 if a.Host() {
2560 return false
2561 }
2562 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2563 return false
2564 }
2565 return true
2566}
2567
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002568// Creates build rules for an APEX. It consists of the following major steps:
2569//
2570// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2571// 2) traverse the dependency tree to collect apexFile structs from them.
2572// 3) some fields in apexBundle struct are configured
2573// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002574func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002575 ////////////////////////////////////////////////////////////////////////////////////////////
2576 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002577 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002578 return
2579 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002580 ////////////////////////////////////////////////////////////////////////////////////////////
2581 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002582 // Collect the module directory for IDE info in java/jdeps.go.
2583 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2584
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002585 // TODO(jiyong): do this using WalkPayloadDeps
2586 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002587 vctx := visitorContext{
2588 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2589 checkDuplicate: a.shouldCheckDuplicate(ctx),
2590 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002591 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002592 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002593 if a.privateKeyFile == nil {
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07002594 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002595 return
2596 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002597
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002598 ////////////////////////////////////////////////////////////////////////////////////////////
2599 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002600 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002601 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002602
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002603 a.setApexTypeAndSuffix(ctx)
2604 a.setPayloadFsType(ctx)
2605 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002606 if a.properties.ApexType != zipApex {
2607 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2608 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002609
2610 ////////////////////////////////////////////////////////////////////////////////////////////
2611 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002612 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002613 if a.properties.ApexType == flattenedApex {
2614 a.buildFlattenedApex(ctx)
2615 } else {
2616 a.buildUnflattenedApex(ctx)
2617 }
Jiyong Park956305c2020-01-09 12:32:06 +09002618 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002619 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002620
2621 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2622 if a.installable() {
2623 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2624 // along with other ordinary files. (Note that this is done by apexer for
2625 // non-flattened APEXes)
2626 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2627
2628 // Place the public key as apex_pubkey. This is also done by apexer for
2629 // non-flattened APEXes case.
2630 // TODO(jiyong): Why do we need this CP rule?
2631 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2632 ctx.Build(pctx, android.BuildParams{
2633 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002634 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002635 Output: copiedPubkey,
2636 })
2637 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2638 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002639}
2640
Paul Duffincc33ec82021-04-25 23:14:55 +01002641// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2642// the bootclasspath_fragment contributes to the apex.
2643func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2644 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2645 var filesToAdd []apexFile
2646
2647 // Add the boot image files, e.g. .art, .oat and .vdex files.
Jiakai Zhang6decef92022-01-12 17:56:19 +00002648 if bootclasspathFragmentInfo.ShouldInstallBootImageInApex() {
2649 for arch, files := range bootclasspathFragmentInfo.AndroidBootImageFilesByArchType() {
2650 dirInApex := filepath.Join("javalib", arch.String())
2651 for _, f := range files {
2652 androidMkModuleName := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
2653 // TODO(b/177892522) - consider passing in the bootclasspath fragment module here instead of nil
2654 af := newApexFile(ctx, f, androidMkModuleName, dirInApex, etc, nil)
2655 filesToAdd = append(filesToAdd, af)
2656 }
Paul Duffincc33ec82021-04-25 23:14:55 +01002657 }
2658 }
2659
satayev3db35472021-05-06 23:59:58 +01002660 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002661 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2662 filesToAdd = append(filesToAdd, *af)
2663 }
satayev3db35472021-05-06 23:59:58 +01002664
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002665 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
2666 if pathInApex != "" && !java.SkipDexpreoptBootJars(ctx) {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002667 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2668 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2669
2670 if pathOnHost != nil {
2671 // We need to copy the profile to a temporary path with the right filename because the apexer
2672 // will take the filename as is.
2673 ctx.Build(pctx, android.BuildParams{
2674 Rule: android.Cp,
2675 Input: pathOnHost,
2676 Output: tempPath,
2677 })
2678 } else {
2679 // At this point, the boot image profile cannot be generated. It is probably because the boot
2680 // image profile source file does not exist on the branch, or it is not available for the
2681 // current build target.
2682 // However, we cannot enforce the boot image profile to be generated because some build
2683 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2684 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2685 // only if the APEX is being built.
2686 ctx.Build(pctx, android.BuildParams{
2687 Rule: android.ErrorRule,
2688 Output: tempPath,
2689 Args: map[string]string{
2690 "error": "Boot image profile cannot be generated",
2691 },
2692 })
2693 }
2694
2695 androidMkModuleName := filepath.Base(pathInApex)
2696 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2697 filesToAdd = append(filesToAdd, af)
2698 }
2699
Paul Duffincc33ec82021-04-25 23:14:55 +01002700 return filesToAdd
2701}
2702
satayevb98371c2021-06-15 16:49:50 +01002703// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2704// the module contributes to the apex; or nil if the proto config was not generated.
2705func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2706 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2707 if !info.ClasspathFragmentProtoGenerated {
2708 return nil
2709 }
2710 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2711 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2712 return &af
satayev14e49132021-05-17 21:03:07 +01002713}
2714
Paul Duffincc33ec82021-04-25 23:14:55 +01002715// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2716// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002717func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2718 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2719
2720 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2721 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002722 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2723 if err != nil {
2724 ctx.ModuleErrorf("%s", err)
2725 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002726
2727 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2728 // bootclasspath_fragment.
2729 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2730 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002731}
2732
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002733///////////////////////////////////////////////////////////////////////////////////////////////////
2734// Factory functions
2735//
2736
2737func newApexBundle() *apexBundle {
2738 module := &apexBundle{}
2739
2740 module.AddProperties(&module.properties)
2741 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002742 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002743 module.AddProperties(&module.overridableProperties)
2744
2745 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2746 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002747 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002748 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002749 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002750 return module
2751}
2752
Paul Duffineb8051d2021-10-18 17:49:39 +01002753func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002754 bundle := newApexBundle()
2755 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002756 return bundle
2757}
2758
2759// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2760// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002761func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002762 bundle := newApexBundle()
2763 bundle.testApex = true
2764 return bundle
2765}
2766
2767// apex packages other modules into an APEX file which is a packaging format for system-level
2768// components like binaries, shared libraries, etc.
2769func BundleFactory() android.Module {
2770 return newApexBundle()
2771}
2772
2773type Defaults struct {
2774 android.ModuleBase
2775 android.DefaultsModuleBase
2776}
2777
2778// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002779func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002780 module := &Defaults{}
2781
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002782 module.AddProperties(
2783 &apexBundleProperties{},
2784 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002785 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002786 &overridableProperties{},
2787 )
2788
2789 android.InitDefaultsModule(module)
2790 return module
2791}
2792
2793type OverrideApex struct {
2794 android.ModuleBase
2795 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002796 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002797}
2798
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002799func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002800 // All the overrides happen in the base module.
2801}
2802
2803// override_apex is used to create an apex module based on another apex module by overriding some of
2804// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002805func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002806 m := &OverrideApex{}
2807
2808 m.AddProperties(&overridableProperties{})
2809
2810 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2811 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002812 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002813 return m
2814}
2815
Wei Li1c66fc72022-05-09 23:59:14 -07002816func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2817 if ctx.ModuleType() != "override_apex" {
2818 return
2819 }
2820
2821 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2822 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2823 if !baseApexExists {
2824 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2825 }
2826
2827 a, baseModuleIsApex := baseModule.(*apexBundle)
2828 if !baseModuleIsApex {
2829 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2830 }
2831 attrs, props := convertWithBp2build(a, ctx)
2832
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002833 // We just want the name, not module reference.
2834 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2835 attrs.Base_apex_name = &baseApexName
2836
Wei Li1c66fc72022-05-09 23:59:14 -07002837 for _, p := range o.GetProperties() {
2838 overridableProperties, ok := p.(*overridableProperties)
2839 if !ok {
2840 continue
2841 }
Wei Li40f98732022-05-20 22:08:11 -07002842
2843 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2844 // After it is converted in convertWithBp2build(baseApex, ctx),
2845 // the attrs.Manifest.Value.Label is the file path relative to the directory
2846 // of base apex. So the following code converts it to a label that looks like
2847 // <package of base apex>:<path of manifest file> if base apex and override
2848 // apex are not in the same package.
2849 baseApexPackage := ctx.OtherModuleDir(a)
2850 overrideApexPackage := ctx.ModuleDir()
2851 if baseApexPackage != overrideApexPackage {
2852 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2853 }
2854
Wei Li1c66fc72022-05-09 23:59:14 -07002855 // Key
2856 if overridableProperties.Key != nil {
2857 attrs.Key = bazel.LabelAttribute{}
2858 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2859 }
2860
2861 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002862 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002863 // If overridableProperties.Certificate is nil, clear this out as
2864 // well with zeroed structs, so the override_apex does not use the
2865 // base apex's certificate.
2866 attrs.Certificate = bazel.LabelAttribute{}
2867 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002868 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002869 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002870 }
2871
2872 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002873 if overridableProperties.Prebuilts != nil {
2874 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2875 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2876 }
Wei Li1c66fc72022-05-09 23:59:14 -07002877
2878 // Compressible
2879 if overridableProperties.Compressible != nil {
2880 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2881 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002882
2883 // Package name
2884 //
2885 // e.g. com.android.adbd's package name is com.android.adbd, but
2886 // com.google.android.adbd overrides the package name to com.google.android.adbd
2887 //
2888 // TODO: this can be overridden from the product configuration, see
2889 // getOverrideManifestPackageName and
2890 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2891 //
2892 // Instead of generating the BUILD files differently based on the product config
2893 // at the point of conversion, this should be handled by the BUILD file loading
2894 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2895 if overridableProperties.Package_name != "" {
2896 attrs.Package_name = &overridableProperties.Package_name
2897 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002898
2899 // Logging parent
2900 if overridableProperties.Logging_parent != "" {
2901 attrs.Logging_parent = &overridableProperties.Logging_parent
2902 }
Wei Li1c66fc72022-05-09 23:59:14 -07002903 }
2904
2905 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: o.Name()}, &attrs)
2906}
2907
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002908///////////////////////////////////////////////////////////////////////////////////////////////////
2909// Vality check routines
2910//
2911// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2912// certain conditions are not met.
2913//
2914// TODO(jiyong): move these checks to a separate go file.
2915
satayevad991492021-12-03 18:58:32 +00002916var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2917
Spandan Dasa5f39a12022-08-05 02:35:52 +00002918// 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 +09002919// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002920func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002921 if a.testApex || a.vndkApex {
2922 return
2923 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002924 // apexBundle::minSdkVersion reports its own errors.
2925 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002926 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002927}
2928
Albert Martineefabcf2022-03-21 20:11:16 +00002929// Returns apex's min_sdk_version string value, honoring overrides
2930func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2931 // Only override the minSdkVersion value on Apexes which already specify
2932 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2933 // min_sdk_version value is lower than the one to override with.
zhidou133c55b2023-01-31 19:34:10 +00002934 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.overridableProperties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002935 if minApiLevel.IsNone() {
2936 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002937 }
2938
Colin Cross56534df2022-10-04 09:58:58 -07002939 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2940 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2941 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2942 minApiLevel = overrideApiLevel
2943 }
2944
2945 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002946}
2947
2948// Returns apex's min_sdk_version SdkSpec, honoring overrides
satayevad991492021-12-03 18:58:32 +00002949func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2950 return android.SdkSpec{
2951 Kind: android.SdkNone,
2952 ApiLevel: a.minSdkVersion(ctx),
Albert Martineefabcf2022-03-21 20:11:16 +00002953 Raw: a.minSdkVersionValue(ctx),
satayevad991492021-12-03 18:58:32 +00002954 }
2955}
2956
Albert Martineefabcf2022-03-21 20:11:16 +00002957// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00002958func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00002959 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
2960}
2961
2962// Construct ApiLevel object from min_sdk_version string value
2963func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
2964 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09002965 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002966 }
Albert Martineefabcf2022-03-21 20:11:16 +00002967 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002968 if err != nil {
2969 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
2970 return android.NoneApiLevel
2971 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002972 return apiLevel
2973}
2974
2975// Ensures that a lib providing stub isn't statically linked
2976func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
2977 // Practically, we only care about regular APEXes on the device.
2978 if ctx.Host() || a.testApex || a.vndkApex {
2979 return
2980 }
2981
2982 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2983
2984 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
2985 if ccm, ok := to.(*cc.Module); ok {
2986 apexName := ctx.ModuleName()
2987 fromName := ctx.OtherModuleName(from)
2988 toName := ctx.OtherModuleName(to)
2989
2990 // If `to` is not actually in the same APEX as `from` then it does not need
2991 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00002992 //
2993 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002994 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
2995 // As soon as the dependency graph crosses the APEX boundary, don't go further.
2996 return false
2997 }
2998
2999 // The dynamic linker and crash_dump tool in the runtime APEX is the only
3000 // exception to this rule. It can't make the static dependencies dynamic
3001 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09003002 // Same rule should be applied to linkerconfig, because it should be executed
3003 // only with static linked libraries before linker is available with ld.config.txt
3004 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003005 return false
3006 }
3007
3008 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
3009 if isStubLibraryFromOtherApex && !externalDep {
3010 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
3011 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
3012 }
3013
3014 }
3015 return true
3016 })
3017}
3018
satayevb98371c2021-06-15 16:49:50 +01003019// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003020func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
3021 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00003022 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003023 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
3024 }
Jiyong Park1bc84122021-06-22 20:23:05 +09003025 if a.UsePlatformApis() {
3026 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
3027 }
Daniel Norman69109112021-12-02 12:52:42 -08003028 if a.SocSpecific() || a.DeviceSpecific() {
3029 ctx.PropertyErrorf("updatable", "vendor APEXes are not updatable")
3030 }
Jiyong Parkf4020582021-11-29 12:37:10 +09003031 if a.FutureUpdatable() {
3032 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
3033 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003034 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01003035 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003036 }
3037}
3038
satayevb98371c2021-06-15 16:49:50 +01003039// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
3040func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
3041 ctx.VisitDirectDeps(func(module android.Module) {
3042 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
3043 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
3044 if !info.ClasspathFragmentProtoGenerated {
3045 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
3046 }
3047 }
3048 })
3049}
3050
3051// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003052func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003053 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3054 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003055 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3056 tag := ctx.OtherModuleDependencyTag(module)
3057 switch tag {
3058 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003059 if m, ok := module.(interface {
3060 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3061 }); ok {
3062 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003063 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3064 }
3065 }
3066 }
3067 })
3068}
3069
satayevb98371c2021-06-15 16:49:50 +01003070// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003071func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3072 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3073 if ctx.Host() || a.testApex || a.vndkApex {
3074 return
3075 }
3076
3077 // Because APEXes targeting other than system/system_ext partitions can't set
3078 // apex_available, we skip checks for these APEXes
3079 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3080 return
3081 }
3082
3083 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3084 // Requiring them and their transitive depencies with apex_available is not right
3085 // because they just add noise.
3086 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3087 return
3088 }
3089
3090 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3091 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3092 if externalDep {
3093 return false
3094 }
3095
3096 apexName := ctx.ModuleName()
3097 fromName := ctx.OtherModuleName(from)
3098 toName := ctx.OtherModuleName(to)
3099
3100 // If `to` is not actually in the same APEX as `from` then it does not need
3101 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003102 //
3103 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003104 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3105 // As soon as the dependency graph crosses the APEX boundary, don't go
3106 // further.
3107 return false
3108 }
3109
3110 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3111 return true
3112 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003113 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3114 "\n\nDependency path:%s\n\n"+
3115 "Consider adding %q to 'apex_available' property of %q",
3116 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003117 // Visit this module's dependencies to check and report any issues with their availability.
3118 return true
3119 })
3120}
3121
Jiyong Park192600a2021-08-03 07:52:17 +00003122// checkStaticExecutable ensures that executables in an APEX are not static.
3123func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003124 // No need to run this for host APEXes
3125 if ctx.Host() {
3126 return
3127 }
3128
Jiyong Park192600a2021-08-03 07:52:17 +00003129 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3130 if ctx.OtherModuleDependencyTag(module) != executableTag {
3131 return
3132 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003133
3134 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003135 apex := a.ApexVariationName()
3136 exec := ctx.OtherModuleName(module)
3137 if isStaticExecutableAllowed(apex, exec) {
3138 return
3139 }
3140 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3141 }
3142 })
3143}
3144
3145// A small list of exceptions where static executables are allowed in APEXes.
3146func isStaticExecutableAllowed(apex string, exec string) bool {
3147 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003148 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003149 "linker",
3150 "linkerconfig",
3151 },
3152 }
3153 execNames, ok := m[apex]
3154 return ok && android.InList(exec, execNames)
3155}
3156
braleeb0c1f0c2021-06-07 22:49:13 +08003157// Collect information for opening IDE project files in java/jdeps.go.
3158func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00003159 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
3160 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
3161 dpInfo.Deps = append(dpInfo.Deps, a.properties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003162 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3163}
3164
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003165var (
3166 apexAvailBaseline = makeApexAvailableBaseline()
3167 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3168)
3169
Colin Cross440e0d02020-06-11 11:32:11 -07003170func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003171 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003172 moduleName = normalizeModuleName(moduleName)
3173
Colin Cross440e0d02020-06-11 11:32:11 -07003174 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003175 return true
3176 }
3177
3178 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003179 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003180 return true
3181 }
3182
3183 return false
3184}
3185
3186func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003187 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3188 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003189 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003190 if strings.HasPrefix(moduleName, "libclang_rt.") {
3191 // This module has many arch variants that depend on the product being built.
3192 // We don't want to list them all
3193 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003194 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003195 if strings.HasPrefix(moduleName, "androidx.") {
3196 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3197 moduleName = "androidx"
3198 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003199 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003200}
3201
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003202// Transform the map of apex -> modules to module -> apexes.
3203func invertApexBaseline(m map[string][]string) map[string][]string {
3204 r := make(map[string][]string)
3205 for apex, modules := range m {
3206 for _, module := range modules {
3207 r[module] = append(r[module], apex)
3208 }
3209 }
3210 return r
3211}
3212
3213// Retrieve the baseline of apexes to which the supplied module belongs.
3214func BaselineApexAvailable(moduleName string) []string {
3215 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3216}
3217
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003218// This is a map from apex to modules, which overrides the apex_available setting for that
3219// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003220// TODO(b/147364041): remove this
3221func makeApexAvailableBaseline() map[string][]string {
3222 // The "Module separator"s below are employed to minimize merge conflicts.
3223 m := make(map[string][]string)
3224 //
3225 // Module separator
3226 //
3227 m["com.android.appsearch"] = []string{
3228 "icing-java-proto-lite",
3229 "libprotobuf-java-lite",
3230 }
3231 //
3232 // Module separator
3233 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003234 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003235 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003236 }
3237 //
3238 // Module separator
3239 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003240 m["com.android.cellbroadcast"] = []string{"CellBroadcastApp", "CellBroadcastServiceModule"}
3241 //
3242 // Module separator
3243 //
3244 m["com.android.extservices"] = []string{
3245 "error_prone_annotations",
3246 "ExtServices-core",
3247 "ExtServices",
3248 "libtextclassifier-java",
3249 "libz_current",
3250 "textclassifier-statsd",
3251 "TextClassifierNotificationLibNoManifest",
3252 "TextClassifierServiceLibNoManifest",
3253 }
3254 //
3255 // Module separator
3256 //
3257 m["com.android.neuralnetworks"] = []string{
3258 "android.hardware.neuralnetworks@1.0",
3259 "android.hardware.neuralnetworks@1.1",
3260 "android.hardware.neuralnetworks@1.2",
3261 "android.hardware.neuralnetworks@1.3",
3262 "android.hidl.allocator@1.0",
3263 "android.hidl.memory.token@1.0",
3264 "android.hidl.memory@1.0",
3265 "android.hidl.safe_union@1.0",
3266 "libarect",
3267 "libbuildversion",
3268 "libmath",
3269 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003270 }
3271 //
3272 // Module separator
3273 //
3274 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003275 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003276 }
3277 //
3278 // Module separator
3279 //
3280 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003281 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003282 }
3283 //
3284 // Module separator
3285 //
3286 m["com.android.mediaprovider"] = []string{
3287 "MediaProvider",
3288 "MediaProviderGoogle",
3289 "fmtlib_ndk",
3290 "libbase_ndk",
3291 "libfuse",
3292 "libfuse_jni",
3293 }
3294 //
3295 // Module separator
3296 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003297 m["com.android.runtime"] = []string{
3298 "bionic_libc_platform_headers",
3299 "libarm-optimized-routines-math",
3300 "libc_aeabi",
3301 "libc_bionic",
3302 "libc_bionic_ndk",
3303 "libc_bootstrap",
3304 "libc_common",
3305 "libc_common_shared",
3306 "libc_common_static",
3307 "libc_dns",
3308 "libc_dynamic_dispatch",
3309 "libc_fortify",
3310 "libc_freebsd",
3311 "libc_freebsd_large_stack",
3312 "libc_gdtoa",
3313 "libc_init_dynamic",
3314 "libc_init_static",
3315 "libc_jemalloc_wrapper",
3316 "libc_netbsd",
3317 "libc_nomalloc",
3318 "libc_nopthread",
3319 "libc_openbsd",
3320 "libc_openbsd_large_stack",
3321 "libc_openbsd_ndk",
3322 "libc_pthread",
3323 "libc_static_dispatch",
3324 "libc_syscalls",
3325 "libc_tzcode",
3326 "libc_unwind_static",
3327 "libdebuggerd",
3328 "libdebuggerd_common_headers",
3329 "libdebuggerd_handler_core",
3330 "libdebuggerd_handler_fallback",
3331 "libdl_static",
3332 "libjemalloc5",
3333 "liblinker_main",
3334 "liblinker_malloc",
3335 "liblz4",
3336 "liblzma",
3337 "libprocinfo",
3338 "libpropertyinfoparser",
3339 "libscudo",
3340 "libstdc++",
3341 "libsystemproperties",
3342 "libtombstoned_client_static",
3343 "libunwindstack",
3344 "libz",
3345 "libziparchive",
3346 }
3347 //
3348 // Module separator
3349 //
3350 m["com.android.tethering"] = []string{
3351 "android.hardware.tetheroffload.config-V1.0-java",
3352 "android.hardware.tetheroffload.control-V1.0-java",
3353 "android.hidl.base-V1.0-java",
3354 "libcgrouprc",
3355 "libcgrouprc_format",
3356 "libtetherutilsjni",
3357 "libvndksupport",
3358 "net-utils-framework-common",
3359 "netd_aidl_interface-V3-java",
3360 "netlink-client",
3361 "networkstack-aidl-interfaces-java",
3362 "tethering-aidl-interfaces-java",
3363 "TetheringApiCurrentLib",
3364 }
3365 //
3366 // Module separator
3367 //
3368 m["com.android.wifi"] = []string{
3369 "PlatformProperties",
3370 "android.hardware.wifi-V1.0-java",
3371 "android.hardware.wifi-V1.0-java-constants",
3372 "android.hardware.wifi-V1.1-java",
3373 "android.hardware.wifi-V1.2-java",
3374 "android.hardware.wifi-V1.3-java",
3375 "android.hardware.wifi-V1.4-java",
3376 "android.hardware.wifi.hostapd-V1.0-java",
3377 "android.hardware.wifi.hostapd-V1.1-java",
3378 "android.hardware.wifi.hostapd-V1.2-java",
3379 "android.hardware.wifi.supplicant-V1.0-java",
3380 "android.hardware.wifi.supplicant-V1.1-java",
3381 "android.hardware.wifi.supplicant-V1.2-java",
3382 "android.hardware.wifi.supplicant-V1.3-java",
3383 "android.hidl.base-V1.0-java",
3384 "android.hidl.manager-V1.0-java",
3385 "android.hidl.manager-V1.1-java",
3386 "android.hidl.manager-V1.2-java",
3387 "bouncycastle-unbundled",
3388 "dnsresolver_aidl_interface-V2-java",
3389 "error_prone_annotations",
3390 "framework-wifi-pre-jarjar",
3391 "framework-wifi-util-lib",
3392 "ipmemorystore-aidl-interfaces-V3-java",
3393 "ipmemorystore-aidl-interfaces-java",
3394 "ksoap2",
3395 "libnanohttpd",
3396 "libwifi-jni",
3397 "net-utils-services-common",
3398 "netd_aidl_interface-V2-java",
3399 "netd_aidl_interface-unstable-java",
3400 "netd_event_listener_interface-java",
3401 "netlink-client",
3402 "networkstack-client",
3403 "services.net",
3404 "wifi-lite-protos",
3405 "wifi-nano-protos",
3406 "wifi-service-pre-jarjar",
3407 "wifi-service-resources",
3408 }
3409 //
3410 // Module separator
3411 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003412 m["com.android.os.statsd"] = []string{
3413 "libstatssocket",
3414 }
3415 //
3416 // Module separator
3417 //
3418 m[android.AvailableToAnyApex] = []string{
3419 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx/extras support libraries
3420 "androidx",
3421 "androidx-constraintlayout_constraintlayout",
3422 "androidx-constraintlayout_constraintlayout-nodeps",
3423 "androidx-constraintlayout_constraintlayout-solver",
3424 "androidx-constraintlayout_constraintlayout-solver-nodeps",
3425 "com.google.android.material_material",
3426 "com.google.android.material_material-nodeps",
3427
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003428 "libclang_rt",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003429 "libprofile-clang-extras",
3430 "libprofile-clang-extras_ndk",
3431 "libprofile-extras",
3432 "libprofile-extras_ndk",
Ryan Prichardb35a85e2021-01-13 19:18:53 -08003433 "libunwind",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003434 }
3435 return m
3436}
3437
3438func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003439 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3440 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003441}
3442
Spandan Dasf14e2542021-11-12 00:01:37 +00003443func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3444 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3445 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003446 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003447 With("name", jar).
3448 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3449 Because(jar +
3450 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003451 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003452 " 1. If the offending code is from a statically linked library, consider " +
3453 "removing that dependency and using an alternative already in the " +
3454 "bootclasspath, or perhaps a shared library." +
3455 " 2. Move the offending code into an allowed package.\n" +
3456 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3457 "health implications of bundling that code, particularly if the offending jar " +
3458 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003459
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003460 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003461 }
3462 return rules
3463}
3464
Anton Hanssone1b18362021-12-23 15:05:38 +00003465// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003466// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003467func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003468 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003469 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003470 "android.net.ssl",
3471 "com.android.org.conscrypt",
3472 },
Wei Li40f98732022-05-20 22:08:11 -07003473 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003474 "android.media",
3475 },
3476 }
3477}
3478
Anton Hanssone1b18362021-12-23 15:05:38 +00003479// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003480// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003481func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003482 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003483 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003484 "android.provider",
3485 },
Wei Li40f98732022-05-20 22:08:11 -07003486 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003487 "android.permission",
3488 "android.app.role",
3489 "com.android.permission",
3490 "com.android.role",
3491 },
Wei Li40f98732022-05-20 22:08:11 -07003492 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003493 "android.os.ext",
3494 },
Wei Li40f98732022-05-20 22:08:11 -07003495 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003496 "android.app",
3497 "android.os",
3498 "android.util",
3499 "com.android.internal.statsd",
3500 "com.android.server.stats",
3501 },
Wei Li40f98732022-05-20 22:08:11 -07003502 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003503 "com.android.server.wifi",
3504 "com.android.wifi.x",
3505 "android.hardware.wifi",
3506 "android.net.wifi",
3507 },
Wei Li40f98732022-05-20 22:08:11 -07003508 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003509 "android.net",
3510 },
3511 }
3512}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003513
3514// For Bazel / bp2build
3515
3516type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003517 Manifest bazel.LabelAttribute
3518 Android_manifest bazel.LabelAttribute
3519 File_contexts bazel.LabelAttribute
3520 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003521 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3522 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003523 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003524 Updatable bazel.BoolAttribute
3525 Installable bazel.BoolAttribute
3526 Binaries bazel.LabelListAttribute
3527 Prebuilts bazel.LabelListAttribute
3528 Native_shared_libs_32 bazel.LabelListAttribute
3529 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003530 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003531 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003532 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003533 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003534 Base_apex_name *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003535}
3536
3537type convertedNativeSharedLibs struct {
3538 Native_shared_libs_32 bazel.LabelListAttribute
3539 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003540}
3541
Liz Kammerb83b7b02022-12-21 14:53:41 -05003542const (
3543 minSdkVersionPropName = "Min_sdk_version"
3544)
3545
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003546// ConvertWithBp2build performs bp2build conversion of an apex
3547func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003548 // We only convert apex and apex_test modules at this time
3549 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003550 return
3551 }
3552
Wei Li1c66fc72022-05-09 23:59:14 -07003553 attrs, props := convertWithBp2build(a, ctx)
Yu Liu4c212ce2022-10-14 12:20:20 -07003554 commonAttrs := android.CommonAttributes{
3555 Name: a.Name(),
3556 }
3557 if a.testApex {
3558 commonAttrs.Testonly = proptools.BoolPtr(a.testApex)
3559 }
3560 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003561}
3562
3563func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003564 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003565 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003566
3567 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003568 if a.properties.AndroidManifest != nil {
3569 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003570 }
3571
3572 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003573 if a.properties.File_contexts == nil {
3574 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3575 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3576 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3577 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003578 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003579 } else {
3580 // File_contexts is a file
3581 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003582 }
3583
Cole Faust912bc882023-03-08 12:29:50 -08003584 productVariableProps := android.ProductVariableProperties(ctx, a)
Albert Martineefabcf2022-03-21 20:11:16 +00003585 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3586 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003587 var minSdkVersion bazel.StringAttribute
zhidou133c55b2023-01-31 19:34:10 +00003588 if a.overridableProperties.Min_sdk_version != nil {
3589 minSdkVersion.SetValue(*a.overridableProperties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003590 }
3591 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3592 for c, p := range props {
3593 if val, ok := p.(*string); ok {
3594 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3595 }
3596 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003597 }
3598
3599 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003600 if a.overridableProperties.Key != nil {
3601 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003602 }
3603
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003604 // Certificate
3605 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003606
Yu Liu4ae55d12022-01-05 17:17:23 -08003607 nativeSharedLibs := &convertedNativeSharedLibs{
3608 Native_shared_libs_32: bazel.LabelListAttribute{},
3609 Native_shared_libs_64: bazel.LabelListAttribute{},
3610 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003611
3612 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3613 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3614 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3615 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3616 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003617 if a.CompileMultilib() != nil {
3618 compileMultilib = *a.CompileMultilib()
3619 }
3620
3621 // properties.Native_shared_libs is treated as "both"
3622 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3623 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3624 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3625 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3626 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003627
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003628 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003629 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3630 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3631
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003632 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003633 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003634
Yu Liu4c212ce2022-10-14 12:20:20 -07003635 var testsAttrs bazel.LabelListAttribute
3636 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3637 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3638 testsAttrs = bazel.MakeLabelListAttribute(tests)
3639 }
3640
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003641 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003642 if a.properties.Updatable != nil {
3643 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003644 }
3645
3646 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003647 if a.properties.Installable != nil {
3648 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003649 }
3650
Wei Lif034cb42022-01-19 15:54:31 -08003651 var compressibleAttribute bazel.BoolAttribute
3652 if a.overridableProperties.Compressible != nil {
3653 compressibleAttribute.Value = a.overridableProperties.Compressible
3654 }
3655
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003656 var packageName *string
3657 if a.overridableProperties.Package_name != "" {
3658 packageName = &a.overridableProperties.Package_name
3659 }
3660
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003661 var loggingParent *string
3662 if a.overridableProperties.Logging_parent != "" {
3663 loggingParent = &a.overridableProperties.Logging_parent
3664 }
3665
Wei Li1c66fc72022-05-09 23:59:14 -07003666 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003667 Manifest: manifestLabelAttribute,
3668 Android_manifest: androidManifestLabelAttribute,
3669 File_contexts: fileContextsLabelAttribute,
3670 Min_sdk_version: minSdkVersion,
3671 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003672 Certificate: certificate,
3673 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003674 Updatable: updatableAttribute,
3675 Installable: installableAttribute,
3676 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3677 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3678 Binaries: binariesLabelListAttribute,
3679 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003680 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003681 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003682 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003683 Tests: testsAttrs,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003684 }
3685
3686 props := bazel.BazelTargetModuleProperties{
3687 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003688 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003689 }
3690
Wei Li1c66fc72022-05-09 23:59:14 -07003691 return attrs, props
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003692}
Yu Liu4ae55d12022-01-05 17:17:23 -08003693
3694// The following conversions are based on this table where the rows are the compile_multilib
3695// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3696// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3697// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3698// should not be compiled.
3699// multib/compile_multilib, 32, 64, both, first
3700// 32, 32/32, none/none, 32/32, none/32
3701// 64, none/none, 64/none, 64/none, 64/none
3702// both, 32/32, 64/none, 32&64/32, 64/32
3703// first, 32/32, 64/none, 64/32, 64/32
3704
3705func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3706 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3707 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3708 switch compileMultilb {
3709 case "both", "32":
3710 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3711 case "first":
3712 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3713 case "64":
3714 // Incompatible, ignore
3715 default:
3716 invalidCompileMultilib(ctx, compileMultilb)
3717 }
3718}
3719
3720func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3721 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3722 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3723 switch compileMultilb {
3724 case "both", "64", "first":
3725 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3726 case "32":
3727 // Incompatible, ignore
3728 default:
3729 invalidCompileMultilib(ctx, compileMultilb)
3730 }
3731}
3732
3733func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3734 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3735 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3736 switch compileMultilb {
3737 case "both":
3738 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3739 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3740 case "first":
3741 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3742 case "32":
3743 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3744 case "64":
3745 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3746 default:
3747 invalidCompileMultilib(ctx, compileMultilb)
3748 }
3749}
3750
3751func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3752 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3753 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3754 switch compileMultilb {
3755 case "both", "first":
3756 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3757 case "32":
3758 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3759 case "64":
3760 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3761 default:
3762 invalidCompileMultilib(ctx, compileMultilb)
3763 }
3764}
3765
3766func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3767 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3768 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3769}
3770
3771func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3772 list := bazel.LabelListAttribute{}
3773 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3774 nativeSharedLibs.Native_shared_libs_32.Append(list)
3775}
3776
3777func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3778 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3779 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3780}
3781
3782func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3783 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3784 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3785}
3786
3787func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3788 labelListAttr *bazel.LabelListAttribute) {
3789 list := bazel.LabelListAttribute{}
3790 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3791 labelListAttr.Append(list)
3792}
3793
3794func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3795 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3796}