blob: 96a4bd5e4876f14a40eeb820d3f74d700496cb78 [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 {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000121 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Roland Levillain54bdfda2018-10-05 19:34:32 +0100122 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
Alex Light3d673592019-01-18 14:37:31 -0800336 symlinks []string
Jiyong Park8fd61922018-11-08 02:50:25 +0900337}
338
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900339type apexBundle struct {
340 android.ModuleBase
341 android.DefaultableModuleBase
342
343 properties apexBundleProperties
344
Alex Light5098a612018-11-29 17:12:15 -0800345 apexTypes apexPackaging
346
Colin Crossa4925902018-11-16 11:36:28 -0800347 bundleModuleFile android.WritablePath
Alex Light5098a612018-11-29 17:12:15 -0800348 outputFiles map[apexPackaging]android.WritablePath
Colin Crossa4925902018-11-16 11:36:28 -0800349 installDir android.OutputPath
Jiyong Park8fd61922018-11-08 02:50:25 +0900350
351 // list of files to be included in this apex
352 filesInfo []apexFile
353
354 flattened bool
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900355}
356
Jiyong Park397e55e2018-10-24 21:09:55 +0900357func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900358 native_shared_libs []string, binaries []string, arch string, imageVariation string) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900359 // Use *FarVariation* to be able to depend on modules having
360 // conflicting variations with this module. This is required since
361 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
362 // for native shared libs.
363 ctx.AddFarVariationDependencies([]blueprint.Variation{
364 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900365 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900366 {Mutator: "link", Variation: "shared"},
Jiyong Park28d395a2018-12-07 22:42:47 +0900367 {Mutator: "version", Variation: ""}, // "" is the non-stub variant
Jiyong Park397e55e2018-10-24 21:09:55 +0900368 }, sharedLibTag, native_shared_libs...)
369
370 ctx.AddFarVariationDependencies([]blueprint.Variation{
371 {Mutator: "arch", Variation: arch},
Jiyong Parkda6eb592018-12-19 17:12:36 +0900372 {Mutator: "image", Variation: imageVariation},
Jiyong Park397e55e2018-10-24 21:09:55 +0900373 }, executableTag, binaries...)
374}
375
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900376func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900377 targets := ctx.MultiTargets()
Jiyong Park7c1dc612019-01-05 11:15:24 +0900378 config := ctx.DeviceConfig()
Jiyong Park397e55e2018-10-24 21:09:55 +0900379 has32BitTarget := false
380 for _, target := range targets {
381 if target.Arch.ArchType.Multilib == "lib32" {
382 has32BitTarget = true
383 }
384 }
385 for i, target := range targets {
386 // When multilib.* is omitted for native_shared_libs, it implies
387 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900388 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900389 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900390 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900391 {Mutator: "link", Variation: "shared"},
392 }, sharedLibTag, a.properties.Native_shared_libs...)
393
Jiyong Park397e55e2018-10-24 21:09:55 +0900394 // Add native modules targetting both ABIs
395 addDependenciesForNativeModules(ctx,
396 a.properties.Multilib.Both.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900397 a.properties.Multilib.Both.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900398 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900399
Alex Light3d673592019-01-18 14:37:31 -0800400 isPrimaryAbi := i == 0
401 if isPrimaryAbi {
Jiyong Park397e55e2018-10-24 21:09:55 +0900402 // When multilib.* is omitted for binaries, it implies
403 // multilib.first.
404 ctx.AddFarVariationDependencies([]blueprint.Variation{
405 {Mutator: "arch", Variation: target.String()},
Jiyong Park7c1dc612019-01-05 11:15:24 +0900406 {Mutator: "image", Variation: a.getImageVariation(config)},
Jiyong Park397e55e2018-10-24 21:09:55 +0900407 }, executableTag, a.properties.Binaries...)
408
409 // Add native modules targetting the first ABI
410 addDependenciesForNativeModules(ctx,
411 a.properties.Multilib.First.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900412 a.properties.Multilib.First.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900413 a.getImageVariation(config))
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800414
415 // When multilib.* is omitted for prebuilts, it implies multilib.first.
416 ctx.AddFarVariationDependencies([]blueprint.Variation{
417 {Mutator: "arch", Variation: target.String()},
418 }, prebuiltTag, a.properties.Prebuilts...)
Jiyong Park397e55e2018-10-24 21:09:55 +0900419 }
420
421 switch target.Arch.ArchType.Multilib {
422 case "lib32":
423 // Add native modules targetting 32-bit ABI
424 addDependenciesForNativeModules(ctx,
425 a.properties.Multilib.Lib32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900426 a.properties.Multilib.Lib32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900427 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900428
429 addDependenciesForNativeModules(ctx,
430 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900431 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900432 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900433 case "lib64":
434 // Add native modules targetting 64-bit ABI
435 addDependenciesForNativeModules(ctx,
436 a.properties.Multilib.Lib64.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900437 a.properties.Multilib.Lib64.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900438 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900439
440 if !has32BitTarget {
441 addDependenciesForNativeModules(ctx,
442 a.properties.Multilib.Prefer32.Native_shared_libs,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900443 a.properties.Multilib.Prefer32.Binaries, target.String(),
Jiyong Park7c1dc612019-01-05 11:15:24 +0900444 a.getImageVariation(config))
Jiyong Park397e55e2018-10-24 21:09:55 +0900445 }
446 }
447
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900448 }
449
Jiyong Parkff1458f2018-10-12 21:49:38 +0900450 ctx.AddFarVariationDependencies([]blueprint.Variation{
451 {Mutator: "arch", Variation: "android_common"},
452 }, javaLibTag, a.properties.Java_libs...)
453
Jiyong Park9335a262018-12-24 11:31:58 +0900454 if !ctx.Config().FlattenApex() || ctx.Config().UnbundledBuild() {
455 if String(a.properties.Key) == "" {
456 ctx.ModuleErrorf("key is missing")
457 return
458 }
459 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900460
Jiyong Park9335a262018-12-24 11:31:58 +0900461 cert := android.SrcIsModule(String(a.properties.Certificate))
462 if cert != "" {
463 ctx.AddDependency(ctx.Module(), certificateTag, cert)
464 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900465 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900466}
467
Jiyong Park74e240b2018-11-27 21:27:08 +0900468func (a *apexBundle) Srcs() android.Paths {
Jiyong Park5a832022018-12-20 09:54:35 +0900469 if file, ok := a.outputFiles[imageApex]; ok {
470 return android.Paths{file}
471 } else {
472 return nil
473 }
Jiyong Park74e240b2018-11-27 21:27:08 +0900474}
475
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900476func (a *apexBundle) installable() bool {
477 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
478}
479
Jiyong Park7c1dc612019-01-05 11:15:24 +0900480func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
481 if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900482 return "vendor"
483 } else {
484 return "core"
485 }
486}
487
Jiyong Park388ef3f2019-01-28 19:47:32 +0900488func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
489 globalSanitizerNames := []string{}
490 if a.Host() {
491 globalSanitizerNames = ctx.Config().SanitizeHost()
492 } else {
493 arches := ctx.Config().SanitizeDeviceArch()
494 if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
495 globalSanitizerNames = ctx.Config().SanitizeDevice()
496 }
497 }
498 return android.InList(sanitizerName, globalSanitizerNames)
Jiyong Park379de2f2018-12-19 02:47:14 +0900499}
500
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900501func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
502 // Decide the APEX-local directory by the multilib of the library
503 // In the future, we may query this to the module.
504 switch cc.Arch().ArchType.Multilib {
505 case "lib32":
506 dirInApex = "lib"
507 case "lib64":
508 dirInApex = "lib64"
509 }
510 if !cc.Arch().Native {
511 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
512 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900513 switch cc.Name() {
514 case "libc", "libm", "libdl":
515 // Special case for bionic libs. This is to prevent the bionic libs
516 // from being included in the search path /apex/com.android.apex/lib.
517 // This exclusion is required because bionic libs in the runtime APEX
518 // are available via the legacy paths /system/lib/libc.so, etc. By the
519 // init process, the bionic libs in the APEX are bind-mounted to the
520 // legacy paths and thus will be loaded into the default linker namespace.
521 // If the bionic libs are directly in /apex/com.android.apex/lib then
522 // the same libs will be again loaded to the runtime linker namespace,
523 // which will result double loading of bionic libs that isn't supported.
524 dirInApex = filepath.Join(dirInApex, "bionic")
525 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900526
527 fileToCopy = cc.OutputFile().Path()
528 return
529}
530
531func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
532 dirInApex = "bin"
533 fileToCopy = cc.OutputFile().Path()
534 return
535}
536
537func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
538 dirInApex = "javalib"
Jiyong Park8fd61922018-11-08 02:50:25 +0900539 fileToCopy = java.DexJarFile()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900540 return
541}
542
543func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
544 dirInApex = filepath.Join("etc", prebuilt.SubDir())
545 fileToCopy = prebuilt.OutputFile()
546 return
547}
548
549func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park8fd61922018-11-08 02:50:25 +0900550 filesInfo := []apexFile{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900551
Jiyong Parkff1458f2018-10-12 21:49:38 +0900552 var keyFile android.Path
Jiyong Park835d82b2018-12-27 16:04:18 +0900553 var pubKeyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900554 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900555
Alex Light5098a612018-11-29 17:12:15 -0800556 if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
557 a.apexTypes = imageApex
558 } else if *a.properties.Payload_type == "zip" {
559 a.apexTypes = zipApex
560 } else if *a.properties.Payload_type == "both" {
561 a.apexTypes = both
562 } else {
563 ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
564 return
565 }
566
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900567 ctx.WalkDeps(func(child, parent android.Module) bool {
568 if _, ok := parent.(*apexBundle); ok {
569 // direct dependencies
570 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900571 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900572 switch depTag {
573 case sharedLibTag:
574 if cc, ok := child.(*cc.Module); ok {
575 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Alex Light3d673592019-01-18 14:37:31 -0800576 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900577 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900578 } else {
579 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900580 }
581 case executableTag:
582 if cc, ok := child.(*cc.Module); ok {
Alex Light16df4e82019-01-24 11:37:55 -0800583 if !cc.Arch().Native {
584 // There is only one 'bin' directory so we shouldn't bother copying in
585 // native-bridge'd binaries and only use main ones.
586 return true
587 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900588 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
Alex Light3d673592019-01-18 14:37:31 -0800589 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeExecutable, cc, cc.Symlinks()})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900590 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900591 } else {
592 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900593 }
594 case javaLibTag:
595 if java, ok := child.(*java.Library); ok {
596 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
Jiyong Park8fd61922018-11-08 02:50:25 +0900597 if fileToCopy == nil {
598 ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
599 } else {
Alex Light3d673592019-01-18 14:37:31 -0800600 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, java.Arch().ArchType, dirInApex, javaSharedLib, java, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900601 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900602 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900603 } else {
604 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900605 }
606 case prebuiltTag:
607 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
608 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
Alex Light3d673592019-01-18 14:37:31 -0800609 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, prebuilt.Arch().ArchType, dirInApex, etc, prebuilt, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900610 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900611 } else {
612 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
613 }
614 case keyTag:
615 if key, ok := child.(*apexKey); ok {
616 keyFile = key.private_key_file
Jiyong Park835d82b2018-12-27 16:04:18 +0900617 if !key.installable() && ctx.Config().Debuggable() {
618 // If the key is not installed, bundled it with the APEX.
619 // Note: this bundled key is valid only for non-production builds
620 // (eng/userdebug).
621 pubKeyFile = key.public_key_file
622 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900623 return false
624 } else {
625 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900626 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900627 case certificateTag:
628 if dep, ok := child.(*java.AndroidAppCertificate); ok {
629 certificate = dep.Certificate
630 return false
631 } else {
632 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
633 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900634 }
635 } else {
636 // indirect dependencies
637 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
638 if cc, ok := child.(*cc.Module); ok {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900639 if cc.IsStubs() || cc.HasStubsVariants() {
640 return false
641 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900642 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900643 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
Alex Light3d673592019-01-18 14:37:31 -0800644 filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc, nil})
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900645 return true
646 }
647 }
648 }
649 return false
650 })
651
Jiyong Park9335a262018-12-24 11:31:58 +0900652 a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
653 if !a.flattened && keyFile == nil {
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900654 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
655 return
656 }
657
Jiyong Park8fd61922018-11-08 02:50:25 +0900658 // remove duplicates in filesInfo
659 removeDup := func(filesInfo []apexFile) []apexFile {
660 encountered := make(map[android.Path]bool)
661 result := []apexFile{}
662 for _, f := range filesInfo {
663 if !encountered[f.builtFile] {
664 encountered[f.builtFile] = true
665 result = append(result, f)
666 }
667 }
668 return result
669 }
670 filesInfo = removeDup(filesInfo)
671
672 // to have consistent build rules
673 sort.Slice(filesInfo, func(i, j int) bool {
674 return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
675 })
676
677 // prepend the name of this APEX to the module names. These names will be the names of
678 // modules that will be defined if the APEX is flattened.
679 for i := range filesInfo {
680 filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
681 }
682
Jiyong Park8fd61922018-11-08 02:50:25 +0900683 a.installDir = android.PathForModuleInstall(ctx, "apex")
684 a.filesInfo = filesInfo
Alex Light5098a612018-11-29 17:12:15 -0800685
686 if a.apexTypes.zip() {
Jiyong Park835d82b2018-12-27 16:04:18 +0900687 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
Alex Light5098a612018-11-29 17:12:15 -0800688 }
689 if a.apexTypes.image() {
690 if ctx.Config().FlattenApex() {
691 a.buildFlattenedApex(ctx)
692 } else {
Jiyong Park835d82b2018-12-27 16:04:18 +0900693 a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
Alex Light5098a612018-11-29 17:12:15 -0800694 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900695 }
696}
697
Jiyong Park835d82b2018-12-27 16:04:18 +0900698func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
699 pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900700 cert := String(a.properties.Certificate)
701 if cert != "" && android.SrcIsModule(cert) == "" {
702 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
703 certificate = java.Certificate{
704 defaultDir.Join(ctx, cert+".x509.pem"),
705 defaultDir.Join(ctx, cert+".pk8"),
706 }
707 } else if cert == "" {
708 pem, key := ctx.Config().DefaultAppCertificate(ctx)
709 certificate = java.Certificate{pem, key}
710 }
711
Dario Freni4abb1dc2018-11-20 18:04:58 +0000712 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900713
Alex Light5098a612018-11-29 17:12:15 -0800714 var abis []string
715 for _, target := range ctx.MultiTargets() {
716 if len(target.Arch.Abi) > 0 {
717 abis = append(abis, target.Arch.Abi[0])
718 }
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900719 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900720
Alex Light5098a612018-11-29 17:12:15 -0800721 abis = android.FirstUniqueStrings(abis)
722
723 suffix := apexType.suffix()
724 unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900725
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900726 filesToCopy := []android.Path{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900727 for _, f := range a.filesInfo {
728 filesToCopy = append(filesToCopy, f.builtFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900729 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900730
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900731 copyCommands := []string{}
Jiyong Park8fd61922018-11-08 02:50:25 +0900732 for i, src := range filesToCopy {
733 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
Alex Light5098a612018-11-29 17:12:15 -0800734 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900735 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
736 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
Alex Light3d673592019-01-18 14:37:31 -0800737 for _, sym := range a.filesInfo[i].symlinks {
738 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
739 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
740 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900741 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900742 implicitInputs := append(android.Paths(nil), filesToCopy...)
Alex Light5098a612018-11-29 17:12:15 -0800743 implicitInputs = append(implicitInputs, manifest)
744
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900745 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
746 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900747
Alex Light5098a612018-11-29 17:12:15 -0800748 if apexType.image() {
749 // files and dirs that will be created in APEX
750 var readOnlyPaths []string
751 var executablePaths []string // this also includes dirs
752 for _, f := range a.filesInfo {
753 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
754 if f.installDir == "bin" {
755 executablePaths = append(executablePaths, pathInApex)
Alex Light3d673592019-01-18 14:37:31 -0800756 for _, s := range f.symlinks {
757 executablePaths = append(executablePaths, filepath.Join("bin", s))
758 }
Alex Light5098a612018-11-29 17:12:15 -0800759 } else {
760 readOnlyPaths = append(readOnlyPaths, pathInApex)
761 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900762 dir := f.installDir
763 for !android.InList(dir, executablePaths) && dir != "" {
764 executablePaths = append(executablePaths, dir)
765 dir, _ = filepath.Split(dir) // move up to the parent
766 if len(dir) > 0 {
767 // remove trailing slash
768 dir = dir[:len(dir)-1]
769 }
Alex Light5098a612018-11-29 17:12:15 -0800770 }
771 }
772 sort.Strings(readOnlyPaths)
773 sort.Strings(executablePaths)
774 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
775 ctx.Build(pctx, android.BuildParams{
776 Rule: generateFsConfig,
777 Output: cannedFsConfig,
778 Description: "generate fs config",
779 Args: map[string]string{
780 "ro_paths": strings.Join(readOnlyPaths, " "),
781 "exec_paths": strings.Join(executablePaths, " "),
782 },
783 })
784
785 fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
786 fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
787 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
788 if !fileContextsOptionalPath.Valid() {
789 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
790 return
791 }
792 fileContexts := fileContextsOptionalPath.Path()
793
Jiyong Park835d82b2018-12-27 16:04:18 +0900794 optFlags := []string{}
795
Alex Light5098a612018-11-29 17:12:15 -0800796 // Additional implicit inputs.
797 implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
Jiyong Park835d82b2018-12-27 16:04:18 +0900798 if pubKeyFile != nil {
799 implicitInputs = append(implicitInputs, pubKeyFile)
800 optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
801 }
Alex Light5098a612018-11-29 17:12:15 -0800802
Jiyong Park7f67f482019-01-05 12:57:48 +0900803 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
804 if overridden {
805 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
806 }
807
Alex Light5098a612018-11-29 17:12:15 -0800808 ctx.Build(pctx, android.BuildParams{
809 Rule: apexRule,
810 Implicits: implicitInputs,
811 Output: unsignedOutputFile,
812 Description: "apex (" + apexType.name() + ")",
813 Args: map[string]string{
814 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
815 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
816 "copy_commands": strings.Join(copyCommands, " && "),
817 "manifest": manifest.String(),
818 "file_contexts": fileContexts.String(),
819 "canned_fs_config": cannedFsConfig.String(),
820 "key": keyFile.String(),
Jiyong Park835d82b2018-12-27 16:04:18 +0900821 "opt_flags": strings.Join(optFlags, " "),
Alex Light5098a612018-11-29 17:12:15 -0800822 },
823 })
824
825 apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
826 bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
827 a.bundleModuleFile = bundleModuleFile
828
829 ctx.Build(pctx, android.BuildParams{
830 Rule: apexProtoConvertRule,
831 Input: unsignedOutputFile,
832 Output: apexProtoFile,
833 Description: "apex proto convert",
834 })
835
836 ctx.Build(pctx, android.BuildParams{
837 Rule: apexBundleRule,
838 Input: apexProtoFile,
839 Output: a.bundleModuleFile,
840 Description: "apex bundle module",
841 Args: map[string]string{
842 "abi": strings.Join(abis, "."),
843 },
844 })
845 } else {
846 ctx.Build(pctx, android.BuildParams{
847 Rule: zipApexRule,
848 Implicits: implicitInputs,
849 Output: unsignedOutputFile,
850 Description: "apex (" + apexType.name() + ")",
851 Args: map[string]string{
852 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
853 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
854 "copy_commands": strings.Join(copyCommands, " && "),
855 "manifest": manifest.String(),
856 },
857 })
Colin Crossa4925902018-11-16 11:36:28 -0800858 }
Colin Crossa4925902018-11-16 11:36:28 -0800859
Alex Light5098a612018-11-29 17:12:15 -0800860 a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900861 ctx.Build(pctx, android.BuildParams{
862 Rule: java.Signapk,
863 Description: "signapk",
Alex Light5098a612018-11-29 17:12:15 -0800864 Output: a.outputFiles[apexType],
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900865 Input: unsignedOutputFile,
866 Args: map[string]string{
867 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
Jiyong Parkbfe64a12018-11-22 02:51:54 +0900868 "flags": "-a 4096", //alignment
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900869 },
870 })
Alex Light5098a612018-11-29 17:12:15 -0800871
872 // Install to $OUT/soong/{target,host}/.../apex
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900873 if a.installable() {
874 ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
875 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900876}
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900877
Jiyong Park8fd61922018-11-08 02:50:25 +0900878func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900879 if a.installable() {
880 // For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
881 // with other ordinary files.
882 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Parkd699cb92019-01-10 00:23:16 +0900883
884 // rename to apex_manifest.json
885 copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
886 ctx.Build(pctx, android.BuildParams{
887 Rule: android.Cp,
888 Input: manifest,
889 Output: copiedManifest,
890 })
Alex Light3d673592019-01-18 14:37:31 -0800891 a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc, nil, nil})
Jiyong Park8fd61922018-11-08 02:50:25 +0900892
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900893 for _, fi := range a.filesInfo {
894 dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
895 ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
896 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900897 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900898}
899
900func (a *apexBundle) AndroidMk() android.AndroidMkData {
Alex Light5098a612018-11-29 17:12:15 -0800901 writers := []android.AndroidMkData{}
902 if a.apexTypes.image() {
903 writers = append(writers, a.androidMkForType(imageApex))
904 }
905 if a.apexTypes.zip() {
906 writers = append(writers, a.androidMkForType(zipApex))
907 }
908 return android.AndroidMkData{
909 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
910 for _, data := range writers {
911 data.Custom(w, name, prefix, moduleDir, data)
912 }
913 }}
914}
915
916func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
Michael Butlereaebd762019-01-11 20:23:36 +0000917 // Only image APEXes can be flattened.
918 if a.flattened && apexType.image() {
919 return android.AndroidMkData{
920 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
921 moduleNames := []string{}
922 for _, fi := range a.filesInfo {
923 if !android.InList(fi.moduleName, moduleNames) {
924 moduleNames = append(moduleNames, fi.moduleName)
Jiyong Park8fd61922018-11-08 02:50:25 +0900925 }
926 }
927 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
928 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
929 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
930 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
931 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Michael Butlereaebd762019-01-11 20:23:36 +0000932
933 for _, fi := range a.filesInfo {
Jiyong Park379de2f2018-12-19 02:47:14 +0900934 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
935 continue
936 }
Michael Butlereaebd762019-01-11 20:23:36 +0000937 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
938 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
939 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
940 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), name, fi.installDir))
Michael Butlereaebd762019-01-11 20:23:36 +0000941 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
942 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
943 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
944 archStr := fi.archType.String()
945 if archStr != "common" {
946 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
947 }
948 if fi.class == javaSharedLib {
949 javaModule := fi.module.(*java.Library)
Jiyong Park087b5412019-01-20 22:39:47 +0900950 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
951 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
952 // we will have foo.jar.jar
953 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
Michael Butlereaebd762019-01-11 20:23:36 +0000954 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
955 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
956 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
957 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
958 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
959 } else {
Jiyong Park087b5412019-01-20 22:39:47 +0900960 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Michael Butlereaebd762019-01-11 20:23:36 +0000961 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
962 }
963 }
964 }}
965 } else {
966 return android.AndroidMkData{
967 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Alex Light5098a612018-11-29 17:12:15 -0800968 // zip-apex is the less common type so have the name refer to the image-apex
969 // only and use {name}.zip if you want the zip-apex
970 if apexType == zipApex && a.apexTypes == both {
971 name = name + ".zip"
972 }
Jiyong Park8fd61922018-11-08 02:50:25 +0900973 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
974 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
975 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
976 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
Alex Light5098a612018-11-29 17:12:15 -0800977 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
Jiyong Park8fd61922018-11-08 02:50:25 +0900978 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
Colin Cross189ff982019-01-02 22:32:27 -0800979 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
Jiyong Park92c0f9c2018-12-13 23:14:57 +0900980 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jiyong Park8fd61922018-11-08 02:50:25 +0900981 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
982 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Colin Crossa4925902018-11-16 11:36:28 -0800983
Alex Light5098a612018-11-29 17:12:15 -0800984 if apexType == imageApex {
985 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
986 }
Michael Butlereaebd762019-01-11 20:23:36 +0000987 }}
988 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900989}
990
Alex Lightee250722018-12-06 14:00:02 -0800991func ApexBundleFactory() android.Module {
Alex Light5098a612018-11-29 17:12:15 -0800992 module := &apexBundle{
993 outputFiles: map[apexPackaging]android.WritablePath{},
994 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900995 module.AddProperties(&module.properties)
Alex Light5098a612018-11-29 17:12:15 -0800996 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
Jiyong Park397e55e2018-10-24 21:09:55 +0900997 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
998 })
Alex Light5098a612018-11-29 17:12:15 -0800999 android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Park48ca7dc2018-10-10 14:01:00 +09001000 android.InitDefaultableModule(module)
1001 return module
1002}