builder.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. package apex
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "path/filepath"
  19. "runtime"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. "android/soong/android"
  24. "android/soong/java"
  25. "github.com/google/blueprint"
  26. "github.com/google/blueprint/proptools"
  27. )
  28. var (
  29. pctx = android.NewPackageContext("android/apex")
  30. )
  31. func init() {
  32. pctx.Import("android/soong/android")
  33. pctx.Import("android/soong/cc/config")
  34. pctx.Import("android/soong/java")
  35. pctx.HostBinToolVariable("apexer", "apexer")
  36. pctx.HostBinToolVariable("apexer_with_DCLA_preprocessing", "apexer_with_DCLA_preprocessing")
  37. pctx.HostBinToolVariable("apexer_with_trim_preprocessing", "apexer_with_trim_preprocessing")
  38. // ART minimal builds (using the master-art manifest) do not have the "frameworks/base"
  39. // projects, and hence cannot build 'aapt2'. Use the SDK prebuilt instead.
  40. hostBinToolVariableWithPrebuilt := func(name, prebuiltDir, tool string) {
  41. pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
  42. if !ctx.Config().FrameworksBaseDirExists(ctx) {
  43. return filepath.Join(prebuiltDir, runtime.GOOS, "bin", tool)
  44. } else {
  45. return ctx.Config().HostToolPath(ctx, tool).String()
  46. }
  47. })
  48. }
  49. hostBinToolVariableWithPrebuilt("aapt2", "prebuilts/sdk/tools", "aapt2")
  50. pctx.HostBinToolVariable("avbtool", "avbtool")
  51. pctx.HostBinToolVariable("e2fsdroid", "e2fsdroid")
  52. pctx.HostBinToolVariable("merge_zips", "merge_zips")
  53. pctx.HostBinToolVariable("mke2fs", "mke2fs")
  54. pctx.HostBinToolVariable("resize2fs", "resize2fs")
  55. pctx.HostBinToolVariable("sefcontext_compile", "sefcontext_compile")
  56. pctx.HostBinToolVariable("soong_zip", "soong_zip")
  57. pctx.HostBinToolVariable("zip2zip", "zip2zip")
  58. pctx.HostBinToolVariable("zipalign", "zipalign")
  59. pctx.HostBinToolVariable("jsonmodify", "jsonmodify")
  60. pctx.HostBinToolVariable("conv_apex_manifest", "conv_apex_manifest")
  61. pctx.HostBinToolVariable("extract_apks", "extract_apks")
  62. pctx.HostBinToolVariable("make_f2fs", "make_f2fs")
  63. pctx.HostBinToolVariable("sload_f2fs", "sload_f2fs")
  64. pctx.HostBinToolVariable("make_erofs", "make_erofs")
  65. pctx.HostBinToolVariable("apex_compression_tool", "apex_compression_tool")
  66. pctx.HostBinToolVariable("dexdeps", "dexdeps")
  67. pctx.HostBinToolVariable("apex_sepolicy_tests", "apex_sepolicy_tests")
  68. pctx.HostBinToolVariable("deapexer", "deapexer")
  69. pctx.HostBinToolVariable("debugfs_static", "debugfs_static")
  70. pctx.SourcePathVariable("genNdkUsedbyApexPath", "build/soong/scripts/gen_ndk_usedby_apex.sh")
  71. }
  72. var (
  73. apexManifestRule = pctx.StaticRule("apexManifestRule", blueprint.RuleParams{
  74. Command: `rm -f $out && ${jsonmodify} $in ` +
  75. `-a provideNativeLibs ${provideNativeLibs} ` +
  76. `-a requireNativeLibs ${requireNativeLibs} ` +
  77. `-se version 0 ${default_version} ` +
  78. `${opt} ` +
  79. `-o $out`,
  80. CommandDeps: []string{"${jsonmodify}"},
  81. Description: "prepare ${out}",
  82. }, "provideNativeLibs", "requireNativeLibs", "default_version", "opt")
  83. stripApexManifestRule = pctx.StaticRule("stripApexManifestRule", blueprint.RuleParams{
  84. Command: `rm -f $out && ${conv_apex_manifest} strip $in -o $out`,
  85. CommandDeps: []string{"${conv_apex_manifest}"},
  86. Description: "strip ${in}=>${out}",
  87. })
  88. pbApexManifestRule = pctx.StaticRule("pbApexManifestRule", blueprint.RuleParams{
  89. Command: `rm -f $out && ${conv_apex_manifest} proto $in -o $out`,
  90. CommandDeps: []string{"${conv_apex_manifest}"},
  91. Description: "convert ${in}=>${out}",
  92. })
  93. // TODO(b/113233103): make sure that file_contexts is as expected, i.e., validate
  94. // against the binary policy using sefcontext_compiler -p <policy>.
  95. // TODO(b/114327326): automate the generation of file_contexts
  96. apexRule = pctx.StaticRule("apexRule", blueprint.RuleParams{
  97. Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
  98. `(. ${out}.copy_commands) && ` +
  99. `APEXER_TOOL_PATH=${tool_path} ` +
  100. `${apexer} --force --manifest ${manifest} ` +
  101. `--file_contexts ${file_contexts} ` +
  102. `--canned_fs_config ${canned_fs_config} ` +
  103. `--include_build_info ` +
  104. `--payload_type image ` +
  105. `--key ${key} ${opt_flags} ${image_dir} ${out} `,
  106. CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
  107. "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}", "${sload_f2fs}", "${make_erofs}",
  108. "${soong_zip}", "${zipalign}", "${aapt2}", "prebuilts/sdk/current/public/android.jar"},
  109. Rspfile: "${out}.copy_commands",
  110. RspfileContent: "${copy_commands}",
  111. Description: "APEX ${image_dir} => ${out}",
  112. }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key",
  113. "opt_flags", "manifest")
  114. DCLAApexRule = pctx.StaticRule("DCLAApexRule", blueprint.RuleParams{
  115. Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
  116. `(. ${out}.copy_commands) && ` +
  117. `APEXER_TOOL_PATH=${tool_path} ` +
  118. `${apexer_with_DCLA_preprocessing} ` +
  119. `--apexer ${apexer} ` +
  120. `--canned_fs_config ${canned_fs_config} ` +
  121. `${image_dir} ` +
  122. `${out} ` +
  123. `-- ` +
  124. `--include_build_info ` +
  125. `--force ` +
  126. `--payload_type image ` +
  127. `--key ${key} ` +
  128. `--file_contexts ${file_contexts} ` +
  129. `--manifest ${manifest} ` +
  130. `${opt_flags} `,
  131. CommandDeps: []string{"${apexer_with_DCLA_preprocessing}", "${apexer}", "${avbtool}", "${e2fsdroid}",
  132. "${merge_zips}", "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}",
  133. "${sload_f2fs}", "${make_erofs}", "${soong_zip}", "${zipalign}", "${aapt2}",
  134. "prebuilts/sdk/current/public/android.jar"},
  135. Rspfile: "${out}.copy_commands",
  136. RspfileContent: "${copy_commands}",
  137. Description: "APEX ${image_dir} => ${out}",
  138. }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key",
  139. "opt_flags", "manifest", "is_DCLA")
  140. TrimmedApexRule = pctx.StaticRule("TrimmedApexRule", blueprint.RuleParams{
  141. Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
  142. `(. ${out}.copy_commands) && ` +
  143. `APEXER_TOOL_PATH=${tool_path} ` +
  144. `${apexer_with_trim_preprocessing} ` +
  145. `--apexer ${apexer} ` +
  146. `--canned_fs_config ${canned_fs_config} ` +
  147. `--manifest ${manifest} ` +
  148. `--libs_to_trim ${libs_to_trim} ` +
  149. `${image_dir} ` +
  150. `${out} ` +
  151. `-- ` +
  152. `--include_build_info ` +
  153. `--force ` +
  154. `--payload_type image ` +
  155. `--key ${key} ` +
  156. `--file_contexts ${file_contexts} ` +
  157. `${opt_flags} `,
  158. CommandDeps: []string{"${apexer_with_trim_preprocessing}", "${apexer}", "${avbtool}", "${e2fsdroid}",
  159. "${merge_zips}", "${mke2fs}", "${resize2fs}", "${sefcontext_compile}", "${make_f2fs}",
  160. "${sload_f2fs}", "${make_erofs}", "${soong_zip}", "${zipalign}", "${aapt2}",
  161. "prebuilts/sdk/current/public/android.jar"},
  162. Rspfile: "${out}.copy_commands",
  163. RspfileContent: "${copy_commands}",
  164. Description: "APEX ${image_dir} => ${out}",
  165. }, "tool_path", "image_dir", "copy_commands", "file_contexts", "canned_fs_config", "key",
  166. "opt_flags", "manifest", "libs_to_trim")
  167. zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
  168. Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
  169. `(. ${out}.copy_commands) && ` +
  170. `APEXER_TOOL_PATH=${tool_path} ` +
  171. `${apexer} --force --manifest ${manifest} ` +
  172. `--payload_type zip ` +
  173. `${image_dir} ${out} `,
  174. CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
  175. Rspfile: "${out}.copy_commands",
  176. RspfileContent: "${copy_commands}",
  177. Description: "ZipAPEX ${image_dir} => ${out}",
  178. }, "tool_path", "image_dir", "copy_commands", "manifest")
  179. apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
  180. blueprint.RuleParams{
  181. Command: `${aapt2} convert --output-format proto $in -o $out`,
  182. CommandDeps: []string{"${aapt2}"},
  183. })
  184. apexBundleRule = pctx.StaticRule("apexBundleRule", blueprint.RuleParams{
  185. Command: `${zip2zip} -i $in -o $out.base ` +
  186. `apex_payload.img:apex/${abi}.img ` +
  187. `apex_build_info.pb:apex/${abi}.build_info.pb ` +
  188. `apex_manifest.json:root/apex_manifest.json ` +
  189. `apex_manifest.pb:root/apex_manifest.pb ` +
  190. `AndroidManifest.xml:manifest/AndroidManifest.xml ` +
  191. `assets/NOTICE.html.gz:assets/NOTICE.html.gz &&` +
  192. `${soong_zip} -o $out.config -C $$(dirname ${config}) -f ${config} && ` +
  193. `${merge_zips} $out $out.base $out.config`,
  194. CommandDeps: []string{"${zip2zip}", "${soong_zip}", "${merge_zips}"},
  195. Description: "app bundle",
  196. }, "abi", "config")
  197. diffApexContentRule = pctx.StaticRule("diffApexContentRule", blueprint.RuleParams{
  198. Command: `diff --unchanged-group-format='' \` +
  199. `--changed-group-format='%<' \` +
  200. `${image_content_file} ${allowed_files_file} || (` +
  201. `echo -e "New unexpected files were added to ${apex_module_name}." ` +
  202. ` "To fix the build run following command:" && ` +
  203. `echo "system/apex/tools/update_allowed_list.sh ${allowed_files_file} ${image_content_file}" && ` +
  204. `exit 1); touch ${out}`,
  205. Description: "Diff ${image_content_file} and ${allowed_files_file}",
  206. }, "image_content_file", "allowed_files_file", "apex_module_name")
  207. generateAPIsUsedbyApexRule = pctx.StaticRule("generateAPIsUsedbyApexRule", blueprint.RuleParams{
  208. Command: "$genNdkUsedbyApexPath ${image_dir} ${readelf} ${out}",
  209. CommandDeps: []string{"${genNdkUsedbyApexPath}"},
  210. Description: "Generate symbol list used by Apex",
  211. }, "image_dir", "readelf")
  212. apexSepolicyTestsRule = pctx.StaticRule("apexSepolicyTestsRule", blueprint.RuleParams{
  213. Command: `${deapexer} --debugfs_path ${debugfs_static} list -Z ${in} > ${out}.fc` +
  214. ` && ${apex_sepolicy_tests} -f ${out}.fc && touch ${out}`,
  215. CommandDeps: []string{"${apex_sepolicy_tests}", "${deapexer}", "${debugfs_static}"},
  216. Description: "run apex_sepolicy_tests",
  217. })
  218. )
  219. // buildManifest creates buile rules to modify the input apex_manifest.json to add information
  220. // gathered by the build system such as provided/required native libraries. Two output files having
  221. // different formats are generated. a.manifestJsonOut is JSON format for Q devices, and
  222. // a.manifest.PbOut is protobuf format for R+ devices.
  223. // TODO(jiyong): make this to return paths instead of directly storing the paths to apexBundle
  224. func (a *apexBundle) buildManifest(ctx android.ModuleContext, provideNativeLibs, requireNativeLibs []string) {
  225. src := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
  226. // Put dependency({provide|require}NativeLibs) in apex_manifest.json
  227. provideNativeLibs = android.SortedUniqueStrings(provideNativeLibs)
  228. requireNativeLibs = android.SortedUniqueStrings(android.RemoveListFromList(requireNativeLibs, provideNativeLibs))
  229. // VNDK APEX name is determined at runtime, so update "name" in apex_manifest
  230. optCommands := []string{}
  231. if a.vndkApex {
  232. apexName := vndkApexNamePrefix + a.vndkVersion(ctx.DeviceConfig())
  233. optCommands = append(optCommands, "-v name "+apexName)
  234. }
  235. // Collect jniLibs. Notice that a.filesInfo is already sorted
  236. var jniLibs []string
  237. for _, fi := range a.filesInfo {
  238. if fi.isJniLib && !android.InList(fi.stem(), jniLibs) {
  239. jniLibs = append(jniLibs, fi.stem())
  240. }
  241. }
  242. if len(jniLibs) > 0 {
  243. optCommands = append(optCommands, "-a jniLibs "+strings.Join(jniLibs, " "))
  244. }
  245. if android.InList(":vndk", requireNativeLibs) {
  246. if _, vndkVersion := a.getImageVariationPair(ctx.DeviceConfig()); vndkVersion != "" {
  247. optCommands = append(optCommands, "-v vndkVersion "+vndkVersion)
  248. }
  249. }
  250. manifestJsonFullOut := android.PathForModuleOut(ctx, "apex_manifest_full.json")
  251. defaultVersion := android.DefaultUpdatableModuleVersion
  252. if a.properties.Variant_version != nil {
  253. defaultVersionInt, err := strconv.Atoi(defaultVersion)
  254. if err != nil {
  255. ctx.ModuleErrorf("expected DefaultUpdatableModuleVersion to be an int, but got %s", defaultVersion)
  256. }
  257. if defaultVersionInt%10 != 0 {
  258. ctx.ModuleErrorf("expected DefaultUpdatableModuleVersion to end in a zero, but got %s", defaultVersion)
  259. }
  260. variantVersion := []rune(*a.properties.Variant_version)
  261. if len(variantVersion) != 1 || variantVersion[0] < '0' || variantVersion[0] > '9' {
  262. ctx.PropertyErrorf("variant_version", "expected an integer between 0-9; got %s", *a.properties.Variant_version)
  263. }
  264. defaultVersionRunes := []rune(defaultVersion)
  265. defaultVersionRunes[len(defaultVersion)-1] = []rune(variantVersion)[0]
  266. defaultVersion = string(defaultVersionRunes)
  267. }
  268. if override := ctx.Config().Getenv("OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION"); override != "" {
  269. defaultVersion = override
  270. }
  271. ctx.Build(pctx, android.BuildParams{
  272. Rule: apexManifestRule,
  273. Input: src,
  274. Output: manifestJsonFullOut,
  275. Args: map[string]string{
  276. "provideNativeLibs": strings.Join(provideNativeLibs, " "),
  277. "requireNativeLibs": strings.Join(requireNativeLibs, " "),
  278. "default_version": defaultVersion,
  279. "opt": strings.Join(optCommands, " "),
  280. },
  281. })
  282. // b/143654022 Q apexd can't understand newly added keys in apex_manifest.json prepare
  283. // stripped-down version so that APEX modules built from R+ can be installed to Q
  284. minSdkVersion := a.minSdkVersion(ctx)
  285. if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
  286. a.manifestJsonOut = android.PathForModuleOut(ctx, "apex_manifest.json")
  287. ctx.Build(pctx, android.BuildParams{
  288. Rule: stripApexManifestRule,
  289. Input: manifestJsonFullOut,
  290. Output: a.manifestJsonOut,
  291. })
  292. }
  293. // From R+, protobuf binary format (.pb) is the standard format for apex_manifest
  294. a.manifestPbOut = android.PathForModuleOut(ctx, "apex_manifest.pb")
  295. ctx.Build(pctx, android.BuildParams{
  296. Rule: pbApexManifestRule,
  297. Input: manifestJsonFullOut,
  298. Output: a.manifestPbOut,
  299. })
  300. }
  301. // buildFileContexts create build rules to append an entry for apex_manifest.pb to the file_contexts
  302. // file for this APEX which is either from /systme/sepolicy/apex/<apexname>-file_contexts or from
  303. // the file_contexts property of this APEX. This is to make sure that the manifest file is correctly
  304. // labeled as system_file or vendor_apex_metadata_file.
  305. func (a *apexBundle) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
  306. var fileContexts android.Path
  307. var fileContextsDir string
  308. if a.properties.File_contexts == nil {
  309. fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
  310. } else {
  311. if m, t := android.SrcIsModuleWithTag(*a.properties.File_contexts); m != "" {
  312. otherModule := android.GetModuleFromPathDep(ctx, m, t)
  313. fileContextsDir = ctx.OtherModuleDir(otherModule)
  314. }
  315. fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
  316. }
  317. if fileContextsDir == "" {
  318. fileContextsDir = filepath.Dir(fileContexts.String())
  319. }
  320. fileContextsDir += string(filepath.Separator)
  321. if a.Platform() {
  322. if !strings.HasPrefix(fileContextsDir, "system/sepolicy/") {
  323. ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but found in %q", fileContextsDir)
  324. }
  325. }
  326. if !android.ExistentPathForSource(ctx, fileContexts.String()).Valid() {
  327. ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", fileContexts.String())
  328. }
  329. useFileContextsAsIs := proptools.Bool(a.properties.Use_file_contexts_as_is)
  330. output := android.PathForModuleOut(ctx, "file_contexts")
  331. rule := android.NewRuleBuilder(pctx, ctx)
  332. forceLabel := "u:object_r:system_file:s0"
  333. if a.SocSpecific() && !a.vndkApex {
  334. // APEX on /vendor should label ./ and ./apex_manifest.pb as vendor_apex_metadata_file.
  335. // The reason why we skip VNDK APEX is that aosp_{pixel device} targets install VNDK APEX on /vendor
  336. // even though VNDK APEX is supposed to be installed on /system. (See com.android.vndk.current.on_vendor)
  337. forceLabel = "u:object_r:vendor_apex_metadata_file:s0"
  338. }
  339. switch a.properties.ApexType {
  340. case imageApex:
  341. // remove old file
  342. rule.Command().Text("rm").FlagWithOutput("-f ", output)
  343. // copy file_contexts
  344. rule.Command().Text("cat").Input(fileContexts).Text(">>").Output(output)
  345. // new line
  346. rule.Command().Text("echo").Text(">>").Output(output)
  347. if !useFileContextsAsIs {
  348. // force-label /apex_manifest.pb and /
  349. rule.Command().Text("echo").Text("/apex_manifest\\\\.pb").Text(forceLabel).Text(">>").Output(output)
  350. rule.Command().Text("echo").Text("/").Text(forceLabel).Text(">>").Output(output)
  351. }
  352. default:
  353. panic(fmt.Errorf("unsupported type %v", a.properties.ApexType))
  354. }
  355. rule.Build("file_contexts."+a.Name(), "Generate file_contexts")
  356. return output.OutputPath
  357. }
  358. // buildInstalledFilesFile creates a build rule for the installed-files.txt file where the list of
  359. // files included in this APEX is shown. The text file is dist'ed so that people can see what's
  360. // included in the APEX without actually downloading and extracting it.
  361. func (a *apexBundle) buildInstalledFilesFile(ctx android.ModuleContext, builtApex android.Path, imageDir android.Path) android.OutputPath {
  362. output := android.PathForModuleOut(ctx, "installed-files.txt")
  363. rule := android.NewRuleBuilder(pctx, ctx)
  364. rule.Command().
  365. Implicit(builtApex).
  366. Text("(cd " + imageDir.String() + " ; ").
  367. Text("find . \\( -type f -o -type l \\) -printf \"%s %p\\n\") ").
  368. Text(" | sort -nr > ").
  369. Output(output)
  370. rule.Build("installed-files."+a.Name(), "Installed files")
  371. return output.OutputPath
  372. }
  373. // buildBundleConfig creates a build rule for the bundle config file that will control the bundle
  374. // creation process.
  375. func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.OutputPath {
  376. output := android.PathForModuleOut(ctx, "bundle_config.json")
  377. type ApkConfig struct {
  378. Package_name string `json:"package_name"`
  379. Apk_path string `json:"path"`
  380. }
  381. config := struct {
  382. Compression struct {
  383. Uncompressed_glob []string `json:"uncompressed_glob"`
  384. } `json:"compression"`
  385. Apex_config struct {
  386. Apex_embedded_apk_config []ApkConfig `json:"apex_embedded_apk_config,omitempty"`
  387. } `json:"apex_config,omitempty"`
  388. }{}
  389. config.Compression.Uncompressed_glob = []string{
  390. "apex_payload.img",
  391. "apex_manifest.*",
  392. }
  393. // Collect the manifest names and paths of android apps if their manifest names are
  394. // overridden.
  395. for _, fi := range a.filesInfo {
  396. if fi.class != app && fi.class != appSet {
  397. continue
  398. }
  399. packageName := fi.overriddenPackageName
  400. if packageName != "" {
  401. config.Apex_config.Apex_embedded_apk_config = append(
  402. config.Apex_config.Apex_embedded_apk_config,
  403. ApkConfig{
  404. Package_name: packageName,
  405. Apk_path: fi.path(),
  406. })
  407. }
  408. }
  409. j, err := json.Marshal(config)
  410. if err != nil {
  411. panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
  412. }
  413. android.WriteFileRule(ctx, output, string(j))
  414. return output.OutputPath
  415. }
  416. func markManifestTestOnly(ctx android.ModuleContext, androidManifestFile android.Path) android.Path {
  417. return java.ManifestFixer(ctx, androidManifestFile, java.ManifestFixerParams{
  418. TestOnly: true,
  419. })
  420. }
  421. // buildApex creates build rules to build an APEX using apexer.
  422. func (a *apexBundle) buildApex(ctx android.ModuleContext) {
  423. apexType := a.properties.ApexType
  424. suffix := apexType.suffix()
  425. apexName := a.BaseModuleName()
  426. ////////////////////////////////////////////////////////////////////////////////////////////
  427. // Step 1: copy built files to appropriate directories under the image directory
  428. imageDir := android.PathForModuleOut(ctx, "image"+suffix)
  429. installSymbolFiles := (!ctx.Config().KatiEnabled() || a.ExportedToMake()) && a.installable()
  430. // set of dependency module:location mappings
  431. installMapSet := make(map[string]bool)
  432. // TODO(jiyong): use the RuleBuilder
  433. var copyCommands []string
  434. var implicitInputs []android.Path
  435. apexDir := android.PathForModuleInPartitionInstall(ctx, "apex", apexName)
  436. for _, fi := range a.filesInfo {
  437. destPath := imageDir.Join(ctx, fi.path()).String()
  438. // Prepare the destination path
  439. destPathDir := filepath.Dir(destPath)
  440. if fi.class == appSet {
  441. copyCommands = append(copyCommands, "rm -rf "+destPathDir)
  442. }
  443. copyCommands = append(copyCommands, "mkdir -p "+destPathDir)
  444. installMapPath := fi.builtFile
  445. // Copy the built file to the directory. But if the symlink optimization is turned
  446. // on, place a symlink to the corresponding file in /system partition instead.
  447. if a.linkToSystemLib && fi.transitiveDep && fi.availableToPlatform() {
  448. pathOnDevice := filepath.Join("/", fi.partition, fi.path())
  449. copyCommands = append(copyCommands, "ln -sfn "+pathOnDevice+" "+destPath)
  450. } else {
  451. // Copy the file into APEX
  452. copyCommands = append(copyCommands, "cp -f "+fi.builtFile.String()+" "+destPath)
  453. var installedPath android.InstallPath
  454. if fi.class == appSet {
  455. // In case of AppSet, we need to copy additional APKs as well. They
  456. // are zipped. So we need to unzip them.
  457. copyCommands = append(copyCommands,
  458. fmt.Sprintf("unzip -qDD -d %s %s", destPathDir,
  459. fi.module.(*java.AndroidAppSet).PackedAdditionalOutputs().String()))
  460. if installSymbolFiles {
  461. installedPath = ctx.InstallFileWithExtraFilesZip(apexDir.Join(ctx, fi.installDir),
  462. fi.stem(), fi.builtFile, fi.module.(*java.AndroidAppSet).PackedAdditionalOutputs())
  463. }
  464. } else {
  465. if installSymbolFiles {
  466. installedPath = ctx.InstallFile(apexDir.Join(ctx, fi.installDir), fi.stem(), fi.builtFile)
  467. }
  468. }
  469. implicitInputs = append(implicitInputs, fi.builtFile)
  470. // Create additional symlinks pointing the file inside the APEX (if any). Note that
  471. // this is independent from the symlink optimization.
  472. for _, symlinkPath := range fi.symlinkPaths() {
  473. symlinkDest := imageDir.Join(ctx, symlinkPath).String()
  474. copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest)
  475. if installSymbolFiles {
  476. ctx.InstallSymlink(apexDir.Join(ctx, filepath.Dir(symlinkPath)), filepath.Base(symlinkPath), installedPath)
  477. }
  478. }
  479. installMapPath = installedPath
  480. }
  481. // Copy the test files (if any)
  482. for _, d := range fi.dataPaths {
  483. // TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible
  484. relPath := d.SrcPath.Rel()
  485. dataPath := d.SrcPath.String()
  486. if !strings.HasSuffix(dataPath, relPath) {
  487. panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
  488. }
  489. dataDest := imageDir.Join(ctx, fi.apexRelativePath(relPath), d.RelativeInstallPath).String()
  490. copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
  491. implicitInputs = append(implicitInputs, d.SrcPath)
  492. }
  493. installMapSet[installMapPath.String()+":"+fi.installDir+"/"+fi.builtFile.Base()] = true
  494. }
  495. implicitInputs = append(implicitInputs, a.manifestPbOut)
  496. if len(installMapSet) > 0 {
  497. var installs []string
  498. installs = append(installs, android.SortedKeys(installMapSet)...)
  499. a.SetLicenseInstallMap(installs)
  500. }
  501. ////////////////////////////////////////////////////////////////////////////////////////////
  502. // Step 1.a: Write the list of files in this APEX to a txt file and compare it against
  503. // the allowed list given via the allowed_files property. Build fails when the two lists
  504. // differ.
  505. //
  506. // TODO(jiyong): consider removing this. Nobody other than com.android.apex.cts.shim.* seems
  507. // to be using this at this moment. Furthermore, this looks very similar to what
  508. // buildInstalledFilesFile does. At least, move this to somewhere else so that this doesn't
  509. // hurt readability.
  510. if a.overridableProperties.Allowed_files != nil {
  511. // Build content.txt
  512. var contentLines []string
  513. imageContentFile := android.PathForModuleOut(ctx, "content.txt")
  514. contentLines = append(contentLines, "./apex_manifest.pb")
  515. minSdkVersion := a.minSdkVersion(ctx)
  516. if minSdkVersion.EqualTo(android.SdkVersion_Android10) {
  517. contentLines = append(contentLines, "./apex_manifest.json")
  518. }
  519. for _, fi := range a.filesInfo {
  520. contentLines = append(contentLines, "./"+fi.path())
  521. }
  522. sort.Strings(contentLines)
  523. android.WriteFileRule(ctx, imageContentFile, strings.Join(contentLines, "\n"))
  524. implicitInputs = append(implicitInputs, imageContentFile)
  525. // Compare content.txt against allowed_files.
  526. allowedFilesFile := android.PathForModuleSrc(ctx, proptools.String(a.overridableProperties.Allowed_files))
  527. phonyOutput := android.PathForModuleOut(ctx, a.Name()+"-diff-phony-output")
  528. ctx.Build(pctx, android.BuildParams{
  529. Rule: diffApexContentRule,
  530. Implicits: implicitInputs,
  531. Output: phonyOutput,
  532. Description: "diff apex image content",
  533. Args: map[string]string{
  534. "allowed_files_file": allowedFilesFile.String(),
  535. "image_content_file": imageContentFile.String(),
  536. "apex_module_name": a.Name(),
  537. },
  538. })
  539. implicitInputs = append(implicitInputs, phonyOutput)
  540. }
  541. unsignedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix+".unsigned")
  542. outHostBinDir := ctx.Config().HostToolPath(ctx, "").String()
  543. prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
  544. if apexType == imageApex {
  545. ////////////////////////////////////////////////////////////////////////////////////
  546. // Step 2: create canned_fs_config which encodes filemode,uid,gid of each files
  547. // in this APEX. The file will be used by apexer in later steps.
  548. cannedFsConfig := a.buildCannedFsConfig(ctx)
  549. implicitInputs = append(implicitInputs, cannedFsConfig)
  550. ////////////////////////////////////////////////////////////////////////////////////
  551. // Step 3: Prepare option flags for apexer and invoke it to create an unsigned APEX.
  552. // TODO(jiyong): use the RuleBuilder
  553. optFlags := []string{}
  554. fileContexts := a.buildFileContexts(ctx)
  555. implicitInputs = append(implicitInputs, fileContexts)
  556. implicitInputs = append(implicitInputs, a.privateKeyFile, a.publicKeyFile)
  557. optFlags = append(optFlags, "--pubkey "+a.publicKeyFile.String())
  558. manifestPackageName := a.getOverrideManifestPackageName(ctx)
  559. if manifestPackageName != "" {
  560. optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
  561. }
  562. if a.properties.AndroidManifest != nil {
  563. androidManifestFile := android.PathForModuleSrc(ctx, proptools.String(a.properties.AndroidManifest))
  564. if a.testApex {
  565. androidManifestFile = markManifestTestOnly(ctx, androidManifestFile)
  566. }
  567. implicitInputs = append(implicitInputs, androidManifestFile)
  568. optFlags = append(optFlags, "--android_manifest "+androidManifestFile.String())
  569. } else if a.testApex {
  570. optFlags = append(optFlags, "--test_only")
  571. }
  572. // Determine target/min sdk version from the context
  573. // TODO(jiyong): make this as a function
  574. moduleMinSdkVersion := a.minSdkVersion(ctx)
  575. minSdkVersion := moduleMinSdkVersion.String()
  576. // bundletool doesn't understand what "current" is. We need to transform it to
  577. // codename
  578. if moduleMinSdkVersion.IsCurrent() || moduleMinSdkVersion.IsNone() {
  579. minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String()
  580. if java.UseApiFingerprint(ctx) {
  581. minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
  582. implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
  583. }
  584. }
  585. // apex module doesn't have a concept of target_sdk_version, hence for the time
  586. // being targetSdkVersion == default targetSdkVersion of the branch.
  587. targetSdkVersion := strconv.Itoa(ctx.Config().DefaultAppTargetSdk(ctx).FinalOrFutureInt())
  588. if java.UseApiFingerprint(ctx) {
  589. targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", java.ApiFingerprintPath(ctx).String())
  590. implicitInputs = append(implicitInputs, java.ApiFingerprintPath(ctx))
  591. }
  592. optFlags = append(optFlags, "--target_sdk_version "+targetSdkVersion)
  593. optFlags = append(optFlags, "--min_sdk_version "+minSdkVersion)
  594. if a.overridableProperties.Logging_parent != "" {
  595. optFlags = append(optFlags, "--logging_parent ", a.overridableProperties.Logging_parent)
  596. }
  597. // Create a NOTICE file, and embed it as an asset file in the APEX.
  598. htmlGzNotice := android.PathForModuleOut(ctx, "NOTICE.html.gz")
  599. android.BuildNoticeHtmlOutputFromLicenseMetadata(
  600. ctx, htmlGzNotice, "", "",
  601. []string{
  602. android.PathForModuleInstall(ctx).String() + "/",
  603. android.PathForModuleInPartitionInstall(ctx, "apex").String() + "/",
  604. })
  605. noticeAssetPath := android.PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
  606. builder := android.NewRuleBuilder(pctx, ctx)
  607. builder.Command().Text("cp").
  608. Input(htmlGzNotice).
  609. Output(noticeAssetPath)
  610. builder.Build("notice_dir", "Building notice dir")
  611. implicitInputs = append(implicitInputs, noticeAssetPath)
  612. optFlags = append(optFlags, "--assets_dir "+filepath.Dir(noticeAssetPath.String()))
  613. // Apexes which are supposed to be installed in builtin dirs(/system, etc)
  614. // don't need hashtree for activation. Therefore, by removing hashtree from
  615. // apex bundle (filesystem image in it, to be specific), we can save storage.
  616. needHashTree := moduleMinSdkVersion.LessThanOrEqualTo(android.SdkVersion_Android10) ||
  617. a.shouldGenerateHashtree()
  618. if ctx.Config().ApexCompressionEnabled() && a.isCompressable() {
  619. needHashTree = true
  620. }
  621. if !needHashTree {
  622. optFlags = append(optFlags, "--no_hashtree")
  623. }
  624. if a.testOnlyShouldSkipPayloadSign() {
  625. optFlags = append(optFlags, "--unsigned_payload")
  626. }
  627. if moduleMinSdkVersion == android.SdkVersion_Android10 {
  628. implicitInputs = append(implicitInputs, a.manifestJsonOut)
  629. optFlags = append(optFlags, "--manifest_json "+a.manifestJsonOut.String())
  630. }
  631. optFlags = append(optFlags, "--payload_fs_type "+a.payloadFsType.string())
  632. if a.dynamic_common_lib_apex() {
  633. ctx.Build(pctx, android.BuildParams{
  634. Rule: DCLAApexRule,
  635. Implicits: implicitInputs,
  636. Output: unsignedOutputFile,
  637. Description: "apex (" + apexType.name() + ")",
  638. Args: map[string]string{
  639. "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
  640. "image_dir": imageDir.String(),
  641. "copy_commands": strings.Join(copyCommands, " && "),
  642. "manifest": a.manifestPbOut.String(),
  643. "file_contexts": fileContexts.String(),
  644. "canned_fs_config": cannedFsConfig.String(),
  645. "key": a.privateKeyFile.String(),
  646. "opt_flags": strings.Join(optFlags, " "),
  647. },
  648. })
  649. } else if ctx.Config().ApexTrimEnabled() && len(a.libs_to_trim(ctx)) > 0 {
  650. ctx.Build(pctx, android.BuildParams{
  651. Rule: TrimmedApexRule,
  652. Implicits: implicitInputs,
  653. Output: unsignedOutputFile,
  654. Description: "apex (" + apexType.name() + ")",
  655. Args: map[string]string{
  656. "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
  657. "image_dir": imageDir.String(),
  658. "copy_commands": strings.Join(copyCommands, " && "),
  659. "manifest": a.manifestPbOut.String(),
  660. "file_contexts": fileContexts.String(),
  661. "canned_fs_config": cannedFsConfig.String(),
  662. "key": a.privateKeyFile.String(),
  663. "opt_flags": strings.Join(optFlags, " "),
  664. "libs_to_trim": strings.Join(a.libs_to_trim(ctx), ","),
  665. },
  666. })
  667. } else {
  668. ctx.Build(pctx, android.BuildParams{
  669. Rule: apexRule,
  670. Implicits: implicitInputs,
  671. Output: unsignedOutputFile,
  672. Description: "apex (" + apexType.name() + ")",
  673. Args: map[string]string{
  674. "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
  675. "image_dir": imageDir.String(),
  676. "copy_commands": strings.Join(copyCommands, " && "),
  677. "manifest": a.manifestPbOut.String(),
  678. "file_contexts": fileContexts.String(),
  679. "canned_fs_config": cannedFsConfig.String(),
  680. "key": a.privateKeyFile.String(),
  681. "opt_flags": strings.Join(optFlags, " "),
  682. },
  683. })
  684. }
  685. // TODO(jiyong): make the two rules below as separate functions
  686. apexProtoFile := android.PathForModuleOut(ctx, a.Name()+".pb"+suffix)
  687. bundleModuleFile := android.PathForModuleOut(ctx, a.Name()+suffix+"-base.zip")
  688. a.bundleModuleFile = bundleModuleFile
  689. ctx.Build(pctx, android.BuildParams{
  690. Rule: apexProtoConvertRule,
  691. Input: unsignedOutputFile,
  692. Output: apexProtoFile,
  693. Description: "apex proto convert",
  694. })
  695. implicitInputs = append(implicitInputs, unsignedOutputFile)
  696. // Run coverage analysis
  697. apisUsedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_using.txt")
  698. ctx.Build(pctx, android.BuildParams{
  699. Rule: generateAPIsUsedbyApexRule,
  700. Implicits: implicitInputs,
  701. Description: "coverage",
  702. Output: apisUsedbyOutputFile,
  703. Args: map[string]string{
  704. "image_dir": imageDir.String(),
  705. "readelf": "${config.ClangBin}/llvm-readelf",
  706. },
  707. })
  708. a.nativeApisUsedByModuleFile = apisUsedbyOutputFile
  709. var nativeLibNames []string
  710. for _, f := range a.filesInfo {
  711. if f.class == nativeSharedLib {
  712. nativeLibNames = append(nativeLibNames, f.stem())
  713. }
  714. }
  715. apisBackedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_backing.txt")
  716. rule := android.NewRuleBuilder(pctx, ctx)
  717. rule.Command().
  718. Tool(android.PathForSource(ctx, "build/soong/scripts/gen_ndk_backedby_apex.sh")).
  719. Output(apisBackedbyOutputFile).
  720. Flags(nativeLibNames)
  721. rule.Build("ndk_backedby_list", "Generate API libraries backed by Apex")
  722. a.nativeApisBackedByModuleFile = apisBackedbyOutputFile
  723. var javaLibOrApkPath []android.Path
  724. for _, f := range a.filesInfo {
  725. if f.class == javaSharedLib || f.class == app {
  726. javaLibOrApkPath = append(javaLibOrApkPath, f.builtFile)
  727. }
  728. }
  729. javaApiUsedbyOutputFile := android.PathForModuleOut(ctx, a.Name()+"_using.xml")
  730. javaUsedByRule := android.NewRuleBuilder(pctx, ctx)
  731. javaUsedByRule.Command().
  732. Tool(android.PathForSource(ctx, "build/soong/scripts/gen_java_usedby_apex.sh")).
  733. BuiltTool("dexdeps").
  734. Output(javaApiUsedbyOutputFile).
  735. Inputs(javaLibOrApkPath)
  736. javaUsedByRule.Build("java_usedby_list", "Generate Java APIs used by Apex")
  737. a.javaApisUsedByModuleFile = javaApiUsedbyOutputFile
  738. bundleConfig := a.buildBundleConfig(ctx)
  739. var abis []string
  740. for _, target := range ctx.MultiTargets() {
  741. if len(target.Arch.Abi) > 0 {
  742. abis = append(abis, target.Arch.Abi[0])
  743. }
  744. }
  745. abis = android.FirstUniqueStrings(abis)
  746. ctx.Build(pctx, android.BuildParams{
  747. Rule: apexBundleRule,
  748. Input: apexProtoFile,
  749. Implicit: bundleConfig,
  750. Output: a.bundleModuleFile,
  751. Description: "apex bundle module",
  752. Args: map[string]string{
  753. "abi": strings.Join(abis, "."),
  754. "config": bundleConfig.String(),
  755. },
  756. })
  757. } else { // zipApex
  758. ctx.Build(pctx, android.BuildParams{
  759. Rule: zipApexRule,
  760. Implicits: implicitInputs,
  761. Output: unsignedOutputFile,
  762. Description: "apex (" + apexType.name() + ")",
  763. Args: map[string]string{
  764. "tool_path": outHostBinDir + ":" + prebuiltSdkToolsBinDir,
  765. "image_dir": imageDir.String(),
  766. "copy_commands": strings.Join(copyCommands, " && "),
  767. "manifest": a.manifestPbOut.String(),
  768. },
  769. })
  770. }
  771. ////////////////////////////////////////////////////////////////////////////////////
  772. // Step 4: Sign the APEX using signapk
  773. signedOutputFile := android.PathForModuleOut(ctx, a.Name()+suffix)
  774. pem, key := a.getCertificateAndPrivateKey(ctx)
  775. rule := java.Signapk
  776. args := map[string]string{
  777. "certificates": pem.String() + " " + key.String(),
  778. "flags": "-a 4096 --align-file-size", //alignment
  779. }
  780. implicits := android.Paths{pem, key}
  781. if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
  782. rule = java.SignapkRE
  783. args["implicits"] = strings.Join(implicits.Strings(), ",")
  784. args["outCommaList"] = signedOutputFile.String()
  785. }
  786. var validations android.Paths
  787. // TODO(b/279688635) deapexer supports [ext4]
  788. if suffix == imageApexSuffix && ext4 == a.payloadFsType {
  789. validations = append(validations, runApexSepolicyTests(ctx, unsignedOutputFile.OutputPath))
  790. }
  791. ctx.Build(pctx, android.BuildParams{
  792. Rule: rule,
  793. Description: "signapk",
  794. Output: signedOutputFile,
  795. Input: unsignedOutputFile,
  796. Implicits: implicits,
  797. Args: args,
  798. Validations: validations,
  799. })
  800. if suffix == imageApexSuffix {
  801. a.outputApexFile = signedOutputFile
  802. }
  803. a.outputFile = signedOutputFile
  804. if ctx.ModuleDir() != "system/apex/apexd/apexd_testdata" && a.testOnlyShouldForceCompression() {
  805. ctx.PropertyErrorf("test_only_force_compression", "not available")
  806. return
  807. }
  808. installSuffix := suffix
  809. a.setCompression(ctx)
  810. if a.isCompressed {
  811. unsignedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+imageCapexSuffix+".unsigned")
  812. compressRule := android.NewRuleBuilder(pctx, ctx)
  813. compressRule.Command().
  814. Text("rm").
  815. FlagWithOutput("-f ", unsignedCompressedOutputFile)
  816. compressRule.Command().
  817. BuiltTool("apex_compression_tool").
  818. Flag("compress").
  819. FlagWithArg("--apex_compression_tool ", outHostBinDir+":"+prebuiltSdkToolsBinDir).
  820. FlagWithInput("--input ", signedOutputFile).
  821. FlagWithOutput("--output ", unsignedCompressedOutputFile)
  822. compressRule.Build("compressRule", "Generate unsigned compressed APEX file")
  823. signedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+imageCapexSuffix)
  824. if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
  825. args["outCommaList"] = signedCompressedOutputFile.String()
  826. }
  827. ctx.Build(pctx, android.BuildParams{
  828. Rule: rule,
  829. Description: "sign compressedApex",
  830. Output: signedCompressedOutputFile,
  831. Input: unsignedCompressedOutputFile,
  832. Implicits: implicits,
  833. Args: args,
  834. })
  835. a.outputFile = signedCompressedOutputFile
  836. installSuffix = imageCapexSuffix
  837. }
  838. if !a.installable() {
  839. a.SkipInstall()
  840. }
  841. // Install to $OUT/soong/{target,host}/.../apex.
  842. a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile,
  843. a.compatSymlinks.Paths()...)
  844. // installed-files.txt is dist'ed
  845. a.installedFilesFile = a.buildInstalledFilesFile(ctx, a.outputFile, imageDir)
  846. }
  847. // getCertificateAndPrivateKey retrieves the cert and the private key that will be used to sign
  848. // the zip container of this APEX. See the description of the 'certificate' property for how
  849. // the cert and the private key are found.
  850. func (a *apexBundle) getCertificateAndPrivateKey(ctx android.PathContext) (pem, key android.Path) {
  851. if a.containerCertificateFile != nil {
  852. return a.containerCertificateFile, a.containerPrivateKeyFile
  853. }
  854. cert := String(a.overridableProperties.Certificate)
  855. if cert == "" {
  856. return ctx.Config().DefaultAppCertificate(ctx)
  857. }
  858. defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
  859. pem = defaultDir.Join(ctx, cert+".x509.pem")
  860. key = defaultDir.Join(ctx, cert+".pk8")
  861. return pem, key
  862. }
  863. func (a *apexBundle) getOverrideManifestPackageName(ctx android.ModuleContext) string {
  864. // For VNDK APEXes, check "com.android.vndk" in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
  865. // to see if it should be overridden because their <apex name> is dynamically generated
  866. // according to its VNDK version.
  867. if a.vndkApex {
  868. overrideName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(vndkApexName)
  869. if overridden {
  870. return overrideName + ".v" + a.vndkVersion(ctx.DeviceConfig())
  871. }
  872. return ""
  873. }
  874. if a.overridableProperties.Package_name != "" {
  875. return a.overridableProperties.Package_name
  876. }
  877. manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
  878. if overridden {
  879. return manifestPackageName
  880. }
  881. return ""
  882. }
  883. func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) {
  884. if !a.primaryApexType {
  885. return
  886. }
  887. if a.properties.IsCoverageVariant {
  888. // Otherwise, we will have duplicated rules for coverage and
  889. // non-coverage variants of the same APEX
  890. return
  891. }
  892. if ctx.Host() {
  893. // No need to generate dependency info for host variant
  894. return
  895. }
  896. depInfos := android.DepNameToDepInfoMap{}
  897. a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
  898. if from.Name() == to.Name() {
  899. // This can happen for cc.reuseObjTag. We are not interested in tracking this.
  900. // As soon as the dependency graph crosses the APEX boundary, don't go further.
  901. return !externalDep
  902. }
  903. // Skip dependencies that are only available to APEXes; they are developed with updatability
  904. // in mind and don't need manual approval.
  905. if to.(android.ApexModule).NotAvailableForPlatform() {
  906. return !externalDep
  907. }
  908. depTag := ctx.OtherModuleDependencyTag(to)
  909. // Check to see if dependency been marked to skip the dependency check
  910. if skipDepCheck, ok := depTag.(android.SkipApexAllowedDependenciesCheck); ok && skipDepCheck.SkipApexAllowedDependenciesCheck() {
  911. return !externalDep
  912. }
  913. if info, exists := depInfos[to.Name()]; exists {
  914. if !android.InList(from.Name(), info.From) {
  915. info.From = append(info.From, from.Name())
  916. }
  917. info.IsExternal = info.IsExternal && externalDep
  918. depInfos[to.Name()] = info
  919. } else {
  920. toMinSdkVersion := "(no version)"
  921. if m, ok := to.(interface {
  922. MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
  923. }); ok {
  924. if v := m.MinSdkVersion(ctx); !v.IsNone() {
  925. toMinSdkVersion = v.String()
  926. }
  927. } else if m, ok := to.(interface{ MinSdkVersion() string }); ok {
  928. // TODO(b/175678607) eliminate the use of MinSdkVersion returning
  929. // string
  930. if v := m.MinSdkVersion(); v != "" {
  931. toMinSdkVersion = v
  932. }
  933. }
  934. depInfos[to.Name()] = android.ApexModuleDepInfo{
  935. To: to.Name(),
  936. From: []string{from.Name()},
  937. IsExternal: externalDep,
  938. MinSdkVersion: toMinSdkVersion,
  939. }
  940. }
  941. // As soon as the dependency graph crosses the APEX boundary, don't go further.
  942. return !externalDep
  943. })
  944. a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).String(), depInfos)
  945. ctx.Build(pctx, android.BuildParams{
  946. Rule: android.Phony,
  947. Output: android.PathForPhony(ctx, a.Name()+"-deps-info"),
  948. Inputs: []android.Path{
  949. a.ApexBundleDepsInfo.FullListPath(),
  950. a.ApexBundleDepsInfo.FlatListPath(),
  951. },
  952. })
  953. }
  954. func (a *apexBundle) buildLintReports(ctx android.ModuleContext) {
  955. depSetsBuilder := java.NewLintDepSetBuilder()
  956. for _, fi := range a.filesInfo {
  957. depSetsBuilder.Transitive(fi.lintDepSets)
  958. }
  959. a.lintReports = java.BuildModuleLintReportZips(ctx, depSetsBuilder.Build())
  960. }
  961. func (a *apexBundle) buildCannedFsConfig(ctx android.ModuleContext) android.OutputPath {
  962. var readOnlyPaths = []string{"apex_manifest.json", "apex_manifest.pb"}
  963. var executablePaths []string // this also includes dirs
  964. var appSetDirs []string
  965. appSetFiles := make(map[string]android.Path)
  966. for _, f := range a.filesInfo {
  967. pathInApex := f.path()
  968. if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
  969. executablePaths = append(executablePaths, pathInApex)
  970. for _, d := range f.dataPaths {
  971. readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.RelativeInstallPath, d.SrcPath.Rel()))
  972. }
  973. for _, s := range f.symlinks {
  974. executablePaths = append(executablePaths, filepath.Join(f.installDir, s))
  975. }
  976. } else if f.class == appSet {
  977. // base APK
  978. readOnlyPaths = append(readOnlyPaths, pathInApex)
  979. // Additional APKs
  980. appSetDirs = append(appSetDirs, f.installDir)
  981. appSetFiles[f.installDir] = f.module.(*java.AndroidAppSet).PackedAdditionalOutputs()
  982. } else {
  983. readOnlyPaths = append(readOnlyPaths, pathInApex)
  984. }
  985. dir := f.installDir
  986. for !android.InList(dir, executablePaths) && dir != "" {
  987. executablePaths = append(executablePaths, dir)
  988. dir, _ = filepath.Split(dir) // move up to the parent
  989. if len(dir) > 0 {
  990. // remove trailing slash
  991. dir = dir[:len(dir)-1]
  992. }
  993. }
  994. }
  995. sort.Strings(readOnlyPaths)
  996. sort.Strings(executablePaths)
  997. sort.Strings(appSetDirs)
  998. cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
  999. builder := android.NewRuleBuilder(pctx, ctx)
  1000. cmd := builder.Command()
  1001. cmd.Text("(")
  1002. cmd.Text("echo '/ 1000 1000 0755';")
  1003. for _, p := range readOnlyPaths {
  1004. cmd.Textf("echo '/%s 1000 1000 0644';", p)
  1005. }
  1006. for _, p := range executablePaths {
  1007. cmd.Textf("echo '/%s 0 2000 0755';", p)
  1008. }
  1009. for _, dir := range appSetDirs {
  1010. cmd.Textf("echo '/%s 0 2000 0755';", dir)
  1011. file := appSetFiles[dir]
  1012. cmd.Text("zipinfo -1").Input(file).Textf(`| sed "s:\(.*\):/%s/\1 1000 1000 0644:";`, dir)
  1013. }
  1014. // Custom fs_config is "appended" to the last so that entries from the file are preferred
  1015. // over default ones set above.
  1016. if a.properties.Canned_fs_config != nil {
  1017. cmd.Text("cat").Input(android.PathForModuleSrc(ctx, *a.properties.Canned_fs_config))
  1018. }
  1019. cmd.Text(")").FlagWithOutput("> ", cannedFsConfig)
  1020. builder.Build("generateFsConfig", fmt.Sprintf("Generating canned fs config for %s", a.BaseModuleName()))
  1021. return cannedFsConfig.OutputPath
  1022. }
  1023. // Runs apex_sepolicy_tests
  1024. //
  1025. // $ deapexer list -Z {apex_file} > {file_contexts}
  1026. // $ apex_sepolicy_tests -f {file_contexts}
  1027. func runApexSepolicyTests(ctx android.ModuleContext, apexFile android.OutputPath) android.Path {
  1028. timestamp := android.PathForModuleOut(ctx, "sepolicy_tests.timestamp")
  1029. ctx.Build(pctx, android.BuildParams{
  1030. Rule: apexSepolicyTestsRule,
  1031. Input: apexFile,
  1032. Output: timestamp,
  1033. })
  1034. return timestamp
  1035. }