blob: 685a774643db98cbfc5cd188050490c7e4284dbc [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 ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000089 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080090 CommandDeps: []string{"${zip2zip}"},
91 Description: "app bundle",
92 }, "abi")
Jiyong Park52818fc2019-03-18 12:01:38 +090093
94 apexMergeNoticeRule = pctx.StaticRule("apexMergeNoticeRule", blueprint.RuleParams{
95 Command: `${mergenotice} --output $out $inputs`,
96 CommandDeps: []string{"${mergenotice}"},
97 Description: "merge notice files into $out",
98 }, "inputs")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090099)
100
Alex Light5098a612018-11-29 17:12:15 -0800101var imageApexSuffix = ".apex"
102var zipApexSuffix = ".zipapex"
103
104var imageApexType = "image"
105var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900106
107type dependencyTag struct {
108 blueprint.BaseDependencyTag
109 name string
110}
111
112var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900113 sharedLibTag = dependencyTag{name: "sharedLib"}
114 executableTag = dependencyTag{name: "executable"}
115 javaLibTag = dependencyTag{name: "javaLib"}
116 prebuiltTag = dependencyTag{name: "prebuilt"}
117 keyTag = dependencyTag{name: "key"}
118 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900119)
120
121func init() {
122 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900123 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900124 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100125 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
126 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
127 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
128 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000129 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100130 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
131 } else {
132 return pctx.HostBinToolPath(ctx, tool).String()
133 }
134 })
135 }
136 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900137 pctx.HostBinToolVariable("avbtool", "avbtool")
138 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
139 pctx.HostBinToolVariable("merge_zips", "merge_zips")
140 pctx.HostBinToolVariable("mke2fs", "mke2fs")
141 pctx.HostBinToolVariable("resize2fs", "resize2fs")
142 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
143 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800144 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900145 pctx.HostBinToolVariable("zipalign", "zipalign")
146
Jiyong Park52818fc2019-03-18 12:01:38 +0900147 pctx.SourcePathVariable("mergenotice", "build/soong/scripts/mergenotice.py")
148
Alex Light0851b882019-02-07 13:20:53 -0800149 android.RegisterModuleType("apex", apexBundleFactory)
150 android.RegisterModuleType("apex_test", testApexBundleFactory)
Jiyong Park30ca9372019-02-07 16:27:23 +0900151 android.RegisterModuleType("apex_defaults", defaultsFactory)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700152 android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900153
154 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
155 ctx.TopDown("apex_deps", apexDepsMutator)
156 ctx.BottomUp("apex", apexMutator)
157 })
158}
159
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160// Mark the direct and transitive dependencies of apex bundles so that they
161// can be built for the apex bundles.
162func apexDepsMutator(mctx android.TopDownMutatorContext) {
Alex Lightf98087f2019-02-04 14:45:06 -0800163 if a, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800164 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900165 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900166 depName := mctx.OtherModuleName(child)
167 // If the parent is apexBundle, this child is directly depended.
168 _, directDep := parent.(*apexBundle)
Alex Light0851b882019-02-07 13:20:53 -0800169 if a.installable() && !a.testApex {
Alex Lightf98087f2019-02-04 14:45:06 -0800170 // TODO(b/123892969): Workaround for not having any way to annotate test-apexs
171 // non-installable apex's cannot be installed and so should not prevent libraries from being
172 // installed to the system.
173 android.UpdateApexDependency(apexBundleName, depName, directDep)
174 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900175
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900176 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900177 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900178 return true
179 } else {
180 return false
181 }
182 })
183 }
184}
185
186// Create apex variations if a module is included in APEX(s).
187func apexMutator(mctx android.BottomUpMutatorContext) {
188 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900189 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900190 } else if _, ok := mctx.Module().(*apexBundle); ok {
191 // apex bundle itself is mutated so that it and its modules have same
192 // apex variant.
193 apexBundleName := mctx.ModuleName()
194 mctx.CreateVariations(apexBundleName)
195 }
196}
197
Alex Light9670d332019-01-29 18:07:33 -0800198type apexNativeDependencies struct {
199 // List of native libraries
200 Native_shared_libs []string
201 // List of native executables
202 Binaries []string
203}
204type apexMultilibProperties struct {
205 // Native dependencies whose compile_multilib is "first"
206 First apexNativeDependencies
207
208 // Native dependencies whose compile_multilib is "both"
209 Both apexNativeDependencies
210
211 // Native dependencies whose compile_multilib is "prefer32"
212 Prefer32 apexNativeDependencies
213
214 // Native dependencies whose compile_multilib is "32"
215 Lib32 apexNativeDependencies
216
217 // Native dependencies whose compile_multilib is "64"
218 Lib64 apexNativeDependencies
219}
220
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900221type apexBundleProperties struct {
222 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000223 // "apex_manifest.json"
Colin Cross27b922f2019-03-04 22:35:41 -0800224 Manifest *string `android:"path"`
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900225
Jiyong Park40e26a22019-02-08 02:53:06 +0900226 // AndroidManifest.xml file used for the zip container of this APEX bundle.
227 // If unspecified, a default one is automatically generated.
Colin Cross27b922f2019-03-04 22:35:41 -0800228 AndroidManifest *string `android:"path"`
Jiyong Park40e26a22019-02-08 02:53:06 +0900229
Jiyong Park05e70dd2019-03-18 14:26:32 +0900230 // Canonical name of the APEX bundle in the manifest file.
231 // If unspecified, defaults to the value of name
232 Apex_name *string
233
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900234 // Determines the file contexts file for setting security context to each file in this APEX bundle.
235 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
236 // used.
237 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900238 File_contexts *string
239
240 // List of native shared libs that are embedded inside this APEX bundle
241 Native_shared_libs []string
242
243 // List of native executables that are embedded inside this APEX bundle
244 Binaries []string
245
246 // List of java libraries that are embedded inside this APEX bundle
247 Java_libs []string
248
249 // List of prebuilt files that are embedded inside this APEX bundle
250 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900251
252 // Name of the apex_key module that provides the private key to sign APEX
253 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900254
Alex Light5098a612018-11-29 17:12:15 -0800255 // The type of APEX to build. Controls what the APEX payload is. Either
256 // 'image', 'zip' or 'both'. Default: 'image'.
257 Payload_type *string
258
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900259 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
260 // or an android_app_certificate module name in the form ":module".
261 Certificate *string
262
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900263 // Whether this APEX is installable to one of the partitions. Default: true.
264 Installable *bool
265
Jiyong Parkda6eb592018-12-19 17:12:36 +0900266 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
267 // Default is false.
268 Use_vendor *bool
269
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800270 // For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
271 Ignore_system_library_special_case *bool
272
Alex Light9670d332019-01-29 18:07:33 -0800273 Multilib apexMultilibProperties
Jiyong Park235e67c2019-02-09 11:50:56 +0900274
Jiyong Parkf97782b2019-02-13 20:28:58 +0900275 // List of sanitizer names that this APEX is enabled for
276 SanitizerNames []string `blueprint:"mutated"`
Alex Light9670d332019-01-29 18:07:33 -0800277}
278
279type apexTargetBundleProperties struct {
280 Target struct {
281 // Multilib properties only for android.
282 Android struct {
283 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900284 }
Alex Light9670d332019-01-29 18:07:33 -0800285 // Multilib properties only for host.
286 Host struct {
287 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900288 }
Alex Light9670d332019-01-29 18:07:33 -0800289 // Multilib properties only for host linux_bionic.
290 Linux_bionic struct {
291 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900292 }
Alex Light9670d332019-01-29 18:07:33 -0800293 // Multilib properties only for host linux_glibc.
294 Linux_glibc struct {
295 Multilib apexMultilibProperties
Jiyong Park397e55e2018-10-24 21:09:55 +0900296 }
297 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900298}
299
Jiyong Park8fd61922018-11-08 02:50:25 +0900300type apexFileClass int
301
302const (
303 etc apexFileClass = iota
304 nativeSharedLib
305 nativeExecutable
Jiyong Park04480cf2019-02-06 00:16:29 +0900306 shBinary
Alex Light778127a2019-02-27 14:19:50 -0800307 pyBinary
308 goBinary
Jiyong Park8fd61922018-11-08 02:50:25 +0900309 javaSharedLib
310)
311
Alex Light5098a612018-11-29 17:12:15 -0800312type apexPackaging int
313
314const (
315 imageApex apexPackaging = iota
316 zipApex
317 both
318)
319
320func (a apexPackaging) image() bool {
321 switch a {
322 case imageApex, both:
323 return true
324 }
325 return false
326}
327
328func (a apexPackaging) zip() bool {
329 switch a {
330 case zipApex, both:
331 return true
332 }
333 return false
334}
335
336func (a apexPackaging) suffix() string {
337 switch a {
338 case imageApex:
339 return imageApexSuffix
340 case zipApex:
341 return zipApexSuffix
342 case both:
343 panic(fmt.Errorf("must be either zip or image"))
344 default:
345 panic(fmt.Errorf("unkonwn APEX type %d", a))
346 }
347}
348
349func (a apexPackaging) name() string {
350 switch a {
351 case imageApex:
352 return imageApexType
353 case zipApex:
354 return zipApexType
355 case both:
356 panic(fmt.Errorf("must be either zip or image"))
357 default:
358 panic(fmt.Errorf("unkonwn APEX type %d", a))
359 }
360}
361
Jiyong Park8fd61922018-11-08 02:50:25 +0900362func (class apexFileClass) NameInMake() string {
363 switch class {
364 case etc:
365 return "ETC"
366 case nativeSharedLib:
367 return "SHARED_LIBRARIES"
Alex Light778127a2019-02-27 14:19:50 -0800368 case nativeExecutable, shBinary, pyBinary, goBinary:
Jiyong Park8fd61922018-11-08 02:50:25 +0900369 return "EXECUTABLES"
370 case javaSharedLib:
371 return "JAVA_LIBRARIES"
372 default:
373 panic(fmt.Errorf("unkonwn class %d", class))
374 }
375}
376
377type apexFile struct {
378 builtFile android.Path
379 moduleName string
Jiyong Park8fd61922018-11-08 02:50:25 +0900380 installDir string
381 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900382 module android.Module
Alex Light3d673592019-01-18 14:37:31 -0800383 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900384}
385
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900386type apexBundle struct {
387 android.ModuleBase
388 android.DefaultableModuleBase
389
Alex Light9670d332019-01-29 18:07:33 -0800390 properties apexBundleProperties
391 targetProperties apexTargetBundleProperties
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900392
Alex Light5098a612018-11-29 17:12:15 -0800393 apexTypes apexPackaging
394
Colin Crossa4925902018-11-16 11:36:28 -0800395 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800396 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800397 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900398
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900399 public_key_file android.Path
400 private_key_file android.Path
401 bundle_public_key bool
402
403 container_certificate_file android.Path
404 container_private_key_file android.Path
405
Jiyong Park52818fc2019-03-18 12:01:38 +0900406 mergedNoticeFile android.WritablePath
407
Jiyong Park8fd61922018-11-08 02:50:25 +0900408 // list of files to be included in this apex
409 filesInfo []apexFile
410
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900411 // list of module names that this APEX is depending on
412 externalDeps []string
413
Jiyong Park8fd61922018-11-08 02:50:25 +0900414 flattened bool
Alex Light0851b882019-02-07 13:20:53 -0800415
416 testApex bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900417}
418
Jiyong Park397e55e2018-10-24 21:09:55 +0900419func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900420 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900421 // Use *FarVariation* to be able to depend on modules having
422 // conflicting variations with this module. This is required since
423 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
424 // for native shared libs.
425 ctx.AddFarVariationDependencies([]blueprint.Variation{
426 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900427 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900428 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900429 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900430 }, sharedLibTag, native_shared_libs...)
431
432 ctx.AddFarVariationDependencies([]blueprint.Variation{
433 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900434 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900435 }, executableTag, binaries...)
436}
437
Alex Light9670d332019-01-29 18:07:33 -0800438func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
439 if ctx.Os().Class == android.Device {
440 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
441 } else {
442 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
443 if ctx.Os().Bionic() {
444 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
445 } else {
446 proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
447 }
448 }
449}
450
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900451func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Alex Light9670d332019-01-29 18:07:33 -0800452
Jiyong Park397e55e2018-10-24 21:09:55 +0900453 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900454 config := ctx.DeviceConfig()
Alex Light9670d332019-01-29 18:07:33 -0800455
456 a.combineProperties(ctx)
457
Jiyong Park397e55e2018-10-24 21:09:55 +0900458 has32BitTarget := false
459 for _, target := range targets {
460 if target.Arch.ArchType.Multilib == "lib32" {
461 has32BitTarget = true
462 }
463 }
464 for i, target := range targets {
465 // When multilib.* is omitted for native_shared_libs, it implies
466 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900467 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900468 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900469 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900470 {Mutator: "link", Variation: "shared"},
471 }, sharedLibTag, a.properties.Native_shared_libs...)
472
Jiyong Park397e55e2018-10-24 21:09:55 +0900473 // Add native modules targetting both ABIs
474 addDependenciesForNativeModules(ctx,
475 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900477 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900478
Alex Light3d673592019-01-18 14:37:31 -0800479 isPrimaryAbi := i == 0
480 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900481 // When multilib.* is omitted for binaries, it implies
482 // multilib.first.
483 ctx.AddFarVariationDependencies([]blueprint.Variation{
484 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900485 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900486 }, executableTag, a.properties.Binaries...)
487
488 // Add native modules targetting the first ABI
489 addDependenciesForNativeModules(ctx,
490 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900491 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900492 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800493
494 // When multilib.* is omitted for prebuilts, it implies multilib.first.
495 ctx.AddFarVariationDependencies([]blueprint.Variation{
496 {Mutator: "arch", Variation: target.String()},
497 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900498 }
499
500 switch target.Arch.ArchType.Multilib {
501 case "lib32":
502 // Add native modules targetting 32-bit ABI
503 addDependenciesForNativeModules(ctx,
504 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900505 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900506 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900507
508 addDependenciesForNativeModules(ctx,
509 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900510 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900511 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900512 case "lib64":
513 // Add native modules targetting 64-bit ABI
514 addDependenciesForNativeModules(ctx,
515 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900516 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900517 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900518
519 if !has32BitTarget {
520 addDependenciesForNativeModules(ctx,
521 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900522 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900523 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900524 }
525 }
526
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900527 }
528
Jiyong Parkff1458f2018-10-12 21:49:38 +0900529 ctx.AddFarVariationDependencies([]blueprint.Variation{
530 {Mutator: "arch", Variation: "android_common"},
531 }, javaLibTag, a.properties.Java_libs...)
532
Jiyong Park23c52b02019-02-02 13:13:47 +0900533 if String(a.properties.Key) == "" {
534 ctx.ModuleErrorf("key is missing")
535 return
536 }
537 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900538
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900539 cert := android.SrcIsModule(a.getCertString(ctx))
Jiyong Park23c52b02019-02-02 13:13:47 +0900540 if cert != "" {
541 ctx.AddDependency(ctx.Module(), certificateTag, cert)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900542 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900543}
544
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900545func (a *apexBundle) getCertString(ctx android.BaseContext) string {
546 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
547 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000548 return ":" + certificate
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900549 }
550 return String(a.properties.Certificate)
551}
552
Jiyong Park74e240b2018-11-27 21:27:08 +0900553func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900554 if file, ok := a.outputFiles[imageApex]; ok {
555 return android.Paths{file}
556 } else {
557 return nil
558 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900559}
560
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900561func (a *apexBundle) installable() bool {
562 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
563}
564
Jiyong Park7c1dc612019-01-05 11:15:24 +0900565func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
566 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900567 return "vendor"
568 } else {
569 return "core"
570 }
571}
572
Jiyong Parkf97782b2019-02-13 20:28:58 +0900573func (a *apexBundle) EnableSanitizer(sanitizerName string) {
574 if !android.InList(sanitizerName, a.properties.SanitizerNames) {
575 a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
576 }
577}
578
Jiyong Park388ef3f2019-01-28 19:47:32 +0900579func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
Jiyong Parkf97782b2019-02-13 20:28:58 +0900580 if android.InList(sanitizerName, a.properties.SanitizerNames) {
581 return true
Jiyong Park235e67c2019-02-09 11:50:56 +0900582 }
583
584 // Then follow the global setting
Jiyong Park388ef3f2019-01-28 19:47:32 +0900585 globalSanitizerNames := []string{}
586 if a.Host() {
587 globalSanitizerNames = ctx.Config().SanitizeHost()
588 } else {
589 arches := ctx.Config().SanitizeDeviceArch()
590 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
591 globalSanitizerNames = ctx.Config().SanitizeDevice()
592 }
593 }
594 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900595}
596
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800597func getCopyManifestForNativeLibrary(cc *cc.Module, handleSpecialLibs bool) (fileToCopy android.Path, dirInApex string) {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900598 // Decide the APEX-local directory by the multilib of the library
599 // In the future, we may query this to the module.
600 switch cc.Arch().ArchType.Multilib {
601 case "lib32":
602 dirInApex = "lib"
603 case "lib64":
604 dirInApex = "lib64"
605 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900606 dirInApex = filepath.Join(dirInApex, cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900607 if !cc.Arch().Native {
608 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
609 }
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800610 if handleSpecialLibs {
611 switch cc.Name() {
612 case "libc", "libm", "libdl":
613 // Special case for bionic libs. This is to prevent the bionic libs
614 // from being included in the search path /apex/com.android.apex/lib.
615 // This exclusion is required because bionic libs in the runtime APEX
616 // are available via the legacy paths /system/lib/libc.so, etc. By the
617 // init process, the bionic libs in the APEX are bind-mounted to the
618 // legacy paths and thus will be loaded into the default linker namespace.
619 // If the bionic libs are directly in /apex/com.android.apex/lib then
620 // the same libs will be again loaded to the runtime linker namespace,
621 // which will result double loading of bionic libs that isn't supported.
622 dirInApex = filepath.Join(dirInApex, "bionic")
623 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900624 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900625
626 fileToCopy = cc.OutputFile().Path()
627 return
628}
629
630func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
Jiyong Parkbd13e442019-03-15 18:10:35 +0900631 dirInApex = filepath.Join("bin", cc.RelativeInstallPath())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900632 fileToCopy = cc.OutputFile().Path()
633 return
634}
635
Alex Light778127a2019-02-27 14:19:50 -0800636func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
637 dirInApex = "bin"
638 fileToCopy = py.HostToolPath().Path()
639 return
640}
641func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
642 dirInApex = "bin"
643 s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
644 if err != nil {
645 ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
646 return
647 }
648 fileToCopy = android.PathForOutput(ctx, s)
649 return
650}
651
Jiyong Park04480cf2019-02-06 00:16:29 +0900652func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
653 dirInApex = filepath.Join("bin", sh.SubDir())
654 fileToCopy = sh.OutputFile()
655 return
656}
657
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900658func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
659 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900660 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900661 return
662}
663
664func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
665 dirInApex = filepath.Join("etc", prebuilt.SubDir())
666 fileToCopy = prebuilt.OutputFile()
667 return
668}
669
670func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900671 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900672
Alex Light5098a612018-11-29 17:12:15 -0800673 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
674 a.apexTypes = imageApex
675 } else if *a.properties.Payload_type == "zip" {
676 a.apexTypes = zipApex
677 } else if *a.properties.Payload_type == "both" {
678 a.apexTypes = both
679 } else {
680 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
681 return
682 }
683
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800684 handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
685
Alex Light778127a2019-02-27 14:19:50 -0800686 ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900687 if _, ok := parent.(*apexBundle); ok {
688 // direct dependencies
689 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900690 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900691 switch depTag {
692 case sharedLibTag:
693 if cc, ok := child.(*cc.Module); ok {
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800694 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900695 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900696 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900697 } else {
698 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900699 }
700 case executableTag:
701 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800702 if !cc.Arch().Native {
703 // There is only one 'bin' directory so we shouldn't bother copying in
704 // native-bridge'd binaries and only use main ones.
705 return true
706 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900707 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Park719b4462019-01-13 00:39:51 +0900708 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900709 return true
Jiyong Park04480cf2019-02-06 00:16:29 +0900710 } else if sh, ok := child.(*android.ShBinary); ok {
711 fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
712 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
Alex Light778127a2019-02-27 14:19:50 -0800713 } else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
714 fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
715 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
716 } else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
717 fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
718 // NB: Since go binaries are static we don't need the module for anything here, which is
719 // good since the go tool is a blueprint.Module not an android.Module like we would
720 // normally use.
721 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
Jiyong Parkff1458f2018-10-12 21:49:38 +0900722 } else {
Alex Light778127a2019-02-27 14:19:50 -0800723 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 +0900724 }
725 case javaLibTag:
726 if java, ok := child.(*java.Library); ok {
727 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900728 if fileToCopy == nil {
729 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
730 } else {
Jiyong Park719b4462019-01-13 00:39:51 +0900731 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900732 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900733 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900734 } else {
735 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900736 }
737 case prebuiltTag:
738 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
739 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Park719b4462019-01-13 00:39:51 +0900740 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900741 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900742 } else {
743 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
744 }
745 case keyTag:
746 if key, ok := child.(*apexKey); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900747 a.private_key_file = key.private_key_file
748 a.public_key_file = key.public_key_file
749 // If the key is not installed, bundled it with the APEX.
750 // Note: this bundled key is valid only for non-production builds
751 // (eng/userdebug).
752 a.bundle_public_key = !key.installable() && ctx.Config().Debuggable()
Jiyong Parkff1458f2018-10-12 21:49:38 +0900753 return false
754 } else {
755 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900756 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900757 case certificateTag:
758 if dep, ok := child.(*java.AndroidAppCertificate); ok {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900759 a.container_certificate_file = dep.Certificate.Pem
760 a.container_private_key_file = dep.Certificate.Key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900761 return false
762 } else {
763 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
764 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900765 }
766 } else {
767 // indirect dependencies
768 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
769 if cc, ok := child.(*cc.Module); ok {
Alex Light49ae3d92019-02-21 14:02:46 -0800770 if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900771 // If the dependency is a stubs lib, don't include it in this APEX,
772 // but make sure that the lib is installed on the device.
773 // In case no APEX is having the lib, the lib is installed to the system
774 // partition.
Alex Light49ae3d92019-02-21 14:02:46 -0800775 //
776 // Always include if we are a host-apex however since those won't have any
777 // system libraries.
Jiyong Parkac2bacd2019-02-20 21:49:26 +0900778 if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
779 a.externalDeps = append(a.externalDeps, cc.Name())
780 }
781 // Don't track further
Jiyong Park25fc6a92018-11-18 18:02:45 +0900782 return false
783 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900784 depName := ctx.OtherModuleName(child)
Alex Lightfc0bd7c2019-01-29 18:31:59 -0800785 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc, handleSpecialLibs)
Jiyong Park719b4462019-01-13 00:39:51 +0900786 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900787 return true
788 }
789 }
790 }
791 return false
792 })
793
Jiyong Park9335a262018-12-24 11:31:58 +0900794 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900795 if a.private_key_file == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900796 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
797 return
798 }
799
Jiyong Park8fd61922018-11-08 02:50:25 +0900800 // remove duplicates in filesInfo
801 removeDup := func(filesInfo []apexFile) []apexFile {
802 encountered := make(map[android.Path]bool)
803 result := []apexFile{}
804 for _, f := range filesInfo {
805 if !encountered[f.builtFile] {
806 encountered[f.builtFile] = true
807 result = append(result, f)
808 }
809 }
810 return result
811 }
812 filesInfo = removeDup(filesInfo)
813
814 // to have consistent build rules
815 sort.Slice(filesInfo, func(i, j int) bool {
816 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
817 })
818
819 // prepend the name of this APEX to the module names. These names will be the names of
820 // modules that will be defined if the APEX is flattened.
821 for i := range filesInfo {
822 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
823 }
824
Jiyong Park8fd61922018-11-08 02:50:25 +0900825 a.installDir = android.PathForModuleInstall(ctx, "apex")
826 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800827
Jiyong Park52818fc2019-03-18 12:01:38 +0900828 a.buildNoticeFile(ctx)
829
Alex Light5098a612018-11-29 17:12:15 -0800830 if a.apexTypes.zip() {
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900831 a.buildUnflattenedApex(ctx, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833 if a.apexTypes.image() {
Jiyong Park23c52b02019-02-02 13:13:47 +0900834 // Build rule for unflattened APEX is created even when ctx.Config().FlattenApex()
835 // is true. This is to support referencing APEX via ":<module_name" syntax
836 // in other modules. It is in AndroidMk where the selection of flattened
837 // or unflattened APEX is made.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900838 a.buildUnflattenedApex(ctx, imageApex)
Jiyong Park23c52b02019-02-02 13:13:47 +0900839 a.buildFlattenedApex(ctx)
Jiyong Park8fd61922018-11-08 02:50:25 +0900840 }
841}
842
Jiyong Park52818fc2019-03-18 12:01:38 +0900843func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext) {
844 noticeFiles := []android.Path{}
845 noticeFilesString := []string{}
846 for _, f := range a.filesInfo {
847 if f.module != nil {
848 notice := f.module.NoticeFile()
849 if notice.Valid() {
850 noticeFiles = append(noticeFiles, notice.Path())
851 noticeFilesString = append(noticeFilesString, notice.Path().String())
852 }
853 }
854 }
855 // append the notice file specified in the apex module itself
856 if a.NoticeFile().Valid() {
857 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
858 noticeFilesString = append(noticeFilesString, a.NoticeFile().Path().String())
859 }
860
861 if len(noticeFiles) > 0 {
862 a.mergedNoticeFile = android.PathForModuleOut(ctx, "NOTICE")
863 ctx.Build(pctx, android.BuildParams{
864 Rule: apexMergeNoticeRule,
865 Inputs: noticeFiles,
866 Output: a.mergedNoticeFile,
867 Args: map[string]string{
868 "inputs": strings.Join(noticeFilesString, " "),
869 },
870 })
871 }
872}
873
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900874func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900875 cert := String(a.properties.Certificate)
876 if cert != "" && android.SrcIsModule(cert) == "" {
877 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900878 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
879 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900880 } else if cert == "" {
881 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900882 a.container_certificate_file = pem
883 a.container_private_key_file = key
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900884 }
885
Colin Cross8a497952019-03-05 22:25:09 -0800886 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900887
Alex Light5098a612018-11-29 17:12:15 -0800888 var abis []string
889 for _, target := range ctx.MultiTargets() {
890 if len(target.Arch.Abi) > 0 {
891 abis = append(abis, target.Arch.Abi[0])
892 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900893 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900894
Alex Light5098a612018-11-29 17:12:15 -0800895 abis = android.FirstUniqueStrings(abis)
896
897 suffix := apexType.suffix()
898 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900899
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900900 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900901 for _, f := range a.filesInfo {
902 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900903 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900904
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900905 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900906 for i, src := range filesToCopy {
907 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800908 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900909 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
910 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800911 for _, sym := range a.filesInfo[i].symlinks {
912 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
913 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
914 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900915 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900916 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800917 implicitInputs = append(implicitInputs, manifest)
918
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900919 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
920 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900921
Alex Light5098a612018-11-29 17:12:15 -0800922 if apexType.image() {
923 // files and dirs that will be created in APEX
924 var readOnlyPaths []string
925 var executablePaths []string // this also includes dirs
926 for _, f := range a.filesInfo {
927 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
928 if f.installDir == "bin" {
929 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800930 for _, s := range f.symlinks {
931 executablePaths = append(executablePaths, filepath.Join("bin", s))
932 }
Alex Light5098a612018-11-29 17:12:15 -0800933 } else {
934 readOnlyPaths = append(readOnlyPaths, pathInApex)
935 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900936 dir := f.installDir
937 for !android.InList(dir, executablePaths) && dir != "" {
938 executablePaths = append(executablePaths, dir)
939 dir, _ = filepath.Split(dir) // move up to the parent
940 if len(dir) > 0 {
941 // remove trailing slash
942 dir = dir[:len(dir)-1]
943 }
Alex Light5098a612018-11-29 17:12:15 -0800944 }
945 }
946 sort.Strings(readOnlyPaths)
947 sort.Strings(executablePaths)
948 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
949 ctx.Build(pctx, android.BuildParams{
950 Rule: generateFsConfig,
951 Output: cannedFsConfig,
952 Description: "generate fs config",
953 Args: map[string]string{
954 "ro_paths": strings.Join(readOnlyPaths, " "),
955 "exec_paths": strings.Join(executablePaths, " "),
956 },
957 })
958
959 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
960 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
961 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
962 if !fileContextsOptionalPath.Valid() {
963 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
964 return
965 }
966 fileContexts := fileContextsOptionalPath.Path()
967
Jiyong Park835d82b2018-12-27 16:04:18 +0900968 optFlags := []string{}
969
Alex Light5098a612018-11-29 17:12:15 -0800970 // Additional implicit inputs.
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900971 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, a.private_key_file)
972 if a.bundle_public_key {
973 implicitInputs = append(implicitInputs, a.public_key_file)
974 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
Jiyong Park835d82b2018-12-27 16:04:18 +0900975 }
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
Alex Light5098a612018-11-29 17:12:15 -0800988 ctx.Build(pctx, android.BuildParams{
989 Rule: apexRule,
990 Implicits: implicitInputs,
991 Output: unsignedOutputFile,
992 Description: "apex (" + apexType.name() + ")",
993 Args: map[string]string{
994 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
995 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
996 "copy_commands": strings.Join(copyCommands, " && "),
997 "manifest": manifest.String(),
998 "file_contexts": fileContexts.String(),
999 "canned_fs_config": cannedFsConfig.String(),
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001000 "key": a.private_key_file.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +09001001 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -08001002 },
1003 })
1004
1005 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
1006 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
1007 a.bundleModuleFile = bundleModuleFile
1008
1009 ctx.Build(pctx, android.BuildParams{
1010 Rule: apexProtoConvertRule,
1011 Input: unsignedOutputFile,
1012 Output: apexProtoFile,
1013 Description: "apex proto convert",
1014 })
1015
1016 ctx.Build(pctx, android.BuildParams{
1017 Rule: apexBundleRule,
1018 Input: apexProtoFile,
1019 Output: a.bundleModuleFile,
1020 Description: "apex bundle module",
1021 Args: map[string]string{
1022 "abi": strings.Join(abis, "."),
1023 },
1024 })
1025 } else {
1026 ctx.Build(pctx, android.BuildParams{
1027 Rule: zipApexRule,
1028 Implicits: implicitInputs,
1029 Output: unsignedOutputFile,
1030 Description: "apex (" + apexType.name() + ")",
1031 Args: map[string]string{
1032 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
1033 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
1034 "copy_commands": strings.Join(copyCommands, " && "),
1035 "manifest": manifest.String(),
1036 },
1037 })
Colin Crossa4925902018-11-16 11:36:28 -08001038 }
Colin Crossa4925902018-11-16 11:36:28 -08001039
Alex Light5098a612018-11-29 17:12:15 -08001040 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001041 ctx.Build(pctx, android.BuildParams{
1042 Rule: java.Signapk,
1043 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -08001044 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001045 Input: unsignedOutputFile,
1046 Args: map[string]string{
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001047 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
Jiyong Parkbfe64a12018-11-22 02:51:54 +09001048 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001049 },
1050 })
Alex Light5098a612018-11-29 17:12:15 -08001051
1052 // Install to $OUT/soong/{target,host}/.../apex
Alex Light2a2561f2019-02-12 16:59:09 -08001053 if a.installable() && (!ctx.Config().FlattenApex() || apexType.zip()) {
Jiyong Park0ca3ce82019-02-18 15:25:04 +09001054 ctx.InstallFile(a.installDir, ctx.ModuleName()+suffix, a.outputFiles[apexType])
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001055 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001056}
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001057
Jiyong Park8fd61922018-11-08 02:50:25 +09001058func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001059 if a.installable() {
1060 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
1061 // with other ordinary files.
Colin Cross8a497952019-03-05 22:25:09 -08001062 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +09001063
1064 // rename to apex_manifest.json
1065 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
1066 ctx.Build(pctx, android.BuildParams{
1067 Rule: android.Cp,
1068 Input: manifest,
1069 Output: copiedManifest,
1070 })
Jiyong Park719b4462019-01-13 00:39:51 +09001071 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +09001072
Jiyong Park23c52b02019-02-02 13:13:47 +09001073 if ctx.Config().FlattenApex() {
1074 for _, fi := range a.filesInfo {
1075 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
Alex Lightf4857cf2019-02-22 13:00:04 -08001076 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
1077 for _, sym := range fi.symlinks {
1078 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
1079 }
Jiyong Park23c52b02019-02-02 13:13:47 +09001080 }
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001081 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001082 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001083}
1084
1085func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -08001086 writers := []android.AndroidMkData{}
1087 if a.apexTypes.image() {
1088 writers = append(writers, a.androidMkForType(imageApex))
1089 }
1090 if a.apexTypes.zip() {
1091 writers = append(writers, a.androidMkForType(zipApex))
1092 }
1093 return android.AndroidMkData{
1094 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1095 for _, data := range writers {
1096 data.Custom(w, name, prefix, moduleDir, data)
1097 }
1098 }}
1099}
1100
Alex Lightf1801bc2019-02-13 11:10:07 -08001101func (a *apexBundle) androidMkForFiles(w io.Writer, name, moduleDir string, apexType apexPackaging) []string {
Jiyong Park94427262019-02-05 23:18:47 +09001102 moduleNames := []string{}
1103
1104 for _, fi := range a.filesInfo {
1105 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
1106 continue
1107 }
1108 if !android.InList(fi.moduleName, moduleNames) {
1109 moduleNames = append(moduleNames, fi.moduleName)
1110 }
1111 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1112 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1113 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
Jiyong Park05e70dd2019-03-18 14:26:32 +09001114 // /apex/<name>/{lib|framework|...}
1115 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex",
1116 proptools.StringDefault(a.properties.Apex_name, name), fi.installDir)
Alex Lightf1801bc2019-02-13 11:10:07 -08001117 if a.flattened && apexType.image() {
Jiyong Park94427262019-02-05 23:18:47 +09001118 // /system/apex/<name>/{lib|framework|...}
1119 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
1120 a.installDir.RelPathString(), name, fi.installDir))
Jiyong Park05e70dd2019-03-18 14:26:32 +09001121 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
Alex Lightf4857cf2019-02-22 13:00:04 -08001122 if len(fi.symlinks) > 0 {
1123 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
1124 }
Jiyong Park52818fc2019-03-18 12:01:38 +09001125
1126 if fi.module != nil && fi.module.NoticeFile().Valid() {
1127 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
1128 }
Jiyong Park94427262019-02-05 23:18:47 +09001129 } else {
Jiyong Park05e70dd2019-03-18 14:26:32 +09001130 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park94427262019-02-05 23:18:47 +09001131 }
1132 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
1133 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
1134 if fi.module != nil {
1135 archStr := fi.module.Target().Arch.ArchType.String()
1136 host := false
1137 switch fi.module.Target().Os.Class {
1138 case android.Host:
1139 if archStr != "common" {
1140 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
1141 }
1142 host = true
1143 case android.HostCross:
1144 if archStr != "common" {
1145 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
1146 }
1147 host = true
1148 case android.Device:
1149 if archStr != "common" {
1150 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
1151 }
1152 }
1153 if host {
1154 makeOs := fi.module.Target().Os.String()
1155 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
1156 makeOs = "linux"
1157 }
1158 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
1159 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
1160 }
1161 }
1162 if fi.class == javaSharedLib {
1163 javaModule := fi.module.(*java.Library)
1164 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
1165 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
1166 // we will have foo.jar.jar
1167 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
1168 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
1169 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
1170 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
1171 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
1172 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
1173 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable {
1174 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1175 if cc, ok := fi.module.(*cc.Module); ok && cc.UnstrippedOutputFile() != nil {
1176 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
1177 }
1178 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
1179 } else {
1180 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
1181 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
1182 }
1183 }
1184 return moduleNames
1185}
1186
Alex Light5098a612018-11-29 17:12:15 -08001187func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Jiyong Park719b4462019-01-13 00:39:51 +09001188 return android.AndroidMkData{
1189 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
1190 moduleNames := []string{}
Jiyong Park94427262019-02-05 23:18:47 +09001191 if a.installable() {
Alex Lightf1801bc2019-02-13 11:10:07 -08001192 moduleNames = a.androidMkForFiles(w, name, moduleDir, apexType)
Jiyong Park719b4462019-01-13 00:39:51 +09001193 }
1194
Jiyong Park719b4462019-01-13 00:39:51 +09001195 if a.flattened && apexType.image() {
1196 // Only image APEXes can be flattened.
Jiyong Park8fd61922018-11-08 02:50:25 +09001197 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1198 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1199 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Jiyong Park94427262019-02-05 23:18:47 +09001200 if len(moduleNames) > 0 {
1201 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
1202 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001203 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park719b4462019-01-13 00:39:51 +09001204 } else {
Alex Light5098a612018-11-29 17:12:15 -08001205 // zip-apex is the less common type so have the name refer to the image-apex
1206 // only and use {name}.zip if you want the zip-apex
1207 if apexType == zipApex && a.apexTypes == both {
1208 name = name + ".zip"
1209 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001210 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
1211 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
1212 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
1213 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -08001214 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +09001215 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -08001216 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +09001217 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +09001218 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park52818fc2019-03-18 12:01:38 +09001219 if a.installable() && a.mergedNoticeFile != nil {
1220 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNoticeFile.String())
1221 }
Jiyong Park94427262019-02-05 23:18:47 +09001222 if len(moduleNames) > 0 {
1223 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
1224 }
Jiyong Parkac2bacd2019-02-20 21:49:26 +09001225 if len(a.externalDeps) > 0 {
1226 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
1227 }
Jiyong Park8fd61922018-11-08 02:50:25 +09001228 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -08001229
Alex Light5098a612018-11-29 17:12:15 -08001230 if apexType == imageApex {
1231 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
1232 }
Jiyong Park719b4462019-01-13 00:39:51 +09001233 }
1234 }}
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001235}
1236
Alex Light0851b882019-02-07 13:20:53 -08001237func testApexBundleFactory() android.Module {
1238 return ApexBundleFactory( /*testApex*/ true)
1239}
1240
1241func apexBundleFactory() android.Module {
1242 return ApexBundleFactory( /*testApex*/ false)
1243}
1244
1245func ApexBundleFactory(testApex bool) android.Module {
Alex Light5098a612018-11-29 17:12:15 -08001246 module := &apexBundle{
1247 outputFiles: map[apexPackaging]android.WritablePath{},
Alex Light0851b882019-02-07 13:20:53 -08001248 testApex: testApex,
Alex Light5098a612018-11-29 17:12:15 -08001249 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001250 module.AddProperties(&module.properties)
Alex Light9670d332019-01-29 18:07:33 -08001251 module.AddProperties(&module.targetProperties)
Alex Light5098a612018-11-29 17:12:15 -08001252 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +09001253 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
1254 })
Alex Light5098a612018-11-29 17:12:15 -08001255 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001256 android.InitDefaultableModule(module)
1257 return module
1258}
Jiyong Park30ca9372019-02-07 16:27:23 +09001259
1260//
1261// Defaults
1262//
1263type Defaults struct {
1264 android.ModuleBase
1265 android.DefaultsModuleBase
1266}
1267
1268func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1269}
1270
1271func defaultsFactory() android.Module {
1272 return DefaultsFactory()
1273}
1274
1275func DefaultsFactory(props ...interface{}) android.Module {
1276 module := &Defaults{}
1277
1278 module.AddProperties(props...)
1279 module.AddProperties(
1280 &apexBundleProperties{},
1281 &apexTargetBundleProperties{},
1282 )
1283
1284 android.InitDefaultsModule(module)
1285 return module
1286}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001287
1288//
1289// Prebuilt APEX
1290//
1291type Prebuilt struct {
1292 android.ModuleBase
1293 prebuilt android.Prebuilt
1294
1295 properties PrebuiltProperties
1296
1297 inputApex android.Path
1298 installDir android.OutputPath
1299}
1300
1301type PrebuiltProperties struct {
1302 // the path to the prebuilt .apex file to import.
1303 Src string `android:"arch_variant"`
1304
1305 // the name of the apex_key module that contains the matching public key to be installed.
1306 Key *string
1307}
1308
1309func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
1310 if String(p.properties.Key) == "" {
1311 ctx.ModuleErrorf("key is missing")
1312 return
1313 }
1314 ctx.AddDependency(ctx.Module(), keyTag, *p.properties.Key)
1315}
1316
1317func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1318 // TODO(jungjw): Check the key validity.
1319 p.inputApex = p.prebuilt.SingleSourcePath(ctx)
1320 p.installDir = android.PathForModuleInstall(ctx, "apex")
1321 ctx.InstallFile(p.installDir, ctx.ModuleName()+imageApexSuffix, p.inputApex)
1322}
1323
1324func (p *Prebuilt) Prebuilt() *android.Prebuilt {
1325 return &p.prebuilt
1326}
1327
1328func (p *Prebuilt) Name() string {
1329 return p.prebuilt.Name(p.ModuleBase.Name())
1330}
1331
1332func (p *Prebuilt) AndroidMk() android.AndroidMkData {
1333 return android.AndroidMkData{
1334 Class: "ETC",
1335 OutputFile: android.OptionalPathForPath(p.inputApex),
1336 Include: "$(BUILD_PREBUILT)",
1337 Extra: []android.AndroidMkExtraFunc{
1338 func(w io.Writer, outputFile android.Path) {
1339 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", p.installDir.RelPathString()))
1340 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", p.BaseModuleName()+imageApexSuffix)
1341 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(p.properties.Key))
1342 },
1343 },
1344 }
1345}
1346
1347// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1348func PrebuiltFactory() android.Module {
1349 module := &Prebuilt{}
1350 module.AddProperties(&module.properties)
1351 android.InitSingleSourcePrebuiltModule(module, &module.properties.Src)
1352 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1353 return module
1354}