blob: c6d2e8e747feb5d67111f4f82c85a128f91c53db [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"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
33var (
34 pctx = android.NewPackageContext("android/apex")
35
36 // Create a canned fs config file where all files and directories are
37 // by default set to (uid/gid/mode) = (1000/1000/0644)
38 // TODO(b/113082813) make this configurable using config.fs syntax
39 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Roland Levillain2b11f742018-11-02 11:50:42 +000040 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000041 `echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
Jiyong Park92905d62018-10-11 13:23:09 +090042 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
Jiyong Park805cbc32019-01-08 14:04:17 +090043 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090044 Description: "fs_config ${out}",
Jiyong Park92905d62018-10-11 13:23:09 +090045 }, "ro_paths", "exec_paths")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090046
47 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
48 // against the binary policy using sefcontext_compiler -p <policy>.
49
50 // TODO(b/114327326): automate the generation of file_contexts
51 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
52 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
53 `(${copy_commands}) && ` +
54 `APEXER_TOOL_PATH=${tool_path} ` +
Jiyong Park25560152018-11-20 09:57:52 +090055 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090056 `--file_contexts ${file_contexts} ` +
57 `--canned_fs_config ${canned_fs_config} ` +
Alex Light5098a612018-11-29 17:12:15 -080058 `--payload_type image ` +
Jiyong Park835d82b2018-12-27 16:04:18 +090059 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
Jiyong Park48ca7dc2018-10-10 14:01:00 +090060 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
61 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
62 "${soong_zip}", "${zipalign}", "${aapt2}"},
63 Description: "APEX ${image_dir} => ${out}",
Jiyong Park835d82b2018-12-27 16:04:18 +090064 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
Colin Crossa4925902018-11-16 11:36:28 -080065
Alex Light5098a612018-11-29 17:12:15 -080066 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
67 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
68 `(${copy_commands}) && ` +
69 `APEXER_TOOL_PATH=${tool_path} ` +
70 `${apexer} --force --manifest ${manifest} ` +
71 `--payload_type zip ` +
72 `${image_dir} ${out} `,
73 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
74 Description: "ZipAPEX ${image_dir} => ${out}",
75 }, "tool_path", "image_dir", "copy_commands", "manifest")
76
Colin Crossa4925902018-11-16 11:36:28 -080077 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
78 blueprint.RuleParams{
79 Command: `${aapt2} convert --output-format proto $in -o $out`,
80 CommandDeps: []string{"${aapt2}"},
81 })
82
83 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Park1ed0fc52018-11-23 13:22:21 +090084 Command: `${zip2zip} -i $in -o $out ` +
Dario Freni4abb1dc2018-11-20 18:04:58 +000085 `apex_payload.img:apex/${abi}.img ` +
86 `apex_manifest.json:root/apex_manifest.json ` +
Shahar Amitai328b0772018-11-26 14:12:02 +000087 `AndroidManifest.xml:manifest/AndroidManifest.xml`,
Colin Crossa4925902018-11-16 11:36:28 -080088 CommandDeps: []string{"${zip2zip}"},
89 Description: "app bundle",
90 }, "abi")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090091)
92
Alex Light5098a612018-11-29 17:12:15 -080093var imageApexSuffix = ".apex"
94var zipApexSuffix = ".zipapex"
95
96var imageApexType = "image"
97var zipApexType = "zip"
Jiyong Park48ca7dc2018-10-10 14:01:00 +090098
99type dependencyTag struct {
100 blueprint.BaseDependencyTag
101 name string
102}
103
104var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900105 sharedLibTag = dependencyTag{name: "sharedLib"}
106 executableTag = dependencyTag{name: "executable"}
107 javaLibTag = dependencyTag{name: "javaLib"}
108 prebuiltTag = dependencyTag{name: "prebuilt"}
109 keyTag = dependencyTag{name: "key"}
110 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900111)
112
113func init() {
114 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900115 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900116 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +0100117 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
118 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
119 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
120 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
121 if !android.ExistentPathForSource(ctx, "frameworks/base").Valid() {
122 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
123 } else {
124 return pctx.HostBinToolPath(ctx, tool).String()
125 }
126 })
127 }
128 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900129 pctx.HostBinToolVariable("avbtool", "avbtool")
130 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
131 pctx.HostBinToolVariable("merge_zips", "merge_zips")
132 pctx.HostBinToolVariable("mke2fs", "mke2fs")
133 pctx.HostBinToolVariable("resize2fs", "resize2fs")
134 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
135 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Colin Crossa4925902018-11-16 11:36:28 -0800136 pctx.HostBinToolVariable("zip2zip", "zip2zip")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900137 pctx.HostBinToolVariable("zipalign", "zipalign")
138
Alex Lightee250722018-12-06 14:00:02 -0800139 android.RegisterModuleType("apex", ApexBundleFactory)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900140
141 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
142 ctx.TopDown("apex_deps", apexDepsMutator)
143 ctx.BottomUp("apex", apexMutator)
144 })
145}
146
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900147// Mark the direct and transitive dependencies of apex bundles so that they
148// can be built for the apex bundles.
149func apexDepsMutator(mctx android.TopDownMutatorContext) {
150 if _, ok := mctx.Module().(*apexBundle); ok {
Colin Crossa4925902018-11-16 11:36:28 -0800151 apexBundleName := mctx.ModuleName()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900152 mctx.WalkDeps(func(child, parent android.Module) bool {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900153 depName := mctx.OtherModuleName(child)
154 // If the parent is apexBundle, this child is directly depended.
155 _, directDep := parent.(*apexBundle)
156 android.UpdateApexDependency(apexBundleName, depName, directDep)
157
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900158 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900159 am.BuildForApex(apexBundleName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900160 return true
161 } else {
162 return false
163 }
164 })
165 }
166}
167
168// Create apex variations if a module is included in APEX(s).
169func apexMutator(mctx android.BottomUpMutatorContext) {
170 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900171 am.CreateApexVariations(mctx)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900172 } else if _, ok := mctx.Module().(*apexBundle); ok {
173 // apex bundle itself is mutated so that it and its modules have same
174 // apex variant.
175 apexBundleName := mctx.ModuleName()
176 mctx.CreateVariations(apexBundleName)
177 }
178}
179
180type apexBundleProperties struct {
181 // Json manifest file describing meta info of this APEX bundle. Default:
Dario Freni4abb1dc2018-11-20 18:04:58 +0000182 // "apex_manifest.json"
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900183 Manifest *string
184
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900185 // Determines the file contexts file for setting security context to each file in this APEX bundle.
186 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
187 // used.
188 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900189 File_contexts *string
190
191 // List of native shared libs that are embedded inside this APEX bundle
192 Native_shared_libs []string
193
194 // List of native executables that are embedded inside this APEX bundle
195 Binaries []string
196
197 // List of java libraries that are embedded inside this APEX bundle
198 Java_libs []string
199
200 // List of prebuilt files that are embedded inside this APEX bundle
201 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900202
203 // Name of the apex_key module that provides the private key to sign APEX
204 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900205
Alex Light5098a612018-11-29 17:12:15 -0800206 // The type of APEX to build. Controls what the APEX payload is. Either
207 // 'image', 'zip' or 'both'. Default: 'image'.
208 Payload_type *string
209
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900210 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
211 // or an android_app_certificate module name in the form ":module".
212 Certificate *string
213
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900214 // Whether this APEX is installable to one of the partitions. Default: true.
215 Installable *bool
216
Jiyong Parkda6eb592018-12-19 17:12:36 +0900217 // For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
218 // Default is false.
219 Use_vendor *bool
220
Jiyong Park397e55e2018-10-24 21:09:55 +0900221 Multilib struct {
222 First struct {
223 // List of native libraries whose compile_multilib is "first"
224 Native_shared_libs []string
225 // List of native executables whose compile_multilib is "first"
226 Binaries []string
227 }
228 Both struct {
229 // List of native libraries whose compile_multilib is "both"
230 Native_shared_libs []string
231 // List of native executables whose compile_multilib is "both"
232 Binaries []string
233 }
234 Prefer32 struct {
235 // List of native libraries whose compile_multilib is "prefer32"
236 Native_shared_libs []string
237 // List of native executables whose compile_multilib is "prefer32"
238 Binaries []string
239 }
240 Lib32 struct {
241 // List of native libraries whose compile_multilib is "32"
242 Native_shared_libs []string
243 // List of native executables whose compile_multilib is "32"
244 Binaries []string
245 }
246 Lib64 struct {
247 // List of native libraries whose compile_multilib is "64"
248 Native_shared_libs []string
249 // List of native executables whose compile_multilib is "64"
250 Binaries []string
251 }
252 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900253}
254
Jiyong Park8fd61922018-11-08 02:50:25 +0900255type apexFileClass int
256
257const (
258 etc apexFileClass = iota
259 nativeSharedLib
260 nativeExecutable
261 javaSharedLib
262)
263
Alex Light5098a612018-11-29 17:12:15 -0800264type apexPackaging int
265
266const (
267 imageApex apexPackaging = iota
268 zipApex
269 both
270)
271
272func (a apexPackaging) image() bool {
273 switch a {
274 case imageApex, both:
275 return true
276 }
277 return false
278}
279
280func (a apexPackaging) zip() bool {
281 switch a {
282 case zipApex, both:
283 return true
284 }
285 return false
286}
287
288func (a apexPackaging) suffix() string {
289 switch a {
290 case imageApex:
291 return imageApexSuffix
292 case zipApex:
293 return zipApexSuffix
294 case both:
295 panic(fmt.Errorf("must be either zip or image"))
296 default:
297 panic(fmt.Errorf("unkonwn APEX type %d", a))
298 }
299}
300
301func (a apexPackaging) name() string {
302 switch a {
303 case imageApex:
304 return imageApexType
305 case zipApex:
306 return zipApexType
307 case both:
308 panic(fmt.Errorf("must be either zip or image"))
309 default:
310 panic(fmt.Errorf("unkonwn APEX type %d", a))
311 }
312}
313
Jiyong Park8fd61922018-11-08 02:50:25 +0900314func (class apexFileClass) NameInMake() string {
315 switch class {
316 case etc:
317 return "ETC"
318 case nativeSharedLib:
319 return "SHARED_LIBRARIES"
320 case nativeExecutable:
321 return "EXECUTABLES"
322 case javaSharedLib:
323 return "JAVA_LIBRARIES"
324 default:
325 panic(fmt.Errorf("unkonwn class %d", class))
326 }
327}
328
329type apexFile struct {
330 builtFile android.Path
331 moduleName string
332 archType android.ArchType
333 installDir string
334 class apexFileClass
Jiyong Parka8894842018-12-19 17:36:39 +0900335 module android.Module
Jiyong Park8fd61922018-11-08 02:50:25 +0900336}
337
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900338type apexBundle struct {
339 android.ModuleBase
340 android.DefaultableModuleBase
341
342 properties apexBundleProperties
343
Alex Light5098a612018-11-29 17:12:15 -0800344 apexTypes apexPackaging
345
Colin Crossa4925902018-11-16 11:36:28 -0800346 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800347 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800348 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900349
350 // list of files to be included in this apex
351 filesInfo []apexFile
352
353 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900354}
355
Jiyong Park397e55e2018-10-24 21:09:55 +0900356func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900357 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900358 // Use *FarVariation* to be able to depend on modules having
359 // conflicting variations with this module. This is required since
360 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
361 // for native shared libs.
362 ctx.AddFarVariationDependencies([]blueprint.Variation{
363 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900364 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900365 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900366 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900367 }, sharedLibTag, native_shared_libs...)
368
369 ctx.AddFarVariationDependencies([]blueprint.Variation{
370 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900371 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900372 }, executableTag, binaries...)
373}
374
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900375func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900376 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900377 config := ctx.DeviceConfig()
Jiyong Park397e55e2018-10-24 21:09:55 +0900378 has32BitTarget := false
379 for _, target := range targets {
380 if target.Arch.ArchType.Multilib == "lib32" {
381 has32BitTarget = true
382 }
383 }
384 for i, target := range targets {
385 // When multilib.* is omitted for native_shared_libs, it implies
386 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900387 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900388 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900389 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900390 {Mutator: "link", Variation: "shared"},
391 }, sharedLibTag, a.properties.Native_shared_libs...)
392
Jiyong Park397e55e2018-10-24 21:09:55 +0900393 // Add native modules targetting both ABIs
394 addDependenciesForNativeModules(ctx,
395 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900396 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900397 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900398
399 if i == 0 {
400 // When multilib.* is omitted for binaries, it implies
401 // multilib.first.
402 ctx.AddFarVariationDependencies([]blueprint.Variation{
403 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900404 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900405 }, executableTag, a.properties.Binaries...)
406
407 // Add native modules targetting the first ABI
408 addDependenciesForNativeModules(ctx,
409 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900410 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900411 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900412 }
413
414 switch target.Arch.ArchType.Multilib {
415 case "lib32":
416 // Add native modules targetting 32-bit ABI
417 addDependenciesForNativeModules(ctx,
418 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900419 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900420 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900421
422 addDependenciesForNativeModules(ctx,
423 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900424 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900425 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900426 case "lib64":
427 // Add native modules targetting 64-bit ABI
428 addDependenciesForNativeModules(ctx,
429 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900430 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900431 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900432
433 if !has32BitTarget {
434 addDependenciesForNativeModules(ctx,
435 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900436 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900437 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900438 }
439 }
440
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900441 }
442
Jiyong Parkff1458f2018-10-12 21:49:38 +0900443 ctx.AddFarVariationDependencies([]blueprint.Variation{
444 {Mutator: "arch", Variation: "android_common"},
445 }, javaLibTag, a.properties.Java_libs...)
446
447 ctx.AddFarVariationDependencies([]blueprint.Variation{
448 {Mutator: "arch", Variation: "android_common"},
449 }, prebuiltTag, a.properties.Prebuilts...)
450
451 if String(a.properties.Key) == "" {
452 ctx.ModuleErrorf("key is missing")
453 return
454 }
455 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900456
457 cert := android.SrcIsModule(String(a.properties.Certificate))
458 if cert != "" {
459 ctx.AddDependency(ctx.Module(), certificateTag, cert)
460 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900461}
462
Jiyong Park74e240b2018-11-27 21:27:08 +0900463func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900464 if file, ok := a.outputFiles[imageApex]; ok {
465 return android.Paths{file}
466 } else {
467 return nil
468 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900469}
470
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900471func (a *apexBundle) installable() bool {
472 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
473}
474
Jiyong Park7c1dc612019-01-05 11:15:24 +0900475func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
476 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900477 return "vendor"
478 } else {
479 return "core"
480 }
481}
482
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900483func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
484 // Decide the APEX-local directory by the multilib of the library
485 // In the future, we may query this to the module.
486 switch cc.Arch().ArchType.Multilib {
487 case "lib32":
488 dirInApex = "lib"
489 case "lib64":
490 dirInApex = "lib64"
491 }
492 if !cc.Arch().Native {
493 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
494 }
495
496 fileToCopy = cc.OutputFile().Path()
497 return
498}
499
500func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
501 dirInApex = "bin"
502 fileToCopy = cc.OutputFile().Path()
503 return
504}
505
506func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
507 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900508 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900509 return
510}
511
512func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
513 dirInApex = filepath.Join("etc", prebuilt.SubDir())
514 fileToCopy = prebuilt.OutputFile()
515 return
516}
517
518func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900519 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900520
Jiyong Parkff1458f2018-10-12 21:49:38 +0900521 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900522 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900523 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900524
Alex Light5098a612018-11-29 17:12:15 -0800525 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
526 a.apexTypes = imageApex
527 } else if *a.properties.Payload_type == "zip" {
528 a.apexTypes = zipApex
529 } else if *a.properties.Payload_type == "both" {
530 a.apexTypes = both
531 } else {
532 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
533 return
534 }
535
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900536 ctx.WalkDeps(func(child, parent android.Module) bool {
537 if _, ok := parent.(*apexBundle); ok {
538 // direct dependencies
539 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900540 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900541 switch depTag {
542 case sharedLibTag:
543 if cc, ok := child.(*cc.Module); ok {
544 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Jiyong Parka8894842018-12-19 17:36:39 +0900545 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900546 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900547 } else {
548 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900549 }
550 case executableTag:
551 if cc, ok := child.(*cc.Module); ok {
552 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Jiyong Parka8894842018-12-19 17:36:39 +0900553 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeExecutable, cc})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900554 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900555 } else {
556 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900557 }
558 case javaLibTag:
559 if java, ok := child.(*java.Library); ok {
560 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900561 if fileToCopy == nil {
562 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
563 } else {
Jiyong Parka8894842018-12-19 17:36:39 +0900564 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, java.Arch().ArchType, dirInApex, javaSharedLib, java})
Jiyong Park8fd61922018-11-08 02:50:25 +0900565 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900566 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900567 } else {
568 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900569 }
570 case prebuiltTag:
571 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
572 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Jiyong Parka8894842018-12-19 17:36:39 +0900573 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, prebuilt.Arch().ArchType, dirInApex, etc, prebuilt})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900574 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900575 } else {
576 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
577 }
578 case keyTag:
579 if key, ok := child.(*apexKey); ok {
580 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900581 if !key.installable() && ctx.Config().Debuggable() {
582 // If the key is not installed, bundled it with the APEX.
583 // Note: this bundled key is valid only for non-production builds
584 // (eng/userdebug).
585 pubKeyFile = key.public_key_file
586 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900587 return false
588 } else {
589 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900590 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900591 case certificateTag:
592 if dep, ok := child.(*java.AndroidAppCertificate); ok {
593 certificate = dep.Certificate
594 return false
595 } else {
596 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
597 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900598 }
599 } else {
600 // indirect dependencies
601 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
602 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900603 if cc.IsStubs() || cc.HasStubsVariants() {
604 return false
605 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900606 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900607 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Jiyong Parka8894842018-12-19 17:36:39 +0900608 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900609 return true
610 }
611 }
612 }
613 return false
614 })
615
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900616 if keyFile == nil {
617 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
618 return
619 }
620
Jiyong Park8fd61922018-11-08 02:50:25 +0900621 // remove duplicates in filesInfo
622 removeDup := func(filesInfo []apexFile) []apexFile {
623 encountered := make(map[android.Path]bool)
624 result := []apexFile{}
625 for _, f := range filesInfo {
626 if !encountered[f.builtFile] {
627 encountered[f.builtFile] = true
628 result = append(result, f)
629 }
630 }
631 return result
632 }
633 filesInfo = removeDup(filesInfo)
634
635 // to have consistent build rules
636 sort.Slice(filesInfo, func(i, j int) bool {
637 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
638 })
639
640 // prepend the name of this APEX to the module names. These names will be the names of
641 // modules that will be defined if the APEX is flattened.
642 for i := range filesInfo {
643 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
644 }
645
Colin Crossa4925902018-11-16 11:36:28 -0800646 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
Jiyong Park8fd61922018-11-08 02:50:25 +0900647 a.installDir = android.PathForModuleInstall(ctx, "apex")
648 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800649
650 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900651 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800652 }
653 if a.apexTypes.image() {
654 if ctx.Config().FlattenApex() {
655 a.buildFlattenedApex(ctx)
656 } else {
Jiyong Park835d82b2018-12-27 16:04:18 +0900657 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
Alex Light5098a612018-11-29 17:12:15 -0800658 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900659 }
660}
661
Jiyong Park835d82b2018-12-27 16:04:18 +0900662func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
663 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900664 cert := String(a.properties.Certificate)
665 if cert != "" && android.SrcIsModule(cert) == "" {
666 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
667 certificate = java.Certificate{
668 defaultDir.Join(ctx, cert+".x509.pem"),
669 defaultDir.Join(ctx, cert+".pk8"),
670 }
671 } else if cert == "" {
672 pem, key := ctx.Config().DefaultAppCertificate(ctx)
673 certificate = java.Certificate{pem, key}
674 }
675
Dario Freni4abb1dc2018-11-20 18:04:58 +0000676 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900677
Alex Light5098a612018-11-29 17:12:15 -0800678 var abis []string
679 for _, target := range ctx.MultiTargets() {
680 if len(target.Arch.Abi) > 0 {
681 abis = append(abis, target.Arch.Abi[0])
682 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900683 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900684
Alex Light5098a612018-11-29 17:12:15 -0800685 abis = android.FirstUniqueStrings(abis)
686
687 suffix := apexType.suffix()
688 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900689
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900690 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900691 for _, f := range a.filesInfo {
692 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900693 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900694
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900695 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900696 for i, src := range filesToCopy {
697 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800698 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900699 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
700 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
701 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900702 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800703 implicitInputs = append(implicitInputs, manifest)
704
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900705 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
706 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900707
Alex Light5098a612018-11-29 17:12:15 -0800708 if apexType.image() {
709 // files and dirs that will be created in APEX
710 var readOnlyPaths []string
711 var executablePaths []string // this also includes dirs
712 for _, f := range a.filesInfo {
713 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
714 if f.installDir == "bin" {
715 executablePaths = append(executablePaths, pathInApex)
716 } else {
717 readOnlyPaths = append(readOnlyPaths, pathInApex)
718 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900719 dir := f.installDir
720 for !android.InList(dir, executablePaths) && dir != "" {
721 executablePaths = append(executablePaths, dir)
722 dir, _ = filepath.Split(dir) // move up to the parent
723 if len(dir) > 0 {
724 // remove trailing slash
725 dir = dir[:len(dir)-1]
726 }
Alex Light5098a612018-11-29 17:12:15 -0800727 }
728 }
729 sort.Strings(readOnlyPaths)
730 sort.Strings(executablePaths)
731 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
732 ctx.Build(pctx, android.BuildParams{
733 Rule: generateFsConfig,
734 Output: cannedFsConfig,
735 Description: "generate fs config",
736 Args: map[string]string{
737 "ro_paths": strings.Join(readOnlyPaths, " "),
738 "exec_paths": strings.Join(executablePaths, " "),
739 },
740 })
741
742 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
743 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
744 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
745 if !fileContextsOptionalPath.Valid() {
746 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
747 return
748 }
749 fileContexts := fileContextsOptionalPath.Path()
750
Jiyong Park835d82b2018-12-27 16:04:18 +0900751 optFlags := []string{}
752
Alex Light5098a612018-11-29 17:12:15 -0800753 // Additional implicit inputs.
754 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900755 if pubKeyFile != nil {
756 implicitInputs = append(implicitInputs, pubKeyFile)
757 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
758 }
Alex Light5098a612018-11-29 17:12:15 -0800759
760 ctx.Build(pctx, android.BuildParams{
761 Rule: apexRule,
762 Implicits: implicitInputs,
763 Output: unsignedOutputFile,
764 Description: "apex (" + apexType.name() + ")",
765 Args: map[string]string{
766 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
767 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
768 "copy_commands": strings.Join(copyCommands, " && "),
769 "manifest": manifest.String(),
770 "file_contexts": fileContexts.String(),
771 "canned_fs_config": cannedFsConfig.String(),
772 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900773 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800774 },
775 })
776
777 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
778 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
779 a.bundleModuleFile = bundleModuleFile
780
781 ctx.Build(pctx, android.BuildParams{
782 Rule: apexProtoConvertRule,
783 Input: unsignedOutputFile,
784 Output: apexProtoFile,
785 Description: "apex proto convert",
786 })
787
788 ctx.Build(pctx, android.BuildParams{
789 Rule: apexBundleRule,
790 Input: apexProtoFile,
791 Output: a.bundleModuleFile,
792 Description: "apex bundle module",
793 Args: map[string]string{
794 "abi": strings.Join(abis, "."),
795 },
796 })
797 } else {
798 ctx.Build(pctx, android.BuildParams{
799 Rule: zipApexRule,
800 Implicits: implicitInputs,
801 Output: unsignedOutputFile,
802 Description: "apex (" + apexType.name() + ")",
803 Args: map[string]string{
804 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
805 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
806 "copy_commands": strings.Join(copyCommands, " && "),
807 "manifest": manifest.String(),
808 },
809 })
Colin Crossa4925902018-11-16 11:36:28 -0800810 }
Colin Crossa4925902018-11-16 11:36:28 -0800811
Alex Light5098a612018-11-29 17:12:15 -0800812 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900813 ctx.Build(pctx, android.BuildParams{
814 Rule: java.Signapk,
815 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800816 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900817 Input: unsignedOutputFile,
818 Args: map[string]string{
819 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900820 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900821 },
822 })
Alex Light5098a612018-11-29 17:12:15 -0800823
824 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900825 if a.installable() {
826 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
827 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900828}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900829
Jiyong Park8fd61922018-11-08 02:50:25 +0900830func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900831 if a.installable() {
832 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
833 // with other ordinary files.
834 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parka8894842018-12-19 17:36:39 +0900835 a.filesInfo = append(a.filesInfo, apexFile{manifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900836
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900837 for _, fi := range a.filesInfo {
838 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
839 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
840 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900841 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900842}
843
844func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800845 writers := []android.AndroidMkData{}
846 if a.apexTypes.image() {
847 writers = append(writers, a.androidMkForType(imageApex))
848 }
849 if a.apexTypes.zip() {
850 writers = append(writers, a.androidMkForType(zipApex))
851 }
852 return android.AndroidMkData{
853 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
854 for _, data := range writers {
855 data.Custom(w, name, prefix, moduleDir, data)
856 }
857 }}
858}
859
860func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
861 // Only image APEXes can be flattened.
862 if a.flattened && apexType.image() {
Jiyong Park8fd61922018-11-08 02:50:25 +0900863 return android.AndroidMkData{
864 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
865 moduleNames := []string{}
866 for _, fi := range a.filesInfo {
867 if !android.InList(fi.moduleName, moduleNames) {
868 moduleNames = append(moduleNames, fi.moduleName)
869 }
870 }
871 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
872 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
873 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
874 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
875 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
876
877 for _, fi := range a.filesInfo {
878 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
879 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
880 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
881 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), name, fi.installDir))
Colin Cross189ff982019-01-02 22:32:27 -0800882 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park8fd61922018-11-08 02:50:25 +0900883 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
884 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900885 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +0900886 archStr := fi.archType.String()
887 if archStr != "common" {
888 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
889 }
890 if fi.class == javaSharedLib {
Jiyong Parka8894842018-12-19 17:36:39 +0900891 javaModule := fi.module.(*java.Library)
892 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
893 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
Jiyong Park8fd61922018-11-08 02:50:25 +0900894 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
895 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
896 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
897 } else {
898 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
899 }
900 }
901 }}
902 } else {
903 return android.AndroidMkData{
904 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Alex Light5098a612018-11-29 17:12:15 -0800905 // zip-apex is the less common type so have the name refer to the image-apex
906 // only and use {name}.zip if you want the zip-apex
907 if apexType == zipApex && a.apexTypes == both {
908 name = name + ".zip"
909 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900910 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
911 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
912 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
913 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -0800914 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +0900915 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -0800916 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900917 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +0900918 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
919 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -0800920
Alex Light5098a612018-11-29 17:12:15 -0800921 if apexType == imageApex {
922 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
923 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900924 }}
925 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900926}
927
Alex Lightee250722018-12-06 14:00:02 -0800928func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -0800929 module := &apexBundle{
930 outputFiles: map[apexPackaging]android.WritablePath{},
931 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900932 module.AddProperties(&module.properties)
Alex Light5098a612018-11-29 17:12:15 -0800933 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +0900934 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
935 })
Alex Light5098a612018-11-29 17:12:15 -0800936 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900937 android.InitDefaultableModule(module)
938 return module
939}