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 { |
| 46 | return pctx.HostBinToolPath(ctx, tool).String() |
| 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} ` + |
| 106 | `--manifest_json ${manifest_json} --manifest_json_full ${manifest_json_full} ` + |
| 107 | `--file_contexts ${file_contexts} ` + |
| 108 | `--canned_fs_config ${canned_fs_config} ` + |
| 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}", |
| 117 | }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", |
| 118 | "manifest", "manifest_json", "manifest_json_full", |
| 119 | ) |
| 120 | |
| 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} ` + |
| 125 | `${apexer} --force --manifest ${manifest} --manifest_json_full ${manifest_json_full} ` + |
| 126 | `--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}", |
| 132 | }, "tool_path", "image_dir", "copy_commands", "manifest", "manifest_json_full") |
| 133 | |
| 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{ |
| 141 | Command: `${zip2zip} -i $in -o $out ` + |
| 142 | `apex_payload.img:apex/${abi}.img ` + |
| 143 | `apex_manifest.json:root/apex_manifest.json ` + |
| 144 | `AndroidManifest.xml:manifest/AndroidManifest.xml ` + |
| 145 | `assets/NOTICE.html.gz:assets/NOTICE.html.gz`, |
| 146 | CommandDeps: []string{"${zip2zip}"}, |
| 147 | Description: "app bundle", |
| 148 | }, "abi") |
| 149 | |
| 150 | emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{ |
| 151 | Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`, |
| 152 | Rspfile: "${out}.emit_commands", |
| 153 | RspfileContent: "${emit_commands}", |
| 154 | Description: "Emit APEX image content", |
| 155 | }, "emit_commands") |
| 156 | |
| 157 | diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{ |
| 158 | Command: `diff --unchanged-group-format='' \` + |
| 159 | `--changed-group-format='%<' \` + |
| 160 | `${image_content_file} ${whitelisted_files_file} || (` + |
| 161 | `echo -e "New unexpected files were added to ${apex_module_name}." ` + |
| 162 | ` "To fix the build run following command:" && ` + |
| 163 | `echo "system/apex/tools/update_whitelist.sh ${whitelisted_files_file} ${image_content_file}" && ` + |
| 164 | `exit 1)`, |
| 165 | Description: "Diff ${image_content_file} and ${whitelisted_files_file}", |
| 166 | }, "image_content_file", "whitelisted_files_file", "apex_module_name") |
| 167 | ) |
| 168 | |
| 169 | func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) { |
| 170 | manifestSrc := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")) |
| 171 | |
| 172 | a.manifestJsonFullOut = android.PathForModuleOut(ctx, "apex_manifest_full.json") |
| 173 | |
| 174 | // put dependency({provide|require}NativeLibs) in apex_manifest.json |
| 175 | provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs) |
| 176 | requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs)) |
| 177 | |
| 178 | // apex name can be overridden |
| 179 | optCommands := []string{} |
| 180 | if a.properties.Apex_name != nil { |
| 181 | optCommands = append(optCommands, "-v name "+*a.properties.Apex_name) |
| 182 | } |
| 183 | |
| 184 | ctx.Build(pctx, android.BuildParams{ |
| 185 | Rule: apexManifestRule, |
| 186 | Input: manifestSrc, |
| 187 | Output: a.manifestJsonFullOut, |
| 188 | Args: map[string]string{ |
| 189 | "provideNativeLibs": strings.Join(provideNativeLibs, " "), |
| 190 | "requireNativeLibs": strings.Join(requireNativeLibs, " "), |
| 191 | "opt": strings.Join(optCommands, " "), |
| 192 | }, |
| 193 | }) |
| 194 | |
| 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: a.manifestJsonFullOut, |
| 201 | Output: a.manifestJsonOut, |
| 202 | }) |
| 203 | |
| 204 | // from R+, protobuf binary format (.pb) is the standard format for apex_manifest |
| 205 | a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb") |
| 206 | ctx.Build(pctx, android.BuildParams{ |
| 207 | Rule: pbApexManifestRule, |
| 208 | Input: a.manifestJsonFullOut, |
| 209 | Output: a.manifestPbOut, |
| 210 | }) |
| 211 | } |
| 212 | |
| 213 | func (a *apexBundle) buildNoticeFile(ctx android.ModuleContext, apexFileName string) android.OptionalPath { |
| 214 | noticeFiles := []android.Path{} |
| 215 | for _, f := range a.filesInfo { |
| 216 | if f.module != nil { |
| 217 | notice := f.module.NoticeFile() |
| 218 | if notice.Valid() { |
| 219 | noticeFiles = append(noticeFiles, notice.Path()) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | // append the notice file specified in the apex module itself |
| 224 | if a.NoticeFile().Valid() { |
| 225 | noticeFiles = append(noticeFiles, a.NoticeFile().Path()) |
| 226 | } |
| 227 | |
| 228 | if len(noticeFiles) == 0 { |
| 229 | return android.OptionalPath{} |
| 230 | } |
| 231 | |
| 232 | return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.FirstUniquePaths(noticeFiles)).HtmlGzOutput |
| 233 | } |
| 234 | |
| 235 | func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) { |
| 236 | var abis []string |
| 237 | for _, target := range ctx.MultiTargets() { |
| 238 | if len(target.Arch.Abi) > 0 { |
| 239 | abis = append(abis, target.Arch.Abi[0]) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | abis = android.FirstUniqueStrings(abis) |
| 244 | |
| 245 | apexType := a.properties.ApexType |
| 246 | suffix := apexType.suffix() |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 247 | unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 248 | |
| 249 | filesToCopy := []android.Path{} |
| 250 | for _, f := range a.filesInfo { |
| 251 | filesToCopy = append(filesToCopy, f.builtFile) |
| 252 | } |
| 253 | |
| 254 | copyCommands := []string{} |
| 255 | emitCommands := []string{} |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 256 | imageContentFile := android.PathForModuleOut(ctx, a.Name()+"-content.txt") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 257 | emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String()) |
| 258 | for i, src := range filesToCopy { |
| 259 | dest := filepath.Join(a.filesInfo[i].installDir, src.Base()) |
| 260 | emitCommands = append(emitCommands, "echo './"+dest+"' >> "+imageContentFile.String()) |
| 261 | dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest) |
| 262 | copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path)) |
| 263 | copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path) |
| 264 | for _, sym := range a.filesInfo[i].symlinks { |
| 265 | symlinkDest := filepath.Join(filepath.Dir(dest_path), sym) |
| 266 | copyCommands = append(copyCommands, "ln -s "+filepath.Base(dest)+" "+symlinkDest) |
| 267 | } |
| 268 | } |
| 269 | emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String()) |
| 270 | |
| 271 | implicitInputs := append(android.Paths(nil), filesToCopy...) |
| 272 | implicitInputs = append(implicitInputs, a.manifestPbOut, a.manifestJsonFullOut, a.manifestJsonOut) |
| 273 | |
| 274 | if a.properties.Whitelisted_files != nil { |
| 275 | ctx.Build(pctx, android.BuildParams{ |
| 276 | Rule: emitApexContentRule, |
| 277 | Implicits: implicitInputs, |
| 278 | Output: imageContentFile, |
| 279 | Description: "emit apex image content", |
| 280 | Args: map[string]string{ |
| 281 | "emit_commands": strings.Join(emitCommands, " && "), |
| 282 | }, |
| 283 | }) |
| 284 | implicitInputs = append(implicitInputs, imageContentFile) |
| 285 | whitelistedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.Whitelisted_files)) |
| 286 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 287 | phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 288 | ctx.Build(pctx, android.BuildParams{ |
| 289 | Rule: diffApexContentRule, |
| 290 | Implicits: implicitInputs, |
| 291 | Output: phonyOutput, |
| 292 | Description: "diff apex image content", |
| 293 | Args: map[string]string{ |
| 294 | "whitelisted_files_file": whitelistedFilesFile.String(), |
| 295 | "image_content_file": imageContentFile.String(), |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 296 | "apex_module_name": a.Name(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 297 | }, |
| 298 | }) |
| 299 | |
| 300 | implicitInputs = append(implicitInputs, phonyOutput) |
| 301 | } |
| 302 | |
| 303 | outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String() |
| 304 | prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin") |
| 305 | |
| 306 | if apexType == imageApex { |
| 307 | // files and dirs that will be created in APEX |
| 308 | var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"} |
| 309 | var executablePaths []string // this also includes dirs |
| 310 | for _, f := range a.filesInfo { |
| 311 | pathInApex := filepath.Join(f.installDir, f.builtFile.Base()) |
| 312 | if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") { |
| 313 | executablePaths = append(executablePaths, pathInApex) |
| 314 | for _, s := range f.symlinks { |
| 315 | executablePaths = append(executablePaths, filepath.Join(f.installDir, s)) |
| 316 | } |
| 317 | } else { |
| 318 | readOnlyPaths = append(readOnlyPaths, pathInApex) |
| 319 | } |
| 320 | dir := f.installDir |
| 321 | for !android.InList(dir, executablePaths) && dir != "" { |
| 322 | executablePaths = append(executablePaths, dir) |
| 323 | dir, _ = filepath.Split(dir) // move up to the parent |
| 324 | if len(dir) > 0 { |
| 325 | // remove trailing slash |
| 326 | dir = dir[:len(dir)-1] |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | sort.Strings(readOnlyPaths) |
| 331 | sort.Strings(executablePaths) |
| 332 | cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config") |
| 333 | ctx.Build(pctx, android.BuildParams{ |
| 334 | Rule: generateFsConfig, |
| 335 | Output: cannedFsConfig, |
| 336 | Description: "generate fs config", |
| 337 | Args: map[string]string{ |
| 338 | "ro_paths": strings.Join(readOnlyPaths, " "), |
| 339 | "exec_paths": strings.Join(executablePaths, " "), |
| 340 | }, |
| 341 | }) |
| 342 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 343 | optFlags := []string{} |
| 344 | |
| 345 | // Additional implicit inputs. |
Jooyung Han | 54aca7b | 2019-11-20 02:26:02 +0900 | [diff] [blame] | 346 | 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] | 347 | optFlags = append(optFlags, "--pubkey "+a.public_key_file.String()) |
| 348 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 349 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(a.Name()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 350 | if overridden { |
| 351 | optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName) |
| 352 | } |
| 353 | |
| 354 | if a.properties.AndroidManifest != nil { |
| 355 | androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest)) |
| 356 | implicitInputs = append(implicitInputs, androidManifestFile) |
| 357 | optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String()) |
| 358 | } |
| 359 | |
| 360 | targetSdkVersion := ctx.Config().DefaultAppTargetSdk() |
| 361 | if targetSdkVersion == ctx.Config().PlatformSdkCodename() && |
| 362 | ctx.Config().UnbundledBuild() && |
| 363 | !ctx.Config().UnbundledBuildUsePrebuiltSdks() && |
| 364 | ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") { |
| 365 | apiFingerprint := java.ApiFingerprintPath(ctx) |
| 366 | targetSdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String()) |
| 367 | implicitInputs = append(implicitInputs, apiFingerprint) |
| 368 | } |
| 369 | optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion) |
| 370 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 371 | noticeFile := a.buildNoticeFile(ctx, a.Name()+suffix) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 372 | if noticeFile.Valid() { |
| 373 | // If there's a NOTICE file, embed it as an asset file in the APEX. |
| 374 | implicitInputs = append(implicitInputs, noticeFile.Path()) |
| 375 | optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeFile.String())) |
| 376 | } |
| 377 | |
| 378 | if !ctx.Config().UnbundledBuild() && a.installable() { |
| 379 | // Apexes which are supposed to be installed in builtin dirs(/system, etc) |
| 380 | // don't need hashtree for activation. Therefore, by removing hashtree from |
| 381 | // apex bundle (filesystem image in it, to be specific), we can save storage. |
| 382 | optFlags = append(optFlags, "--no_hashtree") |
| 383 | } |
| 384 | |
| 385 | if a.properties.Apex_name != nil { |
| 386 | // If apex_name is set, apexer can skip checking if key name matches with apex name. |
| 387 | // Note that apex_manifest is also mended. |
| 388 | optFlags = append(optFlags, "--do_not_check_keyname") |
| 389 | } |
| 390 | |
| 391 | ctx.Build(pctx, android.BuildParams{ |
| 392 | Rule: apexRule, |
| 393 | Implicits: implicitInputs, |
| 394 | Output: unsignedOutputFile, |
| 395 | Description: "apex (" + apexType.name() + ")", |
| 396 | Args: map[string]string{ |
| 397 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
| 398 | "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(), |
| 399 | "copy_commands": strings.Join(copyCommands, " && "), |
| 400 | "manifest_json_full": a.manifestJsonFullOut.String(), |
| 401 | "manifest_json": a.manifestJsonOut.String(), |
| 402 | "manifest": a.manifestPbOut.String(), |
Jooyung Han | 54aca7b | 2019-11-20 02:26:02 +0900 | [diff] [blame] | 403 | "file_contexts": a.fileContexts.String(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 404 | "canned_fs_config": cannedFsConfig.String(), |
| 405 | "key": a.private_key_file.String(), |
| 406 | "opt_flags": strings.Join(optFlags, " "), |
| 407 | }, |
| 408 | }) |
| 409 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 410 | apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix) |
| 411 | bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 412 | a.bundleModuleFile = bundleModuleFile |
| 413 | |
| 414 | ctx.Build(pctx, android.BuildParams{ |
| 415 | Rule: apexProtoConvertRule, |
| 416 | Input: unsignedOutputFile, |
| 417 | Output: apexProtoFile, |
| 418 | Description: "apex proto convert", |
| 419 | }) |
| 420 | |
| 421 | ctx.Build(pctx, android.BuildParams{ |
| 422 | Rule: apexBundleRule, |
| 423 | Input: apexProtoFile, |
| 424 | Output: a.bundleModuleFile, |
| 425 | Description: "apex bundle module", |
| 426 | Args: map[string]string{ |
| 427 | "abi": strings.Join(abis, "."), |
| 428 | }, |
| 429 | }) |
| 430 | } else { |
| 431 | ctx.Build(pctx, android.BuildParams{ |
| 432 | Rule: zipApexRule, |
| 433 | Implicits: implicitInputs, |
| 434 | Output: unsignedOutputFile, |
| 435 | Description: "apex (" + apexType.name() + ")", |
| 436 | Args: map[string]string{ |
| 437 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
| 438 | "image_dir": android.PathForModuleOut(ctx, "image"+suffix).String(), |
| 439 | "copy_commands": strings.Join(copyCommands, " && "), |
| 440 | "manifest": a.manifestPbOut.String(), |
| 441 | "manifest_json_full": a.manifestJsonFullOut.String(), |
| 442 | }, |
| 443 | }) |
| 444 | } |
| 445 | |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 446 | a.outputFile = android.PathForModuleOut(ctx, a.Name()+suffix) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 447 | ctx.Build(pctx, android.BuildParams{ |
| 448 | Rule: java.Signapk, |
| 449 | Description: "signapk", |
| 450 | Output: a.outputFile, |
| 451 | Input: unsignedOutputFile, |
| 452 | Implicits: []android.Path{ |
| 453 | a.container_certificate_file, |
| 454 | a.container_private_key_file, |
| 455 | }, |
| 456 | Args: map[string]string{ |
| 457 | "certificates": a.container_certificate_file.String() + " " + a.container_private_key_file.String(), |
| 458 | "flags": "-a 4096", //alignment |
| 459 | }, |
| 460 | }) |
| 461 | |
| 462 | // Install to $OUT/soong/{target,host}/.../apex |
| 463 | if a.installable() { |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 464 | ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 465 | } |
| 466 | a.buildFilesInfo(ctx) |
| 467 | } |
| 468 | |
| 469 | func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) { |
| 470 | // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it |
| 471 | // reply true to `InstallBypassMake()` (thus making the call |
| 472 | // `android.PathForModuleInstall` below use `android.pathForInstallInMakeDir` |
| 473 | // instead of `android.PathForOutput`) to return the correct path to the flattened |
| 474 | // APEX (as its contents is installed by Make, not Soong). |
| 475 | factx := flattenedApexContext{ctx} |
| 476 | apexName := proptools.StringDefault(a.properties.Apex_name, ctx.ModuleName()) |
| 477 | a.outputFile = android.PathForModuleInstall(&factx, "apex", apexName) |
| 478 | |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 479 | if a.installable() && a.GetOverriddenBy() == "" { |
Jooyung Han | 54aca7b | 2019-11-20 02:26:02 +0900 | [diff] [blame] | 480 | installPath := android.PathForModuleInstall(ctx, "apex", apexName) |
| 481 | devicePath := android.InstallPathToOnDevicePath(ctx, installPath) |
| 482 | addFlattenedFileContextsInfos(ctx, apexName+":"+devicePath+":"+a.fileContexts.String()) |
| 483 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 484 | a.buildFilesInfo(ctx) |
| 485 | } |
| 486 | |
| 487 | func (a *apexBundle) setCertificateAndPrivateKey(ctx android.ModuleContext) { |
| 488 | cert := String(a.properties.Certificate) |
| 489 | if cert != "" && android.SrcIsModule(cert) == "" { |
| 490 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 491 | a.container_certificate_file = defaultDir.Join(ctx, cert+".x509.pem") |
| 492 | a.container_private_key_file = defaultDir.Join(ctx, cert+".pk8") |
| 493 | } else if cert == "" { |
| 494 | pem, key := ctx.Config().DefaultAppCertificate(ctx) |
| 495 | a.container_certificate_file = pem |
| 496 | a.container_private_key_file = key |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | func (a *apexBundle) buildFilesInfo(ctx android.ModuleContext) { |
| 501 | if a.installable() { |
| 502 | // For flattened APEX, do nothing but make sure that apex_manifest.json and apex_pubkey are also copied along |
| 503 | // with other ordinary files. |
Jiyong Park | f653b05 | 2019-11-18 15:39:01 +0900 | [diff] [blame] | 504 | a.filesInfo = append(a.filesInfo, newApexFile(a.manifestJsonOut, "apex_manifest.json."+a.Name()+a.suffix, ".", etc, nil)) |
| 505 | a.filesInfo = append(a.filesInfo, newApexFile(a.manifestPbOut, "apex_manifest.pb."+a.Name()+a.suffix, ".", etc, nil)) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 506 | |
| 507 | // rename to apex_pubkey |
| 508 | copiedPubkey := android.PathForModuleOut(ctx, "apex_pubkey") |
| 509 | ctx.Build(pctx, android.BuildParams{ |
| 510 | Rule: android.Cp, |
| 511 | Input: a.public_key_file, |
| 512 | Output: copiedPubkey, |
| 513 | }) |
Jiyong Park | f653b05 | 2019-11-18 15:39:01 +0900 | [diff] [blame] | 514 | a.filesInfo = append(a.filesInfo, newApexFile(copiedPubkey, "apex_pubkey."+a.Name()+a.suffix, ".", etc, nil)) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 515 | |
| 516 | if a.properties.ApexType == flattenedApex { |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 517 | apexName := proptools.StringDefault(a.properties.Apex_name, a.Name()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 518 | for _, fi := range a.filesInfo { |
| 519 | dir := filepath.Join("apex", apexName, fi.installDir) |
| 520 | target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile) |
| 521 | for _, sym := range fi.symlinks { |
| 522 | ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target) |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |