blob: acfb8c57745ca89d135f9206204b092d45bc139c [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
Colin Crosscf371cc2020-11-13 11:48:42 -0800376 android.WriteFileRule(ctx, output, string(j))
Jiyong Parkbd159612020-02-28 15:22:21 +0900377
378 return output.OutputPath
379}
380
Jiyong Park09d77522019-11-18 11:16:27 +0900381func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
382 var abis []string
383 for _, target := range ctx.MultiTargets() {
384 if len(target.Arch.Abi) > 0 {
385 abis = append(abis, target.Arch.Abi[0])
386 }
387 }
388
389 abis = android.FirstUniqueStrings(abis)
390
391 apexType := a.properties.ApexType
392 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900393 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800394 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900395
Jiyong Park7cd10e32020-01-14 09:22:18 +0900396 // TODO(jiyong): construct the copy rules using RuleBuilder
397 var copyCommands []string
398 for _, fi := range a.filesInfo {
399 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700400 destPathDir := filepath.Dir(destPath)
401 if fi.class == appSet {
402 copyCommands = append(copyCommands, "rm -rf "+destPathDir)
403 }
404 copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900405 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
406 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
407 pathOnDevice := filepath.Join("/system", fi.Path())
408 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
409 } else {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700410 if fi.class == appSet {
411 copyCommands = append(copyCommands,
Colin Crossd783bbb2020-07-11 22:30:45 -0700412 fmt.Sprintf("unzip -qDD -d %s %s", destPathDir, fi.builtFile.String()))
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700413 } else {
414 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
415 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900416 implicitInputs = append(implicitInputs, fi.builtFile)
417 }
418 // create additional symlinks pointing the file inside the APEX
419 for _, symlinkPath := range fi.SymlinkPaths() {
420 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000421 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900422 }
Liz Kammer1c14a212020-05-12 15:26:55 -0700423 for _, d := range fi.dataPaths {
424 // 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 -0400425 relPath := d.SrcPath.Rel()
426 dataPath := d.SrcPath.String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700427 if !strings.HasSuffix(dataPath, relPath) {
428 panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
429 }
430
Liz Kammer0a51aa22020-07-21 11:13:17 -0700431 dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
Liz Kammer1c14a212020-05-12 15:26:55 -0700432
Chris Parsons216e10a2020-07-09 17:12:52 -0400433 copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
434 implicitInputs = append(implicitInputs, d.SrcPath)
Liz Kammer1c14a212020-05-12 15:26:55 -0700435 }
Jiyong Park09d77522019-11-18 11:16:27 +0900436 }
437
Jiyong Park7cd10e32020-01-14 09:22:18 +0900438 // TODO(jiyong): use RuleBuilder
439 var emitCommands []string
440 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900441 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
Dan Albertc8060532020-07-22 22:32:17 -0700442 minSdkVersion := a.minSdkVersion(ctx)
443 if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
Jooyung Han214bf372019-11-12 13:03:50 +0900444 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
445 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900446 for _, fi := range a.filesInfo {
447 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900448 }
449 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900450 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900451
Jooyung Han938b5932020-06-20 12:47:47 +0900452 if a.overridableProperties.Allowed_files != nil {
Jiyong Park09d77522019-11-18 11:16:27 +0900453 ctx.Build(pctx, android.BuildParams{
454 Rule: emitApexContentRule,
455 Implicits: implicitInputs,
456 Output: imageContentFile,
457 Description: "emit apex image content",
458 Args: map[string]string{
459 "emit_commands": strings.Join(emitCommands, " && "),
460 },
461 })
462 implicitInputs = append(implicitInputs, imageContentFile)
Jooyung Han938b5932020-06-20 12:47:47 +0900463 allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
Jiyong Park09d77522019-11-18 11:16:27 +0900464
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800465 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900466 ctx.Build(pctx, android.BuildParams{
467 Rule: diffApexContentRule,
468 Implicits: implicitInputs,
469 Output: phonyOutput,
470 Description: "diff apex image content",
471 Args: map[string]string{
Colin Cross440e0d02020-06-11 11:32:11 -0700472 "allowed_files_file": allowedFilesFile.String(),
473 "image_content_file": imageContentFile.String(),
474 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900475 },
476 })
477
478 implicitInputs = append(implicitInputs, phonyOutput)
479 }
480
481 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
482 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
483
Jiyong Park3a1602e2020-01-14 14:39:19 +0900484 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900485 if apexType == imageApex {
486 // files and dirs that will be created in APEX
487 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
488 var executablePaths []string // this also includes dirs
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700489 var extractedAppSetPaths android.Paths
490 var extractedAppSetDirs []string
Jiyong Park09d77522019-11-18 11:16:27 +0900491 for _, f := range a.filesInfo {
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900492 pathInApex := f.Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900493 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
494 executablePaths = append(executablePaths, pathInApex)
Liz Kammer1c14a212020-05-12 15:26:55 -0700495 for _, d := range f.dataPaths {
Liz Kammer0a51aa22020-07-21 11:13:17 -0700496 readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
Liz Kammer1c14a212020-05-12 15:26:55 -0700497 }
Jiyong Park09d77522019-11-18 11:16:27 +0900498 for _, s := range f.symlinks {
499 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
500 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700501 } else if f.class == appSet {
502 extractedAppSetPaths = append(extractedAppSetPaths, f.builtFile)
503 extractedAppSetDirs = append(extractedAppSetDirs, f.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900504 } else {
505 readOnlyPaths = append(readOnlyPaths, pathInApex)
506 }
507 dir := f.installDir
508 for !android.InList(dir, executablePaths) && dir != "" {
509 executablePaths = append(executablePaths, dir)
510 dir, _ = filepath.Split(dir) // move up to the parent
511 if len(dir) > 0 {
512 // remove trailing slash
513 dir = dir[:len(dir)-1]
514 }
515 }
516 }
517 sort.Strings(readOnlyPaths)
518 sort.Strings(executablePaths)
519 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
520 ctx.Build(pctx, android.BuildParams{
521 Rule: generateFsConfig,
522 Output: cannedFsConfig,
523 Description: "generate fs config",
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700524 Inputs: extractedAppSetPaths,
Jiyong Park09d77522019-11-18 11:16:27 +0900525 Args: map[string]string{
526 "ro_paths": strings.Join(readOnlyPaths, " "),
527 "exec_paths": strings.Join(executablePaths, " "),
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700528 "apk_paths": strings.Join(extractedAppSetDirs, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900529 },
530 })
531
Jiyong Park09d77522019-11-18 11:16:27 +0900532 optFlags := []string{}
533
534 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900535 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900536 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
537
Jooyung Han27151d92019-12-16 17:45:32 +0900538 manifestPackageName := a.getOverrideManifestPackageName(ctx)
539 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900540 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
541 }
542
543 if a.properties.AndroidManifest != nil {
544 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
545 implicitInputs = append(implicitInputs, androidManifestFile)
546 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
547 }
548
Dan Albertc8060532020-07-22 22:32:17 -0700549 moduleMinSdkVersion := a.minSdkVersion(ctx)
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100550 minSdkVersion := moduleMinSdkVersion.String()
551
552 // bundletool doesn't understand what "current" is. We need to transform it to codename
553 if moduleMinSdkVersion.IsCurrent() {
554 minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000555 }
Nikita Ioffe5335bc42020-10-20 00:02:15 +0100556 // apex module doesn't have a concept of target_sdk_version, hence for the time being
557 // targetSdkVersion == default targetSdkVersion of the branch.
558 targetSdkVersion := strconv.Itoa(ctx.Config().DefaultAppTargetSdk(ctx).FinalOrFutureInt())
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000559
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000560 if java.UseApiFingerprint(ctx) {
561 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000562 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
563 }
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000564 if java.UseApiFingerprint(ctx) {
565 minSdkVersion = 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))
Jiyong Park09d77522019-11-18 11:16:27 +0900567 }
568 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000569 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900570
Baligh Uddin004d7172020-02-19 21:29:28 -0800571 if a.overridableProperties.Logging_parent != "" {
572 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
573 }
574
Jiyong Park19972c72020-01-28 20:05:29 +0900575 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
576 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900577 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900578 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
579 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900580 }
581
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000582 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000583 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
584 return
585 }
Dan Albertc8060532020-07-22 22:32:17 -0700586 if moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900587 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
588 // don't need hashtree for activation. Therefore, by removing hashtree from
589 // apex bundle (filesystem image in it, to be specific), we can save storage.
590 optFlags = append(optFlags, "--no_hashtree")
591 }
592
Dario Frenica913392020-04-27 18:21:11 +0100593 if a.testOnlyShouldSkipPayloadSign() {
594 optFlags = append(optFlags, "--unsigned_payload")
595 }
596
Jiyong Park09d77522019-11-18 11:16:27 +0900597 if a.properties.Apex_name != nil {
598 // If apex_name is set, apexer can skip checking if key name matches with apex name.
599 // Note that apex_manifest is also mended.
600 optFlags = append(optFlags, "--do_not_check_keyname")
601 }
602
Dan Albertc8060532020-07-22 22:32:17 -0700603 if moduleMinSdkVersion == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900604 implicitInputs = append(implicitInputs, a.manifestJsonOut)
605 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
606 }
607
Theotime Combes4ba38c12020-06-12 12:46:59 +0000608 optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
609
Jiyong Park09d77522019-11-18 11:16:27 +0900610 ctx.Build(pctx, android.BuildParams{
611 Rule: apexRule,
612 Implicits: implicitInputs,
613 Output: unsignedOutputFile,
614 Description: "apex (" + apexType.name() + ")",
615 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900616 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900617 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900618 "copy_commands": strings.Join(copyCommands, " && "),
619 "manifest": a.manifestPbOut.String(),
620 "file_contexts": a.fileContexts.String(),
621 "canned_fs_config": cannedFsConfig.String(),
622 "key": a.private_key_file.String(),
623 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900624 },
625 })
626
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800627 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
628 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900629 a.bundleModuleFile = bundleModuleFile
630
631 ctx.Build(pctx, android.BuildParams{
632 Rule: apexProtoConvertRule,
633 Input: unsignedOutputFile,
634 Output: apexProtoFile,
635 Description: "apex proto convert",
636 })
637
Jiyong Parkbd159612020-02-28 15:22:21 +0900638 bundleConfig := a.buildBundleConfig(ctx)
639
Jiyong Park09d77522019-11-18 11:16:27 +0900640 ctx.Build(pctx, android.BuildParams{
641 Rule: apexBundleRule,
642 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900643 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900644 Output: a.bundleModuleFile,
645 Description: "apex bundle module",
646 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900647 "abi": strings.Join(abis, "."),
648 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900649 },
650 })
651 } else {
652 ctx.Build(pctx, android.BuildParams{
653 Rule: zipApexRule,
654 Implicits: implicitInputs,
655 Output: unsignedOutputFile,
656 Description: "apex (" + apexType.name() + ")",
657 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900658 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900659 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900660 "copy_commands": strings.Join(copyCommands, " && "),
661 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900662 },
663 })
664 }
665
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800666 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Kousik Kumar309b1c02020-05-28 06:13:33 -0700667 rule := java.Signapk
668 args := map[string]string{
669 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
670 "flags": "-a 4096", //alignment
671 }
672 implicits := android.Paths{
673 a.container_certificate_file,
674 a.container_private_key_file,
675 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400676 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700677 rule = java.SignapkRE
678 args["implicits"] = strings.Join(implicits.Strings(), ",")
679 args["outCommaList"] = a.outputFile.String()
680 }
Jiyong Park09d77522019-11-18 11:16:27 +0900681 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700682 Rule: rule,
Jiyong Park09d77522019-11-18 11:16:27 +0900683 Description: "signapk",
684 Output: a.outputFile,
685 Input: unsignedOutputFile,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700686 Implicits: implicits,
687 Args: args,
Jiyong Park09d77522019-11-18 11:16:27 +0900688 })
689
690 // Install to $OUT/soong/{target,host}/.../apex
691 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800692 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900693 }
694 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900695
696 // installed-files.txt is dist'ed
697 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900698}
699
700func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
701 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
702 // reply true to `InstallBypassMake()` (thus making the call
703 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
704 // instead of `android.PathForOutput`) to return the correct path to the flattened
705 // APEX (as its contents is installed by Make, not Soong).
706 factx := flattenedApexContext{ctx}
Jooyung Han7f146c02020-09-23 19:15:55 +0900707 a.outputFile = android.PathForModuleInstall(&factx, "apex", a.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900708 a.buildFilesInfo(ctx)
709}
710
711func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900712 if a.container_certificate_file == nil {
713 cert := String(a.properties.Certificate)
714 if cert == "" {
715 pem, key := ctx.Config().DefaultAppCertificate(ctx)
716 a.container_certificate_file = pem
717 a.container_private_key_file = key
718 } else {
719 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
720 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
721 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
722 }
Jiyong Park09d77522019-11-18 11:16:27 +0900723 }
724}
725
726func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
727 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900728 // 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 +0900729 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900730 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900731
732 // rename to apex_pubkey
733 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
734 ctx.Build(pctx, android.BuildParams{
735 Rule: android.Cp,
736 Input: a.public_key_file,
737 Output: copiedPubkey,
738 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900739 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900740
741 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900742 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900743 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900744 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900745 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.Stem(), fi.builtFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900746 for _, sym := range fi.symlinks {
747 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
748 }
749 }
750 }
751 }
752}
Jooyung Han27151d92019-12-16 17:45:32 +0900753
754func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
755 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
756 // to see if it should be overridden because their <apex name> is dynamically generated
757 // according to its VNDK version.
758 if a.vndkApex {
759 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
760 if overridden {
761 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
762 }
763 return ""
764 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700765 if a.overridableProperties.Package_name != "" {
766 return a.overridableProperties.Package_name
767 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900768 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900769 if overridden {
770 return manifestPackageName
771 }
772 return ""
773}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900774
775func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
776 if !a.primaryApexType {
777 return
778 }
779
780 if a.properties.IsCoverageVariant {
781 // Otherwise, we will have duplicated rules for coverage and
782 // non-coverage variants of the same APEX
783 return
784 }
785
786 if ctx.Host() {
787 // No need to generate dependency info for host variant
788 return
789 }
790
Artur Satayev872a1442020-04-27 17:08:37 +0100791 depInfos := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900792 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev872a1442020-04-27 17:08:37 +0100793 if from.Name() == to.Name() {
794 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
795 // As soon as the dependency graph crosses the APEX boundary, don't go further.
796 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900797 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900798
Artur Satayev872a1442020-04-27 17:08:37 +0100799 if info, exists := depInfos[to.Name()]; exists {
800 if !android.InList(from.Name(), info.From) {
801 info.From = append(info.From, from.Name())
802 }
803 info.IsExternal = info.IsExternal && externalDep
804 depInfos[to.Name()] = info
805 } else {
Artur Satayev480e25b2020-04-27 18:53:18 +0100806 toMinSdkVersion := "(no version)"
807 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
808 if v := m.MinSdkVersion(); v != "" {
809 toMinSdkVersion = v
810 }
811 }
812
Artur Satayev872a1442020-04-27 17:08:37 +0100813 depInfos[to.Name()] = android.ApexModuleDepInfo{
Artur Satayev480e25b2020-04-27 18:53:18 +0100814 To: to.Name(),
815 From: []string{from.Name()},
816 IsExternal: externalDep,
817 MinSdkVersion: toMinSdkVersion,
Artur Satayev872a1442020-04-27 17:08:37 +0100818 }
819 }
820
821 // As soon as the dependency graph crosses the APEX boundary, don't go further.
822 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +0900823 })
824
Artur Satayev480e25b2020-04-27 18:53:18 +0100825 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos)
Artur Satayev872a1442020-04-27 17:08:37 +0100826
Jiyong Park83dc74b2020-01-14 18:38:44 +0900827 ctx.Build(pctx, android.BuildParams{
828 Rule: android.Phony,
829 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +0100830 Inputs: []android.Path{
831 a.ApexBundleDepsInfo.FullListPath(),
832 a.ApexBundleDepsInfo.FlatListPath(),
833 },
Jiyong Park83dc74b2020-01-14 18:38:44 +0900834 })
835}
Colin Cross08dca382020-07-21 20:31:17 -0700836
837func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
838 depSetsBuilder := java.NewLintDepSetBuilder()
839 for _, fi := range a.filesInfo {
840 depSetsBuilder.Transitive(fi.lintDepSets)
841 }
842
843 a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
844}