blob: 7232c1b6d70467bb2c7dfc20f28caf6cf09504e0 [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} && ` +
Jiyong Park48ca7dc2018-10-10 14:01:00 +090041 `echo '/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} && ` +
43 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 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} ` +
55 `${apexer} --verbose --force --manifest ${manifest} ` +
56 `--file_contexts ${file_contexts} ` +
57 `--canned_fs_config ${canned_fs_config} ` +
58 `--key ${key} ${image_dir} ${out} `,
59 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
60 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
61 "${soong_zip}", "${zipalign}", "${aapt2}"},
62 Description: "APEX ${image_dir} => ${out}",
63 }, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key")
64)
65
66var apexSuffix = ".apex"
67
68type dependencyTag struct {
69 blueprint.BaseDependencyTag
70 name string
71}
72
73var (
Jiyong Parkc00cbd92018-10-30 21:20:05 +090074 sharedLibTag = dependencyTag{name: "sharedLib"}
75 executableTag = dependencyTag{name: "executable"}
76 javaLibTag = dependencyTag{name: "javaLib"}
77 prebuiltTag = dependencyTag{name: "prebuilt"}
78 keyTag = dependencyTag{name: "key"}
79 certificateTag = dependencyTag{name: "certificate"}
Jiyong Park48ca7dc2018-10-10 14:01:00 +090080)
81
82func init() {
83 pctx.Import("android/soong/common")
Jiyong Parkc00cbd92018-10-30 21:20:05 +090084 pctx.Import("android/soong/java")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090085 pctx.HostBinToolVariable("apexer", "apexer")
Roland Levillain54bdfda2018-10-05 19:34:32 +010086 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
87 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
88 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
89 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
90 if !android.ExistentPathForSource(ctx, "frameworks/base").Valid() {
91 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
92 } else {
93 return pctx.HostBinToolPath(ctx, tool).String()
94 }
95 })
96 }
97 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
Jiyong Park48ca7dc2018-10-10 14:01:00 +090098 pctx.HostBinToolVariable("avbtool", "avbtool")
99 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
100 pctx.HostBinToolVariable("merge_zips", "merge_zips")
101 pctx.HostBinToolVariable("mke2fs", "mke2fs")
102 pctx.HostBinToolVariable("resize2fs", "resize2fs")
103 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
104 pctx.HostBinToolVariable("soong_zip", "soong_zip")
105 pctx.HostBinToolVariable("zipalign", "zipalign")
106
107 android.RegisterModuleType("apex", apexBundleFactory)
108
109 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
110 ctx.TopDown("apex_deps", apexDepsMutator)
111 ctx.BottomUp("apex", apexMutator)
112 })
113}
114
115// maps a module name to set of apex bundle names that the module should be built for
116func apexBundleNamesFor(config android.Config) map[string]map[string]bool {
117 return config.Once("apexBundleNames", func() interface{} {
118 return make(map[string]map[string]bool)
119 }).(map[string]map[string]bool)
120}
121
122// Mark the direct and transitive dependencies of apex bundles so that they
123// can be built for the apex bundles.
124func apexDepsMutator(mctx android.TopDownMutatorContext) {
125 if _, ok := mctx.Module().(*apexBundle); ok {
126 apexBundleName := mctx.Module().Name()
127 mctx.WalkDeps(func(child, parent android.Module) bool {
128 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park678529e2018-10-23 23:58:01 +0900129 moduleName := am.Name() + "-" + am.Target().String()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900130 bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName]
131 if !ok {
132 bundleNames = make(map[string]bool)
133 apexBundleNamesFor(mctx.Config())[moduleName] = bundleNames
134 }
135 bundleNames[apexBundleName] = true
136 return true
137 } else {
138 return false
139 }
140 })
141 }
142}
143
144// Create apex variations if a module is included in APEX(s).
145func apexMutator(mctx android.BottomUpMutatorContext) {
146 if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
Jiyong Park678529e2018-10-23 23:58:01 +0900147 moduleName := am.Name() + "-" + am.Target().String()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900148 if bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName]; ok {
149 variations := []string{"platform"}
150 for bn := range bundleNames {
151 variations = append(variations, bn)
152 }
153 modules := mctx.CreateVariations(variations...)
154 for i, m := range modules {
155 if i == 0 {
156 continue // platform
157 }
158 m.(android.ApexModule).BuildForApex(variations[i])
159 }
160 }
161 } else if _, ok := mctx.Module().(*apexBundle); ok {
162 // apex bundle itself is mutated so that it and its modules have same
163 // apex variant.
164 apexBundleName := mctx.ModuleName()
165 mctx.CreateVariations(apexBundleName)
166 }
167}
168
169type apexBundleProperties struct {
170 // Json manifest file describing meta info of this APEX bundle. Default:
171 // "manifest.json"
172 Manifest *string
173
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900174 // Determines the file contexts file for setting security context to each file in this APEX bundle.
175 // Specifically, when this is set to <value>, /system/sepolicy/apex/<value>_file_contexts file is
176 // used.
177 // Default: <name_of_this_module>
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900178 File_contexts *string
179
180 // List of native shared libs that are embedded inside this APEX bundle
181 Native_shared_libs []string
182
183 // List of native executables that are embedded inside this APEX bundle
184 Binaries []string
185
186 // List of java libraries that are embedded inside this APEX bundle
187 Java_libs []string
188
189 // List of prebuilt files that are embedded inside this APEX bundle
190 Prebuilts []string
Jiyong Parkff1458f2018-10-12 21:49:38 +0900191
192 // Name of the apex_key module that provides the private key to sign APEX
193 Key *string
Jiyong Park397e55e2018-10-24 21:09:55 +0900194
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900195 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
196 // or an android_app_certificate module name in the form ":module".
197 Certificate *string
198
Jiyong Park397e55e2018-10-24 21:09:55 +0900199 Multilib struct {
200 First struct {
201 // List of native libraries whose compile_multilib is "first"
202 Native_shared_libs []string
203 // List of native executables whose compile_multilib is "first"
204 Binaries []string
205 }
206 Both struct {
207 // List of native libraries whose compile_multilib is "both"
208 Native_shared_libs []string
209 // List of native executables whose compile_multilib is "both"
210 Binaries []string
211 }
212 Prefer32 struct {
213 // List of native libraries whose compile_multilib is "prefer32"
214 Native_shared_libs []string
215 // List of native executables whose compile_multilib is "prefer32"
216 Binaries []string
217 }
218 Lib32 struct {
219 // List of native libraries whose compile_multilib is "32"
220 Native_shared_libs []string
221 // List of native executables whose compile_multilib is "32"
222 Binaries []string
223 }
224 Lib64 struct {
225 // List of native libraries whose compile_multilib is "64"
226 Native_shared_libs []string
227 // List of native executables whose compile_multilib is "64"
228 Binaries []string
229 }
230 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900231}
232
233type apexBundle struct {
234 android.ModuleBase
235 android.DefaultableModuleBase
236
237 properties apexBundleProperties
238
239 outputFile android.WritablePath
240 installDir android.OutputPath
241}
242
Jiyong Park397e55e2018-10-24 21:09:55 +0900243func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
244 native_shared_libs []string, binaries []string, arch string) {
245 // Use *FarVariation* to be able to depend on modules having
246 // conflicting variations with this module. This is required since
247 // arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
248 // for native shared libs.
249 ctx.AddFarVariationDependencies([]blueprint.Variation{
250 {Mutator: "arch", Variation: arch},
251 {Mutator: "image", Variation: "core"},
252 {Mutator: "link", Variation: "shared"},
253 }, sharedLibTag, native_shared_libs...)
254
255 ctx.AddFarVariationDependencies([]blueprint.Variation{
256 {Mutator: "arch", Variation: arch},
257 {Mutator: "image", Variation: "core"},
258 }, executableTag, binaries...)
259}
260
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900261func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park397e55e2018-10-24 21:09:55 +0900262 targets := ctx.MultiTargets()
263 has32BitTarget := false
264 for _, target := range targets {
265 if target.Arch.ArchType.Multilib == "lib32" {
266 has32BitTarget = true
267 }
268 }
269 for i, target := range targets {
270 // When multilib.* is omitted for native_shared_libs, it implies
271 // multilib.both.
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900272 ctx.AddFarVariationDependencies([]blueprint.Variation{
Jiyong Park397e55e2018-10-24 21:09:55 +0900273 {Mutator: "arch", Variation: target.String()},
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900274 {Mutator: "image", Variation: "core"},
275 {Mutator: "link", Variation: "shared"},
276 }, sharedLibTag, a.properties.Native_shared_libs...)
277
Jiyong Park397e55e2018-10-24 21:09:55 +0900278 // Add native modules targetting both ABIs
279 addDependenciesForNativeModules(ctx,
280 a.properties.Multilib.Both.Native_shared_libs,
281 a.properties.Multilib.Both.Binaries, target.String())
282
283 if i == 0 {
284 // When multilib.* is omitted for binaries, it implies
285 // multilib.first.
286 ctx.AddFarVariationDependencies([]blueprint.Variation{
287 {Mutator: "arch", Variation: target.String()},
288 {Mutator: "image", Variation: "core"},
289 }, executableTag, a.properties.Binaries...)
290
291 // Add native modules targetting the first ABI
292 addDependenciesForNativeModules(ctx,
293 a.properties.Multilib.First.Native_shared_libs,
294 a.properties.Multilib.First.Binaries, target.String())
295 }
296
297 switch target.Arch.ArchType.Multilib {
298 case "lib32":
299 // Add native modules targetting 32-bit ABI
300 addDependenciesForNativeModules(ctx,
301 a.properties.Multilib.Lib32.Native_shared_libs,
302 a.properties.Multilib.Lib32.Binaries, target.String())
303
304 addDependenciesForNativeModules(ctx,
305 a.properties.Multilib.Prefer32.Native_shared_libs,
306 a.properties.Multilib.Prefer32.Binaries, target.String())
307 case "lib64":
308 // Add native modules targetting 64-bit ABI
309 addDependenciesForNativeModules(ctx,
310 a.properties.Multilib.Lib64.Native_shared_libs,
311 a.properties.Multilib.Lib64.Binaries, target.String())
312
313 if !has32BitTarget {
314 addDependenciesForNativeModules(ctx,
315 a.properties.Multilib.Prefer32.Native_shared_libs,
316 a.properties.Multilib.Prefer32.Binaries, target.String())
317 }
318 }
319
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900320 }
321
Jiyong Parkff1458f2018-10-12 21:49:38 +0900322 ctx.AddFarVariationDependencies([]blueprint.Variation{
323 {Mutator: "arch", Variation: "android_common"},
324 }, javaLibTag, a.properties.Java_libs...)
325
326 ctx.AddFarVariationDependencies([]blueprint.Variation{
327 {Mutator: "arch", Variation: "android_common"},
328 }, prebuiltTag, a.properties.Prebuilts...)
329
330 if String(a.properties.Key) == "" {
331 ctx.ModuleErrorf("key is missing")
332 return
333 }
334 ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900335
336 cert := android.SrcIsModule(String(a.properties.Certificate))
337 if cert != "" {
338 ctx.AddDependency(ctx.Module(), certificateTag, cert)
339 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900340}
341
342func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
343 // Decide the APEX-local directory by the multilib of the library
344 // In the future, we may query this to the module.
345 switch cc.Arch().ArchType.Multilib {
346 case "lib32":
347 dirInApex = "lib"
348 case "lib64":
349 dirInApex = "lib64"
350 }
351 if !cc.Arch().Native {
352 dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
353 }
354
355 fileToCopy = cc.OutputFile().Path()
356 return
357}
358
359func getCopyManifestForExecutable(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
360 dirInApex = "bin"
361 fileToCopy = cc.OutputFile().Path()
362 return
363}
364
365func getCopyManifestForJavaLibrary(java *java.Library) (fileToCopy android.Path, dirInApex string) {
366 dirInApex = "javalib"
367 fileToCopy = java.Srcs()[0]
368 return
369}
370
371func getCopyManifestForPrebuiltEtc(prebuilt *android.PrebuiltEtc) (fileToCopy android.Path, dirInApex string) {
372 dirInApex = filepath.Join("etc", prebuilt.SubDir())
373 fileToCopy = prebuilt.OutputFile()
374 return
375}
376
377func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
378 // files to copy -> dir in apex
379 copyManifest := make(map[android.Path]string)
380
Jiyong Parkff1458f2018-10-12 21:49:38 +0900381 var keyFile android.Path
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900382 var certificate java.Certificate
Jiyong Parkff1458f2018-10-12 21:49:38 +0900383
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900384 ctx.WalkDeps(func(child, parent android.Module) bool {
385 if _, ok := parent.(*apexBundle); ok {
386 // direct dependencies
387 depTag := ctx.OtherModuleDependencyTag(child)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900388 depName := ctx.OtherModuleName(child)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900389 switch depTag {
390 case sharedLibTag:
391 if cc, ok := child.(*cc.Module); ok {
392 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
393 copyManifest[fileToCopy] = dirInApex
394 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900395 } else {
396 ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900397 }
398 case executableTag:
399 if cc, ok := child.(*cc.Module); ok {
400 fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
401 copyManifest[fileToCopy] = dirInApex
402 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900403 } else {
404 ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900405 }
406 case javaLibTag:
407 if java, ok := child.(*java.Library); ok {
408 fileToCopy, dirInApex := getCopyManifestForJavaLibrary(java)
409 copyManifest[fileToCopy] = dirInApex
410 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900411 } else {
412 ctx.PropertyErrorf("java_libs", "%q is not a java_library module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900413 }
414 case prebuiltTag:
415 if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
416 fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
417 copyManifest[fileToCopy] = dirInApex
418 return true
Jiyong Parkff1458f2018-10-12 21:49:38 +0900419 } else {
420 ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
421 }
422 case keyTag:
423 if key, ok := child.(*apexKey); ok {
424 keyFile = key.private_key_file
425 return false
426 } else {
427 ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900428 }
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900429 case certificateTag:
430 if dep, ok := child.(*java.AndroidAppCertificate); ok {
431 certificate = dep.Certificate
432 return false
433 } else {
434 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
435 }
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900436 }
437 } else {
438 // indirect dependencies
439 if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
440 if cc, ok := child.(*cc.Module); ok {
441 fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
442 copyManifest[fileToCopy] = dirInApex
443 return true
444 }
445 }
446 }
447 return false
448 })
449
Jiyong Parkfa0a3732018-11-09 05:52:26 +0900450 if keyFile == nil {
451 ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
452 return
453 }
454
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900455 cert := String(a.properties.Certificate)
456 if cert != "" && android.SrcIsModule(cert) == "" {
457 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
458 certificate = java.Certificate{
459 defaultDir.Join(ctx, cert+".x509.pem"),
460 defaultDir.Join(ctx, cert+".pk8"),
461 }
462 } else if cert == "" {
463 pem, key := ctx.Config().DefaultAppCertificate(ctx)
464 certificate = java.Certificate{pem, key}
465 }
466
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900467 // files and dirs that will be created in apex
Jiyong Park92905d62018-10-11 13:23:09 +0900468 var readOnlyPaths []string
469 var executablePaths []string // this also includes dirs
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900470 for fileToCopy, dirInApex := range copyManifest {
471 pathInApex := filepath.Join(dirInApex, fileToCopy.Base())
Jiyong Park92905d62018-10-11 13:23:09 +0900472 if dirInApex == "bin" {
473 executablePaths = append(executablePaths, pathInApex)
474 } else {
475 readOnlyPaths = append(readOnlyPaths, pathInApex)
476 }
477 if !android.InList(dirInApex, executablePaths) {
478 executablePaths = append(executablePaths, dirInApex)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900479 }
480 }
Jiyong Park92905d62018-10-11 13:23:09 +0900481 sort.Strings(readOnlyPaths)
482 sort.Strings(executablePaths)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900483 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
484 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
485 Rule: generateFsConfig,
486 Output: cannedFsConfig,
487 Args: map[string]string{
Jiyong Park92905d62018-10-11 13:23:09 +0900488 "ro_paths": strings.Join(readOnlyPaths, " "),
489 "exec_paths": strings.Join(executablePaths, " "),
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900490 },
491 })
492
493 manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "manifest.json"))
Jiyong Parkd0a65ba2018-11-10 06:37:15 +0900494
495 fcName := proptools.StringDefault(a.properties.File_contexts, a.ModuleBase.Name())
496 fileContextsPath := "system/sepolicy/apex/" + fcName + "_file_contexts"
497 fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
498 if !fileContextsOptionalPath.Valid() {
499 ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
500 return
501 }
502 fileContexts := fileContextsOptionalPath.Path()
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900503
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900504 unsignedOutputFile := android.PathForModuleOut(ctx, a.ModuleBase.Name()+apexSuffix+".unsigned")
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900505
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900506 filesToCopy := []android.Path{}
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900507 for file := range copyManifest {
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900508 filesToCopy = append(filesToCopy, file)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900509 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900510 sort.Slice(filesToCopy, func(i, j int) bool {
511 return filesToCopy[i].String() < filesToCopy[j].String()
512 })
513
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900514 copyCommands := []string{}
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900515 for _, src := range filesToCopy {
516 dest := filepath.Join(copyManifest[src], src.Base())
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900517 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image").String(), dest)
518 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
519 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
520 }
Jiyong Parkab3ceb32018-10-10 14:05:29 +0900521 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900522 implicitInputs = append(implicitInputs, cannedFsConfig, manifest, fileContexts, keyFile)
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900523 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
524 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
525 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
526 Rule: apexRule,
527 Implicits: implicitInputs,
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900528 Output: unsignedOutputFile,
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900529 Args: map[string]string{
530 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
531 "image_dir": android.PathForModuleOut(ctx, "image").String(),
532 "copy_commands": strings.Join(copyCommands, " && "),
533 "manifest": manifest.String(),
534 "file_contexts": fileContexts.String(),
535 "canned_fs_config": cannedFsConfig.String(),
Jiyong Parkff1458f2018-10-12 21:49:38 +0900536 "key": keyFile.String(),
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900537 },
538 })
539
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900540 a.outputFile = android.PathForModuleOut(ctx, a.ModuleBase.Name()+apexSuffix)
541 ctx.Build(pctx, android.BuildParams{
542 Rule: java.Signapk,
543 Description: "signapk",
544 Output: a.outputFile,
545 Input: unsignedOutputFile,
546 Args: map[string]string{
547 "certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
548 },
549 })
550
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900551 a.installDir = android.PathForModuleInstall(ctx, "apex")
552}
553
554func (a *apexBundle) AndroidMk() android.AndroidMkData {
555 return android.AndroidMkData{
556 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
557 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
558 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
559 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
560 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
561 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
562 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
563 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name+apexSuffix)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900564 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900565 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
566 }}
567}
568
569func apexBundleFactory() android.Module {
570 module := &apexBundle{}
571 module.AddProperties(&module.properties)
Jiyong Park397e55e2018-10-24 21:09:55 +0900572 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase,
573 class android.OsClass) bool {
574 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
575 })
Jiyong Park48ca7dc2018-10-10 14:01:00 +0900576 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
577 android.InitDefaultableModule(module)
578 return module
579}