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 ( |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 18 | "encoding/json" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 19 | "fmt" |
Jooyung Han | 580eb4f | 2020-06-24 19:33:06 +0900 | [diff] [blame] | 20 | "path" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "runtime" |
| 23 | "sort" |
Nikita Ioffe | 5335bc4 | 2020-10-20 00:02:15 +0100 | [diff] [blame] | 24 | "strconv" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 25 | "strings" |
| 26 | |
| 27 | "android/soong/android" |
| 28 | "android/soong/java" |
| 29 | |
| 30 | "github.com/google/blueprint" |
| 31 | "github.com/google/blueprint/proptools" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | pctx = android.NewPackageContext("android/apex") |
| 36 | ) |
| 37 | |
| 38 | func init() { |
| 39 | pctx.Import("android/soong/android") |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 40 | pctx.Import("android/soong/cc/config") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 41 | pctx.Import("android/soong/java") |
| 42 | pctx.HostBinToolVariable("apexer", "apexer") |
| 43 | // ART minimal builds (using the master-art manifest) do not have the "frameworks/base" |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 44 | // projects, and hence cannot build 'aapt2'. Use the SDK prebuilt instead. |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 45 | hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) { |
| 46 | pctx.VariableFunc(name, func(ctx android.PackageVarContext) string { |
| 47 | if !ctx.Config().FrameworksBaseDirExists(ctx) { |
| 48 | return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool) |
| 49 | } else { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 50 | return ctx.Config().HostToolPath(ctx, tool).String() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 51 | } |
| 52 | }) |
| 53 | } |
| 54 | hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2") |
| 55 | pctx.HostBinToolVariable("avbtool", "avbtool") |
| 56 | pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid") |
| 57 | pctx.HostBinToolVariable("merge_zips", "merge_zips") |
| 58 | pctx.HostBinToolVariable("mke2fs", "mke2fs") |
| 59 | pctx.HostBinToolVariable("resize2fs", "resize2fs") |
| 60 | pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile") |
| 61 | pctx.HostBinToolVariable("soong_zip", "soong_zip") |
| 62 | pctx.HostBinToolVariable("zip2zip", "zip2zip") |
| 63 | pctx.HostBinToolVariable("zipalign", "zipalign") |
| 64 | pctx.HostBinToolVariable("jsonmodify", "jsonmodify") |
| 65 | pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest") |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 66 | pctx.HostBinToolVariable("extract_apks", "extract_apks") |
Theotime Combes | 4ba38c1 | 2020-06-12 12:46:59 +0000 | [diff] [blame] | 67 | pctx.HostBinToolVariable("make_f2fs", "make_f2fs") |
| 68 | pctx.HostBinToolVariable("sload_f2fs", "sload_f2fs") |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 69 | pctx.HostBinToolVariable("apex_compression_tool", "apex_compression_tool") |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 70 | pctx.SourcePathVariable("genNdkUsedbyApexPath", "build/soong/scripts/gen_ndk_usedby_apex.sh") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | var ( |
| 74 | // Create a canned fs config file where all files and directories are |
| 75 | // by default set to (uid/gid/mode) = (1000/1000/0644) |
| 76 | // TODO(b/113082813) make this configurable using config.fs syntax |
| 77 | generateFsConfig = pctx.StaticRule("generateFsConfig", blueprint.RuleParams{ |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 78 | Command: `( echo '/ 1000 1000 0755' ` + |
| 79 | `&& for i in ${ro_paths}; do echo "/$$i 1000 1000 0644"; done ` + |
| 80 | `&& for i in ${exec_paths}; do echo "/$$i 0 2000 0755"; done ` + |
| 81 | `&& ( tr ' ' '\n' <${out}.apklist | for i in ${apk_paths}; do read apk; echo "/$$i 0 2000 0755"; zipinfo -1 $$apk | sed "s:\(.*\):/$$i/\1 1000 1000 0644:"; done ) ) > ${out}`, |
| 82 | Description: "fs_config ${out}", |
| 83 | Rspfile: "$out.apklist", |
| 84 | RspfileContent: "$in", |
| 85 | }, "ro_paths", "exec_paths", "apk_paths") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 86 | |
| 87 | apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{ |
| 88 | Command: `rm -f $out && ${jsonmodify} $in ` + |
| 89 | `-a provideNativeLibs ${provideNativeLibs} ` + |
| 90 | `-a requireNativeLibs ${requireNativeLibs} ` + |
| 91 | `${opt} ` + |
| 92 | `-o $out`, |
| 93 | CommandDeps: []string{"${jsonmodify}"}, |
| 94 | Description: "prepare ${out}", |
| 95 | }, "provideNativeLibs", "requireNativeLibs", "opt") |
| 96 | |
| 97 | stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{ |
| 98 | Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`, |
| 99 | CommandDeps: []string{"${conv_apex_manifest}"}, |
| 100 | Description: "strip ${in}=>${out}", |
| 101 | }) |
| 102 | |
| 103 | pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{ |
| 104 | Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`, |
| 105 | CommandDeps: []string{"${conv_apex_manifest}"}, |
| 106 | Description: "convert ${in}=>${out}", |
| 107 | }) |
| 108 | |
| 109 | // TODO(b/113233103): make sure that file_contexts is sane, i.e., validate |
| 110 | // against the binary policy using sefcontext_compiler -p <policy>. |
| 111 | |
| 112 | // TODO(b/114327326): automate the generation of file_contexts |
| 113 | apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{ |
| 114 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 115 | `(. ${out}.copy_commands) && ` + |
| 116 | `APEXER_TOOL_PATH=${tool_path} ` + |
| 117 | `${apexer} --force --manifest ${manifest} ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 118 | `--file_contexts ${file_contexts} ` + |
| 119 | `--canned_fs_config ${canned_fs_config} ` + |
Dario Freni | 0f4ae07 | 2020-01-02 15:24:12 +0000 | [diff] [blame] | 120 | `--include_build_info ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 121 | `--payload_type image ` + |
| 122 | `--key ${key} ${opt_flags} ${image_dir} ${out} `, |
| 123 | CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}", |
Theotime Combes | 4ba38c1 | 2020-06-12 12:46:59 +0000 | [diff] [blame] | 124 | "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}", "${sload_f2fs}", |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 125 | "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"}, |
| 126 | Rspfile: "${out}.copy_commands", |
| 127 | RspfileContent: "${copy_commands}", |
| 128 | Description: "APEX ${image_dir} => ${out}", |
Theotime Combes | 4ba38c1 | 2020-06-12 12:46:59 +0000 | [diff] [blame] | 129 | }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key", "opt_flags", "manifest", "payload_fs_type") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 130 | |
| 131 | zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{ |
| 132 | Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` + |
| 133 | `(. ${out}.copy_commands) && ` + |
| 134 | `APEXER_TOOL_PATH=${tool_path} ` + |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 135 | `${apexer} --force --manifest ${manifest} ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 136 | `--payload_type zip ` + |
| 137 | `${image_dir} ${out} `, |
| 138 | CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"}, |
| 139 | Rspfile: "${out}.copy_commands", |
| 140 | RspfileContent: "${copy_commands}", |
| 141 | Description: "ZipAPEX ${image_dir} => ${out}", |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 142 | }, "tool_path", "image_dir", "copy_commands", "manifest") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 143 | |
| 144 | apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule", |
| 145 | blueprint.RuleParams{ |
| 146 | Command: `${aapt2} convert --output-format proto $in -o $out`, |
| 147 | CommandDeps: []string{"${aapt2}"}, |
| 148 | }) |
| 149 | |
| 150 | apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{ |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 151 | Command: `${zip2zip} -i $in -o $out.base ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 152 | `apex_payload.img:apex/${abi}.img ` + |
Dario Freni | da1aefe | 2020-03-02 21:47:09 +0000 | [diff] [blame] | 153 | `apex_build_info.pb:apex/${abi}.build_info.pb ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 154 | `apex_manifest.json:root/apex_manifest.json ` + |
Jiyong Park | 53ae334 | 2019-12-08 02:06:24 +0900 | [diff] [blame] | 155 | `apex_manifest.pb:root/apex_manifest.pb ` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 156 | `AndroidManifest.xml:manifest/AndroidManifest.xml ` + |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 157 | `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` + |
| 158 | `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` + |
| 159 | `${merge_zips} $out $out.base $out.config`, |
| 160 | CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"}, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 161 | Description: "app bundle", |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 162 | }, "abi", "config") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 163 | |
| 164 | emitApexContentRule = pctx.StaticRule("emitApexContentRule", blueprint.RuleParams{ |
| 165 | Command: `rm -f ${out} && touch ${out} && (. ${out}.emit_commands)`, |
| 166 | Rspfile: "${out}.emit_commands", |
| 167 | RspfileContent: "${emit_commands}", |
| 168 | Description: "Emit APEX image content", |
| 169 | }, "emit_commands") |
| 170 | |
| 171 | diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{ |
| 172 | Command: `diff --unchanged-group-format='' \` + |
| 173 | `--changed-group-format='%<' \` + |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 174 | `${image_content_file} ${allowed_files_file} || (` + |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 175 | `echo -e "New unexpected files were added to ${apex_module_name}." ` + |
| 176 | ` "To fix the build run following command:" && ` + |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 177 | `echo "system/apex/tools/update_allowed_list.sh ${allowed_files_file} ${image_content_file}" && ` + |
Dan Willemsen | 81e43c5 | 2020-01-28 15:40:19 -0800 | [diff] [blame] | 178 | `exit 1); touch ${out}`, |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 179 | Description: "Diff ${image_content_file} and ${allowed_files_file}", |
| 180 | }, "image_content_file", "allowed_files_file", "apex_module_name") |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 181 | |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 182 | generateAPIsUsedbyApexRule = pctx.StaticRule("generateAPIsUsedbyApexRule", blueprint.RuleParams{ |
| 183 | Command: "$genNdkUsedbyApexPath ${image_dir} ${readelf} ${out}", |
| 184 | CommandDeps: []string{"${genNdkUsedbyApexPath}"}, |
| 185 | Description: "Generate symbol list used by Apex", |
| 186 | }, "image_dir", "readelf") |
| 187 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 188 | // Don't add more rules here. Consider using android.NewRuleBuilder instead. |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 189 | ) |
| 190 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 191 | // buildManifest creates buile rules to modify the input apex_manifest.json to add information |
| 192 | // gathered by the build system such as provided/required native libraries. Two output files having |
| 193 | // different formats are generated. a.manifestJsonOut is JSON format for Q devices, and |
| 194 | // a.manifest.PbOut is protobuf format for R+ devices. |
| 195 | // TODO(jiyong): make this to return paths instead of directly storing the paths to apexBundle |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 196 | func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 197 | src := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json")) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 198 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 199 | // Put dependency({provide|require}NativeLibs) in apex_manifest.json |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 200 | provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs) |
| 201 | requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs)) |
| 202 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 203 | // APEX name can be overridden |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 204 | optCommands := []string{} |
| 205 | if a.properties.Apex_name != nil { |
| 206 | optCommands = append(optCommands, "-v name "+*a.properties.Apex_name) |
| 207 | } |
| 208 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 209 | // Collect jniLibs. Notice that a.filesInfo is already sorted |
Jooyung Han | 643adc4 | 2020-02-27 13:50:06 +0900 | [diff] [blame] | 210 | var jniLibs []string |
| 211 | for _, fi := range a.filesInfo { |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 212 | if fi.isJniLib && !android.InList(fi.stem(), jniLibs) { |
| 213 | jniLibs = append(jniLibs, fi.stem()) |
Jooyung Han | 643adc4 | 2020-02-27 13:50:06 +0900 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | if len(jniLibs) > 0 { |
| 217 | optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " ")) |
| 218 | } |
| 219 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 220 | manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 221 | ctx.Build(pctx, android.BuildParams{ |
| 222 | Rule: apexManifestRule, |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 223 | Input: src, |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 224 | Output: manifestJsonFullOut, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 225 | Args: map[string]string{ |
| 226 | "provideNativeLibs": strings.Join(provideNativeLibs, " "), |
| 227 | "requireNativeLibs": strings.Join(requireNativeLibs, " "), |
| 228 | "opt": strings.Join(optCommands, " "), |
| 229 | }, |
| 230 | }) |
| 231 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 232 | // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json prepare |
| 233 | // stripped-down version so that APEX modules built from R+ can be installed to Q |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 234 | minSdkVersion := a.minSdkVersion(ctx) |
| 235 | if minSdkVersion.EqualTo(android.SdkVersion_Android10) { |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 236 | a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json") |
| 237 | ctx.Build(pctx, android.BuildParams{ |
| 238 | Rule: stripApexManifestRule, |
| 239 | Input: manifestJsonFullOut, |
| 240 | Output: a.manifestJsonOut, |
| 241 | }) |
| 242 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 243 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 244 | // From R+, protobuf binary format (.pb) is the standard format for apex_manifest |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 245 | a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb") |
| 246 | ctx.Build(pctx, android.BuildParams{ |
| 247 | Rule: pbApexManifestRule, |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 248 | Input: manifestJsonFullOut, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 249 | Output: a.manifestPbOut, |
| 250 | }) |
| 251 | } |
| 252 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 253 | // buildFileContexts create build rules to append an entry for apex_manifest.pb to the file_contexts |
| 254 | // file for this APEX which is either from /systme/sepolicy/apex/<apexname>-file_contexts or from |
| 255 | // the file_contexts property of this APEX. This is to make sure that the manifest file is correctly |
| 256 | // labeled as system_file. |
| 257 | func (a *apexBundle) buildFileContexts(ctx android.ModuleContext) android.OutputPath { |
Jooyung Han | 580eb4f | 2020-06-24 19:33:06 +0900 | [diff] [blame] | 258 | var fileContexts android.Path |
| 259 | if a.properties.File_contexts == nil { |
| 260 | fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts") |
| 261 | } else { |
| 262 | fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts) |
| 263 | } |
| 264 | if a.Platform() { |
| 265 | if matched, err := path.Match("system/sepolicy/**/*", fileContexts.String()); err != nil || !matched { |
| 266 | ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but %q", fileContexts) |
Jooyung Han | 580eb4f | 2020-06-24 19:33:06 +0900 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | if !android.ExistentPathForSource(ctx, fileContexts.String()).Valid() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 270 | ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", fileContexts.String()) |
Jooyung Han | 580eb4f | 2020-06-24 19:33:06 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | output := android.PathForModuleOut(ctx, "file_contexts") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 274 | rule := android.NewRuleBuilder(pctx, ctx) |
Jooyung Han | 7f146c0 | 2020-09-23 19:15:55 +0900 | [diff] [blame] | 275 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 276 | switch a.properties.ApexType { |
| 277 | case imageApex: |
Jooyung Han | 7f146c0 | 2020-09-23 19:15:55 +0900 | [diff] [blame] | 278 | // remove old file |
| 279 | rule.Command().Text("rm").FlagWithOutput("-f ", output) |
| 280 | // copy file_contexts |
| 281 | rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output) |
| 282 | // new line |
| 283 | rule.Command().Text("echo").Text(">>").Output(output) |
| 284 | // force-label /apex_manifest.pb and / as system_file so that apexd can read them |
| 285 | rule.Command().Text("echo").Flag("/apex_manifest\\\\.pb u:object_r:system_file:s0").Text(">>").Output(output) |
| 286 | rule.Command().Text("echo").Flag("/ u:object_r:system_file:s0").Text(">>").Output(output) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 287 | case flattenedApex: |
Jooyung Han | 7f146c0 | 2020-09-23 19:15:55 +0900 | [diff] [blame] | 288 | // For flattened apexes, install path should be prepended. |
| 289 | // File_contexts file should be emiited to make via LOCAL_FILE_CONTEXTS |
| 290 | // so that it can be merged into file_contexts.bin |
| 291 | apexPath := android.InstallPathToOnDevicePath(ctx, a.installDir.Join(ctx, a.Name())) |
| 292 | apexPath = strings.ReplaceAll(apexPath, ".", `\\.`) |
| 293 | // remove old file |
| 294 | rule.Command().Text("rm").FlagWithOutput("-f ", output) |
| 295 | // copy file_contexts |
| 296 | rule.Command().Text("awk").Text(`'/object_r/{printf("` + apexPath + `%s\n", $0)}'`).Input(fileContexts).Text(">").Output(output) |
| 297 | // new line |
| 298 | rule.Command().Text("echo").Text(">>").Output(output) |
| 299 | // force-label /apex_manifest.pb and / as system_file so that apexd can read them |
| 300 | rule.Command().Text("echo").Flag(apexPath + `/apex_manifest\\.pb u:object_r:system_file:s0`).Text(">>").Output(output) |
| 301 | rule.Command().Text("echo").Flag(apexPath + "/ u:object_r:system_file:s0").Text(">>").Output(output) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 302 | default: |
| 303 | panic(fmt.Errorf("unsupported type %v", a.properties.ApexType)) |
Jooyung Han | 7f146c0 | 2020-09-23 19:15:55 +0900 | [diff] [blame] | 304 | } |
| 305 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 306 | rule.Build("file_contexts."+a.Name(), "Generate file_contexts") |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 307 | return output.OutputPath |
Jooyung Han | 580eb4f | 2020-06-24 19:33:06 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 310 | // buildNoticeFiles creates a buile rule for aggregating notice files from the modules that |
| 311 | // contributes to this APEX. The notice files are merged into a big notice file. |
Jiyong Park | 19972c7 | 2020-01-28 20:05:29 +0900 | [diff] [blame] | 312 | func (a *apexBundle) buildNoticeFiles(ctx android.ModuleContext, apexFileName string) android.NoticeOutputs { |
Jiyong Park | 9918e1a | 2020-03-17 19:16:40 +0900 | [diff] [blame] | 313 | var noticeFiles android.Paths |
| 314 | |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 315 | a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool { |
Jiyong Park | 9918e1a | 2020-03-17 19:16:40 +0900 | [diff] [blame] | 316 | if externalDep { |
Paul Duffin | be5a5be | 2020-03-30 15:54:08 +0100 | [diff] [blame] | 317 | // As soon as the dependency graph crosses the APEX boundary, don't go further. |
| 318 | return false |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 319 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 320 | noticeFiles = append(noticeFiles, to.NoticeFiles()...) |
Paul Duffin | be5a5be | 2020-03-30 15:54:08 +0100 | [diff] [blame] | 321 | return true |
Jiyong Park | 9918e1a | 2020-03-17 19:16:40 +0900 | [diff] [blame] | 322 | }) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 323 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 324 | // TODO(jiyong): why do we need this? WalkPayloadDeps should have already covered this. |
Jiyong Park | 41f637d | 2020-09-09 13:18:02 +0900 | [diff] [blame] | 325 | for _, fi := range a.filesInfo { |
| 326 | noticeFiles = append(noticeFiles, fi.noticeFiles...) |
| 327 | } |
| 328 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 329 | if len(noticeFiles) == 0 { |
Jiyong Park | 19972c7 | 2020-01-28 20:05:29 +0900 | [diff] [blame] | 330 | return android.NoticeOutputs{} |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 331 | } |
| 332 | |
Jiyong Park | 33c7736 | 2020-05-29 22:00:16 +0900 | [diff] [blame] | 333 | return android.BuildNoticeOutput(ctx, a.installDir, apexFileName, android.SortedUniquePaths(noticeFiles)) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 334 | } |
| 335 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 336 | // buildInstalledFilesFile creates a build rule for the installed-files.txt file where the list of |
| 337 | // files included in this APEX is shown. The text file is dist'ed so that people can see what's |
| 338 | // included in the APEX without actually downloading and extracting it. |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 339 | func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath { |
| 340 | output := android.PathForModuleOut(ctx, "installed-files.txt") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 341 | rule := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 342 | rule.Command(). |
| 343 | Implicit(builtApex). |
| 344 | Text("(cd " + imageDir.String() + " ; "). |
Jiyong Park | bd63a10 | 2020-02-08 12:40:05 +0900 | [diff] [blame] | 345 | Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") "). |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 346 | Text(" | sort -nr > "). |
| 347 | Output(output) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 348 | rule.Build("installed-files."+a.Name(), "Installed files") |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 349 | return output.OutputPath |
| 350 | } |
| 351 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 352 | // buildBundleConfig creates a build rule for the bundle config file that will control the bundle |
| 353 | // creation process. |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 354 | func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath { |
| 355 | output := android.PathForModuleOut(ctx, "bundle_config.json") |
| 356 | |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 357 | type ApkConfig struct { |
| 358 | Package_name string `json:"package_name"` |
| 359 | Apk_path string `json:"path"` |
| 360 | } |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 361 | config := struct { |
| 362 | Compression struct { |
| 363 | Uncompressed_glob []string `json:"uncompressed_glob"` |
| 364 | } `json:"compression"` |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 365 | Apex_config struct { |
| 366 | Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"` |
| 367 | } `json:"apex_config,omitempty"` |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 368 | }{} |
| 369 | |
| 370 | config.Compression.Uncompressed_glob = []string{ |
| 371 | "apex_payload.img", |
| 372 | "apex_manifest.*", |
| 373 | } |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 374 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 375 | // Collect the manifest names and paths of android apps if their manifest names are |
| 376 | // overridden. |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 377 | for _, fi := range a.filesInfo { |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 378 | if fi.class != app && fi.class != appSet { |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 379 | continue |
| 380 | } |
| 381 | packageName := fi.overriddenPackageName |
| 382 | if packageName != "" { |
| 383 | config.Apex_config.Apex_embedded_apk_config = append( |
| 384 | config.Apex_config.Apex_embedded_apk_config, |
| 385 | ApkConfig{ |
| 386 | Package_name: packageName, |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 387 | Apk_path: fi.path(), |
Jiyong Park | cfaa164 | 2020-02-28 16:51:07 +0900 | [diff] [blame] | 388 | }) |
| 389 | } |
| 390 | } |
| 391 | |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 392 | j, err := json.Marshal(config) |
| 393 | if err != nil { |
| 394 | panic(fmt.Errorf("error while marshalling to %q: %#v", output, err)) |
| 395 | } |
| 396 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 397 | android.WriteFileRule(ctx, output, string(j)) |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 398 | |
| 399 | return output.OutputPath |
| 400 | } |
| 401 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 402 | // buildUnflattendApex creates build rules to build an APEX using apexer. |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 403 | func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 404 | apexType := a.properties.ApexType |
| 405 | suffix := apexType.suffix() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 406 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 407 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 408 | // Step 1: copy built files to appropriate directories under the image directory |
| 409 | |
| 410 | imageDir := android.PathForModuleOut(ctx, "image"+suffix) |
| 411 | |
| 412 | // TODO(jiyong): use the RuleBuilder |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 413 | var copyCommands []string |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 414 | var implicitInputs []android.Path |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 415 | for _, fi := range a.filesInfo { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 416 | destPath := imageDir.Join(ctx, fi.path()).String() |
| 417 | |
| 418 | // Prepare the destination path |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 419 | destPathDir := filepath.Dir(destPath) |
| 420 | if fi.class == appSet { |
| 421 | copyCommands = append(copyCommands, "rm -rf "+destPathDir) |
| 422 | } |
| 423 | copyCommands = append(copyCommands, "mkdir -p "+destPathDir) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 424 | |
| 425 | // Copy the built file to the directory. But if the symlink optimization is turned |
| 426 | // on, place a symlink to the corresponding file in /system partition instead. |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 427 | if a.linkToSystemLib && fi.transitiveDep && fi.availableToPlatform() { |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 428 | // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 429 | pathOnDevice := filepath.Join("/system", fi.path()) |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 430 | copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath) |
| 431 | } else { |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 432 | if fi.class == appSet { |
| 433 | copyCommands = append(copyCommands, |
Colin Cross | d783bbb | 2020-07-11 22:30:45 -0700 | [diff] [blame] | 434 | fmt.Sprintf("unzip -qDD -d %s %s", destPathDir, fi.builtFile.String())) |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 435 | } else { |
| 436 | copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath) |
| 437 | } |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 438 | implicitInputs = append(implicitInputs, fi.builtFile) |
| 439 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 440 | |
| 441 | // Create additional symlinks pointing the file inside the APEX (if any). Note that |
| 442 | // this is independent from the symlink optimization. |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 443 | for _, symlinkPath := range fi.symlinkPaths() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 444 | symlinkDest := imageDir.Join(ctx, symlinkPath).String() |
Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 445 | copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest) |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 446 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 447 | |
| 448 | // Copy the test files (if any) |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 449 | for _, d := range fi.dataPaths { |
| 450 | // TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 451 | relPath := d.SrcPath.Rel() |
| 452 | dataPath := d.SrcPath.String() |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 453 | if !strings.HasSuffix(dataPath, relPath) { |
| 454 | panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath)) |
| 455 | } |
| 456 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 457 | dataDest := imageDir.Join(ctx, fi.apexRelativePath(relPath), d.RelativeInstallPath).String() |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 458 | |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 459 | copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest) |
| 460 | implicitInputs = append(implicitInputs, d.SrcPath) |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 461 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 462 | } |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 463 | implicitInputs = append(implicitInputs, a.manifestPbOut) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 464 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 465 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 466 | // Step 1.a: Write the list of files in this APEX to a txt file and compare it against |
| 467 | // the allowed list given via the allowed_files property. Build fails when the two lists |
| 468 | // differ. |
| 469 | // |
| 470 | // TODO(jiyong): consider removing this. Nobody other than com.android.apex.cts.shim.* seems |
| 471 | // to be using this at this moment. Furthermore, this looks very similar to what |
| 472 | // buildInstalledFilesFile does. At least, move this to somewhere else so that this doesn't |
| 473 | // hurt readability. |
| 474 | // TODO(jiyong): use RuleBuilder |
Jooyung Han | 938b593 | 2020-06-20 12:47:47 +0900 | [diff] [blame] | 475 | if a.overridableProperties.Allowed_files != nil { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 476 | // Build content.txt |
| 477 | var emitCommands []string |
| 478 | imageContentFile := android.PathForModuleOut(ctx, "content.txt") |
| 479 | emitCommands = append(emitCommands, "echo ./apex_manifest.pb >> "+imageContentFile.String()) |
| 480 | minSdkVersion := a.minSdkVersion(ctx) |
| 481 | if minSdkVersion.EqualTo(android.SdkVersion_Android10) { |
| 482 | emitCommands = append(emitCommands, "echo ./apex_manifest.json >> "+imageContentFile.String()) |
| 483 | } |
| 484 | for _, fi := range a.filesInfo { |
| 485 | emitCommands = append(emitCommands, "echo './"+fi.path()+"' >> "+imageContentFile.String()) |
| 486 | } |
| 487 | emitCommands = append(emitCommands, "sort -o "+imageContentFile.String()+" "+imageContentFile.String()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 488 | ctx.Build(pctx, android.BuildParams{ |
| 489 | Rule: emitApexContentRule, |
| 490 | Implicits: implicitInputs, |
| 491 | Output: imageContentFile, |
| 492 | Description: "emit apex image content", |
| 493 | Args: map[string]string{ |
| 494 | "emit_commands": strings.Join(emitCommands, " && "), |
| 495 | }, |
| 496 | }) |
| 497 | implicitInputs = append(implicitInputs, imageContentFile) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 498 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 499 | // Compare content.txt against allowed_files. |
| 500 | allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files)) |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 501 | phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 502 | ctx.Build(pctx, android.BuildParams{ |
| 503 | Rule: diffApexContentRule, |
| 504 | Implicits: implicitInputs, |
| 505 | Output: phonyOutput, |
| 506 | Description: "diff apex image content", |
| 507 | Args: map[string]string{ |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 508 | "allowed_files_file": allowedFilesFile.String(), |
| 509 | "image_content_file": imageContentFile.String(), |
| 510 | "apex_module_name": a.Name(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 511 | }, |
| 512 | }) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 513 | implicitInputs = append(implicitInputs, phonyOutput) |
| 514 | } |
| 515 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 516 | unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 517 | outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String() |
| 518 | prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin") |
| 519 | |
Nikita Ioffe | bc03588 | 2021-04-14 21:35:24 +0100 | [diff] [blame] | 520 | // Figure out if need to compress apex. |
Nikita Ioffe | b6ea6c2 | 2021-04-19 13:07:24 +0100 | [diff] [blame] | 521 | compressionEnabled := ctx.Config().CompressedApex() && proptools.BoolDefault(a.properties.Compressible, false) && !a.testApex && !ctx.Config().UnbundledBuildApps() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 522 | if apexType == imageApex { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 523 | //////////////////////////////////////////////////////////////////////////////////// |
| 524 | // Step 2: create canned_fs_config which encodes filemode,uid,gid of each files |
| 525 | // in this APEX. The file will be used by apexer in later steps. |
| 526 | // TODO(jiyong): make this as a function |
| 527 | // TODO(jiyong): use the RuleBuilder |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 528 | var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"} |
| 529 | var executablePaths []string // this also includes dirs |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 530 | var extractedAppSetPaths android.Paths |
| 531 | var extractedAppSetDirs []string |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 532 | for _, f := range a.filesInfo { |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 533 | pathInApex := f.path() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 534 | if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") { |
| 535 | executablePaths = append(executablePaths, pathInApex) |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 536 | for _, d := range f.dataPaths { |
Liz Kammer | 0a51aa2 | 2020-07-21 11:13:17 -0700 | [diff] [blame] | 537 | readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel())) |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 538 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 539 | for _, s := range f.symlinks { |
| 540 | executablePaths = append(executablePaths, filepath.Join(f.installDir, s)) |
| 541 | } |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 542 | } else if f.class == appSet { |
| 543 | extractedAppSetPaths = append(extractedAppSetPaths, f.builtFile) |
| 544 | extractedAppSetDirs = append(extractedAppSetDirs, f.installDir) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 545 | } else { |
| 546 | readOnlyPaths = append(readOnlyPaths, pathInApex) |
| 547 | } |
| 548 | dir := f.installDir |
| 549 | for !android.InList(dir, executablePaths) && dir != "" { |
| 550 | executablePaths = append(executablePaths, dir) |
| 551 | dir, _ = filepath.Split(dir) // move up to the parent |
| 552 | if len(dir) > 0 { |
| 553 | // remove trailing slash |
| 554 | dir = dir[:len(dir)-1] |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | sort.Strings(readOnlyPaths) |
| 559 | sort.Strings(executablePaths) |
| 560 | cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config") |
| 561 | ctx.Build(pctx, android.BuildParams{ |
| 562 | Rule: generateFsConfig, |
| 563 | Output: cannedFsConfig, |
| 564 | Description: "generate fs config", |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 565 | Inputs: extractedAppSetPaths, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 566 | Args: map[string]string{ |
| 567 | "ro_paths": strings.Join(readOnlyPaths, " "), |
| 568 | "exec_paths": strings.Join(executablePaths, " "), |
Sasha Smundak | 18d98bc | 2020-05-27 16:36:07 -0700 | [diff] [blame] | 569 | "apk_paths": strings.Join(extractedAppSetDirs, " "), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 570 | }, |
| 571 | }) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 572 | implicitInputs = append(implicitInputs, cannedFsConfig) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 573 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 574 | //////////////////////////////////////////////////////////////////////////////////// |
| 575 | // Step 3: Prepare option flags for apexer and invoke it to create an unsigned APEX. |
| 576 | // TODO(jiyong): use the RuleBuilder |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 577 | optFlags := []string{} |
| 578 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 579 | fileContexts := a.buildFileContexts(ctx) |
| 580 | implicitInputs = append(implicitInputs, fileContexts) |
| 581 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 582 | implicitInputs = append(implicitInputs, a.privateKeyFile, a.publicKeyFile) |
| 583 | optFlags = append(optFlags, "--pubkey "+a.publicKeyFile.String()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 584 | |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 585 | manifestPackageName := a.getOverrideManifestPackageName(ctx) |
| 586 | if manifestPackageName != "" { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 587 | optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName) |
| 588 | } |
| 589 | |
| 590 | if a.properties.AndroidManifest != nil { |
| 591 | androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest)) |
| 592 | implicitInputs = append(implicitInputs, androidManifestFile) |
| 593 | optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String()) |
| 594 | } |
| 595 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 596 | // Determine target/min sdk version from the context |
| 597 | // TODO(jiyong): make this as a function |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 598 | moduleMinSdkVersion := a.minSdkVersion(ctx) |
Nikita Ioffe | 5335bc4 | 2020-10-20 00:02:15 +0100 | [diff] [blame] | 599 | minSdkVersion := moduleMinSdkVersion.String() |
| 600 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 601 | // bundletool doesn't understand what "current" is. We need to transform it to |
| 602 | // codename |
Jooyung Han | ed124c3 | 2021-01-26 11:43:46 +0900 | [diff] [blame] | 603 | if moduleMinSdkVersion.IsCurrent() || moduleMinSdkVersion.IsNone() { |
Nikita Ioffe | 5335bc4 | 2020-10-20 00:02:15 +0100 | [diff] [blame] | 604 | minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String() |
Liz Kammer | 4854a7d | 2021-05-27 14:28:27 -0400 | [diff] [blame^] | 605 | |
| 606 | if java.UseApiFingerprint(ctx) { |
| 607 | minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String()) |
| 608 | implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx)) |
| 609 | } |
Nikita Ioffe | 5d600c9 | 2020-02-20 00:43:27 +0000 | [diff] [blame] | 610 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 611 | // apex module doesn't have a concept of target_sdk_version, hence for the time |
| 612 | // being targetSdkVersion == default targetSdkVersion of the branch. |
Nikita Ioffe | 5335bc4 | 2020-10-20 00:02:15 +0100 | [diff] [blame] | 613 | targetSdkVersion := strconv.Itoa(ctx.Config().DefaultAppTargetSdk(ctx).FinalOrFutureInt()) |
Nikita Ioffe | 5d600c9 | 2020-02-20 00:43:27 +0000 | [diff] [blame] | 614 | |
Nikita Ioffe | 1f4f345 | 2020-03-02 16:58:11 +0000 | [diff] [blame] | 615 | if java.UseApiFingerprint(ctx) { |
| 616 | targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String()) |
Baligh Uddin | f620137 | 2020-01-24 23:15:44 +0000 | [diff] [blame] | 617 | implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx)) |
| 618 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 619 | optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion) |
Baligh Uddin | f620137 | 2020-01-24 23:15:44 +0000 | [diff] [blame] | 620 | optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 621 | |
Baligh Uddin | 004d717 | 2020-02-19 21:29:28 -0800 | [diff] [blame] | 622 | if a.overridableProperties.Logging_parent != "" { |
| 623 | optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent) |
| 624 | } |
| 625 | |
Jiyong Park | 19972c7 | 2020-01-28 20:05:29 +0900 | [diff] [blame] | 626 | a.mergedNotices = a.buildNoticeFiles(ctx, a.Name()+suffix) |
| 627 | if a.mergedNotices.HtmlGzOutput.Valid() { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 628 | // If there's a NOTICE file, embed it as an asset file in the APEX. |
Jiyong Park | 19972c7 | 2020-01-28 20:05:29 +0900 | [diff] [blame] | 629 | implicitInputs = append(implicitInputs, a.mergedNotices.HtmlGzOutput.Path()) |
| 630 | optFlags = append(optFlags, "--assets_dir "+filepath.Dir(a.mergedNotices.HtmlGzOutput.String())) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 631 | } |
| 632 | |
Nikita Ioffe | b4b44c0 | 2020-01-02 23:01:39 +0000 | [diff] [blame] | 633 | 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] | 634 | ctx.PropertyErrorf("test_only_no_hashtree", "not available") |
| 635 | return |
| 636 | } |
Nikita Ioffe | bc03588 | 2021-04-14 21:35:24 +0100 | [diff] [blame] | 637 | if (moduleMinSdkVersion.GreaterThan(android.SdkVersion_Android10) || a.testOnlyShouldSkipHashtreeGeneration()) && !compressionEnabled { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 638 | // Apexes which are supposed to be installed in builtin dirs(/system, etc) |
| 639 | // don't need hashtree for activation. Therefore, by removing hashtree from |
| 640 | // apex bundle (filesystem image in it, to be specific), we can save storage. |
| 641 | optFlags = append(optFlags, "--no_hashtree") |
| 642 | } |
| 643 | |
Dario Freni | ca91339 | 2020-04-27 18:21:11 +0100 | [diff] [blame] | 644 | if a.testOnlyShouldSkipPayloadSign() { |
| 645 | optFlags = append(optFlags, "--unsigned_payload") |
| 646 | } |
| 647 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 648 | if a.properties.Apex_name != nil { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 649 | // If apex_name is set, apexer can skip checking if key name matches with |
| 650 | // apex name. Note that apex_manifest is also mended. |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 651 | optFlags = append(optFlags, "--do_not_check_keyname") |
| 652 | } |
| 653 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 654 | if moduleMinSdkVersion == android.SdkVersion_Android10 { |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 655 | implicitInputs = append(implicitInputs, a.manifestJsonOut) |
| 656 | optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String()) |
| 657 | } |
| 658 | |
Theotime Combes | 4ba38c1 | 2020-06-12 12:46:59 +0000 | [diff] [blame] | 659 | optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string()) |
| 660 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 661 | ctx.Build(pctx, android.BuildParams{ |
| 662 | Rule: apexRule, |
| 663 | Implicits: implicitInputs, |
| 664 | Output: unsignedOutputFile, |
| 665 | Description: "apex (" + apexType.name() + ")", |
| 666 | Args: map[string]string{ |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 667 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 668 | "image_dir": imageDir.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 669 | "copy_commands": strings.Join(copyCommands, " && "), |
| 670 | "manifest": a.manifestPbOut.String(), |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 671 | "file_contexts": fileContexts.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 672 | "canned_fs_config": cannedFsConfig.String(), |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 673 | "key": a.privateKeyFile.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 674 | "opt_flags": strings.Join(optFlags, " "), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 675 | }, |
| 676 | }) |
| 677 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 678 | // TODO(jiyong): make the two rules below as separate functions |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 679 | apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix) |
| 680 | bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 681 | a.bundleModuleFile = bundleModuleFile |
| 682 | |
| 683 | ctx.Build(pctx, android.BuildParams{ |
| 684 | Rule: apexProtoConvertRule, |
| 685 | Input: unsignedOutputFile, |
| 686 | Output: apexProtoFile, |
| 687 | Description: "apex proto convert", |
| 688 | }) |
| 689 | |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 690 | implicitInputs = append(implicitInputs, unsignedOutputFile) |
| 691 | |
| 692 | // Run coverage analysis |
sophiez | 6bde0b5 | 2021-01-09 01:03:42 +0000 | [diff] [blame] | 693 | apisUsedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_using.txt") |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 694 | ctx.Build(pctx, android.BuildParams{ |
| 695 | Rule: generateAPIsUsedbyApexRule, |
| 696 | Implicits: implicitInputs, |
| 697 | Description: "coverage", |
| 698 | Output: apisUsedbyOutputFile, |
| 699 | Args: map[string]string{ |
| 700 | "image_dir": imageDir.String(), |
| 701 | "readelf": "${config.ClangBin}/llvm-readelf", |
| 702 | }, |
| 703 | }) |
sophiez | 6bde0b5 | 2021-01-09 01:03:42 +0000 | [diff] [blame] | 704 | a.apisUsedByModuleFile = apisUsedbyOutputFile |
| 705 | |
Colin Cross | 69f0a24 | 2021-02-08 16:49:57 -0800 | [diff] [blame] | 706 | var libNames []string |
| 707 | for _, f := range a.filesInfo { |
| 708 | if f.class == nativeSharedLib { |
| 709 | libNames = append(libNames, f.stem()) |
| 710 | } |
| 711 | } |
sophiez | 6bde0b5 | 2021-01-09 01:03:42 +0000 | [diff] [blame] | 712 | apisBackedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_backing.txt") |
| 713 | ndkLibraryList := android.PathForSource(ctx, "system/core/rootdir/etc/public.libraries.android.txt") |
| 714 | rule := android.NewRuleBuilder(pctx, ctx) |
| 715 | rule.Command(). |
| 716 | Tool(android.PathForSource(ctx, "build/soong/scripts/gen_ndk_backedby_apex.sh")). |
sophiez | 6bde0b5 | 2021-01-09 01:03:42 +0000 | [diff] [blame] | 717 | Output(apisBackedbyOutputFile). |
Colin Cross | 69f0a24 | 2021-02-08 16:49:57 -0800 | [diff] [blame] | 718 | Input(ndkLibraryList). |
| 719 | Flags(libNames) |
sophiez | 6bde0b5 | 2021-01-09 01:03:42 +0000 | [diff] [blame] | 720 | rule.Build("ndk_backedby_list", "Generate API libraries backed by Apex") |
| 721 | a.apisBackedByModuleFile = apisBackedbyOutputFile |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 722 | |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 723 | bundleConfig := a.buildBundleConfig(ctx) |
| 724 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 725 | var abis []string |
| 726 | for _, target := range ctx.MultiTargets() { |
| 727 | if len(target.Arch.Abi) > 0 { |
| 728 | abis = append(abis, target.Arch.Abi[0]) |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | abis = android.FirstUniqueStrings(abis) |
| 733 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 734 | ctx.Build(pctx, android.BuildParams{ |
| 735 | Rule: apexBundleRule, |
| 736 | Input: apexProtoFile, |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 737 | Implicit: bundleConfig, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 738 | Output: a.bundleModuleFile, |
| 739 | Description: "apex bundle module", |
| 740 | Args: map[string]string{ |
Jiyong Park | bd15961 | 2020-02-28 15:22:21 +0900 | [diff] [blame] | 741 | "abi": strings.Join(abis, "."), |
| 742 | "config": bundleConfig.String(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 743 | }, |
| 744 | }) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 745 | } else { // zipApex |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 746 | ctx.Build(pctx, android.BuildParams{ |
| 747 | Rule: zipApexRule, |
| 748 | Implicits: implicitInputs, |
| 749 | Output: unsignedOutputFile, |
| 750 | Description: "apex (" + apexType.name() + ")", |
| 751 | Args: map[string]string{ |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 752 | "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir, |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 753 | "image_dir": imageDir.String(), |
Jooyung Han | 214bf37 | 2019-11-12 13:03:50 +0900 | [diff] [blame] | 754 | "copy_commands": strings.Join(copyCommands, " && "), |
| 755 | "manifest": a.manifestPbOut.String(), |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 756 | }, |
| 757 | }) |
| 758 | } |
| 759 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 760 | //////////////////////////////////////////////////////////////////////////////////// |
| 761 | // Step 4: Sign the APEX using signapk |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 762 | signedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 763 | |
| 764 | pem, key := a.getCertificateAndPrivateKey(ctx) |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 765 | rule := java.Signapk |
| 766 | args := map[string]string{ |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 767 | "certificates": pem.String() + " " + key.String(), |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 768 | "flags": "-a 4096", //alignment |
| 769 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 770 | implicits := android.Paths{pem, key} |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 771 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") { |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 772 | rule = java.SignapkRE |
| 773 | args["implicits"] = strings.Join(implicits.Strings(), ",") |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 774 | args["outCommaList"] = signedOutputFile.String() |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 775 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 776 | ctx.Build(pctx, android.BuildParams{ |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 777 | Rule: rule, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 778 | Description: "signapk", |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 779 | Output: signedOutputFile, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 780 | Input: unsignedOutputFile, |
Kousik Kumar | 309b1c0 | 2020-05-28 06:13:33 -0700 | [diff] [blame] | 781 | Implicits: implicits, |
| 782 | Args: args, |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 783 | }) |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 784 | a.outputFile = signedOutputFile |
| 785 | |
Mohammad Samiul Islam | a8008f9 | 2020-12-22 10:47:50 +0000 | [diff] [blame] | 786 | if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && a.testOnlyShouldForceCompression() { |
| 787 | ctx.PropertyErrorf("test_only_force_compression", "not available") |
| 788 | return |
| 789 | } |
Nikita Ioffe | bc03588 | 2021-04-14 21:35:24 +0100 | [diff] [blame] | 790 | |
Mohammad Samiul Islam | a8008f9 | 2020-12-22 10:47:50 +0000 | [diff] [blame] | 791 | if apexType == imageApex && (compressionEnabled || a.testOnlyShouldForceCompression()) { |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 792 | a.isCompressed = true |
| 793 | unsignedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+".capex.unsigned") |
| 794 | |
| 795 | compressRule := android.NewRuleBuilder(pctx, ctx) |
| 796 | compressRule.Command(). |
| 797 | Text("rm"). |
| 798 | FlagWithOutput("-f ", unsignedCompressedOutputFile) |
| 799 | compressRule.Command(). |
| 800 | BuiltTool("apex_compression_tool"). |
| 801 | Flag("compress"). |
| 802 | FlagWithArg("--apex_compression_tool ", outHostBinDir+":"+prebuiltSdkToolsBinDir). |
| 803 | FlagWithInput("--input ", signedOutputFile). |
| 804 | FlagWithOutput("--output ", unsignedCompressedOutputFile) |
| 805 | compressRule.Build("compressRule", "Generate unsigned compressed APEX file") |
| 806 | |
| 807 | signedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+".capex") |
Mohammad Samiul Islam | 9ac0e32 | 2021-01-19 11:32:29 +0000 | [diff] [blame] | 808 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") { |
| 809 | args["outCommaList"] = signedCompressedOutputFile.String() |
| 810 | } |
Mohammad Samiul Islam | 3cd005d | 2020-11-26 13:32:26 +0000 | [diff] [blame] | 811 | ctx.Build(pctx, android.BuildParams{ |
| 812 | Rule: rule, |
| 813 | Description: "sign compressedApex", |
| 814 | Output: signedCompressedOutputFile, |
| 815 | Input: unsignedCompressedOutputFile, |
| 816 | Implicits: implicits, |
| 817 | Args: args, |
| 818 | }) |
| 819 | a.outputFile = signedCompressedOutputFile |
| 820 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 821 | |
| 822 | // Install to $OUT/soong/{target,host}/.../apex |
| 823 | if a.installable() { |
Jaewoong Jung | 1670ca0 | 2019-11-22 14:50:42 -0800 | [diff] [blame] | 824 | ctx.InstallFile(a.installDir, a.Name()+suffix, a.outputFile) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 825 | } |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 826 | |
| 827 | // installed-files.txt is dist'ed |
| 828 | a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 829 | } |
| 830 | |
Jiyong Park | c0ec6f9 | 2020-11-19 23:00:52 +0900 | [diff] [blame] | 831 | // Context "decorator", overriding the InstallBypassMake method to always reply `true`. |
| 832 | type flattenedApexContext struct { |
| 833 | android.ModuleContext |
| 834 | } |
| 835 | |
| 836 | func (c *flattenedApexContext) InstallBypassMake() bool { |
| 837 | return true |
| 838 | } |
| 839 | |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 840 | // buildFlattenedApex creates rules for a flattened APEX. Flattened APEX actually doesn't have a |
| 841 | // single output file. It is a phony target for all the files under /system/apex/<name> directory. |
| 842 | // This function creates the installation rules for the files. |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 843 | func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 844 | bundleName := a.Name() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 845 | if a.installable() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 846 | for _, fi := range a.filesInfo { |
| 847 | dir := filepath.Join("apex", bundleName, fi.installDir) |
| 848 | target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.stem(), fi.builtFile) |
| 849 | for _, sym := range fi.symlinks { |
| 850 | ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 854 | |
| 855 | a.fileContexts = a.buildFileContexts(ctx) |
| 856 | |
| 857 | // Temporarily wrap the original `ctx` into a `flattenedApexContext` to have it reply true |
| 858 | // to `InstallBypassMake()` (thus making the call `android.PathForModuleInstall` below use |
| 859 | // `android.pathForInstallInMakeDir` instead of `android.PathForOutput`) to return the |
| 860 | // correct path to the flattened APEX (as its contents is installed by Make, not Soong). |
| 861 | // TODO(jiyong): Why do we need to set outputFile for flattened APEX? We don't seem to use |
| 862 | // it and it actually points to a path that can never be built. Remove this. |
| 863 | factx := flattenedApexContext{ctx} |
| 864 | a.outputFile = android.PathForModuleInstall(&factx, "apex", bundleName) |
| 865 | } |
| 866 | |
| 867 | // getCertificateAndPrivateKey retrieves the cert and the private key that will be used to sign |
| 868 | // the zip container of this APEX. See the description of the 'certificate' property for how |
| 869 | // the cert and the private key are found. |
| 870 | func (a *apexBundle) getCertificateAndPrivateKey(ctx android.PathContext) (pem, key android.Path) { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 871 | if a.containerCertificateFile != nil { |
| 872 | return a.containerCertificateFile, a.containerPrivateKeyFile |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 873 | } |
| 874 | |
Jaewoong Jung | 4cfdf7d | 2021-04-20 16:21:24 -0700 | [diff] [blame] | 875 | cert := String(a.overridableProperties.Certificate) |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 876 | if cert == "" { |
| 877 | return ctx.Config().DefaultAppCertificate(ctx) |
| 878 | } |
| 879 | |
| 880 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) |
| 881 | pem = defaultDir.Join(ctx, cert+".x509.pem") |
| 882 | key = defaultDir.Join(ctx, cert+".pk8") |
| 883 | return pem, key |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 884 | } |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 885 | |
| 886 | func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string { |
| 887 | // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES |
| 888 | // to see if it should be overridden because their <apex name> is dynamically generated |
| 889 | // according to its VNDK version. |
| 890 | if a.vndkApex { |
| 891 | overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName) |
| 892 | if overridden { |
| 893 | return strings.Replace(*a.properties.Apex_name, vndkApexName, overrideName, 1) |
| 894 | } |
| 895 | return "" |
| 896 | } |
Baligh Uddin | 5b57dba | 2020-03-15 13:01:05 -0700 | [diff] [blame] | 897 | if a.overridableProperties.Package_name != "" { |
| 898 | return a.overridableProperties.Package_name |
| 899 | } |
Jiyong Park | 20bacab | 2020-03-03 11:45:41 +0900 | [diff] [blame] | 900 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 901 | if overridden { |
| 902 | return manifestPackageName |
| 903 | } |
| 904 | return "" |
| 905 | } |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 906 | |
| 907 | func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) { |
| 908 | if !a.primaryApexType { |
| 909 | return |
| 910 | } |
| 911 | |
| 912 | if a.properties.IsCoverageVariant { |
| 913 | // Otherwise, we will have duplicated rules for coverage and |
| 914 | // non-coverage variants of the same APEX |
| 915 | return |
| 916 | } |
| 917 | |
| 918 | if ctx.Host() { |
| 919 | // No need to generate dependency info for host variant |
| 920 | return |
| 921 | } |
| 922 | |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 923 | depInfos := android.DepNameToDepInfoMap{} |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 924 | a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool { |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 925 | if from.Name() == to.Name() { |
| 926 | // This can happen for cc.reuseObjTag. We are not interested in tracking this. |
| 927 | // As soon as the dependency graph crosses the APEX boundary, don't go further. |
| 928 | return !externalDep |
Jiyong Park | 678c881 | 2020-02-07 17:25:49 +0900 | [diff] [blame] | 929 | } |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 930 | |
Artur Satayev | 533b98c | 2021-03-11 18:03:42 +0000 | [diff] [blame] | 931 | // Skip dependencies that are only available to APEXes; they are developed with updatability |
| 932 | // in mind and don't need manual approval. |
| 933 | if to.(android.ApexModule).NotAvailableForPlatform() { |
| 934 | return !externalDep |
| 935 | } |
| 936 | |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 937 | depTag := ctx.OtherModuleDependencyTag(to) |
Artur Satayev | 533b98c | 2021-03-11 18:03:42 +0000 | [diff] [blame] | 938 | // Check to see if dependency been marked to skip the dependency check |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 939 | if skipDepCheck, ok := depTag.(android.SkipApexAllowedDependenciesCheck); ok && skipDepCheck.SkipApexAllowedDependenciesCheck() { |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 940 | return !externalDep |
| 941 | } |
| 942 | |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 943 | if info, exists := depInfos[to.Name()]; exists { |
| 944 | if !android.InList(from.Name(), info.From) { |
| 945 | info.From = append(info.From, from.Name()) |
| 946 | } |
| 947 | info.IsExternal = info.IsExternal && externalDep |
| 948 | depInfos[to.Name()] = info |
| 949 | } else { |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 950 | toMinSdkVersion := "(no version)" |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 951 | if m, ok := to.(interface { |
| 952 | MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec |
| 953 | }); ok { |
| 954 | if v := m.MinSdkVersion(ctx); !v.ApiLevel.IsNone() { |
| 955 | toMinSdkVersion = v.ApiLevel.String() |
| 956 | } |
| 957 | } else if m, ok := to.(interface{ MinSdkVersion() string }); ok { |
| 958 | // TODO(b/175678607) eliminate the use of MinSdkVersion returning |
| 959 | // string |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 960 | if v := m.MinSdkVersion(); v != "" { |
| 961 | toMinSdkVersion = v |
| 962 | } |
| 963 | } |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 964 | depInfos[to.Name()] = android.ApexModuleDepInfo{ |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 965 | To: to.Name(), |
| 966 | From: []string{from.Name()}, |
| 967 | IsExternal: externalDep, |
| 968 | MinSdkVersion: toMinSdkVersion, |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | // As soon as the dependency graph crosses the APEX boundary, don't go further. |
| 973 | return !externalDep |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 974 | }) |
| 975 | |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 976 | a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, proptools.String(a.properties.Min_sdk_version), depInfos) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 977 | |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 978 | ctx.Build(pctx, android.BuildParams{ |
| 979 | Rule: android.Phony, |
| 980 | Output: android.PathForPhony(ctx, a.Name()+"-deps-info"), |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 981 | Inputs: []android.Path{ |
| 982 | a.ApexBundleDepsInfo.FullListPath(), |
| 983 | a.ApexBundleDepsInfo.FlatListPath(), |
| 984 | }, |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 985 | }) |
| 986 | } |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 987 | |
| 988 | func (a *apexBundle) buildLintReports(ctx android.ModuleContext) { |
| 989 | depSetsBuilder := java.NewLintDepSetBuilder() |
| 990 | for _, fi := range a.filesInfo { |
| 991 | depSetsBuilder.Transitive(fi.lintDepSets) |
| 992 | } |
| 993 | |
| 994 | a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build()) |
| 995 | } |