prebuilt.go 36 KB

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