blob: 7c125ef4f3526b9874d350829423a1840335c34c [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"
Jooyung Han580eb4f2020-06-24 19:33:06 +090020 "path"
Jiyong Park09d77522019-11-18 11:16:27 +090021 "path/filepath"
22 "runtime"
23 "sort"
24 "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")
39 pctx.Import("android/soong/java")
40 pctx.HostBinToolVariable("apexer", "apexer")
41 // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
42 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
43 hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
44 pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
45 if !ctx.Config().FrameworksBaseDirExists(ctx) {
46 return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
47 } else {
Martin Stjernholm7260d062019-12-09 21:47:14 +000048 return ctx.Config().HostToolPath(ctx, tool).String()
Jiyong Park09d77522019-11-18 11:16:27 +090049 }
50 })
51 }
52 hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
53 pctx.HostBinToolVariable("avbtool", "avbtool")
54 pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
55 pctx.HostBinToolVariable("merge_zips", "merge_zips")
56 pctx.HostBinToolVariable("mke2fs", "mke2fs")
57 pctx.HostBinToolVariable("resize2fs", "resize2fs")
58 pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
59 pctx.HostBinToolVariable("soong_zip", "soong_zip")
60 pctx.HostBinToolVariable("zip2zip", "zip2zip")
61 pctx.HostBinToolVariable("zipalign", "zipalign")
62 pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
63 pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
Jaewoong Jungfa00c062020-05-14 14:15:24 -070064 pctx.HostBinToolVariable("extract_apks", "extract_apks")
Theotime Combes4ba38c12020-06-12 12:46:59 +000065 pctx.HostBinToolVariable("make_f2fs", "make_f2fs")
66 pctx.HostBinToolVariable("sload_f2fs", "sload_f2fs")
Jiyong Park09d77522019-11-18 11:16:27 +090067}
68
69var (
70 // Create a canned fs config file where all files and directories are
71 // by default set to (uid/gid/mode) = (1000/1000/0644)
72 // TODO(b/113082813) make this configurable using config.fs syntax
73 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Sasha Smundak18d98bc2020-05-27 16:36:07 -070074 Command: `( echo '/ 1000 1000 0755' ` +
75 `&& for i in ${ro_paths}; do echo "/$$i 1000 1000 0644"; done ` +
76 `&& for i in ${exec_paths}; do echo "/$$i 0 2000 0755"; done ` +
77 `&& ( tr ' ' '\n' <${out}.apklist | for i in ${apk_paths}; do read apk; echo "/$$i 0 2000 0755"; zipinfo -1 $$apk | sed "s:\(.*\):/$$i/\1 1000 1000 0644:"; done ) ) > ${out}`,
78 Description: "fs_config ${out}",
79 Rspfile: "$out.apklist",
80 RspfileContent: "$in",
81 }, "ro_paths", "exec_paths", "apk_paths")
Jiyong Park09d77522019-11-18 11:16:27 +090082
83 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
84 Command: `rm -f $out && ${jsonmodify} $in ` +
85 `-a provideNativeLibs ${provideNativeLibs} ` +
86 `-a requireNativeLibs ${requireNativeLibs} ` +
87 `${opt} ` +
88 `-o $out`,
89 CommandDeps: []string{"${jsonmodify}"},
90 Description: "prepare ${out}",
91 }, "provideNativeLibs", "requireNativeLibs", "opt")
92
93 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
94 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
95 CommandDeps: []string{"${conv_apex_manifest}"},
96 Description: "strip ${in}=>${out}",
97 })
98
99 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
100 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
101 CommandDeps: []string{"${conv_apex_manifest}"},
102 Description: "convert ${in}=>${out}",
103 })
104
105 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
106 // against the binary policy using sefcontext_compiler -p <policy>.
107
108 // TODO(b/114327326): automate the generation of file_contexts
109 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
110 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
111 `(. ${out}.copy_commands) && ` +
112 `APEXER_TOOL_PATH=${tool_path} ` +
113 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900114 `--file_contexts ${file_contexts} ` +
115 `--canned_fs_config ${canned_fs_config} ` +
Dario Freni0f4ae072020-01-02 15:24:12 +0000116 `--include_build_info ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900117 `--payload_type image ` +
118 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
119 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
Theotime Combes4ba38c12020-06-12 12:46:59 +0000120 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}", "${sload_f2fs}",
Jiyong Park09d77522019-11-18 11:16:27 +0900121 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
122 Rspfile: "${out}.copy_commands",
123 RspfileContent: "${copy_commands}",
124 Description: "APEX ${image_dir} => ${out}",
Theotime Combes4ba38c12020-06-12 12:46:59 +0000125 }, "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 +0900126
127 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
128 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
129 `(. ${out}.copy_commands) && ` +
130 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han214bf372019-11-12 13:03:50 +0900131 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900132 `--payload_type zip ` +
133 `${image_dir} ${out} `,
134 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
135 Rspfile: "${out}.copy_commands",
136 RspfileContent: "${copy_commands}",
137 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900138 }, "tool_path", "image_dir", "copy_commands", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900139
140 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
141 blueprint.RuleParams{
142 Command: `${aapt2} convert --output-format proto $in -o $out`,
143 CommandDeps: []string{"${aapt2}"},
144 })
145
146 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Parkbd159612020-02-28 15:22:21 +0900147 Command: `${zip2zip} -i $in -o $out.base ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900148 `apex_payload.img:apex/${abi}.img ` +
Dario Frenida1aefe2020-03-02 21:47:09 +0000149 `apex_build_info.pb:apex/${abi}.build_info.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900150 `apex_manifest.json:root/apex_manifest.json ` +
Jiyong Park53ae3342019-12-08 02:06:24 +0900151 `apex_manifest.pb:root/apex_manifest.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900152 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
Jiyong Parkbd159612020-02-28 15:22:21 +0900153 `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` +
154 `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` +
155 `${merge_zips} $out $out.base $out.config`,
156 CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"},
Jiyong Park09d77522019-11-18 11:16:27 +0900157 Description: "app bundle",
Jiyong Parkbd159612020-02-28 15:22:21 +0900158 }, "abi", "config")
Jiyong Park09d77522019-11-18 11:16:27 +0900159
160 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
161 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
162 Rspfile: "${out}.emit_commands",
163 RspfileContent: "${emit_commands}",
164 Description: "Emit APEX image content",
165 }, "emit_commands")
166
167 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
168 Command: `diff --unchanged-group-format='' \` +
169 `--changed-group-format='%<' \` +
Colin Cross440e0d02020-06-11 11:32:11 -0700170 `${image_content_file} ${allowed_files_file} || (` +
Jiyong Park09d77522019-11-18 11:16:27 +0900171 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
172 ` "To fix the build run following command:" && ` +
Colin Cross440e0d02020-06-11 11:32:11 -0700173 `echo "system/apex/tools/update_allowed_list.sh ${allowed_files_file} ${image_content_file}" && ` +
Dan Willemsen81e43c52020-01-28 15:40:19 -0800174 `exit 1); touch ${out}`,
Colin Cross440e0d02020-06-11 11:32:11 -0700175 Description: "Diff ${image_content_file} and ${allowed_files_file}",
176 }, "image_content_file", "allowed_files_file", "apex_module_name")
Jiyong Park09d77522019-11-18 11:16:27 +0900177)
178
179func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
180 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
181
Jooyung Han214bf372019-11-12 13:03:50 +0900182 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900183
184 // put dependency({provide|require}NativeLibs) in apex_manifest.json
185 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
186 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
187
188 // apex name can be overridden
189 optCommands := []string{}
190 if a.properties.Apex_name != nil {
191 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
192 }
193
Jooyung Han643adc42020-02-27 13:50:06 +0900194 // collect jniLibs. Notice that a.filesInfo is already sorted
195 var jniLibs []string
196 for _, fi := range a.filesInfo {
Jooyung Hanb9007602020-08-28 16:29:27 +0900197 if fi.isJniLib && !android.InList(fi.Stem(), jniLibs) {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900198 jniLibs = append(jniLibs, fi.Stem())
Jooyung Han643adc42020-02-27 13:50:06 +0900199 }
200 }
201 if len(jniLibs) > 0 {
202 optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
203 }
204
Jiyong Park09d77522019-11-18 11:16:27 +0900205 ctx.Build(pctx, android.BuildParams{
206 Rule: apexManifestRule,
207 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900208 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900209 Args: map[string]string{
210 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
211 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
212 "opt": strings.Join(optCommands, " "),
213 },
214 })
215
Dan Albertc8060532020-07-22 22:32:17 -0700216 minSdkVersion := a.minSdkVersion(ctx)
217 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900218 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
219 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
220 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
221 ctx.Build(pctx, android.BuildParams{
222 Rule: stripApexManifestRule,
223 Input: manifestJsonFullOut,
224 Output: a.manifestJsonOut,
225 })
226 }
Jiyong Park09d77522019-11-18 11:16:27 +0900227
228 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
229 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
230 ctx.Build(pctx, android.BuildParams{
231 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900232 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900233 Output: a.manifestPbOut,
234 })
235}
236
Jooyung Han580eb4f2020-06-24 19:33:06 +0900237func (a *apexBundle) buildFileContexts(ctx android.ModuleContext) {
238 if a.properties.ApexType == zipApex {
239 return
240 }
241 var fileContexts android.Path
242 if a.properties.File_contexts == nil {
243 fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
244 } else {
245 fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
246 }
247 if a.Platform() {
248 if matched, err := path.Match("system/sepolicy/**/*", fileContexts.String()); err != nil || !matched {
249 ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but %q", fileContexts)
250 return
251 }
252 }
253 if !android.ExistentPathForSource(ctx, fileContexts.String()).Valid() {
254 ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", a.fileContexts)
255 return
256 }
257
258 output := android.PathForModuleOut(ctx, "file_contexts")
259 rule := android.NewRuleBuilder()
Jooyung Han7f146c02020-09-23 19:15:55 +0900260
261 if a.properties.ApexType == imageApex {
262 // remove old file
263 rule.Command().Text("rm").FlagWithOutput("-f ", output)
264 // copy file_contexts
265 rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output)
266 // new line
267 rule.Command().Text("echo").Text(">>").Output(output)
268 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
269 rule.Command().Text("echo").Flag("/apex_manifest\\\\.pb u:object_r:system_file:s0").Text(">>").Output(output)
270 rule.Command().Text("echo").Flag("/ u:object_r:system_file:s0").Text(">>").Output(output)
271 } else {
272 // For flattened apexes, install path should be prepended.
273 // File_contexts file should be emiited to make via LOCAL_FILE_CONTEXTS
274 // so that it can be merged into file_contexts.bin
275 apexPath := android.InstallPathToOnDevicePath(ctx, a.installDir.Join(ctx, a.Name()))
276 apexPath = strings.ReplaceAll(apexPath, ".", `\\.`)
277 // remove old file
278 rule.Command().Text("rm").FlagWithOutput("-f ", output)
279 // copy file_contexts
280 rule.Command().Text("awk").Text(`'/object_r/{printf("` + apexPath + `%s\n", $0)}'`).Input(fileContexts).Text(">").Output(output)
281 // new line
282 rule.Command().Text("echo").Text(">>").Output(output)
283 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
284 rule.Command().Text("echo").Flag(apexPath + `/apex_manifest\\.pb u:object_r:system_file:s0`).Text(">>").Output(output)
285 rule.Command().Text("echo").Flag(apexPath + "/ u:object_r:system_file:s0").Text(">>").Output(output)
286 }
287
Jooyung Han580eb4f2020-06-24 19:33:06 +0900288 rule.Build(pctx, ctx, "file_contexts."+a.Name(), "Generate file_contexts")
289
290 a.fileContexts = output.OutputPath
291}
292
Jiyong Park19972c72020-01-28 20:05:29 +0900293func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900294 var noticeFiles android.Paths
295
Jooyung Han749dc692020-04-15 11:03:39 +0900296 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900297 if externalDep {
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100298 // As soon as the dependency graph crosses the APEX boundary, don't go further.
299 return false
Jiyong Park09d77522019-11-18 11:16:27 +0900300 }
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100301
Jiyong Park9918e1a2020-03-17 19:16:40 +0900302 notices := to.NoticeFiles()
303 noticeFiles = append(noticeFiles, notices...)
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100304
305 return true
Jiyong Park9918e1a2020-03-17 19:16:40 +0900306 })
Jiyong Park09d77522019-11-18 11:16:27 +0900307
Jiyong Park41f637d2020-09-09 13:18:02 +0900308 for _, fi := range a.filesInfo {
309 noticeFiles = append(noticeFiles, fi.noticeFiles...)
310 }
311
Jiyong Park09d77522019-11-18 11:16:27 +0900312 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900313 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900314 }
315
Jiyong Park33c77362020-05-29 22:00:16 +0900316 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.SortedUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900317}
318
Jiyong Park3a1602e2020-01-14 14:39:19 +0900319func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
320 output := android.PathForModuleOut(ctx, "installed-files.txt")
321 rule := android.NewRuleBuilder()
322 rule.Command().
323 Implicit(builtApex).
324 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900325 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900326 Text(" | sort -nr > ").
327 Output(output)
328 rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files")
329 return output.OutputPath
330}
331
Jiyong Parkbd159612020-02-28 15:22:21 +0900332func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
333 output := android.PathForModuleOut(ctx, "bundle_config.json")
334
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900335 type ApkConfig struct {
336 Package_name string `json:"package_name"`
337 Apk_path string `json:"path"`
338 }
Jiyong Parkbd159612020-02-28 15:22:21 +0900339 config := struct {
340 Compression struct {
341 Uncompressed_glob []string `json:"uncompressed_glob"`
342 } `json:"compression"`
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900343 Apex_config struct {
344 Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
345 } `json:"apex_config,omitempty"`
Jiyong Parkbd159612020-02-28 15:22:21 +0900346 }{}
347
348 config.Compression.Uncompressed_glob = []string{
349 "apex_payload.img",
350 "apex_manifest.*",
351 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900352
353 // collect the manifest names and paths of android apps
354 // if their manifest names are overridden
355 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700356 if fi.class != app && fi.class != appSet {
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900357 continue
358 }
359 packageName := fi.overriddenPackageName
360 if packageName != "" {
361 config.Apex_config.Apex_embedded_apk_config = append(
362 config.Apex_config.Apex_embedded_apk_config,
363 ApkConfig{
364 Package_name: packageName,
365 Apk_path: fi.Path(),
366 })
367 }
368 }
369
Jiyong Parkbd159612020-02-28 15:22:21 +0900370 j, err := json.Marshal(config)
371 if err != nil {
372 panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
373 }
374
375 ctx.Build(pctx, android.BuildParams{
376 Rule: android.WriteFile,
377 Output: output,
378 Description: "Bundle Config " + output.String(),
379 Args: map[string]string{
380 "content": string(j),
381 },
382 })
383
384 return output.OutputPath
385}
386
Jiyong Park09d77522019-11-18 11:16:27 +0900387func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
388 var abis []string
389 for _, target := range ctx.MultiTargets() {
390 if len(target.Arch.Abi) > 0 {
391 abis = append(abis, target.Arch.Abi[0])
392 }
393 }
394
395 abis = android.FirstUniqueStrings(abis)
396
397 apexType := a.properties.ApexType
398 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900399 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800400 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900401
Jiyong Park7cd10e32020-01-14 09:22:18 +0900402 // TODO(jiyong): construct the copy rules using RuleBuilder
403 var copyCommands []string
404 for _, fi := range a.filesInfo {
405 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700406 destPathDir := filepath.Dir(destPath)
407 if fi.class == appSet {
408 copyCommands = append(copyCommands, "rm -rf "+destPathDir)
409 }
410 copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900411 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
412 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
413 pathOnDevice := filepath.Join("/system", fi.Path())
414 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
415 } else {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700416 if fi.class == appSet {
417 copyCommands = append(copyCommands,
Colin Crossd783bbb2020-07-11 22:30:45 -0700418 fmt.Sprintf("unzip -qDD -d %s %s", destPathDir, fi.builtFile.String()))
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700419 } else {
420 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
421 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900422 implicitInputs = append(implicitInputs, fi.builtFile)
423 }
424 // create additional symlinks pointing the file inside the APEX
425 for _, symlinkPath := range fi.SymlinkPaths() {
426 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000427 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900428 }
Liz Kammer1c14a212020-05-12 15:26:55 -0700429 for _, d := range fi.dataPaths {
430 // 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 -0400431 relPath := d.SrcPath.Rel()
432 dataPath := d.SrcPath.String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700433 if !strings.HasSuffix(dataPath, relPath) {
434 panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
435 }
436
Liz Kammer0a51aa22020-07-21 11:13:17 -0700437 dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700438
Chris Parsons216e10a2020-07-09 17:12:52 -0400439 copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
440 implicitInputs = append(implicitInputs, d.SrcPath)
Liz Kammer1c14a212020-05-12 15:26:55 -0700441 }
Jiyong Park09d77522019-11-18 11:16:27 +0900442 }
443
Jiyong Park7cd10e32020-01-14 09:22:18 +0900444 // TODO(jiyong): use RuleBuilder
445 var emitCommands []string
446 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900447 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
Dan Albertc8060532020-07-22 22:32:17 -0700448 minSdkVersion := a.minSdkVersion(ctx)
449 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900450 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
451 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900452 for _, fi := range a.filesInfo {
453 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900454 }
455 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900456 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900457
Jooyung Han938b5932020-06-20 12:47:47 +0900458 if a.overridableProperties.Allowed_files != nil {
Jiyong Park09d77522019-11-18 11:16:27 +0900459 ctx.Build(pctx, android.BuildParams{
460 Rule: emitApexContentRule,
461 Implicits: implicitInputs,
462 Output: imageContentFile,
463 Description: "emit apex image content",
464 Args: map[string]string{
465 "emit_commands": strings.Join(emitCommands, " && "),
466 },
467 })
468 implicitInputs = append(implicitInputs, imageContentFile)
Jooyung Han938b5932020-06-20 12:47:47 +0900469 allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
Jiyong Park09d77522019-11-18 11:16:27 +0900470
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800471 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900472 ctx.Build(pctx, android.BuildParams{
473 Rule: diffApexContentRule,
474 Implicits: implicitInputs,
475 Output: phonyOutput,
476 Description: "diff apex image content",
477 Args: map[string]string{
Colin Cross440e0d02020-06-11 11:32:11 -0700478 "allowed_files_file": allowedFilesFile.String(),
479 "image_content_file": imageContentFile.String(),
480 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900481 },
482 })
483
484 implicitInputs = append(implicitInputs, phonyOutput)
485 }
486
487 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
488 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
489
Jiyong Park3a1602e2020-01-14 14:39:19 +0900490 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900491 if apexType == imageApex {
492 // files and dirs that will be created in APEX
493 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
494 var executablePaths []string // this also includes dirs
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700495 var extractedAppSetPaths android.Paths
496 var extractedAppSetDirs []string
Jiyong Park09d77522019-11-18 11:16:27 +0900497 for _, f := range a.filesInfo {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900498 pathInApex := f.Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900499 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
500 executablePaths = append(executablePaths, pathInApex)
Liz Kammer1c14a212020-05-12 15:26:55 -0700501 for _, d := range f.dataPaths {
Liz Kammer0a51aa22020-07-21 11:13:17 -0700502 readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
Liz Kammer1c14a212020-05-12 15:26:55 -0700503 }
Jiyong Park09d77522019-11-18 11:16:27 +0900504 for _, s := range f.symlinks {
505 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
506 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700507 } else if f.class == appSet {
508 extractedAppSetPaths = append(extractedAppSetPaths, f.builtFile)
509 extractedAppSetDirs = append(extractedAppSetDirs, f.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900510 } else {
511 readOnlyPaths = append(readOnlyPaths, pathInApex)
512 }
513 dir := f.installDir
514 for !android.InList(dir, executablePaths) && dir != "" {
515 executablePaths = append(executablePaths, dir)
516 dir, _ = filepath.Split(dir) // move up to the parent
517 if len(dir) > 0 {
518 // remove trailing slash
519 dir = dir[:len(dir)-1]
520 }
521 }
522 }
523 sort.Strings(readOnlyPaths)
524 sort.Strings(executablePaths)
525 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
526 ctx.Build(pctx, android.BuildParams{
527 Rule: generateFsConfig,
528 Output: cannedFsConfig,
529 Description: "generate fs config",
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700530 Inputs: extractedAppSetPaths,
Jiyong Park09d77522019-11-18 11:16:27 +0900531 Args: map[string]string{
532 "ro_paths": strings.Join(readOnlyPaths, " "),
533 "exec_paths": strings.Join(executablePaths, " "),
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700534 "apk_paths": strings.Join(extractedAppSetDirs, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900535 },
536 })
537
Jiyong Park09d77522019-11-18 11:16:27 +0900538 optFlags := []string{}
539
540 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900541 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900542 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
543
Jooyung Han27151d92019-12-16 17:45:32 +0900544 manifestPackageName := a.getOverrideManifestPackageName(ctx)
545 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900546 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
547 }
548
549 if a.properties.AndroidManifest != nil {
550 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
551 implicitInputs = append(implicitInputs, androidManifestFile)
552 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
553 }
554
Dan Albert4f378d72020-07-23 17:32:15 -0700555 targetSdkVersion := ctx.Config().DefaultAppTargetSdk(ctx).String()
Nikita Ioffe644009a2020-05-20 00:16:27 +0100556 // TODO(b/157078772): propagate min_sdk_version to apexer.
Dan Albert4f378d72020-07-23 17:32:15 -0700557 minSdkVersion := ctx.Config().DefaultAppTargetSdk(ctx).String()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000558
Dan Albertc8060532020-07-22 22:32:17 -0700559 moduleMinSdkVersion := a.minSdkVersion(ctx)
560 if moduleMinSdkVersion.EqualTo(android.SdkVersion_Android10) {
561 minSdkVersion = moduleMinSdkVersion.String()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000562 }
563
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000564 if java.UseApiFingerprint(ctx) {
565 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000566 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
567 }
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000568 if java.UseApiFingerprint(ctx) {
569 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000570 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
Jiyong Park09d77522019-11-18 11:16:27 +0900571 }
572 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000573 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900574
Baligh Uddin004d7172020-02-19 21:29:28 -0800575 if a.overridableProperties.Logging_parent != "" {
576 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
577 }
578
Jiyong Park19972c72020-01-28 20:05:29 +0900579 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
580 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900581 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900582 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
583 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900584 }
585
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000586 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000587 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
588 return
589 }
Dan Albertc8060532020-07-22 22:32:17 -0700590 if moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900591 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
592 // don't need hashtree for activation. Therefore, by removing hashtree from
593 // apex bundle (filesystem image in it, to be specific), we can save storage.
594 optFlags = append(optFlags, "--no_hashtree")
595 }
596
Dario Frenica913392020-04-27 18:21:11 +0100597 if a.testOnlyShouldSkipPayloadSign() {
598 optFlags = append(optFlags, "--unsigned_payload")
599 }
600
Jiyong Park09d77522019-11-18 11:16:27 +0900601 if a.properties.Apex_name != nil {
602 // If apex_name is set, apexer can skip checking if key name matches with apex name.
603 // Note that apex_manifest is also mended.
604 optFlags = append(optFlags, "--do_not_check_keyname")
605 }
606
Dan Albertc8060532020-07-22 22:32:17 -0700607 if moduleMinSdkVersion == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900608 implicitInputs = append(implicitInputs, a.manifestJsonOut)
609 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
610 }
611
Theotime Combes4ba38c12020-06-12 12:46:59 +0000612 optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
613
Jiyong Park09d77522019-11-18 11:16:27 +0900614 ctx.Build(pctx, android.BuildParams{
615 Rule: apexRule,
616 Implicits: implicitInputs,
617 Output: unsignedOutputFile,
618 Description: "apex (" + apexType.name() + ")",
619 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900620 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900621 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900622 "copy_commands": strings.Join(copyCommands, " && "),
623 "manifest": a.manifestPbOut.String(),
624 "file_contexts": a.fileContexts.String(),
625 "canned_fs_config": cannedFsConfig.String(),
626 "key": a.private_key_file.String(),
627 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900628 },
629 })
630
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800631 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
632 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900633 a.bundleModuleFile = bundleModuleFile
634
635 ctx.Build(pctx, android.BuildParams{
636 Rule: apexProtoConvertRule,
637 Input: unsignedOutputFile,
638 Output: apexProtoFile,
639 Description: "apex proto convert",
640 })
641
Jiyong Parkbd159612020-02-28 15:22:21 +0900642 bundleConfig := a.buildBundleConfig(ctx)
643
Jiyong Park09d77522019-11-18 11:16:27 +0900644 ctx.Build(pctx, android.BuildParams{
645 Rule: apexBundleRule,
646 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900647 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900648 Output: a.bundleModuleFile,
649 Description: "apex bundle module",
650 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900651 "abi": strings.Join(abis, "."),
652 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900653 },
654 })
655 } else {
656 ctx.Build(pctx, android.BuildParams{
657 Rule: zipApexRule,
658 Implicits: implicitInputs,
659 Output: unsignedOutputFile,
660 Description: "apex (" + apexType.name() + ")",
661 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900662 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900663 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900664 "copy_commands": strings.Join(copyCommands, " && "),
665 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900666 },
667 })
668 }
669
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800670 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Kousik Kumar309b1c02020-05-28 06:13:33 -0700671 rule := java.Signapk
672 args := map[string]string{
673 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
674 "flags": "-a 4096", //alignment
675 }
676 implicits := android.Paths{
677 a.container_certificate_file,
678 a.container_private_key_file,
679 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400680 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700681 rule = java.SignapkRE
682 args["implicits"] = strings.Join(implicits.Strings(), ",")
683 args["outCommaList"] = a.outputFile.String()
684 }
Jiyong Park09d77522019-11-18 11:16:27 +0900685 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700686 Rule: rule,
Jiyong Park09d77522019-11-18 11:16:27 +0900687 Description: "signapk",
688 Output: a.outputFile,
689 Input: unsignedOutputFile,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700690 Implicits: implicits,
691 Args: args,
Jiyong Park09d77522019-11-18 11:16:27 +0900692 })
693
694 // Install to $OUT/soong/{target,host}/.../apex
695 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800696 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900697 }
698 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900699
700 // installed-files.txt is dist'ed
701 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900702}
703
704func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
705 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
706 // reply true to `InstallBypassMake()` (thus making the call
707 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
708 // instead of `android.PathForOutput`) to return the correct path to the flattened
709 // APEX (as its contents is installed by Make, not Soong).
710 factx := flattenedApexContext{ctx}
Jooyung Han7f146c02020-09-23 19:15:55 +0900711 a.outputFile = android.PathForModuleInstall(&factx, "apex", a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900712 a.buildFilesInfo(ctx)
713}
714
715func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900716 if a.container_certificate_file == nil {
717 cert := String(a.properties.Certificate)
718 if cert == "" {
719 pem, key := ctx.Config().DefaultAppCertificate(ctx)
720 a.container_certificate_file = pem
721 a.container_private_key_file = key
722 } else {
723 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
724 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
725 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
726 }
Jiyong Park09d77522019-11-18 11:16:27 +0900727 }
728}
729
730func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
731 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900732 // 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 +0900733 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900734 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900735
736 // rename to apex_pubkey
737 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
738 ctx.Build(pctx, android.BuildParams{
739 Rule: android.Cp,
740 Input: a.public_key_file,
741 Output: copiedPubkey,
742 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900743 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900744
745 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900746 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900747 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900748 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900749 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.Stem(), fi.builtFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900750 for _, sym := range fi.symlinks {
751 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
752 }
753 }
754 }
755 }
756}
Jooyung Han27151d92019-12-16 17:45:32 +0900757
758func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
759 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
760 // to see if it should be overridden because their <apex name> is dynamically generated
761 // according to its VNDK version.
762 if a.vndkApex {
763 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
764 if overridden {
765 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
766 }
767 return ""
768 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700769 if a.overridableProperties.Package_name != "" {
770 return a.overridableProperties.Package_name
771 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900772 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900773 if overridden {
774 return manifestPackageName
775 }
776 return ""
777}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900778
779func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
780 if !a.primaryApexType {
781 return
782 }
783
784 if a.properties.IsCoverageVariant {
785 // Otherwise, we will have duplicated rules for coverage and
786 // non-coverage variants of the same APEX
787 return
788 }
789
790 if ctx.Host() {
791 // No need to generate dependency info for host variant
792 return
793 }
794
Artur Satayev872a1442020-04-27 17:08:37 +0100795 depInfos := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900796 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev872a1442020-04-27 17:08:37 +0100797 if from.Name() == to.Name() {
798 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
799 // As soon as the dependency graph crosses the APEX boundary, don't go further.
800 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900801 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900802
Artur Satayev872a1442020-04-27 17:08:37 +0100803 if info, exists := depInfos[to.Name()]; exists {
804 if !android.InList(from.Name(), info.From) {
805 info.From = append(info.From, from.Name())
806 }
807 info.IsExternal = info.IsExternal && externalDep
808 depInfos[to.Name()] = info
809 } else {
Artur Satayev480e25b2020-04-27 18:53:18 +0100810 toMinSdkVersion := "(no version)"
811 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
812 if v := m.MinSdkVersion(); v != "" {
813 toMinSdkVersion = v
814 }
815 }
816
Artur Satayev872a1442020-04-27 17:08:37 +0100817 depInfos[to.Name()] = android.ApexModuleDepInfo{
Artur Satayev480e25b2020-04-27 18:53:18 +0100818 To: to.Name(),
819 From: []string{from.Name()},
820 IsExternal: externalDep,
821 MinSdkVersion: toMinSdkVersion,
Artur Satayev872a1442020-04-27 17:08:37 +0100822 }
823 }
824
825 // As soon as the dependency graph crosses the APEX boundary, don't go further.
826 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +0900827 })
828
Artur Satayev480e25b2020-04-27 18:53:18 +0100829 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos)
Artur Satayev872a1442020-04-27 17:08:37 +0100830
Jiyong Park83dc74b2020-01-14 18:38:44 +0900831 ctx.Build(pctx, android.BuildParams{
832 Rule: android.Phony,
833 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +0100834 Inputs: []android.Path{
835 a.ApexBundleDepsInfo.FullListPath(),
836 a.ApexBundleDepsInfo.FlatListPath(),
837 },
Jiyong Park83dc74b2020-01-14 18:38:44 +0900838 })
839}
Colin Cross08dca382020-07-21 20:31:17 -0700840
841func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
842 depSetsBuilder := java.NewLintDepSetBuilder()
843 for _, fi := range a.filesInfo {
844 depSetsBuilder.Transitive(fi.lintDepSets)
845 }
846
847 a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
848}