Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "runtime" |
| 21 | "sort" |
| 22 | "strings" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | "android/soong/java" |
| 26 | |
| 27 | "github.com/google/blueprint" |
| 28 | "github.com/google/blueprint/proptools" |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | pctx = android.NewPackageContext("android/apex") |
| 33 | ) |
| 34 | |
| 35 | func init() { |
| 36 | pctx.Import("android/soong/android") |
| 37 | pctx.Import("android/soong/java") |
| 38 | pctx.HostBinToolVariable("apexer", "apexer") |
| 39 | // ART minimal builds (using the master-art manifest) do not have the "frameworks/base" |
| 40 | // projects, and hence cannot built 'aapt2'. Use the SDK prebuilt instead. |
| 41 | hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) { |
| 42 | pctx.VariableFunc(name, func(ctx android.PackageVarContext) string { |
| 43 | if !ctx.Config().FrameworksBaseDirExists(ctx) { |
| 44 | return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool) |
| 45 | } else { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 46 | return ctx.Config().HostToolPath(ctx, tool).String() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 47 | } |
| 48 | }) |
| 49 | } |
| 50 | hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2") |
| 51 | pctx.HostBinToolVariable("avbtool", "avbtool") |
| 52 | pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid") |
| 53 | pctx.HostBinToolVariable("merge_zips", "merge_zips") |
| 54 | pctx.HostBinToolVariable("mke2fs", "mke2fs") |
| 55 | pctx.HostBinToolVariable("resize2fs", "resize2fs") |
| 56 | pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile") |
| 57 | pctx.HostBinToolVariable("soong_zip", "soong_zip") |
| 58 | pctx.HostBinToolVariable("zip2zip", "zip2zip") |
| 59 | pctx.HostBinToolVariable("zipalign", "zipalign") |
| 60 | pctx.HostBinToolVariable("jsonmodify", "jsonmodify") |
| 61 | pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest") |
| 62 | } |
| 63 | |
| 64 | var ( |
| 65 | // Create a canned fs config file where all files and directories are |
| 66 | // by default set to (uid/gid/mode) = (1000/1000/0644) |
| 67 | // TODO(b/113082813) make this configurable using config.fs syntax |
| 68 | generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{ |
| 69 | Command: `echo '/ 1000 1000 0755' > ${out} && ` + |
| 70 | `echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` + |
| 71 | `echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`, |
| 72 | Description: "fs_config ${out}", |
| 73 | }, "ro_paths", "exec_paths") |
| 74 | |
| 75 | apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{ |
| 76 | Command: `rm -f $out && ${jsonmodify} $in ` + |
| 77 | `-a provideNativeLibs ${provideNativeLibs} ` + |
| 78 | `-a requireNativeLibs ${requireNativeLibs} ` + |
| 79 | `${opt} ` + |
| 80 | `-o $out`, |
| 81 | CommandDeps: []string{"${jsonmodify}"}, |
| 82 | Description: "prepare ${out}", |
| 83 | }, "provideNativeLibs", "requireNativeLibs", "opt") |
| 84 | |
| 85 | stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{ |
| 86 | Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`, |
| 87 | CommandDeps: []string{"${conv_apex_manifest}"}, |
| 88 | Description: "strip ${in}=>${out}", |
| 89 | }) |
| 90 | |
| 91 | pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{ |
| 92 | Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`, |
| 93 | CommandDeps: []string{"${conv_apex_manifest}"}, |
| 94 | Description: "convert ${in}=>${out}", |
| 95 | }) |
| 96 | |
| 97 | // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate |
| 98 | // against the binary policy using sefcontext_compiler -p <policy>. |
| 99 | |
| 100 | // TODO(b/114327326): automate the generation of file_contexts |
| 101 | apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{ |
| 102 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 103 | `(. ${out}.copy_commands) && ` + |
| 104 | `APEXER_TOOL_PATH=${tool_path} ` + |
| 105 | `${apexer} --force --manifest ${manifest} ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 106 | `--file_contexts ${file_contexts} ` + |
| 107 | `--canned_fs_config ${canned_fs_config} ` + |
Dario Freni | 0f4ae07 | 2020-01-02 15:24:12 +0000 | [diff] [blame] | 108 | `--include_build_info ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 109 | `--payload_type image ` + |
| 110 | `--key ${key} ${opt_flags} ${image_dir} ${out} `, |
| 111 | CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}", |
| 112 | "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", |
| 113 | "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"}, |
| 114 | Rspfile: "${out}.copy_commands", |
| 115 | RspfileContent: "${copy_commands}", |
| 116 | Description: "APEX ${image_dir} => ${out}", |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 117 | }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 118 | |
| 119 | zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{ |
| 120 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 121 | `(. ${out}.copy_commands) && ` + |
| 122 | `APEXER_TOOL_PATH=${tool_path} ` + |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 123 | `${apexer} --force --manifest ${manifest} ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 124 | `--payload_type zip ` + |
| 125 | `${image_dir} ${out} `, |
| 126 | CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"}, |
| 127 | Rspfile: "${out}.copy_commands", |
| 128 | RspfileContent: "${copy_commands}", |
| 129 | Description: "ZipAPEX ${image_dir} => ${out}", |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 130 | }, "tool_path", "image_dir", "copy_commands", "manifest") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 131 | |
| 132 | apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule", |
| 133 | blueprint.RuleParams{ |
| 134 | Command: `${aapt2} convert --output-format proto $in -o $out`, |
| 135 | CommandDeps: []string{"${aapt2}"}, |
| 136 | }) |
| 137 | |
| 138 | apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{ |
| 139 | Command: `${zip2zip} -i $in -o $out ` + |
| 140 | `apex_payload.img:apex/${abi}.img ` + |
| 141 | `apex_manifest.json:root/apex_manifest.json ` + |
Jiyong Park | 53ae334 | 2019-12-08 02:06:24 +0900 | [diff] [blame] | 142 | `apex_manifest.pb:root/apex_manifest.pb ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 143 | `AndroidManifest.xml:manifest/AndroidManifest.xml ` + |
| 144 | `assets/NOTICE.html.gz:assets/NOTICE.html.gz`, |
| 145 | CommandDeps: []string{"${zip2zip}"}, |
| 146 | Description: "app bundle", |
| 147 | }, "abi") |
| 148 | |
| 149 | emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{ |
| 150 | Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`, |
| 151 | Rspfile: "${out}.emit_commands", |
| 152 | RspfileContent: "${emit_commands}", |
| 153 | Description: "Emit APEX image content", |
| 154 | }, "emit_commands") |
| 155 | |
| 156 | diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{ |
| 157 | Command: `diff --unchanged-group-format='' \` + |
| 158 | `--changed-group-format='%<' \` + |
| 159 | `${image_content_file} ${whitelisted_files_file} || (` + |
| 160 | `echo -e "New unexpected files were added to ${apex_module_name}." ` + |
| 161 | ` "To fix the build run following command:" && ` + |
| 162 | `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` + |
| 163 | `exit 1)`, |
| 164 | Description: "Diff ${image_content_file} and ${whitelisted_files_file}", |
| 165 | }, "image_content_file", "whitelisted_files_file", "apex_module_name") |
| 166 | ) |
| 167 | |
| 168 | func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) { |
| 169 | manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")) |
| 170 | |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 171 | manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 172 | |
| 173 | // put dependency({provide|require}NativeLibs) in apex_manifest.json |
| 174 | provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs) |
| 175 | requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs)) |
| 176 | |
| 177 | // apex name can be overridden |
| 178 | optCommands := []string{} |
| 179 | if a.properties.Apex_name != nil { |
| 180 | optCommands = append(optCommands, "-v name "+*a.properties.Apex_name) |
| 181 | } |
| 182 | |
| 183 | ctx.Build(pctx, android.BuildParams{ |
| 184 | Rule: apexManifestRule, |
| 185 | Input: manifestSrc, |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 186 | Output: manifestJsonFullOut, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 187 | Args: map[string]string{ |
| 188 | "provideNativeLibs": strings.Join(provideNativeLibs, " "), |
| 189 | "requireNativeLibs": strings.Join(requireNativeLibs, " "), |
| 190 | "opt": strings.Join(optCommands, " "), |
| 191 | }, |
| 192 | }) |
| 193 | |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 194 | if proptools.Bool(a.properties.Legacy_android10_support) { |
| 195 | // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json |
| 196 | // prepare stripped-down version so that APEX modules built from R+ can be installed to Q |
| 197 | a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json") |
| 198 | ctx.Build(pctx, android.BuildParams{ |
| 199 | Rule: stripApexManifestRule, |
| 200 | Input: manifestJsonFullOut, |
| 201 | Output: a.manifestJsonOut, |
| 202 | }) |
| 203 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 204 | |
| 205 | // from R+, protobuf binary format (.pb) is the standard format for apex_manifest |
| 206 | a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb") |
| 207 | ctx.Build(pctx, android.BuildParams{ |
| 208 | Rule: pbApexManifestRule, |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 209 | Input: manifestJsonFullOut, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 210 | Output: a.manifestPbOut, |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath { |
| 215 | noticeFiles := []android.Path{} |
| 216 | for _, f := range a.filesInfo { |
| 217 | if f.module != nil { |
| 218 | notice := f.module.NoticeFile() |
| 219 | if notice.Valid() { |
| 220 | noticeFiles = append(noticeFiles, notice.Path()) |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | // append the notice file specified in the apex module itself |
| 225 | if a.NoticeFile().Valid() { |
| 226 | noticeFiles = append(noticeFiles, a.NoticeFile().Path()) |
| 227 | } |
| 228 | |
| 229 | if len(noticeFiles) == 0 { |
| 230 | return android.OptionalPath{} |
| 231 | } |
| 232 | |
| 233 | return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput |
| 234 | } |
| 235 | |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 236 | func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath { |
| 237 | output := android.PathForModuleOut(ctx, "installed-files.txt") |
| 238 | rule := android.NewRuleBuilder() |
| 239 | rule.Command(). |
| 240 | Implicit(builtApex). |
| 241 | Text("(cd " + imageDir.String() + " ; "). |
| 242 | Text("find . -type f -printf \"%s %p\\n\") "). |
| 243 | Text(" | sort -nr > "). |
| 244 | Output(output) |
| 245 | rule.Build(pctx, ctx, "installed-files."+a.Name(), "Installed files") |
| 246 | return output.OutputPath |
| 247 | } |
| 248 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 249 | func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) { |
| 250 | var abis []string |
| 251 | for _, target := range ctx.MultiTargets() { |
| 252 | if len(target.Arch.Abi) > 0 { |
| 253 | abis = append(abis, target.Arch.Abi[0]) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | abis = android.FirstUniqueStrings(abis) |
| 258 | |
| 259 | apexType := a.properties.ApexType |
| 260 | suffix := apexType.suffix() |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 261 | var implicitInputs []android.Path |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 262 | unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 263 | |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 264 | // TODO(jiyong): construct the copy rules using RuleBuilder |
| 265 | var copyCommands []string |
| 266 | for _, fi := range a.filesInfo { |
| 267 | destPath := android.PathForModuleOut(ctx, "image"+suffix, fi.Path()).String() |
| 268 | copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(destPath)) |
| 269 | if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() { |
| 270 | // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here |
| 271 | pathOnDevice := filepath.Join("/system", fi.Path()) |
| 272 | copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath) |
| 273 | } else { |
| 274 | copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath) |
| 275 | implicitInputs = append(implicitInputs, fi.builtFile) |
| 276 | } |
| 277 | // create additional symlinks pointing the file inside the APEX |
| 278 | for _, symlinkPath := range fi.SymlinkPaths() { |
| 279 | symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String() |
| 280 | copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest) |
| 281 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 282 | } |
| 283 | |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 284 | // TODO(jiyong): use RuleBuilder |
| 285 | var emitCommands []string |
| 286 | imageContentFile := android.PathForModuleOut(ctx, "content.txt") |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 287 | emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String()) |
| 288 | if proptools.Bool(a.properties.Legacy_android10_support) { |
| 289 | emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String()) |
| 290 | } |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 291 | for _, fi := range a.filesInfo { |
| 292 | emitCommands = append(emitCommands, "echo './"+fi.Path()+"' >> "+imageContentFile.String()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 293 | } |
| 294 | emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String()) |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 295 | implicitInputs = append(implicitInputs, a.manifestPbOut) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 296 | |
| 297 | if a.properties.Whitelisted_files != nil { |
| 298 | ctx.Build(pctx, android.BuildParams{ |
| 299 | Rule: emitApexContentRule, |
| 300 | Implicits: implicitInputs, |
| 301 | Output: imageContentFile, |
| 302 | Description: "emit apex image content", |
| 303 | Args: map[string]string{ |
| 304 | "emit_commands": strings.Join(emitCommands, " && "), |
| 305 | }, |
| 306 | }) |
| 307 | implicitInputs = append(implicitInputs, imageContentFile) |
| 308 | whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files)) |
| 309 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 310 | phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 311 | ctx.Build(pctx, android.BuildParams{ |
| 312 | Rule: diffApexContentRule, |
| 313 | Implicits: implicitInputs, |
| 314 | Output: phonyOutput, |
| 315 | Description: "diff apex image content", |
| 316 | Args: map[string]string{ |
| 317 | "whitelisted_files_file": whitelistedFilesFile.String(), |
| 318 | "image_content_file": imageContentFile.String(), |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 319 | "apex_module_name": a.Name(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 320 | }, |
| 321 | }) |
| 322 | |
| 323 | implicitInputs = append(implicitInputs, phonyOutput) |
| 324 | } |
| 325 | |
| 326 | outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String() |
| 327 | prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin") |
| 328 | |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 329 | imageDir := android.PathForModuleOut(ctx, "image"+suffix) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 330 | if apexType == imageApex { |
| 331 | // files and dirs that will be created in APEX |
| 332 | var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"} |
| 333 | var executablePaths []string // this also includes dirs |
| 334 | for _, f := range a.filesInfo { |
| 335 | pathInApex := filepath.Join(f.installDir, f.builtFile.Base()) |
| 336 | if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") { |
| 337 | executablePaths = append(executablePaths, pathInApex) |
| 338 | for _, s := range f.symlinks { |
| 339 | executablePaths = append(executablePaths, filepath.Join(f.installDir, s)) |
| 340 | } |
| 341 | } else { |
| 342 | readOnlyPaths = append(readOnlyPaths, pathInApex) |
| 343 | } |
| 344 | dir := f.installDir |
| 345 | for !android.InList(dir, executablePaths) && dir != "" { |
| 346 | executablePaths = append(executablePaths, dir) |
| 347 | dir, _ = filepath.Split(dir) // move up to the parent |
| 348 | if len(dir) > 0 { |
| 349 | // remove trailing slash |
| 350 | dir = dir[:len(dir)-1] |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | sort.Strings(readOnlyPaths) |
| 355 | sort.Strings(executablePaths) |
| 356 | cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config") |
| 357 | ctx.Build(pctx, android.BuildParams{ |
| 358 | Rule: generateFsConfig, |
| 359 | Output: cannedFsConfig, |
| 360 | Description: "generate fs config", |
| 361 | Args: map[string]string{ |
| 362 | "ro_paths": strings.Join(readOnlyPaths, " "), |
| 363 | "exec_paths": strings.Join(executablePaths, " "), |
| 364 | }, |
| 365 | }) |
| 366 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 367 | optFlags := []string{} |
| 368 | |
| 369 | // Additional implicit inputs. |
Jooyung Han | 54aca7b | 2019-11-20 02:26:02 +0900 | [diff] [blame] | 370 | implicitInputs = append(implicitInputs, cannedFsConfig, a.fileContexts, a.private_key_file, a.public_key_file) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 371 | optFlags = append(optFlags, "--pubkey "+a.public_key_file.String()) |
| 372 | |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 373 | manifestPackageName := a.getOverrideManifestPackageName(ctx) |
| 374 | if manifestPackageName != "" { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 375 | optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName) |
| 376 | } |
| 377 | |
| 378 | if a.properties.AndroidManifest != nil { |
| 379 | androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest)) |
| 380 | implicitInputs = append(implicitInputs, androidManifestFile) |
| 381 | optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String()) |
| 382 | } |
| 383 | |
| 384 | targetSdkVersion := ctx.Config().DefaultAppTargetSdk() |
| 385 | if targetSdkVersion == ctx.Config().PlatformSdkCodename() && |
| 386 | ctx.Config().UnbundledBuild() && |
| 387 | !ctx.Config().UnbundledBuildUsePrebuiltSdks() && |
| 388 | ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") { |
| 389 | apiFingerprint := java.ApiFingerprintPath(ctx) |
| 390 | targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String()) |
| 391 | implicitInputs = append(implicitInputs, apiFingerprint) |
| 392 | } |
| 393 | optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion) |
| 394 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 395 | noticeFile := a.buildNoticeFile(ctx, a.Name()+suffix) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 396 | if noticeFile.Valid() { |
| 397 | // If there's a NOTICE file, embed it as an asset file in the APEX. |
| 398 | implicitInputs = append(implicitInputs, noticeFile.Path()) |
| 399 | optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String())) |
| 400 | } |
| 401 | |
Nikita Ioffe | b4b44c0 | 2020-01-02 23:01:39 +0000 | [diff] [blame] | 402 | if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && ctx.ModuleDir() != "system/apex/shim/build" && a.testOnlyShouldSkipHashtreeGeneration() { |
Nikita Ioffe | c72b5dd | 2019-12-07 17:30:22 +0000 | [diff] [blame] | 403 | ctx.PropertyErrorf("test_only_no_hashtree", "not available") |
| 404 | return |
| 405 | } |
Dario Freni | e354690 | 2020-01-14 23:50:25 +0000 | [diff] [blame] | 406 | if !proptools.Bool(a.properties.Legacy_android10_support) || a.testOnlyShouldSkipHashtreeGeneration() { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 407 | // Apexes which are supposed to be installed in builtin dirs(/system, etc) |
| 408 | // don't need hashtree for activation. Therefore, by removing hashtree from |
| 409 | // apex bundle (filesystem image in it, to be specific), we can save storage. |
| 410 | optFlags = append(optFlags, "--no_hashtree") |
| 411 | } |
| 412 | |
| 413 | if a.properties.Apex_name != nil { |
| 414 | // If apex_name is set, apexer can skip checking if key name matches with apex name. |
| 415 | // Note that apex_manifest is also mended. |
| 416 | optFlags = append(optFlags, "--do_not_check_keyname") |
| 417 | } |
| 418 | |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 419 | if proptools.Bool(a.properties.Legacy_android10_support) { |
| 420 | implicitInputs = append(implicitInputs, a.manifestJsonOut) |
| 421 | optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String()) |
| 422 | } |
| 423 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 424 | ctx.Build(pctx, android.BuildParams{ |
| 425 | Rule: apexRule, |
| 426 | Implicits: implicitInputs, |
| 427 | Output: unsignedOutputFile, |
| 428 | Description: "apex (" + apexType.name() + ")", |
| 429 | Args: map[string]string{ |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 430 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 431 | "image_dir": imageDir.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 432 | "copy_commands": strings.Join(copyCommands, " && "), |
| 433 | "manifest": a.manifestPbOut.String(), |
| 434 | "file_contexts": a.fileContexts.String(), |
| 435 | "canned_fs_config": cannedFsConfig.String(), |
| 436 | "key": a.private_key_file.String(), |
| 437 | "opt_flags": strings.Join(optFlags, " "), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 438 | }, |
| 439 | }) |
| 440 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 441 | apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix) |
| 442 | bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 443 | a.bundleModuleFile = bundleModuleFile |
| 444 | |
| 445 | ctx.Build(pctx, android.BuildParams{ |
| 446 | Rule: apexProtoConvertRule, |
| 447 | Input: unsignedOutputFile, |
| 448 | Output: apexProtoFile, |
| 449 | Description: "apex proto convert", |
| 450 | }) |
| 451 | |
| 452 | ctx.Build(pctx, android.BuildParams{ |
| 453 | Rule: apexBundleRule, |
| 454 | Input: apexProtoFile, |
| 455 | Output: a.bundleModuleFile, |
| 456 | Description: "apex bundle module", |
| 457 | Args: map[string]string{ |
| 458 | "abi": strings.Join(abis, "."), |
| 459 | }, |
| 460 | }) |
| 461 | } else { |
| 462 | ctx.Build(pctx, android.BuildParams{ |
| 463 | Rule: zipApexRule, |
| 464 | Implicits: implicitInputs, |
| 465 | Output: unsignedOutputFile, |
| 466 | Description: "apex (" + apexType.name() + ")", |
| 467 | Args: map[string]string{ |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 468 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 469 | "image_dir": imageDir.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 470 | "copy_commands": strings.Join(copyCommands, " && "), |
| 471 | "manifest": a.manifestPbOut.String(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 472 | }, |
| 473 | }) |
| 474 | } |
| 475 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 476 | a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 477 | ctx.Build(pctx, android.BuildParams{ |
| 478 | Rule: java.Signapk, |
| 479 | Description: "signapk", |
| 480 | Output: a.outputFile, |
| 481 | Input: unsignedOutputFile, |
| 482 | Implicits: []android.Path{ |
| 483 | a.container_certificate_file, |
| 484 | a.container_private_key_file, |
| 485 | }, |
| 486 | Args: map[string]string{ |
| 487 | "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(), |
| 488 | "flags": "-a 4096", //alignment |
| 489 | }, |
| 490 | }) |
| 491 | |
| 492 | // Install to $OUT/soong/{target,host}/.../apex |
| 493 | if a.installable() { |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 494 | ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 495 | } |
| 496 | a.buildFilesInfo(ctx) |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 497 | |
| 498 | // installed-files.txt is dist'ed |
| 499 | a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) { |
| 503 | // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it |
| 504 | // reply true to `InstallBypassMake()` (thus making the call |
| 505 | // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir` |
| 506 | // instead of `android.PathForOutput`) to return the correct path to the flattened |
| 507 | // APEX (as its contents is installed by Make, not Soong). |
| 508 | factx := flattenedApexContext{ctx} |
| 509 | apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName()) |
| 510 | a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName) |
| 511 | |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 512 | if a.installable() && a.GetOverriddenBy() == "" { |
Jooyung Han | 54aca7b | 2019-11-20 02:26:02 +0900 | [diff] [blame] | 513 | installPath := android.PathForModuleInstall(ctx, "apex", apexName) |
| 514 | devicePath := android.InstallPathToOnDevicePath(ctx, installPath) |
| 515 | addFlattenedFileContextsInfos(ctx, apexName+":"+devicePath+":"+a.fileContexts.String()) |
| 516 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 517 | a.buildFilesInfo(ctx) |
| 518 | } |
| 519 | |
| 520 | func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) { |
Jooyung Han | f121a65 | 2019-12-17 14:30:11 +0900 | [diff] [blame] | 521 | if a.container_certificate_file == nil { |
| 522 | cert := String(a.properties.Certificate) |
| 523 | if cert == "" { |
| 524 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 525 | a.container_certificate_file = pem |
| 526 | a.container_private_key_file = key |
| 527 | } else { |
| 528 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 529 | a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem") |
| 530 | a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8") |
| 531 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) { |
| 536 | if a.installable() { |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 537 | // For flattened APEX, do nothing but make sure that APEX manifest and apex_pubkey are also copied along |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 538 | // with other ordinary files. |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 539 | a.filesInfo = append(a.filesInfo, newApexFile(ctx, a.manifestPbOut, "apex_manifest.pb", ".", etc, nil)) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 540 | |
| 541 | // rename to apex_pubkey |
| 542 | copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey") |
| 543 | ctx.Build(pctx, android.BuildParams{ |
| 544 | Rule: android.Cp, |
| 545 | Input: a.public_key_file, |
| 546 | Output: copiedPubkey, |
| 547 | }) |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame^] | 548 | a.filesInfo = append(a.filesInfo, newApexFile(ctx, copiedPubkey, "apex_pubkey", ".", etc, nil)) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 549 | |
| 550 | if a.properties.ApexType == flattenedApex { |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 551 | apexName := proptools.StringDefault(a.properties.Apex_name, a.Name()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 552 | for _, fi := range a.filesInfo { |
| 553 | dir := filepath.Join("apex", apexName, fi.installDir) |
| 554 | target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile) |
| 555 | for _, sym := range fi.symlinks { |
| 556 | ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target) |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 562 | |
| 563 | func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string { |
| 564 | // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES |
| 565 | // to see if it should be overridden because their <apex name> is dynamically generated |
| 566 | // according to its VNDK version. |
| 567 | if a.vndkApex { |
| 568 | overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName) |
| 569 | if overridden { |
| 570 | return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1) |
| 571 | } |
| 572 | return "" |
| 573 | } |
| 574 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(a.Name()) |
| 575 | if overridden { |
| 576 | return manifestPackageName |
| 577 | } |
| 578 | return "" |
| 579 | } |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 580 | |
| 581 | func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) { |
| 582 | if !a.primaryApexType { |
| 583 | return |
| 584 | } |
| 585 | |
| 586 | if a.properties.IsCoverageVariant { |
| 587 | // Otherwise, we will have duplicated rules for coverage and |
| 588 | // non-coverage variants of the same APEX |
| 589 | return |
| 590 | } |
| 591 | |
| 592 | if ctx.Host() { |
| 593 | // No need to generate dependency info for host variant |
| 594 | return |
| 595 | } |
| 596 | |
| 597 | internalDeps := a.internalDeps |
| 598 | externalDeps := a.externalDeps |
| 599 | |
| 600 | internalDeps = android.SortedUniqueStrings(internalDeps) |
| 601 | externalDeps = android.SortedUniqueStrings(externalDeps) |
| 602 | externalDeps = android.RemoveListFromList(externalDeps, internalDeps) |
| 603 | |
| 604 | var content strings.Builder |
| 605 | for _, name := range internalDeps { |
| 606 | fmt.Fprintf(&content, "internal %s\\n", name) |
| 607 | } |
| 608 | for _, name := range externalDeps { |
| 609 | fmt.Fprintf(&content, "external %s\\n", name) |
| 610 | } |
| 611 | |
| 612 | depsInfoFile := android.PathForOutput(ctx, a.Name()+"-deps-info.txt") |
| 613 | ctx.Build(pctx, android.BuildParams{ |
| 614 | Rule: android.WriteFile, |
| 615 | Description: "Dependency Info", |
| 616 | Output: depsInfoFile, |
| 617 | Args: map[string]string{ |
| 618 | "content": content.String(), |
| 619 | }, |
| 620 | }) |
| 621 | |
| 622 | ctx.Build(pctx, android.BuildParams{ |
| 623 | Rule: android.Phony, |
| 624 | Output: android.PathForPhony(ctx, a.Name()+"-deps-info"), |
| 625 | Inputs: []android.Path{depsInfoFile}, |
| 626 | }) |
| 627 | } |