blob: 1e1c33a7d6d73ed499430b85a45f522379f1ea65 [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"
19 "io"
20 "path/filepath"
21 "runtime"
Jiyong Parkab3ceb32018-10-10 14:05:29 +090022 "sort"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090023 "strings"
24
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
35var (
36 pctx = android.NewPackageContext("android/apex")
37
38 // Create a canned fs config file where all files and directories are
39 // by default set to (uid/gid/mode) = (1000/1000/0644)
40 // TODO(b/113082813) make this configurable using config.fs syntax
41 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000042 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000043 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090044 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090045 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090047 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090048
49 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
50 // against the binary policy using sefcontext_compiler -p <policy>.
51
52 // TODO(b/114327326): automate the generation of file_contexts
53 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
54 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
55 `(${copy_commands}) && ` +
56 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090057 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090058 `--file_contexts ${file_contexts} ` +
59 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080060 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090061 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090062 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
63 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
64 "${soong_zip}", "${zipalign}", "${aapt2}"},
65 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090066 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080067
Alex Light5098a612018-11-29 17:12:15 -080068 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
69 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
70 `(${copy_commands}) && ` +
71 `APEXER_TOOL_PATH=${tool_path} ` +
72 `${apexer} --force --manifest ${manifest} ` +
73 `--payload_type zip ` +
74 `${image_dir} ${out} `,
75 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
76 Description: "ZipAPEX ${image_dir} => ${out}",
77 }, "tool_path", "image_dir", "copy_commands", "manifest")
78
Colin Crossa4925902018-11-16 11:36:28 -080079 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
80 blueprint.RuleParams{
81 Command: `${aapt2} convert --output-format proto $in -o $out`,
82 CommandDeps: []string{"${aapt2}"},
83 })
84
85 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090086 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000087 `apex_payload.img:apex/${abi}.img ` +
88 `apex_manifest.json:root/apex_manifest.json ` +
Jaewoong Jung105e1662019-09-04 13:26:18 -070089 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
90 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
Colin Crossa4925902018-11-16 11:36:28 -080091 CommandDeps: []string{"${zip2zip}"},
92 Description: "app bundle",
93 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090094)
95
Alex Light5098a612018-11-29 17:12:15 -080096var imageApexSuffix = ".apex"
97var zipApexSuffix = ".zipapex"
98
99var imageApexType = "image"
100var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900101
102type dependencyTag struct {
103 blueprint.BaseDependencyTag
104 name string
105}
106
107var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900108 sharedLibTag = dependencyTag{name: "sharedLib"}
109 executableTag = dependencyTag{name: "executable"}
110 javaLibTag = dependencyTag{name: "javaLib"}
111 prebuiltTag = dependencyTag{name: "prebuilt"}
112 keyTag = dependencyTag{name: "key"}
113 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900114)
115
116func init() {
Colin Cross713ef2b2019-04-02 16:14:11 -0700117 pctx.Import("android/soong/android")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900118 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900119 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100120 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
121 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
122 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
123 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000124 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100125 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
126 } else {
127 return pctx.HostBinToolPath(ctx, tool).String()
128 }
129 })
130 }
131 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900132 pctx.HostBinToolVariable("avbtool", "avbtool")
133 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
134 pctx.HostBinToolVariable("merge_zips", "merge_zips")
135 pctx.HostBinToolVariable("mke2fs", "mke2fs")
136 pctx.HostBinToolVariable("resize2fs", "resize2fs")
137 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
138 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800139 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140 pctx.HostBinToolVariable("zipalign", "zipalign")
141
Alex Light0851b882019-02-07 13:20:53 -0800142 android.RegisterModuleType("apex", apexBundleFactory)
143 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900144 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700145 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900146
147 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
148 ctx.TopDown("apex_deps", apexDepsMutator)
149 ctx.BottomUp("apex", apexMutator)
150 })
151}
152
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900153// Mark the direct and transitive dependencies of apex bundles so that they
154// can be built for the apex bundles.
155func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800156 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800157 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900159 depName := mctx.OtherModuleName(child)
160 // If the parent is apexBundle, this child is directly depended.
161 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800162 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800163 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
164 // non-installable apex's cannot be installed and so should not prevent libraries from being
165 // installed to the system.
166 android.UpdateApexDependency(apexBundleName, depName, directDep)
167 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900168
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900169 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900170 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900171 return true
172 } else {
173 return false
174 }
175 })
176 }
177}
178
179// Create apex variations if a module is included in APEX(s).
180func apexMutator(mctx android.BottomUpMutatorContext) {
181 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900182 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 } else if _, ok := mctx.Module().(*apexBundle); ok {
184 // apex bundle itself is mutated so that it and its modules have same
185 // apex variant.
186 apexBundleName := mctx.ModuleName()
187 mctx.CreateVariations(apexBundleName)
188 }
189}
190
Alex Light9670d332019-01-29 18:07:33 -0800191type apexNativeDependencies struct {
192 // List of native libraries
193 Native_shared_libs []string
194 // List of native executables
195 Binaries []string
196}
197type apexMultilibProperties struct {
198 // Native dependencies whose compile_multilib is "first"
199 First apexNativeDependencies
200
201 // Native dependencies whose compile_multilib is "both"
202 Both apexNativeDependencies
203
204 // Native dependencies whose compile_multilib is "prefer32"
205 Prefer32 apexNativeDependencies
206
207 // Native dependencies whose compile_multilib is "32"
208 Lib32 apexNativeDependencies
209
210 // Native dependencies whose compile_multilib is "64"
211 Lib64 apexNativeDependencies
212}
213
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900214type apexBundleProperties struct {
215 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000216 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800217 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900218
Jiyong Park40e26a22019-02-08 02:53:06 +0900219 // AndroidManifest.xml file used for the zip container of this APEX bundle.
220 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800221 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900222
Jiyong Park05e70dd2019-03-18 14:26:32 +0900223 // Canonical name of the APEX bundle in the manifest file.
224 // If unspecified, defaults to the value of name
225 Apex_name *string
226
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900227 // Determines the file contexts file for setting security context to each file in this APEX bundle.
228 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
229 // used.
230 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900231 File_contexts *string
232
233 // List of native shared libs that are embedded inside this APEX bundle
234 Native_shared_libs []string
235
236 // List of native executables that are embedded inside this APEX bundle
237 Binaries []string
238
239 // List of java libraries that are embedded inside this APEX bundle
240 Java_libs []string
241
242 // List of prebuilt files that are embedded inside this APEX bundle
243 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900244
245 // Name of the apex_key module that provides the private key to sign APEX
246 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900247
Alex Light5098a612018-11-29 17:12:15 -0800248 // The type of APEX to build. Controls what the APEX payload is. Either
249 // 'image', 'zip' or 'both'. Default: 'image'.
250 Payload_type *string
251
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900252 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
253 // or an android_app_certificate module name in the form ":module".
254 Certificate *string
255
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900256 // Whether this APEX is installable to one of the partitions. Default: true.
257 Installable *bool
258
Jiyong Parkda6eb592018-12-19 17:12:36 +0900259 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
260 // Default is false.
261 Use_vendor *bool
262
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800263 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
264 Ignore_system_library_special_case *bool
265
Alex Light9670d332019-01-29 18:07:33 -0800266 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900267
Jiyong Parkf97782b2019-02-13 20:28:58 +0900268 // List of sanitizer names that this APEX is enabled for
269 SanitizerNames []string `blueprint:"mutated"`
Jiyong Park49932f32019-08-09 14:44:36 +0900270
271 PreventInstall bool `blueprint:"mutated"`
272
273 HideFromMake bool `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800274}
275
276type apexTargetBundleProperties struct {
277 Target struct {
278 // Multilib properties only for android.
279 Android struct {
280 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900281 }
Alex Light9670d332019-01-29 18:07:33 -0800282 // Multilib properties only for host.
283 Host struct {
284 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900285 }
Alex Light9670d332019-01-29 18:07:33 -0800286 // Multilib properties only for host linux_bionic.
287 Linux_bionic struct {
288 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900289 }
Alex Light9670d332019-01-29 18:07:33 -0800290 // Multilib properties only for host linux_glibc.
291 Linux_glibc struct {
292 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900293 }
294 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900295}
296
Jiyong Park8fd61922018-11-08 02:50:25 +0900297type apexFileClass int
298
299const (
300 etc apexFileClass = iota
301 nativeSharedLib
302 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900303 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800304 pyBinary
305 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900306 javaSharedLib
307)
308
Alex Light5098a612018-11-29 17:12:15 -0800309type apexPackaging int
310
311const (
312 imageApex apexPackaging = iota
313 zipApex
314 both
315)
316
317func (a apexPackaging) image() bool {
318 switch a {
319 case imageApex, both:
320 return true
321 }
322 return false
323}
324
325func (a apexPackaging) zip() bool {
326 switch a {
327 case zipApex, both:
328 return true
329 }
330 return false
331}
332
333func (a apexPackaging) suffix() string {
334 switch a {
335 case imageApex:
336 return imageApexSuffix
337 case zipApex:
338 return zipApexSuffix
339 case both:
340 panic(fmt.Errorf("must be either zip or image"))
341 default:
342 panic(fmt.Errorf("unkonwn APEX type %d", a))
343 }
344}
345
346func (a apexPackaging) name() string {
347 switch a {
348 case imageApex:
349 return imageApexType
350 case zipApex:
351 return zipApexType
352 case both:
353 panic(fmt.Errorf("must be either zip or image"))
354 default:
355 panic(fmt.Errorf("unkonwn APEX type %d", a))
356 }
357}
358
Jiyong Park8fd61922018-11-08 02:50:25 +0900359func (class apexFileClass) NameInMake() string {
360 switch class {
361 case etc:
362 return "ETC"
363 case nativeSharedLib:
364 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800365 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900366 return "EXECUTABLES"
367 case javaSharedLib:
368 return "JAVA_LIBRARIES"
369 default:
370 panic(fmt.Errorf("unkonwn class %d", class))
371 }
372}
373
374type apexFile struct {
375 builtFile android.Path
376 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900377 installDir string
378 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900379 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800380 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900381}
382
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900383type apexBundle struct {
384 android.ModuleBase
385 android.DefaultableModuleBase
386
Alex Light9670d332019-01-29 18:07:33 -0800387 properties apexBundleProperties
388 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900389
Alex Light5098a612018-11-29 17:12:15 -0800390 apexTypes apexPackaging
391
Colin Crossa4925902018-11-16 11:36:28 -0800392 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800393 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800394 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900395
Jiyong Park42cca6c2019-04-01 11:15:50 +0900396 public_key_file android.Path
397 private_key_file android.Path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900398
399 container_certificate_file android.Path
400 container_private_key_file android.Path
401
Jiyong Park8fd61922018-11-08 02:50:25 +0900402 // list of files to be included in this apex
403 filesInfo []apexFile
404
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900405 // list of module names that this APEX is depending on
406 externalDeps []string
407
Jiyong Park8fd61922018-11-08 02:50:25 +0900408 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800409
410 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900411}
412
Jiyong Park397e55e2018-10-24 21:09:55 +0900413func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900414 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900415 // Use *FarVariation* to be able to depend on modules having
416 // conflicting variations with this module. This is required since
417 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
418 // for native shared libs.
419 ctx.AddFarVariationDependencies([]blueprint.Variation{
420 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900421 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900422 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900423 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900424 }, sharedLibTag, native_shared_libs...)
425
426 ctx.AddFarVariationDependencies([]blueprint.Variation{
427 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900428 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900429 }, executableTag, binaries...)
430}
431
Alex Light9670d332019-01-29 18:07:33 -0800432func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
433 if ctx.Os().Class == android.Device {
434 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
435 } else {
436 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
437 if ctx.Os().Bionic() {
438 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
439 } else {
440 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
441 }
442 }
443}
444
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900445func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800446
Jiyong Park397e55e2018-10-24 21:09:55 +0900447 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900448 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800449
450 a.combineProperties(ctx)
451
Jiyong Park397e55e2018-10-24 21:09:55 +0900452 has32BitTarget := false
453 for _, target := range targets {
454 if target.Arch.ArchType.Multilib == "lib32" {
455 has32BitTarget = true
456 }
457 }
458 for i, target := range targets {
459 // When multilib.* is omitted for native_shared_libs, it implies
460 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900461 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900462 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900463 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900464 {Mutator: "link", Variation: "shared"},
465 }, sharedLibTag, a.properties.Native_shared_libs...)
466
Jiyong Park397e55e2018-10-24 21:09:55 +0900467 // Add native modules targetting both ABIs
468 addDependenciesForNativeModules(ctx,
469 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900470 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900471 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900472
Alex Light3d673592019-01-18 14:37:31 -0800473 isPrimaryAbi := i == 0
474 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900475 // When multilib.* is omitted for binaries, it implies
476 // multilib.first.
477 ctx.AddFarVariationDependencies([]blueprint.Variation{
478 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900479 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900480 }, executableTag, a.properties.Binaries...)
481
482 // Add native modules targetting the first ABI
483 addDependenciesForNativeModules(ctx,
484 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900485 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900486 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800487
488 // When multilib.* is omitted for prebuilts, it implies multilib.first.
489 ctx.AddFarVariationDependencies([]blueprint.Variation{
490 {Mutator: "arch", Variation: target.String()},
491 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900492 }
493
494 switch target.Arch.ArchType.Multilib {
495 case "lib32":
496 // Add native modules targetting 32-bit ABI
497 addDependenciesForNativeModules(ctx,
498 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900499 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900500 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900501
502 addDependenciesForNativeModules(ctx,
503 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900504 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900505 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900506 case "lib64":
507 // Add native modules targetting 64-bit ABI
508 addDependenciesForNativeModules(ctx,
509 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900510 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900511 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900512
513 if !has32BitTarget {
514 addDependenciesForNativeModules(ctx,
515 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900516 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900517 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900518 }
Peter Collingbournee1629272019-04-24 14:41:12 -0700519
520 if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
521 for _, sanitizer := range ctx.Config().SanitizeDevice() {
522 if sanitizer == "hwaddress" {
523 addDependenciesForNativeModules(ctx,
524 []string{"libclang_rt.hwasan-aarch64-android"},
525 nil, target.String(), a.getImageVariation(config))
526 break
527 }
528 }
529 }
Jiyong Park397e55e2018-10-24 21:09:55 +0900530 }
531
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900532 }
533
Jiyong Parkff1458f2018-10-12 21:49:38 +0900534 ctx.AddFarVariationDependencies([]blueprint.Variation{
535 {Mutator: "arch", Variation: "android_common"},
536 }, javaLibTag, a.properties.Java_libs...)
537
Jiyong Park23c52b02019-02-02 13:13:47 +0900538 if String(a.properties.Key) == "" {
539 ctx.ModuleErrorf("key is missing")
540 return
541 }
542 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900543
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900544 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900545 if cert != "" {
546 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900547 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900548}
549
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900550func (a *apexBundle) getCertString(ctx android.BaseContext) string {
551 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
552 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000553 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900554 }
555 return String(a.properties.Certificate)
556}
557
Jiyong Park74e240b2018-11-27 21:27:08 +0900558func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900559 if file, ok := a.outputFiles[imageApex]; ok {
560 return android.Paths{file}
561 } else {
562 return nil
563 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900564}
565
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900566func (a *apexBundle) installable() bool {
Jiyong Park49932f32019-08-09 14:44:36 +0900567 return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900568}
569
Jiyong Park7c1dc612019-01-05 11:15:24 +0900570func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
571 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900572 return "vendor"
573 } else {
574 return "core"
575 }
576}
577
Jiyong Parkf97782b2019-02-13 20:28:58 +0900578func (a *apexBundle) EnableSanitizer(sanitizerName string) {
579 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
580 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
581 }
582}
583
Jiyong Park388ef3f2019-01-28 19:47:32 +0900584func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900585 if android.InList(sanitizerName, a.properties.SanitizerNames) {
586 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900587 }
588
589 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900590 globalSanitizerNames := []string{}
591 if a.Host() {
592 globalSanitizerNames = ctx.Config().SanitizeHost()
593 } else {
594 arches := ctx.Config().SanitizeDeviceArch()
595 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
596 globalSanitizerNames = ctx.Config().SanitizeDevice()
597 }
598 }
599 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900600}
601
Jiyong Park49932f32019-08-09 14:44:36 +0900602func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseContext) bool {
603 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
604}
605
606func (a *apexBundle) PreventInstall() {
607 a.properties.PreventInstall = true
608}
609
610func (a *apexBundle) HideFromMake() {
611 a.properties.HideFromMake = true
612}
613
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800614func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900615 // Decide the APEX-local directory by the multilib of the library
616 // In the future, we may query this to the module.
617 switch cc.Arch().ArchType.Multilib {
618 case "lib32":
619 dirInApex = "lib"
620 case "lib64":
621 dirInApex = "lib64"
622 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900623 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900624 if !cc.Arch().Native {
625 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
626 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800627 if handleSpecialLibs {
628 switch cc.Name() {
629 case "libc", "libm", "libdl":
630 // Special case for bionic libs. This is to prevent the bionic libs
631 // from being included in the search path /apex/com.android.apex/lib.
632 // This exclusion is required because bionic libs in the runtime APEX
633 // are available via the legacy paths /system/lib/libc.so, etc. By the
634 // init process, the bionic libs in the APEX are bind-mounted to the
635 // legacy paths and thus will be loaded into the default linker namespace.
636 // If the bionic libs are directly in /apex/com.android.apex/lib then
637 // the same libs will be again loaded to the runtime linker namespace,
638 // which will result double loading of bionic libs that isn't supported.
639 dirInApex = filepath.Join(dirInApex, "bionic")
640 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900641 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900642
643 fileToCopy = cc.OutputFile().Path()
644 return
645}
646
647func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900648 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900649 fileToCopy = cc.OutputFile().Path()
650 return
651}
652
Alex Light778127a2019-02-27 14:19:50 -0800653func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
654 dirInApex = "bin"
655 fileToCopy = py.HostToolPath().Path()
656 return
657}
658func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
659 dirInApex = "bin"
660 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
661 if err != nil {
662 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
663 return
664 }
665 fileToCopy = android.PathForOutput(ctx, s)
666 return
667}
668
Jiyong Park04480cf2019-02-06 00:16:29 +0900669func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
670 dirInApex = filepath.Join("bin", sh.SubDir())
671 fileToCopy = sh.OutputFile()
672 return
673}
674
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900675func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
676 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900677 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900678 return
679}
680
681func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
682 dirInApex = filepath.Join("etc", prebuilt.SubDir())
683 fileToCopy = prebuilt.OutputFile()
684 return
685}
686
687func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900688 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900689
Alex Light5098a612018-11-29 17:12:15 -0800690 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
691 a.apexTypes = imageApex
692 } else if *a.properties.Payload_type == "zip" {
693 a.apexTypes = zipApex
694 } else if *a.properties.Payload_type == "both" {
695 a.apexTypes = both
696 } else {
697 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
698 return
699 }
700
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800701 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
702
Alex Light778127a2019-02-27 14:19:50 -0800703 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900704 if _, ok := parent.(*apexBundle); ok {
705 // direct dependencies
706 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900707 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900708 switch depTag {
709 case sharedLibTag:
710 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800711 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900712 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900713 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900714 } else {
715 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900716 }
717 case executableTag:
718 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800719 if !cc.Arch().Native {
720 // There is only one 'bin' directory so we shouldn't bother copying in
721 // native-bridge'd binaries and only use main ones.
722 return true
723 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900724 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900725 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900726 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900727 } else if sh, ok := child.(*android.ShBinary); ok {
728 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
729 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800730 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
731 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
732 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
733 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
734 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
735 // NB: Since go binaries are static we don't need the module for anything here, which is
736 // good since the go tool is a blueprint.Module not an android.Module like we would
737 // normally use.
738 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900739 } else {
Alex Light778127a2019-02-27 14:19:50 -0800740 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 +0900741 }
742 case javaLibTag:
743 if java, ok := child.(*java.Library); ok {
744 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900745 if fileToCopy == nil {
746 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
747 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900748 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900749 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900750 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900751 } else {
752 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900753 }
754 case prebuiltTag:
755 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
756 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900757 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900758 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900759 } else {
760 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
761 }
762 case keyTag:
763 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900764 a.private_key_file = key.private_key_file
765 a.public_key_file = key.public_key_file
Jiyong Parkff1458f2018-10-12 21:49:38 +0900766 return false
767 } else {
768 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900769 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900770 case certificateTag:
771 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900772 a.container_certificate_file = dep.Certificate.Pem
773 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900774 return false
775 } else {
776 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
777 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900778 }
779 } else {
780 // indirect dependencies
781 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
782 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800783 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900784 // If the dependency is a stubs lib, don't include it in this APEX,
785 // but make sure that the lib is installed on the device.
786 // In case no APEX is having the lib, the lib is installed to the system
787 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800788 //
789 // Always include if we are a host-apex however since those won't have any
790 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900791 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
792 a.externalDeps = append(a.externalDeps, cc.Name())
793 }
794 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900795 return false
796 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900797 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800798 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900799 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900800 return true
801 }
802 }
803 }
804 return false
805 })
806
Jiyong Park9335a262018-12-24 11:31:58 +0900807 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900808 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900809 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
810 return
811 }
812
Jiyong Park8fd61922018-11-08 02:50:25 +0900813 // remove duplicates in filesInfo
814 removeDup := func(filesInfo []apexFile) []apexFile {
815 encountered := make(map[android.Path]bool)
816 result := []apexFile{}
817 for _, f := range filesInfo {
818 if !encountered[f.builtFile] {
819 encountered[f.builtFile] = true
820 result = append(result, f)
821 }
822 }
823 return result
824 }
825 filesInfo = removeDup(filesInfo)
826
827 // to have consistent build rules
828 sort.Slice(filesInfo, func(i, j int) bool {
829 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
830 })
831
832 // prepend the name of this APEX to the module names. These names will be the names of
833 // modules that will be defined if the APEX is flattened.
834 for i := range filesInfo {
835 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
836 }
837
Jiyong Park8fd61922018-11-08 02:50:25 +0900838 a.installDir = android.PathForModuleInstall(ctx, "apex")
839 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800840
841 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900842 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800843 }
844 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900845 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
846 // is true. This is to support referencing APEX via ":<module_name" syntax
847 // in other modules. It is in AndroidMk where the selection of flattened
848 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900849 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900850 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900851 }
852}
853
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700854func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
Jiyong Park52818fc2019-03-18 12:01:38 +0900855 noticeFiles := []android.Path{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900856 for _, f := range a.filesInfo {
857 if f.module != nil {
858 notice := f.module.NoticeFile()
859 if notice.Valid() {
860 noticeFiles = append(noticeFiles, notice.Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900861 }
862 }
863 }
864 // append the notice file specified in the apex module itself
865 if a.NoticeFile().Valid() {
866 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
Jiyong Park52818fc2019-03-18 12:01:38 +0900867 }
868
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700869 if len(noticeFiles) == 0 {
870 return android.OptionalPath{}
Jiyong Park52818fc2019-03-18 12:01:38 +0900871 }
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700872
873 return android.OptionalPathForPath(
874 android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)))
Jiyong Park52818fc2019-03-18 12:01:38 +0900875}
876
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900877func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900878 cert := String(a.properties.Certificate)
879 if cert != "" && android.SrcIsModule(cert) == "" {
880 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900881 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
882 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900883 } else if cert == "" {
884 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900885 a.container_certificate_file = pem
886 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900887 }
888
Colin Cross8a497952019-03-05 22:25:09 -0800889 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900890
Alex Light5098a612018-11-29 17:12:15 -0800891 var abis []string
892 for _, target := range ctx.MultiTargets() {
893 if len(target.Arch.Abi) > 0 {
894 abis = append(abis, target.Arch.Abi[0])
895 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900896 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900897
Alex Light5098a612018-11-29 17:12:15 -0800898 abis = android.FirstUniqueStrings(abis)
899
900 suffix := apexType.suffix()
901 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900902
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900903 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900904 for _, f := range a.filesInfo {
905 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900906 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900907
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900908 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900909 for i, src := range filesToCopy {
910 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800911 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900912 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
913 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800914 for _, sym := range a.filesInfo[i].symlinks {
915 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
916 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
917 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900918 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900919 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800920 implicitInputs = append(implicitInputs, manifest)
921
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900922 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
923 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900924
Alex Light5098a612018-11-29 17:12:15 -0800925 if apexType.image() {
926 // files and dirs that will be created in APEX
927 var readOnlyPaths []string
928 var executablePaths []string // this also includes dirs
929 for _, f := range a.filesInfo {
930 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
931 if f.installDir == "bin" {
932 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800933 for _, s := range f.symlinks {
934 executablePaths = append(executablePaths, filepath.Join("bin", s))
935 }
Alex Light5098a612018-11-29 17:12:15 -0800936 } else {
937 readOnlyPaths = append(readOnlyPaths, pathInApex)
938 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900939 dir := f.installDir
940 for !android.InList(dir, executablePaths) && dir != "" {
941 executablePaths = append(executablePaths, dir)
942 dir, _ = filepath.Split(dir) // move up to the parent
943 if len(dir) > 0 {
944 // remove trailing slash
945 dir = dir[:len(dir)-1]
946 }
Alex Light5098a612018-11-29 17:12:15 -0800947 }
948 }
949 sort.Strings(readOnlyPaths)
950 sort.Strings(executablePaths)
951 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
952 ctx.Build(pctx, android.BuildParams{
953 Rule: generateFsConfig,
954 Output: cannedFsConfig,
955 Description: "generate fs config",
956 Args: map[string]string{
957 "ro_paths": strings.Join(readOnlyPaths, " "),
958 "exec_paths": strings.Join(executablePaths, " "),
959 },
960 })
961
962 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
963 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
964 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
965 if !fileContextsOptionalPath.Valid() {
966 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
967 return
968 }
969 fileContexts := fileContextsOptionalPath.Path()
970
Jiyong Park835d82b2018-12-27 16:04:18 +0900971 optFlags := []string{}
972
Alex Light5098a612018-11-29 17:12:15 -0800973 // Additional implicit inputs.
Jiyong Park42cca6c2019-04-01 11:15:50 +0900974 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file, a.public_key_file)
975 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Alex Light5098a612018-11-29 17:12:15 -0800976
Jiyong Park7f67f482019-01-05 12:57:48 +0900977 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
978 if overridden {
979 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
980 }
981
Jiyong Park40e26a22019-02-08 02:53:06 +0900982 if a.properties.AndroidManifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800983 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Jiyong Park40e26a22019-02-08 02:53:06 +0900984 implicitInputs = append(implicitInputs, androidManifestFile)
985 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
986 }
987
Jiyong Parkd37a8822019-04-18 17:25:49 +0900988 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
989 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
990 ctx.Config().UnbundledBuild() &&
991 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
992 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
993 apiFingerprint := java.ApiFingerprintPath(ctx)
994 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
995 implicitInputs = append(implicitInputs, apiFingerprint)
996 }
997 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
998
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700999 noticeFile := a.buildNoticeFile(ctx, ctx.ModuleName()+suffix)
1000 if noticeFile.Valid() {
1001 // If there's a NOTICE file, embed it as an asset file in the APEX.
1002 implicitInputs = append(implicitInputs, noticeFile.Path())
1003 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
1004 }
1005
Alex Light5098a612018-11-29 17:12:15 -08001006 ctx.Build(pctx, android.BuildParams{
1007 Rule: apexRule,
1008 Implicits: implicitInputs,
1009 Output: unsignedOutputFile,
1010 Description: "apex (" + apexType.name() + ")",
1011 Args: map[string]string{
1012 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1013 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1014 "copy_commands": strings.Join(copyCommands, " && "),
1015 "manifest": manifest.String(),
1016 "file_contexts": fileContexts.String(),
1017 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001018 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001019 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001020 },
1021 })
1022
1023 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1024 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1025 a.bundleModuleFile = bundleModuleFile
1026
1027 ctx.Build(pctx, android.BuildParams{
1028 Rule: apexProtoConvertRule,
1029 Input: unsignedOutputFile,
1030 Output: apexProtoFile,
1031 Description: "apex proto convert",
1032 })
1033
1034 ctx.Build(pctx, android.BuildParams{
1035 Rule: apexBundleRule,
1036 Input: apexProtoFile,
1037 Output: a.bundleModuleFile,
1038 Description: "apex bundle module",
1039 Args: map[string]string{
1040 "abi": strings.Join(abis, "."),
1041 },
1042 })
1043 } else {
1044 ctx.Build(pctx, android.BuildParams{
1045 Rule: zipApexRule,
1046 Implicits: implicitInputs,
1047 Output: unsignedOutputFile,
1048 Description: "apex (" + apexType.name() + ")",
1049 Args: map[string]string{
1050 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1051 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1052 "copy_commands": strings.Join(copyCommands, " && "),
1053 "manifest": manifest.String(),
1054 },
1055 })
Colin Crossa4925902018-11-16 11:36:28 -08001056 }
Colin Crossa4925902018-11-16 11:36:28 -08001057
Alex Light5098a612018-11-29 17:12:15 -08001058 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001059 ctx.Build(pctx, android.BuildParams{
1060 Rule: java.Signapk,
1061 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001062 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001063 Input: unsignedOutputFile,
1064 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001065 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001066 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001067 },
1068 })
Alex Light5098a612018-11-29 17:12:15 -08001069
1070 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001071 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001072 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001073 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001074}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001075
Jiyong Park8fd61922018-11-08 02:50:25 +09001076func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001077 if a.installable() {
Jiyong Park42cca6c2019-04-01 11:15:50 +09001078 // For flattened APEX, do nothing but make sure that apex_manifest.json and apex_pubkey are also copied along
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001079 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001080 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001081
1082 // rename to apex_manifest.json
1083 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1084 ctx.Build(pctx, android.BuildParams{
1085 Rule: android.Cp,
1086 Input: manifest,
1087 Output: copiedManifest,
1088 })
Jiyong Park719b4462019-01-13 00:39:51 +09001089 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001090
Jiyong Park42cca6c2019-04-01 11:15:50 +09001091 // rename to apex_pubkey
1092 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
1093 ctx.Build(pctx, android.BuildParams{
1094 Rule: android.Cp,
1095 Input: a.public_key_file,
1096 Output: copiedPubkey,
1097 })
1098 a.filesInfo = append(a.filesInfo, apexFile{copiedPubkey, ctx.ModuleName() + ".apex_pubkey", ".", etc, nil, nil})
1099
Jiyong Park23c52b02019-02-02 13:13:47 +09001100 if ctx.Config().FlattenApex() {
1101 for _, fi := range a.filesInfo {
1102 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001103 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1104 for _, sym := range fi.symlinks {
1105 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1106 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001107 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001108 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001109 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001110}
1111
1112func (a *apexBundle) AndroidMk() android.AndroidMkData {
Jiyong Park49932f32019-08-09 14:44:36 +09001113 if a.properties.HideFromMake {
1114 return android.AndroidMkData{
1115 Disabled: true,
1116 }
1117 }
Alex Light5098a612018-11-29 17:12:15 -08001118 writers := []android.AndroidMkData{}
1119 if a.apexTypes.image() {
1120 writers = append(writers, a.androidMkForType(imageApex))
1121 }
1122 if a.apexTypes.zip() {
1123 writers = append(writers, a.androidMkForType(zipApex))
1124 }
1125 return android.AndroidMkData{
1126 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1127 for _, data := range writers {
1128 data.Custom(w, name, prefix, moduleDir, data)
1129 }
1130 }}
1131}
1132
Alex Lightf1801bc2019-02-13 11:10:07 -08001133func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001134 moduleNames := []string{}
1135
1136 for _, fi := range a.filesInfo {
1137 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1138 continue
1139 }
1140 if !android.InList(fi.moduleName, moduleNames) {
1141 moduleNames = append(moduleNames, fi.moduleName)
1142 }
1143 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1144 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1145 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001146 // /apex/<name>/{lib|framework|...}
1147 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1148 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001149 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001150 // /system/apex/<name>/{lib|framework|...}
1151 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1152 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001153 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001154 if len(fi.symlinks) > 0 {
1155 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1156 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001157
1158 if fi.module != nil && fi.module.NoticeFile().Valid() {
1159 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1160 }
Jiyong Park94427262019-02-05 23:18:47 +09001161 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001162 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001163 }
1164 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1165 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1166 if fi.module != nil {
1167 archStr := fi.module.Target().Arch.ArchType.String()
1168 host := false
1169 switch fi.module.Target().Os.Class {
1170 case android.Host:
1171 if archStr != "common" {
1172 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1173 }
1174 host = true
1175 case android.HostCross:
1176 if archStr != "common" {
1177 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1178 }
1179 host = true
1180 case android.Device:
1181 if archStr != "common" {
1182 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1183 }
1184 }
1185 if host {
1186 makeOs := fi.module.Target().Os.String()
1187 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1188 makeOs = "linux"
1189 }
1190 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1191 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1192 }
1193 }
1194 if fi.class == javaSharedLib {
1195 javaModule := fi.module.(*java.Library)
1196 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1197 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1198 // we will have foo.jar.jar
1199 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1200 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1201 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1202 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1203 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1204 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1205 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1206 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park49932f32019-08-09 14:44:36 +09001207 if cc, ok := fi.module.(*cc.Module); ok {
1208 if cc.UnstrippedOutputFile() != nil {
1209 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1210 }
1211 if cc.CoverageOutputFile().Valid() {
1212 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
1213 }
Jiyong Park94427262019-02-05 23:18:47 +09001214 }
1215 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1216 } else {
1217 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1218 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1219 }
1220 }
1221 return moduleNames
1222}
1223
Alex Light5098a612018-11-29 17:12:15 -08001224func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001225 return android.AndroidMkData{
1226 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1227 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001228 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001229 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001230 }
1231
Jiyong Park719b4462019-01-13 00:39:51 +09001232 if a.flattened && apexType.image() {
1233 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001234 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1235 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1236 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001237 if len(moduleNames) > 0 {
1238 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1239 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001240 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001241 } else {
Alex Light5098a612018-11-29 17:12:15 -08001242 // zip-apex is the less common type so have the name refer to the image-apex
1243 // only and use {name}.zip if you want the zip-apex
1244 if apexType == zipApex && a.apexTypes == both {
1245 name = name + ".zip"
1246 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001247 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1248 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1249 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1250 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001251 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001252 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001253 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001254 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park94427262019-02-05 23:18:47 +09001255 if len(moduleNames) > 0 {
1256 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1257 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001258 if len(a.externalDeps) > 0 {
1259 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1260 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001261 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001262
Alex Light5098a612018-11-29 17:12:15 -08001263 if apexType == imageApex {
1264 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1265 }
Jiyong Park719b4462019-01-13 00:39:51 +09001266 }
1267 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001268}
1269
Alex Light0851b882019-02-07 13:20:53 -08001270func testApexBundleFactory() android.Module {
1271 return ApexBundleFactory( /*testApex*/ true)
1272}
1273
1274func apexBundleFactory() android.Module {
1275 return ApexBundleFactory( /*testApex*/ false)
1276}
1277
1278func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001279 module := &apexBundle{
1280 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001281 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001282 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001283 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001284 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001285 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001286 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1287 })
Alex Light5098a612018-11-29 17:12:15 -08001288 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001289 android.InitDefaultableModule(module)
1290 return module
1291}
Jiyong Park30ca9372019-02-07 16:27:23 +09001292
1293//
1294// Defaults
1295//
1296type Defaults struct {
1297 android.ModuleBase
1298 android.DefaultsModuleBase
1299}
1300
1301func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1302}
1303
1304func defaultsFactory() android.Module {
1305 return DefaultsFactory()
1306}
1307
1308func DefaultsFactory(props ...interface{}) android.Module {
1309 module := &Defaults{}
1310
1311 module.AddProperties(props...)
1312 module.AddProperties(
1313 &apexBundleProperties{},
1314 &apexTargetBundleProperties{},
1315 )
1316
1317 android.InitDefaultsModule(module)
1318 return module
1319}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001320
1321//
1322// Prebuilt APEX
1323//
1324type Prebuilt struct {
1325 android.ModuleBase
1326 prebuilt android.Prebuilt
1327
1328 properties PrebuiltProperties
1329
Nikita Ioffeed75f612019-04-04 18:09:48 +01001330 inputApex android.Path
1331 installDir android.OutputPath
1332 installFilename string
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001333 outputApex android.WritablePath
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001334}
1335
1336type PrebuiltProperties struct {
1337 // the path to the prebuilt .apex file to import.
Jiyong Park0a573d72019-07-07 12:39:16 +09001338 Source string `blueprint:"mutated"`
1339 ForceDisable bool `blueprint:"mutated"`
Jiyong Parkc95714e2019-03-29 14:23:10 +09001340
1341 Src *string
1342 Arch struct {
1343 Arm struct {
1344 Src *string
1345 }
1346 Arm64 struct {
1347 Src *string
1348 }
1349 X86 struct {
1350 Src *string
1351 }
1352 X86_64 struct {
1353 Src *string
1354 }
1355 }
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001356
1357 Installable *bool
Nikita Ioffeed75f612019-04-04 18:09:48 +01001358 // Optional name for the installed apex. If unspecified, name of the
1359 // module is used as the file name
1360 Filename *string
Jaewoong Jung270a5eb2019-07-16 18:25:41 -07001361
1362 // Names of modules to be overridden. Listed modules can only be other binaries
1363 // (in Make or Soong).
1364 // This does not completely prevent installation of the overridden binaries, but if both
1365 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1366 // from PRODUCT_PACKAGES.
1367 Overrides []string
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001368}
1369
1370func (p *Prebuilt) installable() bool {
1371 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001372}
1373
1374func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkb8650ff2019-07-15 15:31:16 +09001375 // If the device is configured to use flattened APEX, force disable the prebuilt because
1376 // the prebuilt is a non-flattened one.
1377 forceDisable := ctx.Config().FlattenApex()
1378
1379 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
1380 // to build the prebuilts themselves.
Jiyong Park9863d532019-07-17 08:21:36 +09001381 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
Jiyong Parkb1d60cb2019-07-11 11:24:41 +09001382
Kun Niu1bc40c52019-07-29 16:28:57 -07001383 // Force disable the prebuilts when coverage is enabled.
1384 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
1385 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
1386
Jiyong Parkb1d60cb2019-07-11 11:24:41 +09001387 // b/137216042 don't use prebuilts when address sanitizer is on
1388 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
1389 android.InList("hwaddress", ctx.Config().SanitizeDevice())
1390
1391 if forceDisable && p.prebuilt.SourceExists() {
Jiyong Park0a573d72019-07-07 12:39:16 +09001392 p.properties.ForceDisable = true
1393 return
1394 }
1395
Jiyong Parkc95714e2019-03-29 14:23:10 +09001396 // This is called before prebuilt_select and prebuilt_postdeps mutators
1397 // The mutators requires that src to be set correctly for each arch so that
1398 // arch variants are disabled when src is not provided for the arch.
1399 if len(ctx.MultiTargets()) != 1 {
1400 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
1401 return
1402 }
1403 var src string
1404 switch ctx.MultiTargets()[0].Arch.ArchType {
1405 case android.Arm:
1406 src = String(p.properties.Arch.Arm.Src)
1407 case android.Arm64:
1408 src = String(p.properties.Arch.Arm64.Src)
1409 case android.X86:
1410 src = String(p.properties.Arch.X86.Src)
1411 case android.X86_64:
1412 src = String(p.properties.Arch.X86_64.Src)
1413 default:
1414 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
1415 return
1416 }
1417 if src == "" {
1418 src = String(p.properties.Src)
1419 }
1420 p.properties.Source = src
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001421}
1422
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001423func (p *Prebuilt) Srcs() android.Paths {
1424 return android.Paths{p.outputApex}
1425}
1426
Jiyong Parka41f12a2019-04-23 18:00:10 +09001427func (p *Prebuilt) InstallFilename() string {
1428 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
1429}
1430
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001431func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park0a573d72019-07-07 12:39:16 +09001432 if p.properties.ForceDisable {
1433 return
1434 }
1435
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001436 // TODO(jungjw): Check the key validity.
Jiyong Parkc95714e2019-03-29 14:23:10 +09001437 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001438 p.installDir = android.PathForModuleInstall(ctx, "apex")
Jiyong Parka41f12a2019-04-23 18:00:10 +09001439 p.installFilename = p.InstallFilename()
Nikita Ioffeed75f612019-04-04 18:09:48 +01001440 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
1441 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
1442 }
Nikita Ioffebed7cd32019-04-05 02:10:45 +01001443 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
1444 ctx.Build(pctx, android.BuildParams{
1445 Rule: android.Cp,
1446 Input: p.inputApex,
1447 Output: p.outputApex,
1448 })
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001449 if p.installable() {
Nikita Ioffeed75f612019-04-04 18:09:48 +01001450 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
Nikita Ioffe03a31cc2019-04-04 13:42:00 +01001451 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001452}
1453
1454func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1455 return &p.prebuilt
1456}
1457
1458func (p *Prebuilt) Name() string {
1459 return p.prebuilt.Name(p.ModuleBase.Name())
1460}
1461
Jaewoong Jung270a5eb2019-07-16 18:25:41 -07001462func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
1463 return android.AndroidMkEntries{
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001464 Class: "ETC",
1465 OutputFile: android.OptionalPathForPath(p.inputApex),
1466 Include: "$(BUILD_PREBUILT)",
Jaewoong Jung283a9ee2019-08-27 17:33:16 -07001467 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1468 func(entries *android.AndroidMkEntries) {
1469 entries.SetString("LOCAL_MODULE_PATH", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1470 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
1471 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
1472 entries.AddStrings("LOCAL_OVERRIDES_PACKAGES", p.properties.Overrides...)
1473 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001474 },
1475 }
1476}
1477
1478// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1479func PrebuiltFactory() android.Module {
1480 module := &Prebuilt{}
1481 module.AddProperties(&module.properties)
Jaewoong Jung05d5b542019-06-11 12:25:34 -07001482 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
Jiyong Parkc95714e2019-03-29 14:23:10 +09001483 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001484 return module
1485}