blob: 8c5f99bf50d81b37eb7634d493d8aeeff2e6f22b [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 (
Jiyong Parkbd159612020-02-28 15:22:21 +090018 "encoding/json"
Jiyong Park09d77522019-11-18 11:16:27 +090019 "fmt"
20 "path/filepath"
21 "runtime"
22 "sort"
Nikita Ioffe5335bc42020-10-20 00:02:15 +010023 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090024 "strings"
25
26 "android/soong/android"
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
37func init() {
38 pctx.Import("android/soong/android")
sophiezc80a2b32020-11-12 16:39:19 +000039 pctx.Import("android/soong/cc/config")
Jiyong Park09d77522019-11-18 11:16:27 +090040 pctx.Import("android/soong/java")
41 pctx.HostBinToolVariable("apexer", "apexer")
42 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
Jiyong Parkb81b9902020-11-24 19:51:18 +090043 // projects, and hence cannot build 'aapt2'. Use the SDK prebuilt instead.
Jiyong Park09d77522019-11-18 11:16:27 +090044 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
45 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
46 if !ctx.Config().FrameworksBaseDirExists(ctx) {
47 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
48 } else {
Martin Stjernholm7260d062019-12-09 21:47:14 +000049 return ctx.Config().HostToolPath(ctx, tool).String()
Jiyong Park09d77522019-11-18 11:16:27 +090050 }
51 })
52 }
53 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
54 pctx.HostBinToolVariable("avbtool", "avbtool")
55 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
56 pctx.HostBinToolVariable("merge_zips", "merge_zips")
57 pctx.HostBinToolVariable("mke2fs", "mke2fs")
58 pctx.HostBinToolVariable("resize2fs", "resize2fs")
59 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
60 pctx.HostBinToolVariable("soong_zip", "soong_zip")
61 pctx.HostBinToolVariable("zip2zip", "zip2zip")
62 pctx.HostBinToolVariable("zipalign", "zipalign")
63 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
64 pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
Jaewoong Jungfa00c062020-05-14 14:15:24 -070065 pctx.HostBinToolVariable("extract_apks", "extract_apks")
Theotime Combes4ba38c12020-06-12 12:46:59 +000066 pctx.HostBinToolVariable("make_f2fs", "make_f2fs")
67 pctx.HostBinToolVariable("sload_f2fs", "sload_f2fs")
Huang Jianan13cac632021-08-02 15:02:17 +080068 pctx.HostBinToolVariable("make_erofs", "make_erofs")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +000069 pctx.HostBinToolVariable("apex_compression_tool", "apex_compression_tool")
sophiez02347372021-11-02 17:58:02 -070070 pctx.HostBinToolVariable("dexdeps", "dexdeps")
sophiezc80a2b32020-11-12 16:39:19 +000071 pctx.SourcePathVariable("genNdkUsedbyApexPath", "build/soong/scripts/gen_ndk_usedby_apex.sh")
Jiyong Park09d77522019-11-18 11:16:27 +090072}
73
74var (
Jiyong Park09d77522019-11-18 11:16:27 +090075 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
Joe Onoratob4638c12021-10-27 15:47:06 -070097 // TODO(b/113233103): make sure that file_contexts is as expected, i.e., validate
Jiyong Park09d77522019-11-18 11:16:27 +090098 // 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}",
Huang Jianan13cac632021-08-02 15:02:17 +0800112 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}", "${sload_f2fs}", "${make_erofs}",
Jiyong Park09d77522019-11-18 11:16:27 +0900113 "${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}",
Theotime Combes4ba38c12020-06-12 12:46:59 +0000117 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest", "payload_fs_type")
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{
Jiyong Parkbd159612020-02-28 15:22:21 +0900139 Command: `${zip2zip} -i $in -o $out.base ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900140 `apex_payload.img:apex/${abi}.img ` +
Dario Frenida1aefe2020-03-02 21:47:09 +0000141 `apex_build_info.pb:apex/${abi}.build_info.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900142 `apex_manifest.json:root/apex_manifest.json ` +
Jiyong Park53ae3342019-12-08 02:06:24 +0900143 `apex_manifest.pb:root/apex_manifest.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900144 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
Jiyong Parkbd159612020-02-28 15:22:21 +0900145 `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` +
146 `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` +
147 `${merge_zips} $out $out.base $out.config`,
148 CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"},
Jiyong Park09d77522019-11-18 11:16:27 +0900149 Description: "app bundle",
Jiyong Parkbd159612020-02-28 15:22:21 +0900150 }, "abi", "config")
Jiyong Park09d77522019-11-18 11:16:27 +0900151
152 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
153 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
154 Rspfile: "${out}.emit_commands",
155 RspfileContent: "${emit_commands}",
156 Description: "Emit APEX image content",
157 }, "emit_commands")
158
159 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
160 Command: `diff --unchanged-group-format='' \` +
161 `--changed-group-format='%<' \` +
Colin Cross440e0d02020-06-11 11:32:11 -0700162 `${image_content_file} ${allowed_files_file} || (` +
Jiyong Park09d77522019-11-18 11:16:27 +0900163 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
164 ` "To fix the build run following command:" && ` +
Colin Cross440e0d02020-06-11 11:32:11 -0700165 `echo "system/apex/tools/update_allowed_list.sh ${allowed_files_file} ${image_content_file}" && ` +
Dan Willemsen81e43c52020-01-28 15:40:19 -0800166 `exit 1); touch ${out}`,
Colin Cross440e0d02020-06-11 11:32:11 -0700167 Description: "Diff ${image_content_file} and ${allowed_files_file}",
168 }, "image_content_file", "allowed_files_file", "apex_module_name")
Jiyong Parkb81b9902020-11-24 19:51:18 +0900169
sophiezc80a2b32020-11-12 16:39:19 +0000170 generateAPIsUsedbyApexRule = pctx.StaticRule("generateAPIsUsedbyApexRule", blueprint.RuleParams{
171 Command: "$genNdkUsedbyApexPath ${image_dir} ${readelf} ${out}",
172 CommandDeps: []string{"${genNdkUsedbyApexPath}"},
173 Description: "Generate symbol list used by Apex",
174 }, "image_dir", "readelf")
175
Jiyong Parkb81b9902020-11-24 19:51:18 +0900176 // Don't add more rules here. Consider using android.NewRuleBuilder instead.
Jiyong Park09d77522019-11-18 11:16:27 +0900177)
178
Jiyong Parkb81b9902020-11-24 19:51:18 +0900179// buildManifest creates buile rules to modify the input apex_manifest.json to add information
180// gathered by the build system such as provided/required native libraries. Two output files having
181// different formats are generated. a.manifestJsonOut is JSON format for Q devices, and
182// a.manifest.PbOut is protobuf format for R+ devices.
183// TODO(jiyong): make this to return paths instead of directly storing the paths to apexBundle
Jiyong Park09d77522019-11-18 11:16:27 +0900184func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900185 src := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
Jiyong Park09d77522019-11-18 11:16:27 +0900186
Jiyong Parkb81b9902020-11-24 19:51:18 +0900187 // Put dependency({provide|require}NativeLibs) in apex_manifest.json
Jiyong Park09d77522019-11-18 11:16:27 +0900188 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
189 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
190
Jiyong Parkb81b9902020-11-24 19:51:18 +0900191 // APEX name can be overridden
Jiyong Park09d77522019-11-18 11:16:27 +0900192 optCommands := []string{}
193 if a.properties.Apex_name != nil {
194 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
195 }
196
Jiyong Parkb81b9902020-11-24 19:51:18 +0900197 // Collect jniLibs. Notice that a.filesInfo is already sorted
Jooyung Han643adc42020-02-27 13:50:06 +0900198 var jniLibs []string
199 for _, fi := range a.filesInfo {
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900200 if fi.isJniLib && !android.InList(fi.stem(), jniLibs) {
201 jniLibs = append(jniLibs, fi.stem())
Jooyung Han643adc42020-02-27 13:50:06 +0900202 }
203 }
204 if len(jniLibs) > 0 {
205 optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
206 }
207
Jiyong Parkb81b9902020-11-24 19:51:18 +0900208 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900209 ctx.Build(pctx, android.BuildParams{
210 Rule: apexManifestRule,
Jiyong Parkb81b9902020-11-24 19:51:18 +0900211 Input: src,
Jooyung Han214bf372019-11-12 13:03:50 +0900212 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900213 Args: map[string]string{
214 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
215 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
216 "opt": strings.Join(optCommands, " "),
217 },
218 })
219
Jiyong Parkb81b9902020-11-24 19:51:18 +0900220 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json prepare
221 // stripped-down version so that APEX modules built from R+ can be installed to Q
Dan Albertc8060532020-07-22 22:32:17 -0700222 minSdkVersion := a.minSdkVersion(ctx)
223 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900224 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
225 ctx.Build(pctx, android.BuildParams{
226 Rule: stripApexManifestRule,
227 Input: manifestJsonFullOut,
228 Output: a.manifestJsonOut,
229 })
230 }
Jiyong Park09d77522019-11-18 11:16:27 +0900231
Jiyong Parkb81b9902020-11-24 19:51:18 +0900232 // From R+, protobuf binary format (.pb) is the standard format for apex_manifest
Jiyong Park09d77522019-11-18 11:16:27 +0900233 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
234 ctx.Build(pctx, android.BuildParams{
235 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900236 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900237 Output: a.manifestPbOut,
238 })
239}
240
Jiyong Parkb81b9902020-11-24 19:51:18 +0900241// buildFileContexts create build rules to append an entry for apex_manifest.pb to the file_contexts
242// file for this APEX which is either from /systme/sepolicy/apex/<apexname>-file_contexts or from
243// the file_contexts property of this APEX. This is to make sure that the manifest file is correctly
244// labeled as system_file.
245func (a *apexBundle) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
Jooyung Han580eb4f2020-06-24 19:33:06 +0900246 var fileContexts android.Path
Liz Kammer37997c42021-09-14 17:53:38 -0400247 var fileContextsDir string
Jooyung Han580eb4f2020-06-24 19:33:06 +0900248 if a.properties.File_contexts == nil {
249 fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
250 } else {
Liz Kammer37997c42021-09-14 17:53:38 -0400251 if m, t := android.SrcIsModuleWithTag(*a.properties.File_contexts); m != "" {
252 otherModule := android.GetModuleFromPathDep(ctx, m, t)
253 fileContextsDir = ctx.OtherModuleDir(otherModule)
254 }
Jooyung Han580eb4f2020-06-24 19:33:06 +0900255 fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
256 }
Liz Kammer37997c42021-09-14 17:53:38 -0400257 if fileContextsDir == "" {
258 fileContextsDir = filepath.Dir(fileContexts.String())
259 }
260 fileContextsDir += string(filepath.Separator)
261
Jooyung Han580eb4f2020-06-24 19:33:06 +0900262 if a.Platform() {
Liz Kammer37997c42021-09-14 17:53:38 -0400263 if !strings.HasPrefix(fileContextsDir, "system/sepolicy/") {
264 ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but found in %q", fileContextsDir)
Jooyung Han580eb4f2020-06-24 19:33:06 +0900265 }
266 }
267 if !android.ExistentPathForSource(ctx, fileContexts.String()).Valid() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900268 ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", fileContexts.String())
Jooyung Han580eb4f2020-06-24 19:33:06 +0900269 }
270
271 output := android.PathForModuleOut(ctx, "file_contexts")
Colin Crossf1a035e2020-11-16 17:32:30 -0800272 rule := android.NewRuleBuilder(pctx, ctx)
Jooyung Han7f146c02020-09-23 19:15:55 +0900273
Jiyong Parkb81b9902020-11-24 19:51:18 +0900274 switch a.properties.ApexType {
275 case imageApex:
Jooyung Han7f146c02020-09-23 19:15:55 +0900276 // remove old file
277 rule.Command().Text("rm").FlagWithOutput("-f ", output)
278 // copy file_contexts
279 rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output)
280 // new line
281 rule.Command().Text("echo").Text(">>").Output(output)
282 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
283 rule.Command().Text("echo").Flag("/apex_manifest\\\\.pb u:object_r:system_file:s0").Text(">>").Output(output)
284 rule.Command().Text("echo").Flag("/ u:object_r:system_file:s0").Text(">>").Output(output)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900285 case flattenedApex:
Jooyung Han7f146c02020-09-23 19:15:55 +0900286 // For flattened apexes, install path should be prepended.
287 // File_contexts file should be emiited to make via LOCAL_FILE_CONTEXTS
288 // so that it can be merged into file_contexts.bin
289 apexPath := android.InstallPathToOnDevicePath(ctx, a.installDir.Join(ctx, a.Name()))
290 apexPath = strings.ReplaceAll(apexPath, ".", `\\.`)
291 // remove old file
292 rule.Command().Text("rm").FlagWithOutput("-f ", output)
293 // copy file_contexts
294 rule.Command().Text("awk").Text(`'/object_r/{printf("` + apexPath + `%s\n", $0)}'`).Input(fileContexts).Text(">").Output(output)
295 // new line
296 rule.Command().Text("echo").Text(">>").Output(output)
297 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
298 rule.Command().Text("echo").Flag(apexPath + `/apex_manifest\\.pb u:object_r:system_file:s0`).Text(">>").Output(output)
299 rule.Command().Text("echo").Flag(apexPath + "/ u:object_r:system_file:s0").Text(">>").Output(output)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900300 default:
301 panic(fmt.Errorf("unsupported type %v", a.properties.ApexType))
Jooyung Han7f146c02020-09-23 19:15:55 +0900302 }
303
Colin Crossf1a035e2020-11-16 17:32:30 -0800304 rule.Build("file_contexts."+a.Name(), "Generate file_contexts")
Jiyong Parkb81b9902020-11-24 19:51:18 +0900305 return output.OutputPath
Jooyung Han580eb4f2020-06-24 19:33:06 +0900306}
307
Jiyong Parkb81b9902020-11-24 19:51:18 +0900308// buildNoticeFiles creates a buile rule for aggregating notice files from the modules that
309// contributes to this APEX. The notice files are merged into a big notice file.
Jiyong Park19972c72020-01-28 20:05:29 +0900310func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900311 var noticeFiles android.Paths
312
Jooyung Han749dc692020-04-15 11:03:39 +0900313 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900314 if externalDep {
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100315 // As soon as the dependency graph crosses the APEX boundary, don't go further.
316 return false
Jiyong Park09d77522019-11-18 11:16:27 +0900317 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900318 noticeFiles = append(noticeFiles, to.NoticeFiles()...)
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100319 return true
Jiyong Park9918e1a2020-03-17 19:16:40 +0900320 })
Jiyong Park09d77522019-11-18 11:16:27 +0900321
Jiyong Parkb81b9902020-11-24 19:51:18 +0900322 // TODO(jiyong): why do we need this? WalkPayloadDeps should have already covered this.
Jiyong Park41f637d2020-09-09 13:18:02 +0900323 for _, fi := range a.filesInfo {
324 noticeFiles = append(noticeFiles, fi.noticeFiles...)
325 }
326
Jiyong Park09d77522019-11-18 11:16:27 +0900327 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900328 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900329 }
330
Jiyong Park33c77362020-05-29 22:00:16 +0900331 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.SortedUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900332}
333
Jiyong Parkb81b9902020-11-24 19:51:18 +0900334// buildInstalledFilesFile creates a build rule for the installed-files.txt file where the list of
335// files included in this APEX is shown. The text file is dist'ed so that people can see what's
336// included in the APEX without actually downloading and extracting it.
Jiyong Park3a1602e2020-01-14 14:39:19 +0900337func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
338 output := android.PathForModuleOut(ctx, "installed-files.txt")
Colin Crossf1a035e2020-11-16 17:32:30 -0800339 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900340 rule.Command().
341 Implicit(builtApex).
342 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900343 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900344 Text(" | sort -nr > ").
345 Output(output)
Colin Crossf1a035e2020-11-16 17:32:30 -0800346 rule.Build("installed-files."+a.Name(), "Installed files")
Jiyong Park3a1602e2020-01-14 14:39:19 +0900347 return output.OutputPath
348}
349
Jiyong Parkb81b9902020-11-24 19:51:18 +0900350// buildBundleConfig creates a build rule for the bundle config file that will control the bundle
351// creation process.
Jiyong Parkbd159612020-02-28 15:22:21 +0900352func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
353 output := android.PathForModuleOut(ctx, "bundle_config.json")
354
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900355 type ApkConfig struct {
356 Package_name string `json:"package_name"`
357 Apk_path string `json:"path"`
358 }
Jiyong Parkbd159612020-02-28 15:22:21 +0900359 config := struct {
360 Compression struct {
361 Uncompressed_glob []string `json:"uncompressed_glob"`
362 } `json:"compression"`
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900363 Apex_config struct {
364 Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
365 } `json:"apex_config,omitempty"`
Jiyong Parkbd159612020-02-28 15:22:21 +0900366 }{}
367
368 config.Compression.Uncompressed_glob = []string{
369 "apex_payload.img",
370 "apex_manifest.*",
371 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900372
Jiyong Parkb81b9902020-11-24 19:51:18 +0900373 // Collect the manifest names and paths of android apps if their manifest names are
374 // overridden.
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900375 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700376 if fi.class != app && fi.class != appSet {
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900377 continue
378 }
379 packageName := fi.overriddenPackageName
380 if packageName != "" {
381 config.Apex_config.Apex_embedded_apk_config = append(
382 config.Apex_config.Apex_embedded_apk_config,
383 ApkConfig{
384 Package_name: packageName,
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900385 Apk_path: fi.path(),
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900386 })
387 }
388 }
389
Jiyong Parkbd159612020-02-28 15:22:21 +0900390 j, err := json.Marshal(config)
391 if err != nil {
392 panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
393 }
394
Colin Crosscf371cc2020-11-13 11:48:42 -0800395 android.WriteFileRule(ctx, output, string(j))
Jiyong Parkbd159612020-02-28 15:22:21 +0900396
397 return output.OutputPath
398}
399
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000400func markManifestTestOnly(ctx android.ModuleContext, androidManifestFile android.Path) android.Path {
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000401 return java.ManifestFixer(ctx, androidManifestFile, java.ManifestFixerParams{
402 TestOnly: true,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000403 })
404}
405
Jiyong Parkb81b9902020-11-24 19:51:18 +0900406// buildUnflattendApex creates build rules to build an APEX using apexer.
Jiyong Park09d77522019-11-18 11:16:27 +0900407func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900408 apexType := a.properties.ApexType
409 suffix := apexType.suffix()
Colin Cross6340ea52021-11-04 12:01:18 -0700410 apexName := proptools.StringDefault(a.properties.Apex_name, a.BaseModuleName())
Jiyong Park09d77522019-11-18 11:16:27 +0900411
Jiyong Parkb81b9902020-11-24 19:51:18 +0900412 ////////////////////////////////////////////////////////////////////////////////////////////
413 // Step 1: copy built files to appropriate directories under the image directory
414
415 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
416
Colin Cross6340ea52021-11-04 12:01:18 -0700417 installSymbolFiles := !ctx.Config().KatiEnabled() || a.ExportedToMake()
418
419 // b/140136207. When there are overriding APEXes for a VNDK APEX, the symbols file for the overridden
420 // APEX and the overriding APEX will have the same installation paths at /apex/com.android.vndk.v<ver>
421 // as their apexName will be the same. To avoid the path conflicts, skip installing the symbol files
422 // for the overriding VNDK APEXes.
423 if a.vndkApex && len(a.overridableProperties.Overrides) > 0 {
424 installSymbolFiles = false
425 }
426
427 // Avoid creating duplicate build rules for multi-installed APEXes.
428 if proptools.BoolDefault(a.properties.Multi_install_skip_symbol_files, false) {
429 installSymbolFiles = false
Colin Cross4acaea92021-12-10 23:05:02 +0000430
Colin Cross6340ea52021-11-04 12:01:18 -0700431 }
Colin Cross4acaea92021-12-10 23:05:02 +0000432 // set of dependency module:location mappings
433 installMapSet := make(map[string]bool)
Colin Cross6340ea52021-11-04 12:01:18 -0700434
Jiyong Parkb81b9902020-11-24 19:51:18 +0900435 // TODO(jiyong): use the RuleBuilder
Jiyong Park7cd10e32020-01-14 09:22:18 +0900436 var copyCommands []string
Jiyong Parkb81b9902020-11-24 19:51:18 +0900437 var implicitInputs []android.Path
Colin Cross6340ea52021-11-04 12:01:18 -0700438 pathWhenActivated := android.PathForModuleInPartitionInstall(ctx, "apex", apexName)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900439 for _, fi := range a.filesInfo {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900440 destPath := imageDir.Join(ctx, fi.path()).String()
Jiyong Parkb81b9902020-11-24 19:51:18 +0900441 // Prepare the destination path
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700442 destPathDir := filepath.Dir(destPath)
443 if fi.class == appSet {
444 copyCommands = append(copyCommands, "rm -rf "+destPathDir)
445 }
446 copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900447
Colin Cross4acaea92021-12-10 23:05:02 +0000448 installMapPath := fi.builtFile
449
Jiyong Parkb81b9902020-11-24 19:51:18 +0900450 // Copy the built file to the directory. But if the symlink optimization is turned
451 // on, place a symlink to the corresponding file in /system partition instead.
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900452 if a.linkToSystemLib && fi.transitiveDep && fi.availableToPlatform() {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900453 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
Jiyong Parkc0ec6f92020-11-19 23:00:52 +0900454 pathOnDevice := filepath.Join("/system", fi.path())
Jiyong Park7cd10e32020-01-14 09:22:18 +0900455 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
456 } else {
Colin Cross4acaea92021-12-10 23:05:02 +0000457 var installedPath android.InstallPath
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700458 if fi.class == appSet {
459 copyCommands = append(copyCommands,
Colin Crossffbcd1d2021-11-12 12:19:42 -0800460 fmt.Sprintf("unzip -qDD -d %s %s", destPathDir,
461 fi.module.(*java.AndroidAppSet).PackedAdditionalOutputs().String()))
Colin Cross6340ea52021-11-04 12:01:18 -0700462 if installSymbolFiles {
463 installedPath = ctx.InstallFileWithExtraFilesZip(pathWhenActivated.Join(ctx, fi.installDir),
464 fi.stem(), fi.builtFile, fi.module.(*java.AndroidAppSet).PackedAdditionalOutputs())
465 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700466 } else {
467 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
Colin Cross6340ea52021-11-04 12:01:18 -0700468 if installSymbolFiles {
469 installedPath = ctx.InstallFile(pathWhenActivated.Join(ctx, fi.installDir), fi.stem(), fi.builtFile)
470 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700471 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900472 implicitInputs = append(implicitInputs, fi.builtFile)
Colin Cross6340ea52021-11-04 12:01:18 -0700473 if installSymbolFiles {
474 implicitInputs = append(implicitInputs, installedPath)
475 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900476
Colin Cross4acaea92021-12-10 23:05:02 +0000477 // Create additional symlinks pointing the file inside the APEX (if any). Note that
478 // this is independent from the symlink optimization.
479 for _, symlinkPath := range fi.symlinkPaths() {
480 symlinkDest := imageDir.Join(ctx, symlinkPath).String()
481 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
482 if installSymbolFiles {
483 installedSymlink := ctx.InstallSymlink(pathWhenActivated.Join(ctx, filepath.Dir(symlinkPath)), filepath.Base(symlinkPath), installedPath)
484 implicitInputs = append(implicitInputs, installedSymlink)
485 }
Colin Cross6340ea52021-11-04 12:01:18 -0700486 }
Colin Cross4acaea92021-12-10 23:05:02 +0000487
488 installMapPath = installedPath
Jiyong Park7cd10e32020-01-14 09:22:18 +0900489 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900490
491 // Copy the test files (if any)
Liz Kammer1c14a212020-05-12 15:26:55 -0700492 for _, d := range fi.dataPaths {
493 // TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible
Chris Parsons216e10a2020-07-09 17:12:52 -0400494 relPath := d.SrcPath.Rel()
495 dataPath := d.SrcPath.String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700496 if !strings.HasSuffix(dataPath, relPath) {
497 panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
498 }
499
Jiyong Parkb81b9902020-11-24 19:51:18 +0900500 dataDest := imageDir.Join(ctx, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700501
Chris Parsons216e10a2020-07-09 17:12:52 -0400502 copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
503 implicitInputs = append(implicitInputs, d.SrcPath)
Liz Kammer1c14a212020-05-12 15:26:55 -0700504 }
Colin Cross4acaea92021-12-10 23:05:02 +0000505
506 installMapSet[installMapPath.String()+":"+fi.installDir+"/"+fi.builtFile.Base()] = true
Jiyong Park09d77522019-11-18 11:16:27 +0900507 }
Jooyung Han214bf372019-11-12 13:03:50 +0900508 implicitInputs = append(implicitInputs, a.manifestPbOut)
Colin Cross6340ea52021-11-04 12:01:18 -0700509 if installSymbolFiles {
510 installedManifest := ctx.InstallFile(pathWhenActivated, "apex_manifest.pb", a.manifestPbOut)
511 installedKey := ctx.InstallFile(pathWhenActivated, "apex_pubkey", a.publicKeyFile)
512 implicitInputs = append(implicitInputs, installedManifest, installedKey)
513 }
Jiyong Park09d77522019-11-18 11:16:27 +0900514
Colin Cross4acaea92021-12-10 23:05:02 +0000515 if len(installMapSet) > 0 {
516 var installs []string
517 installs = append(installs, android.SortedStringKeys(installMapSet)...)
518 a.SetLicenseInstallMap(installs)
519 }
520
Jiyong Parkb81b9902020-11-24 19:51:18 +0900521 ////////////////////////////////////////////////////////////////////////////////////////////
522 // Step 1.a: Write the list of files in this APEX to a txt file and compare it against
523 // the allowed list given via the allowed_files property. Build fails when the two lists
524 // differ.
525 //
526 // TODO(jiyong): consider removing this. Nobody other than com.android.apex.cts.shim.* seems
527 // to be using this at this moment. Furthermore, this looks very similar to what
528 // buildInstalledFilesFile does. At least, move this to somewhere else so that this doesn't
529 // hurt readability.
530 // TODO(jiyong): use RuleBuilder
Jooyung Han938b5932020-06-20 12:47:47 +0900531 if a.overridableProperties.Allowed_files != nil {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900532 // Build content.txt
533 var emitCommands []string
534 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
535 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
536 minSdkVersion := a.minSdkVersion(ctx)
537 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
538 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
539 }
540 for _, fi := range a.filesInfo {
541 emitCommands = append(emitCommands, "echo './"+fi.path()+"' >> "+imageContentFile.String())
542 }
543 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900544 ctx.Build(pctx, android.BuildParams{
545 Rule: emitApexContentRule,
546 Implicits: implicitInputs,
547 Output: imageContentFile,
548 Description: "emit apex image content",
549 Args: map[string]string{
550 "emit_commands": strings.Join(emitCommands, " && "),
551 },
552 })
553 implicitInputs = append(implicitInputs, imageContentFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900554
Jiyong Parkb81b9902020-11-24 19:51:18 +0900555 // Compare content.txt against allowed_files.
556 allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800557 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900558 ctx.Build(pctx, android.BuildParams{
559 Rule: diffApexContentRule,
560 Implicits: implicitInputs,
561 Output: phonyOutput,
562 Description: "diff apex image content",
563 Args: map[string]string{
Colin Cross440e0d02020-06-11 11:32:11 -0700564 "allowed_files_file": allowedFilesFile.String(),
565 "image_content_file": imageContentFile.String(),
566 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900567 },
568 })
Jiyong Park09d77522019-11-18 11:16:27 +0900569 implicitInputs = append(implicitInputs, phonyOutput)
570 }
571
Jiyong Parkb81b9902020-11-24 19:51:18 +0900572 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Colin Cross790ef352021-10-25 19:15:55 -0700573 outHostBinDir := ctx.Config().HostToolPath(ctx, "").String()
Jiyong Park09d77522019-11-18 11:16:27 +0900574 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
575
Oriol Prieto Gascoa07099d2021-10-14 15:33:41 -0400576 // Figure out if we need to compress the apex.
577 compressionEnabled := ctx.Config().CompressedApex() && proptools.BoolDefault(a.overridableProperties.Compressible, false) && !a.testApex && !ctx.Config().UnbundledBuildApps()
Jiyong Park09d77522019-11-18 11:16:27 +0900578 if apexType == imageApex {
Jiyong Park1b0893e2021-12-13 23:40:17 +0900579
Jiyong Parkb81b9902020-11-24 19:51:18 +0900580 ////////////////////////////////////////////////////////////////////////////////////
581 // Step 2: create canned_fs_config which encodes filemode,uid,gid of each files
582 // in this APEX. The file will be used by apexer in later steps.
Jiyong Park1b0893e2021-12-13 23:40:17 +0900583 cannedFsConfig := a.buildCannedFsConfig(ctx)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900584 implicitInputs = append(implicitInputs, cannedFsConfig)
Jiyong Park09d77522019-11-18 11:16:27 +0900585
Jiyong Parkb81b9902020-11-24 19:51:18 +0900586 ////////////////////////////////////////////////////////////////////////////////////
587 // Step 3: Prepare option flags for apexer and invoke it to create an unsigned APEX.
588 // TODO(jiyong): use the RuleBuilder
Jiyong Park09d77522019-11-18 11:16:27 +0900589 optFlags := []string{}
590
Jiyong Parkb81b9902020-11-24 19:51:18 +0900591 fileContexts := a.buildFileContexts(ctx)
592 implicitInputs = append(implicitInputs, fileContexts)
593
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800594 implicitInputs = append(implicitInputs, a.privateKeyFile, a.publicKeyFile)
595 optFlags = append(optFlags, "--pubkey "+a.publicKeyFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900596
Jooyung Han27151d92019-12-16 17:45:32 +0900597 manifestPackageName := a.getOverrideManifestPackageName(ctx)
598 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900599 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
600 }
601
602 if a.properties.AndroidManifest != nil {
603 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000604
605 if a.testApex {
606 androidManifestFile = markManifestTestOnly(ctx, androidManifestFile)
607 }
608
Jiyong Park09d77522019-11-18 11:16:27 +0900609 implicitInputs = append(implicitInputs, androidManifestFile)
610 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
Gurpreet Singha76f8742022-02-03 21:01:51 +0000611 } else if a.testApex {
612 optFlags = append(optFlags, "--test_only")
Jiyong Park09d77522019-11-18 11:16:27 +0900613 }
614
Jiyong Parkb81b9902020-11-24 19:51:18 +0900615 // Determine target/min sdk version from the context
616 // TODO(jiyong): make this as a function
Dan Albertc8060532020-07-22 22:32:17 -0700617 moduleMinSdkVersion := a.minSdkVersion(ctx)
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100618 minSdkVersion := moduleMinSdkVersion.String()
619
Jiyong Parkb81b9902020-11-24 19:51:18 +0900620 // bundletool doesn't understand what "current" is. We need to transform it to
621 // codename
Jooyung Haned124c32021-01-26 11:43:46 +0900622 if moduleMinSdkVersion.IsCurrent() || moduleMinSdkVersion.IsNone() {
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100623 minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String()
Liz Kammer4854a7d2021-05-27 14:28:27 -0400624
625 if java.UseApiFingerprint(ctx) {
626 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
627 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
628 }
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000629 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900630 // apex module doesn't have a concept of target_sdk_version, hence for the time
631 // being targetSdkVersion == default targetSdkVersion of the branch.
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100632 targetSdkVersion := strconv.Itoa(ctx.Config().DefaultAppTargetSdk(ctx).FinalOrFutureInt())
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000633
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000634 if java.UseApiFingerprint(ctx) {
635 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000636 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
637 }
Jiyong Park09d77522019-11-18 11:16:27 +0900638 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000639 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900640
Baligh Uddin004d7172020-02-19 21:29:28 -0800641 if a.overridableProperties.Logging_parent != "" {
642 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
643 }
644
Jiyong Park19972c72020-01-28 20:05:29 +0900645 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
646 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900647 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900648 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
649 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900650 }
651
Nikita Ioffe9d9960f2021-06-09 19:43:46 +0100652 if (moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) && !a.shouldGenerateHashtree()) && !compressionEnabled {
Jiyong Park09d77522019-11-18 11:16:27 +0900653 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
654 // don't need hashtree for activation. Therefore, by removing hashtree from
655 // apex bundle (filesystem image in it, to be specific), we can save storage.
656 optFlags = append(optFlags, "--no_hashtree")
657 }
658
Dario Frenica913392020-04-27 18:21:11 +0100659 if a.testOnlyShouldSkipPayloadSign() {
660 optFlags = append(optFlags, "--unsigned_payload")
661 }
662
Jiyong Park09d77522019-11-18 11:16:27 +0900663 if a.properties.Apex_name != nil {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900664 // If apex_name is set, apexer can skip checking if key name matches with
665 // apex name. Note that apex_manifest is also mended.
Jiyong Park09d77522019-11-18 11:16:27 +0900666 optFlags = append(optFlags, "--do_not_check_keyname")
667 }
668
Dan Albertc8060532020-07-22 22:32:17 -0700669 if moduleMinSdkVersion == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900670 implicitInputs = append(implicitInputs, a.manifestJsonOut)
671 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
672 }
673
Theotime Combes4ba38c12020-06-12 12:46:59 +0000674 optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
675
Jiyong Park09d77522019-11-18 11:16:27 +0900676 ctx.Build(pctx, android.BuildParams{
677 Rule: apexRule,
678 Implicits: implicitInputs,
679 Output: unsignedOutputFile,
680 Description: "apex (" + apexType.name() + ")",
681 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900682 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900683 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900684 "copy_commands": strings.Join(copyCommands, " && "),
685 "manifest": a.manifestPbOut.String(),
Jiyong Parkb81b9902020-11-24 19:51:18 +0900686 "file_contexts": fileContexts.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900687 "canned_fs_config": cannedFsConfig.String(),
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800688 "key": a.privateKeyFile.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900689 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900690 },
691 })
692
Jiyong Parkb81b9902020-11-24 19:51:18 +0900693 // TODO(jiyong): make the two rules below as separate functions
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800694 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
695 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900696 a.bundleModuleFile = bundleModuleFile
697
698 ctx.Build(pctx, android.BuildParams{
699 Rule: apexProtoConvertRule,
700 Input: unsignedOutputFile,
701 Output: apexProtoFile,
702 Description: "apex proto convert",
703 })
704
sophiezc80a2b32020-11-12 16:39:19 +0000705 implicitInputs = append(implicitInputs, unsignedOutputFile)
706
707 // Run coverage analysis
sophiez6bde0b52021-01-09 01:03:42 +0000708 apisUsedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_using.txt")
sophiezc80a2b32020-11-12 16:39:19 +0000709 ctx.Build(pctx, android.BuildParams{
710 Rule: generateAPIsUsedbyApexRule,
711 Implicits: implicitInputs,
712 Description: "coverage",
713 Output: apisUsedbyOutputFile,
714 Args: map[string]string{
715 "image_dir": imageDir.String(),
716 "readelf": "${config.ClangBin}/llvm-readelf",
717 },
718 })
sophiez02347372021-11-02 17:58:02 -0700719 a.nativeApisUsedByModuleFile = apisUsedbyOutputFile
sophiez6bde0b52021-01-09 01:03:42 +0000720
sophiez02347372021-11-02 17:58:02 -0700721 var nativeLibNames []string
Colin Cross69f0a242021-02-08 16:49:57 -0800722 for _, f := range a.filesInfo {
723 if f.class == nativeSharedLib {
sophiez02347372021-11-02 17:58:02 -0700724 nativeLibNames = append(nativeLibNames, f.stem())
Colin Cross69f0a242021-02-08 16:49:57 -0800725 }
726 }
sophiez6bde0b52021-01-09 01:03:42 +0000727 apisBackedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_backing.txt")
sophiez6bde0b52021-01-09 01:03:42 +0000728 rule := android.NewRuleBuilder(pctx, ctx)
729 rule.Command().
730 Tool(android.PathForSource(ctx, "build/soong/scripts/gen_ndk_backedby_apex.sh")).
sophiez6bde0b52021-01-09 01:03:42 +0000731 Output(apisBackedbyOutputFile).
sophiez02347372021-11-02 17:58:02 -0700732 Flags(nativeLibNames)
sophiez6bde0b52021-01-09 01:03:42 +0000733 rule.Build("ndk_backedby_list", "Generate API libraries backed by Apex")
sophiez02347372021-11-02 17:58:02 -0700734 a.nativeApisBackedByModuleFile = apisBackedbyOutputFile
735
736 var javaLibOrApkPath []android.Path
737 for _, f := range a.filesInfo {
738 if f.class == javaSharedLib || f.class == app {
739 javaLibOrApkPath = append(javaLibOrApkPath, f.builtFile)
740 }
741 }
742 javaApiUsedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_using.xml")
743 javaUsedByRule := android.NewRuleBuilder(pctx, ctx)
744 javaUsedByRule.Command().
745 Tool(android.PathForSource(ctx, "build/soong/scripts/gen_java_usedby_apex.sh")).
746 BuiltTool("dexdeps").
747 Output(javaApiUsedbyOutputFile).
748 Inputs(javaLibOrApkPath)
749 javaUsedByRule.Build("java_usedby_list", "Generate Java APIs used by Apex")
750 a.javaApisUsedByModuleFile = javaApiUsedbyOutputFile
sophiezc80a2b32020-11-12 16:39:19 +0000751
Jiyong Parkbd159612020-02-28 15:22:21 +0900752 bundleConfig := a.buildBundleConfig(ctx)
753
Jiyong Parkb81b9902020-11-24 19:51:18 +0900754 var abis []string
755 for _, target := range ctx.MultiTargets() {
756 if len(target.Arch.Abi) > 0 {
757 abis = append(abis, target.Arch.Abi[0])
758 }
759 }
760
761 abis = android.FirstUniqueStrings(abis)
762
Jiyong Park09d77522019-11-18 11:16:27 +0900763 ctx.Build(pctx, android.BuildParams{
764 Rule: apexBundleRule,
765 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900766 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900767 Output: a.bundleModuleFile,
768 Description: "apex bundle module",
769 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900770 "abi": strings.Join(abis, "."),
771 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900772 },
773 })
Jiyong Parkb81b9902020-11-24 19:51:18 +0900774 } else { // zipApex
Jiyong Park09d77522019-11-18 11:16:27 +0900775 ctx.Build(pctx, android.BuildParams{
776 Rule: zipApexRule,
777 Implicits: implicitInputs,
778 Output: unsignedOutputFile,
779 Description: "apex (" + apexType.name() + ")",
780 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900781 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900782 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900783 "copy_commands": strings.Join(copyCommands, " && "),
784 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900785 },
786 })
787 }
788
Jiyong Parkb81b9902020-11-24 19:51:18 +0900789 ////////////////////////////////////////////////////////////////////////////////////
790 // Step 4: Sign the APEX using signapk
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000791 signedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900792
793 pem, key := a.getCertificateAndPrivateKey(ctx)
Kousik Kumar309b1c02020-05-28 06:13:33 -0700794 rule := java.Signapk
795 args := map[string]string{
Jiyong Parkb81b9902020-11-24 19:51:18 +0900796 "certificates": pem.String() + " " + key.String(),
Jooyung Han5d00f502021-07-11 07:26:22 +0900797 "flags": "-a 4096 --align-file-size", //alignment
Kousik Kumar309b1c02020-05-28 06:13:33 -0700798 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900799 implicits := android.Paths{pem, key}
Ramy Medhat16f23a42020-09-03 01:29:49 -0400800 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700801 rule = java.SignapkRE
802 args["implicits"] = strings.Join(implicits.Strings(), ",")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000803 args["outCommaList"] = signedOutputFile.String()
Kousik Kumar309b1c02020-05-28 06:13:33 -0700804 }
Jiyong Park09d77522019-11-18 11:16:27 +0900805 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700806 Rule: rule,
Jiyong Park09d77522019-11-18 11:16:27 +0900807 Description: "signapk",
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000808 Output: signedOutputFile,
Jiyong Park09d77522019-11-18 11:16:27 +0900809 Input: unsignedOutputFile,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700810 Implicits: implicits,
811 Args: args,
Jiyong Park09d77522019-11-18 11:16:27 +0900812 })
Jooyung Hana6d36672022-02-24 13:58:07 +0900813 if suffix == imageApexSuffix {
814 a.outputApexFile = signedOutputFile
815 }
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000816 a.outputFile = signedOutputFile
817
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000818 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && a.testOnlyShouldForceCompression() {
819 ctx.PropertyErrorf("test_only_force_compression", "not available")
820 return
821 }
Nikita Ioffebc035882021-04-14 21:35:24 +0100822
Mohammad Samiul Islama8008f92020-12-22 10:47:50 +0000823 if apexType == imageApex && (compressionEnabled || a.testOnlyShouldForceCompression()) {
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000824 a.isCompressed = true
Samiul Islam7c02e262021-09-08 17:48:28 +0100825 unsignedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+imageCapexSuffix+".unsigned")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000826
827 compressRule := android.NewRuleBuilder(pctx, ctx)
828 compressRule.Command().
829 Text("rm").
830 FlagWithOutput("-f ", unsignedCompressedOutputFile)
831 compressRule.Command().
832 BuiltTool("apex_compression_tool").
833 Flag("compress").
834 FlagWithArg("--apex_compression_tool ", outHostBinDir+":"+prebuiltSdkToolsBinDir).
835 FlagWithInput("--input ", signedOutputFile).
836 FlagWithOutput("--output ", unsignedCompressedOutputFile)
837 compressRule.Build("compressRule", "Generate unsigned compressed APEX file")
838
Samiul Islam7c02e262021-09-08 17:48:28 +0100839 signedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+imageCapexSuffix)
Mohammad Samiul Islam9ac0e322021-01-19 11:32:29 +0000840 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
841 args["outCommaList"] = signedCompressedOutputFile.String()
842 }
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000843 ctx.Build(pctx, android.BuildParams{
844 Rule: rule,
845 Description: "sign compressedApex",
846 Output: signedCompressedOutputFile,
847 Input: unsignedCompressedOutputFile,
848 Implicits: implicits,
849 Args: args,
850 })
851 a.outputFile = signedCompressedOutputFile
852 }
Jiyong Park09d77522019-11-18 11:16:27 +0900853
Colin Cross6340ea52021-11-04 12:01:18 -0700854 installSuffix := suffix
855 if a.isCompressed {
856 installSuffix = imageCapexSuffix
857 }
858
Colin Crossd9ccb6a2022-03-07 18:38:34 -0800859 if !a.installable() {
860 a.SkipInstall()
861 }
862
Jiyong Park17ff2832021-09-27 12:50:30 +0900863 // Install to $OUT/soong/{target,host}/.../apex.
Colin Cross6340ea52021-11-04 12:01:18 -0700864 a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
865 a.compatSymlinks.Paths()...)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900866
867 // installed-files.txt is dist'ed
868 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900869}
870
Jiyong Parkb81b9902020-11-24 19:51:18 +0900871// buildFlattenedApex creates rules for a flattened APEX. Flattened APEX actually doesn't have a
872// single output file. It is a phony target for all the files under /system/apex/<name> directory.
873// This function creates the installation rules for the files.
Jiyong Park09d77522019-11-18 11:16:27 +0900874func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900875 bundleName := a.Name()
Colin Cross6340ea52021-11-04 12:01:18 -0700876 installedSymlinks := append(android.InstallPaths(nil), a.compatSymlinks...)
Jiyong Park09d77522019-11-18 11:16:27 +0900877 if a.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900878 for _, fi := range a.filesInfo {
879 dir := filepath.Join("apex", bundleName, fi.installDir)
Colin Cross6340ea52021-11-04 12:01:18 -0700880 installDir := android.PathForModuleInstall(ctx, dir)
881 if a.linkToSystemLib && fi.transitiveDep && fi.availableToPlatform() {
882 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
883 pathOnDevice := filepath.Join("/system", fi.path())
884 installedSymlinks = append(installedSymlinks,
885 ctx.InstallAbsoluteSymlink(installDir, fi.stem(), pathOnDevice))
886 } else {
887 target := ctx.InstallFile(installDir, fi.stem(), fi.builtFile)
888 for _, sym := range fi.symlinks {
889 installedSymlinks = append(installedSymlinks,
890 ctx.InstallSymlink(installDir, sym, target))
891 }
Jiyong Park09d77522019-11-18 11:16:27 +0900892 }
893 }
Colin Cross6340ea52021-11-04 12:01:18 -0700894
895 // Create install rules for the files added in GenerateAndroidBuildActions after
896 // buildFlattenedApex is called. Add the links to system libs (if any) as dependencies
897 // of the apex_manifest.pb file since it is always present.
898 dir := filepath.Join("apex", bundleName)
899 installDir := android.PathForModuleInstall(ctx, dir)
900 ctx.InstallFile(installDir, "apex_manifest.pb", a.manifestPbOut, installedSymlinks.Paths()...)
901 ctx.InstallFile(installDir, "apex_pubkey", a.publicKeyFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900902 }
Jiyong Parkb81b9902020-11-24 19:51:18 +0900903
904 a.fileContexts = a.buildFileContexts(ctx)
905
Colin Cross6340ea52021-11-04 12:01:18 -0700906 a.outputFile = android.PathForModuleInstall(ctx, "apex", bundleName)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900907}
908
909// getCertificateAndPrivateKey retrieves the cert and the private key that will be used to sign
910// the zip container of this APEX. See the description of the 'certificate' property for how
911// the cert and the private key are found.
912func (a *apexBundle) getCertificateAndPrivateKey(ctx android.PathContext) (pem, key android.Path) {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800913 if a.containerCertificateFile != nil {
914 return a.containerCertificateFile, a.containerPrivateKeyFile
Jiyong Parkb81b9902020-11-24 19:51:18 +0900915 }
916
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -0700917 cert := String(a.overridableProperties.Certificate)
Jiyong Parkb81b9902020-11-24 19:51:18 +0900918 if cert == "" {
919 return ctx.Config().DefaultAppCertificate(ctx)
920 }
921
922 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
923 pem = defaultDir.Join(ctx, cert+".x509.pem")
924 key = defaultDir.Join(ctx, cert+".pk8")
925 return pem, key
Jiyong Park09d77522019-11-18 11:16:27 +0900926}
Jooyung Han27151d92019-12-16 17:45:32 +0900927
928func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
929 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
930 // to see if it should be overridden because their <apex name> is dynamically generated
931 // according to its VNDK version.
932 if a.vndkApex {
933 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
934 if overridden {
935 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
936 }
937 return ""
938 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700939 if a.overridableProperties.Package_name != "" {
940 return a.overridableProperties.Package_name
941 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900942 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900943 if overridden {
944 return manifestPackageName
945 }
946 return ""
947}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900948
949func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
950 if !a.primaryApexType {
951 return
952 }
953
954 if a.properties.IsCoverageVariant {
955 // Otherwise, we will have duplicated rules for coverage and
956 // non-coverage variants of the same APEX
957 return
958 }
959
960 if ctx.Host() {
961 // No need to generate dependency info for host variant
962 return
963 }
964
Artur Satayev872a1442020-04-27 17:08:37 +0100965 depInfos := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900966 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev872a1442020-04-27 17:08:37 +0100967 if from.Name() == to.Name() {
968 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
969 // As soon as the dependency graph crosses the APEX boundary, don't go further.
970 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900971 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900972
Artur Satayev533b98c2021-03-11 18:03:42 +0000973 // Skip dependencies that are only available to APEXes; they are developed with updatability
974 // in mind and don't need manual approval.
975 if to.(android.ApexModule).NotAvailableForPlatform() {
976 return !externalDep
977 }
978
Cindy Zhou18417cb2020-12-10 07:12:38 -0800979 depTag := ctx.OtherModuleDependencyTag(to)
Artur Satayev533b98c2021-03-11 18:03:42 +0000980 // Check to see if dependency been marked to skip the dependency check
Cindy Zhou18417cb2020-12-10 07:12:38 -0800981 if skipDepCheck, ok := depTag.(android.SkipApexAllowedDependenciesCheck); ok && skipDepCheck.SkipApexAllowedDependenciesCheck() {
Cindy Zhou18417cb2020-12-10 07:12:38 -0800982 return !externalDep
983 }
984
Artur Satayev872a1442020-04-27 17:08:37 +0100985 if info, exists := depInfos[to.Name()]; exists {
986 if !android.InList(from.Name(), info.From) {
987 info.From = append(info.From, from.Name())
988 }
989 info.IsExternal = info.IsExternal && externalDep
990 depInfos[to.Name()] = info
991 } else {
Artur Satayev480e25b2020-04-27 18:53:18 +0100992 toMinSdkVersion := "(no version)"
Jiyong Park92315372021-04-02 08:45:46 +0900993 if m, ok := to.(interface {
994 MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec
995 }); ok {
996 if v := m.MinSdkVersion(ctx); !v.ApiLevel.IsNone() {
997 toMinSdkVersion = v.ApiLevel.String()
998 }
999 } else if m, ok := to.(interface{ MinSdkVersion() string }); ok {
1000 // TODO(b/175678607) eliminate the use of MinSdkVersion returning
1001 // string
Artur Satayev480e25b2020-04-27 18:53:18 +01001002 if v := m.MinSdkVersion(); v != "" {
1003 toMinSdkVersion = v
1004 }
1005 }
Artur Satayev872a1442020-04-27 17:08:37 +01001006 depInfos[to.Name()] = android.ApexModuleDepInfo{
Artur Satayev480e25b2020-04-27 18:53:18 +01001007 To: to.Name(),
1008 From: []string{from.Name()},
1009 IsExternal: externalDep,
1010 MinSdkVersion: toMinSdkVersion,
Artur Satayev872a1442020-04-27 17:08:37 +01001011 }
1012 }
1013
1014 // As soon as the dependency graph crosses the APEX boundary, don't go further.
1015 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +09001016 })
1017
Artur Satayev480e25b2020-04-27 18:53:18 +01001018 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos)
Artur Satayev872a1442020-04-27 17:08:37 +01001019
Jiyong Park83dc74b2020-01-14 18:38:44 +09001020 ctx.Build(pctx, android.BuildParams{
1021 Rule: android.Phony,
1022 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +01001023 Inputs: []android.Path{
1024 a.ApexBundleDepsInfo.FullListPath(),
1025 a.ApexBundleDepsInfo.FlatListPath(),
1026 },
Jiyong Park83dc74b2020-01-14 18:38:44 +09001027 })
1028}
Colin Cross08dca382020-07-21 20:31:17 -07001029
1030func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
1031 depSetsBuilder := java.NewLintDepSetBuilder()
1032 for _, fi := range a.filesInfo {
1033 depSetsBuilder.Transitive(fi.lintDepSets)
1034 }
1035
1036 a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
1037}
Jiyong Park1b0893e2021-12-13 23:40:17 +09001038
1039func (a *apexBundle) buildCannedFsConfig(ctx android.ModuleContext) android.OutputPath {
1040 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
1041 var executablePaths []string // this also includes dirs
1042 var appSetDirs []string
1043 appSetFiles := make(map[string]android.Path)
1044 for _, f := range a.filesInfo {
1045 pathInApex := f.path()
1046 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
1047 executablePaths = append(executablePaths, pathInApex)
1048 for _, d := range f.dataPaths {
1049 readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
1050 }
1051 for _, s := range f.symlinks {
1052 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
1053 }
1054 } else if f.class == appSet {
1055 appSetDirs = append(appSetDirs, f.installDir)
1056 appSetFiles[f.installDir] = f.builtFile
1057 } else {
1058 readOnlyPaths = append(readOnlyPaths, pathInApex)
1059 }
1060 dir := f.installDir
1061 for !android.InList(dir, executablePaths) && dir != "" {
1062 executablePaths = append(executablePaths, dir)
1063 dir, _ = filepath.Split(dir) // move up to the parent
1064 if len(dir) > 0 {
1065 // remove trailing slash
1066 dir = dir[:len(dir)-1]
1067 }
1068 }
1069 }
1070 sort.Strings(readOnlyPaths)
1071 sort.Strings(executablePaths)
1072 sort.Strings(appSetDirs)
1073
1074 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
1075 builder := android.NewRuleBuilder(pctx, ctx)
1076 cmd := builder.Command()
1077 cmd.Text("(")
1078 cmd.Text("echo '/ 1000 1000 0755';")
1079 for _, p := range readOnlyPaths {
1080 cmd.Textf("echo '/%s 1000 1000 0644';", p)
1081 }
1082 for _, p := range executablePaths {
1083 cmd.Textf("echo '/%s 0 2000 0755';", p)
1084 }
1085 for _, dir := range appSetDirs {
1086 cmd.Textf("echo '/%s 0 2000 0755';", dir)
1087 file := appSetFiles[dir]
1088 cmd.Text("zipinfo -1").Input(file).Textf(`| sed "s:\(.*\):/%s/\1 1000 1000 0644:";`, dir)
1089 }
Jiyong Park038e8522021-12-13 23:56:35 +09001090 // Custom fs_config is "appended" to the last so that entries from the file are preferred
1091 // over default ones set above.
1092 if a.properties.Canned_fs_config != nil {
1093 cmd.Text("cat").Input(android.PathForModuleSrc(ctx, *a.properties.Canned_fs_config))
1094 }
Jiyong Park1b0893e2021-12-13 23:40:17 +09001095 cmd.Text(")").FlagWithOutput("> ", cannedFsConfig)
1096 builder.Build("generateFsConfig", fmt.Sprintf("Generating canned fs config for %s", a.BaseModuleName()))
1097
1098 return cannedFsConfig.OutputPath
1099}