blob: ad673d656974483cf79bbbbbea76d51671f55887 [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"
Nikita Ioffe5335bc42020-10-20 00:02:15 +010024 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090025 "strings"
26
27 "android/soong/android"
28 "android/soong/java"
29
30 "github.com/google/blueprint"
31 "github.com/google/blueprint/proptools"
32)
33
34var (
35 pctx = android.NewPackageContext("android/apex")
36)
37
38func init() {
39 pctx.Import("android/soong/android")
40 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"
43 // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead.
44 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")
Jiyong Park09d77522019-11-18 11:16:27 +090068}
69
70var (
71 // Create a canned fs config file where all files and directories are
72 // by default set to (uid/gid/mode) = (1000/1000/0644)
73 // TODO(b/113082813) make this configurable using config.fs syntax
74 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
Sasha Smundak18d98bc2020-05-27 16:36:07 -070075 Command: `( echo '/ 1000 1000 0755' ` +
76 `&& for i in ${ro_paths}; do echo "/$$i 1000 1000 0644"; done ` +
77 `&& for i in ${exec_paths}; do echo "/$$i 0 2000 0755"; done ` +
78 `&& ( 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}`,
79 Description: "fs_config ${out}",
80 Rspfile: "$out.apklist",
81 RspfileContent: "$in",
82 }, "ro_paths", "exec_paths", "apk_paths")
Jiyong Park09d77522019-11-18 11:16:27 +090083
84 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
85 Command: `rm -f $out && ${jsonmodify} $in ` +
86 `-a provideNativeLibs ${provideNativeLibs} ` +
87 `-a requireNativeLibs ${requireNativeLibs} ` +
88 `${opt} ` +
89 `-o $out`,
90 CommandDeps: []string{"${jsonmodify}"},
91 Description: "prepare ${out}",
92 }, "provideNativeLibs", "requireNativeLibs", "opt")
93
94 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
95 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
96 CommandDeps: []string{"${conv_apex_manifest}"},
97 Description: "strip ${in}=>${out}",
98 })
99
100 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
101 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
102 CommandDeps: []string{"${conv_apex_manifest}"},
103 Description: "convert ${in}=>${out}",
104 })
105
106 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
107 // against the binary policy using sefcontext_compiler -p <policy>.
108
109 // TODO(b/114327326): automate the generation of file_contexts
110 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
111 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
112 `(. ${out}.copy_commands) && ` +
113 `APEXER_TOOL_PATH=${tool_path} ` +
114 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900115 `--file_contexts ${file_contexts} ` +
116 `--canned_fs_config ${canned_fs_config} ` +
Dario Freni0f4ae072020-01-02 15:24:12 +0000117 `--include_build_info ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900118 `--payload_type image ` +
119 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
120 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
Theotime Combes4ba38c12020-06-12 12:46:59 +0000121 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}", "${sload_f2fs}",
Jiyong Park09d77522019-11-18 11:16:27 +0900122 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
123 Rspfile: "${out}.copy_commands",
124 RspfileContent: "${copy_commands}",
125 Description: "APEX ${image_dir} => ${out}",
Theotime Combes4ba38c12020-06-12 12:46:59 +0000126 }, "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 +0900127
128 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
129 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
130 `(. ${out}.copy_commands) && ` +
131 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han214bf372019-11-12 13:03:50 +0900132 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900133 `--payload_type zip ` +
134 `${image_dir} ${out} `,
135 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
136 Rspfile: "${out}.copy_commands",
137 RspfileContent: "${copy_commands}",
138 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900139 }, "tool_path", "image_dir", "copy_commands", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900140
141 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
142 blueprint.RuleParams{
143 Command: `${aapt2} convert --output-format proto $in -o $out`,
144 CommandDeps: []string{"${aapt2}"},
145 })
146
147 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Parkbd159612020-02-28 15:22:21 +0900148 Command: `${zip2zip} -i $in -o $out.base ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900149 `apex_payload.img:apex/${abi}.img ` +
Dario Frenida1aefe2020-03-02 21:47:09 +0000150 `apex_build_info.pb:apex/${abi}.build_info.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900151 `apex_manifest.json:root/apex_manifest.json ` +
Jiyong Park53ae3342019-12-08 02:06:24 +0900152 `apex_manifest.pb:root/apex_manifest.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900153 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
Jiyong Parkbd159612020-02-28 15:22:21 +0900154 `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` +
155 `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` +
156 `${merge_zips} $out $out.base $out.config`,
157 CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"},
Jiyong Park09d77522019-11-18 11:16:27 +0900158 Description: "app bundle",
Jiyong Parkbd159612020-02-28 15:22:21 +0900159 }, "abi", "config")
Jiyong Park09d77522019-11-18 11:16:27 +0900160
161 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
162 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
163 Rspfile: "${out}.emit_commands",
164 RspfileContent: "${emit_commands}",
165 Description: "Emit APEX image content",
166 }, "emit_commands")
167
168 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
169 Command: `diff --unchanged-group-format='' \` +
170 `--changed-group-format='%<' \` +
Colin Cross440e0d02020-06-11 11:32:11 -0700171 `${image_content_file} ${allowed_files_file} || (` +
Jiyong Park09d77522019-11-18 11:16:27 +0900172 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
173 ` "To fix the build run following command:" && ` +
Colin Cross440e0d02020-06-11 11:32:11 -0700174 `echo "system/apex/tools/update_allowed_list.sh ${allowed_files_file} ${image_content_file}" && ` +
Dan Willemsen81e43c52020-01-28 15:40:19 -0800175 `exit 1); touch ${out}`,
Colin Cross440e0d02020-06-11 11:32:11 -0700176 Description: "Diff ${image_content_file} and ${allowed_files_file}",
177 }, "image_content_file", "allowed_files_file", "apex_module_name")
Jiyong Park09d77522019-11-18 11:16:27 +0900178)
179
180func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
181 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
182
Jooyung Han214bf372019-11-12 13:03:50 +0900183 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900184
185 // put dependency({provide|require}NativeLibs) in apex_manifest.json
186 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
187 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
188
189 // apex name can be overridden
190 optCommands := []string{}
191 if a.properties.Apex_name != nil {
192 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
193 }
194
Jooyung Han643adc42020-02-27 13:50:06 +0900195 // collect jniLibs. Notice that a.filesInfo is already sorted
196 var jniLibs []string
197 for _, fi := range a.filesInfo {
Jooyung Hanb9007602020-08-28 16:29:27 +0900198 if fi.isJniLib && !android.InList(fi.Stem(), jniLibs) {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900199 jniLibs = append(jniLibs, fi.Stem())
Jooyung Han643adc42020-02-27 13:50:06 +0900200 }
201 }
202 if len(jniLibs) > 0 {
203 optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
204 }
205
Jiyong Park09d77522019-11-18 11:16:27 +0900206 ctx.Build(pctx, android.BuildParams{
207 Rule: apexManifestRule,
208 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900209 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900210 Args: map[string]string{
211 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
212 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
213 "opt": strings.Join(optCommands, " "),
214 },
215 })
216
Dan Albertc8060532020-07-22 22:32:17 -0700217 minSdkVersion := a.minSdkVersion(ctx)
218 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900219 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
220 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
221 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
222 ctx.Build(pctx, android.BuildParams{
223 Rule: stripApexManifestRule,
224 Input: manifestJsonFullOut,
225 Output: a.manifestJsonOut,
226 })
227 }
Jiyong Park09d77522019-11-18 11:16:27 +0900228
229 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
230 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
231 ctx.Build(pctx, android.BuildParams{
232 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900233 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900234 Output: a.manifestPbOut,
235 })
236}
237
Jooyung Han580eb4f2020-06-24 19:33:06 +0900238func (a *apexBundle) buildFileContexts(ctx android.ModuleContext) {
239 if a.properties.ApexType == zipApex {
240 return
241 }
242 var fileContexts android.Path
243 if a.properties.File_contexts == nil {
244 fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
245 } else {
246 fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
247 }
248 if a.Platform() {
249 if matched, err := path.Match("system/sepolicy/**/*", fileContexts.String()); err != nil || !matched {
250 ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but %q", fileContexts)
251 return
252 }
253 }
254 if !android.ExistentPathForSource(ctx, fileContexts.String()).Valid() {
255 ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", a.fileContexts)
256 return
257 }
258
259 output := android.PathForModuleOut(ctx, "file_contexts")
260 rule := android.NewRuleBuilder()
Jooyung Han7f146c02020-09-23 19:15:55 +0900261
262 if a.properties.ApexType == imageApex {
263 // remove old file
264 rule.Command().Text("rm").FlagWithOutput("-f ", output)
265 // copy file_contexts
266 rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output)
267 // new line
268 rule.Command().Text("echo").Text(">>").Output(output)
269 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
270 rule.Command().Text("echo").Flag("/apex_manifest\\\\.pb u:object_r:system_file:s0").Text(">>").Output(output)
271 rule.Command().Text("echo").Flag("/ u:object_r:system_file:s0").Text(">>").Output(output)
272 } else {
273 // For flattened apexes, install path should be prepended.
274 // File_contexts file should be emiited to make via LOCAL_FILE_CONTEXTS
275 // so that it can be merged into file_contexts.bin
276 apexPath := android.InstallPathToOnDevicePath(ctx, a.installDir.Join(ctx, a.Name()))
277 apexPath = strings.ReplaceAll(apexPath, ".", `\\.`)
278 // remove old file
279 rule.Command().Text("rm").FlagWithOutput("-f ", output)
280 // copy file_contexts
281 rule.Command().Text("awk").Text(`'/object_r/{printf("` + apexPath + `%s\n", $0)}'`).Input(fileContexts).Text(">").Output(output)
282 // new line
283 rule.Command().Text("echo").Text(">>").Output(output)
284 // force-label /apex_manifest.pb and / as system_file so that apexd can read them
285 rule.Command().Text("echo").Flag(apexPath + `/apex_manifest\\.pb u:object_r:system_file:s0`).Text(">>").Output(output)
286 rule.Command().Text("echo").Flag(apexPath + "/ u:object_r:system_file:s0").Text(">>").Output(output)
287 }
288
Jooyung Han580eb4f2020-06-24 19:33:06 +0900289 rule.Build(pctx, ctx, "file_contexts."+a.Name(), "Generate file_contexts")
290
291 a.fileContexts = output.OutputPath
292}
293
Jiyong Park19972c72020-01-28 20:05:29 +0900294func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900295 var noticeFiles android.Paths
296
Jooyung Han749dc692020-04-15 11:03:39 +0900297 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900298 if externalDep {
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100299 // As soon as the dependency graph crosses the APEX boundary, don't go further.
300 return false
Jiyong Park09d77522019-11-18 11:16:27 +0900301 }
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100302
Jiyong Park9918e1a2020-03-17 19:16:40 +0900303 notices := to.NoticeFiles()
304 noticeFiles = append(noticeFiles, notices...)
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100305
306 return true
Jiyong Park9918e1a2020-03-17 19:16:40 +0900307 })
Jiyong Park09d77522019-11-18 11:16:27 +0900308
Jiyong Park41f637d2020-09-09 13:18:02 +0900309 for _, fi := range a.filesInfo {
310 noticeFiles = append(noticeFiles, fi.noticeFiles...)
311 }
312
Jiyong Park09d77522019-11-18 11:16:27 +0900313 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900314 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900315 }
316
Jiyong Park33c77362020-05-29 22:00:16 +0900317 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.SortedUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900318}
319
Jiyong Park3a1602e2020-01-14 14:39:19 +0900320func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
321 output := android.PathForModuleOut(ctx, "installed-files.txt")
322 rule := android.NewRuleBuilder()
323 rule.Command().
324 Implicit(builtApex).
325 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900326 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900327 Text(" | sort -nr > ").
328 Output(output)
329 rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files")
330 return output.OutputPath
331}
332
Jiyong Parkbd159612020-02-28 15:22:21 +0900333func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
334 output := android.PathForModuleOut(ctx, "bundle_config.json")
335
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900336 type ApkConfig struct {
337 Package_name string `json:"package_name"`
338 Apk_path string `json:"path"`
339 }
Jiyong Parkbd159612020-02-28 15:22:21 +0900340 config := struct {
341 Compression struct {
342 Uncompressed_glob []string `json:"uncompressed_glob"`
343 } `json:"compression"`
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900344 Apex_config struct {
345 Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
346 } `json:"apex_config,omitempty"`
Jiyong Parkbd159612020-02-28 15:22:21 +0900347 }{}
348
349 config.Compression.Uncompressed_glob = []string{
350 "apex_payload.img",
351 "apex_manifest.*",
352 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900353
354 // collect the manifest names and paths of android apps
355 // if their manifest names are overridden
356 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700357 if fi.class != app && fi.class != appSet {
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900358 continue
359 }
360 packageName := fi.overriddenPackageName
361 if packageName != "" {
362 config.Apex_config.Apex_embedded_apk_config = append(
363 config.Apex_config.Apex_embedded_apk_config,
364 ApkConfig{
365 Package_name: packageName,
366 Apk_path: fi.Path(),
367 })
368 }
369 }
370
Jiyong Parkbd159612020-02-28 15:22:21 +0900371 j, err := json.Marshal(config)
372 if err != nil {
373 panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
374 }
375
376 ctx.Build(pctx, android.BuildParams{
377 Rule: android.WriteFile,
378 Output: output,
379 Description: "Bundle Config " + output.String(),
380 Args: map[string]string{
381 "content": string(j),
382 },
383 })
384
385 return output.OutputPath
386}
387
Jiyong Park09d77522019-11-18 11:16:27 +0900388func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
389 var abis []string
390 for _, target := range ctx.MultiTargets() {
391 if len(target.Arch.Abi) > 0 {
392 abis = append(abis, target.Arch.Abi[0])
393 }
394 }
395
396 abis = android.FirstUniqueStrings(abis)
397
398 apexType := a.properties.ApexType
399 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900400 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800401 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900402
Jiyong Park7cd10e32020-01-14 09:22:18 +0900403 // TODO(jiyong): construct the copy rules using RuleBuilder
404 var copyCommands []string
405 for _, fi := range a.filesInfo {
406 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700407 destPathDir := filepath.Dir(destPath)
408 if fi.class == appSet {
409 copyCommands = append(copyCommands, "rm -rf "+destPathDir)
410 }
411 copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900412 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
413 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
414 pathOnDevice := filepath.Join("/system", fi.Path())
415 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
416 } else {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700417 if fi.class == appSet {
418 copyCommands = append(copyCommands,
Colin Crossd783bbb2020-07-11 22:30:45 -0700419 fmt.Sprintf("unzip -qDD -d %s %s", destPathDir, fi.builtFile.String()))
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700420 } else {
421 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
422 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900423 implicitInputs = append(implicitInputs, fi.builtFile)
424 }
425 // create additional symlinks pointing the file inside the APEX
426 for _, symlinkPath := range fi.SymlinkPaths() {
427 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000428 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900429 }
Liz Kammer1c14a212020-05-12 15:26:55 -0700430 for _, d := range fi.dataPaths {
431 // 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 -0400432 relPath := d.SrcPath.Rel()
433 dataPath := d.SrcPath.String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700434 if !strings.HasSuffix(dataPath, relPath) {
435 panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
436 }
437
Liz Kammer0a51aa22020-07-21 11:13:17 -0700438 dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700439
Chris Parsons216e10a2020-07-09 17:12:52 -0400440 copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
441 implicitInputs = append(implicitInputs, d.SrcPath)
Liz Kammer1c14a212020-05-12 15:26:55 -0700442 }
Jiyong Park09d77522019-11-18 11:16:27 +0900443 }
444
Jiyong Park7cd10e32020-01-14 09:22:18 +0900445 // TODO(jiyong): use RuleBuilder
446 var emitCommands []string
447 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900448 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
Dan Albertc8060532020-07-22 22:32:17 -0700449 minSdkVersion := a.minSdkVersion(ctx)
450 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900451 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
452 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900453 for _, fi := range a.filesInfo {
454 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900455 }
456 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900457 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900458
Jooyung Han938b5932020-06-20 12:47:47 +0900459 if a.overridableProperties.Allowed_files != nil {
Jiyong Park09d77522019-11-18 11:16:27 +0900460 ctx.Build(pctx, android.BuildParams{
461 Rule: emitApexContentRule,
462 Implicits: implicitInputs,
463 Output: imageContentFile,
464 Description: "emit apex image content",
465 Args: map[string]string{
466 "emit_commands": strings.Join(emitCommands, " && "),
467 },
468 })
469 implicitInputs = append(implicitInputs, imageContentFile)
Jooyung Han938b5932020-06-20 12:47:47 +0900470 allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
Jiyong Park09d77522019-11-18 11:16:27 +0900471
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800472 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900473 ctx.Build(pctx, android.BuildParams{
474 Rule: diffApexContentRule,
475 Implicits: implicitInputs,
476 Output: phonyOutput,
477 Description: "diff apex image content",
478 Args: map[string]string{
Colin Cross440e0d02020-06-11 11:32:11 -0700479 "allowed_files_file": allowedFilesFile.String(),
480 "image_content_file": imageContentFile.String(),
481 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900482 },
483 })
484
485 implicitInputs = append(implicitInputs, phonyOutput)
486 }
487
488 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
489 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
490
Jiyong Park3a1602e2020-01-14 14:39:19 +0900491 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900492 if apexType == imageApex {
493 // files and dirs that will be created in APEX
494 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
495 var executablePaths []string // this also includes dirs
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700496 var extractedAppSetPaths android.Paths
497 var extractedAppSetDirs []string
Jiyong Park09d77522019-11-18 11:16:27 +0900498 for _, f := range a.filesInfo {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900499 pathInApex := f.Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900500 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
501 executablePaths = append(executablePaths, pathInApex)
Liz Kammer1c14a212020-05-12 15:26:55 -0700502 for _, d := range f.dataPaths {
Liz Kammer0a51aa22020-07-21 11:13:17 -0700503 readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
Liz Kammer1c14a212020-05-12 15:26:55 -0700504 }
Jiyong Park09d77522019-11-18 11:16:27 +0900505 for _, s := range f.symlinks {
506 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
507 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700508 } else if f.class == appSet {
509 extractedAppSetPaths = append(extractedAppSetPaths, f.builtFile)
510 extractedAppSetDirs = append(extractedAppSetDirs, f.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900511 } else {
512 readOnlyPaths = append(readOnlyPaths, pathInApex)
513 }
514 dir := f.installDir
515 for !android.InList(dir, executablePaths) && dir != "" {
516 executablePaths = append(executablePaths, dir)
517 dir, _ = filepath.Split(dir) // move up to the parent
518 if len(dir) > 0 {
519 // remove trailing slash
520 dir = dir[:len(dir)-1]
521 }
522 }
523 }
524 sort.Strings(readOnlyPaths)
525 sort.Strings(executablePaths)
526 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
527 ctx.Build(pctx, android.BuildParams{
528 Rule: generateFsConfig,
529 Output: cannedFsConfig,
530 Description: "generate fs config",
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700531 Inputs: extractedAppSetPaths,
Jiyong Park09d77522019-11-18 11:16:27 +0900532 Args: map[string]string{
533 "ro_paths": strings.Join(readOnlyPaths, " "),
534 "exec_paths": strings.Join(executablePaths, " "),
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700535 "apk_paths": strings.Join(extractedAppSetDirs, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900536 },
537 })
538
Jiyong Park09d77522019-11-18 11:16:27 +0900539 optFlags := []string{}
540
541 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900542 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900543 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
544
Jooyung Han27151d92019-12-16 17:45:32 +0900545 manifestPackageName := a.getOverrideManifestPackageName(ctx)
546 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900547 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
548 }
549
550 if a.properties.AndroidManifest != nil {
551 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
552 implicitInputs = append(implicitInputs, androidManifestFile)
553 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
554 }
555
Dan Albertc8060532020-07-22 22:32:17 -0700556 moduleMinSdkVersion := a.minSdkVersion(ctx)
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100557 minSdkVersion := moduleMinSdkVersion.String()
558
559 // bundletool doesn't understand what "current" is. We need to transform it to codename
560 if moduleMinSdkVersion.IsCurrent() {
561 minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000562 }
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100563 // apex module doesn't have a concept of target_sdk_version, hence for the time being
564 // targetSdkVersion == default targetSdkVersion of the branch.
565 targetSdkVersion := strconv.Itoa(ctx.Config().DefaultAppTargetSdk(ctx).FinalOrFutureInt())
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000566
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000567 if java.UseApiFingerprint(ctx) {
568 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000569 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
570 }
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000571 if java.UseApiFingerprint(ctx) {
572 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000573 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
Jiyong Park09d77522019-11-18 11:16:27 +0900574 }
575 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000576 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900577
Baligh Uddin004d7172020-02-19 21:29:28 -0800578 if a.overridableProperties.Logging_parent != "" {
579 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
580 }
581
Jiyong Park19972c72020-01-28 20:05:29 +0900582 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
583 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900584 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900585 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
586 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900587 }
588
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000589 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000590 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
591 return
592 }
Dan Albertc8060532020-07-22 22:32:17 -0700593 if moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900594 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
595 // don't need hashtree for activation. Therefore, by removing hashtree from
596 // apex bundle (filesystem image in it, to be specific), we can save storage.
597 optFlags = append(optFlags, "--no_hashtree")
598 }
599
Dario Frenica913392020-04-27 18:21:11 +0100600 if a.testOnlyShouldSkipPayloadSign() {
601 optFlags = append(optFlags, "--unsigned_payload")
602 }
603
Jiyong Park09d77522019-11-18 11:16:27 +0900604 if a.properties.Apex_name != nil {
605 // If apex_name is set, apexer can skip checking if key name matches with apex name.
606 // Note that apex_manifest is also mended.
607 optFlags = append(optFlags, "--do_not_check_keyname")
608 }
609
Dan Albertc8060532020-07-22 22:32:17 -0700610 if moduleMinSdkVersion == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900611 implicitInputs = append(implicitInputs, a.manifestJsonOut)
612 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
613 }
614
Theotime Combes4ba38c12020-06-12 12:46:59 +0000615 optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
616
Jiyong Park09d77522019-11-18 11:16:27 +0900617 ctx.Build(pctx, android.BuildParams{
618 Rule: apexRule,
619 Implicits: implicitInputs,
620 Output: unsignedOutputFile,
621 Description: "apex (" + apexType.name() + ")",
622 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900623 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900624 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900625 "copy_commands": strings.Join(copyCommands, " && "),
626 "manifest": a.manifestPbOut.String(),
627 "file_contexts": a.fileContexts.String(),
628 "canned_fs_config": cannedFsConfig.String(),
629 "key": a.private_key_file.String(),
630 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900631 },
632 })
633
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800634 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
635 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900636 a.bundleModuleFile = bundleModuleFile
637
638 ctx.Build(pctx, android.BuildParams{
639 Rule: apexProtoConvertRule,
640 Input: unsignedOutputFile,
641 Output: apexProtoFile,
642 Description: "apex proto convert",
643 })
644
Jiyong Parkbd159612020-02-28 15:22:21 +0900645 bundleConfig := a.buildBundleConfig(ctx)
646
Jiyong Park09d77522019-11-18 11:16:27 +0900647 ctx.Build(pctx, android.BuildParams{
648 Rule: apexBundleRule,
649 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900650 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900651 Output: a.bundleModuleFile,
652 Description: "apex bundle module",
653 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900654 "abi": strings.Join(abis, "."),
655 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900656 },
657 })
658 } else {
659 ctx.Build(pctx, android.BuildParams{
660 Rule: zipApexRule,
661 Implicits: implicitInputs,
662 Output: unsignedOutputFile,
663 Description: "apex (" + apexType.name() + ")",
664 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900665 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900666 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900667 "copy_commands": strings.Join(copyCommands, " && "),
668 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900669 },
670 })
671 }
672
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800673 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Kousik Kumar309b1c02020-05-28 06:13:33 -0700674 rule := java.Signapk
675 args := map[string]string{
676 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
677 "flags": "-a 4096", //alignment
678 }
679 implicits := android.Paths{
680 a.container_certificate_file,
681 a.container_private_key_file,
682 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400683 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700684 rule = java.SignapkRE
685 args["implicits"] = strings.Join(implicits.Strings(), ",")
686 args["outCommaList"] = a.outputFile.String()
687 }
Jiyong Park09d77522019-11-18 11:16:27 +0900688 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700689 Rule: rule,
Jiyong Park09d77522019-11-18 11:16:27 +0900690 Description: "signapk",
691 Output: a.outputFile,
692 Input: unsignedOutputFile,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700693 Implicits: implicits,
694 Args: args,
Jiyong Park09d77522019-11-18 11:16:27 +0900695 })
696
697 // Install to $OUT/soong/{target,host}/.../apex
698 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800699 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900700 }
701 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900702
703 // installed-files.txt is dist'ed
704 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900705}
706
707func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
708 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
709 // reply true to `InstallBypassMake()` (thus making the call
710 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
711 // instead of `android.PathForOutput`) to return the correct path to the flattened
712 // APEX (as its contents is installed by Make, not Soong).
713 factx := flattenedApexContext{ctx}
Jooyung Han7f146c02020-09-23 19:15:55 +0900714 a.outputFile = android.PathForModuleInstall(&factx, "apex", a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900715 a.buildFilesInfo(ctx)
716}
717
718func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900719 if a.container_certificate_file == nil {
720 cert := String(a.properties.Certificate)
721 if cert == "" {
722 pem, key := ctx.Config().DefaultAppCertificate(ctx)
723 a.container_certificate_file = pem
724 a.container_private_key_file = key
725 } else {
726 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
727 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
728 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
729 }
Jiyong Park09d77522019-11-18 11:16:27 +0900730 }
731}
732
733func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
734 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900735 // 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 +0900736 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900737 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900738
739 // rename to apex_pubkey
740 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
741 ctx.Build(pctx, android.BuildParams{
742 Rule: android.Cp,
743 Input: a.public_key_file,
744 Output: copiedPubkey,
745 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900746 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900747
748 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900749 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900750 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900751 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900752 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.Stem(), fi.builtFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900753 for _, sym := range fi.symlinks {
754 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
755 }
756 }
757 }
758 }
759}
Jooyung Han27151d92019-12-16 17:45:32 +0900760
761func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
762 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
763 // to see if it should be overridden because their <apex name> is dynamically generated
764 // according to its VNDK version.
765 if a.vndkApex {
766 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
767 if overridden {
768 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
769 }
770 return ""
771 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700772 if a.overridableProperties.Package_name != "" {
773 return a.overridableProperties.Package_name
774 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900775 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900776 if overridden {
777 return manifestPackageName
778 }
779 return ""
780}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900781
782func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
783 if !a.primaryApexType {
784 return
785 }
786
787 if a.properties.IsCoverageVariant {
788 // Otherwise, we will have duplicated rules for coverage and
789 // non-coverage variants of the same APEX
790 return
791 }
792
793 if ctx.Host() {
794 // No need to generate dependency info for host variant
795 return
796 }
797
Artur Satayev872a1442020-04-27 17:08:37 +0100798 depInfos := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900799 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev872a1442020-04-27 17:08:37 +0100800 if from.Name() == to.Name() {
801 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
802 // As soon as the dependency graph crosses the APEX boundary, don't go further.
803 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900804 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900805
Artur Satayev872a1442020-04-27 17:08:37 +0100806 if info, exists := depInfos[to.Name()]; exists {
807 if !android.InList(from.Name(), info.From) {
808 info.From = append(info.From, from.Name())
809 }
810 info.IsExternal = info.IsExternal && externalDep
811 depInfos[to.Name()] = info
812 } else {
Artur Satayev480e25b2020-04-27 18:53:18 +0100813 toMinSdkVersion := "(no version)"
814 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
815 if v := m.MinSdkVersion(); v != "" {
816 toMinSdkVersion = v
817 }
818 }
819
Artur Satayev872a1442020-04-27 17:08:37 +0100820 depInfos[to.Name()] = android.ApexModuleDepInfo{
Artur Satayev480e25b2020-04-27 18:53:18 +0100821 To: to.Name(),
822 From: []string{from.Name()},
823 IsExternal: externalDep,
824 MinSdkVersion: toMinSdkVersion,
Artur Satayev872a1442020-04-27 17:08:37 +0100825 }
826 }
827
828 // As soon as the dependency graph crosses the APEX boundary, don't go further.
829 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +0900830 })
831
Artur Satayev480e25b2020-04-27 18:53:18 +0100832 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos)
Artur Satayev872a1442020-04-27 17:08:37 +0100833
Jiyong Park83dc74b2020-01-14 18:38:44 +0900834 ctx.Build(pctx, android.BuildParams{
835 Rule: android.Phony,
836 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +0100837 Inputs: []android.Path{
838 a.ApexBundleDepsInfo.FullListPath(),
839 a.ApexBundleDepsInfo.FlatListPath(),
840 },
Jiyong Park83dc74b2020-01-14 18:38:44 +0900841 })
842}
Colin Cross08dca382020-07-21 20:31:17 -0700843
844func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
845 depSetsBuilder := java.NewLintDepSetBuilder()
846 for _, fi := range a.filesInfo {
847 depSetsBuilder.Transitive(fi.lintDepSets)
848 }
849
850 a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
851}