blob: 2e1cbfc1f1b3f9f2710ab4bc568d3d79dd38d9be [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkbd159612020-02-28 15:22:21 +090018 "encoding/json"
Jiyong Park09d77522019-11-18 11:16:27 +090019 "fmt"
20 "path/filepath"
21 "runtime"
22 "sort"
Jooyung Han5417f772020-03-12 18:37:20 +090023 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090024 "strings"
25
26 "android/soong/android"
27 "android/soong/java"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
33var (
34 pctx = android.NewPackageContext("android/apex")
35)
36
37func init() {
38 pctx.Import("android/soong/android")
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")
64}
65
66var (
67 // Create a canned fs config file where all files and directories are
68 // by default set to (uid/gid/mode) = (1000/1000/0644)
69 // TODO(b/113082813) make this configurable using config.fs syntax
70 generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{
71 Command: `echo '/ 1000 1000 0755' > ${out} && ` +
72 `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
73 `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
74 Description: "fs_config ${out}",
75 }, "ro_paths", "exec_paths")
76
77 apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
78 Command: `rm -f $out && ${jsonmodify} $in ` +
79 `-a provideNativeLibs ${provideNativeLibs} ` +
80 `-a requireNativeLibs ${requireNativeLibs} ` +
81 `${opt} ` +
82 `-o $out`,
83 CommandDeps: []string{"${jsonmodify}"},
84 Description: "prepare ${out}",
85 }, "provideNativeLibs", "requireNativeLibs", "opt")
86
87 stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
88 Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
89 CommandDeps: []string{"${conv_apex_manifest}"},
90 Description: "strip ${in}=>${out}",
91 })
92
93 pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
94 Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
95 CommandDeps: []string{"${conv_apex_manifest}"},
96 Description: "convert ${in}=>${out}",
97 })
98
99 // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate
100 // against the binary policy using sefcontext_compiler -p <policy>.
101
102 // TODO(b/114327326): automate the generation of file_contexts
103 apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
104 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
105 `(. ${out}.copy_commands) && ` +
106 `APEXER_TOOL_PATH=${tool_path} ` +
107 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900108 `--file_contexts ${file_contexts} ` +
109 `--canned_fs_config ${canned_fs_config} ` +
Dario Freni0f4ae072020-01-02 15:24:12 +0000110 `--include_build_info ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900111 `--payload_type image ` +
112 `--key ${key} ${opt_flags} ${image_dir} ${out} `,
113 CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
114 "${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
115 "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
116 Rspfile: "${out}.copy_commands",
117 RspfileContent: "${copy_commands}",
118 Description: "APEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900119 }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900120
121 zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
122 Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
123 `(. ${out}.copy_commands) && ` +
124 `APEXER_TOOL_PATH=${tool_path} ` +
Jooyung Han214bf372019-11-12 13:03:50 +0900125 `${apexer} --force --manifest ${manifest} ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900126 `--payload_type zip ` +
127 `${image_dir} ${out} `,
128 CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
129 Rspfile: "${out}.copy_commands",
130 RspfileContent: "${copy_commands}",
131 Description: "ZipAPEX ${image_dir} => ${out}",
Jooyung Han214bf372019-11-12 13:03:50 +0900132 }, "tool_path", "image_dir", "copy_commands", "manifest")
Jiyong Park09d77522019-11-18 11:16:27 +0900133
134 apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
135 blueprint.RuleParams{
136 Command: `${aapt2} convert --output-format proto $in -o $out`,
137 CommandDeps: []string{"${aapt2}"},
138 })
139
140 apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
Jiyong Parkbd159612020-02-28 15:22:21 +0900141 Command: `${zip2zip} -i $in -o $out.base ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900142 `apex_payload.img:apex/${abi}.img ` +
Dario Frenida1aefe2020-03-02 21:47:09 +0000143 `apex_build_info.pb:apex/${abi}.build_info.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900144 `apex_manifest.json:root/apex_manifest.json ` +
Jiyong Park53ae3342019-12-08 02:06:24 +0900145 `apex_manifest.pb:root/apex_manifest.pb ` +
Jiyong Park09d77522019-11-18 11:16:27 +0900146 `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
Jiyong Parkbd159612020-02-28 15:22:21 +0900147 `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` +
148 `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` +
149 `${merge_zips} $out $out.base $out.config`,
150 CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"},
Jiyong Park09d77522019-11-18 11:16:27 +0900151 Description: "app bundle",
Jiyong Parkbd159612020-02-28 15:22:21 +0900152 }, "abi", "config")
Jiyong Park09d77522019-11-18 11:16:27 +0900153
154 emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{
155 Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`,
156 Rspfile: "${out}.emit_commands",
157 RspfileContent: "${emit_commands}",
158 Description: "Emit APEX image content",
159 }, "emit_commands")
160
161 diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
162 Command: `diff --unchanged-group-format='' \` +
163 `--changed-group-format='%<' \` +
164 `${image_content_file} ${whitelisted_files_file} || (` +
165 `echo -e "New unexpected files were added to ${apex_module_name}." ` +
166 ` "To fix the build run following command:" && ` +
167 `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` +
Dan Willemsen81e43c52020-01-28 15:40:19 -0800168 `exit 1); touch ${out}`,
Jiyong Park09d77522019-11-18 11:16:27 +0900169 Description: "Diff ${image_content_file} and ${whitelisted_files_file}",
170 }, "image_content_file", "whitelisted_files_file", "apex_module_name")
171)
172
173func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
174 manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
175
Jooyung Han214bf372019-11-12 13:03:50 +0900176 manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
Jiyong Park09d77522019-11-18 11:16:27 +0900177
178 // put dependency({provide|require}NativeLibs) in apex_manifest.json
179 provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
180 requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
181
182 // apex name can be overridden
183 optCommands := []string{}
184 if a.properties.Apex_name != nil {
185 optCommands = append(optCommands, "-v name "+*a.properties.Apex_name)
186 }
187
Jooyung Han643adc42020-02-27 13:50:06 +0900188 // collect jniLibs. Notice that a.filesInfo is already sorted
189 var jniLibs []string
190 for _, fi := range a.filesInfo {
191 if fi.isJniLib {
192 jniLibs = append(jniLibs, fi.builtFile.Base())
193 }
194 }
195 if len(jniLibs) > 0 {
196 optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
197 }
198
Jiyong Park09d77522019-11-18 11:16:27 +0900199 ctx.Build(pctx, android.BuildParams{
200 Rule: apexManifestRule,
201 Input: manifestSrc,
Jooyung Han214bf372019-11-12 13:03:50 +0900202 Output: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900203 Args: map[string]string{
204 "provideNativeLibs": strings.Join(provideNativeLibs, " "),
205 "requireNativeLibs": strings.Join(requireNativeLibs, " "),
206 "opt": strings.Join(optCommands, " "),
207 },
208 })
209
Jooyung Han5417f772020-03-12 18:37:20 +0900210 if a.minSdkVersion(ctx) == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900211 // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json
212 // prepare stripped-down version so that APEX modules built from R+ can be installed to Q
213 a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
214 ctx.Build(pctx, android.BuildParams{
215 Rule: stripApexManifestRule,
216 Input: manifestJsonFullOut,
217 Output: a.manifestJsonOut,
218 })
219 }
Jiyong Park09d77522019-11-18 11:16:27 +0900220
221 // from R+, protobuf binary format (.pb) is the standard format for apex_manifest
222 a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
223 ctx.Build(pctx, android.BuildParams{
224 Rule: pbApexManifestRule,
Jooyung Han214bf372019-11-12 13:03:50 +0900225 Input: manifestJsonFullOut,
Jiyong Park09d77522019-11-18 11:16:27 +0900226 Output: a.manifestPbOut,
227 })
228}
229
Jiyong Park19972c72020-01-28 20:05:29 +0900230func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900231 var noticeFiles android.Paths
232
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100233 a.walkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Jiyong Park9918e1a2020-03-17 19:16:40 +0900234 if externalDep {
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100235 // As soon as the dependency graph crosses the APEX boundary, don't go further.
236 return false
Jiyong Park09d77522019-11-18 11:16:27 +0900237 }
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100238
Jiyong Park9918e1a2020-03-17 19:16:40 +0900239 notices := to.NoticeFiles()
240 noticeFiles = append(noticeFiles, notices...)
Paul Duffinbe5a5be2020-03-30 15:54:08 +0100241
242 return true
Jiyong Park9918e1a2020-03-17 19:16:40 +0900243 })
Jiyong Park09d77522019-11-18 11:16:27 +0900244
245 if len(noticeFiles) == 0 {
Jiyong Park19972c72020-01-28 20:05:29 +0900246 return android.NoticeOutputs{}
Jiyong Park09d77522019-11-18 11:16:27 +0900247 }
248
Jiyong Park19972c72020-01-28 20:05:29 +0900249 return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles))
Jiyong Park09d77522019-11-18 11:16:27 +0900250}
251
Jiyong Park3a1602e2020-01-14 14:39:19 +0900252func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
253 output := android.PathForModuleOut(ctx, "installed-files.txt")
254 rule := android.NewRuleBuilder()
255 rule.Command().
256 Implicit(builtApex).
257 Text("(cd " + imageDir.String() + " ; ").
Jiyong Parkbd63a102020-02-08 12:40:05 +0900258 Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
Jiyong Park3a1602e2020-01-14 14:39:19 +0900259 Text(" | sort -nr > ").
260 Output(output)
261 rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files")
262 return output.OutputPath
263}
264
Jiyong Parkbd159612020-02-28 15:22:21 +0900265func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
266 output := android.PathForModuleOut(ctx, "bundle_config.json")
267
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900268 type ApkConfig struct {
269 Package_name string `json:"package_name"`
270 Apk_path string `json:"path"`
271 }
Jiyong Parkbd159612020-02-28 15:22:21 +0900272 config := struct {
273 Compression struct {
274 Uncompressed_glob []string `json:"uncompressed_glob"`
275 } `json:"compression"`
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900276 Apex_config struct {
277 Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
278 } `json:"apex_config,omitempty"`
Jiyong Parkbd159612020-02-28 15:22:21 +0900279 }{}
280
281 config.Compression.Uncompressed_glob = []string{
282 "apex_payload.img",
283 "apex_manifest.*",
284 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900285
286 // collect the manifest names and paths of android apps
287 // if their manifest names are overridden
288 for _, fi := range a.filesInfo {
289 if fi.class != app {
290 continue
291 }
292 packageName := fi.overriddenPackageName
293 if packageName != "" {
294 config.Apex_config.Apex_embedded_apk_config = append(
295 config.Apex_config.Apex_embedded_apk_config,
296 ApkConfig{
297 Package_name: packageName,
298 Apk_path: fi.Path(),
299 })
300 }
301 }
302
Jiyong Parkbd159612020-02-28 15:22:21 +0900303 j, err := json.Marshal(config)
304 if err != nil {
305 panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
306 }
307
308 ctx.Build(pctx, android.BuildParams{
309 Rule: android.WriteFile,
310 Output: output,
311 Description: "Bundle Config " + output.String(),
312 Args: map[string]string{
313 "content": string(j),
314 },
315 })
316
317 return output.OutputPath
318}
319
Jiyong Park09d77522019-11-18 11:16:27 +0900320func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
321 var abis []string
322 for _, target := range ctx.MultiTargets() {
323 if len(target.Arch.Abi) > 0 {
324 abis = append(abis, target.Arch.Abi[0])
325 }
326 }
327
328 abis = android.FirstUniqueStrings(abis)
329
330 apexType := a.properties.ApexType
331 suffix := apexType.suffix()
Jiyong Park7cd10e32020-01-14 09:22:18 +0900332 var implicitInputs []android.Path
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800333 unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
Jiyong Park09d77522019-11-18 11:16:27 +0900334
Jiyong Park7cd10e32020-01-14 09:22:18 +0900335 // TODO(jiyong): construct the copy rules using RuleBuilder
336 var copyCommands []string
337 for _, fi := range a.filesInfo {
338 destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String()
339 copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(destPath))
340 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
341 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
342 pathOnDevice := filepath.Join("/system", fi.Path())
343 copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
344 } else {
345 copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
346 implicitInputs = append(implicitInputs, fi.builtFile)
347 }
348 // create additional symlinks pointing the file inside the APEX
349 for _, symlinkPath := range fi.SymlinkPaths() {
350 symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String()
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000351 copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900352 }
Jiyong Park09d77522019-11-18 11:16:27 +0900353 }
354
Jiyong Park7cd10e32020-01-14 09:22:18 +0900355 // TODO(jiyong): use RuleBuilder
356 var emitCommands []string
357 imageContentFile := android.PathForModuleOut(ctx, "content.txt")
Jooyung Han214bf372019-11-12 13:03:50 +0900358 emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String())
Jooyung Han5417f772020-03-12 18:37:20 +0900359 if a.minSdkVersion(ctx) == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900360 emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String())
361 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900362 for _, fi := range a.filesInfo {
363 emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900364 }
365 emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String())
Jooyung Han214bf372019-11-12 13:03:50 +0900366 implicitInputs = append(implicitInputs, a.manifestPbOut)
Jiyong Park09d77522019-11-18 11:16:27 +0900367
368 if a.properties.Whitelisted_files != nil {
369 ctx.Build(pctx, android.BuildParams{
370 Rule: emitApexContentRule,
371 Implicits: implicitInputs,
372 Output: imageContentFile,
373 Description: "emit apex image content",
374 Args: map[string]string{
375 "emit_commands": strings.Join(emitCommands, " && "),
376 },
377 })
378 implicitInputs = append(implicitInputs, imageContentFile)
379 whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files))
380
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800381 phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
Jiyong Park09d77522019-11-18 11:16:27 +0900382 ctx.Build(pctx, android.BuildParams{
383 Rule: diffApexContentRule,
384 Implicits: implicitInputs,
385 Output: phonyOutput,
386 Description: "diff apex image content",
387 Args: map[string]string{
388 "whitelisted_files_file": whitelistedFilesFile.String(),
389 "image_content_file": imageContentFile.String(),
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800390 "apex_module_name": a.Name(),
Jiyong Park09d77522019-11-18 11:16:27 +0900391 },
392 })
393
394 implicitInputs = append(implicitInputs, phonyOutput)
395 }
396
397 outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
398 prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
399
Jiyong Park3a1602e2020-01-14 14:39:19 +0900400 imageDir := android.PathForModuleOut(ctx, "image"+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900401 if apexType == imageApex {
402 // files and dirs that will be created in APEX
403 var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
404 var executablePaths []string // this also includes dirs
405 for _, f := range a.filesInfo {
406 pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
407 if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
408 executablePaths = append(executablePaths, pathInApex)
409 for _, s := range f.symlinks {
410 executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
411 }
412 } else {
413 readOnlyPaths = append(readOnlyPaths, pathInApex)
414 }
415 dir := f.installDir
416 for !android.InList(dir, executablePaths) && dir != "" {
417 executablePaths = append(executablePaths, dir)
418 dir, _ = filepath.Split(dir) // move up to the parent
419 if len(dir) > 0 {
420 // remove trailing slash
421 dir = dir[:len(dir)-1]
422 }
423 }
424 }
425 sort.Strings(readOnlyPaths)
426 sort.Strings(executablePaths)
427 cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
428 ctx.Build(pctx, android.BuildParams{
429 Rule: generateFsConfig,
430 Output: cannedFsConfig,
431 Description: "generate fs config",
432 Args: map[string]string{
433 "ro_paths": strings.Join(readOnlyPaths, " "),
434 "exec_paths": strings.Join(executablePaths, " "),
435 },
436 })
437
Jiyong Park09d77522019-11-18 11:16:27 +0900438 optFlags := []string{}
439
440 // Additional implicit inputs.
Jooyung Han54aca7b2019-11-20 02:26:02 +0900441 implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file)
Jiyong Park09d77522019-11-18 11:16:27 +0900442 optFlags = append(optFlags, "--pubkey "+a.public_key_file.String())
443
Jooyung Han27151d92019-12-16 17:45:32 +0900444 manifestPackageName := a.getOverrideManifestPackageName(ctx)
445 if manifestPackageName != "" {
Jiyong Park09d77522019-11-18 11:16:27 +0900446 optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
447 }
448
449 if a.properties.AndroidManifest != nil {
450 androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
451 implicitInputs = append(implicitInputs, androidManifestFile)
452 optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
453 }
454
455 targetSdkVersion := ctx.Config().DefaultAppTargetSdk()
Baligh Uddinf6201372020-01-24 23:15:44 +0000456 minSdkVersion := ctx.Config().DefaultAppTargetSdk()
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000457
Jooyung Han5417f772020-03-12 18:37:20 +0900458 if a.minSdkVersion(ctx) == android.SdkVersion_Android10 {
459 minSdkVersion = strconv.Itoa(a.minSdkVersion(ctx))
460 targetSdkVersion = strconv.Itoa(a.minSdkVersion(ctx))
Nikita Ioffe5d600c92020-02-20 00:43:27 +0000461 }
462
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000463 if java.UseApiFingerprint(ctx) {
464 targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000465 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
466 }
Nikita Ioffe1f4f3452020-03-02 16:58:11 +0000467 if java.UseApiFingerprint(ctx) {
468 minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
Baligh Uddinf6201372020-01-24 23:15:44 +0000469 implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
Jiyong Park09d77522019-11-18 11:16:27 +0900470 }
471 optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
Baligh Uddinf6201372020-01-24 23:15:44 +0000472 optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
Jiyong Park09d77522019-11-18 11:16:27 +0900473
Baligh Uddin004d7172020-02-19 21:29:28 -0800474 if a.overridableProperties.Logging_parent != "" {
475 optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
476 }
477
Jiyong Park19972c72020-01-28 20:05:29 +0900478 a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix)
479 if a.mergedNotices.HtmlGzOutput.Valid() {
Jiyong Park09d77522019-11-18 11:16:27 +0900480 // If there's a NOTICE file, embed it as an asset file in the APEX.
Jiyong Park19972c72020-01-28 20:05:29 +0900481 implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path())
482 optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String()))
Jiyong Park09d77522019-11-18 11:16:27 +0900483 }
484
Nikita Ioffeb4b44c02020-01-02 23:01:39 +0000485 if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() {
Nikita Ioffec72b5dd2019-12-07 17:30:22 +0000486 ctx.PropertyErrorf("test_only_no_hashtree", "not available")
487 return
488 }
Jooyung Han5417f772020-03-12 18:37:20 +0900489 if a.minSdkVersion(ctx) > android.SdkVersion_Android10 || a.testOnlyShouldSkipHashtreeGeneration() {
Jiyong Park09d77522019-11-18 11:16:27 +0900490 // Apexes which are supposed to be installed in builtin dirs(/system, etc)
491 // don't need hashtree for activation. Therefore, by removing hashtree from
492 // apex bundle (filesystem image in it, to be specific), we can save storage.
493 optFlags = append(optFlags, "--no_hashtree")
494 }
495
Dario Frenica913392020-04-27 18:21:11 +0100496 if a.testOnlyShouldSkipPayloadSign() {
497 optFlags = append(optFlags, "--unsigned_payload")
498 }
499
Jiyong Park09d77522019-11-18 11:16:27 +0900500 if a.properties.Apex_name != nil {
501 // If apex_name is set, apexer can skip checking if key name matches with apex name.
502 // Note that apex_manifest is also mended.
503 optFlags = append(optFlags, "--do_not_check_keyname")
504 }
505
Jooyung Han5417f772020-03-12 18:37:20 +0900506 if a.minSdkVersion(ctx) == android.SdkVersion_Android10 {
Jooyung Han214bf372019-11-12 13:03:50 +0900507 implicitInputs = append(implicitInputs, a.manifestJsonOut)
508 optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
509 }
510
Jiyong Park09d77522019-11-18 11:16:27 +0900511 ctx.Build(pctx, android.BuildParams{
512 Rule: apexRule,
513 Implicits: implicitInputs,
514 Output: unsignedOutputFile,
515 Description: "apex (" + apexType.name() + ")",
516 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900517 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900518 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900519 "copy_commands": strings.Join(copyCommands, " && "),
520 "manifest": a.manifestPbOut.String(),
521 "file_contexts": a.fileContexts.String(),
522 "canned_fs_config": cannedFsConfig.String(),
523 "key": a.private_key_file.String(),
524 "opt_flags": strings.Join(optFlags, " "),
Jiyong Park09d77522019-11-18 11:16:27 +0900525 },
526 })
527
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800528 apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
529 bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
Jiyong Park09d77522019-11-18 11:16:27 +0900530 a.bundleModuleFile = bundleModuleFile
531
532 ctx.Build(pctx, android.BuildParams{
533 Rule: apexProtoConvertRule,
534 Input: unsignedOutputFile,
535 Output: apexProtoFile,
536 Description: "apex proto convert",
537 })
538
Jiyong Parkbd159612020-02-28 15:22:21 +0900539 bundleConfig := a.buildBundleConfig(ctx)
540
Jiyong Park09d77522019-11-18 11:16:27 +0900541 ctx.Build(pctx, android.BuildParams{
542 Rule: apexBundleRule,
543 Input: apexProtoFile,
Jiyong Parkbd159612020-02-28 15:22:21 +0900544 Implicit: bundleConfig,
Jiyong Park09d77522019-11-18 11:16:27 +0900545 Output: a.bundleModuleFile,
546 Description: "apex bundle module",
547 Args: map[string]string{
Jiyong Parkbd159612020-02-28 15:22:21 +0900548 "abi": strings.Join(abis, "."),
549 "config": bundleConfig.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900550 },
551 })
552 } else {
553 ctx.Build(pctx, android.BuildParams{
554 Rule: zipApexRule,
555 Implicits: implicitInputs,
556 Output: unsignedOutputFile,
557 Description: "apex (" + apexType.name() + ")",
558 Args: map[string]string{
Jooyung Han214bf372019-11-12 13:03:50 +0900559 "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
Jiyong Park3a1602e2020-01-14 14:39:19 +0900560 "image_dir": imageDir.String(),
Jooyung Han214bf372019-11-12 13:03:50 +0900561 "copy_commands": strings.Join(copyCommands, " && "),
562 "manifest": a.manifestPbOut.String(),
Jiyong Park09d77522019-11-18 11:16:27 +0900563 },
564 })
565 }
566
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800567 a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix)
Jiyong Park09d77522019-11-18 11:16:27 +0900568 ctx.Build(pctx, android.BuildParams{
569 Rule: java.Signapk,
570 Description: "signapk",
571 Output: a.outputFile,
572 Input: unsignedOutputFile,
573 Implicits: []android.Path{
574 a.container_certificate_file,
575 a.container_private_key_file,
576 },
577 Args: map[string]string{
578 "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(),
579 "flags": "-a 4096", //alignment
580 },
581 })
582
583 // Install to $OUT/soong/{target,host}/.../apex
584 if a.installable() {
Jaewoong Jung1670ca02019-11-22 14:50:42 -0800585 ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile)
Jiyong Park09d77522019-11-18 11:16:27 +0900586 }
587 a.buildFilesInfo(ctx)
Jiyong Park3a1602e2020-01-14 14:39:19 +0900588
589 // installed-files.txt is dist'ed
590 a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900591}
592
593func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
594 // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it
595 // reply true to `InstallBypassMake()` (thus making the call
596 // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir`
597 // instead of `android.PathForOutput`) to return the correct path to the flattened
598 // APEX (as its contents is installed by Make, not Soong).
599 factx := flattenedApexContext{ctx}
Jiyong Parka5948012020-02-07 10:15:14 +0900600 apexBundleName := a.Name()
601 a.outputFile = android.PathForModuleInstall(&factx, "apex", apexBundleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900602
Jiyong Park317645e2019-12-05 13:20:58 +0900603 if a.installable() && a.GetOverriddenBy() == "" {
Jiyong Parka5948012020-02-07 10:15:14 +0900604 installPath := android.PathForModuleInstall(ctx, "apex", apexBundleName)
Jooyung Han54aca7b2019-11-20 02:26:02 +0900605 devicePath := android.InstallPathToOnDevicePath(ctx, installPath)
Jiyong Parka5948012020-02-07 10:15:14 +0900606 addFlattenedFileContextsInfos(ctx, apexBundleName+":"+devicePath+":"+a.fileContexts.String())
Jooyung Han54aca7b2019-11-20 02:26:02 +0900607 }
Jiyong Park09d77522019-11-18 11:16:27 +0900608 a.buildFilesInfo(ctx)
609}
610
611func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) {
Jooyung Hanf121a652019-12-17 14:30:11 +0900612 if a.container_certificate_file == nil {
613 cert := String(a.properties.Certificate)
614 if cert == "" {
615 pem, key := ctx.Config().DefaultAppCertificate(ctx)
616 a.container_certificate_file = pem
617 a.container_private_key_file = key
618 } else {
619 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
620 a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem")
621 a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8")
622 }
Jiyong Park09d77522019-11-18 11:16:27 +0900623 }
624}
625
626func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) {
627 if a.installable() {
Jooyung Han214bf372019-11-12 13:03:50 +0900628 // 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 +0900629 // with other ordinary files.
Jiyong Park7cd10e32020-01-14 09:22:18 +0900630 a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900631
632 // rename to apex_pubkey
633 copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey")
634 ctx.Build(pctx, android.BuildParams{
635 Rule: android.Cp,
636 Input: a.public_key_file,
637 Output: copiedPubkey,
638 })
Jiyong Park7cd10e32020-01-14 09:22:18 +0900639 a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil))
Jiyong Park09d77522019-11-18 11:16:27 +0900640
641 if a.properties.ApexType == flattenedApex {
Jiyong Parka5948012020-02-07 10:15:14 +0900642 apexBundleName := a.Name()
Jiyong Park09d77522019-11-18 11:16:27 +0900643 for _, fi := range a.filesInfo {
Jiyong Parka5948012020-02-07 10:15:14 +0900644 dir := filepath.Join("apex", apexBundleName, fi.installDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900645 target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
646 for _, sym := range fi.symlinks {
647 ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
648 }
649 }
650 }
651 }
652}
Jooyung Han27151d92019-12-16 17:45:32 +0900653
654func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
655 // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
656 // to see if it should be overridden because their <apex name> is dynamically generated
657 // according to its VNDK version.
658 if a.vndkApex {
659 overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
660 if overridden {
661 return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1)
662 }
663 return ""
664 }
Baligh Uddin5b57dba2020-03-15 13:01:05 -0700665 if a.overridableProperties.Package_name != "" {
666 return a.overridableProperties.Package_name
667 }
Jiyong Park20bacab2020-03-03 11:45:41 +0900668 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jooyung Han27151d92019-12-16 17:45:32 +0900669 if overridden {
670 return manifestPackageName
671 }
672 return ""
673}
Jiyong Park83dc74b2020-01-14 18:38:44 +0900674
675func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
676 if !a.primaryApexType {
677 return
678 }
679
680 if a.properties.IsCoverageVariant {
681 // Otherwise, we will have duplicated rules for coverage and
682 // non-coverage variants of the same APEX
683 return
684 }
685
686 if ctx.Host() {
687 // No need to generate dependency info for host variant
688 return
689 }
690
Artur Satayev872a1442020-04-27 17:08:37 +0100691 depInfos := android.DepNameToDepInfoMap{}
692 a.walkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
693 if from.Name() == to.Name() {
694 // This can happen for cc.reuseObjTag. We are not interested in tracking this.
695 // As soon as the dependency graph crosses the APEX boundary, don't go further.
696 return !externalDep
Jiyong Park678c8812020-02-07 17:25:49 +0900697 }
Jiyong Park83dc74b2020-01-14 18:38:44 +0900698
Artur Satayev872a1442020-04-27 17:08:37 +0100699 if info, exists := depInfos[to.Name()]; exists {
700 if !android.InList(from.Name(), info.From) {
701 info.From = append(info.From, from.Name())
702 }
703 info.IsExternal = info.IsExternal && externalDep
704 depInfos[to.Name()] = info
705 } else {
706 depInfos[to.Name()] = android.ApexModuleDepInfo{
707 To: to.Name(),
708 From: []string{from.Name()},
709 IsExternal: externalDep,
710 }
711 }
712
713 // As soon as the dependency graph crosses the APEX boundary, don't go further.
714 return !externalDep
Jiyong Park83dc74b2020-01-14 18:38:44 +0900715 })
716
Artur Satayev872a1442020-04-27 17:08:37 +0100717 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, depInfos)
718
Jiyong Park83dc74b2020-01-14 18:38:44 +0900719 ctx.Build(pctx, android.BuildParams{
720 Rule: android.Phony,
721 Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
Artur Satayeva8bd1132020-04-27 18:07:06 +0100722 Inputs: []android.Path{
723 a.ApexBundleDepsInfo.FullListPath(),
724 a.ApexBundleDepsInfo.FlatListPath(),
725 },
Jiyong Park83dc74b2020-01-14 18:38:44 +0900726 })
727}