blob: adc62be5421954dcc004ad7da98e4a2d4cd69416 [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} ` +
Dario Freni0f4ae072020-01-02 15:24:12 +0000108 `--include_build_info ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900109 `--payload_type image ` +
110 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
111 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
112 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
113 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
114 Rspfile: "${out}.copy_commands",
115 RspfileContent: "${copy_commands}",
116 Description: "APEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900117 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900118
119 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
120 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
121 `(. ${out}.copy_commands) && ` +
122 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han214bf372019-11-12 13:03:50 +0900123 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900124 `--payload_type zip ` +
125 `${image_dir} ${out} `,
126 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
127 Rspfile: "${out}.copy_commands",
128 RspfileContent: "${copy_commands}",
129 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900130 }, "tool_path", "image_dir", "copy_commands", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900131
132 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
133 blueprint.RuleParams{
134 Command: `${aapt2} convert --output-format proto $in -o $out`,
135 CommandDeps: []string{"${aapt2}"},
136 })
137
138 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
139 Command: `${zip2zip} -i $in -o $out ` +
140 `apex_payload.img:apex/${abi}.img ` +
141 `apex_manifest.json:root/apex_manifest.json ` +
Jiyong Park53ae3342019-12-08 02:06:24 +0900142 `apex_manifest.pb:root/apex_manifest.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900143 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
144 `assets/NOTICE.html.gz:assets/NOTICE.html.gz`,
145 CommandDeps: []string{"${zip2zip}"},
146 Description: "app bundle",
147 }, "abi")
148
149 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
150 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
151 Rspfile: "${out}.emit_commands",
152 RspfileContent: "${emit_commands}",
153 Description: "Emit APEX image content",
154 }, "emit_commands")
155
156 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
157 Command: `diff --unchanged-group-format='' \` +
158 `--changed-group-format='%<' \` +
159 `${image_content_file} ${whitelisted_files_file} || (` +
160 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
161 ` "To fix the build run following command:" && ` +
162 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
163 `exit 1)`,
164 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
165 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
166)
167
168func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
169 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
170
Jooyung Han214bf372019-11-12 13:03:50 +0900171 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900172
173 // put dependency({provide|require}NativeLibs) in apex_manifest.json
174 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
175 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
176
177 // apex name can be overridden
178 optCommands := []string{}
179 if a.properties.Apex_name != nil {
180 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
181 }
182
183 ctx.Build(pctx, android.BuildParams{
184 Rule: apexManifestRule,
185 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900186 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900187 Args: map[string]string{
188 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
189 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
190 "opt": strings.Join(optCommands, " "),
191 },
192 })
193
Jooyung Han214bf372019-11-12 13:03:50 +0900194 if proptools.Bool(a.properties.Legacy_android10_support) {
195 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
196 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
197 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
198 ctx.Build(pctx, android.BuildParams{
199 Rule: stripApexManifestRule,
200 Input: manifestJsonFullOut,
201 Output: a.manifestJsonOut,
202 })
203 }
Jiyong Park09d77522019-11-18 11:16:27 +0900204
205 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
206 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
207 ctx.Build(pctx, android.BuildParams{
208 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900209 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900210 Output: a.manifestPbOut,
211 })
212}
213
214func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath {
215 noticeFiles := []android.Path{}
216 for _, f := range a.filesInfo {
217 if f.module != nil {
218 notice := f.module.NoticeFile()
219 if notice.Valid() {
220 noticeFiles = append(noticeFiles, notice.Path())
221 }
222 }
223 }
224 // append the notice file specified in the apex module itself
225 if a.NoticeFile().Valid() {
226 noticeFiles = append(noticeFiles, a.NoticeFile().Path())
227 }
228
229 if len(noticeFiles) == 0 {
230 return android.OptionalPath{}
231 }
232
233 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput
234}
235
236func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
237 var abis []string
238 for _, target := range ctx.MultiTargets() {
239 if len(target.Arch.Abi) > 0 {
240 abis = append(abis, target.Arch.Abi[0])
241 }
242 }
243
244 abis = android.FirstUniqueStrings(abis)
245
246 apexType := a.properties.ApexType
247 suffix := apexType.suffix()
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800248 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900249
250 filesToCopy := []android.Path{}
251 for _, f := range a.filesInfo {
252 filesToCopy = append(filesToCopy, f.builtFile)
253 }
254
255 copyCommands := []string{}
256 emitCommands := []string{}
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800257 imageContentFile := android.PathForModuleOut(ctx, a.Name()+"-content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900258 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
259 if proptools.Bool(a.properties.Legacy_android10_support) {
260 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
261 }
Jiyong Park09d77522019-11-18 11:16:27 +0900262 for i, src := range filesToCopy {
263 dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
264 emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String())
265 dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
266 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
267 copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
268 for _, sym := range a.filesInfo[i].symlinks {
269 symlinkDest := filepath.Join(filepath.Dir(dest_path), sym)
270 copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest)
271 }
272 }
273 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
274
275 implicitInputs := append(android.Paths(nil), filesToCopy...)
Jooyung Han214bf372019-11-12 13:03:50 +0900276 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900277
278 if a.properties.Whitelisted_files != nil {
279 ctx.Build(pctx, android.BuildParams{
280 Rule: emitApexContentRule,
281 Implicits: implicitInputs,
282 Output: imageContentFile,
283 Description: "emit apex image content",
284 Args: map[string]string{
285 "emit_commands": strings.Join(emitCommands, " && "),
286 },
287 })
288 implicitInputs = append(implicitInputs, imageContentFile)
289 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
290
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800291 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900292 ctx.Build(pctx, android.BuildParams{
293 Rule: diffApexContentRule,
294 Implicits: implicitInputs,
295 Output: phonyOutput,
296 Description: "diff apex image content",
297 Args: map[string]string{
298 "whitelisted_files_file": whitelistedFilesFile.String(),
299 "image_content_file": imageContentFile.String(),
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800300 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900301 },
302 })
303
304 implicitInputs = append(implicitInputs, phonyOutput)
305 }
306
307 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
308 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
309
310 if apexType == imageApex {
311 // files and dirs that will be created in APEX
312 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
313 var executablePaths []string // this also includes dirs
314 for _, f := range a.filesInfo {
315 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
316 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
317 executablePaths = append(executablePaths, pathInApex)
318 for _, s := range f.symlinks {
319 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
320 }
321 } else {
322 readOnlyPaths = append(readOnlyPaths, pathInApex)
323 }
324 dir := f.installDir
325 for !android.InList(dir, executablePaths) && dir != "" {
326 executablePaths = append(executablePaths, dir)
327 dir, _ = filepath.Split(dir) // move up to the parent
328 if len(dir) > 0 {
329 // remove trailing slash
330 dir = dir[:len(dir)-1]
331 }
332 }
333 }
334 sort.Strings(readOnlyPaths)
335 sort.Strings(executablePaths)
336 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
337 ctx.Build(pctx, android.BuildParams{
338 Rule: generateFsConfig,
339 Output: cannedFsConfig,
340 Description: "generate fs config",
341 Args: map[string]string{
342 "ro_paths": strings.Join(readOnlyPaths, " "),
343 "exec_paths": strings.Join(executablePaths, " "),
344 },
345 })
346
Jiyong Park09d77522019-11-18 11:16:27 +0900347 optFlags := []string{}
348
349 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900350 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900351 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
352
Jooyung Han27151d92019-12-16 17:45:32 +0900353 manifestPackageName := a.getOverrideManifestPackageName(ctx)
354 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900355 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
356 }
357
358 if a.properties.AndroidManifest != nil {
359 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
360 implicitInputs = append(implicitInputs, androidManifestFile)
361 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
362 }
363
364 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
365 if targetSdkVersion == ctx.Config().PlatformSdkCodename() &&
366 ctx.Config().UnbundledBuild() &&
367 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
368 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
369 apiFingerprint := java.ApiFingerprintPath(ctx)
370 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
371 implicitInputs = append(implicitInputs, apiFingerprint)
372 }
373 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
374
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800375 noticeFile := a.buildNoticeFile(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900376 if noticeFile.Valid() {
377 // If there's a NOTICE file, embed it as an asset file in the APEX.
378 implicitInputs = append(implicitInputs, noticeFile.Path())
379 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String()))
380 }
381
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000382 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && a.testOnlyShouldSkipHashtreeGeneration() {
383 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
384 return
385 }
386 if (!ctx.Config().UnbundledBuild() && a.installable()) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900387 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
388 // don't need hashtree for activation. Therefore, by removing hashtree from
389 // apex bundle (filesystem image in it, to be specific), we can save storage.
390 optFlags = append(optFlags, "--no_hashtree")
391 }
392
393 if a.properties.Apex_name != nil {
394 // If apex_name is set, apexer can skip checking if key name matches with apex name.
395 // Note that apex_manifest is also mended.
396 optFlags = append(optFlags, "--do_not_check_keyname")
397 }
398
Jooyung Han214bf372019-11-12 13:03:50 +0900399 if proptools.Bool(a.properties.Legacy_android10_support) {
400 implicitInputs = append(implicitInputs, a.manifestJsonOut)
401 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
402 }
403
Jiyong Park09d77522019-11-18 11:16:27 +0900404 ctx.Build(pctx, android.BuildParams{
405 Rule: apexRule,
406 Implicits: implicitInputs,
407 Output: unsignedOutputFile,
408 Description: "apex (" + apexType.name() + ")",
409 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900410 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
411 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
412 "copy_commands": strings.Join(copyCommands, " && "),
413 "manifest": a.manifestPbOut.String(),
414 "file_contexts": a.fileContexts.String(),
415 "canned_fs_config": cannedFsConfig.String(),
416 "key": a.private_key_file.String(),
417 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900418 },
419 })
420
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800421 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
422 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900423 a.bundleModuleFile = bundleModuleFile
424
425 ctx.Build(pctx, android.BuildParams{
426 Rule: apexProtoConvertRule,
427 Input: unsignedOutputFile,
428 Output: apexProtoFile,
429 Description: "apex proto convert",
430 })
431
432 ctx.Build(pctx, android.BuildParams{
433 Rule: apexBundleRule,
434 Input: apexProtoFile,
435 Output: a.bundleModuleFile,
436 Description: "apex bundle module",
437 Args: map[string]string{
438 "abi": strings.Join(abis, "."),
439 },
440 })
441 } else {
442 ctx.Build(pctx, android.BuildParams{
443 Rule: zipApexRule,
444 Implicits: implicitInputs,
445 Output: unsignedOutputFile,
446 Description: "apex (" + apexType.name() + ")",
447 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900448 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
449 "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(),
450 "copy_commands": strings.Join(copyCommands, " && "),
451 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900452 },
453 })
454 }
455
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800456 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900457 ctx.Build(pctx, android.BuildParams{
458 Rule: java.Signapk,
459 Description: "signapk",
460 Output: a.outputFile,
461 Input: unsignedOutputFile,
462 Implicits: []android.Path{
463 a.container_certificate_file,
464 a.container_private_key_file,
465 },
466 Args: map[string]string{
467 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
468 "flags": "-a 4096", //alignment
469 },
470 })
471
472 // Install to $OUT/soong/{target,host}/.../apex
473 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800474 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900475 }
476 a.buildFilesInfo(ctx)
477}
478
479func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
480 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
481 // reply true to `InstallBypassMake()` (thus making the call
482 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
483 // instead of `android.PathForOutput`) to return the correct path to the flattened
484 // APEX (as its contents is installed by Make, not Soong).
485 factx := flattenedApexContext{ctx}
486 apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName())
487 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName)
488
Jiyong Park317645e2019-12-05 13:20:58 +0900489 if a.installable() && a.GetOverriddenBy() == "" {
Jooyung Han54aca7b2019-11-20 02:26:02 +0900490 installPath := android.PathForModuleInstall(ctx, "apex", apexName)
491 devicePath := android.InstallPathToOnDevicePath(ctx, installPath)
492 addFlattenedFileContextsInfos(ctx, apexName+":"+devicePath+":"+a.fileContexts.String())
493 }
Jiyong Park09d77522019-11-18 11:16:27 +0900494 a.buildFilesInfo(ctx)
495}
496
497func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900498 if a.container_certificate_file == nil {
499 cert := String(a.properties.Certificate)
500 if cert == "" {
501 pem, key := ctx.Config().DefaultAppCertificate(ctx)
502 a.container_certificate_file = pem
503 a.container_private_key_file = key
504 } else {
505 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
506 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
507 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
508 }
Jiyong Park09d77522019-11-18 11:16:27 +0900509 }
510}
511
512func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
513 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900514 // 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 +0900515 // with other ordinary files.
Jiyong Park1833cef2019-12-13 13:28:36 +0900516 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb."+a.Name()+a.suffix, ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900517
518 // rename to apex_pubkey
519 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
520 ctx.Build(pctx, android.BuildParams{
521 Rule: android.Cp,
522 Input: a.public_key_file,
523 Output: copiedPubkey,
524 })
Jiyong Park1833cef2019-12-13 13:28:36 +0900525 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey."+a.Name()+a.suffix, ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900526
527 if a.properties.ApexType == flattenedApex {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800528 apexName := proptools.StringDefault(a.properties.Apex_name, a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900529 for _, fi := range a.filesInfo {
530 dir := filepath.Join("apex", apexName, fi.installDir)
531 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
532 for _, sym := range fi.symlinks {
533 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
534 }
535 }
536 }
537 }
538}
Jooyung Han27151d92019-12-16 17:45:32 +0900539
540func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
541 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
542 // to see if it should be overridden because their <apex name> is dynamically generated
543 // according to its VNDK version.
544 if a.vndkApex {
545 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
546 if overridden {
547 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
548 }
549 return ""
550 }
551 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(a.Name())
552 if overridden {
553 return manifestPackageName
554 }
555 return ""
556}