prebuilt.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. "fmt"
  17. "io"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. "android/soong/android"
  22. "android/soong/java"
  23. "android/soong/provenance"
  24. "github.com/google/blueprint"
  25. "github.com/google/blueprint/proptools"
  26. )
  27. var (
  28. extractMatchingApex = pctx.StaticRule(
  29. "extractMatchingApex",
  30. blueprint.RuleParams{
  31. Command: `rm -rf "$out" && ` +
  32. `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
  33. `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
  34. `-screen-densities=all -extract-single ` +
  35. `${in}`,
  36. CommandDeps: []string{"${extract_apks}"},
  37. },
  38. "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
  39. )
  40. type prebuilt interface {
  41. isForceDisabled() bool
  42. InstallFilename() string
  43. }
  44. type prebuiltCommon struct {
  45. android.ModuleBase
  46. prebuilt android.Prebuilt
  47. // Properties common to both prebuilt_apex and apex_set.
  48. prebuiltCommonProperties *PrebuiltCommonProperties
  49. installDir android.InstallPath
  50. installFilename string
  51. installedFile android.InstallPath
  52. outputApex android.WritablePath
  53. // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
  54. // to create make modules in prebuiltCommon.AndroidMkEntries.
  55. apexFilesForAndroidMk []apexFile
  56. // Installed locations of symlinks for backward compatibility.
  57. compatSymlinks android.InstallPaths
  58. hostRequired []string
  59. requiredModuleNames []string
  60. }
  61. type sanitizedPrebuilt interface {
  62. hasSanitizedSource(sanitizer string) bool
  63. }
  64. type PrebuiltCommonProperties struct {
  65. SelectedApexProperties
  66. // Canonical name of this APEX. Used to determine the path to the activated APEX on
  67. // device (/apex/<apex_name>). If unspecified, follows the name property.
  68. Apex_name *string
  69. ForceDisable bool `blueprint:"mutated"`
  70. // whether the extracted apex file is installable.
  71. Installable *bool
  72. // optional name for the installed apex. If unspecified, name of the
  73. // module is used as the file name
  74. Filename *string
  75. // names of modules to be overridden. Listed modules can only be other binaries
  76. // (in Make or Soong).
  77. // This does not completely prevent installation of the overridden binaries, but if both
  78. // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
  79. // from PRODUCT_PACKAGES.
  80. Overrides []string
  81. // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
  82. // APEX bundle will create an APEX variant and provide dex implementation jars for use by
  83. // dexpreopt and boot jars package check.
  84. Exported_java_libs []string
  85. // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
  86. // bundle will create an APEX variant.
  87. Exported_bootclasspath_fragments []string
  88. // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
  89. // APEX bundle will create an APEX variant.
  90. Exported_systemserverclasspath_fragments []string
  91. }
  92. // initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
  93. // module that is common to Prebuilt and ApexSet.
  94. func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
  95. p.prebuiltCommonProperties = properties
  96. android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
  97. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  98. }
  99. func (p *prebuiltCommon) ApexVariationName() string {
  100. return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
  101. }
  102. func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
  103. return &p.prebuilt
  104. }
  105. func (p *prebuiltCommon) isForceDisabled() bool {
  106. return p.prebuiltCommonProperties.ForceDisable
  107. }
  108. func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
  109. forceDisable := false
  110. // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
  111. // to build the prebuilts themselves.
  112. forceDisable = forceDisable || ctx.Config().UnbundledBuild()
  113. // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
  114. sanitized := ctx.Module().(sanitizedPrebuilt)
  115. forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
  116. forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
  117. if forceDisable && p.prebuilt.SourceExists() {
  118. p.prebuiltCommonProperties.ForceDisable = true
  119. return true
  120. }
  121. return false
  122. }
  123. func (p *prebuiltCommon) InstallFilename() string {
  124. return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
  125. }
  126. func (p *prebuiltCommon) Name() string {
  127. return p.prebuilt.Name(p.ModuleBase.Name())
  128. }
  129. func (p *prebuiltCommon) Overrides() []string {
  130. return p.prebuiltCommonProperties.Overrides
  131. }
  132. func (p *prebuiltCommon) installable() bool {
  133. return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
  134. }
  135. // initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
  136. // modules that this depends upon.
  137. func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
  138. // Walk the dependencies of this module looking for the java modules that it exports.
  139. ctx.WalkDeps(func(child, parent android.Module) bool {
  140. tag := ctx.OtherModuleDependencyTag(child)
  141. name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
  142. if java.IsBootclasspathFragmentContentDepTag(tag) ||
  143. java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
  144. // If the exported java module provides a dex jar path then add it to the list of apexFiles.
  145. path := child.(interface {
  146. DexJarBuildPath() java.OptionalDexJarPath
  147. }).DexJarBuildPath()
  148. if path.IsSet() {
  149. af := apexFile{
  150. module: child,
  151. moduleDir: ctx.OtherModuleDir(child),
  152. androidMkModuleName: name,
  153. builtFile: path.Path(),
  154. class: javaSharedLib,
  155. }
  156. if module, ok := child.(java.DexpreopterInterface); ok {
  157. for _, install := range module.DexpreoptBuiltInstalledForApex() {
  158. af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
  159. }
  160. }
  161. p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
  162. }
  163. } else if tag == exportedBootclasspathFragmentTag {
  164. _, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
  165. if !ok {
  166. ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
  167. return false
  168. }
  169. // Visit the children of the bootclasspath_fragment.
  170. return true
  171. } else if tag == exportedSystemserverclasspathFragmentTag {
  172. // Visit the children of the systemserver_fragment.
  173. return true
  174. }
  175. return false
  176. })
  177. }
  178. func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
  179. for _, fi := range p.apexFilesForAndroidMk {
  180. entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
  181. entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
  182. entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
  183. }
  184. entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
  185. }
  186. func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
  187. entriesList := []android.AndroidMkEntries{
  188. {
  189. Class: "ETC",
  190. OutputFile: android.OptionalPathForPath(p.outputApex),
  191. Include: "$(BUILD_PREBUILT)",
  192. Host_required: p.hostRequired,
  193. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  194. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  195. entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
  196. entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
  197. entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
  198. entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
  199. entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
  200. entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
  201. p.addRequiredModules(entries)
  202. },
  203. },
  204. },
  205. }
  206. // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
  207. // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
  208. // apex specific variants of the exported java modules available for use from within make.
  209. apexName := p.BaseModuleName()
  210. for _, fi := range p.apexFilesForAndroidMk {
  211. entries := p.createEntriesForApexFile(fi, apexName)
  212. entriesList = append(entriesList, entries)
  213. }
  214. return entriesList
  215. }
  216. // createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
  217. func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
  218. moduleName := fi.androidMkModuleName + "." + apexName
  219. entries := android.AndroidMkEntries{
  220. Class: fi.class.nameInMake(),
  221. OverrideName: moduleName,
  222. OutputFile: android.OptionalPathForPath(fi.builtFile),
  223. Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
  224. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  225. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  226. entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
  227. entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
  228. entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
  229. fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
  230. // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
  231. // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
  232. // we will have foo.jar.jar
  233. entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
  234. entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
  235. entries.SetString("LOCAL_DEX_PREOPT", "false")
  236. },
  237. },
  238. ExtraFooters: []android.AndroidMkExtraFootersFunc{
  239. func(w io.Writer, name, prefix, moduleDir string) {
  240. // m <module_name> will build <module_name>.<apex_name> as well.
  241. if fi.androidMkModuleName != moduleName {
  242. fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
  243. fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
  244. }
  245. },
  246. },
  247. }
  248. return entries
  249. }
  250. // prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
  251. // apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
  252. type prebuiltApexModuleCreator interface {
  253. createPrebuiltApexModules(ctx android.TopDownMutatorContext)
  254. }
  255. // prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
  256. // prebuiltApexModuleCreator's createPrebuiltApexModules method.
  257. //
  258. // It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
  259. // will need to access dependencies added by that (exported modules) but must run before the
  260. // DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
  261. // exported modules.
  262. func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
  263. module := ctx.Module()
  264. if creator, ok := module.(prebuiltApexModuleCreator); ok {
  265. creator.createPrebuiltApexModules(ctx)
  266. }
  267. }
  268. func (p *prebuiltCommon) hasExportedDeps() bool {
  269. return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
  270. len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
  271. len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
  272. }
  273. // prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
  274. func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
  275. module := ctx.Module()
  276. for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
  277. prebuiltDep := android.PrebuiltNameFromSource(dep)
  278. ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
  279. }
  280. for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
  281. prebuiltDep := android.PrebuiltNameFromSource(dep)
  282. ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
  283. }
  284. for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
  285. prebuiltDep := android.PrebuiltNameFromSource(dep)
  286. ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
  287. }
  288. }
  289. // Implements android.DepInInSameApex
  290. func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
  291. tag := ctx.OtherModuleDependencyTag(dep)
  292. _, ok := tag.(exportedDependencyTag)
  293. return ok
  294. }
  295. // apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
  296. // specific variant and checks that they are supported.
  297. //
  298. // The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
  299. // associated with the apex specific variant using the ApexInfoProvider for later retrieval.
  300. //
  301. // Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
  302. // across prebuilt_apex modules. That is because there is no way to determine whether two
  303. // prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
  304. // been built from different source at different times or they could have been built with different
  305. // build options that affect the libraries.
  306. //
  307. // While it may be possible to provide sufficient information to determine whether two prebuilt_apex
  308. // modules were compatible it would be a lot of work and would not provide much benefit for a couple
  309. // of reasons:
  310. // - The number of prebuilt_apex modules that will be exporting files for the same module will be
  311. // low as the prebuilt_apex only exports files for the direct dependencies that require it and
  312. // very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
  313. // few com.android.art* apex files that contain the same contents and could export files for the
  314. // same modules but only one of them needs to do so. Contrast that with source apex modules which
  315. // need apex specific variants for every module that contributes code to the apex, whether direct
  316. // or indirect.
  317. // - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
  318. // extra copying of files. Contrast that with source apex modules that has to build each variant
  319. // from source.
  320. func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
  321. // Collect direct dependencies into contents.
  322. contents := make(map[string]android.ApexMembership)
  323. // Collect the list of dependencies.
  324. var dependencies []android.ApexModule
  325. mctx.WalkDeps(func(child, parent android.Module) bool {
  326. // If the child is not in the same apex as the parent then exit immediately and do not visit
  327. // any of the child's dependencies.
  328. if !android.IsDepInSameApex(mctx, parent, child) {
  329. return false
  330. }
  331. tag := mctx.OtherModuleDependencyTag(child)
  332. depName := mctx.OtherModuleName(child)
  333. if exportedTag, ok := tag.(exportedDependencyTag); ok {
  334. propertyName := exportedTag.name
  335. // It is an error if the other module is not a prebuilt.
  336. if !android.IsModulePrebuilt(child) {
  337. mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
  338. return false
  339. }
  340. // It is an error if the other module is not an ApexModule.
  341. if _, ok := child.(android.ApexModule); !ok {
  342. mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
  343. return false
  344. }
  345. }
  346. // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
  347. // variant.
  348. if _, ok := child.(android.ApexModule); !ok {
  349. return false
  350. }
  351. // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
  352. // behavior whether there is a corresponding source module present or not.
  353. depName = android.RemoveOptionalPrebuiltPrefix(depName)
  354. // Remember if this module was added as a direct dependency.
  355. direct := parent == mctx.Module()
  356. contents[depName] = contents[depName].Add(direct)
  357. // Add the module to the list of dependencies that need to have an APEX variant.
  358. dependencies = append(dependencies, child.(android.ApexModule))
  359. return true
  360. })
  361. // Create contents for the prebuilt_apex and store it away for later use.
  362. apexContents := android.NewApexContents(contents)
  363. mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
  364. Contents: apexContents,
  365. })
  366. // Create an ApexInfo for the prebuilt_apex.
  367. apexVariationName := p.ApexVariationName()
  368. apexInfo := android.ApexInfo{
  369. ApexVariationName: apexVariationName,
  370. InApexVariants: []string{apexVariationName},
  371. InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
  372. ApexContents: []*android.ApexContents{apexContents},
  373. ForPrebuiltApex: true,
  374. }
  375. // Mark the dependencies of this module as requiring a variant for this module.
  376. for _, am := range dependencies {
  377. am.BuildForApex(apexInfo)
  378. }
  379. }
  380. // prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
  381. // module. It selects the apex to use and makes it available for use by prebuilt_apex and the
  382. // deapexer.
  383. type prebuiltApexSelectorModule struct {
  384. android.ModuleBase
  385. apexFileProperties ApexFileProperties
  386. inputApex android.Path
  387. }
  388. func privateApexSelectorModuleFactory() android.Module {
  389. module := &prebuiltApexSelectorModule{}
  390. module.AddProperties(
  391. &module.apexFileProperties,
  392. )
  393. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  394. return module
  395. }
  396. func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
  397. return android.Paths{p.inputApex}
  398. }
  399. func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  400. p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
  401. }
  402. type Prebuilt struct {
  403. prebuiltCommon
  404. properties PrebuiltProperties
  405. inputApex android.Path
  406. provenanceMetaDataFile android.OutputPath
  407. }
  408. type ApexFileProperties struct {
  409. // the path to the prebuilt .apex file to import.
  410. //
  411. // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
  412. // for android_common. That is so that it will have the same arch variant as, and so be compatible
  413. // with, the source `apex` module type that it replaces.
  414. Src *string `android:"path"`
  415. Arch struct {
  416. Arm struct {
  417. Src *string `android:"path"`
  418. }
  419. Arm64 struct {
  420. Src *string `android:"path"`
  421. }
  422. Riscv64 struct {
  423. Src *string `android:"path"`
  424. }
  425. X86 struct {
  426. Src *string `android:"path"`
  427. }
  428. X86_64 struct {
  429. Src *string `android:"path"`
  430. }
  431. }
  432. }
  433. // prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
  434. //
  435. // The ctx parameter can be for any module not just the prebuilt module so care must be taken not
  436. // to use methods on it that are specific to the current module.
  437. //
  438. // See the ApexFileProperties.Src property.
  439. func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
  440. multiTargets := prebuilt.MultiTargets()
  441. if len(multiTargets) != 1 {
  442. ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
  443. return nil
  444. }
  445. var src string
  446. switch multiTargets[0].Arch.ArchType {
  447. case android.Arm:
  448. src = String(p.Arch.Arm.Src)
  449. case android.Arm64:
  450. src = String(p.Arch.Arm64.Src)
  451. case android.Riscv64:
  452. src = String(p.Arch.Riscv64.Src)
  453. // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
  454. if src == "" {
  455. src = String(p.Arch.Arm64.Src)
  456. }
  457. case android.X86:
  458. src = String(p.Arch.X86.Src)
  459. case android.X86_64:
  460. src = String(p.Arch.X86_64.Src)
  461. }
  462. if src == "" {
  463. src = String(p.Src)
  464. }
  465. if src == "" {
  466. if ctx.Config().AllowMissingDependencies() {
  467. ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
  468. } else {
  469. ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
  470. }
  471. // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
  472. // logic from reporting a more general, less useful message.
  473. }
  474. return []string{src}
  475. }
  476. type PrebuiltProperties struct {
  477. ApexFileProperties
  478. PrebuiltCommonProperties
  479. }
  480. func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
  481. return false
  482. }
  483. func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
  484. switch tag {
  485. case "":
  486. return android.Paths{p.outputApex}, nil
  487. default:
  488. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  489. }
  490. }
  491. // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
  492. func PrebuiltFactory() android.Module {
  493. module := &Prebuilt{}
  494. module.AddProperties(&module.properties)
  495. module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
  496. return module
  497. }
  498. func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
  499. props := struct {
  500. Name *string
  501. }{
  502. Name: proptools.StringPtr(name),
  503. }
  504. ctx.CreateModule(privateApexSelectorModuleFactory,
  505. &props,
  506. apexFileProperties,
  507. )
  508. }
  509. // createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
  510. //
  511. // A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
  512. // the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
  513. // the listed modules need access to files from within the prebuilt .apex file.
  514. func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
  515. // Only create the deapexer module if it is needed.
  516. if !p.hasExportedDeps() {
  517. return
  518. }
  519. // Compute the deapexer properties from the transitive dependencies of this module.
  520. commonModules := []string{}
  521. exportedFiles := []string{}
  522. ctx.WalkDeps(func(child, parent android.Module) bool {
  523. tag := ctx.OtherModuleDependencyTag(child)
  524. // If the child is not in the same apex as the parent then ignore it and all its children.
  525. if !android.IsDepInSameApex(ctx, parent, child) {
  526. return false
  527. }
  528. name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
  529. if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
  530. commonModules = append(commonModules, name)
  531. requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
  532. exportedFiles = append(exportedFiles, requiredFiles...)
  533. // Visit the dependencies of this module just in case they also require files from the
  534. // prebuilt apex.
  535. return true
  536. }
  537. return false
  538. })
  539. // Create properties for deapexer module.
  540. deapexerProperties := &DeapexerProperties{
  541. // Remove any duplicates from the common modules lists as a module may be included via a direct
  542. // dependency as well as transitive ones.
  543. CommonModules: android.SortedUniqueStrings(commonModules),
  544. }
  545. // Populate the exported files property in a fixed order.
  546. deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
  547. props := struct {
  548. Name *string
  549. Selected_apex *string
  550. }{
  551. Name: proptools.StringPtr(deapexerName),
  552. Selected_apex: proptools.StringPtr(apexFileSource),
  553. }
  554. ctx.CreateModule(privateDeapexerFactory,
  555. &props,
  556. deapexerProperties,
  557. )
  558. }
  559. func apexSelectorModuleName(baseModuleName string) string {
  560. return baseModuleName + ".apex.selector"
  561. }
  562. func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
  563. // The prebuilt_apex should be depending on prebuilt modules but as this runs after
  564. // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
  565. // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
  566. // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
  567. // and not a renamed prebuilt module then that will be detected and reported as an error when
  568. // processing the dependency in ApexInfoMutator().
  569. prebuiltName := android.PrebuiltNameFromSource(name)
  570. if ctx.OtherModuleExists(prebuiltName) {
  571. name = prebuiltName
  572. }
  573. return name
  574. }
  575. type exportedDependencyTag struct {
  576. blueprint.BaseDependencyTag
  577. name string
  578. }
  579. // Mark this tag so dependencies that use it are excluded from visibility enforcement.
  580. //
  581. // This does allow any prebuilt_apex to reference any module which does open up a small window for
  582. // restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
  583. // avoids opening up a much bigger window by widening the visibility of modules that need files
  584. // provided by the prebuilt_apex to include all the possible locations they may be defined, which
  585. // could include everything below vendor/.
  586. //
  587. // A prebuilt_apex that references a module via this tag will have to contain the appropriate files
  588. // corresponding to that module, otherwise it will fail when attempting to retrieve the files from
  589. // the .apex file. It will also have to be included in the module's apex_available property too.
  590. // That makes it highly unlikely that a prebuilt_apex would reference a restricted module
  591. // incorrectly.
  592. func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
  593. func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
  594. var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
  595. var (
  596. exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
  597. exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
  598. exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
  599. )
  600. var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
  601. // createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
  602. // build.
  603. //
  604. // If this needs to make files from within a `.apex` file available for use by other Soong modules,
  605. // e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
  606. // it does so as follows:
  607. //
  608. // 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
  609. // makes them available for use by other modules, at both Soong and ninja levels.
  610. //
  611. // 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
  612. // an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
  613. // dexpreopt, will work the same way from source and prebuilt.
  614. //
  615. // 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
  616. // itself so that they can retrieve the file paths to those files.
  617. //
  618. // It also creates a child module `selector` that is responsible for selecting the appropriate
  619. // input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
  620. //
  621. // 1. To dedup the selection logic so it only runs in one module.
  622. //
  623. // 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
  624. // `apex_set`.
  625. //
  626. // prebuilt_apex
  627. // / | \
  628. // / | \
  629. // V V V
  630. // selector <--- deapexer <--- exported java lib
  631. func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
  632. baseModuleName := p.BaseModuleName()
  633. apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
  634. createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
  635. apexFileSource := ":" + apexSelectorModuleName
  636. p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
  637. // Add a source reference to retrieve the selected apex from the selector module.
  638. p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
  639. }
  640. func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
  641. p.prebuiltApexContentsDeps(ctx)
  642. }
  643. var _ ApexInfoMutator = (*Prebuilt)(nil)
  644. func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
  645. p.apexInfoMutator(mctx)
  646. }
  647. func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  648. // TODO(jungjw): Check the key validity.
  649. p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
  650. p.installDir = android.PathForModuleInstall(ctx, "apex")
  651. p.installFilename = p.InstallFilename()
  652. if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
  653. ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
  654. }
  655. p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
  656. ctx.Build(pctx, android.BuildParams{
  657. Rule: android.Cp,
  658. Input: p.inputApex,
  659. Output: p.outputApex,
  660. })
  661. if p.prebuiltCommon.checkForceDisable(ctx) {
  662. p.HideFromMake()
  663. return
  664. }
  665. // Save the files that need to be made available to Make.
  666. p.initApexFilesForAndroidMk(ctx)
  667. // in case that prebuilt_apex replaces source apex (using prefer: prop)
  668. p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
  669. // or that prebuilt_apex overrides other apexes (using overrides: prop)
  670. for _, overridden := range p.prebuiltCommonProperties.Overrides {
  671. p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
  672. }
  673. if p.installable() {
  674. p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
  675. p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
  676. }
  677. }
  678. func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
  679. return p.provenanceMetaDataFile
  680. }
  681. // prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
  682. // module. It extracts the correct apex to use and makes it available for use by apex_set.
  683. type prebuiltApexExtractorModule struct {
  684. android.ModuleBase
  685. properties ApexExtractorProperties
  686. extractedApex android.WritablePath
  687. }
  688. func privateApexExtractorModuleFactory() android.Module {
  689. module := &prebuiltApexExtractorModule{}
  690. module.AddProperties(
  691. &module.properties,
  692. )
  693. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  694. return module
  695. }
  696. func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
  697. return android.Paths{p.extractedApex}
  698. }
  699. func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  700. srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
  701. return p.properties.prebuiltSrcs(ctx)
  702. }
  703. defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
  704. apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
  705. p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
  706. // Filter out NativeBridge archs (b/260115309)
  707. abis := java.SupportedAbis(ctx, true)
  708. ctx.Build(pctx,
  709. android.BuildParams{
  710. Rule: extractMatchingApex,
  711. Description: "Extract an apex from an apex set",
  712. Inputs: android.Paths{apexSet},
  713. Output: p.extractedApex,
  714. Args: map[string]string{
  715. "abis": strings.Join(abis, ","),
  716. "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
  717. "sdk-version": ctx.Config().PlatformSdkVersion().String(),
  718. "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
  719. },
  720. })
  721. }
  722. type ApexSet struct {
  723. prebuiltCommon
  724. properties ApexSetProperties
  725. }
  726. type ApexExtractorProperties struct {
  727. // the .apks file path that contains prebuilt apex files to be extracted.
  728. Set *string `android:"path"`
  729. Sanitized struct {
  730. None struct {
  731. Set *string `android:"path"`
  732. }
  733. Address struct {
  734. Set *string `android:"path"`
  735. }
  736. Hwaddress struct {
  737. Set *string `android:"path"`
  738. }
  739. }
  740. // apexes in this set use prerelease SDK version
  741. Prerelease *bool
  742. }
  743. func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
  744. var srcs []string
  745. if e.Set != nil {
  746. srcs = append(srcs, *e.Set)
  747. }
  748. var sanitizers []string
  749. if ctx.Host() {
  750. sanitizers = ctx.Config().SanitizeHost()
  751. } else {
  752. sanitizers = ctx.Config().SanitizeDevice()
  753. }
  754. if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
  755. srcs = append(srcs, *e.Sanitized.Address.Set)
  756. } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
  757. srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
  758. } else if e.Sanitized.None.Set != nil {
  759. srcs = append(srcs, *e.Sanitized.None.Set)
  760. }
  761. return srcs
  762. }
  763. type ApexSetProperties struct {
  764. ApexExtractorProperties
  765. PrebuiltCommonProperties
  766. }
  767. func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
  768. if sanitizer == "address" {
  769. return a.properties.Sanitized.Address.Set != nil
  770. }
  771. if sanitizer == "hwaddress" {
  772. return a.properties.Sanitized.Hwaddress.Set != nil
  773. }
  774. return false
  775. }
  776. func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
  777. switch tag {
  778. case "":
  779. return android.Paths{a.outputApex}, nil
  780. default:
  781. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  782. }
  783. }
  784. // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
  785. func apexSetFactory() android.Module {
  786. module := &ApexSet{}
  787. module.AddProperties(&module.properties)
  788. module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
  789. return module
  790. }
  791. func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
  792. props := struct {
  793. Name *string
  794. }{
  795. Name: proptools.StringPtr(name),
  796. }
  797. ctx.CreateModule(privateApexExtractorModuleFactory,
  798. &props,
  799. apexExtractorProperties,
  800. )
  801. }
  802. func apexExtractorModuleName(baseModuleName string) string {
  803. return baseModuleName + ".apex.extractor"
  804. }
  805. var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
  806. // createPrebuiltApexModules creates modules necessary to export files from the apex set to other
  807. // modules.
  808. //
  809. // This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
  810. // prebuilt_apex except that instead of creating a selector module which selects one .apex file
  811. // from those provided this creates an extractor module which extracts the appropriate .apex file
  812. // from the zip file containing them.
  813. func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
  814. baseModuleName := a.BaseModuleName()
  815. apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
  816. createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
  817. apexFileSource := ":" + apexExtractorModuleName
  818. a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
  819. // After passing the arch specific src properties to the creating the apex selector module
  820. a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
  821. }
  822. func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
  823. a.prebuiltApexContentsDeps(ctx)
  824. }
  825. var _ ApexInfoMutator = (*ApexSet)(nil)
  826. func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
  827. a.apexInfoMutator(mctx)
  828. }
  829. func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  830. a.installFilename = a.InstallFilename()
  831. if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
  832. ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
  833. }
  834. inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
  835. a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
  836. ctx.Build(pctx, android.BuildParams{
  837. Rule: android.Cp,
  838. Input: inputApex,
  839. Output: a.outputApex,
  840. })
  841. if a.prebuiltCommon.checkForceDisable(ctx) {
  842. a.HideFromMake()
  843. return
  844. }
  845. // Save the files that need to be made available to Make.
  846. a.initApexFilesForAndroidMk(ctx)
  847. a.installDir = android.PathForModuleInstall(ctx, "apex")
  848. if a.installable() {
  849. a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
  850. }
  851. // in case that apex_set replaces source apex (using prefer: prop)
  852. a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
  853. // or that apex_set overrides other apexes (using overrides: prop)
  854. for _, overridden := range a.prebuiltCommonProperties.Overrides {
  855. a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
  856. }
  857. }
  858. type systemExtContext struct {
  859. android.ModuleContext
  860. }
  861. func (*systemExtContext) SystemExtSpecific() bool {
  862. return true
  863. }