blob: 7cafa41857b09452c8a617fad512bbbc810b8f8a [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
15package apex
16
17import (
18 "fmt"
Jooyung Han54aca7b2019-11-20 02:26:02 +090019 "path"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090020 "path/filepath"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090021 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090022 "strings"
Jooyung Han344d5432019-08-23 11:17:39 +090023 "sync"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090024
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Alex Light778127a2019-02-27 14:19:50 -080028 "android/soong/python"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090029
30 "github.com/google/blueprint"
Alex Light778127a2019-02-27 14:19:50 -080031 "github.com/google/blueprint/bootstrap"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090032 "github.com/google/blueprint/proptools"
33)
34
Jooyung Han72bd2f82019-10-23 16:46:38 +090035const (
36 imageApexSuffix = ".apex"
37 zipApexSuffix = ".zipapex"
Sundong Ahnabb64432019-10-22 13:58:29 +090038 flattenedSuffix = ".flattened"
Alex Light5098a612018-11-29 17:12:15 -080039
Sundong Ahnabb64432019-10-22 13:58:29 +090040 imageApexType = "image"
41 zipApexType = "zip"
42 flattenedApexType = "flattened"
Jooyung Han72bd2f82019-10-23 16:46:38 +090043)
Jiyong Park48ca7dc2018-10-10 14:01:00 +090044
45type dependencyTag struct {
46 blueprint.BaseDependencyTag
47 name string
48}
49
50var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +090051 sharedLibTag = dependencyTag{name: "sharedLib"}
52 executableTag = dependencyTag{name: "executable"}
53 javaLibTag = dependencyTag{name: "javaLib"}
54 prebuiltTag = dependencyTag{name: "prebuilt"}
Roland Levillain630846d2019-06-26 12:48:34 +010055 testTag = dependencyTag{name: "test"}
Jiyong Parkc00cbd92018-10-30 21:20:05 +090056 keyTag = dependencyTag{name: "key"}
57 certificateTag = dependencyTag{name: "certificate"}
Jooyung Han5c998b92019-06-27 11:30:33 +090058 usesTag = dependencyTag{name: "uses"}
Sundong Ahne1f05aa2019-08-27 13:55:42 +090059 androidAppTag = dependencyTag{name: "androidApp"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060)
61
62func init() {
Jiyong Parkd1063c12019-07-17 20:08:41 +090063 android.RegisterModuleType("apex", BundleFactory)
Alex Light0851b882019-02-07 13:20:53 -080064 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jooyung Han344d5432019-08-23 11:17:39 +090065 android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +090066 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070067 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park5d790c32019-11-15 18:40:32 +090068 android.RegisterModuleType("override_apex", overrideApexFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +090069
Jooyung Han31c470b2019-10-18 16:26:59 +090070 android.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +090071 android.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han7a78a922019-10-08 21:59:58 +090072
73 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
74 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
75 sort.Strings(*apexFileContextsInfos)
76 ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
77 })
Jiyong Parkd1063c12019-07-17 20:08:41 +090078}
79
Jooyung Han31c470b2019-10-18 16:26:59 +090080func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
81 ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
82 ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
83}
84
Jiyong Parkd1063c12019-07-17 20:08:41 +090085func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
Jiyong Parka308ea12019-11-15 10:38:39 +090086 ctx.BottomUp("apex_deps", apexDepsMutator)
Jiyong Parkd1063c12019-07-17 20:08:41 +090087 ctx.BottomUp("apex", apexMutator).Parallel()
88 ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
89 ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
Jiyong Park48ca7dc2018-10-10 14:01:00 +090090}
91
Jiyong Park48ca7dc2018-10-10 14:01:00 +090092// Mark the direct and transitive dependencies of apex bundles so that they
93// can be built for the apex bundles.
Jiyong Parka308ea12019-11-15 10:38:39 +090094func apexDepsMutator(mctx android.BottomUpMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -080095 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -080096 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +090097 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +090098 depName := mctx.OtherModuleName(child)
99 // If the parent is apexBundle, this child is directly depended.
100 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800101 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800102 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
103 // non-installable apex's cannot be installed and so should not prevent libraries from being
104 // installed to the system.
105 android.UpdateApexDependency(apexBundleName, depName, directDep)
106 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900107
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900108 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900109 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900110 return true
111 } else {
112 return false
113 }
114 })
115 }
116}
117
118// Create apex variations if a module is included in APEX(s).
119func apexMutator(mctx android.BottomUpMutatorContext) {
120 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900121 am.CreateApexVariations(mctx)
Jooyung Han54aca7b2019-11-20 02:26:02 +0900122 } else if _, ok := mctx.Module().(*apexBundle); ok {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900123 // apex bundle itself is mutated so that it and its modules have same
124 // apex variant.
125 apexBundleName := mctx.ModuleName()
126 mctx.CreateVariations(apexBundleName)
Jiyong Park5d790c32019-11-15 18:40:32 +0900127 } else if o, ok := mctx.Module().(*OverrideApex); ok {
128 apexBundleName := o.GetOverriddenModuleName()
129 if apexBundleName == "" {
130 mctx.ModuleErrorf("base property is not set")
131 return
132 }
133 mctx.CreateVariations(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900134 }
Jiyong Park5d790c32019-11-15 18:40:32 +0900135
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900136}
Sundong Ahne9b55722019-09-06 17:37:42 +0900137
Jooyung Han7a78a922019-10-08 21:59:58 +0900138var (
139 apexFileContextsInfosKey = android.NewOnceKey("apexFileContextsInfosKey")
140 apexFileContextsInfosMutex sync.Mutex
141)
142
143func apexFileContextsInfos(config android.Config) *[]string {
144 return config.Once(apexFileContextsInfosKey, func() interface{} {
145 return &[]string{}
146 }).(*[]string)
147}
148
Jooyung Han54aca7b2019-11-20 02:26:02 +0900149func addFlattenedFileContextsInfos(ctx android.BaseModuleContext, fileContextsInfo string) {
Jooyung Han7a78a922019-10-08 21:59:58 +0900150 apexFileContextsInfosMutex.Lock()
151 defer apexFileContextsInfosMutex.Unlock()
152 apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
Jooyung Han54aca7b2019-11-20 02:26:02 +0900153 *apexFileContextsInfos = append(*apexFileContextsInfos, fileContextsInfo)
Jooyung Han7a78a922019-10-08 21:59:58 +0900154}
155
Sundong Ahne9b55722019-09-06 17:37:42 +0900156func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
Sundong Ahne8fb7242019-09-17 13:50:45 +0900157 if ab, ok := mctx.Module().(*apexBundle); ok {
Sundong Ahnabb64432019-10-22 13:58:29 +0900158 var variants []string
159 switch proptools.StringDefault(ab.properties.Payload_type, "image") {
160 case "image":
161 variants = append(variants, imageApexType, flattenedApexType)
162 case "zip":
163 variants = append(variants, zipApexType)
164 case "both":
165 variants = append(variants, imageApexType, zipApexType, flattenedApexType)
166 default:
Sundong Ahnd95aa2d2019-10-08 19:34:03 +0900167 mctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
Sundong Ahnabb64432019-10-22 13:58:29 +0900168 return
169 }
170
171 modules := mctx.CreateLocalVariations(variants...)
172
173 for i, v := range variants {
174 switch v {
175 case imageApexType:
176 modules[i].(*apexBundle).properties.ApexType = imageApex
177 case zipApexType:
178 modules[i].(*apexBundle).properties.ApexType = zipApex
179 case flattenedApexType:
180 modules[i].(*apexBundle).properties.ApexType = flattenedApex
Jooyung Han91df2082019-11-20 01:49:42 +0900181 if !mctx.Config().FlattenApex() && ab.Platform() {
Sundong Ahnd95aa2d2019-10-08 19:34:03 +0900182 modules[i].(*apexBundle).MakeAsSystemExt()
183 }
Sundong Ahnabb64432019-10-22 13:58:29 +0900184 }
Sundong Ahne9b55722019-09-06 17:37:42 +0900185 }
Jiyong Park5d790c32019-11-15 18:40:32 +0900186 } else if _, ok := mctx.Module().(*OverrideApex); ok {
187 mctx.CreateVariations(imageApexType, flattenedApexType)
Sundong Ahne9b55722019-09-06 17:37:42 +0900188 }
189}
190
Jooyung Han5c998b92019-06-27 11:30:33 +0900191func apexUsesMutator(mctx android.BottomUpMutatorContext) {
192 if ab, ok := mctx.Module().(*apexBundle); ok {
193 mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
194 }
195}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900196
Jooyung Handc782442019-11-01 03:14:38 +0900197var (
198 useVendorWhitelistKey = android.NewOnceKey("useVendorWhitelist")
199)
200
201// useVendorWhitelist returns the list of APEXes which are allowed to use_vendor.
202// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
203// which may cause compatibility issues. (e.g. libbinder)
204// Even though libbinder restricts its availability via 'apex_available' property and relies on
205// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
206// to avoid similar problems.
207func useVendorWhitelist(config android.Config) []string {
208 return config.Once(useVendorWhitelistKey, func() interface{} {
209 return []string{
210 // swcodec uses "vendor" variants for smaller size
211 "com.android.media.swcodec",
212 "test_com.android.media.swcodec",
213 }
214 }).([]string)
215}
216
217// setUseVendorWhitelistForTest overrides useVendorWhitelist and must be
218// called before the first call to useVendorWhitelist()
219func setUseVendorWhitelistForTest(config android.Config, whitelist []string) {
220 config.Once(useVendorWhitelistKey, func() interface{} {
221 return whitelist
222 })
223}
224
Alex Light9670d332019-01-29 18:07:33 -0800225type apexNativeDependencies struct {
226 // List of native libraries
227 Native_shared_libs []string
Jooyung Han344d5432019-08-23 11:17:39 +0900228
Alex Light9670d332019-01-29 18:07:33 -0800229 // List of native executables
230 Binaries []string
Jooyung Han344d5432019-08-23 11:17:39 +0900231
Roland Levillain630846d2019-06-26 12:48:34 +0100232 // List of native tests
233 Tests []string
Alex Light9670d332019-01-29 18:07:33 -0800234}
Jooyung Han344d5432019-08-23 11:17:39 +0900235
Alex Light9670d332019-01-29 18:07:33 -0800236type apexMultilibProperties struct {
237 // Native dependencies whose compile_multilib is "first"
238 First apexNativeDependencies
239
240 // Native dependencies whose compile_multilib is "both"
241 Both apexNativeDependencies
242
243 // Native dependencies whose compile_multilib is "prefer32"
244 Prefer32 apexNativeDependencies
245
246 // Native dependencies whose compile_multilib is "32"
247 Lib32 apexNativeDependencies
248
249 // Native dependencies whose compile_multilib is "64"
250 Lib64 apexNativeDependencies
251}
252
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900253type apexBundleProperties struct {
254 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000255 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800256 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900257
Jiyong Park40e26a22019-02-08 02:53:06 +0900258 // AndroidManifest.xml file used for the zip container of this APEX bundle.
259 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800260 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900261
Roland Levillain411c5842019-09-19 16:37:20 +0100262 // Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
263 // device (/apex/<apex_name>).
264 // If unspecified, defaults to the value of name.
Jiyong Park05e70dd2019-03-18 14:26:32 +0900265 Apex_name *string
266
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900267 // Determines the file contexts file for setting security context to each file in this APEX bundle.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900268 // For platform APEXes, this should points to a file under /system/sepolicy
269 // Default: /system/sepolicy/apex/<module_name>_file_contexts.
270 File_contexts *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900271
272 // List of native shared libs that are embedded inside this APEX bundle
273 Native_shared_libs []string
274
Roland Levillain630846d2019-06-26 12:48:34 +0100275 // List of executables that are embedded inside this APEX bundle
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900276 Binaries []string
277
278 // List of java libraries that are embedded inside this APEX bundle
279 Java_libs []string
280
281 // List of prebuilt files that are embedded inside this APEX bundle
282 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900283
Roland Levillain630846d2019-06-26 12:48:34 +0100284 // List of tests that are embedded inside this APEX bundle
285 Tests []string
286
Jiyong Parkff1458f2018-10-12 21:49:38 +0900287 // Name of the apex_key module that provides the private key to sign APEX
288 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900289
Alex Light5098a612018-11-29 17:12:15 -0800290 // The type of APEX to build. Controls what the APEX payload is. Either
291 // 'image', 'zip' or 'both'. Default: 'image'.
292 Payload_type *string
293
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900294 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
295 // or an android_app_certificate module name in the form ":module".
296 Certificate *string
297
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900298 // Whether this APEX is installable to one of the partitions. Default: true.
299 Installable *bool
300
Jiyong Parkda6eb592018-12-19 17:12:36 +0900301 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
302 // Default is false.
303 Use_vendor *bool
304
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800305 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
306 Ignore_system_library_special_case *bool
307
Alex Light9670d332019-01-29 18:07:33 -0800308 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900309
Jiyong Parkf97782b2019-02-13 20:28:58 +0900310 // List of sanitizer names that this APEX is enabled for
311 SanitizerNames []string `blueprint:"mutated"`
Jooyung Han5c998b92019-06-27 11:30:33 +0900312
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900313 PreventInstall bool `blueprint:"mutated"`
314
315 HideFromMake bool `blueprint:"mutated"`
316
Jooyung Han5c998b92019-06-27 11:30:33 +0900317 // Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
318 Provide_cpp_shared_libs *bool
319
320 // List of providing APEXes' names so that this APEX can depend on provided shared libraries.
321 Uses []string
Nikita Ioffe5d5ae762019-08-31 14:38:05 +0100322
323 // A txt file containing list of files that are whitelisted to be included in this APEX.
324 Whitelisted_files *string
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900325
Sundong Ahnabb64432019-10-22 13:58:29 +0900326 // package format of this apex variant; could be non-flattened, flattened, or zip.
327 // imageApex, zipApex or flattened
328 ApexType apexPackaging `blueprint:"mutated"`
Sundong Ahne8fb7242019-09-17 13:50:45 +0900329
Jiyong Parkd1063c12019-07-17 20:08:41 +0900330 // List of SDKs that are used to build this APEX. A reference to an SDK should be either
331 // `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
332 // is implied. This value affects all modules included in this APEX. In other words, they are
333 // also built with the SDKs specified here.
334 Uses_sdks []string
Jiyong Park5d790c32019-11-15 18:40:32 +0900335
336 // Names of modules to be overridden. Listed modules can only be other binaries
337 // (in Make or Soong).
338 // This does not completely prevent installation of the overridden binaries, but if both
339 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
340 // from PRODUCT_PACKAGES.
341 Overrides []string
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000342
343 // Whenever apex_payload.img of the APEX should include dm-verity hashtree.
344 // Should be only used in tests#.
345 Test_only_no_hashtree *bool
Jooyung Han214bf372019-11-12 13:03:50 +0900346
347 // Whether this APEX should support Android10. Default is false. If this is set true, then apex_manifest.json is bundled as well
348 // because Android10 requires legacy apex_manifest.json instead of apex_manifest.pb
349 Legacy_android10_support *bool
Alex Light9670d332019-01-29 18:07:33 -0800350}
351
352type apexTargetBundleProperties struct {
353 Target struct {
354 // Multilib properties only for android.
355 Android struct {
356 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900357 }
Jooyung Han344d5432019-08-23 11:17:39 +0900358
Alex Light9670d332019-01-29 18:07:33 -0800359 // Multilib properties only for host.
360 Host struct {
361 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900362 }
Jooyung Han344d5432019-08-23 11:17:39 +0900363
Alex Light9670d332019-01-29 18:07:33 -0800364 // Multilib properties only for host linux_bionic.
365 Linux_bionic struct {
366 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900367 }
Jooyung Han344d5432019-08-23 11:17:39 +0900368
Alex Light9670d332019-01-29 18:07:33 -0800369 // Multilib properties only for host linux_glibc.
370 Linux_glibc struct {
371 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900372 }
373 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900374}
375
Jiyong Park5d790c32019-11-15 18:40:32 +0900376type overridableProperties struct {
377 // List of APKs to package inside APEX
378 Apps []string
379}
380
Alex Light5098a612018-11-29 17:12:15 -0800381type apexPackaging int
382
383const (
384 imageApex apexPackaging = iota
385 zipApex
Sundong Ahnabb64432019-10-22 13:58:29 +0900386 flattenedApex
Alex Light5098a612018-11-29 17:12:15 -0800387)
388
Sundong Ahnabb64432019-10-22 13:58:29 +0900389// The suffix for the output "file", not the module
Alex Light5098a612018-11-29 17:12:15 -0800390func (a apexPackaging) suffix() string {
391 switch a {
392 case imageApex:
393 return imageApexSuffix
394 case zipApex:
395 return zipApexSuffix
Alex Light5098a612018-11-29 17:12:15 -0800396 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100397 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800398 }
399}
400
401func (a apexPackaging) name() string {
402 switch a {
403 case imageApex:
404 return imageApexType
405 case zipApex:
406 return zipApexType
Alex Light5098a612018-11-29 17:12:15 -0800407 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100408 panic(fmt.Errorf("unknown APEX type %d", a))
Alex Light5098a612018-11-29 17:12:15 -0800409 }
410}
411
Jiyong Parkf653b052019-11-18 15:39:01 +0900412type apexFileClass int
413
414const (
415 etc apexFileClass = iota
416 nativeSharedLib
417 nativeExecutable
418 shBinary
419 pyBinary
420 goBinary
421 javaSharedLib
422 nativeTest
423 app
424)
425
Jiyong Park8fd61922018-11-08 02:50:25 +0900426func (class apexFileClass) NameInMake() string {
427 switch class {
428 case etc:
429 return "ETC"
430 case nativeSharedLib:
431 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800432 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900433 return "EXECUTABLES"
434 case javaSharedLib:
435 return "JAVA_LIBRARIES"
Roland Levillain630846d2019-06-26 12:48:34 +0100436 case nativeTest:
437 return "NATIVE_TESTS"
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900438 case app:
Jiyong Parkf383f7c2019-10-11 20:46:25 +0900439 // b/142537672 Why isn't this APP? We want to have full control over
440 // the paths and file names of the apk file under the flattend APEX.
441 // If this is set to APP, then the paths and file names are modified
442 // by the Make build system. For example, it is installed to
443 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
444 // /system/apex/<apexname>/app/<Appname> because the build system automatically
445 // appends module name (which is <apexname>.<Appname> to the path.
446 return "ETC"
Jiyong Park8fd61922018-11-08 02:50:25 +0900447 default:
Roland Levillain4644b222019-07-31 14:09:17 +0100448 panic(fmt.Errorf("unknown class %d", class))
Jiyong Park8fd61922018-11-08 02:50:25 +0900449 }
450}
451
Jiyong Parkf653b052019-11-18 15:39:01 +0900452// apexFile represents a file in an APEX bundle
Jiyong Park8fd61922018-11-08 02:50:25 +0900453type apexFile struct {
454 builtFile android.Path
455 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900456 installDir string
457 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900458 module android.Module
Jiyong Parkf653b052019-11-18 15:39:01 +0900459 // list of symlinks that will be created in installDir that point to this apexFile
460 symlinks []string
461 transitiveDep bool
Jiyong Park1833cef2019-12-13 13:28:36 +0900462 moduleDir string
Jiyong Parkf653b052019-11-18 15:39:01 +0900463}
464
Jiyong Park1833cef2019-12-13 13:28:36 +0900465func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, moduleName string, installDir string, class apexFileClass, module android.Module) apexFile {
466 ret := apexFile{
Jiyong Parkf653b052019-11-18 15:39:01 +0900467 builtFile: builtFile,
468 moduleName: moduleName,
469 installDir: installDir,
470 class: class,
471 module: module,
472 }
Jiyong Park1833cef2019-12-13 13:28:36 +0900473 if module != nil {
474 ret.moduleDir = ctx.OtherModuleDir(module)
475 }
476 return ret
Jiyong Parkf653b052019-11-18 15:39:01 +0900477}
478
479func (af *apexFile) Ok() bool {
Jiyong Park479321d2019-12-16 11:47:12 +0900480 return af.builtFile != nil && af.builtFile.String() != ""
Jiyong Park8fd61922018-11-08 02:50:25 +0900481}
482
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900483type apexBundle struct {
484 android.ModuleBase
485 android.DefaultableModuleBase
Jiyong Park5d790c32019-11-15 18:40:32 +0900486 android.OverridableModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900487 android.SdkBase
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900488
Jiyong Park5d790c32019-11-15 18:40:32 +0900489 properties apexBundleProperties
490 targetProperties apexTargetBundleProperties
491 vndkProperties apexVndkProperties
492 overridableProperties overridableProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900493
Colin Crossa4925902018-11-16 11:36:28 -0800494 bundleModuleFile android.WritablePath
Sundong Ahnabb64432019-10-22 13:58:29 +0900495 outputFile android.WritablePath
Colin Cross70dda7e2019-10-01 22:05:35 -0700496 installDir android.InstallPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900497
Jiyong Park03b68dd2019-07-26 23:20:40 +0900498 prebuiltFileToDelete string
499
Jiyong Park42cca6c2019-04-01 11:15:50 +0900500 public_key_file android.Path
501 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900502
503 container_certificate_file android.Path
504 container_private_key_file android.Path
505
Jooyung Han54aca7b2019-11-20 02:26:02 +0900506 fileContexts android.Path
507
Jiyong Park8fd61922018-11-08 02:50:25 +0900508 // list of files to be included in this apex
509 filesInfo []apexFile
510
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900511 // list of module names that this APEX is depending on
512 externalDeps []string
513
Sundong Ahnabb64432019-10-22 13:58:29 +0900514 testApex bool
515 vndkApex bool
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000516 artApex bool
Sundong Ahnabb64432019-10-22 13:58:29 +0900517 primaryApexType bool
Jooyung Hane1633032019-08-01 17:41:43 +0900518
Jooyung Han214bf372019-11-12 13:03:50 +0900519 manifestJsonOut android.WritablePath
520 manifestPbOut android.WritablePath
Jooyung Han72bd2f82019-10-23 16:46:38 +0900521
522 // list of commands to create symlinks for backward compatibility
523 // these commands will be attached as LOCAL_POST_INSTALL_CMD to
524 // apex package itself(for unflattened build) or apex_manifest.json(for flattened build)
525 // so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
526 compatSymlinks []string
Sundong Ahnabb64432019-10-22 13:58:29 +0900527
528 // Suffix of module name in Android.mk
529 // ".flattened", ".apex", ".zipapex", or ""
530 suffix string
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900531}
532
Jiyong Park397e55e2018-10-24 21:09:55 +0900533func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Roland Levillain630846d2019-06-26 12:48:34 +0100534 native_shared_libs []string, binaries []string, tests []string,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700535 target android.Target, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900536 // Use *FarVariation* to be able to depend on modules having
537 // conflicting variations with this module. This is required since
538 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
539 // for native shared libs.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700540 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkda6eb592018-12-19 17:12:36 +0900541 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900542 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900543 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700544 }...), sharedLibTag, native_shared_libs...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900545
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700546 ctx.AddFarVariationDependencies(append(target.Variations(),
547 blueprint.Variation{Mutator: "image", Variation: imageVariation}),
548 executableTag, binaries...)
Roland Levillain630846d2019-06-26 12:48:34 +0100549
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700550 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100551 {Mutator: "image", Variation: imageVariation},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100552 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700553 }...), testTag, tests...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900554}
555
Alex Light9670d332019-01-29 18:07:33 -0800556func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
557 if ctx.Os().Class == android.Device {
558 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
559 } else {
560 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
561 if ctx.Os().Bionic() {
562 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
563 } else {
564 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
565 }
566 }
567}
568
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900569func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Handc782442019-11-01 03:14:38 +0900570 if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorWhitelist(ctx.Config())) {
571 ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
572 }
573
Jiyong Park397e55e2018-10-24 21:09:55 +0900574 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900575 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800576
577 a.combineProperties(ctx)
578
Jiyong Park397e55e2018-10-24 21:09:55 +0900579 has32BitTarget := false
580 for _, target := range targets {
581 if target.Arch.ArchType.Multilib == "lib32" {
582 has32BitTarget = true
583 }
584 }
585 for i, target := range targets {
586 // When multilib.* is omitted for native_shared_libs, it implies
587 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700588 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Park7c1dc612019-01-05 11:15:24 +0900589 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900590 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700591 }...), sharedLibTag, a.properties.Native_shared_libs...)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900592
Roland Levillain630846d2019-06-26 12:48:34 +0100593 // When multilib.* is omitted for tests, it implies
594 // multilib.both.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700595 ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Roland Levillain630846d2019-06-26 12:48:34 +0100596 {Mutator: "image", Variation: a.getImageVariation(config)},
Roland Levillain9b5fde92019-06-28 15:41:19 +0100597 {Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700598 }...), testTag, a.properties.Tests...)
Roland Levillain630846d2019-06-26 12:48:34 +0100599
Jiyong Park397e55e2018-10-24 21:09:55 +0900600 // Add native modules targetting both ABIs
601 addDependenciesForNativeModules(ctx,
602 a.properties.Multilib.Both.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100603 a.properties.Multilib.Both.Binaries,
604 a.properties.Multilib.Both.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700605 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900606 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900607
Alex Light3d673592019-01-18 14:37:31 -0800608 isPrimaryAbi := i == 0
609 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900610 // When multilib.* is omitted for binaries, it implies
611 // multilib.first.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700612 ctx.AddFarVariationDependencies(append(target.Variations(),
613 blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
614 executableTag, a.properties.Binaries...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900615
616 // Add native modules targetting the first ABI
617 addDependenciesForNativeModules(ctx,
618 a.properties.Multilib.First.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100619 a.properties.Multilib.First.Binaries,
620 a.properties.Multilib.First.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700621 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900622 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900623 }
624
625 switch target.Arch.ArchType.Multilib {
626 case "lib32":
627 // Add native modules targetting 32-bit ABI
628 addDependenciesForNativeModules(ctx,
629 a.properties.Multilib.Lib32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100630 a.properties.Multilib.Lib32.Binaries,
631 a.properties.Multilib.Lib32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700632 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900633 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900634
635 addDependenciesForNativeModules(ctx,
636 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100637 a.properties.Multilib.Prefer32.Binaries,
638 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700639 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900640 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900641 case "lib64":
642 // Add native modules targetting 64-bit ABI
643 addDependenciesForNativeModules(ctx,
644 a.properties.Multilib.Lib64.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100645 a.properties.Multilib.Lib64.Binaries,
646 a.properties.Multilib.Lib64.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700647 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900648 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900649
650 if !has32BitTarget {
651 addDependenciesForNativeModules(ctx,
652 a.properties.Multilib.Prefer32.Native_shared_libs,
Roland Levillain630846d2019-06-26 12:48:34 +0100653 a.properties.Multilib.Prefer32.Binaries,
654 a.properties.Multilib.Prefer32.Tests,
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700655 target,
Jiyong Park7c1dc612019-01-05 11:15:24 +0900656 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900657 }
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700658
659 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
660 for _, sanitizer := range ctx.Config().SanitizeDevice() {
661 if sanitizer == "hwaddress" {
662 addDependenciesForNativeModules(ctx,
663 []string{"libclang_rt.hwasan-aarch64-android"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700664 nil, nil, target, a.getImageVariation(config))
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700665 break
666 }
667 }
668 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900669 }
670
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900671 }
672
Jiyong Parkce6aadc2019-11-20 13:58:28 +0900673 // For prebuilt_etc, use the first variant (64 on 64/32bit device,
674 // 32 on 32bit device) regardless of the TARGET_PREFER_* setting.
675 // b/144532908
676 archForPrebuiltEtc := config.Arches()[0]
677 for _, arch := range config.Arches() {
678 // Prefer 64-bit arch if there is any
679 if arch.ArchType.Multilib == "lib64" {
680 archForPrebuiltEtc = arch
681 break
682 }
683 }
684 ctx.AddFarVariationDependencies([]blueprint.Variation{
685 {Mutator: "os", Variation: ctx.Os().String()},
686 {Mutator: "arch", Variation: archForPrebuiltEtc.String()},
687 }, prebuiltTag, a.properties.Prebuilts...)
688
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700689 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
690 javaLibTag, a.properties.Java_libs...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900691
Jiyong Park23c52b02019-02-02 13:13:47 +0900692 if String(a.properties.Key) == "" {
693 ctx.ModuleErrorf("key is missing")
694 return
695 }
696 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900697
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900698 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900699 if cert != "" {
700 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900701 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900702
703 // TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
704 if len(a.properties.Uses_sdks) > 0 {
705 sdkRefs := []android.SdkRef{}
706 for _, str := range a.properties.Uses_sdks {
707 parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
708 sdkRefs = append(sdkRefs, parsed)
709 }
710 a.BuildWithSdks(sdkRefs)
711 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900712}
713
Jiyong Park5d790c32019-11-15 18:40:32 +0900714func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
715 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
716 androidAppTag, a.overridableProperties.Apps...)
717}
718
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900719func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
720 // direct deps of an APEX bundle are all part of the APEX bundle
721 return true
722}
723
Colin Cross0ea8ba82019-06-06 14:33:29 -0700724func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900725 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
726 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000727 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900728 }
729 return String(a.properties.Certificate)
730}
731
Colin Cross41955e82019-05-29 14:40:35 -0700732func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
733 switch tag {
734 case "":
Sundong Ahnabb64432019-10-22 13:58:29 +0900735 return android.Paths{a.outputFile}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700736 default:
737 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Jiyong Park5a832022018-12-20 09:54:35 +0900738 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900739}
740
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900741func (a *apexBundle) installable() bool {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900742 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900743}
744
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000745func (a *apexBundle) testOnlyShouldSkipHashtreeGeneration() bool {
746 return proptools.Bool(a.properties.Test_only_no_hashtree)
747}
748
Jiyong Park7c1dc612019-01-05 11:15:24 +0900749func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
Jooyung Han31c470b2019-10-18 16:26:59 +0900750 if a.vndkApex {
Colin Cross7228ecd2019-11-18 16:00:16 -0800751 return cc.VendorVariationPrefix + a.vndkVersion(config)
Jooyung Han31c470b2019-10-18 16:26:59 +0900752 }
Jiyong Park7c1dc612019-01-05 11:15:24 +0900753 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800754 return cc.VendorVariationPrefix + config.PlatformVndkVersion()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900755 } else {
Colin Cross7228ecd2019-11-18 16:00:16 -0800756 return android.CoreVariation
Jiyong Parkda6eb592018-12-19 17:12:36 +0900757 }
758}
759
Jiyong Parkf97782b2019-02-13 20:28:58 +0900760func (a *apexBundle) EnableSanitizer(sanitizerName string) {
761 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
762 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
763 }
764}
765
Jiyong Park388ef3f2019-01-28 19:47:32 +0900766func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900767 if android.InList(sanitizerName, a.properties.SanitizerNames) {
768 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900769 }
770
771 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900772 globalSanitizerNames := []string{}
773 if a.Host() {
774 globalSanitizerNames = ctx.Config().SanitizeHost()
775 } else {
776 arches := ctx.Config().SanitizeDeviceArch()
777 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
778 globalSanitizerNames = ctx.Config().SanitizeDevice()
779 }
780 }
781 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900782}
783
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900784func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
785 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
786}
787
788func (a *apexBundle) PreventInstall() {
789 a.properties.PreventInstall = true
790}
791
792func (a *apexBundle) HideFromMake() {
793 a.properties.HideFromMake = true
794}
795
Jiyong Parkf653b052019-11-18 15:39:01 +0900796// TODO(jiyong) move apexFileFor* close to the apexFile type definition
Jiyong Park1833cef2019-12-13 13:28:36 +0900797func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900798 // Decide the APEX-local directory by the multilib of the library
799 // In the future, we may query this to the module.
Jiyong Parkf653b052019-11-18 15:39:01 +0900800 var dirInApex string
Martin Stjernholm279de572019-09-10 23:18:20 +0100801 switch ccMod.Arch().ArchType.Multilib {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900802 case "lib32":
803 dirInApex = "lib"
804 case "lib64":
805 dirInApex = "lib64"
806 }
Martin Stjernholm279de572019-09-10 23:18:20 +0100807 dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700808 if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
Martin Stjernholm279de572019-09-10 23:18:20 +0100809 dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900810 }
Jiyong Park1833cef2019-12-13 13:28:36 +0900811 if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
Martin Stjernholm279de572019-09-10 23:18:20 +0100812 // Special case for Bionic libs and other libs installed with them. This is
813 // to prevent those libs from being included in the search path
814 // /apex/com.android.runtime/${LIB}. This exclusion is required because
815 // those libs in the Runtime APEX are available via the legacy paths in
816 // /system/lib/. By the init process, the libs in the APEX are bind-mounted
817 // to the legacy paths and thus will be loaded into the default linker
818 // namespace (aka "platform" namespace). If the libs are directly in
819 // /apex/com.android.runtime/${LIB} then the same libs will be loaded again
820 // into the runtime linker namespace, which will result in double loading of
821 // them, which isn't supported.
822 dirInApex = filepath.Join(dirInApex, "bionic")
Jiyong Parkb0788572018-12-20 22:10:17 +0900823 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900824
Jiyong Parkf653b052019-11-18 15:39:01 +0900825 fileToCopy := ccMod.OutputFile().Path()
Jiyong Park1833cef2019-12-13 13:28:36 +0900826 return newApexFile(ctx, fileToCopy, ccMod.Name(), dirInApex, nativeSharedLib, ccMod)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900827}
828
Jiyong Park1833cef2019-12-13 13:28:36 +0900829func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900830 dirInApex := filepath.Join("bin", cc.RelativeInstallPath())
Colin Cross3b19f5d2019-09-17 14:45:31 -0700831 if cc.Target().NativeBridge == android.NativeBridgeEnabled {
dimitry8d6dde82019-07-11 10:23:53 +0200832 dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
Jiyong Parkacbf6c72019-07-09 16:19:16 +0900833 }
Jiyong Parkf653b052019-11-18 15:39:01 +0900834 fileToCopy := cc.OutputFile().Path()
Jiyong Park1833cef2019-12-13 13:28:36 +0900835 af := newApexFile(ctx, fileToCopy, cc.Name(), dirInApex, nativeExecutable, cc)
Jiyong Parkf653b052019-11-18 15:39:01 +0900836 af.symlinks = cc.Symlinks()
837 return af
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900838}
839
Jiyong Park1833cef2019-12-13 13:28:36 +0900840func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.Module) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900841 dirInApex := "bin"
842 fileToCopy := py.HostToolPath().Path()
Jiyong Park1833cef2019-12-13 13:28:36 +0900843 return newApexFile(ctx, fileToCopy, py.Name(), dirInApex, pyBinary, py)
Alex Light778127a2019-02-27 14:19:50 -0800844}
Jiyong Park1833cef2019-12-13 13:28:36 +0900845func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900846 dirInApex := "bin"
Alex Light778127a2019-02-27 14:19:50 -0800847 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
848 if err != nil {
849 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
Jiyong Parkf653b052019-11-18 15:39:01 +0900850 return apexFile{}
Alex Light778127a2019-02-27 14:19:50 -0800851 }
Jiyong Parkf653b052019-11-18 15:39:01 +0900852 fileToCopy := android.PathForOutput(ctx, s)
853 // NB: Since go binaries are static we don't need the module for anything here, which is
854 // good since the go tool is a blueprint.Module not an android.Module like we would
855 // normally use.
Jiyong Park1833cef2019-12-13 13:28:36 +0900856 return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
Alex Light778127a2019-02-27 14:19:50 -0800857}
858
Jiyong Park1833cef2019-12-13 13:28:36 +0900859func apexFileForShBinary(ctx android.BaseModuleContext, sh *android.ShBinary) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900860 dirInApex := filepath.Join("bin", sh.SubDir())
861 fileToCopy := sh.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +0900862 af := newApexFile(ctx, fileToCopy, sh.Name(), dirInApex, shBinary, sh)
Jiyong Parkf653b052019-11-18 15:39:01 +0900863 af.symlinks = sh.Symlinks()
864 return af
Jiyong Park04480cf2019-02-06 00:16:29 +0900865}
866
Jiyong Park1833cef2019-12-13 13:28:36 +0900867func apexFileForJavaLibrary(ctx android.BaseModuleContext, java *java.Library) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900868 dirInApex := "javalib"
869 fileToCopy := java.DexJarFile()
Jiyong Park1833cef2019-12-13 13:28:36 +0900870 return newApexFile(ctx, fileToCopy, java.Name(), dirInApex, javaSharedLib, java)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900871}
872
Jiyong Park1833cef2019-12-13 13:28:36 +0900873func apexFileForPrebuiltJavaLibrary(ctx android.BaseModuleContext, java *java.Import) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900874 dirInApex := "javalib"
Jiyong Park9e6c2422019-08-09 20:39:45 +0900875 // The output is only one, but for some reason, ImplementationJars returns Paths, not Path
876 implJars := java.ImplementationJars()
877 if len(implJars) != 1 {
878 panic(fmt.Errorf("java.ImplementationJars() must return single Path, but got: %s",
879 strings.Join(implJars.Strings(), ", ")))
880 }
Jiyong Parkf653b052019-11-18 15:39:01 +0900881 fileToCopy := implJars[0]
Jiyong Park1833cef2019-12-13 13:28:36 +0900882 return newApexFile(ctx, fileToCopy, java.Name(), dirInApex, javaSharedLib, java)
Jiyong Park9e6c2422019-08-09 20:39:45 +0900883}
884
Jiyong Park1833cef2019-12-13 13:28:36 +0900885func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt android.PrebuiltEtcModule, depName string) apexFile {
Jiyong Parkf653b052019-11-18 15:39:01 +0900886 dirInApex := filepath.Join("etc", prebuilt.SubDir())
887 fileToCopy := prebuilt.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +0900888 return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900889}
890
Jiyong Park1833cef2019-12-13 13:28:36 +0900891func apexFileForAndroidApp(ctx android.BaseModuleContext, aapp interface {
Jiyong Parkf653b052019-11-18 15:39:01 +0900892 android.Module
893 Privileged() bool
894 OutputFile() android.Path
895}, pkgName string) apexFile {
Jiyong Parkf7487312019-10-17 12:54:30 +0900896 appDir := "app"
Jiyong Parkf653b052019-11-18 15:39:01 +0900897 if aapp.Privileged() {
Jiyong Parkf7487312019-10-17 12:54:30 +0900898 appDir = "priv-app"
899 }
Jiyong Parkf653b052019-11-18 15:39:01 +0900900 dirInApex := filepath.Join(appDir, pkgName)
901 fileToCopy := aapp.OutputFile()
Jiyong Park1833cef2019-12-13 13:28:36 +0900902 return newApexFile(ctx, fileToCopy, aapp.Name(), dirInApex, app, aapp)
Dario Frenicde2a032019-10-27 00:29:22 +0100903}
904
Roland Levillain935639d2019-08-13 14:55:28 +0100905// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
906type flattenedApexContext struct {
907 android.ModuleContext
908}
909
910func (c *flattenedApexContext) InstallBypassMake() bool {
911 return true
912}
913
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900914func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sundong Ahnabb64432019-10-22 13:58:29 +0900915 buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
916 switch a.properties.ApexType {
917 case imageApex:
918 if buildFlattenedAsDefault {
919 a.suffix = imageApexSuffix
920 } else {
921 a.suffix = ""
922 a.primaryApexType = true
Jooyung Han3ab2c3e2019-12-05 16:27:44 +0900923
924 if ctx.Config().InstallExtraFlattenedApexes() {
925 a.externalDeps = append(a.externalDeps, a.Name()+flattenedSuffix)
926 }
Sundong Ahnabb64432019-10-22 13:58:29 +0900927 }
928 case zipApex:
929 if proptools.String(a.properties.Payload_type) == "zip" {
930 a.suffix = ""
931 a.primaryApexType = true
932 } else {
933 a.suffix = zipApexSuffix
934 }
935 case flattenedApex:
936 if buildFlattenedAsDefault {
937 a.suffix = ""
938 a.primaryApexType = true
939 } else {
940 a.suffix = flattenedSuffix
941 }
Alex Light5098a612018-11-29 17:12:15 -0800942 }
943
Roland Levillain630846d2019-06-26 12:48:34 +0100944 if len(a.properties.Tests) > 0 && !a.testApex {
945 ctx.PropertyErrorf("tests", "property not allowed in apex module type")
946 return
947 }
948
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800949 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
950
Jooyung Hane1633032019-08-01 17:41:43 +0900951 // native lib dependencies
952 var provideNativeLibs []string
953 var requireNativeLibs []string
954
Jooyung Han5c998b92019-06-27 11:30:33 +0900955 // Check if "uses" requirements are met with dependent apexBundles
956 var providedNativeSharedLibs []string
957 useVendor := proptools.Bool(a.properties.Use_vendor)
958 ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
959 if ctx.OtherModuleDependencyTag(m) != usesTag {
960 return
961 }
962 otherName := ctx.OtherModuleName(m)
963 other, ok := m.(*apexBundle)
964 if !ok {
965 ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
966 return
967 }
968 if proptools.Bool(other.properties.Use_vendor) != useVendor {
969 ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
970 return
971 }
972 if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
973 ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
974 return
975 }
976 providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
977 })
978
Jiyong Parkf653b052019-11-18 15:39:01 +0900979 var filesInfo []apexFile
Alex Light778127a2019-02-27 14:19:50 -0800980 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Roland Levillainf89cd092019-07-29 16:22:59 +0100981 depTag := ctx.OtherModuleDependencyTag(child)
982 depName := ctx.OtherModuleName(child)
Jiyong Parkf653b052019-11-18 15:39:01 +0900983 if _, isDirectDep := parent.(*apexBundle); isDirectDep {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900984 switch depTag {
985 case sharedLibTag:
986 if cc, ok := child.(*cc.Module); ok {
Jooyung Hane1633032019-08-01 17:41:43 +0900987 if cc.HasStubsVariants() {
988 provideNativeLibs = append(provideNativeLibs, cc.OutputFile().Path().Base())
989 }
Jiyong Park1833cef2019-12-13 13:28:36 +0900990 filesInfo = append(filesInfo, apexFileForNativeLibrary(ctx, cc, handleSpecialLibs))
Jiyong Parkf653b052019-11-18 15:39:01 +0900991 return true // track transitive dependencies
Jiyong Parkff1458f2018-10-12 21:49:38 +0900992 } else {
993 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900994 }
995 case executableTag:
996 if cc, ok := child.(*cc.Module); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +0900997 filesInfo = append(filesInfo, apexFileForExecutable(ctx, cc))
Jiyong Parkf653b052019-11-18 15:39:01 +0900998 return true // track transitive dependencies
Jiyong Park04480cf2019-02-06 00:16:29 +0900999 } else if sh, ok := child.(*android.ShBinary); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001000 filesInfo = append(filesInfo, apexFileForShBinary(ctx, sh))
Alex Light778127a2019-02-27 14:19:50 -08001001 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
Jiyong Park1833cef2019-12-13 13:28:36 +09001002 filesInfo = append(filesInfo, apexFileForPyBinary(ctx, py))
Alex Light778127a2019-02-27 14:19:50 -08001003 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
Jiyong Parkf653b052019-11-18 15:39:01 +09001004 filesInfo = append(filesInfo, apexFileForGoBinary(ctx, depName, gb))
Jiyong Parkff1458f2018-10-12 21:49:38 +09001005 } else {
Alex Light778127a2019-02-27 14:19:50 -08001006 ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001007 }
1008 case javaLibTag:
Jiyong Park9e6c2422019-08-09 20:39:45 +09001009 if javaLib, ok := child.(*java.Library); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001010 af := apexFileForJavaLibrary(ctx, javaLib)
Jiyong Parkf653b052019-11-18 15:39:01 +09001011 if !af.Ok() {
Jiyong Park8fd61922018-11-08 02:50:25 +09001012 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
1013 } else {
Jiyong Parkf653b052019-11-18 15:39:01 +09001014 filesInfo = append(filesInfo, af)
1015 return true // track transitive dependencies
Jiyong Park9e6c2422019-08-09 20:39:45 +09001016 }
Jiyong Park9e6c2422019-08-09 20:39:45 +09001017 } else if javaLib, ok := child.(*java.Import); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001018 af := apexFileForPrebuiltJavaLibrary(ctx, javaLib)
Jiyong Parkf653b052019-11-18 15:39:01 +09001019 if !af.Ok() {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001020 ctx.PropertyErrorf("java_libs", "%q does not have a jar output", depName)
1021 } else {
Jiyong Parkf653b052019-11-18 15:39:01 +09001022 filesInfo = append(filesInfo, af)
Jiyong Park8fd61922018-11-08 02:50:25 +09001023 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001024 } else {
Jiyong Park9e6c2422019-08-09 20:39:45 +09001025 ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001026 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001027 case androidAppTag:
1028 pkgName := ctx.DeviceConfig().OverridePackageNameFor(depName)
1029 if ap, ok := child.(*java.AndroidApp); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001030 filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap, pkgName))
Jiyong Parkf653b052019-11-18 15:39:01 +09001031 return true // track transitive dependencies
1032 } else if ap, ok := child.(*java.AndroidAppImport); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001033 filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap, pkgName))
Jiyong Parkf653b052019-11-18 15:39:01 +09001034 } else {
1035 ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
1036 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001037 case prebuiltTag:
Jooyung Han39edb6c2019-11-06 16:53:07 +09001038 if prebuilt, ok := child.(android.PrebuiltEtcModule); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001039 filesInfo = append(filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
Jiyong Parkff1458f2018-10-12 21:49:38 +09001040 } else {
1041 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
1042 }
Roland Levillain630846d2019-06-26 12:48:34 +01001043 case testTag:
Roland Levillainf89cd092019-07-29 16:22:59 +01001044 if ccTest, ok := child.(*cc.Module); ok {
1045 if ccTest.IsTestPerSrcAllTestsVariation() {
1046 // Multiple-output test module (where `test_per_src: true`).
1047 //
1048 // `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
1049 // We do not add this variation to `filesInfo`, as it has no output;
1050 // however, we do add the other variations of this module as indirect
1051 // dependencies (see below).
1052 return true
Roland Levillain9b5fde92019-06-28 15:41:19 +01001053 } else {
Roland Levillainf89cd092019-07-29 16:22:59 +01001054 // Single-output test module (where `test_per_src: false`).
Jiyong Park1833cef2019-12-13 13:28:36 +09001055 af := apexFileForExecutable(ctx, ccTest)
Jiyong Parkf653b052019-11-18 15:39:01 +09001056 af.class = nativeTest
1057 filesInfo = append(filesInfo, af)
Roland Levillain9b5fde92019-06-28 15:41:19 +01001058 }
Roland Levillain630846d2019-06-26 12:48:34 +01001059 } else {
1060 ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
1061 }
Jiyong Parkff1458f2018-10-12 21:49:38 +09001062 case keyTag:
1063 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001064 a.private_key_file = key.private_key_file
1065 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +09001066 } else {
1067 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001068 }
Jiyong Parkf653b052019-11-18 15:39:01 +09001069 return false
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001070 case certificateTag:
1071 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001072 a.container_certificate_file = dep.Certificate.Pem
1073 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001074 } else {
1075 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
1076 }
Jiyong Park03b68dd2019-07-26 23:20:40 +09001077 case android.PrebuiltDepTag:
1078 // If the prebuilt is force disabled, remember to delete the prebuilt file
1079 // that might have been installed in the previous builds
1080 if prebuilt, ok := child.(*Prebuilt); ok && prebuilt.isForceDisabled() {
1081 a.prebuiltFileToDelete = prebuilt.InstallFilename()
1082 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001083 }
Jooyung Han8aee2042019-10-29 05:08:31 +09001084 } else if !a.vndkApex {
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001085 // indirect dependencies
Jooyung Han9c80bae2019-08-20 17:30:57 +09001086 if am, ok := child.(android.ApexModule); ok {
Roland Levillainf89cd092019-07-29 16:22:59 +01001087 // We cannot use a switch statement on `depTag` here as the checked
1088 // tags used below are private (e.g. `cc.sharedDepTag`).
Jiyong Park52cd06f2019-11-11 10:14:32 +09001089 if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001090 if cc, ok := child.(*cc.Module); ok {
1091 if android.InList(cc.Name(), providedNativeSharedLibs) {
1092 // If we're using a shared library which is provided from other APEX,
1093 // don't include it in this APEX
1094 return false
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001095 }
Jooyung Han671f1ce2019-12-17 12:47:13 +09001096 if !a.Host() && !android.DirectlyInApex(ctx.ModuleName(), ctx.OtherModuleName(cc)) && (cc.IsStubs() || cc.HasStubsVariants()) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001097 // If the dependency is a stubs lib, don't include it in this APEX,
1098 // but make sure that the lib is installed on the device.
1099 // In case no APEX is having the lib, the lib is installed to the system
1100 // partition.
1101 //
1102 // Always include if we are a host-apex however since those won't have any
1103 // system libraries.
1104 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
1105 a.externalDeps = append(a.externalDeps, cc.Name())
1106 }
Jooyung Hane1633032019-08-01 17:41:43 +09001107 requireNativeLibs = append(requireNativeLibs, cc.OutputFile().Path().Base())
Roland Levillainf89cd092019-07-29 16:22:59 +01001108 // Don't track further
1109 return false
1110 }
Jiyong Park1833cef2019-12-13 13:28:36 +09001111 af := apexFileForNativeLibrary(ctx, cc, handleSpecialLibs)
Jiyong Parkf653b052019-11-18 15:39:01 +09001112 af.transitiveDep = true
1113 filesInfo = append(filesInfo, af)
1114 return true // track transitive dependencies
Jiyong Park25fc6a92018-11-18 18:02:45 +09001115 }
Roland Levillainf89cd092019-07-29 16:22:59 +01001116 } else if cc.IsTestPerSrcDepTag(depTag) {
1117 if cc, ok := child.(*cc.Module); ok {
Jiyong Park1833cef2019-12-13 13:28:36 +09001118 af := apexFileForExecutable(ctx, cc)
Roland Levillainf89cd092019-07-29 16:22:59 +01001119 // Handle modules created as `test_per_src` variations of a single test module:
1120 // use the name of the generated test binary (`fileToCopy`) instead of the name
1121 // of the original test module (`depName`, shared by all `test_per_src`
1122 // variations of that module).
Jiyong Parkf653b052019-11-18 15:39:01 +09001123 af.moduleName = filepath.Base(af.builtFile.String())
1124 af.transitiveDep = true
1125 filesInfo = append(filesInfo, af)
1126 return true // track transitive dependencies
Roland Levillainf89cd092019-07-29 16:22:59 +01001127 }
Jiyong Park52cd06f2019-11-11 10:14:32 +09001128 } else if java.IsJniDepTag(depTag) {
1129 // Do nothing for JNI dep. JNI libraries are always embedded in APK-in-APEX.
Jiyong Parkf653b052019-11-18 15:39:01 +09001130 return true
Jooyung Han9c80bae2019-08-20 17:30:57 +09001131 } else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
Roland Levillainf89cd092019-07-29 16:22:59 +01001132 ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001133 }
1134 }
1135 }
1136 return false
1137 })
1138
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001139 // Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
1140 // Build rules are generated by the dexpreopt singleton, and here we access build artifacts
1141 // via the global boot image config.
1142 if a.artApex {
1143 for arch, files := range java.DexpreoptedArtApexJars(ctx) {
1144 dirInApex := filepath.Join("javalib", arch.String())
1145 for _, f := range files {
1146 localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
Jiyong Park1833cef2019-12-13 13:28:36 +09001147 af := newApexFile(ctx, f, localModule, dirInApex, etc, nil)
Jiyong Parkf653b052019-11-18 15:39:01 +09001148 filesInfo = append(filesInfo, af)
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001149 }
1150 }
1151 }
1152
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001153 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +09001154 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
1155 return
1156 }
1157
Jiyong Park8fd61922018-11-08 02:50:25 +09001158 // remove duplicates in filesInfo
1159 removeDup := func(filesInfo []apexFile) []apexFile {
Jooyung Han344d5432019-08-23 11:17:39 +09001160 encountered := make(map[string]bool)
Jiyong Park8fd61922018-11-08 02:50:25 +09001161 result := []apexFile{}
1162 for _, f := range filesInfo {
Jooyung Han344d5432019-08-23 11:17:39 +09001163 dest := filepath.Join(f.installDir, f.builtFile.Base())
1164 if !encountered[dest] {
1165 encountered[dest] = true
Jiyong Park8fd61922018-11-08 02:50:25 +09001166 result = append(result, f)
1167 }
1168 }
1169 return result
1170 }
1171 filesInfo = removeDup(filesInfo)
1172
1173 // to have consistent build rules
1174 sort.Slice(filesInfo, func(i, j int) bool {
1175 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
1176 })
1177
Jiyong Park127b40b2019-09-30 16:04:35 +09001178 // check apex_available requirements
Jiyong Park3814f4d2019-12-02 13:08:53 +09001179 if !ctx.Host() && !a.testApex {
Jiyong Park583a2262019-10-08 20:55:38 +09001180 for _, fi := range filesInfo {
1181 if am, ok := fi.module.(android.ApexModule); ok {
1182 if !am.AvailableFor(ctx.ModuleName()) {
1183 ctx.ModuleErrorf("requires %q that is not available for the APEX", fi.module.Name())
Jiyong Park3814f4d2019-12-02 13:08:53 +09001184 // don't stop so that we can report other violations in the same run
Jiyong Park583a2262019-10-08 20:55:38 +09001185 }
Jiyong Park127b40b2019-09-30 16:04:35 +09001186 }
1187 }
1188 }
1189
Jiyong Park8fd61922018-11-08 02:50:25 +09001190 // prepend the name of this APEX to the module names. These names will be the names of
1191 // modules that will be defined if the APEX is flattened.
1192 for i := range filesInfo {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08001193 filesInfo[i].moduleName = filesInfo[i].moduleName + "." + a.Name() + a.suffix
Jiyong Park8fd61922018-11-08 02:50:25 +09001194 }
1195
Jiyong Park8fd61922018-11-08 02:50:25 +09001196 a.installDir = android.PathForModuleInstall(ctx, "apex")
1197 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -08001198
Jooyung Han54aca7b2019-11-20 02:26:02 +09001199 if a.properties.ApexType != zipApex {
1200 if a.properties.File_contexts == nil {
1201 a.fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
1202 } else {
1203 a.fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
1204 if a.Platform() {
1205 if matched, err := path.Match("system/sepolicy/**/*", a.fileContexts.String()); err != nil || !matched {
1206 ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but %q", a.fileContexts)
1207 }
1208 }
1209 }
1210 if !android.ExistentPathForSource(ctx, a.fileContexts.String()).Valid() {
1211 ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", a.fileContexts)
1212 return
1213 }
1214 }
1215
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001216 // prepare apex_manifest.json
Jooyung Han01a3ee22019-11-02 02:52:25 +09001217 a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
1218
1219 a.setCertificateAndPrivateKey(ctx)
1220 if a.properties.ApexType == flattenedApex {
1221 a.buildFlattenedApex(ctx)
1222 } else {
1223 a.buildUnflattenedApex(ctx)
1224 }
1225
Jaewoong Jung1670ca02019-11-22 14:50:42 -08001226 apexName := proptools.StringDefault(a.properties.Apex_name, a.Name())
Jooyung Han01a3ee22019-11-02 02:52:25 +09001227 a.compatSymlinks = makeCompatSymlinks(apexName, ctx)
1228}
1229
Jooyung Han344d5432019-08-23 11:17:39 +09001230func newApexBundle() *apexBundle {
Sundong Ahnabb64432019-10-22 13:58:29 +09001231 module := &apexBundle{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001232 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001233 module.AddProperties(&module.targetProperties)
Jiyong Park5d790c32019-11-15 18:40:32 +09001234 module.AddProperties(&module.overridableProperties)
Alex Light5098a612018-11-29 17:12:15 -08001235 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001236 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1237 })
Alex Light5098a612018-11-29 17:12:15 -08001238 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001239 android.InitDefaultableModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001240 android.InitSdkAwareModule(module)
Jiyong Park5d790c32019-11-15 18:40:32 +09001241 android.InitOverridableModule(module, &module.properties.Overrides)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001242 return module
1243}
Jiyong Park30ca9372019-02-07 16:27:23 +09001244
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001245func ApexBundleFactory(testApex bool, artApex bool) android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001246 bundle := newApexBundle()
1247 bundle.testApex = testApex
Ulyana Trafimovichde534412019-11-08 10:51:01 +00001248 bundle.artApex = artApex
Jooyung Han344d5432019-08-23 11:17:39 +09001249 return bundle
1250}
1251
1252func testApexBundleFactory() android.Module {
1253 bundle := newApexBundle()
1254 bundle.testApex = true
1255 return bundle
1256}
1257
Jiyong Parkd1063c12019-07-17 20:08:41 +09001258func BundleFactory() android.Module {
Jooyung Han344d5432019-08-23 11:17:39 +09001259 return newApexBundle()
1260}
1261
Jiyong Park30ca9372019-02-07 16:27:23 +09001262//
1263// Defaults
1264//
1265type Defaults struct {
1266 android.ModuleBase
1267 android.DefaultsModuleBase
1268}
1269
Jiyong Park30ca9372019-02-07 16:27:23 +09001270func defaultsFactory() android.Module {
1271 return DefaultsFactory()
1272}
1273
1274func DefaultsFactory(props ...interface{}) android.Module {
1275 module := &Defaults{}
1276
1277 module.AddProperties(props...)
1278 module.AddProperties(
1279 &apexBundleProperties{},
1280 &apexTargetBundleProperties{},
1281 )
1282
1283 android.InitDefaultsModule(module)
1284 return module
1285}
Jiyong Park5d790c32019-11-15 18:40:32 +09001286
1287//
1288// OverrideApex
1289//
1290type OverrideApex struct {
1291 android.ModuleBase
1292 android.OverrideModuleBase
1293}
1294
1295func (o *OverrideApex) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1296 // All the overrides happen in the base module.
1297}
1298
1299// override_apex is used to create an apex module based on another apex module
1300// by overriding some of its properties.
1301func overrideApexFactory() android.Module {
1302 m := &OverrideApex{}
1303 m.AddProperties(&overridableProperties{})
1304
1305 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1306 android.InitOverrideModule(m)
1307 return m
1308}