blob: 213f9ea0129a15a9a10abff78544fa0f4a5e7151 [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 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 "path/filepath"
20 "runtime"
21 "sort"
22 "strings"
23
24 "android/soong/android"
25 "android/soong/java"
26
27 "github.com/google/blueprint"
28 "github.com/google/blueprint/proptools"
29)
30
31var (
32 pctx = android.NewPackageContext("android/apex")
33)
34
35func init() {
36 pctx.Import("android/soong/android")
37 pctx.Import("android/soong/java")
38 pctx.HostBinToolVariable("apexer", "apexer")
39 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
40 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
41 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
42 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
43 if !ctx.Config().FrameworksBaseDirExists(ctx) {
44 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
45 } else {
Martin Stjernholm7260d062019-12-09 21:47:14 +000046 return ctx.Config().HostToolPath(ctx, tool).String()
Jiyong Park09d77522019-11-18 11:16:27 +090047 }
48 })
49 }
50 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
51 pctx.HostBinToolVariable("avbtool", "avbtool")
52 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
53 pctx.HostBinToolVariable("merge_zips", "merge_zips")
54 pctx.HostBinToolVariable("mke2fs", "mke2fs")
55 pctx.HostBinToolVariable("resize2fs", "resize2fs")
56 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
57 pctx.HostBinToolVariable("soong_zip", "soong_zip")
58 pctx.HostBinToolVariable("zip2zip", "zip2zip")
59 pctx.HostBinToolVariable("zipalign", "zipalign")
60 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
61 pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
62}
63
64var (
65 // Create a canned fs config file where all files and directories are
66 // by default set to (uid/gid/mode) = (1000/1000/0644)
67 // TODO(b/113082813) make this configurable using config.fs syntax
68 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
69 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
70 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
71 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
72 Description: "fs_config ${out}",
73 }, "ro_paths", "exec_paths")
74
75 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
76 Command: `rm -f $out && ${jsonmodify} $in ` +
77 `-a provideNativeLibs ${provideNativeLibs} ` +
78 `-a requireNativeLibs ${requireNativeLibs} ` +
79 `${opt} ` +
80 `-o $out`,
81 CommandDeps: []string{"${jsonmodify}"},
82 Description: "prepare ${out}",
83 }, "provideNativeLibs", "requireNativeLibs", "opt")
84
85 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
86 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
87 CommandDeps: []string{"${conv_apex_manifest}"},
88 Description: "strip ${in}=>${out}",
89 })
90
91 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
92 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
93 CommandDeps: []string{"${conv_apex_manifest}"},
94 Description: "convert ${in}=>${out}",
95 })
96
97 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
98 // against the binary policy using sefcontext_compiler -p <policy>.
99
100 // TODO(b/114327326): automate the generation of file_contexts
101 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
102 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
103 `(. ${out}.copy_commands) && ` +
104 `APEXER_TOOL_PATH=${tool_path} ` +
105 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900106 `--file_contexts ${file_contexts} ` +
107 `--canned_fs_config ${canned_fs_config} ` +
108 `--payload_type image ` +
109 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
110 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
111 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
112 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
113 Rspfile: "${out}.copy_commands",
114 RspfileContent: "${copy_commands}",
115 Description: "APEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900116 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900117
118 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
119 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
120 `(. ${out}.copy_commands) && ` +
121 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han214bf372019-11-12 13:03:50 +0900122 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900123 `--payload_type zip ` +
124 `${image_dir} ${out} `,
125 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
126 Rspfile: "${out}.copy_commands",
127 RspfileContent: "${copy_commands}",
128 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900129 }, "tool_path", "image_dir", "copy_commands", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900130
131 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
132 blueprint.RuleParams{
133 Command: `${aapt2} convert --output-format proto $in -o $out`,
134 CommandDeps: []string{"${aapt2}"},
135 })
136
137 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
138 Command: `${zip2zip} -i $in -o $out ` +
139 `apex_payload.img:apex/${abi}.img ` +
140 `apex_manifest.json:root/apex_manifest.json ` +
141 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
142 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
143 CommandDeps: []string{"${zip2zip}"},
144 Description: "app bundle",
145 }, "abi")
146
147 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
148 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
149 Rspfile: "${out}.emit_commands",
150 RspfileContent: "${emit_commands}",
151 Description: "Emit APEX image content",
152 }, "emit_commands")
153
154 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
155 Command: `diff --unchanged-group-format='' \` +
156 `--changed-group-format='%<' \` +
157 `${image_content_file} ${whitelisted_files_file} || (` +
158 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
159 ` "To fix the build run following command:" && ` +
160 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
161 `exit 1)`,
162 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
163 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
164)
165
166func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
167 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
168
Jooyung Han214bf372019-11-12 13:03:50 +0900169 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900170
171 // put dependency({provide|require}NativeLibs) in apex_manifest.json
172 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
173 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
174
175 // apex name can be overridden
176 optCommands := []string{}
177 if a.properties.Apex_name != nil {
178 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
179 }
180
181 ctx.Build(pctx, android.BuildParams{
182 Rule: apexManifestRule,
183 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900184 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900185 Args: map[string]string{
186 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
187 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
188 "opt": strings.Join(optCommands, " "),
189 },
190 })
191
Jooyung Han214bf372019-11-12 13:03:50 +0900192 if proptools.Bool(a.properties.Legacy_android10_support) {
193 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
194 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
195 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
196 ctx.Build(pctx, android.BuildParams{
197 Rule: stripApexManifestRule,
198 Input: manifestJsonFullOut,
199 Output: a.manifestJsonOut,
200 })
201 }
Jiyong Park09d77522019-11-18 11:16:27 +0900202
203 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
204 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
205 ctx.Build(pctx, android.BuildParams{
206 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900207 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900208 Output: a.manifestPbOut,
209 })
210}
211
212func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
213 noticeFiles := []android.Path{}
214 for _, f := range a.filesInfo {
215 if f.module != nil {
216 notice := f.module.NoticeFile()
217 if notice.Valid() {
218 noticeFiles = append(noticeFiles, notice.Path())
219 }
220 }
221 }
222 // append the notice file specified in the apex module itself
223 if a.NoticeFile().Valid() {
224 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
225 }
226
227 if len(noticeFiles) == 0 {
228 return android.OptionalPath{}
229 }
230
231 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
232}
233
234func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
235 var abis []string
236 for _, target := range ctx.MultiTargets() {
237 if len(target.Arch.Abi) > 0 {
238 abis = append(abis, target.Arch.Abi[0])
239 }
240 }
241
242 abis = android.FirstUniqueStrings(abis)
243
244 apexType := a.properties.ApexType
245 suffix := apexType.suffix()
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800246 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900247
248 filesToCopy := []android.Path{}
249 for _, f := range a.filesInfo {
250 filesToCopy = append(filesToCopy, f.builtFile)
251 }
252
253 copyCommands := []string{}
254 emitCommands := []string{}
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800255 imageContentFile := android.PathForModuleOut(ctx, a.Name()+"-content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900256 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
257 if proptools.Bool(a.properties.Legacy_android10_support) {
258 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
259 }
Jiyong Park09d77522019-11-18 11:16:27 +0900260 for i, src := range filesToCopy {
261 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
262 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
263 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
264 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
265 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
266 for _, sym := range a.filesInfo[i].symlinks {
267 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
268 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
269 }
270 }
271 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
272
273 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han214bf372019-11-12 13:03:50 +0900274 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900275
276 if a.properties.Whitelisted_files != nil {
277 ctx.Build(pctx, android.BuildParams{
278 Rule: emitApexContentRule,
279 Implicits: implicitInputs,
280 Output: imageContentFile,
281 Description: "emit apex image content",
282 Args: map[string]string{
283 "emit_commands": strings.Join(emitCommands, " && "),
284 },
285 })
286 implicitInputs = append(implicitInputs, imageContentFile)
287 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
288
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800289 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900290 ctx.Build(pctx, android.BuildParams{
291 Rule: diffApexContentRule,
292 Implicits: implicitInputs,
293 Output: phonyOutput,
294 Description: "diff apex image content",
295 Args: map[string]string{
296 "whitelisted_files_file": whitelistedFilesFile.String(),
297 "image_content_file": imageContentFile.String(),
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800298 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900299 },
300 })
301
302 implicitInputs = append(implicitInputs, phonyOutput)
303 }
304
305 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
306 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
307
308 if apexType == imageApex {
309 // files and dirs that will be created in APEX
310 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
311 var executablePaths []string // this also includes dirs
312 for _, f := range a.filesInfo {
313 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
314 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
315 executablePaths = append(executablePaths, pathInApex)
316 for _, s := range f.symlinks {
317 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
318 }
319 } else {
320 readOnlyPaths = append(readOnlyPaths, pathInApex)
321 }
322 dir := f.installDir
323 for !android.InList(dir, executablePaths) && dir != "" {
324 executablePaths = append(executablePaths, dir)
325 dir, _ = filepath.Split(dir) // move up to the parent
326 if len(dir) > 0 {
327 // remove trailing slash
328 dir = dir[:len(dir)-1]
329 }
330 }
331 }
332 sort.Strings(readOnlyPaths)
333 sort.Strings(executablePaths)
334 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
335 ctx.Build(pctx, android.BuildParams{
336 Rule: generateFsConfig,
337 Output: cannedFsConfig,
338 Description: "generate fs config",
339 Args: map[string]string{
340 "ro_paths": strings.Join(readOnlyPaths, " "),
341 "exec_paths": strings.Join(executablePaths, " "),
342 },
343 })
344
Jiyong Park09d77522019-11-18 11:16:27 +0900345 optFlags := []string{}
346
347 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900348 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900349 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
350
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800351 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900352 if overridden {
353 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
354 }
355
356 if a.properties.AndroidManifest != nil {
357 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
358 implicitInputs = append(implicitInputs, androidManifestFile)
359 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
360 }
361
362 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
363 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
364 ctx.Config().UnbundledBuild() &&
365 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
366 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
367 apiFingerprint := java.ApiFingerprintPath(ctx)
368 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
369 implicitInputs = append(implicitInputs, apiFingerprint)
370 }
371 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
372
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800373 noticeFile := a.buildNoticeFile(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900374 if noticeFile.Valid() {
375 // If there's a NOTICE file, embed it as an asset file in the APEX.
376 implicitInputs = append(implicitInputs, noticeFile.Path())
377 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
378 }
379
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000380 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && a.testOnlyShouldSkipHashtreeGeneration() {
381 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
382 return
383 }
384 if (!ctx.Config().UnbundledBuild() && a.installable()) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900385 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
386 // don't need hashtree for activation. Therefore, by removing hashtree from
387 // apex bundle (filesystem image in it, to be specific), we can save storage.
388 optFlags = append(optFlags, "--no_hashtree")
389 }
390
391 if a.properties.Apex_name != nil {
392 // If apex_name is set, apexer can skip checking if key name matches with apex name.
393 // Note that apex_manifest is also mended.
394 optFlags = append(optFlags, "--do_not_check_keyname")
395 }
396
Jooyung Han214bf372019-11-12 13:03:50 +0900397 if proptools.Bool(a.properties.Legacy_android10_support) {
398 implicitInputs = append(implicitInputs, a.manifestJsonOut)
399 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
400 }
401
Jiyong Park09d77522019-11-18 11:16:27 +0900402 ctx.Build(pctx, android.BuildParams{
403 Rule: apexRule,
404 Implicits: implicitInputs,
405 Output: unsignedOutputFile,
406 Description: "apex (" + apexType.name() + ")",
407 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900408 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
409 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
410 "copy_commands": strings.Join(copyCommands, " && "),
411 "manifest": a.manifestPbOut.String(),
412 "file_contexts": a.fileContexts.String(),
413 "canned_fs_config": cannedFsConfig.String(),
414 "key": a.private_key_file.String(),
415 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900416 },
417 })
418
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800419 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
420 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900421 a.bundleModuleFile = bundleModuleFile
422
423 ctx.Build(pctx, android.BuildParams{
424 Rule: apexProtoConvertRule,
425 Input: unsignedOutputFile,
426 Output: apexProtoFile,
427 Description: "apex proto convert",
428 })
429
430 ctx.Build(pctx, android.BuildParams{
431 Rule: apexBundleRule,
432 Input: apexProtoFile,
433 Output: a.bundleModuleFile,
434 Description: "apex bundle module",
435 Args: map[string]string{
436 "abi": strings.Join(abis, "."),
437 },
438 })
439 } else {
440 ctx.Build(pctx, android.BuildParams{
441 Rule: zipApexRule,
442 Implicits: implicitInputs,
443 Output: unsignedOutputFile,
444 Description: "apex (" + apexType.name() + ")",
445 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900446 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
447 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
448 "copy_commands": strings.Join(copyCommands, " && "),
449 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900450 },
451 })
452 }
453
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800454 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900455 ctx.Build(pctx, android.BuildParams{
456 Rule: java.Signapk,
457 Description: "signapk",
458 Output: a.outputFile,
459 Input: unsignedOutputFile,
460 Implicits: []android.Path{
461 a.container_certificate_file,
462 a.container_private_key_file,
463 },
464 Args: map[string]string{
465 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
466 "flags": "-a 4096", //alignment
467 },
468 })
469
470 // Install to $OUT/soong/{target,host}/.../apex
471 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800472 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900473 }
474 a.buildFilesInfo(ctx)
475}
476
477func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
478 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
479 // reply true to `InstallBypassMake()` (thus making the call
480 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
481 // instead of `android.PathForOutput`) to return the correct path to the flattened
482 // APEX (as its contents is installed by Make, not Soong).
483 factx := flattenedApexContext{ctx}
484 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
485 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
486
Jiyong Park317645e2019-12-05 13:20:58 +0900487 if a.installable() && a.GetOverriddenBy() == "" {
Jooyung Han54aca7b2019-11-20 02:26:02 +0900488 installPath := android.PathForModuleInstall(ctx, "apex", apexName)
489 devicePath := android.InstallPathToOnDevicePath(ctx, installPath)
490 addFlattenedFileContextsInfos(ctx, apexName+":"+devicePath+":"+a.fileContexts.String())
491 }
Jiyong Park09d77522019-11-18 11:16:27 +0900492 a.buildFilesInfo(ctx)
493}
494
495func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
496 cert := String(a.properties.Certificate)
497 if cert != "" && android.SrcIsModule(cert) == "" {
498 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
499 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
500 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
501 } else if cert == "" {
502 pem, key := ctx.Config().DefaultAppCertificate(ctx)
503 a.container_certificate_file = pem
504 a.container_private_key_file = key
505 }
506}
507
508func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
509 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900510 // For flattened APEX, do nothing but make sure that APEX manifest and apex_pubkey are also copied along
Jiyong Park09d77522019-11-18 11:16:27 +0900511 // with other ordinary files.
Jiyong Parkf653b052019-11-18 15:39:01 +0900512 a.filesInfo = append(a.filesInfo, newApexFile(a.manifestPbOut, "apex_manifest.pb."+a.Name()+a.suffix, ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900513
514 // rename to apex_pubkey
515 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
516 ctx.Build(pctx, android.BuildParams{
517 Rule: android.Cp,
518 Input: a.public_key_file,
519 Output: copiedPubkey,
520 })
Jiyong Parkf653b052019-11-18 15:39:01 +0900521 a.filesInfo = append(a.filesInfo, newApexFile(copiedPubkey, "apex_pubkey."+a.Name()+a.suffix, ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900522
523 if a.properties.ApexType == flattenedApex {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800524 apexName := proptools.StringDefault(a.properties.Apex_name, a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900525 for _, fi := range a.filesInfo {
526 dir := filepath.Join("apex", apexName, fi.installDir)
527 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
528 for _, sym := range fi.symlinks {
529 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
530 }
531 }
532 }
533 }
534}