blob: a3c4d5cea86f8fbca3c3f80489dba1dd89ed02d6 [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 Han2dfd54d2020-06-29 14:54:22 +0900260 // remove old file
Jooyung Han580eb4f2020-06-24 19:33:06 +0900261 rule.Command().Text("rm").FlagWithOutput("-f ", output)
Jooyung Han2dfd54d2020-06-29 14:54:22 +0900262 // copy file_contexts
Jooyung Han580eb4f2020-06-24 19:33:06 +0900263 rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output)
Jooyung Han2dfd54d2020-06-29 14:54:22 +0900264 // new line
Jooyung Han580eb4f2020-06-24 19:33:06 +0900265 rule.Command().Text("echo").Text(">>").Output(output)
Jooyung Han2dfd54d2020-06-29 14:54:22 +0900266 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
Jooyung Han580eb4f2020-06-24 19:33:06 +0900267 rule.Command().Text("echo").Flag("/apex_manifest\\\\.pb u:object_r:system_file:s0").Text(">>").Output(output)
Jooyung Han2dfd54d2020-06-29 14:54:22 +0900268 rule.Command().Text("echo").Flag("/ u:object_r:system_file:s0").Text(">>").Output(output)
Jooyung Han580eb4f2020-06-24 19:33:06 +0900269 rule.Build(pctx, ctx, "file_contexts."+a.Name(), "Generate file_contexts")
270
271 a.fileContexts = output.OutputPath
272}
273
Jiyong Park19972c72020-01-28 20:05:29 +0900274func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900275 var noticeFiles android.Paths
276
Jooyung Han749dc692020-04-15 11:03:39 +0900277 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900278 if externalDep {
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100279 // As soon as the dependency graph crosses the APEX boundary, don't go further.
280 return false
Jiyong Park09d77522019-11-18 11:16:27 +0900281 }
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100282
Jiyong Park9918e1a2020-03-17 19:16:40 +0900283 notices := to.NoticeFiles()
284 noticeFiles = append(noticeFiles, notices...)
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100285
286 return true
Jiyong Park9918e1a2020-03-17 19:16:40 +0900287 })
Jiyong Park09d77522019-11-18 11:16:27 +0900288
Jiyong Park41f637d2020-09-09 13:18:02 +0900289 for _, fi := range a.filesInfo {
290 noticeFiles = append(noticeFiles, fi.noticeFiles...)
291 }
292
Jiyong Park09d77522019-11-18 11:16:27 +0900293 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900294 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900295 }
296
Jiyong Park33c77362020-05-29 22:00:16 +0900297 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.SortedUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900298}
299
Jiyong Park3a1602e2020-01-14 14:39:19 +0900300func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
301 output := android.PathForModuleOut(ctx, "installed-files.txt")
302 rule := android.NewRuleBuilder()
303 rule.Command().
304 Implicit(builtApex).
305 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900306 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900307 Text(" | sort -nr > ").
308 Output(output)
309 rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files")
310 return output.OutputPath
311}
312
Jiyong Parkbd159612020-02-28 15:22:21 +0900313func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
314 output := android.PathForModuleOut(ctx, "bundle_config.json")
315
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900316 type ApkConfig struct {
317 Package_name string `json:"package_name"`
318 Apk_path string `json:"path"`
319 }
Jiyong Parkbd159612020-02-28 15:22:21 +0900320 config := struct {
321 Compression struct {
322 Uncompressed_glob []string `json:"uncompressed_glob"`
323 } `json:"compression"`
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900324 Apex_config struct {
325 Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
326 } `json:"apex_config,omitempty"`
Jiyong Parkbd159612020-02-28 15:22:21 +0900327 }{}
328
329 config.Compression.Uncompressed_glob = []string{
330 "apex_payload.img",
331 "apex_manifest.*",
332 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900333
334 // collect the manifest names and paths of android apps
335 // if their manifest names are overridden
336 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700337 if fi.class != app && fi.class != appSet {
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900338 continue
339 }
340 packageName := fi.overriddenPackageName
341 if packageName != "" {
342 config.Apex_config.Apex_embedded_apk_config = append(
343 config.Apex_config.Apex_embedded_apk_config,
344 ApkConfig{
345 Package_name: packageName,
346 Apk_path: fi.Path(),
347 })
348 }
349 }
350
Jiyong Parkbd159612020-02-28 15:22:21 +0900351 j, err := json.Marshal(config)
352 if err != nil {
353 panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
354 }
355
356 ctx.Build(pctx, android.BuildParams{
357 Rule: android.WriteFile,
358 Output: output,
359 Description: "Bundle Config " + output.String(),
360 Args: map[string]string{
361 "content": string(j),
362 },
363 })
364
365 return output.OutputPath
366}
367
Jiyong Park09d77522019-11-18 11:16:27 +0900368func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
369 var abis []string
370 for _, target := range ctx.MultiTargets() {
371 if len(target.Arch.Abi) > 0 {
372 abis = append(abis, target.Arch.Abi[0])
373 }
374 }
375
376 abis = android.FirstUniqueStrings(abis)
377
378 apexType := a.properties.ApexType
379 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900380 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800381 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900382
Jiyong Park7cd10e32020-01-14 09:22:18 +0900383 // TODO(jiyong): construct the copy rules using RuleBuilder
384 var copyCommands []string
385 for _, fi := range a.filesInfo {
386 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700387 destPathDir := filepath.Dir(destPath)
388 if fi.class == appSet {
389 copyCommands = append(copyCommands, "rm -rf "+destPathDir)
390 }
391 copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900392 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
393 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
394 pathOnDevice := filepath.Join("/system", fi.Path())
395 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
396 } else {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700397 if fi.class == appSet {
398 copyCommands = append(copyCommands,
Colin Crossd783bbb2020-07-11 22:30:45 -0700399 fmt.Sprintf("unzip -qDD -d %s %s", destPathDir, fi.builtFile.String()))
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700400 } else {
401 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
402 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900403 implicitInputs = append(implicitInputs, fi.builtFile)
404 }
405 // create additional symlinks pointing the file inside the APEX
406 for _, symlinkPath := range fi.SymlinkPaths() {
407 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000408 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900409 }
Liz Kammer1c14a212020-05-12 15:26:55 -0700410 for _, d := range fi.dataPaths {
411 // 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 -0400412 relPath := d.SrcPath.Rel()
413 dataPath := d.SrcPath.String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700414 if !strings.HasSuffix(dataPath, relPath) {
415 panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
416 }
417
Liz Kammer0a51aa22020-07-21 11:13:17 -0700418 dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700419
Chris Parsons216e10a2020-07-09 17:12:52 -0400420 copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
421 implicitInputs = append(implicitInputs, d.SrcPath)
Liz Kammer1c14a212020-05-12 15:26:55 -0700422 }
Jiyong Park09d77522019-11-18 11:16:27 +0900423 }
424
Jiyong Park7cd10e32020-01-14 09:22:18 +0900425 // TODO(jiyong): use RuleBuilder
426 var emitCommands []string
427 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900428 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
Dan Albertc8060532020-07-22 22:32:17 -0700429 minSdkVersion := a.minSdkVersion(ctx)
430 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900431 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
432 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900433 for _, fi := range a.filesInfo {
434 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900435 }
436 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900437 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900438
Jooyung Han938b5932020-06-20 12:47:47 +0900439 if a.overridableProperties.Allowed_files != nil {
Jiyong Park09d77522019-11-18 11:16:27 +0900440 ctx.Build(pctx, android.BuildParams{
441 Rule: emitApexContentRule,
442 Implicits: implicitInputs,
443 Output: imageContentFile,
444 Description: "emit apex image content",
445 Args: map[string]string{
446 "emit_commands": strings.Join(emitCommands, " && "),
447 },
448 })
449 implicitInputs = append(implicitInputs, imageContentFile)
Jooyung Han938b5932020-06-20 12:47:47 +0900450 allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
Jiyong Park09d77522019-11-18 11:16:27 +0900451
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800452 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900453 ctx.Build(pctx, android.BuildParams{
454 Rule: diffApexContentRule,
455 Implicits: implicitInputs,
456 Output: phonyOutput,
457 Description: "diff apex image content",
458 Args: map[string]string{
Colin Cross440e0d02020-06-11 11:32:11 -0700459 "allowed_files_file": allowedFilesFile.String(),
460 "image_content_file": imageContentFile.String(),
461 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900462 },
463 })
464
465 implicitInputs = append(implicitInputs, phonyOutput)
466 }
467
468 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
469 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
470
Jiyong Park3a1602e2020-01-14 14:39:19 +0900471 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900472 if apexType == imageApex {
473 // files and dirs that will be created in APEX
474 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
475 var executablePaths []string // this also includes dirs
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700476 var extractedAppSetPaths android.Paths
477 var extractedAppSetDirs []string
Jiyong Park09d77522019-11-18 11:16:27 +0900478 for _, f := range a.filesInfo {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900479 pathInApex := f.Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900480 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
481 executablePaths = append(executablePaths, pathInApex)
Liz Kammer1c14a212020-05-12 15:26:55 -0700482 for _, d := range f.dataPaths {
Liz Kammer0a51aa22020-07-21 11:13:17 -0700483 readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
Liz Kammer1c14a212020-05-12 15:26:55 -0700484 }
Jiyong Park09d77522019-11-18 11:16:27 +0900485 for _, s := range f.symlinks {
486 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
487 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700488 } else if f.class == appSet {
489 extractedAppSetPaths = append(extractedAppSetPaths, f.builtFile)
490 extractedAppSetDirs = append(extractedAppSetDirs, f.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900491 } else {
492 readOnlyPaths = append(readOnlyPaths, pathInApex)
493 }
494 dir := f.installDir
495 for !android.InList(dir, executablePaths) && dir != "" {
496 executablePaths = append(executablePaths, dir)
497 dir, _ = filepath.Split(dir) // move up to the parent
498 if len(dir) > 0 {
499 // remove trailing slash
500 dir = dir[:len(dir)-1]
501 }
502 }
503 }
504 sort.Strings(readOnlyPaths)
505 sort.Strings(executablePaths)
506 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
507 ctx.Build(pctx, android.BuildParams{
508 Rule: generateFsConfig,
509 Output: cannedFsConfig,
510 Description: "generate fs config",
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700511 Inputs: extractedAppSetPaths,
Jiyong Park09d77522019-11-18 11:16:27 +0900512 Args: map[string]string{
513 "ro_paths": strings.Join(readOnlyPaths, " "),
514 "exec_paths": strings.Join(executablePaths, " "),
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700515 "apk_paths": strings.Join(extractedAppSetDirs, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900516 },
517 })
518
Jiyong Park09d77522019-11-18 11:16:27 +0900519 optFlags := []string{}
520
521 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900522 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900523 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
524
Jooyung Han27151d92019-12-16 17:45:32 +0900525 manifestPackageName := a.getOverrideManifestPackageName(ctx)
526 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900527 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
528 }
529
530 if a.properties.AndroidManifest != nil {
531 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
532 implicitInputs = append(implicitInputs, androidManifestFile)
533 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
534 }
535
536 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
Nikita Ioffe644009a2020-05-20 00:16:27 +0100537 // TODO(b/157078772): propagate min_sdk_version to apexer.
Baligh Uddinf6201372020-01-24 23:15:44 +0000538 minSdkVersion := ctx.Config().DefaultAppTargetSdk()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000539
Dan Albertc8060532020-07-22 22:32:17 -0700540 moduleMinSdkVersion := a.minSdkVersion(ctx)
541 if moduleMinSdkVersion.EqualTo(android.SdkVersion_Android10) {
542 minSdkVersion = moduleMinSdkVersion.String()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000543 }
544
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000545 if java.UseApiFingerprint(ctx) {
546 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000547 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
548 }
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000549 if java.UseApiFingerprint(ctx) {
550 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000551 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
Jiyong Park09d77522019-11-18 11:16:27 +0900552 }
553 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000554 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900555
Baligh Uddin004d7172020-02-19 21:29:28 -0800556 if a.overridableProperties.Logging_parent != "" {
557 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
558 }
559
Jiyong Park19972c72020-01-28 20:05:29 +0900560 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
561 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900562 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900563 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
564 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900565 }
566
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000567 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000568 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
569 return
570 }
Dan Albertc8060532020-07-22 22:32:17 -0700571 if moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900572 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
573 // don't need hashtree for activation. Therefore, by removing hashtree from
574 // apex bundle (filesystem image in it, to be specific), we can save storage.
575 optFlags = append(optFlags, "--no_hashtree")
576 }
577
Dario Frenica913392020-04-27 18:21:11 +0100578 if a.testOnlyShouldSkipPayloadSign() {
579 optFlags = append(optFlags, "--unsigned_payload")
580 }
581
Jiyong Park09d77522019-11-18 11:16:27 +0900582 if a.properties.Apex_name != nil {
583 // If apex_name is set, apexer can skip checking if key name matches with apex name.
584 // Note that apex_manifest is also mended.
585 optFlags = append(optFlags, "--do_not_check_keyname")
586 }
587
Dan Albertc8060532020-07-22 22:32:17 -0700588 if moduleMinSdkVersion == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900589 implicitInputs = append(implicitInputs, a.manifestJsonOut)
590 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
591 }
592
Theotime Combes4ba38c12020-06-12 12:46:59 +0000593 optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
594
Jiyong Park09d77522019-11-18 11:16:27 +0900595 ctx.Build(pctx, android.BuildParams{
596 Rule: apexRule,
597 Implicits: implicitInputs,
598 Output: unsignedOutputFile,
599 Description: "apex (" + apexType.name() + ")",
600 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900601 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900602 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900603 "copy_commands": strings.Join(copyCommands, " && "),
604 "manifest": a.manifestPbOut.String(),
605 "file_contexts": a.fileContexts.String(),
606 "canned_fs_config": cannedFsConfig.String(),
607 "key": a.private_key_file.String(),
608 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900609 },
610 })
611
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800612 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
613 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900614 a.bundleModuleFile = bundleModuleFile
615
616 ctx.Build(pctx, android.BuildParams{
617 Rule: apexProtoConvertRule,
618 Input: unsignedOutputFile,
619 Output: apexProtoFile,
620 Description: "apex proto convert",
621 })
622
Jiyong Parkbd159612020-02-28 15:22:21 +0900623 bundleConfig := a.buildBundleConfig(ctx)
624
Jiyong Park09d77522019-11-18 11:16:27 +0900625 ctx.Build(pctx, android.BuildParams{
626 Rule: apexBundleRule,
627 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900628 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900629 Output: a.bundleModuleFile,
630 Description: "apex bundle module",
631 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900632 "abi": strings.Join(abis, "."),
633 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900634 },
635 })
636 } else {
637 ctx.Build(pctx, android.BuildParams{
638 Rule: zipApexRule,
639 Implicits: implicitInputs,
640 Output: unsignedOutputFile,
641 Description: "apex (" + apexType.name() + ")",
642 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900643 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900644 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900645 "copy_commands": strings.Join(copyCommands, " && "),
646 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900647 },
648 })
649 }
650
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800651 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Kousik Kumar309b1c02020-05-28 06:13:33 -0700652 rule := java.Signapk
653 args := map[string]string{
654 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
655 "flags": "-a 4096", //alignment
656 }
657 implicits := android.Paths{
658 a.container_certificate_file,
659 a.container_private_key_file,
660 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400661 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700662 rule = java.SignapkRE
663 args["implicits"] = strings.Join(implicits.Strings(), ",")
664 args["outCommaList"] = a.outputFile.String()
665 }
Jiyong Park09d77522019-11-18 11:16:27 +0900666 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700667 Rule: rule,
Jiyong Park09d77522019-11-18 11:16:27 +0900668 Description: "signapk",
669 Output: a.outputFile,
670 Input: unsignedOutputFile,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700671 Implicits: implicits,
672 Args: args,
Jiyong Park09d77522019-11-18 11:16:27 +0900673 })
674
675 // Install to $OUT/soong/{target,host}/.../apex
676 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800677 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900678 }
679 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900680
681 // installed-files.txt is dist'ed
682 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900683}
684
685func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
686 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
687 // reply true to `InstallBypassMake()` (thus making the call
688 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
689 // instead of `android.PathForOutput`) to return the correct path to the flattened
690 // APEX (as its contents is installed by Make, not Soong).
691 factx := flattenedApexContext{ctx}
Jiyong Parka5948012020-02-07 10:15:14 +0900692 apexBundleName := a.Name()
693 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexBundleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900694
Jooyung Han6bfc0432020-04-27 22:18:19 +0900695 if a.installable() {
Jiyong Parka5948012020-02-07 10:15:14 +0900696 installPath := android.PathForModuleInstall(ctx, "apex", apexBundleName)
Jooyung Han54aca7b2019-11-20 02:26:02 +0900697 devicePath := android.InstallPathToOnDevicePath(ctx, installPath)
Jiyong Parka5948012020-02-07 10:15:14 +0900698 addFlattenedFileContextsInfos(ctx, apexBundleName+":"+devicePath+":"+a.fileContexts.String())
Jooyung Han54aca7b2019-11-20 02:26:02 +0900699 }
Jiyong Park09d77522019-11-18 11:16:27 +0900700 a.buildFilesInfo(ctx)
701}
702
703func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900704 if a.container_certificate_file == nil {
705 cert := String(a.properties.Certificate)
706 if cert == "" {
707 pem, key := ctx.Config().DefaultAppCertificate(ctx)
708 a.container_certificate_file = pem
709 a.container_private_key_file = key
710 } else {
711 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
712 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
713 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
714 }
Jiyong Park09d77522019-11-18 11:16:27 +0900715 }
716}
717
718func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
719 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900720 // 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 +0900721 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900722 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900723
724 // rename to apex_pubkey
725 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
726 ctx.Build(pctx, android.BuildParams{
727 Rule: android.Cp,
728 Input: a.public_key_file,
729 Output: copiedPubkey,
730 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900731 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900732
733 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900734 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900735 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900736 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900737 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.Stem(), fi.builtFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900738 for _, sym := range fi.symlinks {
739 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
740 }
741 }
742 }
743 }
744}
Jooyung Han27151d92019-12-16 17:45:32 +0900745
746func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
747 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
748 // to see if it should be overridden because their <apex name> is dynamically generated
749 // according to its VNDK version.
750 if a.vndkApex {
751 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
752 if overridden {
753 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
754 }
755 return ""
756 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700757 if a.overridableProperties.Package_name != "" {
758 return a.overridableProperties.Package_name
759 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900760 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900761 if overridden {
762 return manifestPackageName
763 }
764 return ""
765}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900766
767func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
768 if !a.primaryApexType {
769 return
770 }
771
772 if a.properties.IsCoverageVariant {
773 // Otherwise, we will have duplicated rules for coverage and
774 // non-coverage variants of the same APEX
775 return
776 }
777
778 if ctx.Host() {
779 // No need to generate dependency info for host variant
780 return
781 }
782
Artur Satayev872a1442020-04-27 17:08:37 +0100783 depInfos := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900784 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev872a1442020-04-27 17:08:37 +0100785 if from.Name() == to.Name() {
786 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
787 // As soon as the dependency graph crosses the APEX boundary, don't go further.
788 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900789 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900790
Artur Satayev872a1442020-04-27 17:08:37 +0100791 if info, exists := depInfos[to.Name()]; exists {
792 if !android.InList(from.Name(), info.From) {
793 info.From = append(info.From, from.Name())
794 }
795 info.IsExternal = info.IsExternal && externalDep
796 depInfos[to.Name()] = info
797 } else {
Artur Satayev480e25b2020-04-27 18:53:18 +0100798 toMinSdkVersion := "(no version)"
799 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
800 if v := m.MinSdkVersion(); v != "" {
801 toMinSdkVersion = v
802 }
803 }
804
Artur Satayev872a1442020-04-27 17:08:37 +0100805 depInfos[to.Name()] = android.ApexModuleDepInfo{
Artur Satayev480e25b2020-04-27 18:53:18 +0100806 To: to.Name(),
807 From: []string{from.Name()},
808 IsExternal: externalDep,
809 MinSdkVersion: toMinSdkVersion,
Artur Satayev872a1442020-04-27 17:08:37 +0100810 }
811 }
812
813 // As soon as the dependency graph crosses the APEX boundary, don't go further.
814 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +0900815 })
816
Artur Satayev480e25b2020-04-27 18:53:18 +0100817 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos)
Artur Satayev872a1442020-04-27 17:08:37 +0100818
Jiyong Park83dc74b2020-01-14 18:38:44 +0900819 ctx.Build(pctx, android.BuildParams{
820 Rule: android.Phony,
821 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +0100822 Inputs: []android.Path{
823 a.ApexBundleDepsInfo.FullListPath(),
824 a.ApexBundleDepsInfo.FlatListPath(),
825 },
Jiyong Park83dc74b2020-01-14 18:38:44 +0900826 })
827}
Colin Cross08dca382020-07-21 20:31:17 -0700828
829func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
830 depSetsBuilder := java.NewLintDepSetBuilder()
831 for _, fi := range a.filesInfo {
832 depSetsBuilder.Transitive(fi.lintDepSets)
833 }
834
835 a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
836}