blob: 8c81fb471ed7a69431da10851b5f2897603b330d [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}" && ` +
Dan Willemsen81e43c52020-01-28 15:40:19 -0800163 `exit 1); touch ${out}`,
Jiyong Park09d77522019-11-18 11:16:27 +0900164 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
Jooyung Han643adc42020-02-27 13:50:06 +0900183 // collect jniLibs. Notice that a.filesInfo is already sorted
184 var jniLibs []string
185 for _, fi := range a.filesInfo {
186 if fi.isJniLib {
187 jniLibs = append(jniLibs, fi.builtFile.Base())
188 }
189 }
190 if len(jniLibs) > 0 {
191 optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
192 }
193
Jiyong Park09d77522019-11-18 11:16:27 +0900194 ctx.Build(pctx, android.BuildParams{
195 Rule: apexManifestRule,
196 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900197 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900198 Args: map[string]string{
199 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
200 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
201 "opt": strings.Join(optCommands, " "),
202 },
203 })
204
Jooyung Han214bf372019-11-12 13:03:50 +0900205 if proptools.Bool(a.properties.Legacy_android10_support) {
206 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
207 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
208 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
209 ctx.Build(pctx, android.BuildParams{
210 Rule: stripApexManifestRule,
211 Input: manifestJsonFullOut,
212 Output: a.manifestJsonOut,
213 })
214 }
Jiyong Park09d77522019-11-18 11:16:27 +0900215
216 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
217 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
218 ctx.Build(pctx, android.BuildParams{
219 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900220 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900221 Output: a.manifestPbOut,
222 })
223}
224
Jiyong Park19972c72020-01-28 20:05:29 +0900225func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park09d77522019-11-18 11:16:27 +0900226 noticeFiles := []android.Path{}
227 for _, f := range a.filesInfo {
228 if f.module != nil {
Bob Badoura75b0572020-02-18 20:21:55 -0800229 notices := f.module.NoticeFiles()
230 if len(notices) > 0 {
231 noticeFiles = append(noticeFiles, notices...)
Jiyong Park09d77522019-11-18 11:16:27 +0900232 }
233 }
234 }
235 // append the notice file specified in the apex module itself
Bob Badoura75b0572020-02-18 20:21:55 -0800236 if len(a.NoticeFiles()) > 0 {
237 noticeFiles = append(noticeFiles, a.NoticeFiles()...)
Jiyong Park09d77522019-11-18 11:16:27 +0900238 }
239
240 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900241 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900242 }
243
Jiyong Park19972c72020-01-28 20:05:29 +0900244 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900245}
246
Jiyong Park3a1602e2020-01-14 14:39:19 +0900247func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
248 output := android.PathForModuleOut(ctx, "installed-files.txt")
249 rule := android.NewRuleBuilder()
250 rule.Command().
251 Implicit(builtApex).
252 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900253 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900254 Text(" | sort -nr > ").
255 Output(output)
256 rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files")
257 return output.OutputPath
258}
259
Jiyong Park09d77522019-11-18 11:16:27 +0900260func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
261 var abis []string
262 for _, target := range ctx.MultiTargets() {
263 if len(target.Arch.Abi) > 0 {
264 abis = append(abis, target.Arch.Abi[0])
265 }
266 }
267
268 abis = android.FirstUniqueStrings(abis)
269
270 apexType := a.properties.ApexType
271 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900272 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800273 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900274
Jiyong Park7cd10e32020-01-14 09:22:18 +0900275 // TODO(jiyong): construct the copy rules using RuleBuilder
276 var copyCommands []string
277 for _, fi := range a.filesInfo {
278 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
279 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(destPath))
280 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
281 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
282 pathOnDevice := filepath.Join("/system", fi.Path())
283 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
284 } else {
285 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
286 implicitInputs = append(implicitInputs, fi.builtFile)
287 }
288 // create additional symlinks pointing the file inside the APEX
289 for _, symlinkPath := range fi.SymlinkPaths() {
290 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
291 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
292 }
Jiyong Park09d77522019-11-18 11:16:27 +0900293 }
294
Jiyong Park7cd10e32020-01-14 09:22:18 +0900295 // TODO(jiyong): use RuleBuilder
296 var emitCommands []string
297 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900298 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
299 if proptools.Bool(a.properties.Legacy_android10_support) {
300 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
301 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900302 for _, fi := range a.filesInfo {
303 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900304 }
305 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900306 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900307
308 if a.properties.Whitelisted_files != nil {
309 ctx.Build(pctx, android.BuildParams{
310 Rule: emitApexContentRule,
311 Implicits: implicitInputs,
312 Output: imageContentFile,
313 Description: "emit apex image content",
314 Args: map[string]string{
315 "emit_commands": strings.Join(emitCommands, " && "),
316 },
317 })
318 implicitInputs = append(implicitInputs, imageContentFile)
319 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
320
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800321 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900322 ctx.Build(pctx, android.BuildParams{
323 Rule: diffApexContentRule,
324 Implicits: implicitInputs,
325 Output: phonyOutput,
326 Description: "diff apex image content",
327 Args: map[string]string{
328 "whitelisted_files_file": whitelistedFilesFile.String(),
329 "image_content_file": imageContentFile.String(),
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800330 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900331 },
332 })
333
334 implicitInputs = append(implicitInputs, phonyOutput)
335 }
336
337 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
338 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
339
Jiyong Park3a1602e2020-01-14 14:39:19 +0900340 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900341 if apexType == imageApex {
342 // files and dirs that will be created in APEX
343 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
344 var executablePaths []string // this also includes dirs
345 for _, f := range a.filesInfo {
346 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
347 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
348 executablePaths = append(executablePaths, pathInApex)
349 for _, s := range f.symlinks {
350 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
351 }
352 } else {
353 readOnlyPaths = append(readOnlyPaths, pathInApex)
354 }
355 dir := f.installDir
356 for !android.InList(dir, executablePaths) && dir != "" {
357 executablePaths = append(executablePaths, dir)
358 dir, _ = filepath.Split(dir) // move up to the parent
359 if len(dir) > 0 {
360 // remove trailing slash
361 dir = dir[:len(dir)-1]
362 }
363 }
364 }
365 sort.Strings(readOnlyPaths)
366 sort.Strings(executablePaths)
367 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
368 ctx.Build(pctx, android.BuildParams{
369 Rule: generateFsConfig,
370 Output: cannedFsConfig,
371 Description: "generate fs config",
372 Args: map[string]string{
373 "ro_paths": strings.Join(readOnlyPaths, " "),
374 "exec_paths": strings.Join(executablePaths, " "),
375 },
376 })
377
Jiyong Park09d77522019-11-18 11:16:27 +0900378 optFlags := []string{}
379
380 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900381 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900382 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
383
Jooyung Han27151d92019-12-16 17:45:32 +0900384 manifestPackageName := a.getOverrideManifestPackageName(ctx)
385 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900386 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
387 }
388
389 if a.properties.AndroidManifest != nil {
390 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
391 implicitInputs = append(implicitInputs, androidManifestFile)
392 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
393 }
394
395 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
Baligh Uddinf6201372020-01-24 23:15:44 +0000396 minSdkVersion := ctx.Config().DefaultAppTargetSdk()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000397
398 if proptools.Bool(a.properties.Legacy_android10_support) {
399 if !java.UseApiFingerprint(ctx, targetSdkVersion) {
400 targetSdkVersion = "29"
401 }
402 if !java.UseApiFingerprint(ctx, minSdkVersion) {
403 minSdkVersion = "29"
404 }
405 }
406
Baligh Uddinf6201372020-01-24 23:15:44 +0000407 if java.UseApiFingerprint(ctx, targetSdkVersion) {
408 targetSdkVersion += fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
409 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
410 }
411 if java.UseApiFingerprint(ctx, minSdkVersion) {
412 minSdkVersion += fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
413 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
Jiyong Park09d77522019-11-18 11:16:27 +0900414 }
415 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000416 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900417
Baligh Uddin004d7172020-02-19 21:29:28 -0800418 if a.overridableProperties.Logging_parent != "" {
419 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
420 }
421
Jiyong Park19972c72020-01-28 20:05:29 +0900422 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
423 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900424 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900425 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
426 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900427 }
428
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000429 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000430 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
431 return
432 }
Dario Frenie3546902020-01-14 23:50:25 +0000433 if !proptools.Bool(a.properties.Legacy_android10_support) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900434 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
435 // don't need hashtree for activation. Therefore, by removing hashtree from
436 // apex bundle (filesystem image in it, to be specific), we can save storage.
437 optFlags = append(optFlags, "--no_hashtree")
438 }
439
440 if a.properties.Apex_name != nil {
441 // If apex_name is set, apexer can skip checking if key name matches with apex name.
442 // Note that apex_manifest is also mended.
443 optFlags = append(optFlags, "--do_not_check_keyname")
444 }
445
Jooyung Han214bf372019-11-12 13:03:50 +0900446 if proptools.Bool(a.properties.Legacy_android10_support) {
447 implicitInputs = append(implicitInputs, a.manifestJsonOut)
448 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
449 }
450
Jiyong Park09d77522019-11-18 11:16:27 +0900451 ctx.Build(pctx, android.BuildParams{
452 Rule: apexRule,
453 Implicits: implicitInputs,
454 Output: unsignedOutputFile,
455 Description: "apex (" + apexType.name() + ")",
456 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900457 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900458 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900459 "copy_commands": strings.Join(copyCommands, " && "),
460 "manifest": a.manifestPbOut.String(),
461 "file_contexts": a.fileContexts.String(),
462 "canned_fs_config": cannedFsConfig.String(),
463 "key": a.private_key_file.String(),
464 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900465 },
466 })
467
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800468 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
469 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900470 a.bundleModuleFile = bundleModuleFile
471
472 ctx.Build(pctx, android.BuildParams{
473 Rule: apexProtoConvertRule,
474 Input: unsignedOutputFile,
475 Output: apexProtoFile,
476 Description: "apex proto convert",
477 })
478
479 ctx.Build(pctx, android.BuildParams{
480 Rule: apexBundleRule,
481 Input: apexProtoFile,
482 Output: a.bundleModuleFile,
483 Description: "apex bundle module",
484 Args: map[string]string{
485 "abi": strings.Join(abis, "."),
486 },
487 })
488 } else {
489 ctx.Build(pctx, android.BuildParams{
490 Rule: zipApexRule,
491 Implicits: implicitInputs,
492 Output: unsignedOutputFile,
493 Description: "apex (" + apexType.name() + ")",
494 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900495 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900496 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900497 "copy_commands": strings.Join(copyCommands, " && "),
498 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900499 },
500 })
501 }
502
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800503 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900504 ctx.Build(pctx, android.BuildParams{
505 Rule: java.Signapk,
506 Description: "signapk",
507 Output: a.outputFile,
508 Input: unsignedOutputFile,
509 Implicits: []android.Path{
510 a.container_certificate_file,
511 a.container_private_key_file,
512 },
513 Args: map[string]string{
514 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
515 "flags": "-a 4096", //alignment
516 },
517 })
518
519 // Install to $OUT/soong/{target,host}/.../apex
520 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800521 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900522 }
523 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900524
525 // installed-files.txt is dist'ed
526 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900527}
528
529func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
530 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
531 // reply true to `InstallBypassMake()` (thus making the call
532 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
533 // instead of `android.PathForOutput`) to return the correct path to the flattened
534 // APEX (as its contents is installed by Make, not Soong).
535 factx := flattenedApexContext{ctx}
Jiyong Parka5948012020-02-07 10:15:14 +0900536 apexBundleName := a.Name()
537 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexBundleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900538
Jiyong Park317645e2019-12-05 13:20:58 +0900539 if a.installable() && a.GetOverriddenBy() == "" {
Jiyong Parka5948012020-02-07 10:15:14 +0900540 installPath := android.PathForModuleInstall(ctx, "apex", apexBundleName)
Jooyung Han54aca7b2019-11-20 02:26:02 +0900541 devicePath := android.InstallPathToOnDevicePath(ctx, installPath)
Jiyong Parka5948012020-02-07 10:15:14 +0900542 addFlattenedFileContextsInfos(ctx, apexBundleName+":"+devicePath+":"+a.fileContexts.String())
Jooyung Han54aca7b2019-11-20 02:26:02 +0900543 }
Jiyong Park09d77522019-11-18 11:16:27 +0900544 a.buildFilesInfo(ctx)
545}
546
547func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900548 if a.container_certificate_file == nil {
549 cert := String(a.properties.Certificate)
550 if cert == "" {
551 pem, key := ctx.Config().DefaultAppCertificate(ctx)
552 a.container_certificate_file = pem
553 a.container_private_key_file = key
554 } else {
555 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
556 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
557 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
558 }
Jiyong Park09d77522019-11-18 11:16:27 +0900559 }
560}
561
562func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
563 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900564 // 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 +0900565 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900566 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900567
568 // rename to apex_pubkey
569 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
570 ctx.Build(pctx, android.BuildParams{
571 Rule: android.Cp,
572 Input: a.public_key_file,
573 Output: copiedPubkey,
574 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900575 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900576
577 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900578 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900579 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900580 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900581 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
582 for _, sym := range fi.symlinks {
583 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
584 }
585 }
586 }
587 }
588}
Jooyung Han27151d92019-12-16 17:45:32 +0900589
590func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
591 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
592 // to see if it should be overridden because their <apex name> is dynamically generated
593 // according to its VNDK version.
594 if a.vndkApex {
595 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
596 if overridden {
597 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
598 }
599 return ""
600 }
601 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(a.Name())
602 if overridden {
603 return manifestPackageName
604 }
605 return ""
606}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900607
608func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
609 if !a.primaryApexType {
610 return
611 }
612
613 if a.properties.IsCoverageVariant {
614 // Otherwise, we will have duplicated rules for coverage and
615 // non-coverage variants of the same APEX
616 return
617 }
618
619 if ctx.Host() {
620 // No need to generate dependency info for host variant
621 return
622 }
623
Jiyong Park83dc74b2020-01-14 18:38:44 +0900624 var content strings.Builder
Jiyong Park678c8812020-02-07 17:25:49 +0900625 for _, key := range android.SortedStringKeys(a.depInfos) {
626 info := a.depInfos[key]
627 toName := info.to
628 if info.isExternal {
629 toName = toName + " (external)"
630 }
631 fmt.Fprintf(&content, "%s <- %s\\n", toName, strings.Join(android.SortedUniqueStrings(info.from), ", "))
Jiyong Park83dc74b2020-01-14 18:38:44 +0900632 }
633
634 depsInfoFile := android.PathForOutput(ctx, a.Name()+"-deps-info.txt")
635 ctx.Build(pctx, android.BuildParams{
636 Rule: android.WriteFile,
637 Description: "Dependency Info",
638 Output: depsInfoFile,
639 Args: map[string]string{
640 "content": content.String(),
641 },
642 })
643
644 ctx.Build(pctx, android.BuildParams{
645 Rule: android.Phony,
646 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
647 Inputs: []android.Path{depsInfoFile},
648 })
649}