blob: 6a64ad6cd4c669b958d6c2490c9742fe9b38defd [file] [log] [blame]
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090015// package apex implements build rules for creating the APEX files which are container for
16// lower-level system components. See https://source.android.com/devices/tech/ota/apex
Jiyong Park48ca7dc2018-10-10 14:01:00 +090017package apex
18
19import (
20 "fmt"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090021 "path/filepath"
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +000022 "regexp"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090023 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024 "strings"
25
Yu Liu4c212ce2022-10-14 12:20:20 -070026 "android/soong/bazel/cquery"
27
Jiyong Park48ca7dc2018-10-10 14:01:00 +090028 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080029 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090030 "github.com/google/blueprint/proptools"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070031
32 "android/soong/android"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040033 "android/soong/bazel"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 "android/soong/cc"
36 prebuilt_etc "android/soong/etc"
Jiyong Park12a719c2021-01-07 15:31:24 +090037 "android/soong/filesystem"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/java"
Inseob Kim5eb7ee92022-04-27 10:30:34 +090039 "android/soong/multitree"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040 "android/soong/python"
Jiyong Park99644e92020-11-17 22:21:02 +090041 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042 "android/soong/sh"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090043)
44
Jiyong Park8e6d52f2020-11-19 14:37:47 +090045func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000046 registerApexBuildComponents(android.InitRegistrationContext)
47}
Jiyong Park8e6d52f2020-11-19 14:37:47 +090048
Paul Duffin667893c2021-03-09 22:34:13 +000049func registerApexBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("apex", BundleFactory)
Yu Liu4c212ce2022-10-14 12:20:20 -070051 ctx.RegisterModuleType("apex_test", TestApexBundleFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000052 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Cole Faust912bc882023-03-08 12:29:50 -080053 ctx.RegisterModuleType("apex_defaults", DefaultsFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000054 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Wei Li1c66fc72022-05-09 23:59:14 -070055 ctx.RegisterModuleType("override_apex", OverrideApexFactory)
Paul Duffin667893c2021-03-09 22:34:13 +000056 ctx.RegisterModuleType("apex_set", apexSetFactory)
57
Paul Duffin5dda3e32021-05-05 14:13:27 +010058 ctx.PreArchMutators(registerPreArchMutators)
Paul Duffin667893c2021-03-09 22:34:13 +000059 ctx.PreDepsMutators(RegisterPreDepsMutators)
60 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Park8e6d52f2020-11-19 14:37:47 +090061}
62
Paul Duffin5dda3e32021-05-05 14:13:27 +010063func registerPreArchMutators(ctx android.RegisterMutatorsContext) {
64 ctx.TopDown("prebuilt_apex_module_creator", prebuiltApexModuleCreatorMutator).Parallel()
65}
66
Jiyong Park8e6d52f2020-11-19 14:37:47 +090067func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
68 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
69 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
70}
71
72func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Paul Duffin949abc02020-12-08 10:34:30 +000073 ctx.TopDown("apex_info", apexInfoMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090074 ctx.BottomUp("apex_unique", apexUniqueVariationsMutator).Parallel()
75 ctx.BottomUp("apex_test_for_deps", apexTestForDepsMutator).Parallel()
76 ctx.BottomUp("apex_test_for", apexTestForMutator).Parallel()
Paul Duffin28bf7ee2021-05-12 16:41:35 +010077 // Run mark_platform_availability before the apexMutator as the apexMutator needs to know whether
78 // it should create a platform variant.
79 ctx.BottomUp("mark_platform_availability", markPlatformAvailability).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090080 ctx.BottomUp("apex", apexMutator).Parallel()
81 ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).Parallel()
82 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
Dennis Shene2ed70c2023-01-11 14:15:43 +000083 ctx.BottomUp("apex_dcla_deps", apexDCLADepsMutator).Parallel()
Spandan Das66773252022-01-15 00:23:18 +000084 // Register after apex_info mutator so that it can use ApexVariationName
85 ctx.TopDown("apex_strict_updatability_lint", apexStrictUpdatibilityLintMutator).Parallel()
Jiyong Park8e6d52f2020-11-19 14:37:47 +090086}
87
88type apexBundleProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090089 // Json manifest file describing meta info of this APEX bundle. Refer to
90 // system/apex/proto/apex_manifest.proto for the schema. Default: "apex_manifest.json"
Jiyong Park8e6d52f2020-11-19 14:37:47 +090091 Manifest *string `android:"path"`
92
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090093 // AndroidManifest.xml file used for the zip container of this APEX bundle. If unspecified,
94 // a default one is automatically generated.
Jiyong Park8e6d52f2020-11-19 14:37:47 +090095 AndroidManifest *string `android:"path"`
96
Jiyong Parkc0ec6f92020-11-19 23:00:52 +090097 // Determines the file contexts file for setting the security contexts to files in this APEX
98 // bundle. For platform APEXes, this should points to a file under /system/sepolicy Default:
99 // /system/sepolicy/apex/<module_name>_file_contexts.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900100 File_contexts *string `android:"path"`
101
Jooyung Hanaf730952023-02-28 14:13:38 +0900102 // By default, file_contexts is amended by force-labelling / and /apex_manifest.pb as system_file
103 // to avoid mistakes. When set as true, no force-labelling.
104 Use_file_contexts_as_is *bool
105
Jingwen Chendea7a642023-03-28 11:30:50 +0000106 // Path to the canned fs config file for customizing file's
107 // uid/gid/mod/capabilities. The content of this file is appended to the
108 // default config, so that the custom entries are preferred. The format is
109 // /<path_or_glob> <uid> <gid> <mode> [capabilities=0x<cap>], where
110 // path_or_glob is a path or glob pattern for a file or set of files,
111 // uid/gid are numerial values of user ID and group ID, mode is octal value
112 // for the file mode, and cap is hexadecimal value for the capability.
Jiyong Park038e8522021-12-13 23:56:35 +0900113 Canned_fs_config *string `android:"path"`
114
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900115 ApexNativeDependencies
116
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900117 Multilib apexMultilibProperties
118
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000119 // List of runtime resource overlays (RROs) that are embedded inside this APEX.
120 Rros []string
121
Anton Hanssone7545852023-02-24 11:06:07 +0000122 // List of bootclasspath fragments that are embedded inside this APEX bundle.
123 Bootclasspath_fragments []string
124
125 // List of systemserverclasspath fragments that are embedded inside this APEX bundle.
126 Systemserverclasspath_fragments []string
127
128 // List of java libraries that are embedded inside this APEX bundle.
129 Java_libs []string
130
Sundong Ahn80c04892021-11-23 00:57:19 +0000131 // List of sh binaries that are embedded inside this APEX bundle.
132 Sh_binaries []string
133
Paul Duffin3abc1742021-03-15 19:32:23 +0000134 // List of platform_compat_config files that are embedded inside this APEX bundle.
135 Compat_configs []string
136
Jiyong Park12a719c2021-01-07 15:31:24 +0900137 // List of filesystem images that are embedded inside this APEX bundle.
138 Filesystems []string
139
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900140 // Whether this APEX is considered updatable or not. When set to true, this will enforce
141 // additional rules for making sure that the APEX is truly updatable. To be updatable,
142 // min_sdk_version should be set as well. This will also disable the size optimizations like
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000143 // symlinking to the system libs. Default is true.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900144 Updatable *bool
145
Jiyong Parkf4020582021-11-29 12:37:10 +0900146 // Marks that this APEX is designed to be updatable in the future, although it's not
147 // updatable yet. This is used to mimic some of the build behaviors that are applied only to
148 // updatable APEXes. Currently, this disables the size optimization, so that the size of
149 // APEX will not increase when the APEX is actually marked as truly updatable. Default is
150 // false.
151 Future_updatable *bool
152
Jiyong Park1bc84122021-06-22 20:23:05 +0900153 // Whether this APEX can use platform APIs or not. Can be set to true only when `updatable:
154 // false`. Default is false.
155 Platform_apis *bool
156
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900157 // Whether this APEX is installable to one of the partitions like system, vendor, etc.
158 // Default: true.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900159 Installable *bool
160
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900161 // If set true, VNDK libs are considered as stable libs and are not included in this APEX.
162 // Should be only used in non-system apexes (e.g. vendor: true). Default is false.
163 Use_vndk_as_stable *bool
164
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900165 // The type of APEX to build. Controls what the APEX payload is. Either 'image', 'zip' or
166 // 'both'. When set to image, contents are stored in a filesystem image inside a zip
167 // container. When set to zip, contents are stored in a zip container directly. This type is
168 // mostly for host-side debugging. When set to both, the two types are both built. Default
169 // is 'image'.
170 Payload_type *string
171
Huang Jianan13cac632021-08-02 15:02:17 +0800172 // The type of filesystem to use when the payload_type is 'image'. Either 'ext4', 'f2fs'
173 // or 'erofs'. Default 'ext4'.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900174 Payload_fs_type *string
175
176 // For telling the APEX to ignore special handling for system libraries such as bionic.
177 // Default is false.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900178 Ignore_system_library_special_case *bool
179
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100180 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
Nikita Ioffee261ae62021-06-16 18:15:03 +0100181 // Default value is true.
Nikita Ioffeda6dc312021-06-09 19:43:46 +0100182 Generate_hashtree *bool
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900183
184 // Whenever apex_payload.img of the APEX should not be dm-verity signed. Should be only
185 // used in tests.
186 Test_only_unsigned_payload *bool
187
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000188 // Whenever apex should be compressed, regardless of product flag used. Should be only
189 // used in tests.
190 Test_only_force_compression *bool
191
Jooyung Han09c11ad2021-10-27 03:45:31 +0900192 // Put extra tags (signer=<value>) to apexkeys.txt, so that release tools can sign this apex
193 // with the tool to sign payload contents.
194 Custom_sign_tool *string
195
Dennis Shenaf41bc12022-08-03 16:46:43 +0000196 // Whether this is a dynamic common lib apex, if so the native shared libs will be placed
197 // in a special way that include the digest of the lib file under /lib(64)?
198 Dynamic_common_lib_apex *bool
199
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100200 // Canonical name of this APEX bundle. Used to determine the path to the
201 // activated APEX on device (i.e. /apex/<apexVariationName>), and used for the
202 // apex mutator variations. For override_apex modules, this is the name of the
203 // overridden base module.
204 ApexVariationName string `blueprint:"mutated"`
205
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900206 IsCoverageVariant bool `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900207
208 // List of sanitizer names that this APEX is enabled for
209 SanitizerNames []string `blueprint:"mutated"`
210
211 PreventInstall bool `blueprint:"mutated"`
212
213 HideFromMake bool `blueprint:"mutated"`
214
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900215 // Internal package method for this APEX. When payload_type is image, this can be either
216 // imageApex or flattenedApex depending on Config.FlattenApex(). When payload_type is zip,
217 // this becomes zipApex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900218 ApexType apexPackaging `blueprint:"mutated"`
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900219}
220
221type ApexNativeDependencies struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900222 // List of native libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900223 Native_shared_libs []string
224
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900225 // List of JNI libraries that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900226 Jni_libs []string
227
Colin Cross70572ed2022-11-02 13:14:20 -0700228 // List of rust dyn libraries that are embedded inside this APEX.
Jiyong Park99644e92020-11-17 22:21:02 +0900229 Rust_dyn_libs []string
230
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900231 // List of native executables that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900232 Binaries []string
233
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900234 // List of native tests that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900235 Tests []string
Jiyong Park06711462021-02-15 17:54:43 +0900236
237 // List of filesystem images that are embedded inside this APEX bundle.
238 Filesystems []string
Colin Cross70572ed2022-11-02 13:14:20 -0700239
240 // List of native libraries to exclude from this APEX.
241 Exclude_native_shared_libs []string
242
243 // List of JNI libraries to exclude from this APEX.
244 Exclude_jni_libs []string
245
246 // List of rust dyn libraries to exclude from this APEX.
247 Exclude_rust_dyn_libs []string
248
249 // List of native executables to exclude from this APEX.
250 Exclude_binaries []string
251
252 // List of native tests to exclude from this APEX.
253 Exclude_tests []string
254
255 // List of filesystem images to exclude from this APEX bundle.
256 Exclude_filesystems []string
257}
258
259// Merge combines another ApexNativeDependencies into this one
260func (a *ApexNativeDependencies) Merge(b ApexNativeDependencies) {
261 a.Native_shared_libs = append(a.Native_shared_libs, b.Native_shared_libs...)
262 a.Jni_libs = append(a.Jni_libs, b.Jni_libs...)
263 a.Rust_dyn_libs = append(a.Rust_dyn_libs, b.Rust_dyn_libs...)
264 a.Binaries = append(a.Binaries, b.Binaries...)
265 a.Tests = append(a.Tests, b.Tests...)
266 a.Filesystems = append(a.Filesystems, b.Filesystems...)
267
268 a.Exclude_native_shared_libs = append(a.Exclude_native_shared_libs, b.Exclude_native_shared_libs...)
269 a.Exclude_jni_libs = append(a.Exclude_jni_libs, b.Exclude_jni_libs...)
270 a.Exclude_rust_dyn_libs = append(a.Exclude_rust_dyn_libs, b.Exclude_rust_dyn_libs...)
271 a.Exclude_binaries = append(a.Exclude_binaries, b.Exclude_binaries...)
272 a.Exclude_tests = append(a.Exclude_tests, b.Exclude_tests...)
273 a.Exclude_filesystems = append(a.Exclude_filesystems, b.Exclude_filesystems...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900274}
275
276type apexMultilibProperties struct {
277 // Native dependencies whose compile_multilib is "first"
278 First ApexNativeDependencies
279
280 // Native dependencies whose compile_multilib is "both"
281 Both ApexNativeDependencies
282
283 // Native dependencies whose compile_multilib is "prefer32"
284 Prefer32 ApexNativeDependencies
285
286 // Native dependencies whose compile_multilib is "32"
287 Lib32 ApexNativeDependencies
288
289 // Native dependencies whose compile_multilib is "64"
290 Lib64 ApexNativeDependencies
291}
292
293type apexTargetBundleProperties struct {
294 Target struct {
295 // Multilib properties only for android.
296 Android struct {
297 Multilib apexMultilibProperties
298 }
299
300 // Multilib properties only for host.
301 Host struct {
302 Multilib apexMultilibProperties
303 }
304
305 // Multilib properties only for host linux_bionic.
306 Linux_bionic struct {
307 Multilib apexMultilibProperties
308 }
309
310 // Multilib properties only for host linux_glibc.
311 Linux_glibc struct {
312 Multilib apexMultilibProperties
313 }
314 }
315}
316
Jiyong Park59140302020-12-14 18:44:04 +0900317type apexArchBundleProperties struct {
318 Arch struct {
319 Arm struct {
320 ApexNativeDependencies
321 }
322 Arm64 struct {
323 ApexNativeDependencies
324 }
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700325 Riscv64 struct {
326 ApexNativeDependencies
327 }
Jiyong Park59140302020-12-14 18:44:04 +0900328 X86 struct {
329 ApexNativeDependencies
330 }
331 X86_64 struct {
332 ApexNativeDependencies
333 }
334 }
335}
336
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900337// These properties can be used in override_apex to override the corresponding properties in the
338// base apex.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900339type overridableProperties struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900340 // List of APKs that are embedded inside this APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900341 Apps []string
342
Daniel Norman5a3ce132021-08-26 15:44:43 -0700343 // List of prebuilt files that are embedded inside this APEX bundle.
344 Prebuilts []string
345
markchien7c803b82021-08-26 22:10:06 +0800346 // List of BPF programs inside this APEX bundle.
347 Bpfs []string
348
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900349 // Names of modules to be overridden. Listed modules can only be other binaries (in Make or
350 // Soong). This does not completely prevent installation of the overridden binaries, but if
351 // both binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will
352 // be removed from PRODUCT_PACKAGES.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900353 Overrides []string
354
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900355 // Logging parent value.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900356 Logging_parent string
357
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900358 // Apex Container package name. Override value for attribute package:name in
359 // AndroidManifest.xml
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900360 Package_name string
361
362 // A txt file containing list of files that are allowed to be included in this APEX.
363 Allowed_files *string `android:"path"`
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700364
365 // Name of the apex_key module that provides the private key to sign this APEX bundle.
366 Key *string
367
368 // Specifies the certificate and the private key to sign the zip container of this APEX. If
369 // this is "foo", foo.x509.pem and foo.pk8 under PRODUCT_DEFAULT_DEV_CERTIFICATE are used
370 // as the certificate and the private key, respectively. If this is ":module", then the
371 // certificate and the private key are provided from the android_app_certificate module
372 // named "module".
373 Certificate *string
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400374
375 // Whether this APEX can be compressed or not. Setting this property to false means this
376 // APEX will never be compressed. When set to true, APEX will be compressed if other
377 // conditions, e.g., target device needs to support APEX compression, are also fulfilled.
378 // Default: false.
379 Compressible *bool
Dennis Shene2ed70c2023-01-11 14:15:43 +0000380
381 // Trim against a specific Dynamic Common Lib APEX
382 Trim_against *string
zhidou133c55b2023-01-31 19:34:10 +0000383
384 // The minimum SDK version that this APEX must support at minimum. This is usually set to
385 // the SDK version that the APEX was first introduced.
386 Min_sdk_version *string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900387}
388
389type apexBundle struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900390 // Inherited structs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900391 android.ModuleBase
392 android.DefaultableModuleBase
393 android.OverridableModuleBase
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400394 android.BazelModuleBase
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900395 multitree.ExportableModuleBase
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900396
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900397 // Properties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900398 properties apexBundleProperties
399 targetProperties apexTargetBundleProperties
Jiyong Park59140302020-12-14 18:44:04 +0900400 archProperties apexArchBundleProperties
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900401 overridableProperties overridableProperties
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900402 vndkProperties apexVndkProperties // only for apex_vndk modules
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900403
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900404 ///////////////////////////////////////////////////////////////////////////////////////////
405 // Inputs
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900406
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900407 // Keys for apex_paylaod.img
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800408 publicKeyFile android.Path
409 privateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900410
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900411 // Cert/priv-key for the zip container
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800412 containerCertificateFile android.Path
413 containerPrivateKeyFile android.Path
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900414
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900415 // Flags for special variants of APEX
416 testApex bool
417 vndkApex bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900418
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900419 // Tells whether this variant of the APEX bundle is the primary one or not. Only the primary
420 // one gets installed to the device.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900421 primaryApexType bool
422
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900423 // Suffix of module name in Android.mk ".flattened", ".apex", ".zipapex", or ""
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900424 suffix string
425
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900426 // File system type of apex_payload.img
427 payloadFsType fsType
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900428
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900429 // Whether to create symlink to the system file instead of having a file inside the apex or
430 // not
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900431 linkToSystemLib bool
432
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900433 // List of files to be included in this APEX. This is filled in the first part of
434 // GenerateAndroidBuildActions.
435 filesInfo []apexFile
436
Jingwen Chen29743c82023-01-25 17:49:46 +0000437 // List of other module names that should be installed when this APEX gets installed (LOCAL_REQUIRED_MODULES).
438 makeModulesToInstall []string
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900439
440 ///////////////////////////////////////////////////////////////////////////////////////////
441 // Outputs (final and intermediates)
442
443 // Processed apex manifest in JSONson format (for Q)
444 manifestJsonOut android.WritablePath
445
446 // Processed apex manifest in PB format (for R+)
447 manifestPbOut android.WritablePath
448
449 // Processed file_contexts files
450 fileContexts android.WritablePath
451
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900452 // The built APEX file. This is the main product.
Jooyung Hana6d36672022-02-24 13:58:07 +0900453 // Could be .apex or .capex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900454 outputFile android.WritablePath
455
Jooyung Hana6d36672022-02-24 13:58:07 +0900456 // The built uncompressed .apex file.
457 outputApexFile android.WritablePath
458
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900459 // The built APEX file in app bundle format. This file is not directly installed to the
460 // device. For an APEX, multiple app bundles are created each of which is for a specific ABI
461 // like arm, arm64, x86, etc. Then they are processed again (outside of the Android build
462 // system) to be merged into a single app bundle file that Play accepts. See
463 // vendor/google/build/build_unbundled_mainline_module.sh for more detail.
464 bundleModuleFile android.WritablePath
465
Colin Cross6340ea52021-11-04 12:01:18 -0700466 // Target directory to install this APEX. Usually out/target/product/<device>/<partition>/apex.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900467 installDir android.InstallPath
468
Colin Cross6340ea52021-11-04 12:01:18 -0700469 // Path where this APEX was installed.
470 installedFile android.InstallPath
471
472 // Installed locations of symlinks for backward compatibility.
473 compatSymlinks android.InstallPaths
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900474
475 // Text file having the list of individual files that are included in this APEX. Used for
476 // debugging purpose.
477 installedFilesFile android.WritablePath
478
479 // List of module names that this APEX is including (to be shown via *-deps-info target).
480 // Used for debugging purpose.
481 android.ApexBundleDepsInfo
482
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900483 // Optional list of lint report zip files for apexes that contain java or app modules
484 lintReports android.Paths
485
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000486 isCompressed bool
487
sophiezc80a2b32020-11-12 16:39:19 +0000488 // Path of API coverage generate file
sophiez02347372021-11-02 17:58:02 -0700489 nativeApisUsedByModuleFile android.ModuleOutPath
490 nativeApisBackedByModuleFile android.ModuleOutPath
491 javaApisUsedByModuleFile android.ModuleOutPath
braleeb0c1f0c2021-06-07 22:49:13 +0800492
493 // Collect the module directory for IDE info in java/jdeps.go.
494 modulePaths []string
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900495}
496
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900497// apexFileClass represents a type of file that can be included in APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900498type apexFileClass int
499
Jooyung Han72bd2f82019-10-23 16:46:38 +0900500const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900501 app apexFileClass = iota
502 appSet
503 etc
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900504 goBinary
505 javaSharedLib
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900506 nativeExecutable
507 nativeSharedLib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900508 nativeTest
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900509 pyBinary
510 shBinary
Jooyung Han72bd2f82019-10-23 16:46:38 +0900511)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900512
Jingwen Chen2d37b642023-03-14 16:11:38 +0000513var (
514 classes = map[string]apexFileClass{
515 "app": app,
516 "appSet": appSet,
517 "etc": etc,
518 "goBinary": goBinary,
519 "javaSharedLib": javaSharedLib,
520 "nativeExecutable": nativeExecutable,
521 "nativeSharedLib": nativeSharedLib,
522 "nativeTest": nativeTest,
523 "pyBinary": pyBinary,
524 "shBinary": shBinary,
525 }
526)
527
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900528// apexFile represents a file in an APEX bundle. This is created during the first half of
529// GenerateAndroidBuildActions by traversing the dependencies of the APEX. Then in the second half
530// of the function, this is used to create commands that copies the files into a staging directory,
531// where they are packaged into the APEX file. This struct is also used for creating Make modules
532// for each of the files in case when the APEX is flattened.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900533type apexFile struct {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900534 // buildFile is put in the installDir inside the APEX.
Bob Badourde6a0872022-04-01 18:00:00 +0000535 builtFile android.Path
536 installDir string
Jiyong Parkce243632023-02-17 18:22:25 +0900537 partition string
Bob Badourde6a0872022-04-01 18:00:00 +0000538 customStem string
539 symlinks []string // additional symlinks
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900540
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900541 // Info for Android.mk Module name of `module` in AndroidMk. Note the generated AndroidMk
542 // module for apexFile is named something like <AndroidMk module name>.<apex name>[<apex
543 // suffix>]
544 androidMkModuleName string // becomes LOCAL_MODULE
545 class apexFileClass // becomes LOCAL_MODULE_CLASS
546 moduleDir string // becomes LOCAL_PATH
547 requiredModuleNames []string // becomes LOCAL_REQUIRED_MODULES
548 targetRequiredModuleNames []string // becomes LOCAL_TARGET_REQUIRED_MODULES
549 hostRequiredModuleNames []string // becomes LOCAL_HOST_REQUIRED_MODULES
550 dataPaths []android.DataPath // becomes LOCAL_TEST_DATA
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900551
552 jacocoReportClassesFile android.Path // only for javalibs and apps
553 lintDepSets java.LintDepSets // only for javalibs and apps
554 certificate java.Certificate // only for apps
555 overriddenPackageName string // only for apps
556
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900557 transitiveDep bool
558 isJniLib bool
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900559
Jiyong Park57621b22021-01-20 20:33:11 +0900560 multilib string
561
Jingwen Chen2d37b642023-03-14 16:11:38 +0000562 isBazelPrebuilt bool
563 unstrippedBuiltFile android.Path
564 arch string
565
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900566 // TODO(jiyong): remove this
567 module android.Module
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900568}
569
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900570// TODO(jiyong): shorten the arglist using an option struct
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900571func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, androidMkModuleName string, installDir string, class apexFileClass, module android.Module) apexFile {
572 ret := apexFile{
573 builtFile: builtFile,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900574 installDir: installDir,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900575 androidMkModuleName: androidMkModuleName,
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900576 class: class,
577 module: module,
578 }
579 if module != nil {
580 ret.moduleDir = ctx.OtherModuleDir(module)
Jiyong Parkce243632023-02-17 18:22:25 +0900581 ret.partition = module.PartitionTag(ctx.DeviceConfig())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900582 ret.requiredModuleNames = module.RequiredModuleNames()
583 ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
584 ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
Jiyong Park57621b22021-01-20 20:33:11 +0900585 ret.multilib = module.Target().Arch.ArchType.Multilib
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900586 }
587 return ret
588}
589
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900590func (af *apexFile) ok() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900591 return af.builtFile != nil && af.builtFile.String() != ""
592}
593
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900594// apexRelativePath returns the relative path of the given path from the install directory of this
595// apexFile.
596// TODO(jiyong): rename this
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900597func (af *apexFile) apexRelativePath(path string) string {
598 return filepath.Join(af.installDir, path)
599}
600
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900601// path returns path of this apex file relative to the APEX root
602func (af *apexFile) path() string {
603 return af.apexRelativePath(af.stem())
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900604}
605
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900606// stem returns the base filename of this apex file
607func (af *apexFile) stem() string {
608 if af.customStem != "" {
609 return af.customStem
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900610 }
611 return af.builtFile.Base()
612}
613
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900614// symlinkPaths returns paths of the symlinks (if any) relative to the APEX root
615func (af *apexFile) symlinkPaths() []string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900616 var ret []string
617 for _, symlink := range af.symlinks {
618 ret = append(ret, af.apexRelativePath(symlink))
619 }
620 return ret
621}
622
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900623// availableToPlatform tests whether this apexFile is from a module that can be installed to the
624// platform.
625func (af *apexFile) availableToPlatform() bool {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900626 if af.module == nil {
627 return false
628 }
629 if am, ok := af.module.(android.ApexModule); ok {
630 return am.AvailableFor(android.AvailableToPlatform)
631 }
632 return false
633}
634
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900635////////////////////////////////////////////////////////////////////////////////////////////////////
636// Mutators
637//
638// Brief description about mutators for APEX. The following three mutators are the most important
639// ones.
640//
641// 1) DepsMutator: from the properties like native_shared_libs, java_libs, etc., modules are added
642// to the (direct) dependencies of this APEX bundle.
643//
Paul Duffin949abc02020-12-08 10:34:30 +0000644// 2) apexInfoMutator: this is a post-deps mutator, so runs after DepsMutator. Its goal is to
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900645// collect modules that are direct and transitive dependencies of each APEX bundle. The collected
646// modules are marked as being included in the APEX via BuildForApex().
647//
Paul Duffin949abc02020-12-08 10:34:30 +0000648// 3) apexMutator: this is a post-deps mutator that runs after apexInfoMutator. For each module that
649// are marked by the apexInfoMutator, apex variations are created using CreateApexVariations().
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900650
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900651type dependencyTag struct {
652 blueprint.BaseDependencyTag
653 name string
654
655 // Determines if the dependent will be part of the APEX payload. Can be false for the
656 // dependencies to the signing key module, etc.
657 payload bool
Paul Duffin8c535da2021-03-17 14:51:03 +0000658
659 // True if the dependent can only be a source module, false if a prebuilt module is a suitable
660 // replacement. This is needed because some prebuilt modules do not provide all the information
661 // needed by the apex.
662 sourceOnly bool
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000663
664 // If not-nil and an APEX is a member of an SDK then dependencies of that APEX with this tag will
665 // also be added as exported members of that SDK.
666 memberType android.SdkMemberType
667}
668
669func (d *dependencyTag) SdkMemberType(_ android.Module) android.SdkMemberType {
670 return d.memberType
671}
672
673func (d *dependencyTag) ExportMember() bool {
674 return true
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900675}
676
Paul Duffin520917a2022-05-13 13:01:59 +0000677func (d *dependencyTag) String() string {
678 return fmt.Sprintf("apex.dependencyTag{%q}", d.name)
679}
680
681func (d *dependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffin8c535da2021-03-17 14:51:03 +0000682 return !d.sourceOnly
683}
684
685var _ android.ReplaceSourceWithPrebuilt = &dependencyTag{}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000686var _ android.SdkMemberDependencyTag = &dependencyTag{}
Paul Duffin8c535da2021-03-17 14:51:03 +0000687
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900688var (
Paul Duffin520917a2022-05-13 13:01:59 +0000689 androidAppTag = &dependencyTag{name: "androidApp", payload: true}
690 bpfTag = &dependencyTag{name: "bpf", payload: true}
691 certificateTag = &dependencyTag{name: "certificate"}
Dennis Shene2ed70c2023-01-11 14:15:43 +0000692 dclaTag = &dependencyTag{name: "dcla"}
Paul Duffin520917a2022-05-13 13:01:59 +0000693 executableTag = &dependencyTag{name: "executable", payload: true}
694 fsTag = &dependencyTag{name: "filesystem", payload: true}
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000695 bcpfTag = &dependencyTag{name: "bootclasspathFragment", payload: true, sourceOnly: true, memberType: java.BootclasspathFragmentSdkMemberType}
696 sscpfTag = &dependencyTag{name: "systemserverclasspathFragment", payload: true, sourceOnly: true, memberType: java.SystemServerClasspathFragmentSdkMemberType}
Paul Duffinfcf79852022-07-20 14:18:24 +0000697 compatConfigTag = &dependencyTag{name: "compatConfig", payload: true, sourceOnly: true, memberType: java.CompatConfigSdkMemberType}
Paul Duffin520917a2022-05-13 13:01:59 +0000698 javaLibTag = &dependencyTag{name: "javaLib", payload: true}
699 jniLibTag = &dependencyTag{name: "jniLib", payload: true}
700 keyTag = &dependencyTag{name: "key"}
701 prebuiltTag = &dependencyTag{name: "prebuilt", payload: true}
702 rroTag = &dependencyTag{name: "rro", payload: true}
703 sharedLibTag = &dependencyTag{name: "sharedLib", payload: true}
704 testForTag = &dependencyTag{name: "test for"}
705 testTag = &dependencyTag{name: "test", payload: true}
706 shBinaryTag = &dependencyTag{name: "shBinary", payload: true}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900707)
708
709// TODO(jiyong): shorten this function signature
710func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext, nativeModules ApexNativeDependencies, target android.Target, imageVariation string) {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900711 binVariations := target.Variations()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900712 libVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jiyong Park99644e92020-11-17 22:21:02 +0900713 rustLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900714
715 if ctx.Device() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900716 binVariations = append(binVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900717 libVariations = append(libVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
718 rustLibVariations = append(rustLibVariations, blueprint.Variation{Mutator: "image", Variation: imageVariation})
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900719 }
720
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900721 // Use *FarVariation* to be able to depend on modules having conflicting variations with
722 // this module. This is required since arch variant of an APEX bundle is 'common' but it is
723 // 'arm' or 'arm64' for native shared libs.
Colin Cross70572ed2022-11-02 13:14:20 -0700724 ctx.AddFarVariationDependencies(binVariations, executableTag,
725 android.RemoveListFromList(nativeModules.Binaries, nativeModules.Exclude_binaries)...)
726 ctx.AddFarVariationDependencies(binVariations, testTag,
727 android.RemoveListFromList(nativeModules.Tests, nativeModules.Exclude_tests)...)
728 ctx.AddFarVariationDependencies(libVariations, jniLibTag,
729 android.RemoveListFromList(nativeModules.Jni_libs, nativeModules.Exclude_jni_libs)...)
730 ctx.AddFarVariationDependencies(libVariations, sharedLibTag,
731 android.RemoveListFromList(nativeModules.Native_shared_libs, nativeModules.Exclude_native_shared_libs)...)
732 ctx.AddFarVariationDependencies(rustLibVariations, sharedLibTag,
733 android.RemoveListFromList(nativeModules.Rust_dyn_libs, nativeModules.Exclude_rust_dyn_libs)...)
734 ctx.AddFarVariationDependencies(target.Variations(), fsTag,
735 android.RemoveListFromList(nativeModules.Filesystems, nativeModules.Exclude_filesystems)...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900736}
737
738func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900739 if ctx.Device() {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900740 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
741 } else {
742 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
743 if ctx.Os().Bionic() {
744 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
745 } else {
746 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
747 }
748 }
749}
750
Jooyung Hand045ebc2022-12-06 15:23:57 +0900751// getImageVariationPair returns a pair for the image variation name as its
752// prefix and suffix. The prefix indicates whether it's core/vendor/product and the
753// suffix indicates the vndk version when it's vendor or product.
754// getImageVariation can simply join the result of this function to get the
755// image variation name.
756func (a *apexBundle) getImageVariationPair(deviceConfig android.DeviceConfig) (string, string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900757 if a.vndkApex {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900758 return cc.VendorVariationPrefix, a.vndkVersion(deviceConfig)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900759 }
760
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900761 var prefix string
762 var vndkVersion string
763 if deviceConfig.VndkVersion() != "" {
Steven Moreland2c4000c2021-04-27 02:08:49 +0000764 if a.SocSpecific() || a.DeviceSpecific() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900765 prefix = cc.VendorVariationPrefix
766 vndkVersion = deviceConfig.VndkVersion()
767 } else if a.ProductSpecific() {
768 prefix = cc.ProductVariationPrefix
769 vndkVersion = deviceConfig.ProductVndkVersion()
770 }
771 }
772 if vndkVersion == "current" {
773 vndkVersion = deviceConfig.PlatformVndkVersion()
774 }
775 if vndkVersion != "" {
Jooyung Hand045ebc2022-12-06 15:23:57 +0900776 return prefix, vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900777 }
778
Jooyung Hand045ebc2022-12-06 15:23:57 +0900779 return android.CoreVariation, "" // The usual case
780}
781
782// getImageVariation returns the image variant name for this apexBundle. In most cases, it's simply
783// android.CoreVariation, but gets complicated for the vendor APEXes and the VNDK APEX.
784func (a *apexBundle) getImageVariation(ctx android.BottomUpMutatorContext) string {
785 prefix, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig())
786 return prefix + vndkVersion
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900787}
788
789func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900790 // apexBundle is a multi-arch targets module. Arch variant of apexBundle is set to 'common'.
791 // arch-specific targets are enabled by the compile_multilib setting of the apex bundle. For
792 // each target os/architectures, appropriate dependencies are selected by their
793 // target.<os>.multilib.<type> groups and are added as (direct) dependencies.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900794 targets := ctx.MultiTargets()
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900795 imageVariation := a.getImageVariation(ctx)
796
797 a.combineProperties(ctx)
798
799 has32BitTarget := false
800 for _, target := range targets {
801 if target.Arch.ArchType.Multilib == "lib32" {
802 has32BitTarget = true
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000803 }
804 }
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900805 for i, target := range targets {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900806 // Don't include artifacts for the host cross targets because there is no way for us
807 // to run those artifacts natively on host
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900808 if target.HostCross {
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900809 continue
810 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000811
Colin Cross70572ed2022-11-02 13:14:20 -0700812 var deps ApexNativeDependencies
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000813
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900814 // Add native modules targeting both ABIs. When multilib.* is omitted for
815 // native_shared_libs/jni_libs/tests, it implies multilib.both
Colin Cross70572ed2022-11-02 13:14:20 -0700816 deps.Merge(a.properties.Multilib.Both)
817 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900818 Native_shared_libs: a.properties.Native_shared_libs,
819 Tests: a.properties.Tests,
820 Jni_libs: a.properties.Jni_libs,
821 Binaries: nil,
822 })
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900823
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900824 // Add native modules targeting the first ABI When multilib.* is omitted for
825 // binaries, it implies multilib.first
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900826 isPrimaryAbi := i == 0
827 if isPrimaryAbi {
Colin Cross70572ed2022-11-02 13:14:20 -0700828 deps.Merge(a.properties.Multilib.First)
829 deps.Merge(ApexNativeDependencies{
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900830 Native_shared_libs: nil,
831 Tests: nil,
832 Jni_libs: nil,
833 Binaries: a.properties.Binaries,
834 })
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900835 }
836
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900837 // Add native modules targeting either 32-bit or 64-bit ABI
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900838 switch target.Arch.ArchType.Multilib {
839 case "lib32":
Colin Cross70572ed2022-11-02 13:14:20 -0700840 deps.Merge(a.properties.Multilib.Lib32)
841 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900842 case "lib64":
Colin Cross70572ed2022-11-02 13:14:20 -0700843 deps.Merge(a.properties.Multilib.Lib64)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900844 if !has32BitTarget {
Colin Cross70572ed2022-11-02 13:14:20 -0700845 deps.Merge(a.properties.Multilib.Prefer32)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900846 }
847 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900848
Jiyong Park59140302020-12-14 18:44:04 +0900849 // Add native modules targeting a specific arch variant
850 switch target.Arch.ArchType {
851 case android.Arm:
Colin Cross70572ed2022-11-02 13:14:20 -0700852 deps.Merge(a.archProperties.Arch.Arm.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900853 case android.Arm64:
Colin Cross70572ed2022-11-02 13:14:20 -0700854 deps.Merge(a.archProperties.Arch.Arm64.ApexNativeDependencies)
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700855 case android.Riscv64:
Colin Cross70572ed2022-11-02 13:14:20 -0700856 deps.Merge(a.archProperties.Arch.Riscv64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900857 case android.X86:
Colin Cross70572ed2022-11-02 13:14:20 -0700858 deps.Merge(a.archProperties.Arch.X86.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900859 case android.X86_64:
Colin Cross70572ed2022-11-02 13:14:20 -0700860 deps.Merge(a.archProperties.Arch.X86_64.ApexNativeDependencies)
Jiyong Park59140302020-12-14 18:44:04 +0900861 default:
862 panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
863 }
864
Colin Cross70572ed2022-11-02 13:14:20 -0700865 addDependenciesForNativeModules(ctx, deps, target, imageVariation)
Sundong Ahn80c04892021-11-23 00:57:19 +0000866 ctx.AddFarVariationDependencies([]blueprint.Variation{
867 {Mutator: "os", Variation: target.OsVariation()},
868 {Mutator: "arch", Variation: target.ArchVariation()},
869 }, shBinaryTag, a.properties.Sh_binaries...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900870 }
871
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900872 // Common-arch dependencies come next
873 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
Anton Hansson72e7ffe2023-02-24 11:12:31 +0000874 ctx.AddFarVariationDependencies(commonVariation, rroTag, a.properties.Rros...)
Anton Hanssone7545852023-02-24 11:06:07 +0000875 ctx.AddFarVariationDependencies(commonVariation, bcpfTag, a.properties.Bootclasspath_fragments...)
876 ctx.AddFarVariationDependencies(commonVariation, sscpfTag, a.properties.Systemserverclasspath_fragments...)
877 ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
Jiyong Park12a719c2021-01-07 15:31:24 +0900878 ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
Paul Duffin0b817782021-03-17 15:02:19 +0000879 ctx.AddFarVariationDependencies(commonVariation, compatConfigTag, a.properties.Compat_configs...)
Andrei Onea115e7e72020-06-05 21:14:03 +0100880}
881
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900882// DepsMutator for the overridden properties.
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900883func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
884 if a.overridableProperties.Allowed_files != nil {
885 android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
Andrei Onea115e7e72020-06-05 21:14:03 +0100886 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900887
888 commonVariation := ctx.Config().AndroidCommonTarget.Variations()
889 ctx.AddFarVariationDependencies(commonVariation, androidAppTag, a.overridableProperties.Apps...)
markchien7c803b82021-08-26 22:10:06 +0800890 ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.overridableProperties.Bpfs...)
Daniel Norman5a3ce132021-08-26 15:44:43 -0700891 if prebuilts := a.overridableProperties.Prebuilts; len(prebuilts) > 0 {
892 // For prebuilt_etc, use the first variant (64 on 64/32bit device, 32 on 32bit device)
893 // regardless of the TARGET_PREFER_* setting. See b/144532908
894 arches := ctx.DeviceConfig().Arches()
895 if len(arches) != 0 {
896 archForPrebuiltEtc := arches[0]
897 for _, arch := range arches {
898 // Prefer 64-bit arch if there is any
899 if arch.ArchType.Multilib == "lib64" {
900 archForPrebuiltEtc = arch
901 break
902 }
903 }
904 ctx.AddFarVariationDependencies([]blueprint.Variation{
905 {Mutator: "os", Variation: ctx.Os().String()},
906 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
907 }, prebuiltTag, prebuilts...)
908 }
909 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700910
911 // Dependencies for signing
912 if String(a.overridableProperties.Key) == "" {
913 ctx.PropertyErrorf("key", "missing")
914 return
915 }
916 ctx.AddDependency(ctx.Module(), keyTag, String(a.overridableProperties.Key))
917
918 cert := android.SrcIsModule(a.getCertString(ctx))
919 if cert != "" {
920 ctx.AddDependency(ctx.Module(), certificateTag, cert)
921 // empty cert is not an error. Cert and private keys will be directly found under
922 // PRODUCT_DEFAULT_DEV_CERTIFICATE
923 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100924}
925
Dennis Shene2ed70c2023-01-11 14:15:43 +0000926func apexDCLADepsMutator(mctx android.BottomUpMutatorContext) {
927 if !mctx.Config().ApexTrimEnabled() {
928 return
929 }
930 if a, ok := mctx.Module().(*apexBundle); ok && a.overridableProperties.Trim_against != nil {
931 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
932 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(a.overridableProperties.Trim_against))
933 } else if o, ok := mctx.Module().(*OverrideApex); ok {
934 for _, p := range o.GetProperties() {
935 properties, ok := p.(*overridableProperties)
936 if !ok {
937 continue
938 }
939 if properties.Trim_against != nil {
940 commonVariation := mctx.Config().AndroidCommonTarget.Variations()
941 mctx.AddFarVariationDependencies(commonVariation, dclaTag, String(properties.Trim_against))
942 }
943 }
944 }
945}
946
947type DCLAInfo struct {
948 ProvidedLibs []string
949}
950
951var DCLAInfoProvider = blueprint.NewMutatorProvider(DCLAInfo{}, "apex_info")
952
Jiyong Park8e6d52f2020-11-19 14:37:47 +0900953type ApexBundleInfo struct {
954 Contents *android.ApexContents
Andrei Onea115e7e72020-06-05 21:14:03 +0100955}
956
Paul Duffin949abc02020-12-08 10:34:30 +0000957var ApexBundleInfoProvider = blueprint.NewMutatorProvider(ApexBundleInfo{}, "apex_info")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900958
Paul Duffina7d6a892020-12-07 17:39:59 +0000959var _ ApexInfoMutator = (*apexBundle)(nil)
960
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100961func (a *apexBundle) ApexVariationName() string {
962 return a.properties.ApexVariationName
963}
964
Paul Duffina7d6a892020-12-07 17:39:59 +0000965// ApexInfoMutator is responsible for collecting modules that need to have apex variants. They are
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900966// identified by doing a graph walk starting from an apexBundle. Basically, all the (direct and
967// indirect) dependencies are collected. But a few types of modules that shouldn't be included in
968// the apexBundle (e.g. stub libraries) are not collected. Note that a single module can be depended
969// on by multiple apexBundles. In that case, the module is collected for all of the apexBundles.
Paul Duffin949abc02020-12-08 10:34:30 +0000970//
971// For each dependency between an apex and an ApexModule an ApexInfo object describing the apex
972// is passed to that module's BuildForApex(ApexInfo) method which collates them all in a list.
973// The apexMutator uses that list to create module variants for the apexes to which it belongs.
974// The relationship between module variants and apexes is not one-to-one as variants will be
975// shared between compatible apexes.
Paul Duffina7d6a892020-12-07 17:39:59 +0000976func (a *apexBundle) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Jooyung Handf78e212020-07-22 15:54:47 +0900977
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900978 // The VNDK APEX is special. For the APEX, the membership is described in a very different
979 // way. There is no dependency from the VNDK APEX to the VNDK libraries. Instead, VNDK
980 // libraries are self-identified by their vndk.enabled properties. There is no need to run
981 // this mutator for the APEX as nothing will be collected. So, let's return fast.
982 if a.vndkApex {
983 return
984 }
985
986 // Special casing for APEXes on non-system (e.g., vendor, odm, etc.) partitions. They are
987 // provided with a property named use_vndk_as_stable, which when set to true doesn't collect
988 // VNDK libraries as transitive dependencies. This option is useful for reducing the size of
989 // the non-system APEXes because the VNDK libraries won't be included (and duped) in the
990 // APEX, but shared across APEXes via the VNDK APEX.
Jooyung Handf78e212020-07-22 15:54:47 +0900991 useVndk := a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && mctx.Config().EnforceProductPartitionInterface())
992 excludeVndkLibs := useVndk && proptools.Bool(a.properties.Use_vndk_as_stable)
Jooyung Hanc5a96762022-02-04 11:54:50 +0900993 if proptools.Bool(a.properties.Use_vndk_as_stable) {
994 if !useVndk {
995 mctx.PropertyErrorf("use_vndk_as_stable", "not supported for system/system_ext APEXes")
996 }
Jooyung Han02873da2023-03-22 17:41:03 +0900997 if a.minSdkVersionValue(mctx) != "" {
998 mctx.PropertyErrorf("use_vndk_as_stable", "not supported when min_sdk_version is set")
999 }
Jooyung Hanc5a96762022-02-04 11:54:50 +09001000 mctx.VisitDirectDepsWithTag(sharedLibTag, func(dep android.Module) {
1001 if c, ok := dep.(*cc.Module); ok && c.IsVndk() {
1002 mctx.PropertyErrorf("use_vndk_as_stable", "Trying to include a VNDK library(%s) while use_vndk_as_stable is true.", dep.Name())
1003 }
1004 })
1005 if mctx.Failed() {
1006 return
1007 }
Jooyung Handf78e212020-07-22 15:54:47 +09001008 }
1009
Colin Cross56a83212020-09-15 18:30:11 -07001010 continueApexDepsWalk := func(child, parent android.Module) bool {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001011 am, ok := child.(android.ApexModule)
1012 if !ok || !am.CanHaveApexVariants() {
1013 return false
Jiyong Parkf760cae2020-02-12 07:53:12 +09001014 }
Paul Duffin573989d2021-03-17 13:25:29 +00001015 depTag := mctx.OtherModuleDependencyTag(child)
1016
1017 // Check to see if the tag always requires that the child module has an apex variant for every
1018 // apex variant of the parent module. If it does not then it is still possible for something
1019 // else, e.g. the DepIsInSameApex(...) method to decide that a variant is required.
1020 if required, ok := depTag.(android.AlwaysRequireApexVariantTag); ok && required.AlwaysRequireApexVariant() {
1021 return true
1022 }
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001023 if !android.IsDepInSameApex(mctx, parent, child) {
Jooyung Han698dd9f2020-07-22 15:17:19 +09001024 return false
1025 }
Jooyung Handf78e212020-07-22 15:54:47 +09001026 if excludeVndkLibs {
1027 if c, ok := child.(*cc.Module); ok && c.IsVndk() {
1028 return false
1029 }
1030 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001031 // By default, all the transitive dependencies are collected, unless filtered out
1032 // above.
Colin Cross56a83212020-09-15 18:30:11 -07001033 return true
1034 }
1035
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001036 // Records whether a certain module is included in this apexBundle via direct dependency or
1037 // inndirect dependency.
1038 contents := make(map[string]android.ApexMembership)
Colin Cross56a83212020-09-15 18:30:11 -07001039 mctx.WalkDeps(func(child, parent android.Module) bool {
1040 if !continueApexDepsWalk(child, parent) {
1041 return false
1042 }
Jooyung Han698dd9f2020-07-22 15:17:19 +09001043 // If the parent is apexBundle, this child is directly depended.
1044 _, directDep := parent.(*apexBundle)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001045 depName := mctx.OtherModuleName(child)
Colin Cross56a83212020-09-15 18:30:11 -07001046 contents[depName] = contents[depName].Add(directDep)
1047 return true
1048 })
1049
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001050 // The membership information is saved for later access
Jiyong Parke4758ed2020-11-18 01:34:22 +09001051 apexContents := android.NewApexContents(contents)
Colin Cross56a83212020-09-15 18:30:11 -07001052 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
1053 Contents: apexContents,
1054 })
1055
Jooyung Haned124c32021-01-26 11:43:46 +09001056 minSdkVersion := a.minSdkVersion(mctx)
1057 // When min_sdk_version is not set, the apex is built against FutureApiLevel.
1058 if minSdkVersion.IsNone() {
1059 minSdkVersion = android.FutureApiLevel
1060 }
1061
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001062 // This is the main part of this mutator. Mark the collected dependencies that they need to
1063 // be built for this apexBundle.
Jiyong Park78349b52021-05-12 17:13:56 +09001064
Jooyung Han63dff462023-02-09 00:11:27 +00001065 apexVariationName := mctx.ModuleName() // could be com.android.foo
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001066 a.properties.ApexVariationName = apexVariationName
Spandan Dase8173a82023-04-12 17:14:11 +00001067 testApexes := []string{}
1068 if a.testApex {
1069 testApexes = []string{apexVariationName}
1070 }
Colin Cross56a83212020-09-15 18:30:11 -07001071 apexInfo := android.ApexInfo{
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001072 ApexVariationName: apexVariationName,
Jiyong Park4eab21d2021-04-15 15:17:54 +09001073 MinSdkVersion: minSdkVersion,
Colin Cross56a83212020-09-15 18:30:11 -07001074 Updatable: a.Updatable(),
Jiyong Park1bc84122021-06-22 20:23:05 +09001075 UsePlatformApis: a.UsePlatformApis(),
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001076 InApexVariants: []string{apexVariationName},
1077 InApexModules: []string{a.Name()}, // could be com.mycompany.android.foo
Colin Cross56a83212020-09-15 18:30:11 -07001078 ApexContents: []*android.ApexContents{apexContents},
Spandan Dase8173a82023-04-12 17:14:11 +00001079 TestApexes: testApexes,
Colin Cross56a83212020-09-15 18:30:11 -07001080 }
Colin Cross56a83212020-09-15 18:30:11 -07001081 mctx.WalkDeps(func(child, parent android.Module) bool {
1082 if !continueApexDepsWalk(child, parent) {
1083 return false
1084 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001085 child.(android.ApexModule).BuildForApex(apexInfo) // leave a mark!
Jooyung Han698dd9f2020-07-22 15:17:19 +09001086 return true
Jiyong Parkf760cae2020-02-12 07:53:12 +09001087 })
Dennis Shene2ed70c2023-01-11 14:15:43 +00001088
1089 if a.dynamic_common_lib_apex() {
1090 mctx.SetProvider(DCLAInfoProvider, DCLAInfo{
1091 ProvidedLibs: a.properties.Native_shared_libs,
1092 })
1093 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001094}
1095
Paul Duffina7d6a892020-12-07 17:39:59 +00001096type ApexInfoMutator interface {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001097 // ApexVariationName returns the name of the APEX variation to use in the apex
1098 // mutator etc. It is the same name as ApexInfo.ApexVariationName.
1099 ApexVariationName() string
1100
Paul Duffina7d6a892020-12-07 17:39:59 +00001101 // ApexInfoMutator implementations must call BuildForApex(ApexInfo) on any modules that are
1102 // depended upon by an apex and which require an apex specific variant.
1103 ApexInfoMutator(android.TopDownMutatorContext)
1104}
1105
1106// apexInfoMutator delegates the work of identifying which modules need an ApexInfo and apex
1107// specific variant to modules that support the ApexInfoMutator.
Spandan Das42e89502022-05-06 22:12:55 +00001108// It also propagates updatable=true to apps of updatable apexes
Paul Duffina7d6a892020-12-07 17:39:59 +00001109func apexInfoMutator(mctx android.TopDownMutatorContext) {
1110 if !mctx.Module().Enabled() {
1111 return
1112 }
1113
1114 if a, ok := mctx.Module().(ApexInfoMutator); ok {
1115 a.ApexInfoMutator(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001116 }
Spandan Das42e89502022-05-06 22:12:55 +00001117 enforceAppUpdatability(mctx)
Paul Duffina7d6a892020-12-07 17:39:59 +00001118}
1119
Spandan Das66773252022-01-15 00:23:18 +00001120// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
1121// This check is enforced for updatable modules
1122func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
1123 if !mctx.Module().Enabled() {
1124 return
1125 }
Spandan Das08c911f2022-01-21 22:07:26 +00001126 if apex, ok := mctx.Module().(*apexBundle); ok && apex.checkStrictUpdatabilityLinting() {
Spandan Das66773252022-01-15 00:23:18 +00001127 mctx.WalkDeps(func(child, parent android.Module) bool {
Spandan Dasd9c23ab2022-02-10 02:34:13 +00001128 // b/208656169 Do not propagate strict updatability linting to libcore/
1129 // These libs are available on the classpath during compilation
1130 // These libs are transitive deps of the sdk. See java/sdk.go:decodeSdkDep
1131 // Only skip libraries defined in libcore root, not subdirectories
1132 if mctx.OtherModuleDir(child) == "libcore" {
1133 // Do not traverse transitive deps of libcore/ libs
1134 return false
1135 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001136 if android.InList(child.Name(), skipLintJavalibAllowlist) {
1137 return false
1138 }
Spandan Das66773252022-01-15 00:23:18 +00001139 if lintable, ok := child.(java.LintDepSetsIntf); ok {
1140 lintable.SetStrictUpdatabilityLinting(true)
1141 }
1142 // visit transitive deps
1143 return true
1144 })
1145 }
1146}
1147
Spandan Das42e89502022-05-06 22:12:55 +00001148// enforceAppUpdatability propagates updatable=true to apps of updatable apexes
1149func enforceAppUpdatability(mctx android.TopDownMutatorContext) {
1150 if !mctx.Module().Enabled() {
1151 return
1152 }
1153 if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
1154 // checking direct deps is sufficient since apex->apk is a direct edge, even when inherited via apex_defaults
1155 mctx.VisitDirectDeps(func(module android.Module) {
1156 // ignore android_test_app
1157 if app, ok := module.(*java.AndroidApp); ok {
1158 app.SetUpdatable(true)
1159 }
1160 })
1161 }
1162}
1163
Spandan Das08c911f2022-01-21 22:07:26 +00001164// TODO: b/215736885 Whittle the denylist
1165// Transitive deps of certain mainline modules baseline NewApi errors
1166// Skip these mainline modules for now
1167var (
1168 skipStrictUpdatabilityLintAllowlist = []string{
1169 "com.android.art",
1170 "com.android.art.debug",
1171 "com.android.conscrypt",
1172 "com.android.media",
1173 // test apexes
1174 "test_com.android.art",
1175 "test_com.android.conscrypt",
1176 "test_com.android.media",
1177 "test_jitzygote_com.android.art",
1178 }
Spandan Das2cf278e2022-03-24 20:19:35 +00001179
1180 // TODO: b/215736885 Remove this list
1181 skipLintJavalibAllowlist = []string{
1182 "conscrypt.module.platform.api.stubs",
1183 "conscrypt.module.public.api.stubs",
1184 "conscrypt.module.public.api.stubs.system",
1185 "conscrypt.module.public.api.stubs.module_lib",
1186 "framework-media.stubs",
1187 "framework-media.stubs.system",
1188 "framework-media.stubs.module_lib",
1189 }
Spandan Das08c911f2022-01-21 22:07:26 +00001190)
1191
1192func (a *apexBundle) checkStrictUpdatabilityLinting() bool {
1193 return a.Updatable() && !android.InList(a.ApexVariationName(), skipStrictUpdatabilityLintAllowlist)
1194}
1195
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001196// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
1197// unique apex variations for this module. See android/apex.go for more about unique apex variant.
1198// TODO(jiyong): move this to android/apex.go?
Colin Crossaede88c2020-08-11 12:17:01 -07001199func apexUniqueVariationsMutator(mctx android.BottomUpMutatorContext) {
1200 if !mctx.Module().Enabled() {
1201 return
1202 }
1203 if am, ok := mctx.Module().(android.ApexModule); ok {
Colin Cross56a83212020-09-15 18:30:11 -07001204 android.UpdateUniqueApexVariationsForDeps(mctx, am)
1205 }
1206}
1207
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001208// apexTestForDepsMutator checks if this module is a test for an apex. If so, add a dependency on
1209// the apex in order to retrieve its contents later.
1210// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001211func apexTestForDepsMutator(mctx android.BottomUpMutatorContext) {
1212 if !mctx.Module().Enabled() {
1213 return
1214 }
Colin Cross56a83212020-09-15 18:30:11 -07001215 if am, ok := mctx.Module().(android.ApexModule); ok {
1216 if testFor := am.TestFor(); len(testFor) > 0 {
1217 mctx.AddFarVariationDependencies([]blueprint.Variation{
1218 {Mutator: "os", Variation: am.Target().OsVariation()},
1219 {"arch", "common"},
1220 }, testForTag, testFor...)
1221 }
1222 }
1223}
1224
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001225// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001226func apexTestForMutator(mctx android.BottomUpMutatorContext) {
1227 if !mctx.Module().Enabled() {
1228 return
1229 }
Colin Cross56a83212020-09-15 18:30:11 -07001230 if _, ok := mctx.Module().(android.ApexModule); ok {
1231 var contents []*android.ApexContents
1232 for _, testFor := range mctx.GetDirectDepsWithTag(testForTag) {
1233 abInfo := mctx.OtherModuleProvider(testFor, ApexBundleInfoProvider).(ApexBundleInfo)
1234 contents = append(contents, abInfo.Contents)
1235 }
1236 mctx.SetProvider(android.ApexTestForInfoProvider, android.ApexTestForInfo{
1237 ApexContents: contents,
1238 })
Colin Crossaede88c2020-08-11 12:17:01 -07001239 }
1240}
1241
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001242// markPlatformAvailability marks whether or not a module can be available to platform. A module
1243// cannot be available to platform if 1) it is explicitly marked as not available (i.e.
1244// "//apex_available:platform" is absent) or 2) it depends on another module that isn't (or can't
1245// be) available to platform
1246// TODO(jiyong): move this to android/apex.go?
Jiyong Park89e850a2020-04-07 16:37:39 +09001247func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
1248 // Host and recovery are not considered as platform
1249 if mctx.Host() || mctx.Module().InstallInRecovery() {
1250 return
1251 }
1252
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001253 am, ok := mctx.Module().(android.ApexModule)
1254 if !ok {
1255 return
1256 }
Jiyong Park89e850a2020-04-07 16:37:39 +09001257
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001258 availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
Jiyong Park89e850a2020-04-07 16:37:39 +09001259
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001260 // If any of the dep is not available to platform, this module is also considered as being
1261 // not available to platform even if it has "//apex_available:platform"
1262 mctx.VisitDirectDeps(func(child android.Module) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +00001263 if !android.IsDepInSameApex(mctx, am, child) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001264 // if the dependency crosses apex boundary, don't consider it
1265 return
Jiyong Park89e850a2020-04-07 16:37:39 +09001266 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001267 if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
1268 availableToPlatform = false
1269 // TODO(b/154889534) trigger an error when 'am' has
1270 // "//apex_available:platform"
Jiyong Park89e850a2020-04-07 16:37:39 +09001271 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001272 })
Jiyong Park89e850a2020-04-07 16:37:39 +09001273
Paul Duffinb5769c12021-05-12 16:16:51 +01001274 // Exception 1: check to see if the module always requires it.
1275 if am.AlwaysRequiresPlatformApexVariant() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001276 availableToPlatform = true
1277 }
1278
1279 // Exception 2: bootstrap bionic libraries are also always available to platform
1280 if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
1281 availableToPlatform = true
1282 }
1283
1284 if !availableToPlatform {
1285 am.SetNotAvailableForPlatform()
Jiyong Park89e850a2020-04-07 16:37:39 +09001286 }
1287}
1288
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001289// apexMutator visits each module and creates apex variations if the module was marked in the
Paul Duffin949abc02020-12-08 10:34:30 +00001290// previous run of apexInfoMutator.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001291func apexMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001292 if !mctx.Module().Enabled() {
1293 return
1294 }
Colin Cross56a83212020-09-15 18:30:11 -07001295
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001296 // This is the usual path.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001297 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Colin Cross56a83212020-09-15 18:30:11 -07001298 android.CreateApexVariations(mctx, am)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001299 return
1300 }
1301
1302 // apexBundle itself is mutated so that it and its dependencies have the same apex variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001303 if ai, ok := mctx.Module().(ApexInfoMutator); ok && apexModuleTypeRequiresVariant(ai) {
1304 apexBundleName := ai.ApexVariationName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001305 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001306 if strings.HasPrefix(apexBundleName, "com.android.art") {
1307 // Create an alias from the platform variant. This is done to make
1308 // test_for dependencies work for modules that are split by the APEX
1309 // mutator, since test_for dependencies always go to the platform variant.
1310 // This doesn't happen for normal APEXes that are disjunct, so only do
1311 // this for the overlapping ART APEXes.
1312 // TODO(b/183882457): Remove this if the test_for functionality is
1313 // refactored to depend on the proper APEX variants instead of platform.
1314 mctx.CreateAliasVariation("", apexBundleName)
1315 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001316 } else if o, ok := mctx.Module().(*OverrideApex); ok {
1317 apexBundleName := o.GetOverriddenModuleName()
1318 if apexBundleName == "" {
1319 mctx.ModuleErrorf("base property is not set")
1320 return
1321 }
1322 mctx.CreateVariations(apexBundleName)
Martin Stjernholmec009002021-03-27 15:18:31 +00001323 if strings.HasPrefix(apexBundleName, "com.android.art") {
1324 // TODO(b/183882457): See note for CreateAliasVariation above.
1325 mctx.CreateAliasVariation("", apexBundleName)
1326 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001327 }
1328}
Sundong Ahne9b55722019-09-06 17:37:42 +09001329
Paul Duffin6717d882021-06-15 19:09:41 +01001330// apexModuleTypeRequiresVariant determines whether the module supplied requires an apex specific
1331// variant.
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001332func apexModuleTypeRequiresVariant(module ApexInfoMutator) bool {
Paul Duffin6717d882021-06-15 19:09:41 +01001333 if a, ok := module.(*apexBundle); ok {
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001334 // TODO(jiyong): document the reason why the VNDK APEX is an exception here.
Paul Duffin6717d882021-06-15 19:09:41 +01001335 return !a.vndkApex
1336 }
1337
Martin Stjernholmbfffae72021-06-24 14:37:13 +01001338 return true
Paul Duffin6717d882021-06-15 19:09:41 +01001339}
1340
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001341// See android.UpdateDirectlyInAnyApex
1342// TODO(jiyong): move this to android/apex.go?
Colin Cross56a83212020-09-15 18:30:11 -07001343func apexDirectlyInAnyMutator(mctx android.BottomUpMutatorContext) {
1344 if !mctx.Module().Enabled() {
1345 return
1346 }
1347 if am, ok := mctx.Module().(android.ApexModule); ok {
1348 android.UpdateDirectlyInAnyApex(mctx, am)
1349 }
1350}
1351
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001352// apexPackaging represents a specific packaging method for an APEX.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001353type apexPackaging int
1354
1355const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001356 // imageApex is a packaging method where contents are included in a filesystem image which
1357 // is then included in a zip container. This is the most typical way of packaging.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001358 imageApex apexPackaging = iota
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001359
1360 // zipApex is a packaging method where contents are directly included in the zip container.
1361 // This is used for host-side testing - because the contents are easily accessible by
1362 // unzipping the container.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001363 zipApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001364
1365 // flattendApex is a packaging method where contents are not included in the APEX file, but
1366 // installed to /apex/<apexname> directory on the device. This packaging method is used for
1367 // old devices where the filesystem-based APEX file can't be supported.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001368 flattenedApex
1369)
1370
1371const (
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001372 // File extensions of an APEX for different packaging methods
Samiul Islam7c02e262021-09-08 17:48:28 +01001373 imageApexSuffix = ".apex"
1374 imageCapexSuffix = ".capex"
1375 zipApexSuffix = ".zipapex"
1376 flattenedSuffix = ".flattened"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001377
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001378 // variant names each of which is for a packaging method
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001379 imageApexType = "image"
1380 zipApexType = "zip"
1381 flattenedApexType = "flattened"
1382
Dan Willemsen47e1a752021-10-16 18:36:13 -07001383 ext4FsType = "ext4"
1384 f2fsFsType = "f2fs"
Huang Jianan13cac632021-08-02 15:02:17 +08001385 erofsFsType = "erofs"
Jiyong Park8e6d52f2020-11-19 14:37:47 +09001386)
1387
1388// The suffix for the output "file", not the module
1389func (a apexPackaging) suffix() string {
1390 switch a {
1391 case imageApex:
1392 return imageApexSuffix
1393 case zipApex:
1394 return zipApexSuffix
1395 default:
1396 panic(fmt.Errorf("unknown APEX type %d", a))
1397 }
1398}
1399
1400func (a apexPackaging) name() string {
1401 switch a {
1402 case imageApex:
1403 return imageApexType
1404 case zipApex:
1405 return zipApexType
1406 default:
1407 panic(fmt.Errorf("unknown APEX type %d", a))
1408 }
1409}
1410
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001411// apexFlattenedMutator creates one or more variations each of which is for a packaging method.
1412// TODO(jiyong): give a better name to this mutator
Sundong Ahne9b55722019-09-06 17:37:42 +09001413func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Jooyung Han49f67012020-04-17 13:43:10 +09001414 if !mctx.Module().Enabled() {
1415 return
1416 }
Sundong Ahne8fb7242019-09-17 13:50:45 +09001417 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +09001418 var variants []string
1419 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
1420 case "image":
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001421 // This is the normal case. Note that both image and flattend APEXes are
1422 // created. The image type is installed to the system partition, while the
1423 // flattened APEX is (optionally) installed to the system_ext partition.
1424 // This is mostly for GSI which has to support wide range of devices. If GSI
1425 // is installed on a newer (APEX-capable) device, the image APEX in the
1426 // system will be used. However, if the same GSI is installed on an old
1427 // device which can't support image APEX, the flattened APEX in the
1428 // system_ext partion (which still is part of GSI) is used instead.
Sundong Ahnabb64432019-10-22 13:58:29 +09001429 variants = append(variants, imageApexType, flattenedApexType)
1430 case "zip":
1431 variants = append(variants, zipApexType)
1432 case "both":
1433 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
1434 default:
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001435 mctx.PropertyErrorf("payload_type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +09001436 return
1437 }
1438
1439 modules := mctx.CreateLocalVariations(variants...)
1440
1441 for i, v := range variants {
1442 switch v {
1443 case imageApexType:
1444 modules[i].(*apexBundle).properties.ApexType = imageApex
1445 case zipApexType:
1446 modules[i].(*apexBundle).properties.ApexType = zipApex
1447 case flattenedApexType:
1448 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001449 // See the comment above for why system_ext.
Jooyung Han91df2082019-11-20 01:49:42 +09001450 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09001451 modules[i].(*apexBundle).MakeAsSystemExt()
1452 }
Sundong Ahnabb64432019-10-22 13:58:29 +09001453 }
Sundong Ahne9b55722019-09-06 17:37:42 +09001454 }
Jiyong Park5d790c32019-11-15 18:40:32 +09001455 } else if _, ok := mctx.Module().(*OverrideApex); ok {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001456 // payload_type is forcibly overridden to "image"
1457 // TODO(jiyong): is this the right decision?
Jiyong Park5d790c32019-11-15 18:40:32 +09001458 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +09001459 }
1460}
1461
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001462var _ android.DepIsInSameApex = (*apexBundle)(nil)
Theotime Combes4ba38c12020-06-12 12:46:59 +00001463
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001464// Implements android.DepInInSameApex
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001465func (a *apexBundle) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001466 // direct deps of an APEX bundle are all part of the APEX bundle
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001467 // TODO(jiyong): shouldn't we look into the payload field of the dependencyTag?
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001468 return true
1469}
1470
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001471var _ android.OutputFileProducer = (*apexBundle)(nil)
1472
1473// Implements android.OutputFileProducer
1474func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1475 switch tag {
Paul Duffin74f05592020-11-25 16:37:46 +00001476 case "", android.DefaultDistTag:
1477 // This is the default dist path.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001478 return android.Paths{a.outputFile}, nil
Jooyung Hana6d36672022-02-24 13:58:07 +09001479 case imageApexSuffix:
1480 // uncompressed one
1481 if a.outputApexFile != nil {
1482 return android.Paths{a.outputApexFile}, nil
1483 }
1484 fallthrough
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001485 default:
1486 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1487 }
1488}
1489
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001490var _ multitree.Exportable = (*apexBundle)(nil)
1491
1492func (a *apexBundle) Exportable() bool {
1493 if a.properties.ApexType == flattenedApex {
1494 return false
1495 }
1496 return true
1497}
1498
1499func (a *apexBundle) TaggedOutputs() map[string]android.Paths {
1500 ret := make(map[string]android.Paths)
1501 ret["apex"] = android.Paths{a.outputFile}
1502 return ret
1503}
1504
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001505var _ cc.Coverage = (*apexBundle)(nil)
1506
1507// Implements cc.Coverage
1508func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1509 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1510}
1511
1512// Implements cc.Coverage
Ivan Lozanod7586b62021-04-01 09:49:36 -04001513func (a *apexBundle) SetPreventInstall() {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001514 a.properties.PreventInstall = true
1515}
1516
1517// Implements cc.Coverage
1518func (a *apexBundle) HideFromMake() {
1519 a.properties.HideFromMake = true
Colin Crosse6a83e62020-12-17 18:22:34 -08001520 // This HideFromMake is shadowing the ModuleBase one, call through to it for now.
1521 // TODO(ccross): untangle these
1522 a.ModuleBase.HideFromMake()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001523}
1524
1525// Implements cc.Coverage
1526func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1527 a.properties.IsCoverageVariant = coverage
1528}
1529
1530// Implements cc.Coverage
1531func (a *apexBundle) EnableCoverageIfNeeded() {}
1532
1533var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1534
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -04001535// Implements android.ApexBundleDepsInfoIntf
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001536func (a *apexBundle) Updatable() bool {
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001537 return proptools.BoolDefault(a.properties.Updatable, true)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001538}
1539
Jiyong Parkf4020582021-11-29 12:37:10 +09001540func (a *apexBundle) FutureUpdatable() bool {
1541 return proptools.BoolDefault(a.properties.Future_updatable, false)
1542}
1543
Jiyong Park1bc84122021-06-22 20:23:05 +09001544func (a *apexBundle) UsePlatformApis() bool {
1545 return proptools.BoolDefault(a.properties.Platform_apis, false)
1546}
1547
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001548// getCertString returns the name of the cert that should be used to sign this APEX. This is
1549// basically from the "certificate" property, but could be overridden by the device config.
Colin Cross0ea8ba82019-06-06 14:33:29 -07001550func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jooyung Han27151d92019-12-16 17:45:32 +09001551 moduleName := ctx.ModuleName()
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001552 // VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the
1553 // OVERRIDE_* list, we check with the pseudo module name to see if its certificate is
1554 // overridden.
Jooyung Han27151d92019-12-16 17:45:32 +09001555 if a.vndkApex {
1556 moduleName = vndkApexName
1557 }
1558 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001559 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001560 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001561 }
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07001562 return String(a.overridableProperties.Certificate)
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001563}
1564
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001565// See the installable property
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001566func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +09001567 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001568}
1569
Nikita Ioffeda6dc312021-06-09 19:43:46 +01001570// See the generate_hashtree property
1571func (a *apexBundle) shouldGenerateHashtree() bool {
Nikita Ioffee261ae62021-06-16 18:15:03 +01001572 return proptools.BoolDefault(a.properties.Generate_hashtree, true)
Nikita Ioffec72b5dd2019-12-07 17:30:22 +00001573}
1574
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001575// See the test_only_unsigned_payload property
Dario Frenica913392020-04-27 18:21:11 +01001576func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1577 return proptools.Bool(a.properties.Test_only_unsigned_payload)
1578}
1579
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +00001580// See the test_only_force_compression property
1581func (a *apexBundle) testOnlyShouldForceCompression() bool {
1582 return proptools.Bool(a.properties.Test_only_force_compression)
1583}
1584
Dennis Shenaf41bc12022-08-03 16:46:43 +00001585// See the dynamic_common_lib_apex property
1586func (a *apexBundle) dynamic_common_lib_apex() bool {
1587 return proptools.BoolDefault(a.properties.Dynamic_common_lib_apex, false)
1588}
1589
Dennis Shene2ed70c2023-01-11 14:15:43 +00001590// See the list of libs to trim
1591func (a *apexBundle) libs_to_trim(ctx android.ModuleContext) []string {
1592 dclaModules := ctx.GetDirectDepsWithTag(dclaTag)
1593 if len(dclaModules) > 1 {
1594 panic(fmt.Errorf("expected exactly at most one dcla dependency, got %d", len(dclaModules)))
1595 }
1596 if len(dclaModules) > 0 {
1597 DCLAInfo := ctx.OtherModuleProvider(dclaModules[0], DCLAInfoProvider).(DCLAInfo)
1598 return DCLAInfo.ProvidedLibs
1599 }
1600 return []string{}
1601}
1602
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001603// These functions are interfacing with cc/sanitizer.go. The entire APEX (along with all of its
1604// members) can be sanitized, either forcibly, or by the global configuration. For some of the
1605// sanitizers, extra dependencies can be forcibly added as well.
Jiyong Parkda6eb592018-12-19 17:12:36 +09001606
Jiyong Parkf97782b2019-02-13 20:28:58 +09001607func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1608 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1609 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1610 }
1611}
1612
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001613func (a *apexBundle) IsSanitizerEnabled(config android.Config, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +09001614 if android.InList(sanitizerName, a.properties.SanitizerNames) {
1615 return true
Jiyong Park235e67c2019-02-09 11:50:56 +09001616 }
1617
1618 // Then follow the global setting
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07001619 var globalSanitizerNames []string
Jiyong Park388ef3f2019-01-28 19:47:32 +09001620 if a.Host() {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001621 globalSanitizerNames = config.SanitizeHost()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001622 } else {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001623 arches := config.SanitizeDeviceArch()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001624 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001625 globalSanitizerNames = config.SanitizeDevice()
Jiyong Park388ef3f2019-01-28 19:47:32 +09001626 }
1627 }
1628 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +09001629}
1630
Jooyung Han8ce8db92020-05-15 19:05:05 +09001631func (a *apexBundle) AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001632 // TODO(jiyong): move this info (the sanitizer name, the lib name, etc.) to cc/sanitize.go
1633 // Keep only the mechanism here.
Jooyung Han8ce8db92020-05-15 19:05:05 +09001634 if ctx.Device() && sanitizerName == "hwaddress" && strings.HasPrefix(a.Name(), "com.android.runtime") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001635 imageVariation := a.getImageVariation(ctx)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001636 for _, target := range ctx.MultiTargets() {
1637 if target.Arch.ArchType.Multilib == "lib64" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001638 addDependenciesForNativeModules(ctx, ApexNativeDependencies{
Colin Cross4c4c1be2022-02-10 11:41:18 -08001639 Native_shared_libs: []string{"libclang_rt.hwasan"},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001640 Tests: nil,
1641 Jni_libs: nil,
1642 Binaries: nil,
1643 }, target, imageVariation)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001644 break
1645 }
1646 }
1647 }
1648}
1649
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001650// apexFileFor<Type> functions below create an apexFile struct for a given Soong module. The
1651// returned apexFile saves information about the Soong module that will be used for creating the
1652// build rules.
Jiyong Park1833cef2019-12-13 13:28:36 +09001653func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001654 // Decide the APEX-local directory by the multilib of the library In the future, we may
1655 // query this to the module.
1656 // TODO(jiyong): use the new PackagingSpec
Jiyong Parkf653b052019-11-18 15:39:01 +09001657 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +01001658 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001659 case "lib32":
1660 dirInApex = "lib"
1661 case "lib64":
1662 dirInApex = "lib64"
1663 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001664 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +01001665 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001666 }
Jiyong Park1833cef2019-12-13 13:28:36 +09001667 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001668 // Special case for Bionic libs and other libs installed with them. This is to
1669 // prevent those libs from being included in the search path
1670 // /apex/com.android.runtime/${LIB}. This exclusion is required because those libs
1671 // in the Runtime APEX are available via the legacy paths in /system/lib/. By the
1672 // init process, the libs in the APEX are bind-mounted to the legacy paths and thus
1673 // will be loaded into the default linker namespace (aka "platform" namespace). If
1674 // the libs are directly in /apex/com.android.runtime/${LIB} then the same libs will
1675 // be loaded again into the runtime linker namespace, which will result in double
1676 // loading of them, which isn't supported.
Martin Stjernholm279de572019-09-10 23:18:20 +01001677 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +09001678 }
Florian Mayer95cd6db2023-03-23 17:48:07 -07001679 // This needs to go after the runtime APEX handling because otherwise we would get
1680 // weird paths like lib64/rel_install_path/bionic rather than
1681 // lib64/bionic/rel_install_path.
1682 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001683
Colin Cross1d487152022-10-03 19:14:46 -07001684 fileToCopy := android.OutputFileForModule(ctx, ccMod, "")
Yo Chiange8128052020-07-23 20:09:18 +08001685 androidMkModuleName := ccMod.BaseModuleName() + ccMod.Properties.SubName
1686 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001687}
1688
Jiyong Park1833cef2019-12-13 13:28:36 +09001689func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jooyung Han35155c42020-02-06 17:33:20 +09001690 dirInApex := "bin"
Colin Cross3b19f5d2019-09-17 14:45:31 -07001691 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +02001692 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +09001693 }
Jooyung Han35155c42020-02-06 17:33:20 +09001694 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Colin Cross1d487152022-10-03 19:14:46 -07001695 fileToCopy := android.OutputFileForModule(ctx, cc, "")
Yo Chiange8128052020-07-23 20:09:18 +08001696 androidMkModuleName := cc.BaseModuleName() + cc.Properties.SubName
1697 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +09001698 af.symlinks = cc.Symlinks()
Liz Kammer1c14a212020-05-12 15:26:55 -07001699 af.dataPaths = cc.DataPaths()
Jiyong Parkf653b052019-11-18 15:39:01 +09001700 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001701}
1702
Jiyong Park99644e92020-11-17 22:21:02 +09001703func apexFileForRustExecutable(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1704 dirInApex := "bin"
1705 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1706 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1707 }
Colin Cross1d487152022-10-03 19:14:46 -07001708 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001709 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1710 af := newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeExecutable, rustm)
1711 return af
1712}
1713
1714func apexFileForRustLibrary(ctx android.BaseModuleContext, rustm *rust.Module) apexFile {
1715 // Decide the APEX-local directory by the multilib of the library
1716 // In the future, we may query this to the module.
1717 var dirInApex string
1718 switch rustm.Arch().ArchType.Multilib {
1719 case "lib32":
1720 dirInApex = "lib"
1721 case "lib64":
1722 dirInApex = "lib64"
1723 }
1724 if rustm.Target().NativeBridge == android.NativeBridgeEnabled {
1725 dirInApex = filepath.Join(dirInApex, rustm.Target().NativeBridgeRelativePath)
1726 }
Colin Cross1d487152022-10-03 19:14:46 -07001727 fileToCopy := android.OutputFileForModule(ctx, rustm, "")
Jiyong Park99644e92020-11-17 22:21:02 +09001728 androidMkModuleName := rustm.BaseModuleName() + rustm.Properties.SubName
1729 return newApexFile(ctx, fileToCopy, androidMkModuleName, dirInApex, nativeSharedLib, rustm)
1730}
1731
Cole Faust4d247e62023-01-23 10:14:58 -08001732func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.PythonBinaryModule) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001733 dirInApex := "bin"
1734 fileToCopy := py.HostToolPath().Path()
Yo Chiange8128052020-07-23 20:09:18 +08001735 return newApexFile(ctx, fileToCopy, py.BaseModuleName(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -08001736}
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001737
Jiyong Park1833cef2019-12-13 13:28:36 +09001738func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001739 dirInApex := "bin"
Colin Crossa44551f2021-10-25 15:36:21 -07001740 fileToCopy := android.PathForGoBinary(ctx, gb)
Jiyong Parkf653b052019-11-18 15:39:01 +09001741 // NB: Since go binaries are static we don't need the module for anything here, which is
1742 // good since the go tool is a blueprint.Module not an android.Module like we would
1743 // normally use.
Jingwen Chen2d37b642023-03-14 16:11:38 +00001744 //
Jiyong Park1833cef2019-12-13 13:28:36 +09001745 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -08001746}
1747
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001748func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +09001749 dirInApex := filepath.Join("bin", sh.SubDir())
Sundong Ahn80c04892021-11-23 00:57:19 +00001750 if sh.Target().NativeBridge == android.NativeBridgeEnabled {
1751 dirInApex = filepath.Join(dirInApex, sh.Target().NativeBridgeRelativePath)
1752 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001753 fileToCopy := sh.OutputFile()
Yo Chiange8128052020-07-23 20:09:18 +08001754 af := newApexFile(ctx, fileToCopy, sh.BaseModuleName(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +09001755 af.symlinks = sh.Symlinks()
1756 return af
Jiyong Park04480cf2019-02-06 00:16:29 +09001757}
1758
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001759func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
Jooyung Han0703fd82020-08-26 22:11:53 +09001760 dirInApex := filepath.Join(prebuilt.BaseDir(), prebuilt.SubDir())
Jiyong Parkf653b052019-11-18 15:39:01 +09001761 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +09001762 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001763}
1764
atrost6e126252020-01-27 17:01:16 +00001765func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1766 dirInApex := filepath.Join("etc", config.SubDir())
1767 fileToCopy := config.CompatConfig()
1768 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1769}
1770
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001771// javaModule is an interface to handle all Java modules (java_library, dex_import, etc) in the same
1772// way.
1773type javaModule interface {
1774 android.Module
1775 BaseModuleName() string
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001776 DexJarBuildPath() java.OptionalDexJarPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001777 JacocoReportClassesFile() android.Path
1778 LintDepSets() java.LintDepSets
1779 Stem() string
1780}
1781
1782var _ javaModule = (*java.Library)(nil)
Bill Peckhama41a6962021-01-11 10:58:54 -08001783var _ javaModule = (*java.Import)(nil)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001784var _ javaModule = (*java.SdkLibrary)(nil)
1785var _ javaModule = (*java.DexImport)(nil)
1786var _ javaModule = (*java.SdkLibraryImport)(nil)
1787
Paul Duffin190fdef2021-04-26 10:33:59 +01001788// apexFileForJavaModule creates an apexFile for a java module's dex implementation jar.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001789func apexFileForJavaModule(ctx android.BaseModuleContext, module javaModule) apexFile {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001790 return apexFileForJavaModuleWithFile(ctx, module, module.DexJarBuildPath().PathOrNil())
Paul Duffin190fdef2021-04-26 10:33:59 +01001791}
1792
1793// apexFileForJavaModuleWithFile creates an apexFile for a java module with the supplied file.
1794func apexFileForJavaModuleWithFile(ctx android.BaseModuleContext, module javaModule, dexImplementationJar android.Path) apexFile {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001795 dirInApex := "javalib"
Paul Duffin190fdef2021-04-26 10:33:59 +01001796 af := newApexFile(ctx, dexImplementationJar, module.BaseModuleName(), dirInApex, javaSharedLib, module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001797 af.jacocoReportClassesFile = module.JacocoReportClassesFile()
1798 af.lintDepSets = module.LintDepSets()
1799 af.customStem = module.Stem() + ".jar"
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001800 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
1801 for _, install := range dexpreopter.DexpreoptBuiltInstalledForApex() {
1802 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
1803 }
1804 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001805 return af
1806}
1807
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001808func apexFileForJavaModuleProfile(ctx android.BaseModuleContext, module javaModule) *apexFile {
1809 if dexpreopter, ok := module.(java.DexpreopterInterface); ok {
Jiakai Zhang81e46812023-02-08 21:56:07 +08001810 if profilePathOnHost := dexpreopter.OutputProfilePathOnHost(); profilePathOnHost != nil {
Jiakai Zhang3317ce72023-02-08 01:19:19 +08001811 dirInApex := "javalib"
1812 af := newApexFile(ctx, profilePathOnHost, module.BaseModuleName()+"-profile", dirInApex, etc, nil)
1813 af.customStem = module.Stem() + ".jar.prof"
1814 return &af
1815 }
1816 }
1817 return nil
1818}
1819
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001820// androidApp is an interface to handle all app modules (android_app, android_app_import, etc.) in
1821// the same way.
1822type androidApp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +09001823 android.Module
1824 Privileged() bool
Jooyung Han39ee1192020-03-23 20:21:11 +09001825 InstallApkName() string
Jiyong Parkf653b052019-11-18 15:39:01 +09001826 OutputFile() android.Path
Jiyong Park618922e2020-01-08 13:35:43 +09001827 JacocoReportClassesFile() android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001828 Certificate() java.Certificate
Yo Chiange8128052020-07-23 20:09:18 +08001829 BaseModuleName() string
Colin Cross8355c152021-08-10 19:24:07 -07001830 LintDepSets() java.LintDepSets
Andrei Onea580636b2022-08-17 16:53:46 +00001831 PrivAppAllowlist() android.OptionalPath
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001832}
1833
1834var _ androidApp = (*java.AndroidApp)(nil)
1835var _ androidApp = (*java.AndroidAppImport)(nil)
1836
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001837func sanitizedBuildIdForPath(ctx android.BaseModuleContext) string {
1838 buildId := ctx.Config().BuildId()
1839
1840 // The build ID is used as a suffix for a filename, so ensure that
1841 // the set of characters being used are sanitized.
1842 // - any word character: [a-zA-Z0-9_]
1843 // - dots: .
1844 // - dashes: -
1845 validRegex := regexp.MustCompile(`^[\w\.\-\_]+$`)
1846 if !validRegex.MatchString(buildId) {
1847 ctx.ModuleErrorf("Unable to use build id %s as filename suffix, valid characters are [a-z A-Z 0-9 _ . -].", buildId)
1848 }
1849 return buildId
1850}
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001851
Andrei Onea580636b2022-08-17 16:53:46 +00001852func apexFilesForAndroidApp(ctx android.BaseModuleContext, aapp androidApp) []apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +09001853 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +09001854 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +09001855 appDir = "priv-app"
1856 }
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001857
1858 // TODO(b/224589412, b/226559955): Ensure that the subdirname is suffixed
1859 // so that PackageManager correctly invalidates the existing installed apk
1860 // in favour of the new APK-in-APEX. See bugs for more information.
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00001861 dirInApex := filepath.Join(appDir, aapp.InstallApkName()+"@"+sanitizedBuildIdForPath(ctx))
Jiyong Parkf653b052019-11-18 15:39:01 +09001862 fileToCopy := aapp.OutputFile()
Jingwen Chen8ce1efc2022-04-19 13:57:01 +00001863
Yo Chiange8128052020-07-23 20:09:18 +08001864 af := newApexFile(ctx, fileToCopy, aapp.BaseModuleName(), dirInApex, app, aapp)
Jiyong Park618922e2020-01-08 13:35:43 +09001865 af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
Colin Cross8355c152021-08-10 19:24:07 -07001866 af.lintDepSets = aapp.LintDepSets()
Colin Cross503c1d02020-01-28 14:00:53 -08001867 af.certificate = aapp.Certificate()
Jiyong Parkcfaa1642020-02-28 16:51:07 +09001868
1869 if app, ok := aapp.(interface {
1870 OverriddenManifestPackageName() string
1871 }); ok {
1872 af.overriddenPackageName = app.OverriddenManifestPackageName()
1873 }
Andrei Onea580636b2022-08-17 16:53:46 +00001874 apexFiles := []apexFile{af}
1875
1876 if allowlist := aapp.PrivAppAllowlist(); allowlist.Valid() {
1877 dirInApex := filepath.Join("etc", "permissions")
1878 privAppAllowlist := newApexFile(ctx, allowlist.Path(), aapp.BaseModuleName()+"privapp", dirInApex, etc, aapp)
1879 apexFiles = append(apexFiles, privAppAllowlist)
1880 }
1881
1882 return apexFiles
Dario Frenicde2a032019-10-27 00:29:22 +01001883}
1884
Jiyong Park69aeba92020-04-24 21:16:36 +09001885func apexFileForRuntimeResourceOverlay(ctx android.BaseModuleContext, rro java.RuntimeResourceOverlayModule) apexFile {
1886 rroDir := "overlay"
1887 dirInApex := filepath.Join(rroDir, rro.Theme())
1888 fileToCopy := rro.OutputFile()
1889 af := newApexFile(ctx, fileToCopy, rro.Name(), dirInApex, app, rro)
1890 af.certificate = rro.Certificate()
1891
1892 if a, ok := rro.(interface {
1893 OverriddenManifestPackageName() string
1894 }); ok {
1895 af.overriddenPackageName = a.OverriddenManifestPackageName()
1896 }
1897 return af
1898}
1899
Ken Chenfad7f9d2021-11-10 22:02:57 +08001900func apexFileForBpfProgram(ctx android.BaseModuleContext, builtFile android.Path, apex_sub_dir string, bpfProgram bpf.BpfModule) apexFile {
1901 dirInApex := filepath.Join("etc", "bpf", apex_sub_dir)
markchien2f59ec92020-09-02 16:23:38 +08001902 return newApexFile(ctx, builtFile, builtFile.Base(), dirInApex, etc, bpfProgram)
1903}
1904
Jiyong Park12a719c2021-01-07 15:31:24 +09001905func apexFileForFilesystem(ctx android.BaseModuleContext, buildFile android.Path, fs filesystem.Filesystem) apexFile {
1906 dirInApex := filepath.Join("etc", "fs")
1907 return newApexFile(ctx, buildFile, buildFile.Base(), dirInApex, etc, fs)
1908}
1909
Paul Duffin064b70c2020-11-02 17:32:38 +00001910// WalkPayloadDeps visits dependencies that contributes to the payload of this APEX. For each of the
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001911// visited module, the `do` callback is executed. Returning true in the callback continues the visit
1912// to the child modules. Returning false makes the visit to continue in the sibling or the parent
1913// modules. This is used in check* functions below.
Jooyung Han749dc692020-04-15 11:03:39 +09001914func (a *apexBundle) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Paul Duffindf915ff2020-03-30 17:58:21 +01001915 ctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001916 am, ok := child.(android.ApexModule)
1917 if !ok || !am.CanHaveApexVariants() {
1918 return false
1919 }
1920
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001921 // Filter-out unwanted depedendencies
1922 depTag := ctx.OtherModuleDependencyTag(child)
1923 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
1924 return false
1925 }
Paul Duffin520917a2022-05-13 13:01:59 +00001926 if dt, ok := depTag.(*dependencyTag); ok && !dt.payload {
Martin Stjernholm58c33f02020-07-06 22:56:01 +01001927 return false
1928 }
1929
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001930 ai := ctx.OtherModuleProvider(child, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001931 externalDep := !android.InList(ctx.ModuleName(), ai.InApexVariants)
Jiyong Park0f80c182020-01-31 02:49:53 +09001932
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001933 // Visit actually
1934 return do(ctx, parent, am, externalDep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001935 })
1936}
1937
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001938// filesystem type of the apex_payload.img inside the APEX. Currently, ext4 and f2fs are supported.
1939type fsType int
Jooyung Han03b51852020-02-26 22:45:42 +09001940
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001941const (
1942 ext4 fsType = iota
1943 f2fs
Huang Jianan13cac632021-08-02 15:02:17 +08001944 erofs
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001945)
Artur Satayev849f8442020-04-28 14:57:42 +01001946
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001947func (f fsType) string() string {
1948 switch f {
1949 case ext4:
1950 return ext4FsType
1951 case f2fs:
1952 return f2fsFsType
Huang Jianan13cac632021-08-02 15:02:17 +08001953 case erofs:
1954 return erofsFsType
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09001955 default:
1956 panic(fmt.Errorf("unknown APEX payload type %d", f))
Jooyung Han548640b2020-04-27 12:10:30 +09001957 }
1958}
1959
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001960var _ android.MixedBuildBuildable = (*apexBundle)(nil)
1961
1962func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
Jingwen Chenbad41822023-03-23 03:04:00 +00001963 return a.properties.ApexType == imageApex
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001964}
1965
1966func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) {
1967 bazelCtx := ctx.Config().BazelContext
1968 bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx))
1969}
1970
Jingwen Chen889f2f22022-12-16 08:16:01 +00001971// GetBazelLabel returns the bazel label of this apexBundle, or the label of the
1972// override_apex module overriding this apexBundle. An apexBundle can be
1973// overridden by different override_apex modules (e.g. Google or Go variants),
1974// which is handled by the overrides mutators.
1975func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string {
Jingwen Chen889f2f22022-12-16 08:16:01 +00001976 return a.BazelModuleBase.GetBazelLabel(ctx, a)
1977}
1978
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07001979func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) {
1980 if !a.commonBuildActions(ctx) {
1981 return
1982 }
1983
1984 a.setApexTypeAndSuffix(ctx)
1985 a.setPayloadFsType(ctx)
1986 a.setSystemLibLink(ctx)
1987
1988 if a.properties.ApexType != zipApex {
1989 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
1990 }
1991
1992 bazelCtx := ctx.Config().BazelContext
1993 outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx))
1994 if err != nil {
1995 ctx.ModuleErrorf(err.Error())
1996 return
1997 }
1998 a.installDir = android.PathForModuleInstall(ctx, "apex")
Jingwen Chen94098e82023-01-10 14:50:42 +00001999
2000 // Set the output file to .apex or .capex depending on the compression configuration.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002001 a.setCompression(ctx)
Jingwen Chen94098e82023-01-10 14:50:42 +00002002 if a.isCompressed {
Cole Faustb0bfa072023-04-03 14:28:36 -07002003 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedCompressedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002004 } else {
Cole Faustb0bfa072023-04-03 14:28:36 -07002005 a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedOutput)
Jingwen Chen94098e82023-01-10 14:50:42 +00002006 }
2007 a.outputFile = a.outputApexFile
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002008
Sam Delmerico4ed95e22023-02-03 18:12:15 -05002009 if len(outputs.TidyFiles) > 0 {
2010 tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles)
2011 a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles)
2012 }
2013
Liz Kammer0e255ef2022-11-04 16:07:04 -04002014 // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer
2015 // interface if these were set in a provider rather than the module itself
Wei Li32dcdf92022-10-26 22:30:48 -07002016 a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0])
2017 a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1])
2018 a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0])
2019 a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1])
Liz Kammer0e255ef2022-11-04 16:07:04 -04002020
Jingwen Chen29743c82023-01-25 17:49:46 +00002021 // Ensure ApexMkInfo.install_to_system make module names are installed as
2022 // part of a bundled build.
2023 a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...)
Vinh Tranb6803a52022-12-14 11:34:54 -05002024
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002025 apexType := a.properties.ApexType
2026 switch apexType {
2027 case imageApex:
Liz Kammer303978d2022-11-04 16:12:43 -04002028 a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile)
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002029 a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex))
Wei Licc73a052022-11-07 14:25:34 -08002030 a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs))
Jingwen Chen0c9a2762022-11-04 09:40:47 +00002031 // TODO(b/239084755): Generate the java api using.xml file from Bazel.
Jingwen Chen1ec77852022-11-07 14:36:12 +00002032 a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex))
Wei Li78c07de2022-11-08 16:01:05 -08002033 a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002034 installSuffix := imageApexSuffix
2035 if a.isCompressed {
2036 installSuffix = imageCapexSuffix
2037 }
2038 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
2039 a.compatSymlinks.Paths()...)
2040 default:
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002041 panic(fmt.Errorf("internal error: unexpected apex_type for the ProcessBazelQueryResponse: %v", a.properties.ApexType))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002042 }
2043
Jingwen Chen2d37b642023-03-14 16:11:38 +00002044 // filesInfo in mixed mode must retrieve all information about the apex's
2045 // contents completely from the Starlark providers. It should never rely on
2046 // Android.bp information, as they might not exist for fully migrated
2047 // dependencies.
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002048 //
2049 // Prevent accidental writes to filesInfo in the earlier parts Soong by
2050 // asserting it to be nil.
2051 if a.filesInfo != nil {
Jingwen Chen2d37b642023-03-14 16:11:38 +00002052 panic(
2053 fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel. " +
2054 "Did something else set filesInfo before this line of code?"))
2055 }
2056 for _, f := range outputs.PayloadFilesInfo {
2057 fileInfo := apexFile{
2058 isBazelPrebuilt: true,
2059
2060 builtFile: android.PathForBazelOut(ctx, f["built_file"]),
2061 unstrippedBuiltFile: android.PathForBazelOut(ctx, f["unstripped_built_file"]),
2062 androidMkModuleName: f["make_module_name"],
2063 installDir: f["install_dir"],
2064 class: classes[f["class"]],
2065 customStem: f["basename"],
2066 moduleDir: f["package"],
2067 }
2068
2069 arch := f["arch"]
2070 fileInfo.arch = arch
2071 if len(arch) > 0 {
2072 fileInfo.multilib = "lib32"
2073 if strings.HasSuffix(arch, "64") {
2074 fileInfo.multilib = "lib64"
2075 }
2076 }
2077
2078 a.filesInfo = append(a.filesInfo, fileInfo)
Jingwen Chen2d7f6fd2023-02-03 10:31:08 +00002079 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002080}
2081
2082func (a *apexBundle) setCompression(ctx android.ModuleContext) {
2083 if a.properties.ApexType != imageApex {
2084 a.isCompressed = false
2085 } else if a.testOnlyShouldForceCompression() {
2086 a.isCompressed = true
2087 } else {
2088 a.isCompressed = ctx.Config().ApexCompressionEnabled() && a.isCompressable()
2089 }
2090}
2091
2092func (a *apexBundle) setSystemLibLink(ctx android.ModuleContext) {
2093 // Optimization. If we are building bundled APEX, for the files that are gathered due to the
2094 // transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2095 // the same library in the system partition, thus effectively sharing the same libraries
2096 // across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2097 // in the APEX.
2098 a.linkToSystemLib = !ctx.Config().UnbundledBuild() && a.installable()
2099
2100 // APEXes targeting other than system/system_ext partitions use vendor/product variants.
2101 // So we can't link them to /system/lib libs which are core variants.
2102 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
2103 a.linkToSystemLib = false
2104 }
2105
2106 forced := ctx.Config().ForceApexSymlinkOptimization()
2107 updatable := a.Updatable() || a.FutureUpdatable()
2108
2109 // We don't need the optimization for updatable APEXes, as it might give false signal
2110 // to the system health when the APEXes are still bundled (b/149805758).
2111 if !forced && updatable && a.properties.ApexType == imageApex {
2112 a.linkToSystemLib = false
2113 }
2114
2115 // We also don't want the optimization for host APEXes, because it doesn't make sense.
2116 if ctx.Host() {
2117 a.linkToSystemLib = false
2118 }
2119}
2120
2121func (a *apexBundle) setPayloadFsType(ctx android.ModuleContext) {
2122 switch proptools.StringDefault(a.properties.Payload_fs_type, ext4FsType) {
2123 case ext4FsType:
2124 a.payloadFsType = ext4
2125 case f2fsFsType:
2126 a.payloadFsType = f2fs
2127 case erofsFsType:
2128 a.payloadFsType = erofs
2129 default:
2130 ctx.PropertyErrorf("payload_fs_type", "%q is not a valid filesystem for apex [ext4, f2fs, erofs]", *a.properties.Payload_fs_type)
2131 }
2132}
2133
2134func (a *apexBundle) setApexTypeAndSuffix(ctx android.ModuleContext) {
2135 // Set suffix and primaryApexType depending on the ApexType
2136 buildFlattenedAsDefault := ctx.Config().FlattenApex()
2137 switch a.properties.ApexType {
2138 case imageApex:
2139 if buildFlattenedAsDefault {
2140 a.suffix = imageApexSuffix
2141 } else {
2142 a.suffix = ""
2143 a.primaryApexType = true
2144
2145 if ctx.Config().InstallExtraFlattenedApexes() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002146 a.makeModulesToInstall = append(a.makeModulesToInstall, a.Name()+flattenedSuffix)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002147 }
2148 }
2149 case zipApex:
2150 if proptools.String(a.properties.Payload_type) == "zip" {
2151 a.suffix = ""
2152 a.primaryApexType = true
2153 } else {
2154 a.suffix = zipApexSuffix
2155 }
2156 case flattenedApex:
2157 if buildFlattenedAsDefault {
2158 a.suffix = ""
2159 a.primaryApexType = true
2160 } else {
2161 a.suffix = flattenedSuffix
2162 }
2163 }
2164}
2165
2166func (a apexBundle) isCompressable() bool {
2167 return proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex
2168}
2169
2170func (a *apexBundle) commonBuildActions(ctx android.ModuleContext) bool {
2171 a.checkApexAvailability(ctx)
2172 a.checkUpdatable(ctx)
2173 a.CheckMinSdkVersion(ctx)
2174 a.checkStaticLinkingToStubLibraries(ctx)
2175 a.checkStaticExecutables(ctx)
2176 if len(a.properties.Tests) > 0 && !a.testApex {
2177 ctx.PropertyErrorf("tests", "property allowed only in apex_test module type")
2178 return false
2179 }
2180 return true
2181}
2182
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002183type visitorContext struct {
2184 // all the files that will be included in this APEX
2185 filesInfo []apexFile
2186
2187 // native lib dependencies
2188 provideNativeLibs []string
2189 requireNativeLibs []string
2190
2191 handleSpecialLibs bool
Jooyung Han862c0d62022-12-21 10:15:37 +09002192
2193 // if true, raise error on duplicate apexFile
2194 checkDuplicate bool
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002195}
2196
Jooyung Han862c0d62022-12-21 10:15:37 +09002197func (vctx *visitorContext) normalizeFileInfo(mctx android.ModuleContext) {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002198 encountered := make(map[string]apexFile)
2199 for _, f := range vctx.filesInfo {
2200 dest := filepath.Join(f.installDir, f.builtFile.Base())
2201 if e, ok := encountered[dest]; !ok {
2202 encountered[dest] = f
2203 } else {
Jooyung Han862c0d62022-12-21 10:15:37 +09002204 if vctx.checkDuplicate && f.builtFile.String() != e.builtFile.String() {
2205 mctx.ModuleErrorf("apex file %v is provided by two different files %v and %v",
2206 dest, e.builtFile, f.builtFile)
2207 return
2208 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002209 // If a module is directly included and also transitively depended on
2210 // consider it as directly included.
2211 e.transitiveDep = e.transitiveDep && f.transitiveDep
2212 encountered[dest] = e
2213 }
2214 }
2215 vctx.filesInfo = vctx.filesInfo[:0]
2216 for _, v := range encountered {
2217 vctx.filesInfo = append(vctx.filesInfo, v)
2218 }
2219 sort.Slice(vctx.filesInfo, func(i, j int) bool {
2220 // Sort by destination path so as to ensure consistent ordering even if the source of the files
2221 // changes.
2222 return vctx.filesInfo[i].path() < vctx.filesInfo[j].path()
2223 })
2224}
2225
2226func (a *apexBundle) depVisitor(vctx *visitorContext, ctx android.ModuleContext, child, parent blueprint.Module) bool {
2227 depTag := ctx.OtherModuleDependencyTag(child)
2228 if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2229 return false
2230 }
2231 if mod, ok := child.(android.Module); ok && !mod.Enabled() {
2232 return false
2233 }
2234 depName := ctx.OtherModuleName(child)
2235 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2236 switch depTag {
2237 case sharedLibTag, jniLibTag:
2238 isJniLib := depTag == jniLibTag
2239 switch ch := child.(type) {
2240 case *cc.Module:
2241 fi := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2242 fi.isJniLib = isJniLib
2243 vctx.filesInfo = append(vctx.filesInfo, fi)
2244 // Collect the list of stub-providing libs except:
2245 // - VNDK libs are only for vendors
2246 // - bootstrap bionic libs are treated as provided by system
2247 if ch.HasStubsVariants() && !a.vndkApex && !cc.InstallToBootstrap(ch.BaseModuleName(), ctx.Config()) {
2248 vctx.provideNativeLibs = append(vctx.provideNativeLibs, fi.stem())
2249 }
2250 return true // track transitive dependencies
2251 case *rust.Module:
2252 fi := apexFileForRustLibrary(ctx, ch)
2253 fi.isJniLib = isJniLib
2254 vctx.filesInfo = append(vctx.filesInfo, fi)
2255 return true // track transitive dependencies
2256 default:
2257 propertyName := "native_shared_libs"
2258 if isJniLib {
2259 propertyName = "jni_libs"
2260 }
2261 ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
2262 }
2263 case executableTag:
2264 switch ch := child.(type) {
2265 case *cc.Module:
2266 vctx.filesInfo = append(vctx.filesInfo, apexFileForExecutable(ctx, ch))
2267 return true // track transitive dependencies
Cole Faust4d247e62023-01-23 10:14:58 -08002268 case *python.PythonBinaryModule:
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002269 if ch.HostToolPath().Valid() {
2270 vctx.filesInfo = append(vctx.filesInfo, apexFileForPyBinary(ctx, ch))
2271 }
2272 case bootstrap.GoBinaryTool:
2273 if a.Host() {
2274 vctx.filesInfo = append(vctx.filesInfo, apexFileForGoBinary(ctx, depName, ch))
2275 }
2276 case *rust.Module:
2277 vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
2278 return true // track transitive dependencies
2279 default:
2280 ctx.PropertyErrorf("binaries",
2281 "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, nor (host) bootstrap_go_binary", depName)
2282 }
2283 case shBinaryTag:
2284 if csh, ok := child.(*sh.ShBinary); ok {
2285 vctx.filesInfo = append(vctx.filesInfo, apexFileForShBinary(ctx, csh))
2286 } else {
2287 ctx.PropertyErrorf("sh_binaries", "%q is not a sh_binary module", depName)
2288 }
2289 case bcpfTag:
2290 bcpfModule, ok := child.(*java.BootclasspathFragmentModule)
2291 if !ok {
2292 ctx.PropertyErrorf("bootclasspath_fragments", "%q is not a bootclasspath_fragment module", depName)
2293 return false
2294 }
2295
2296 vctx.filesInfo = append(vctx.filesInfo, apexBootclasspathFragmentFiles(ctx, child)...)
2297 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
Jingwen Chen29743c82023-01-25 17:49:46 +00002298 a.makeModulesToInstall = append(a.makeModulesToInstall, makeModuleName)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002299 }
2300 return true
2301 case sscpfTag:
2302 if _, ok := child.(*java.SystemServerClasspathModule); !ok {
2303 ctx.PropertyErrorf("systemserverclasspath_fragments",
2304 "%q is not a systemserverclasspath_fragment module", depName)
2305 return false
2306 }
2307 if af := apexClasspathFragmentProtoFile(ctx, child); af != nil {
2308 vctx.filesInfo = append(vctx.filesInfo, *af)
2309 }
2310 return true
2311 case javaLibTag:
2312 switch child.(type) {
2313 case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
2314 af := apexFileForJavaModule(ctx, child.(javaModule))
2315 if !af.ok() {
2316 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2317 return false
2318 }
2319 vctx.filesInfo = append(vctx.filesInfo, af)
2320 return true // track transitive dependencies
2321 default:
2322 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2323 }
2324 case androidAppTag:
2325 switch ap := child.(type) {
2326 case *java.AndroidApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002327 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002328 return true // track transitive dependencies
2329 case *java.AndroidAppImport:
Andrei Onea580636b2022-08-17 16:53:46 +00002330 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002331 case *java.AndroidTestHelperApp:
Andrei Onea580636b2022-08-17 16:53:46 +00002332 vctx.filesInfo = append(vctx.filesInfo, apexFilesForAndroidApp(ctx, ap)...)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002333 case *java.AndroidAppSet:
2334 appDir := "app"
2335 if ap.Privileged() {
2336 appDir = "priv-app"
2337 }
2338 // TODO(b/224589412, b/226559955): Ensure that the dirname is
2339 // suffixed so that PackageManager correctly invalidates the
2340 // existing installed apk in favour of the new APK-in-APEX.
2341 // See bugs for more information.
2342 appDirName := filepath.Join(appDir, ap.BaseModuleName()+"@"+sanitizedBuildIdForPath(ctx))
2343 af := newApexFile(ctx, ap.OutputFile(), ap.BaseModuleName(), appDirName, appSet, ap)
2344 af.certificate = java.PresignedCertificate
2345 vctx.filesInfo = append(vctx.filesInfo, af)
2346 default:
2347 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2348 }
2349 case rroTag:
2350 if rro, ok := child.(java.RuntimeResourceOverlayModule); ok {
2351 vctx.filesInfo = append(vctx.filesInfo, apexFileForRuntimeResourceOverlay(ctx, rro))
2352 } else {
2353 ctx.PropertyErrorf("rros", "%q is not an runtime_resource_overlay module", depName)
2354 }
2355 case bpfTag:
2356 if bpfProgram, ok := child.(bpf.BpfModule); ok {
2357 filesToCopy, _ := bpfProgram.OutputFiles("")
2358 apex_sub_dir := bpfProgram.SubDir()
2359 for _, bpfFile := range filesToCopy {
2360 vctx.filesInfo = append(vctx.filesInfo, apexFileForBpfProgram(ctx, bpfFile, apex_sub_dir, bpfProgram))
2361 }
2362 } else {
2363 ctx.PropertyErrorf("bpfs", "%q is not a bpf module", depName)
2364 }
2365 case fsTag:
2366 if fs, ok := child.(filesystem.Filesystem); ok {
2367 vctx.filesInfo = append(vctx.filesInfo, apexFileForFilesystem(ctx, fs.OutputPath(), fs))
2368 } else {
2369 ctx.PropertyErrorf("filesystems", "%q is not a filesystem module", depName)
2370 }
2371 case prebuiltTag:
2372 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2373 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2374 } else {
2375 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
2376 }
2377 case compatConfigTag:
2378 if compatConfig, ok := child.(java.PlatformCompatConfigIntf); ok {
2379 vctx.filesInfo = append(vctx.filesInfo, apexFileForCompatConfig(ctx, compatConfig, depName))
2380 } else {
2381 ctx.PropertyErrorf("compat_configs", "%q is not a platform_compat_config module", depName)
2382 }
2383 case testTag:
2384 if ccTest, ok := child.(*cc.Module); ok {
2385 if ccTest.IsTestPerSrcAllTestsVariation() {
2386 // Multiple-output test module (where `test_per_src: true`).
2387 //
2388 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2389 // We do not add this variation to `filesInfo`, as it has no output;
2390 // however, we do add the other variations of this module as indirect
2391 // dependencies (see below).
2392 } else {
2393 // Single-output test module (where `test_per_src: false`).
2394 af := apexFileForExecutable(ctx, ccTest)
2395 af.class = nativeTest
2396 vctx.filesInfo = append(vctx.filesInfo, af)
2397 }
2398 return true // track transitive dependencies
2399 } else {
2400 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2401 }
2402 case keyTag:
2403 if key, ok := child.(*apexKey); ok {
2404 a.privateKeyFile = key.privateKeyFile
2405 a.publicKeyFile = key.publicKeyFile
2406 } else {
2407 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2408 }
2409 case certificateTag:
2410 if dep, ok := child.(*java.AndroidAppCertificate); ok {
2411 a.containerCertificateFile = dep.Certificate.Pem
2412 a.containerPrivateKeyFile = dep.Certificate.Key
2413 } else {
2414 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2415 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002416 }
2417 return false
2418 }
2419
2420 if a.vndkApex {
2421 return false
2422 }
2423
2424 // indirect dependencies
2425 am, ok := child.(android.ApexModule)
2426 if !ok {
2427 return false
2428 }
2429 // We cannot use a switch statement on `depTag` here as the checked
2430 // tags used below are private (e.g. `cc.sharedDepTag`).
2431 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2432 if ch, ok := child.(*cc.Module); ok {
2433 if ch.UseVndk() && proptools.Bool(a.properties.Use_vndk_as_stable) && ch.IsVndk() {
2434 vctx.requireNativeLibs = append(vctx.requireNativeLibs, ":vndk")
2435 return false
2436 }
2437 af := apexFileForNativeLibrary(ctx, ch, vctx.handleSpecialLibs)
2438 af.transitiveDep = true
2439
2440 // Always track transitive dependencies for host.
2441 if a.Host() {
2442 vctx.filesInfo = append(vctx.filesInfo, af)
2443 return true
2444 }
2445
2446 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2447 if !abInfo.Contents.DirectlyInApex(depName) && (ch.IsStubs() || ch.HasStubsVariants()) {
2448 // If the dependency is a stubs lib, don't include it in this APEX,
2449 // but make sure that the lib is installed on the device.
2450 // In case no APEX is having the lib, the lib is installed to the system
2451 // partition.
2452 //
2453 // Always include if we are a host-apex however since those won't have any
2454 // system libraries.
Colin Crossdf2043e2023-01-26 15:39:15 -08002455 //
2456 // Skip the dependency in unbundled builds where the device image is not
2457 // being built.
2458 if ch.IsStubsImplementationRequired() && !am.DirectlyInAnyApex() && !ctx.Config().UnbundledBuild() {
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002459 // we need a module name for Make
2460 name := ch.ImplementationModuleNameForMake(ctx) + ch.Properties.SubName
Jingwen Chen29743c82023-01-25 17:49:46 +00002461 if !android.InList(name, a.makeModulesToInstall) {
2462 a.makeModulesToInstall = append(a.makeModulesToInstall, name)
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002463 }
2464 }
2465 vctx.requireNativeLibs = append(vctx.requireNativeLibs, af.stem())
2466 // Don't track further
2467 return false
2468 }
2469
2470 // If the dep is not considered to be in the same
2471 // apex, don't add it to filesInfo so that it is not
2472 // included in this APEX.
2473 // TODO(jiyong): move this to at the top of the
2474 // else-if clause for the indirect dependencies.
2475 // Currently, that's impossible because we would
2476 // like to record requiredNativeLibs even when
2477 // DepIsInSameAPex is false. We also shouldn't do
2478 // this for host.
2479 //
2480 // TODO(jiyong): explain why the same module is passed in twice.
2481 // Switching the first am to parent breaks lots of tests.
2482 if !android.IsDepInSameApex(ctx, am, am) {
2483 return false
2484 }
2485
2486 vctx.filesInfo = append(vctx.filesInfo, af)
2487 return true // track transitive dependencies
2488 } else if rm, ok := child.(*rust.Module); ok {
2489 af := apexFileForRustLibrary(ctx, rm)
2490 af.transitiveDep = true
2491 vctx.filesInfo = append(vctx.filesInfo, af)
2492 return true // track transitive dependencies
2493 }
2494 } else if cc.IsTestPerSrcDepTag(depTag) {
2495 if ch, ok := child.(*cc.Module); ok {
2496 af := apexFileForExecutable(ctx, ch)
2497 // Handle modules created as `test_per_src` variations of a single test module:
2498 // use the name of the generated test binary (`fileToCopy`) instead of the name
2499 // of the original test module (`depName`, shared by all `test_per_src`
2500 // variations of that module).
2501 af.androidMkModuleName = filepath.Base(af.builtFile.String())
2502 // these are not considered transitive dep
2503 af.transitiveDep = false
2504 vctx.filesInfo = append(vctx.filesInfo, af)
2505 return true // track transitive dependencies
2506 }
2507 } else if cc.IsHeaderDepTag(depTag) {
2508 // nothing
2509 } else if java.IsJniDepTag(depTag) {
2510 // Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2511 } else if java.IsXmlPermissionsFileDepTag(depTag) {
2512 if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2513 vctx.filesInfo = append(vctx.filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2514 }
2515 } else if rust.IsDylibDepTag(depTag) {
2516 if rustm, ok := child.(*rust.Module); ok && rustm.IsInstallableToApex() {
2517 af := apexFileForRustLibrary(ctx, rustm)
2518 af.transitiveDep = true
2519 vctx.filesInfo = append(vctx.filesInfo, af)
2520 return true // track transitive dependencies
2521 }
2522 } else if rust.IsRlibDepTag(depTag) {
2523 // Rlib is statically linked, but it might have shared lib
2524 // dependencies. Track them.
2525 return true
2526 } else if java.IsBootclasspathFragmentContentDepTag(depTag) {
2527 // Add the contents of the bootclasspath fragment to the apex.
2528 switch child.(type) {
2529 case *java.Library, *java.SdkLibrary:
2530 javaModule := child.(javaModule)
2531 af := apexFileForBootclasspathFragmentContentModule(ctx, parent, javaModule)
2532 if !af.ok() {
2533 ctx.PropertyErrorf("bootclasspath_fragments",
2534 "bootclasspath_fragment content %q is not configured to be compiled into dex", depName)
2535 return false
2536 }
2537 vctx.filesInfo = append(vctx.filesInfo, af)
2538 return true // track transitive dependencies
2539 default:
2540 ctx.PropertyErrorf("bootclasspath_fragments",
2541 "bootclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2542 }
2543 } else if java.IsSystemServerClasspathFragmentContentDepTag(depTag) {
2544 // Add the contents of the systemserverclasspath fragment to the apex.
2545 switch child.(type) {
2546 case *java.Library, *java.SdkLibrary:
2547 af := apexFileForJavaModule(ctx, child.(javaModule))
2548 vctx.filesInfo = append(vctx.filesInfo, af)
Jiakai Zhang3317ce72023-02-08 01:19:19 +08002549 if profileAf := apexFileForJavaModuleProfile(ctx, child.(javaModule)); profileAf != nil {
2550 vctx.filesInfo = append(vctx.filesInfo, *profileAf)
2551 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002552 return true // track transitive dependencies
2553 default:
2554 ctx.PropertyErrorf("systemserverclasspath_fragments",
2555 "systemserverclasspath_fragment content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
2556 }
2557 } else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
2558 // nothing
2559 } else if depTag == android.DarwinUniversalVariantTag {
2560 // nothing
2561 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2562 ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", android.PrettyPrintTag(depTag), depName)
2563 }
2564 return false
2565}
2566
Jooyung Han862c0d62022-12-21 10:15:37 +09002567func (a *apexBundle) shouldCheckDuplicate(ctx android.ModuleContext) bool {
2568 // TODO(b/263308293) remove this
2569 if a.properties.IsCoverageVariant {
2570 return false
2571 }
2572 // TODO(b/263308515) remove this
2573 if a.testApex {
2574 return false
2575 }
2576 // TODO(b/263309864) remove this
2577 if a.Host() {
2578 return false
2579 }
2580 if a.Device() && ctx.DeviceConfig().DeviceArch() == "" {
2581 return false
2582 }
2583 return true
2584}
2585
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002586// Creates build rules for an APEX. It consists of the following major steps:
2587//
2588// 1) do some validity checks such as apex_available, min_sdk_version, etc.
2589// 2) traverse the dependency tree to collect apexFile structs from them.
2590// 3) some fields in apexBundle struct are configured
2591// 4) generate the build rules to create the APEX. This is mostly done in builder.go.
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002592func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002593 ////////////////////////////////////////////////////////////////////////////////////////////
2594 // 1) do some validity checks such as apex_available, min_sdk_version, etc.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002595 if !a.commonBuildActions(ctx) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002596 return
2597 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002598 ////////////////////////////////////////////////////////////////////////////////////////////
2599 // 2) traverse the dependency tree to collect apexFile structs from them.
braleeb0c1f0c2021-06-07 22:49:13 +08002600 // Collect the module directory for IDE info in java/jdeps.go.
2601 a.modulePaths = append(a.modulePaths, ctx.ModuleDir())
2602
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002603 // TODO(jiyong): do this using WalkPayloadDeps
2604 // TODO(jiyong): make this clean!!!
Jooyung Han862c0d62022-12-21 10:15:37 +09002605 vctx := visitorContext{
2606 handleSpecialLibs: !android.Bool(a.properties.Ignore_system_library_special_case),
2607 checkDuplicate: a.shouldCheckDuplicate(ctx),
2608 }
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002609 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool { return a.depVisitor(&vctx, ctx, child, parent) })
Jooyung Han862c0d62022-12-21 10:15:37 +09002610 vctx.normalizeFileInfo(ctx)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002611 if a.privateKeyFile == nil {
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07002612 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.overridableProperties.Key))
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002613 return
2614 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09002615
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002616 ////////////////////////////////////////////////////////////////////////////////////////////
2617 // 3) some fields in apexBundle struct are configured
Jiyong Park8fd61922018-11-08 02:50:25 +09002618 a.installDir = android.PathForModuleInstall(ctx, "apex")
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002619 a.filesInfo = vctx.filesInfo
Alex Light5098a612018-11-29 17:12:15 -08002620
Sasha Smundakfe9a5b82022-07-27 14:51:45 -07002621 a.setApexTypeAndSuffix(ctx)
2622 a.setPayloadFsType(ctx)
2623 a.setSystemLibLink(ctx)
Colin Cross6340ea52021-11-04 12:01:18 -07002624 if a.properties.ApexType != zipApex {
2625 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, a.primaryApexType)
2626 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002627
2628 ////////////////////////////////////////////////////////////////////////////////////////////
2629 // 4) generate the build rules to create the APEX. This is done in builder.go.
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002630 a.buildManifest(ctx, vctx.provideNativeLibs, vctx.requireNativeLibs)
Jooyung Han01a3ee22019-11-02 02:52:25 +09002631 if a.properties.ApexType == flattenedApex {
2632 a.buildFlattenedApex(ctx)
2633 } else {
2634 a.buildUnflattenedApex(ctx)
2635 }
Jiyong Park956305c2020-01-09 12:32:06 +09002636 a.buildApexDependencyInfo(ctx)
Colin Cross08dca382020-07-21 20:31:17 -07002637 a.buildLintReports(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +09002638
2639 // Append meta-files to the filesInfo list so that they are reflected in Android.mk as well.
2640 if a.installable() {
2641 // For flattened APEX, make sure that APEX manifest and apex_pubkey are also copied
2642 // along with other ordinary files. (Note that this is done by apexer for
2643 // non-flattened APEXes)
2644 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
2645
2646 // Place the public key as apex_pubkey. This is also done by apexer for
2647 // non-flattened APEXes case.
2648 // TODO(jiyong): Why do we need this CP rule?
2649 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
2650 ctx.Build(pctx, android.BuildParams{
2651 Rule: android.Cp,
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002652 Input: a.publicKeyFile,
Jiyong Parkb81b9902020-11-24 19:51:18 +09002653 Output: copiedPubkey,
2654 })
2655 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
2656 }
Jooyung Han01a3ee22019-11-02 02:52:25 +09002657}
2658
Paul Duffincc33ec82021-04-25 23:14:55 +01002659// apexBootclasspathFragmentFiles returns the list of apexFile structures defining the files that
2660// the bootclasspath_fragment contributes to the apex.
2661func apexBootclasspathFragmentFiles(ctx android.ModuleContext, module blueprint.Module) []apexFile {
2662 bootclasspathFragmentInfo := ctx.OtherModuleProvider(module, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2663 var filesToAdd []apexFile
2664
2665 // Add the boot image files, e.g. .art, .oat and .vdex files.
Jiakai Zhang6decef92022-01-12 17:56:19 +00002666 if bootclasspathFragmentInfo.ShouldInstallBootImageInApex() {
2667 for arch, files := range bootclasspathFragmentInfo.AndroidBootImageFilesByArchType() {
2668 dirInApex := filepath.Join("javalib", arch.String())
2669 for _, f := range files {
2670 androidMkModuleName := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
2671 // TODO(b/177892522) - consider passing in the bootclasspath fragment module here instead of nil
2672 af := newApexFile(ctx, f, androidMkModuleName, dirInApex, etc, nil)
2673 filesToAdd = append(filesToAdd, af)
2674 }
Paul Duffincc33ec82021-04-25 23:14:55 +01002675 }
2676 }
2677
satayev3db35472021-05-06 23:59:58 +01002678 // Add classpaths.proto config.
satayevb98371c2021-06-15 16:49:50 +01002679 if af := apexClasspathFragmentProtoFile(ctx, module); af != nil {
2680 filesToAdd = append(filesToAdd, *af)
2681 }
satayev3db35472021-05-06 23:59:58 +01002682
Ulya Trafimovichf5c548d2022-11-16 14:52:41 +00002683 pathInApex := bootclasspathFragmentInfo.ProfileInstallPathInApex()
Jiakai Zhangbc698cd2023-05-08 16:28:38 +00002684 if pathInApex != "" {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00002685 pathOnHost := bootclasspathFragmentInfo.ProfilePathOnHost()
2686 tempPath := android.PathForModuleOut(ctx, "boot_image_profile", pathInApex)
2687
2688 if pathOnHost != nil {
2689 // We need to copy the profile to a temporary path with the right filename because the apexer
2690 // will take the filename as is.
2691 ctx.Build(pctx, android.BuildParams{
2692 Rule: android.Cp,
2693 Input: pathOnHost,
2694 Output: tempPath,
2695 })
2696 } else {
2697 // At this point, the boot image profile cannot be generated. It is probably because the boot
2698 // image profile source file does not exist on the branch, or it is not available for the
2699 // current build target.
2700 // However, we cannot enforce the boot image profile to be generated because some build
2701 // targets (such as module SDK) do not need it. It is only needed when the APEX is being
2702 // built. Therefore, we create an error rule so that an error will occur at the ninja phase
2703 // only if the APEX is being built.
2704 ctx.Build(pctx, android.BuildParams{
2705 Rule: android.ErrorRule,
2706 Output: tempPath,
2707 Args: map[string]string{
2708 "error": "Boot image profile cannot be generated",
2709 },
2710 })
2711 }
2712
2713 androidMkModuleName := filepath.Base(pathInApex)
2714 af := newApexFile(ctx, tempPath, androidMkModuleName, filepath.Dir(pathInApex), etc, nil)
2715 filesToAdd = append(filesToAdd, af)
2716 }
2717
Paul Duffincc33ec82021-04-25 23:14:55 +01002718 return filesToAdd
2719}
2720
satayevb98371c2021-06-15 16:49:50 +01002721// apexClasspathFragmentProtoFile returns *apexFile structure defining the classpath.proto config that
2722// the module contributes to the apex; or nil if the proto config was not generated.
2723func apexClasspathFragmentProtoFile(ctx android.ModuleContext, module blueprint.Module) *apexFile {
2724 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
2725 if !info.ClasspathFragmentProtoGenerated {
2726 return nil
2727 }
2728 classpathProtoOutput := info.ClasspathFragmentProtoOutput
2729 af := newApexFile(ctx, classpathProtoOutput, classpathProtoOutput.Base(), info.ClasspathFragmentProtoInstallDir.Rel(), etc, nil)
2730 return &af
satayev14e49132021-05-17 21:03:07 +01002731}
2732
Paul Duffincc33ec82021-04-25 23:14:55 +01002733// apexFileForBootclasspathFragmentContentModule creates an apexFile for a bootclasspath_fragment
2734// content module, i.e. a library that is part of the bootclasspath.
Paul Duffin190fdef2021-04-26 10:33:59 +01002735func apexFileForBootclasspathFragmentContentModule(ctx android.ModuleContext, fragmentModule blueprint.Module, javaModule javaModule) apexFile {
2736 bootclasspathFragmentInfo := ctx.OtherModuleProvider(fragmentModule, java.BootclasspathFragmentApexContentInfoProvider).(java.BootclasspathFragmentApexContentInfo)
2737
2738 // Get the dexBootJar from the bootclasspath_fragment as that is responsible for performing the
2739 // hidden API encpding.
Paul Duffin1a8010a2021-05-15 12:39:23 +01002740 dexBootJar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(javaModule)
2741 if err != nil {
2742 ctx.ModuleErrorf("%s", err)
2743 }
Paul Duffin190fdef2021-04-26 10:33:59 +01002744
2745 // Create an apexFile as for a normal java module but with the dex boot jar provided by the
2746 // bootclasspath_fragment.
2747 af := apexFileForJavaModuleWithFile(ctx, javaModule, dexBootJar)
2748 return af
Paul Duffincc33ec82021-04-25 23:14:55 +01002749}
2750
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002751///////////////////////////////////////////////////////////////////////////////////////////////////
2752// Factory functions
2753//
2754
2755func newApexBundle() *apexBundle {
2756 module := &apexBundle{}
2757
2758 module.AddProperties(&module.properties)
2759 module.AddProperties(&module.targetProperties)
Jiyong Park59140302020-12-14 18:44:04 +09002760 module.AddProperties(&module.archProperties)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002761 module.AddProperties(&module.overridableProperties)
2762
2763 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2764 android.InitDefaultableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002765 android.InitOverridableModule(module, &module.overridableProperties.Overrides)
Jingwen Chenf59a8e12021-07-16 09:28:53 +00002766 android.InitBazelModule(module)
Inseob Kim5eb7ee92022-04-27 10:30:34 +09002767 multitree.InitExportableModule(module)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002768 return module
2769}
2770
Paul Duffineb8051d2021-10-18 17:49:39 +01002771func ApexBundleFactory(testApex bool) android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002772 bundle := newApexBundle()
2773 bundle.testApex = testApex
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002774 return bundle
2775}
2776
2777// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2778// certain compatibility checks such as apex_available are not done for apex_test.
Yu Liu4c212ce2022-10-14 12:20:20 -07002779func TestApexBundleFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002780 bundle := newApexBundle()
2781 bundle.testApex = true
2782 return bundle
2783}
2784
2785// apex packages other modules into an APEX file which is a packaging format for system-level
2786// components like binaries, shared libraries, etc.
2787func BundleFactory() android.Module {
2788 return newApexBundle()
2789}
2790
2791type Defaults struct {
2792 android.ModuleBase
2793 android.DefaultsModuleBase
2794}
2795
2796// apex_defaults provides defaultable properties to other apex modules.
Cole Faust912bc882023-03-08 12:29:50 -08002797func DefaultsFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002798 module := &Defaults{}
2799
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002800 module.AddProperties(
2801 &apexBundleProperties{},
2802 &apexTargetBundleProperties{},
Nikita Ioffee58f5272022-10-24 17:24:38 +01002803 &apexArchBundleProperties{},
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002804 &overridableProperties{},
2805 )
2806
2807 android.InitDefaultsModule(module)
2808 return module
2809}
2810
2811type OverrideApex struct {
2812 android.ModuleBase
2813 android.OverrideModuleBase
Wei Li1c66fc72022-05-09 23:59:14 -07002814 android.BazelModuleBase
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002815}
2816
Sasha Smundak6f9e91d2022-06-28 22:43:04 -07002817func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002818 // All the overrides happen in the base module.
2819}
2820
2821// override_apex is used to create an apex module based on another apex module by overriding some of
2822// its properties.
Wei Li1c66fc72022-05-09 23:59:14 -07002823func OverrideApexFactory() android.Module {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002824 m := &OverrideApex{}
2825
2826 m.AddProperties(&overridableProperties{})
2827
2828 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2829 android.InitOverrideModule(m)
Wei Li1c66fc72022-05-09 23:59:14 -07002830 android.InitBazelModule(m)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002831 return m
2832}
2833
Wei Li1c66fc72022-05-09 23:59:14 -07002834func (o *OverrideApex) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2835 if ctx.ModuleType() != "override_apex" {
2836 return
2837 }
2838
2839 baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName()
2840 baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName)
2841 if !baseApexExists {
2842 panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName))
2843 }
2844
2845 a, baseModuleIsApex := baseModule.(*apexBundle)
2846 if !baseModuleIsApex {
2847 panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName))
2848 }
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002849 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
Wei Li1c66fc72022-05-09 23:59:14 -07002850
Jingwen Chenc4c34e12022-11-29 12:07:45 +00002851 // We just want the name, not module reference.
2852 baseApexName := strings.TrimPrefix(baseApexModuleName, ":")
2853 attrs.Base_apex_name = &baseApexName
2854
Wei Li1c66fc72022-05-09 23:59:14 -07002855 for _, p := range o.GetProperties() {
2856 overridableProperties, ok := p.(*overridableProperties)
2857 if !ok {
2858 continue
2859 }
Wei Li40f98732022-05-20 22:08:11 -07002860
2861 // Manifest is either empty or a file in the directory of base APEX and is not overridable.
2862 // After it is converted in convertWithBp2build(baseApex, ctx),
2863 // the attrs.Manifest.Value.Label is the file path relative to the directory
2864 // of base apex. So the following code converts it to a label that looks like
2865 // <package of base apex>:<path of manifest file> if base apex and override
2866 // apex are not in the same package.
2867 baseApexPackage := ctx.OtherModuleDir(a)
2868 overrideApexPackage := ctx.ModuleDir()
2869 if baseApexPackage != overrideApexPackage {
2870 attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label
2871 }
2872
Wei Li1c66fc72022-05-09 23:59:14 -07002873 // Key
2874 if overridableProperties.Key != nil {
2875 attrs.Key = bazel.LabelAttribute{}
2876 attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key))
2877 }
2878
2879 // Certificate
Jingwen Chenbea58092022-09-29 16:56:02 +00002880 if overridableProperties.Certificate == nil {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002881 // If overridableProperties.Certificate is nil, clear this out as
2882 // well with zeroed structs, so the override_apex does not use the
2883 // base apex's certificate.
2884 attrs.Certificate = bazel.LabelAttribute{}
2885 attrs.Certificate_name = bazel.StringAttribute{}
Jingwen Chenbea58092022-09-29 16:56:02 +00002886 } else {
Jingwen Chen6817bbb2022-10-14 09:56:07 +00002887 attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate)
Wei Li1c66fc72022-05-09 23:59:14 -07002888 }
2889
2890 // Prebuilts
Jingwen Chendf165c92022-06-08 16:00:39 +00002891 if overridableProperties.Prebuilts != nil {
2892 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts)
2893 attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList)
2894 }
Wei Li1c66fc72022-05-09 23:59:14 -07002895
2896 // Compressible
2897 if overridableProperties.Compressible != nil {
2898 attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible}
2899 }
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00002900
2901 // Package name
2902 //
2903 // e.g. com.android.adbd's package name is com.android.adbd, but
2904 // com.google.android.adbd overrides the package name to com.google.android.adbd
2905 //
2906 // TODO: this can be overridden from the product configuration, see
2907 // getOverrideManifestPackageName and
2908 // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES.
2909 //
2910 // Instead of generating the BUILD files differently based on the product config
2911 // at the point of conversion, this should be handled by the BUILD file loading
2912 // from the soong_injection's product_vars, so product config is decoupled from bp2build.
2913 if overridableProperties.Package_name != "" {
2914 attrs.Package_name = &overridableProperties.Package_name
2915 }
Jingwen Chenb732d7c2022-06-10 08:14:19 +00002916
2917 // Logging parent
2918 if overridableProperties.Logging_parent != "" {
2919 attrs.Logging_parent = &overridableProperties.Logging_parent
2920 }
Wei Li1c66fc72022-05-09 23:59:14 -07002921 }
2922
Liz Kammer1a1c9df2023-03-28 11:39:50 -04002923 commonAttrs.Name = o.Name()
2924
2925 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07002926}
2927
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002928///////////////////////////////////////////////////////////////////////////////////////////////////
2929// Vality check routines
2930//
2931// These are called in at the very beginning of GenerateAndroidBuildActions to flag an error when
2932// certain conditions are not met.
2933//
2934// TODO(jiyong): move these checks to a separate go file.
2935
satayevad991492021-12-03 18:58:32 +00002936var _ android.ModuleWithMinSdkVersionCheck = (*apexBundle)(nil)
2937
Spandan Dasa5f39a12022-08-05 02:35:52 +00002938// 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 +09002939// of this apexBundle.
satayevb3fd4112021-12-02 13:59:35 +00002940func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002941 if a.testApex || a.vndkApex {
2942 return
2943 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002944 // apexBundle::minSdkVersion reports its own errors.
2945 minSdkVersion := a.minSdkVersion(ctx)
satayevb3fd4112021-12-02 13:59:35 +00002946 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002947}
2948
Albert Martineefabcf2022-03-21 20:11:16 +00002949// Returns apex's min_sdk_version string value, honoring overrides
2950func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string {
2951 // Only override the minSdkVersion value on Apexes which already specify
2952 // a min_sdk_version (it's optional for non-updatable apexes), and that its
2953 // min_sdk_version value is lower than the one to override with.
zhidou133c55b2023-01-31 19:34:10 +00002954 minApiLevel := minSdkVersionFromValue(ctx, proptools.String(a.overridableProperties.Min_sdk_version))
Colin Cross56534df2022-10-04 09:58:58 -07002955 if minApiLevel.IsNone() {
2956 return ""
Albert Martineefabcf2022-03-21 20:11:16 +00002957 }
2958
Colin Cross56534df2022-10-04 09:58:58 -07002959 overrideMinSdkValue := ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()
2960 overrideApiLevel := minSdkVersionFromValue(ctx, overrideMinSdkValue)
2961 if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(minApiLevel) > 0 {
2962 minApiLevel = overrideApiLevel
2963 }
2964
2965 return minApiLevel.String()
Albert Martineefabcf2022-03-21 20:11:16 +00002966}
2967
2968// Returns apex's min_sdk_version SdkSpec, honoring overrides
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002969func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2970 return a.minSdkVersion(ctx)
satayevad991492021-12-03 18:58:32 +00002971}
2972
Albert Martineefabcf2022-03-21 20:11:16 +00002973// Returns apex's min_sdk_version ApiLevel, honoring overrides
satayevad991492021-12-03 18:58:32 +00002974func (a *apexBundle) minSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Albert Martineefabcf2022-03-21 20:11:16 +00002975 return minSdkVersionFromValue(ctx, a.minSdkVersionValue(ctx))
2976}
2977
2978// Construct ApiLevel object from min_sdk_version string value
2979func minSdkVersionFromValue(ctx android.EarlyModuleContext, value string) android.ApiLevel {
2980 if value == "" {
Jooyung Haned124c32021-01-26 11:43:46 +09002981 return android.NoneApiLevel
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002982 }
Albert Martineefabcf2022-03-21 20:11:16 +00002983 apiLevel, err := android.ApiLevelFromUser(ctx, value)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002984 if err != nil {
2985 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
2986 return android.NoneApiLevel
2987 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09002988 return apiLevel
2989}
2990
2991// Ensures that a lib providing stub isn't statically linked
2992func (a *apexBundle) checkStaticLinkingToStubLibraries(ctx android.ModuleContext) {
2993 // Practically, we only care about regular APEXes on the device.
2994 if ctx.Host() || a.testApex || a.vndkApex {
2995 return
2996 }
2997
2998 abInfo := ctx.Provider(ApexBundleInfoProvider).(ApexBundleInfo)
2999
3000 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3001 if ccm, ok := to.(*cc.Module); ok {
3002 apexName := ctx.ModuleName()
3003 fromName := ctx.OtherModuleName(from)
3004 toName := ctx.OtherModuleName(to)
3005
3006 // If `to` is not actually in the same APEX as `from` then it does not need
3007 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003008 //
3009 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003010 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3011 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3012 return false
3013 }
3014
3015 // The dynamic linker and crash_dump tool in the runtime APEX is the only
3016 // exception to this rule. It can't make the static dependencies dynamic
3017 // because it can't do the dynamic linking for itself.
Kiyoung Kim4098c7e2020-11-30 14:42:14 +09003018 // Same rule should be applied to linkerconfig, because it should be executed
3019 // only with static linked libraries before linker is available with ld.config.txt
3020 if apexName == "com.android.runtime" && (fromName == "linker" || fromName == "crash_dump" || fromName == "linkerconfig") {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003021 return false
3022 }
3023
3024 isStubLibraryFromOtherApex := ccm.HasStubsVariants() && !abInfo.Contents.DirectlyInApex(toName)
3025 if isStubLibraryFromOtherApex && !externalDep {
3026 ctx.ModuleErrorf("%q required by %q is a native library providing stub. "+
3027 "It shouldn't be included in this APEX via static linking. Dependency path: %s", to.String(), fromName, ctx.GetPathString(false))
3028 }
3029
3030 }
3031 return true
3032 })
3033}
3034
satayevb98371c2021-06-15 16:49:50 +01003035// checkUpdatable enforces APEX and its transitive dep properties to have desired values for updatable APEXes.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003036func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
3037 if a.Updatable() {
Albert Martineefabcf2022-03-21 20:11:16 +00003038 if a.minSdkVersionValue(ctx) == "" {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003039 ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
3040 }
Jiyong Park1bc84122021-06-22 20:23:05 +09003041 if a.UsePlatformApis() {
3042 ctx.PropertyErrorf("updatable", "updatable APEXes can't use platform APIs")
3043 }
Jooyung Handfc864c2023-03-20 18:19:07 +09003044 if proptools.Bool(a.properties.Use_vndk_as_stable) {
3045 ctx.PropertyErrorf("use_vndk_as_stable", "updatable APEXes can't use external VNDK libs")
Daniel Norman69109112021-12-02 12:52:42 -08003046 }
Jiyong Parkf4020582021-11-29 12:37:10 +09003047 if a.FutureUpdatable() {
3048 ctx.PropertyErrorf("future_updatable", "Already updatable. Remove `future_updatable: true:`")
3049 }
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003050 a.checkJavaStableSdkVersion(ctx)
satayevb98371c2021-06-15 16:49:50 +01003051 a.checkClasspathFragments(ctx)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003052 }
3053}
3054
satayevb98371c2021-06-15 16:49:50 +01003055// checkClasspathFragments enforces that all classpath fragments in deps generate classpaths.proto config.
3056func (a *apexBundle) checkClasspathFragments(ctx android.ModuleContext) {
3057 ctx.VisitDirectDeps(func(module android.Module) {
3058 if tag := ctx.OtherModuleDependencyTag(module); tag == bcpfTag || tag == sscpfTag {
3059 info := ctx.OtherModuleProvider(module, java.ClasspathFragmentProtoContentInfoProvider).(java.ClasspathFragmentProtoContentInfo)
3060 if !info.ClasspathFragmentProtoGenerated {
3061 ctx.OtherModuleErrorf(module, "is included in updatable apex %v, it must not set generate_classpaths_proto to false", ctx.ModuleName())
3062 }
3063 }
3064 })
3065}
3066
3067// checkJavaStableSdkVersion enforces that all Java deps are using stable SDKs to compile.
Artur Satayev8cf899a2020-04-15 17:29:42 +01003068func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003069 // Visit direct deps only. As long as we guarantee top-level deps are using stable SDKs,
3070 // java's checkLinkType guarantees correct usage for transitive deps
Artur Satayev8cf899a2020-04-15 17:29:42 +01003071 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3072 tag := ctx.OtherModuleDependencyTag(module)
3073 switch tag {
3074 case javaLibTag, androidAppTag:
Jiyong Parkdbd710c2021-04-02 08:45:46 +09003075 if m, ok := module.(interface {
3076 CheckStableSdkVersion(ctx android.BaseModuleContext) error
3077 }); ok {
3078 if err := m.CheckStableSdkVersion(ctx); err != nil {
Artur Satayev8cf899a2020-04-15 17:29:42 +01003079 ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
3080 }
3081 }
3082 }
3083 })
3084}
3085
satayevb98371c2021-06-15 16:49:50 +01003086// checkApexAvailability ensures that the all the dependencies are marked as available for this APEX.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003087func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
3088 // Let's be practical. Availability for test, host, and the VNDK apex isn't important
3089 if ctx.Host() || a.testApex || a.vndkApex {
3090 return
3091 }
3092
3093 // Because APEXes targeting other than system/system_ext partitions can't set
3094 // apex_available, we skip checks for these APEXes
3095 if a.SocSpecific() || a.DeviceSpecific() || (a.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
3096 return
3097 }
3098
3099 // Coverage build adds additional dependencies for the coverage-only runtime libraries.
3100 // Requiring them and their transitive depencies with apex_available is not right
3101 // because they just add noise.
3102 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
3103 return
3104 }
3105
3106 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
3107 // As soon as the dependency graph crosses the APEX boundary, don't go further.
3108 if externalDep {
3109 return false
3110 }
3111
3112 apexName := ctx.ModuleName()
3113 fromName := ctx.OtherModuleName(from)
3114 toName := ctx.OtherModuleName(to)
3115
3116 // If `to` is not actually in the same APEX as `from` then it does not need
3117 // apex_available and neither do any of its dependencies.
Paul Duffin4c3e8e22021-03-18 15:41:29 +00003118 //
3119 // It is ok to call DepIsInSameApex() directly from within WalkPayloadDeps().
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003120 if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
3121 // As soon as the dependency graph crosses the APEX boundary, don't go
3122 // further.
3123 return false
3124 }
3125
3126 if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
3127 return true
3128 }
Jiyong Park767dbd92021-03-04 13:03:10 +09003129 ctx.ModuleErrorf("%q requires %q that doesn't list the APEX under 'apex_available'."+
3130 "\n\nDependency path:%s\n\n"+
3131 "Consider adding %q to 'apex_available' property of %q",
3132 fromName, toName, ctx.GetPathString(true), apexName, toName)
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003133 // Visit this module's dependencies to check and report any issues with their availability.
3134 return true
3135 })
3136}
3137
Jiyong Park192600a2021-08-03 07:52:17 +00003138// checkStaticExecutable ensures that executables in an APEX are not static.
3139func (a *apexBundle) checkStaticExecutables(ctx android.ModuleContext) {
Jiyong Parkd12979d2021-08-03 13:36:09 +09003140 // No need to run this for host APEXes
3141 if ctx.Host() {
3142 return
3143 }
3144
Jiyong Park192600a2021-08-03 07:52:17 +00003145 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
3146 if ctx.OtherModuleDependencyTag(module) != executableTag {
3147 return
3148 }
Jiyong Parkd12979d2021-08-03 13:36:09 +09003149
3150 if l, ok := module.(cc.LinkableInterface); ok && l.StaticExecutable() {
Jiyong Park192600a2021-08-03 07:52:17 +00003151 apex := a.ApexVariationName()
3152 exec := ctx.OtherModuleName(module)
3153 if isStaticExecutableAllowed(apex, exec) {
3154 return
3155 }
3156 ctx.ModuleErrorf("executable %s is static", ctx.OtherModuleName(module))
3157 }
3158 })
3159}
3160
3161// A small list of exceptions where static executables are allowed in APEXes.
3162func isStaticExecutableAllowed(apex string, exec string) bool {
3163 m := map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003164 "com.android.runtime": {
Jiyong Park192600a2021-08-03 07:52:17 +00003165 "linker",
3166 "linkerconfig",
3167 },
3168 }
3169 execNames, ok := m[apex]
3170 return ok && android.InList(exec, execNames)
3171}
3172
braleeb0c1f0c2021-06-07 22:49:13 +08003173// Collect information for opening IDE project files in java/jdeps.go.
3174func (a *apexBundle) IDEInfo(dpInfo *android.IdeInfo) {
Anton Hanssone7545852023-02-24 11:06:07 +00003175 dpInfo.Deps = append(dpInfo.Deps, a.properties.Java_libs...)
3176 dpInfo.Deps = append(dpInfo.Deps, a.properties.Bootclasspath_fragments...)
3177 dpInfo.Deps = append(dpInfo.Deps, a.properties.Systemserverclasspath_fragments...)
braleeb0c1f0c2021-06-07 22:49:13 +08003178 dpInfo.Paths = append(dpInfo.Paths, a.modulePaths...)
3179}
3180
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003181var (
3182 apexAvailBaseline = makeApexAvailableBaseline()
3183 inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
3184)
3185
Colin Cross440e0d02020-06-11 11:32:11 -07003186func baselineApexAvailable(apex, moduleName string) bool {
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003187 key := apex
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003188 moduleName = normalizeModuleName(moduleName)
3189
Colin Cross440e0d02020-06-11 11:32:11 -07003190 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003191 return true
3192 }
3193
3194 key = android.AvailableToAnyApex
Colin Cross440e0d02020-06-11 11:32:11 -07003195 if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003196 return true
3197 }
3198
3199 return false
3200}
3201
3202func normalizeModuleName(moduleName string) string {
Jiyong Park0f80c182020-01-31 02:49:53 +09003203 // Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
3204 // system. Trim the prefix for the check since they are confusing
Paul Duffind23c7262020-12-11 18:13:08 +00003205 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
Jiyong Park0f80c182020-01-31 02:49:53 +09003206 if strings.HasPrefix(moduleName, "libclang_rt.") {
3207 // This module has many arch variants that depend on the product being built.
3208 // We don't want to list them all
3209 moduleName = "libclang_rt"
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003210 }
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09003211 if strings.HasPrefix(moduleName, "androidx.") {
3212 // TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
3213 moduleName = "androidx"
3214 }
Paul Duffin7d74e7b2020-03-06 12:30:13 +00003215 return moduleName
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003216}
3217
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003218// Transform the map of apex -> modules to module -> apexes.
3219func invertApexBaseline(m map[string][]string) map[string][]string {
3220 r := make(map[string][]string)
3221 for apex, modules := range m {
3222 for _, module := range modules {
3223 r[module] = append(r[module], apex)
3224 }
3225 }
3226 return r
3227}
3228
3229// Retrieve the baseline of apexes to which the supplied module belongs.
3230func BaselineApexAvailable(moduleName string) []string {
3231 return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
3232}
3233
Jiyong Parkc0ec6f92020-11-19 23:00:52 +09003234// This is a map from apex to modules, which overrides the apex_available setting for that
3235// particular module to make it available for the apex regardless of its setting.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003236// TODO(b/147364041): remove this
3237func makeApexAvailableBaseline() map[string][]string {
3238 // The "Module separator"s below are employed to minimize merge conflicts.
3239 m := make(map[string][]string)
3240 //
3241 // Module separator
3242 //
3243 m["com.android.appsearch"] = []string{
3244 "icing-java-proto-lite",
3245 "libprotobuf-java-lite",
3246 }
3247 //
3248 // Module separator
3249 //
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003250 m["com.android.btservices"] = []string{
William Escande89bca3f2022-06-28 18:03:30 -07003251 // empty
Oriol Prieto Gasco8132fbf2022-06-17 19:44:25 +00003252 }
3253 //
3254 // Module separator
3255 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003256 m["com.android.cellbroadcast"] = []string{"CellBroadcastApp", "CellBroadcastServiceModule"}
3257 //
3258 // Module separator
3259 //
3260 m["com.android.extservices"] = []string{
3261 "error_prone_annotations",
3262 "ExtServices-core",
3263 "ExtServices",
3264 "libtextclassifier-java",
3265 "libz_current",
3266 "textclassifier-statsd",
3267 "TextClassifierNotificationLibNoManifest",
3268 "TextClassifierServiceLibNoManifest",
3269 }
3270 //
3271 // Module separator
3272 //
3273 m["com.android.neuralnetworks"] = []string{
3274 "android.hardware.neuralnetworks@1.0",
3275 "android.hardware.neuralnetworks@1.1",
3276 "android.hardware.neuralnetworks@1.2",
3277 "android.hardware.neuralnetworks@1.3",
3278 "android.hidl.allocator@1.0",
3279 "android.hidl.memory.token@1.0",
3280 "android.hidl.memory@1.0",
3281 "android.hidl.safe_union@1.0",
3282 "libarect",
3283 "libbuildversion",
3284 "libmath",
3285 "libprocpartition",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003286 }
3287 //
3288 // Module separator
3289 //
3290 m["com.android.media"] = []string{
Ray Essick5d240fb2022-02-07 11:01:32 -08003291 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003292 }
3293 //
3294 // Module separator
3295 //
3296 m["com.android.media.swcodec"] = []string{
Ray Essickde1e3002022-02-10 17:37:51 -08003297 // empty
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003298 }
3299 //
3300 // Module separator
3301 //
3302 m["com.android.mediaprovider"] = []string{
3303 "MediaProvider",
3304 "MediaProviderGoogle",
3305 "fmtlib_ndk",
3306 "libbase_ndk",
3307 "libfuse",
3308 "libfuse_jni",
3309 }
3310 //
3311 // Module separator
3312 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003313 m["com.android.runtime"] = []string{
3314 "bionic_libc_platform_headers",
3315 "libarm-optimized-routines-math",
3316 "libc_aeabi",
3317 "libc_bionic",
3318 "libc_bionic_ndk",
3319 "libc_bootstrap",
3320 "libc_common",
3321 "libc_common_shared",
3322 "libc_common_static",
3323 "libc_dns",
3324 "libc_dynamic_dispatch",
3325 "libc_fortify",
3326 "libc_freebsd",
3327 "libc_freebsd_large_stack",
3328 "libc_gdtoa",
3329 "libc_init_dynamic",
3330 "libc_init_static",
3331 "libc_jemalloc_wrapper",
3332 "libc_netbsd",
3333 "libc_nomalloc",
3334 "libc_nopthread",
3335 "libc_openbsd",
3336 "libc_openbsd_large_stack",
3337 "libc_openbsd_ndk",
3338 "libc_pthread",
3339 "libc_static_dispatch",
3340 "libc_syscalls",
3341 "libc_tzcode",
3342 "libc_unwind_static",
3343 "libdebuggerd",
3344 "libdebuggerd_common_headers",
3345 "libdebuggerd_handler_core",
3346 "libdebuggerd_handler_fallback",
3347 "libdl_static",
3348 "libjemalloc5",
3349 "liblinker_main",
3350 "liblinker_malloc",
3351 "liblz4",
3352 "liblzma",
3353 "libprocinfo",
3354 "libpropertyinfoparser",
3355 "libscudo",
3356 "libstdc++",
3357 "libsystemproperties",
3358 "libtombstoned_client_static",
3359 "libunwindstack",
3360 "libz",
3361 "libziparchive",
3362 }
3363 //
3364 // Module separator
3365 //
3366 m["com.android.tethering"] = []string{
3367 "android.hardware.tetheroffload.config-V1.0-java",
3368 "android.hardware.tetheroffload.control-V1.0-java",
3369 "android.hidl.base-V1.0-java",
3370 "libcgrouprc",
3371 "libcgrouprc_format",
3372 "libtetherutilsjni",
3373 "libvndksupport",
3374 "net-utils-framework-common",
3375 "netd_aidl_interface-V3-java",
3376 "netlink-client",
3377 "networkstack-aidl-interfaces-java",
3378 "tethering-aidl-interfaces-java",
3379 "TetheringApiCurrentLib",
3380 }
3381 //
3382 // Module separator
3383 //
3384 m["com.android.wifi"] = []string{
3385 "PlatformProperties",
3386 "android.hardware.wifi-V1.0-java",
3387 "android.hardware.wifi-V1.0-java-constants",
3388 "android.hardware.wifi-V1.1-java",
3389 "android.hardware.wifi-V1.2-java",
3390 "android.hardware.wifi-V1.3-java",
3391 "android.hardware.wifi-V1.4-java",
3392 "android.hardware.wifi.hostapd-V1.0-java",
3393 "android.hardware.wifi.hostapd-V1.1-java",
3394 "android.hardware.wifi.hostapd-V1.2-java",
3395 "android.hardware.wifi.supplicant-V1.0-java",
3396 "android.hardware.wifi.supplicant-V1.1-java",
3397 "android.hardware.wifi.supplicant-V1.2-java",
3398 "android.hardware.wifi.supplicant-V1.3-java",
3399 "android.hidl.base-V1.0-java",
3400 "android.hidl.manager-V1.0-java",
3401 "android.hidl.manager-V1.1-java",
3402 "android.hidl.manager-V1.2-java",
3403 "bouncycastle-unbundled",
3404 "dnsresolver_aidl_interface-V2-java",
3405 "error_prone_annotations",
3406 "framework-wifi-pre-jarjar",
3407 "framework-wifi-util-lib",
3408 "ipmemorystore-aidl-interfaces-V3-java",
3409 "ipmemorystore-aidl-interfaces-java",
3410 "ksoap2",
3411 "libnanohttpd",
3412 "libwifi-jni",
3413 "net-utils-services-common",
3414 "netd_aidl_interface-V2-java",
3415 "netd_aidl_interface-unstable-java",
3416 "netd_event_listener_interface-java",
3417 "netlink-client",
3418 "networkstack-client",
3419 "services.net",
3420 "wifi-lite-protos",
3421 "wifi-nano-protos",
3422 "wifi-service-pre-jarjar",
3423 "wifi-service-resources",
3424 }
3425 //
3426 // Module separator
3427 //
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003428 m["com.android.os.statsd"] = []string{
3429 "libstatssocket",
3430 }
3431 //
3432 // Module separator
3433 //
3434 m[android.AvailableToAnyApex] = []string{
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003435 "libclang_rt",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003436 "libprofile-clang-extras",
3437 "libprofile-clang-extras_ndk",
3438 "libprofile-extras",
3439 "libprofile-extras_ndk",
Ryan Prichardb35a85e2021-01-13 19:18:53 -08003440 "libunwind",
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003441 }
3442 return m
3443}
3444
3445func init() {
Spandan Dasf14e2542021-11-12 00:01:37 +00003446 android.AddNeverAllowRules(createBcpPermittedPackagesRules(qBcpPackages())...)
3447 android.AddNeverAllowRules(createBcpPermittedPackagesRules(rBcpPackages())...)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003448}
3449
Spandan Dasf14e2542021-11-12 00:01:37 +00003450func createBcpPermittedPackagesRules(bcpPermittedPackages map[string][]string) []android.Rule {
3451 rules := make([]android.Rule, 0, len(bcpPermittedPackages))
3452 for jar, permittedPackages := range bcpPermittedPackages {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003453 permittedPackagesRule := android.NeverAllow().
Spandan Dasf14e2542021-11-12 00:01:37 +00003454 With("name", jar).
3455 WithMatcher("permitted_packages", android.NotInList(permittedPackages)).
3456 Because(jar +
3457 " bootjar may only use these package prefixes: " + strings.Join(permittedPackages, ",") +
Anton Hanssone1b18362021-12-23 15:05:38 +00003458 ". Please consider the following alternatives:\n" +
Andrei Onead967aee2022-01-19 15:36:40 +00003459 " 1. If the offending code is from a statically linked library, consider " +
3460 "removing that dependency and using an alternative already in the " +
3461 "bootclasspath, or perhaps a shared library." +
3462 " 2. Move the offending code into an allowed package.\n" +
3463 " 3. Jarjar the offending code. Please be mindful of the potential system " +
3464 "health implications of bundling that code, particularly if the offending jar " +
3465 "is part of the bootclasspath.")
Spandan Dasf14e2542021-11-12 00:01:37 +00003466
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003467 rules = append(rules, permittedPackagesRule)
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003468 }
3469 return rules
3470}
3471
Anton Hanssone1b18362021-12-23 15:05:38 +00003472// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003473// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003474func qBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003475 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003476 "conscrypt": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003477 "android.net.ssl",
3478 "com.android.org.conscrypt",
3479 },
Wei Li40f98732022-05-20 22:08:11 -07003480 "updatable-media": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003481 "android.media",
3482 },
3483 }
3484}
3485
Anton Hanssone1b18362021-12-23 15:05:38 +00003486// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003487// Adding code to the bootclasspath in new packages will cause issues on module update.
Spandan Dasf14e2542021-11-12 00:01:37 +00003488func rBcpPackages() map[string][]string {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003489 return map[string][]string{
Wei Li40f98732022-05-20 22:08:11 -07003490 "framework-mediaprovider": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003491 "android.provider",
3492 },
Wei Li40f98732022-05-20 22:08:11 -07003493 "framework-permission": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003494 "android.permission",
3495 "android.app.role",
3496 "com.android.permission",
3497 "com.android.role",
3498 },
Wei Li40f98732022-05-20 22:08:11 -07003499 "framework-sdkextensions": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003500 "android.os.ext",
3501 },
Wei Li40f98732022-05-20 22:08:11 -07003502 "framework-statsd": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003503 "android.app",
3504 "android.os",
3505 "android.util",
3506 "com.android.internal.statsd",
3507 "com.android.server.stats",
3508 },
Wei Li40f98732022-05-20 22:08:11 -07003509 "framework-wifi": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003510 "com.android.server.wifi",
3511 "com.android.wifi.x",
3512 "android.hardware.wifi",
3513 "android.net.wifi",
3514 },
Wei Li40f98732022-05-20 22:08:11 -07003515 "framework-tethering": {
Jiyong Park8e6d52f2020-11-19 14:37:47 +09003516 "android.net",
3517 },
3518 }
3519}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003520
3521// For Bazel / bp2build
3522
3523type bazelApexBundleAttributes struct {
Yu Liu4ae55d12022-01-05 17:17:23 -08003524 Manifest bazel.LabelAttribute
3525 Android_manifest bazel.LabelAttribute
3526 File_contexts bazel.LabelAttribute
Jingwen Chena8623da2023-03-28 13:05:02 +00003527 Canned_fs_config bazel.LabelAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003528 Key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003529 Certificate bazel.LabelAttribute // used when the certificate prop is a module
3530 Certificate_name bazel.StringAttribute // used when the certificate prop is a string
Liz Kammerb83b7b02022-12-21 14:53:41 -05003531 Min_sdk_version bazel.StringAttribute
Yu Liu4ae55d12022-01-05 17:17:23 -08003532 Updatable bazel.BoolAttribute
3533 Installable bazel.BoolAttribute
3534 Binaries bazel.LabelListAttribute
3535 Prebuilts bazel.LabelListAttribute
3536 Native_shared_libs_32 bazel.LabelListAttribute
3537 Native_shared_libs_64 bazel.LabelListAttribute
Wei Lif034cb42022-01-19 15:54:31 -08003538 Compressible bazel.BoolAttribute
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003539 Package_name *string
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003540 Logging_parent *string
Yu Liu4c212ce2022-10-14 12:20:20 -07003541 Tests bazel.LabelListAttribute
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003542 Base_apex_name *string
Yu Liu4ae55d12022-01-05 17:17:23 -08003543}
3544
3545type convertedNativeSharedLibs struct {
3546 Native_shared_libs_32 bazel.LabelListAttribute
3547 Native_shared_libs_64 bazel.LabelListAttribute
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003548}
3549
Liz Kammerb83b7b02022-12-21 14:53:41 -05003550const (
3551 minSdkVersionPropName = "Min_sdk_version"
3552)
3553
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003554// ConvertWithBp2build performs bp2build conversion of an apex
3555func (a *apexBundle) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Yu Liu4c212ce2022-10-14 12:20:20 -07003556 // We only convert apex and apex_test modules at this time
3557 if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003558 return
3559 }
3560
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003561 attrs, props, commonAttrs := convertWithBp2build(a, ctx)
3562 commonAttrs.Name = a.Name()
Yu Liu4c212ce2022-10-14 12:20:20 -07003563 ctx.CreateBazelTargetModule(props, commonAttrs, &attrs)
Wei Li1c66fc72022-05-09 23:59:14 -07003564}
3565
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003566func convertWithBp2build(a *apexBundle, ctx android.TopDownMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties, android.CommonAttributes) {
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003567 var manifestLabelAttribute bazel.LabelAttribute
Wei Li40f98732022-05-20 22:08:11 -07003568 manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003569
3570 var androidManifestLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003571 if a.properties.AndroidManifest != nil {
3572 androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003573 }
3574
3575 var fileContextsLabelAttribute bazel.LabelAttribute
Wei Li1c66fc72022-05-09 23:59:14 -07003576 if a.properties.File_contexts == nil {
3577 // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:<module name>-file_contexts
3578 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts"))
3579 } else if strings.HasPrefix(*a.properties.File_contexts, ":") {
3580 // File_contexts is a module
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003581 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts))
Wei Li1c66fc72022-05-09 23:59:14 -07003582 } else {
3583 // File_contexts is a file
3584 fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003585 }
3586
Jingwen Chena8623da2023-03-28 13:05:02 +00003587 var cannedFsConfigAttribute bazel.LabelAttribute
3588 if a.properties.Canned_fs_config != nil {
3589 cannedFsConfigAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Canned_fs_config))
3590 }
3591
Cole Faust912bc882023-03-08 12:29:50 -08003592 productVariableProps := android.ProductVariableProperties(ctx, a)
Albert Martineefabcf2022-03-21 20:11:16 +00003593 // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but
3594 // given it's coming via config, we probably don't want to put it in here.
Liz Kammerb83b7b02022-12-21 14:53:41 -05003595 var minSdkVersion bazel.StringAttribute
zhidou133c55b2023-01-31 19:34:10 +00003596 if a.overridableProperties.Min_sdk_version != nil {
3597 minSdkVersion.SetValue(*a.overridableProperties.Min_sdk_version)
Liz Kammerb83b7b02022-12-21 14:53:41 -05003598 }
3599 if props, ok := productVariableProps[minSdkVersionPropName]; ok {
3600 for c, p := range props {
3601 if val, ok := p.(*string); ok {
3602 minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
3603 }
3604 }
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003605 }
3606
3607 var keyLabelAttribute bazel.LabelAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003608 if a.overridableProperties.Key != nil {
3609 keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key))
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003610 }
3611
Jingwen Chen6817bbb2022-10-14 09:56:07 +00003612 // Certificate
3613 certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003614
Yu Liu4ae55d12022-01-05 17:17:23 -08003615 nativeSharedLibs := &convertedNativeSharedLibs{
3616 Native_shared_libs_32: bazel.LabelListAttribute{},
3617 Native_shared_libs_64: bazel.LabelListAttribute{},
3618 }
Vinh Tran8f5310f2022-10-07 18:16:47 -04003619
3620 // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68
3621 // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669
3622 // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common".
3623 // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first"
3624 compileMultilib := "first"
Yu Liu4ae55d12022-01-05 17:17:23 -08003625 if a.CompileMultilib() != nil {
3626 compileMultilib = *a.CompileMultilib()
3627 }
3628
3629 // properties.Native_shared_libs is treated as "both"
3630 convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs)
3631 convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs)
3632 convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs)
3633 convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs)
3634 convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003635
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003636 prebuilts := a.overridableProperties.Prebuilts
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -04003637 prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
3638 prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
3639
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003640 binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries)
Jingwen Chenb07c9012021-12-08 10:05:45 +00003641 binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003642
Yu Liu4c212ce2022-10-14 12:20:20 -07003643 var testsAttrs bazel.LabelListAttribute
3644 if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 {
3645 tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests)
3646 testsAttrs = bazel.MakeLabelListAttribute(tests)
3647 }
3648
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003649 var updatableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003650 if a.properties.Updatable != nil {
3651 updatableAttribute.Value = a.properties.Updatable
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -04003652 }
3653
3654 var installableAttribute bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -04003655 if a.properties.Installable != nil {
3656 installableAttribute.Value = a.properties.Installable
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003657 }
3658
Wei Lif034cb42022-01-19 15:54:31 -08003659 var compressibleAttribute bazel.BoolAttribute
3660 if a.overridableProperties.Compressible != nil {
3661 compressibleAttribute.Value = a.overridableProperties.Compressible
3662 }
3663
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003664 var packageName *string
3665 if a.overridableProperties.Package_name != "" {
3666 packageName = &a.overridableProperties.Package_name
3667 }
3668
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003669 var loggingParent *string
3670 if a.overridableProperties.Logging_parent != "" {
3671 loggingParent = &a.overridableProperties.Logging_parent
3672 }
3673
Wei Li1c66fc72022-05-09 23:59:14 -07003674 attrs := bazelApexBundleAttributes{
Yu Liu4ae55d12022-01-05 17:17:23 -08003675 Manifest: manifestLabelAttribute,
3676 Android_manifest: androidManifestLabelAttribute,
3677 File_contexts: fileContextsLabelAttribute,
Jingwen Chena8623da2023-03-28 13:05:02 +00003678 Canned_fs_config: cannedFsConfigAttribute,
Yu Liu4ae55d12022-01-05 17:17:23 -08003679 Min_sdk_version: minSdkVersion,
3680 Key: keyLabelAttribute,
Jingwen Chenbea58092022-09-29 16:56:02 +00003681 Certificate: certificate,
3682 Certificate_name: certificateName,
Yu Liu4ae55d12022-01-05 17:17:23 -08003683 Updatable: updatableAttribute,
3684 Installable: installableAttribute,
3685 Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32,
3686 Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64,
3687 Binaries: binariesLabelListAttribute,
3688 Prebuilts: prebuiltsLabelListAttribute,
Wei Lif034cb42022-01-19 15:54:31 -08003689 Compressible: compressibleAttribute,
Jingwen Chen9b7ebca2022-06-03 09:11:20 +00003690 Package_name: packageName,
Jingwen Chenb732d7c2022-06-10 08:14:19 +00003691 Logging_parent: loggingParent,
Yu Liu4c212ce2022-10-14 12:20:20 -07003692 Tests: testsAttrs,
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003693 }
3694
3695 props := bazel.BazelTargetModuleProperties{
3696 Rule_class: "apex",
Cole Faust5f90da32022-04-29 13:37:43 -07003697 Bzl_load_location: "//build/bazel/rules/apex:apex.bzl",
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003698 }
3699
Liz Kammer1a1c9df2023-03-28 11:39:50 -04003700 commonAttrs := android.CommonAttributes{}
3701 if a.testApex {
3702 commonAttrs.Testonly = proptools.BoolPtr(true)
3703 }
3704
3705 return attrs, props, commonAttrs
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04003706}
Yu Liu4ae55d12022-01-05 17:17:23 -08003707
3708// The following conversions are based on this table where the rows are the compile_multilib
3709// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell
3710// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it
3711// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it
3712// should not be compiled.
3713// multib/compile_multilib, 32, 64, both, first
3714// 32, 32/32, none/none, 32/32, none/32
3715// 64, none/none, 64/none, 64/none, 64/none
3716// both, 32/32, 64/none, 32&64/32, 64/32
3717// first, 32/32, 64/none, 64/32, 64/32
3718
3719func convert32Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3720 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3721 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3722 switch compileMultilb {
3723 case "both", "32":
3724 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3725 case "first":
3726 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3727 case "64":
3728 // Incompatible, ignore
3729 default:
3730 invalidCompileMultilib(ctx, compileMultilb)
3731 }
3732}
3733
3734func convert64Libs(ctx android.TopDownMutatorContext, compileMultilb string,
3735 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3736 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3737 switch compileMultilb {
3738 case "both", "64", "first":
3739 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3740 case "32":
3741 // Incompatible, ignore
3742 default:
3743 invalidCompileMultilib(ctx, compileMultilb)
3744 }
3745}
3746
3747func convertBothLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3748 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3749 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3750 switch compileMultilb {
3751 case "both":
3752 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3753 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3754 case "first":
3755 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3756 case "32":
3757 makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3758 case "64":
3759 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3760 default:
3761 invalidCompileMultilib(ctx, compileMultilb)
3762 }
3763}
3764
3765func convertFirstLibs(ctx android.TopDownMutatorContext, compileMultilb string,
3766 libs []string, nativeSharedLibs *convertedNativeSharedLibs) {
3767 libsLabelList := android.BazelLabelForModuleDeps(ctx, libs)
3768 switch compileMultilb {
3769 case "both", "first":
3770 makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs)
3771 case "32":
3772 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3773 case "64":
3774 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3775 default:
3776 invalidCompileMultilib(ctx, compileMultilb)
3777 }
3778}
3779
3780func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3781 make32SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3782 make64SharedLibsAttributes(libsLabelList, nativeSharedLibs)
3783}
3784
3785func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3786 list := bazel.LabelListAttribute{}
3787 list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList)
3788 nativeSharedLibs.Native_shared_libs_32.Append(list)
3789}
3790
3791func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3792 makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3793 makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32)
3794}
3795
3796func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) {
3797 makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3798 makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64)
3799}
3800
3801func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
3802 labelListAttr *bazel.LabelListAttribute) {
3803 list := bazel.LabelListAttribute{}
3804 list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList)
3805 labelListAttr.Append(list)
3806}
3807
3808func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
3809 ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
3810}
Spandan Dasf57a9662023-04-12 19:05:49 +00003811
3812func (a *apexBundle) IsTestApex() bool {
3813 return a.testApex
3814}