bootclasspath_fragment.go 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. // Copyright (C) 2021 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 java
  15. import (
  16. "fmt"
  17. "io"
  18. "path/filepath"
  19. "reflect"
  20. "strings"
  21. "android/soong/android"
  22. "android/soong/dexpreopt"
  23. "github.com/google/blueprint/proptools"
  24. "github.com/google/blueprint"
  25. )
  26. func init() {
  27. registerBootclasspathFragmentBuildComponents(android.InitRegistrationContext)
  28. android.RegisterSdkMemberType(BootclasspathFragmentSdkMemberType)
  29. }
  30. func registerBootclasspathFragmentBuildComponents(ctx android.RegistrationContext) {
  31. ctx.RegisterModuleType("bootclasspath_fragment", bootclasspathFragmentFactory)
  32. ctx.RegisterModuleType("bootclasspath_fragment_test", testBootclasspathFragmentFactory)
  33. ctx.RegisterModuleType("prebuilt_bootclasspath_fragment", prebuiltBootclasspathFragmentFactory)
  34. }
  35. // BootclasspathFragmentSdkMemberType is the member type used to add bootclasspath_fragments to
  36. // the SDK snapshot. It is exported for use by apex.
  37. var BootclasspathFragmentSdkMemberType = &bootclasspathFragmentMemberType{
  38. SdkMemberTypeBase: android.SdkMemberTypeBase{
  39. PropertyName: "bootclasspath_fragments",
  40. SupportsSdk: true,
  41. },
  42. }
  43. type bootclasspathFragmentContentDependencyTag struct {
  44. blueprint.BaseDependencyTag
  45. }
  46. // Avoid having to make bootclasspath_fragment content visible to the bootclasspath_fragment.
  47. //
  48. // This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
  49. // with proper dependencies.
  50. // TODO(b/177892522): Remove this and add needed visibility.
  51. func (b bootclasspathFragmentContentDependencyTag) ExcludeFromVisibilityEnforcement() {
  52. }
  53. // The bootclasspath_fragment contents must never depend on prebuilts.
  54. func (b bootclasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool {
  55. return false
  56. }
  57. // SdkMemberType causes dependencies added with this tag to be automatically added to the sdk as if
  58. // they were specified using java_boot_libs or java_sdk_libs.
  59. func (b bootclasspathFragmentContentDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
  60. // If the module is a java_sdk_library then treat it as if it was specified in the java_sdk_libs
  61. // property, otherwise treat if it was specified in the java_boot_libs property.
  62. if javaSdkLibrarySdkMemberType.IsInstance(child) {
  63. return javaSdkLibrarySdkMemberType
  64. }
  65. return javaBootLibsSdkMemberType
  66. }
  67. func (b bootclasspathFragmentContentDependencyTag) ExportMember() bool {
  68. return true
  69. }
  70. // Contents of bootclasspath fragments in an apex are considered to be directly in the apex, as if
  71. // they were listed in java_libs.
  72. func (b bootclasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {}
  73. // Contents of bootclasspath fragments require files from prebuilt apex files.
  74. func (b bootclasspathFragmentContentDependencyTag) RequiresFilesFromPrebuiltApex() {}
  75. // The tag used for the dependency between the bootclasspath_fragment module and its contents.
  76. var bootclasspathFragmentContentDepTag = bootclasspathFragmentContentDependencyTag{}
  77. var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathFragmentContentDepTag
  78. var _ android.ReplaceSourceWithPrebuilt = bootclasspathFragmentContentDepTag
  79. var _ android.SdkMemberDependencyTag = bootclasspathFragmentContentDepTag
  80. var _ android.CopyDirectlyInAnyApexTag = bootclasspathFragmentContentDepTag
  81. var _ android.RequiresFilesFromPrebuiltApexTag = bootclasspathFragmentContentDepTag
  82. func IsBootclasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
  83. return tag == bootclasspathFragmentContentDepTag
  84. }
  85. // Properties that can be different when coverage is enabled.
  86. type BootclasspathFragmentCoverageAffectedProperties struct {
  87. // The contents of this bootclasspath_fragment, could be either java_library, or java_sdk_library.
  88. //
  89. // A java_sdk_library specified here will also be treated as if it was specified on the stub_libs
  90. // property.
  91. //
  92. // The order of this list matters as it is the order that is used in the bootclasspath.
  93. Contents []string
  94. // The properties for specifying the API stubs provided by this fragment.
  95. BootclasspathAPIProperties
  96. }
  97. type bootclasspathFragmentProperties struct {
  98. // The name of the image this represents.
  99. //
  100. // If specified then it must be one of "art" or "boot".
  101. Image_name *string
  102. // Properties whose values need to differ with and without coverage.
  103. BootclasspathFragmentCoverageAffectedProperties
  104. Coverage BootclasspathFragmentCoverageAffectedProperties
  105. // Hidden API related properties.
  106. HiddenAPIFlagFileProperties
  107. // The list of additional stub libraries which this fragment's contents use but which are not
  108. // provided by another bootclasspath_fragment.
  109. //
  110. // Note, "android-non-updatable" is treated specially. While no such module exists it is treated
  111. // as if it was a java_sdk_library. So, when public API stubs are needed then it will be replaced
  112. // with "android-non-updatable.stubs", with "androidn-non-updatable.system.stubs" when the system
  113. // stubs are needed and so on.
  114. Additional_stubs []string
  115. // Properties that allow a fragment to depend on other fragments. This is needed for hidden API
  116. // processing as it needs access to all the classes used by a fragment including those provided
  117. // by other fragments.
  118. BootclasspathFragmentsDepsProperties
  119. }
  120. type HiddenAPIPackageProperties struct {
  121. Hidden_api struct {
  122. // Contains prefixes of a package hierarchy that is provided solely by this
  123. // bootclasspath_fragment.
  124. //
  125. // This affects the signature patterns file that is used to select the subset of monolithic
  126. // hidden API flags. See split_packages property for more details.
  127. Package_prefixes []string
  128. // A list of individual packages that are provided solely by this
  129. // bootclasspath_fragment but which cannot be listed in package_prefixes
  130. // because there are sub-packages which are provided by other modules.
  131. //
  132. // This should only be used for legacy packages. New packages should be
  133. // covered by a package prefix.
  134. Single_packages []string
  135. // The list of split packages provided by this bootclasspath_fragment.
  136. //
  137. // A split package is one that contains classes which are provided by multiple
  138. // bootclasspath_fragment modules.
  139. //
  140. // This defaults to "*" - which treats all packages as being split. A module that has no split
  141. // packages must specify an empty list.
  142. //
  143. // This affects the signature patterns file that is generated by a bootclasspath_fragment and
  144. // used to select the subset of monolithic hidden API flags against which the flags generated
  145. // by the bootclasspath_fragment are compared.
  146. //
  147. // The signature patterns file selects the subset of monolithic hidden API flags using a number
  148. // of patterns, i.e.:
  149. // * The qualified name (including package) of an outermost class, e.g. java/lang/Character.
  150. // This selects all the flags for all the members of this class and any nested classes.
  151. // * A package wildcard, e.g. java/lang/*. This selects all the flags for all the members of all
  152. // the classes in this package (but not in sub-packages).
  153. // * A recursive package wildcard, e.g. java/**. This selects all the flags for all the members
  154. // of all the classes in this package and sub-packages.
  155. //
  156. // The signature patterns file is constructed as follows:
  157. // * All the signatures are retrieved from the all-flags.csv file.
  158. // * The member and inner class names are removed.
  159. // * If a class is in a split package then that is kept, otherwise the class part is removed
  160. // and replaced with a wildcard, i.e. *.
  161. // * If a package matches a package prefix then the package is removed.
  162. // * All the package prefixes are added with a recursive wildcard appended to each, i.e. **.
  163. // * The resulting patterns are sorted.
  164. //
  165. // So, by default (i.e. without specifying any package_prefixes or split_packages) the signature
  166. // patterns is a list of class names, because there are no package packages and all packages are
  167. // assumed to be split.
  168. //
  169. // If any split packages are specified then only those packages are treated as split and all
  170. // other packages are treated as belonging solely to the bootclasspath_fragment and so they use
  171. // wildcard package patterns.
  172. //
  173. // So, if an empty list of split packages is specified then the signature patterns file just
  174. // includes a wildcard package pattern for every package provided by the bootclasspath_fragment.
  175. //
  176. // If split_packages are specified and a package that is split is not listed then it could lead
  177. // to build failures as it will select monolithic flags that are generated by another
  178. // bootclasspath_fragment to compare against the flags provided by this fragment. The latter
  179. // will obviously not contain those flags and that can cause the comparison and build to fail.
  180. //
  181. // If any package prefixes are specified then any matching packages are removed from the
  182. // signature patterns and replaced with a single recursive package pattern.
  183. //
  184. // It is not strictly necessary to specify either package_prefixes or split_packages as the
  185. // defaults will produce a valid set of signature patterns. However, those patterns may include
  186. // implementation details, e.g. names of implementation classes or packages, which will be
  187. // exported to the sdk snapshot in the signature patterns file. That is something that should be
  188. // avoided where possible. Specifying package_prefixes and split_packages allows those
  189. // implementation details to be excluded from the snapshot.
  190. Split_packages []string
  191. }
  192. }
  193. type SourceOnlyBootclasspathProperties struct {
  194. HiddenAPIPackageProperties
  195. Coverage HiddenAPIPackageProperties
  196. }
  197. type BootclasspathFragmentModule struct {
  198. android.ModuleBase
  199. android.ApexModuleBase
  200. ClasspathFragmentBase
  201. // True if this fragment is for testing purposes.
  202. testFragment bool
  203. properties bootclasspathFragmentProperties
  204. sourceOnlyProperties SourceOnlyBootclasspathProperties
  205. // Collect the module directory for IDE info in java/jdeps.go.
  206. modulePaths []string
  207. // Path to the boot image profile.
  208. profilePath android.Path
  209. }
  210. // commonBootclasspathFragment defines the methods that are implemented by both source and prebuilt
  211. // bootclasspath fragment modules.
  212. type commonBootclasspathFragment interface {
  213. // produceHiddenAPIOutput produces the all-flags.csv and intermediate files and encodes the flags
  214. // into dex files.
  215. //
  216. // Returns a *HiddenAPIOutput containing the paths for the generated files. Returns nil if the
  217. // module cannot contribute to hidden API processing, e.g. because it is a prebuilt module in a
  218. // versioned sdk.
  219. produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput
  220. // produceBootImageFiles will attempt to produce rules to create the boot image files at the paths
  221. // predefined in the bootImageConfig.
  222. //
  223. // If it could not create the files then it will return nil. Otherwise, it will return a map from
  224. // android.ArchType to the predefined paths of the boot image files.
  225. produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs
  226. // getImageName returns the `image_name` property of this fragment.
  227. getImageName() *string
  228. // getProfilePath returns the path to the boot image profile.
  229. getProfilePath() android.Path
  230. }
  231. var _ commonBootclasspathFragment = (*BootclasspathFragmentModule)(nil)
  232. func bootclasspathFragmentFactory() android.Module {
  233. m := &BootclasspathFragmentModule{}
  234. m.AddProperties(&m.properties, &m.sourceOnlyProperties)
  235. android.InitApexModule(m)
  236. initClasspathFragment(m, BOOTCLASSPATH)
  237. android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
  238. android.AddLoadHook(m, func(ctx android.LoadHookContext) {
  239. // If code coverage has been enabled for the framework then append the properties with
  240. // coverage specific properties.
  241. if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
  242. err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
  243. if err != nil {
  244. ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
  245. return
  246. }
  247. err = proptools.AppendProperties(&m.sourceOnlyProperties.HiddenAPIPackageProperties, &m.sourceOnlyProperties.Coverage, nil)
  248. if err != nil {
  249. ctx.PropertyErrorf("coverage", "error trying to append hidden api coverage specific properties: %s", err)
  250. return
  251. }
  252. }
  253. // Initialize the contents property from the image_name.
  254. bootclasspathFragmentInitContentsFromImage(ctx, m)
  255. })
  256. return m
  257. }
  258. func testBootclasspathFragmentFactory() android.Module {
  259. m := bootclasspathFragmentFactory().(*BootclasspathFragmentModule)
  260. m.testFragment = true
  261. return m
  262. }
  263. // bootclasspathFragmentInitContentsFromImage will initialize the contents property from the image_name if
  264. // necessary.
  265. func bootclasspathFragmentInitContentsFromImage(ctx android.EarlyModuleContext, m *BootclasspathFragmentModule) {
  266. contents := m.properties.Contents
  267. if len(contents) == 0 {
  268. ctx.PropertyErrorf("contents", "required property is missing")
  269. return
  270. }
  271. if m.properties.Image_name == nil {
  272. // Nothing to do.
  273. return
  274. }
  275. imageName := proptools.String(m.properties.Image_name)
  276. if imageName != "art" {
  277. ctx.PropertyErrorf("image_name", `unknown image name %q, expected "art"`, imageName)
  278. return
  279. }
  280. // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
  281. // too early in the Soong processing for that to work.
  282. global := dexpreopt.GetGlobalConfig(ctx)
  283. modules := global.ArtApexJars
  284. // Make sure that the apex specified in the configuration is consistent and is one for which
  285. // this boot image is available.
  286. commonApex := ""
  287. for i := 0; i < modules.Len(); i++ {
  288. apex := modules.Apex(i)
  289. jar := modules.Jar(i)
  290. if apex == "platform" {
  291. ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
  292. continue
  293. }
  294. if !m.AvailableFor(apex) {
  295. ctx.ModuleErrorf("ArtApexJars configuration incompatible with this module, ArtApexJars expects this to be in apex %q but this is only in apexes %q",
  296. apex, m.ApexAvailable())
  297. continue
  298. }
  299. if commonApex == "" {
  300. commonApex = apex
  301. } else if commonApex != apex {
  302. ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
  303. commonApex, apex)
  304. }
  305. }
  306. }
  307. // bootclasspathImageNameContentsConsistencyCheck checks that the configuration that applies to this
  308. // module (if any) matches the contents.
  309. //
  310. // This should be a noop as if image_name="art" then the contents will be set from the ArtApexJars
  311. // config by bootclasspathFragmentInitContentsFromImage so it will be guaranteed to match. However,
  312. // in future this will not be the case.
  313. func (b *BootclasspathFragmentModule) bootclasspathImageNameContentsConsistencyCheck(ctx android.BaseModuleContext) {
  314. imageName := proptools.String(b.properties.Image_name)
  315. if imageName == "art" {
  316. // Get the configuration for the art apex jars.
  317. modules := b.getImageConfig(ctx).modules
  318. configuredJars := modules.CopyOfJars()
  319. // Skip the check if the configured jars list is empty as that is a common configuration when
  320. // building targets that do not result in a system image.
  321. if len(configuredJars) == 0 {
  322. return
  323. }
  324. contents := b.properties.Contents
  325. if !reflect.DeepEqual(configuredJars, contents) {
  326. ctx.ModuleErrorf("inconsistency in specification of contents. ArtApexJars configuration specifies %#v, contents property specifies %#v",
  327. configuredJars, contents)
  328. }
  329. }
  330. }
  331. var BootclasspathFragmentApexContentInfoProvider = blueprint.NewProvider(BootclasspathFragmentApexContentInfo{})
  332. // BootclasspathFragmentApexContentInfo contains the bootclasspath_fragments contributions to the
  333. // apex contents.
  334. type BootclasspathFragmentApexContentInfo struct {
  335. // The configured modules, will be empty if this is from a bootclasspath_fragment that does not
  336. // set image_name: "art".
  337. modules android.ConfiguredJarList
  338. // Map from the base module name (without prebuilt_ prefix) of a fragment's contents module to the
  339. // hidden API encoded dex jar path.
  340. contentModuleDexJarPaths bootDexJarByModule
  341. // Path to the image profile file on host (or empty, if profile is not generated).
  342. profilePathOnHost android.Path
  343. // Install path of the boot image profile if it needs to be installed in the APEX, or empty if not
  344. // needed.
  345. profileInstallPathInApex string
  346. }
  347. func (i BootclasspathFragmentApexContentInfo) Modules() android.ConfiguredJarList {
  348. return i.modules
  349. }
  350. // DexBootJarPathForContentModule returns the path to the dex boot jar for specified module.
  351. //
  352. // The dex boot jar is one which has had hidden API encoding performed on it.
  353. func (i BootclasspathFragmentApexContentInfo) DexBootJarPathForContentModule(module android.Module) (android.Path, error) {
  354. // A bootclasspath_fragment cannot use a prebuilt library so Name() will return the base name
  355. // without a prebuilt_ prefix so is safe to use as the key for the contentModuleDexJarPaths.
  356. name := module.Name()
  357. if dexJar, ok := i.contentModuleDexJarPaths[name]; ok {
  358. return dexJar, nil
  359. } else {
  360. return nil, fmt.Errorf("unknown bootclasspath_fragment content module %s, expected one of %s",
  361. name, strings.Join(android.SortedKeys(i.contentModuleDexJarPaths), ", "))
  362. }
  363. }
  364. func (i BootclasspathFragmentApexContentInfo) ProfilePathOnHost() android.Path {
  365. return i.profilePathOnHost
  366. }
  367. func (i BootclasspathFragmentApexContentInfo) ProfileInstallPathInApex() string {
  368. return i.profileInstallPathInApex
  369. }
  370. func (b *BootclasspathFragmentModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
  371. tag := ctx.OtherModuleDependencyTag(dep)
  372. if IsBootclasspathFragmentContentDepTag(tag) {
  373. // Boot image contents are automatically added to apex.
  374. return true
  375. }
  376. if android.IsMetaDependencyTag(tag) {
  377. // Cross-cutting metadata dependencies are metadata.
  378. return false
  379. }
  380. panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
  381. }
  382. func (b *BootclasspathFragmentModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
  383. return nil
  384. }
  385. // ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
  386. // corresponding source module are renamed. This means that adding a dependency using a name without
  387. // a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
  388. // it will always resolve to a prebuilt module.
  389. func (b *BootclasspathFragmentModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
  390. module := ctx.Module()
  391. _, isSourceModule := module.(*BootclasspathFragmentModule)
  392. for _, name := range b.properties.Contents {
  393. // A bootclasspath_fragment must depend only on other source modules, while the
  394. // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
  395. //
  396. // TODO(b/177892522) - avoid special handling of jacocoagent.
  397. if !isSourceModule && name != "jacocoagent" {
  398. name = android.PrebuiltNameFromSource(name)
  399. }
  400. ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
  401. }
  402. }
  403. func (b *BootclasspathFragmentModule) DepsMutator(ctx android.BottomUpMutatorContext) {
  404. // Add dependencies onto all the modules that provide the API stubs for classes on this
  405. // bootclasspath fragment.
  406. hiddenAPIAddStubLibDependencies(ctx, b.properties.apiScopeToStubLibs())
  407. for _, additionalStubModule := range b.properties.Additional_stubs {
  408. for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
  409. // Add a dependency onto a possibly scope specific stub library.
  410. scopeSpecificDependency := apiScope.scopeSpecificStubModule(ctx, additionalStubModule)
  411. tag := hiddenAPIStubsDependencyTag{apiScope: apiScope, fromAdditionalDependency: true}
  412. ctx.AddVariationDependencies(nil, tag, scopeSpecificDependency)
  413. }
  414. }
  415. if !dexpreopt.IsDex2oatNeeded(ctx) {
  416. return
  417. }
  418. // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
  419. // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
  420. dexpreopt.RegisterToolDeps(ctx)
  421. }
  422. func (b *BootclasspathFragmentModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
  423. // Add dependencies on all the fragments.
  424. b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
  425. }
  426. func (b *BootclasspathFragmentModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  427. // Only perform a consistency check if this module is the active module. That will prevent an
  428. // unused prebuilt that was created without instrumentation from breaking an instrumentation
  429. // build.
  430. if isActiveModule(ctx.Module()) {
  431. b.bootclasspathImageNameContentsConsistencyCheck(ctx)
  432. }
  433. // Generate classpaths.proto config
  434. b.generateClasspathProtoBuildActions(ctx)
  435. // Collect the module directory for IDE info in java/jdeps.go.
  436. b.modulePaths = append(b.modulePaths, ctx.ModuleDir())
  437. // Gather the bootclasspath fragment's contents.
  438. var contents []android.Module
  439. ctx.VisitDirectDeps(func(module android.Module) {
  440. tag := ctx.OtherModuleDependencyTag(module)
  441. if IsBootclasspathFragmentContentDepTag(tag) {
  442. contents = append(contents, module)
  443. }
  444. })
  445. fragments := gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
  446. // Verify that the image_name specified on a bootclasspath_fragment is valid even if this is a
  447. // prebuilt which will not use the image config.
  448. imageConfig := b.getImageConfig(ctx)
  449. // Perform hidden API processing.
  450. hiddenAPIOutput := b.generateHiddenAPIBuildActions(ctx, contents, fragments)
  451. var bootImageFiles bootImageOutputs
  452. if imageConfig != nil {
  453. // Delegate the production of the boot image files to a module type specific method.
  454. common := ctx.Module().(commonBootclasspathFragment)
  455. bootImageFiles = common.produceBootImageFiles(ctx, imageConfig)
  456. b.profilePath = bootImageFiles.profile
  457. if shouldCopyBootFilesToPredefinedLocations(ctx, imageConfig) {
  458. // Zip the boot image files up, if available. This will generate the zip file in a
  459. // predefined location.
  460. buildBootImageZipInPredefinedLocation(ctx, imageConfig, bootImageFiles.byArch)
  461. // Copy the dex jars of this fragment's content modules to their predefined locations.
  462. copyBootJarsToPredefinedLocations(ctx, hiddenAPIOutput.EncodedBootDexFilesByModule, imageConfig.dexPathsByModule)
  463. }
  464. }
  465. // A prebuilt fragment cannot contribute to an apex.
  466. if !android.IsModulePrebuilt(ctx.Module()) {
  467. // Provide the apex content info.
  468. b.provideApexContentInfo(ctx, imageConfig, hiddenAPIOutput, bootImageFiles)
  469. }
  470. // In order for information about bootclasspath_fragment modules to be added to module-info.json
  471. // it is necessary to output an entry to Make. As bootclasspath_fragment modules are part of an
  472. // APEX there can be multiple variants, including the default/platform variant and only one can
  473. // be output to Make but it does not really matter which variant is output. The default/platform
  474. // variant is the first (ctx.PrimaryModule()) and is usually hidden from make so this just picks
  475. // the last variant (ctx.FinalModule()).
  476. if ctx.Module() != ctx.FinalModule() {
  477. b.HideFromMake()
  478. }
  479. }
  480. // shouldCopyBootFilesToPredefinedLocations determines whether the current module should copy boot
  481. // files, e.g. boot dex jars or boot image files, to the predefined location expected by the rest
  482. // of the build.
  483. //
  484. // This ensures that only a single module will copy its files to the image configuration.
  485. func shouldCopyBootFilesToPredefinedLocations(ctx android.ModuleContext, imageConfig *bootImageConfig) bool {
  486. // Bootclasspath fragment modules that are for the platform do not produce boot related files.
  487. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  488. if apexInfo.IsForPlatform() {
  489. return false
  490. }
  491. // If the image configuration has no modules specified then it means that the build has been
  492. // configured to build something other than a boot image, e.g. an sdk, so do not try and copy the
  493. // files.
  494. if imageConfig.modules.Len() == 0 {
  495. return false
  496. }
  497. // Only copy files from the module that is preferred.
  498. return isActiveModule(ctx.Module())
  499. }
  500. // provideApexContentInfo creates, initializes and stores the apex content info for use by other
  501. // modules.
  502. func (b *BootclasspathFragmentModule) provideApexContentInfo(ctx android.ModuleContext, imageConfig *bootImageConfig, hiddenAPIOutput *HiddenAPIOutput, bootImageFiles bootImageOutputs) {
  503. // Construct the apex content info from the config.
  504. info := BootclasspathFragmentApexContentInfo{
  505. // Populate the apex content info with paths to the dex jars.
  506. contentModuleDexJarPaths: hiddenAPIOutput.EncodedBootDexFilesByModule,
  507. }
  508. if imageConfig != nil {
  509. info.modules = imageConfig.modules
  510. global := dexpreopt.GetGlobalConfig(ctx)
  511. if !global.DisableGenerateProfile {
  512. info.profilePathOnHost = bootImageFiles.profile
  513. info.profileInstallPathInApex = imageConfig.profileInstallPathInApex
  514. }
  515. }
  516. // Make the apex content info available for other modules.
  517. ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
  518. }
  519. // generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
  520. func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
  521. var classpathJars []classpathJar
  522. configuredJars := b.configuredJars(ctx)
  523. if "art" == proptools.String(b.properties.Image_name) {
  524. // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
  525. classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
  526. } else {
  527. classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, b.classpathType)
  528. }
  529. b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
  530. }
  531. func (b *BootclasspathFragmentModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
  532. if "art" == proptools.String(b.properties.Image_name) {
  533. return b.getImageConfig(ctx).modules
  534. }
  535. global := dexpreopt.GetGlobalConfig(ctx)
  536. possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, b.properties.Contents, bootclasspathFragmentContentDepTag)
  537. jars, unknown := global.ApexBootJars.Filter(possibleUpdatableModules)
  538. // TODO(satayev): for apex_test we want to include all contents unconditionally to classpaths
  539. // config. However, any test specific jars would not be present in ApexBootJars. Instead,
  540. // we should check if we are creating a config for apex_test via ApexInfo and amend the values.
  541. // This is an exception to support end-to-end test for SdkExtensions, until such support exists.
  542. if android.InList("test_framework-sdkextensions", possibleUpdatableModules) {
  543. jars = jars.Append("com.android.sdkext", "test_framework-sdkextensions")
  544. } else if android.InList("test_framework-apexd", possibleUpdatableModules) {
  545. jars = jars.Append("com.android.apex.test_package", "test_framework-apexd")
  546. } else if global.ApexBootJars.Len() != 0 {
  547. unknown = android.RemoveListFromList(unknown, b.properties.Coverage.Contents)
  548. _, unknown = android.RemoveFromList("core-icu4j", unknown)
  549. // This module only exists in car products.
  550. // So ignore it even if it is not in PRODUCT_APEX_BOOT_JARS.
  551. // TODO(b/202896428): Add better way to handle this.
  552. _, unknown = android.RemoveFromList("android.car-module", unknown)
  553. if len(unknown) > 0 {
  554. ctx.ModuleErrorf("%s in contents must also be declared in PRODUCT_APEX_BOOT_JARS", unknown)
  555. }
  556. }
  557. return jars
  558. }
  559. func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
  560. // Get a map of the image configs that are supported.
  561. imageConfigs := genBootImageConfigs(ctx)
  562. // Retrieve the config for this image.
  563. imageNamePtr := b.properties.Image_name
  564. if imageNamePtr == nil {
  565. return nil
  566. }
  567. imageName := *imageNamePtr
  568. imageConfig := imageConfigs[imageName]
  569. if imageConfig == nil {
  570. ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedKeys(imageConfigs), ", "))
  571. return nil
  572. }
  573. return imageConfig
  574. }
  575. // generateHiddenAPIBuildActions generates all the hidden API related build rules.
  576. func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) *HiddenAPIOutput {
  577. // Create hidden API input structure.
  578. input := b.createHiddenAPIFlagInput(ctx, contents, fragments)
  579. // Delegate the production of the hidden API all-flags.csv file to a module type specific method.
  580. common := ctx.Module().(commonBootclasspathFragment)
  581. output := common.produceHiddenAPIOutput(ctx, contents, fragments, input)
  582. // If the source or prebuilts module does not provide a signature patterns file then generate one
  583. // from the flags.
  584. // TODO(b/192868581): Remove once the source and prebuilts provide a signature patterns file of
  585. // their own.
  586. if output.SignaturePatternsPath == nil {
  587. output.SignaturePatternsPath = buildRuleSignaturePatternsFile(
  588. ctx, output.AllFlagsPath, []string{"*"}, nil, nil, "")
  589. }
  590. // Initialize a HiddenAPIInfo structure.
  591. hiddenAPIInfo := HiddenAPIInfo{
  592. // The monolithic hidden API processing needs access to the flag files that override the default
  593. // flags from all the fragments whether or not they actually perform their own hidden API flag
  594. // generation. That is because the monolithic hidden API processing uses those flag files to
  595. // perform its own flag generation.
  596. FlagFilesByCategory: input.FlagFilesByCategory,
  597. // Other bootclasspath_fragments that depend on this need the transitive set of stub dex jars
  598. // from this to resolve any references from their code to classes provided by this fragment
  599. // and the fragments this depends upon.
  600. TransitiveStubDexJarsByScope: input.transitiveStubDexJarsByScope(),
  601. }
  602. // The monolithic hidden API processing also needs access to all the output files produced by
  603. // hidden API processing of this fragment.
  604. hiddenAPIInfo.HiddenAPIFlagOutput = output.HiddenAPIFlagOutput
  605. // Provide it for use by other modules.
  606. ctx.SetProvider(HiddenAPIInfoProvider, hiddenAPIInfo)
  607. return output
  608. }
  609. // retrieveLegacyEncodedBootDexFiles attempts to retrieve the legacy encoded boot dex jar files.
  610. func retrieveLegacyEncodedBootDexFiles(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule {
  611. // If the current bootclasspath_fragment is the active module or a source module then retrieve the
  612. // encoded dex files, otherwise return an empty map.
  613. //
  614. // An inactive (i.e. not preferred) bootclasspath_fragment needs to retrieve the encoded dex jars
  615. // as they are still needed by an apex. An inactive prebuilt_bootclasspath_fragment does not need
  616. // to do so and may not yet have access to dex boot jars from a prebuilt_apex/apex_set.
  617. if isActiveModule(ctx.Module()) || !android.IsModulePrebuilt(ctx.Module()) {
  618. return extractEncodedDexJarsFromModules(ctx, contents)
  619. } else {
  620. return nil
  621. }
  622. }
  623. // createHiddenAPIFlagInput creates a HiddenAPIFlagInput struct and initializes it with information derived
  624. // from the properties on this module and its dependencies.
  625. func (b *BootclasspathFragmentModule) createHiddenAPIFlagInput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) HiddenAPIFlagInput {
  626. // Merge the HiddenAPIInfo from all the fragment dependencies.
  627. dependencyHiddenApiInfo := newHiddenAPIInfo()
  628. dependencyHiddenApiInfo.mergeFromFragmentDeps(ctx, fragments)
  629. // Create hidden API flag input structure.
  630. input := newHiddenAPIFlagInput()
  631. // Update the input structure with information obtained from the stub libraries.
  632. input.gatherStubLibInfo(ctx, contents)
  633. // Populate with flag file paths from the properties.
  634. input.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
  635. // Populate with package rules from the properties.
  636. input.extractPackageRulesFromProperties(&b.sourceOnlyProperties.HiddenAPIPackageProperties)
  637. input.gatherPropertyInfo(ctx, contents)
  638. // Add the stub dex jars from this module's fragment dependencies.
  639. input.DependencyStubDexJarsByScope.addStubDexJarsByModule(dependencyHiddenApiInfo.TransitiveStubDexJarsByScope)
  640. return input
  641. }
  642. // isTestFragment returns true if the current module is a test bootclasspath_fragment.
  643. func (b *BootclasspathFragmentModule) isTestFragment() bool {
  644. return b.testFragment
  645. }
  646. // generateHiddenApiFlagRules generates rules to generate hidden API flags and compute the signature
  647. // patterns file.
  648. func (b *BootclasspathFragmentModule) generateHiddenApiFlagRules(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput, bootDexInfoByModule bootDexInfoByModule, suffix string) HiddenAPIFlagOutput {
  649. // Generate the rules to create the hidden API flags and update the supplied hiddenAPIInfo with the
  650. // paths to the created files.
  651. flagOutput := hiddenAPIFlagRulesForBootclasspathFragment(ctx, bootDexInfoByModule, contents, input, suffix)
  652. // If the module specifies split_packages or package_prefixes then use those to generate the
  653. // signature patterns.
  654. splitPackages := input.SplitPackages
  655. packagePrefixes := input.PackagePrefixes
  656. singlePackages := input.SinglePackages
  657. if splitPackages != nil || packagePrefixes != nil || singlePackages != nil {
  658. flagOutput.SignaturePatternsPath = buildRuleSignaturePatternsFile(
  659. ctx, flagOutput.AllFlagsPath, splitPackages, packagePrefixes, singlePackages, suffix)
  660. } else if !b.isTestFragment() {
  661. ctx.ModuleErrorf(`Must specify at least one of the split_packages, package_prefixes and single_packages properties
  662. If this is a new bootclasspath_fragment or you are unsure what to do add the
  663. the following to the bootclasspath_fragment:
  664. hidden_api: {split_packages: ["*"]},
  665. and then run the following:
  666. m analyze_bcpf && analyze_bcpf --bcpf %q
  667. it will analyze the bootclasspath_fragment and provide hints as to what you
  668. should specify here. If you are happy with its suggestions then you can add
  669. the --fix option and it will fix them for you.`, b.BaseModuleName())
  670. }
  671. return flagOutput
  672. }
  673. // produceHiddenAPIOutput produces the hidden API all-flags.csv file (and supporting files)
  674. // for the fragment as well as encoding the flags in the boot dex jars.
  675. func (b *BootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
  676. // Gather information about the boot dex files for the boot libraries provided by this fragment.
  677. bootDexInfoByModule := extractBootDexInfoFromModules(ctx, contents)
  678. // Generate the flag file needed to encode into the dex files.
  679. flagOutput := b.generateHiddenApiFlagRules(ctx, contents, input, bootDexInfoByModule, "")
  680. // Encode those flags into the dex files of the contents of this fragment.
  681. encodedBootDexFilesByModule := hiddenAPIEncodeRulesForBootclasspathFragment(ctx, bootDexInfoByModule, flagOutput.AllFlagsPath)
  682. // Store that information for return for use by other rules.
  683. output := &HiddenAPIOutput{
  684. HiddenAPIFlagOutput: flagOutput,
  685. EncodedBootDexFilesByModule: encodedBootDexFilesByModule,
  686. }
  687. // Get the ApiLevel associated with SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE, defaulting to current
  688. // if not set.
  689. config := ctx.Config()
  690. targetApiLevel := android.ApiLevelOrPanic(ctx,
  691. config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", "current"))
  692. // Filter the contents list to remove any modules that do not support the target build release.
  693. // The current build release supports all the modules.
  694. contentsForSdkSnapshot := []android.Module{}
  695. for _, module := range contents {
  696. // If the module has a min_sdk_version that is higher than the target build release then it will
  697. // not work on the target build release and so must not be included in the sdk snapshot.
  698. minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, module)
  699. if minApiLevel.GreaterThan(targetApiLevel) {
  700. continue
  701. }
  702. contentsForSdkSnapshot = append(contentsForSdkSnapshot, module)
  703. }
  704. var flagFilesByCategory FlagFilesByCategory
  705. if len(contentsForSdkSnapshot) != len(contents) {
  706. // The sdk snapshot has different contents to the runtime fragment so it is not possible to
  707. // reuse the hidden API information generated for the fragment. So, recompute that information
  708. // for the sdk snapshot.
  709. filteredInput := b.createHiddenAPIFlagInput(ctx, contentsForSdkSnapshot, fragments)
  710. // Gather information about the boot dex files for the boot libraries provided by this fragment.
  711. filteredBootDexInfoByModule := extractBootDexInfoFromModules(ctx, contentsForSdkSnapshot)
  712. flagOutput = b.generateHiddenApiFlagRules(ctx, contentsForSdkSnapshot, filteredInput, filteredBootDexInfoByModule, "-for-sdk-snapshot")
  713. flagFilesByCategory = filteredInput.FlagFilesByCategory
  714. } else {
  715. // The sdk snapshot has the same contents as the runtime fragment so reuse that information.
  716. flagFilesByCategory = input.FlagFilesByCategory
  717. }
  718. // Make the information available for the sdk snapshot.
  719. ctx.SetProvider(HiddenAPIInfoForSdkProvider, HiddenAPIInfoForSdk{
  720. FlagFilesByCategory: flagFilesByCategory,
  721. HiddenAPIFlagOutput: flagOutput,
  722. })
  723. return output
  724. }
  725. // produceBootImageFiles builds the boot image files from the source if it is required.
  726. func (b *BootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
  727. // Only generate the boot image if the configuration does not skip it.
  728. return b.generateBootImageBuildActions(ctx, imageConfig)
  729. }
  730. // generateBootImageBuildActions generates ninja rules to create the boot image if required for this
  731. // module.
  732. //
  733. // If it could not create the files then it will return nil. Otherwise, it will return a map from
  734. // android.ArchType to the predefined paths of the boot image files.
  735. func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
  736. global := dexpreopt.GetGlobalConfig(ctx)
  737. if !shouldBuildBootImages(ctx.Config(), global) {
  738. return bootImageOutputs{}
  739. }
  740. // Bootclasspath fragment modules that are for the platform do not produce a boot image.
  741. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  742. if apexInfo.IsForPlatform() {
  743. return bootImageOutputs{}
  744. }
  745. // Build a profile for the image config and then use that to build the boot image.
  746. profile := bootImageProfileRule(ctx, imageConfig)
  747. // If dexpreopt of boot image jars should be skipped, generate only a profile.
  748. if SkipDexpreoptBootJars(ctx) {
  749. return bootImageOutputs{
  750. profile: profile,
  751. }
  752. }
  753. // Build boot image files for the host variants.
  754. buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
  755. // Build boot image files for the android variants.
  756. bootImageFiles := buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
  757. // Return the boot image files for the android variants for inclusion in an APEX and to be zipped
  758. // up for the dist.
  759. return bootImageFiles
  760. }
  761. func (b *BootclasspathFragmentModule) AndroidMkEntries() []android.AndroidMkEntries {
  762. // Use the generated classpath proto as the output.
  763. outputFile := b.outputFilepath
  764. // Create a fake entry that will cause this to be added to the module-info.json file.
  765. entriesList := []android.AndroidMkEntries{{
  766. Class: "FAKE",
  767. OutputFile: android.OptionalPathForPath(outputFile),
  768. Include: "$(BUILD_PHONY_PACKAGE)",
  769. ExtraFooters: []android.AndroidMkExtraFootersFunc{
  770. func(w io.Writer, name, prefix, moduleDir string) {
  771. // Allow the bootclasspath_fragment to be built by simply passing its name on the command
  772. // line.
  773. fmt.Fprintln(w, ".PHONY:", b.Name())
  774. fmt.Fprintln(w, b.Name()+":", outputFile.String())
  775. },
  776. },
  777. }}
  778. return entriesList
  779. }
  780. func (b *BootclasspathFragmentModule) getImageName() *string {
  781. return b.properties.Image_name
  782. }
  783. func (b *BootclasspathFragmentModule) getProfilePath() android.Path {
  784. return b.profilePath
  785. }
  786. // Collect information for opening IDE project files in java/jdeps.go.
  787. func (b *BootclasspathFragmentModule) IDEInfo(dpInfo *android.IdeInfo) {
  788. dpInfo.Deps = append(dpInfo.Deps, b.properties.Contents...)
  789. dpInfo.Paths = append(dpInfo.Paths, b.modulePaths...)
  790. }
  791. type bootclasspathFragmentMemberType struct {
  792. android.SdkMemberTypeBase
  793. }
  794. func (b *bootclasspathFragmentMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
  795. ctx.AddVariationDependencies(nil, dependencyTag, names...)
  796. }
  797. func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
  798. _, ok := module.(*BootclasspathFragmentModule)
  799. return ok
  800. }
  801. func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
  802. if b.PropertyName == "boot_images" {
  803. return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
  804. } else {
  805. return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
  806. }
  807. }
  808. func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
  809. return &bootclasspathFragmentSdkMemberProperties{}
  810. }
  811. type bootclasspathFragmentSdkMemberProperties struct {
  812. android.SdkMemberPropertiesBase
  813. // The image name
  814. Image_name *string
  815. // Contents of the bootclasspath fragment
  816. Contents []string
  817. // Stub_libs properties.
  818. Stub_libs []string
  819. Core_platform_stub_libs []string
  820. // Fragment properties
  821. Fragments []ApexVariantReference
  822. // Flag files by *hiddenAPIFlagFileCategory
  823. Flag_files_by_category FlagFilesByCategory
  824. // The path to the generated annotation-flags.csv file.
  825. Annotation_flags_path android.OptionalPath
  826. // The path to the generated metadata.csv file.
  827. Metadata_path android.OptionalPath
  828. // The path to the generated index.csv file.
  829. Index_path android.OptionalPath
  830. // The path to the generated stub-flags.csv file.
  831. Stub_flags_path android.OptionalPath `supported_build_releases:"S"`
  832. // The path to the generated all-flags.csv file.
  833. All_flags_path android.OptionalPath `supported_build_releases:"S"`
  834. // The path to the generated signature-patterns.csv file.
  835. Signature_patterns_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
  836. // The path to the generated filtered-stub-flags.csv file.
  837. Filtered_stub_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
  838. // The path to the generated filtered-flags.csv file.
  839. Filtered_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
  840. }
  841. func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
  842. module := variant.(*BootclasspathFragmentModule)
  843. b.Image_name = module.properties.Image_name
  844. b.Contents = module.properties.Contents
  845. // Get the hidden API information from the module.
  846. mctx := ctx.SdkModuleContext()
  847. hiddenAPIInfo := mctx.OtherModuleProvider(module, HiddenAPIInfoForSdkProvider).(HiddenAPIInfoForSdk)
  848. b.Flag_files_by_category = hiddenAPIInfo.FlagFilesByCategory
  849. // Copy all the generated file paths.
  850. b.Annotation_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AnnotationFlagsPath)
  851. b.Metadata_path = android.OptionalPathForPath(hiddenAPIInfo.MetadataPath)
  852. b.Index_path = android.OptionalPathForPath(hiddenAPIInfo.IndexPath)
  853. b.Stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.StubFlagsPath)
  854. b.All_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AllFlagsPath)
  855. b.Signature_patterns_path = android.OptionalPathForPath(hiddenAPIInfo.SignaturePatternsPath)
  856. b.Filtered_stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredStubFlagsPath)
  857. b.Filtered_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredFlagsPath)
  858. // Copy stub_libs properties.
  859. b.Stub_libs = module.properties.Api.Stub_libs
  860. b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
  861. // Copy fragment properties.
  862. b.Fragments = module.properties.Fragments
  863. }
  864. func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
  865. if b.Image_name != nil {
  866. propertySet.AddProperty("image_name", *b.Image_name)
  867. }
  868. builder := ctx.SnapshotBuilder()
  869. requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
  870. if len(b.Contents) > 0 {
  871. propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
  872. }
  873. if len(b.Stub_libs) > 0 {
  874. apiPropertySet := propertySet.AddPropertySet("api")
  875. apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
  876. }
  877. if len(b.Core_platform_stub_libs) > 0 {
  878. corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
  879. corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
  880. }
  881. if len(b.Fragments) > 0 {
  882. propertySet.AddProperty("fragments", b.Fragments)
  883. }
  884. hiddenAPISet := propertySet.AddPropertySet("hidden_api")
  885. hiddenAPIDir := "hiddenapi"
  886. // Copy manually curated flag files specified on the bootclasspath_fragment.
  887. if b.Flag_files_by_category != nil {
  888. for _, category := range HiddenAPIFlagFileCategories {
  889. paths := b.Flag_files_by_category[category]
  890. if len(paths) > 0 {
  891. dests := []string{}
  892. for _, p := range paths {
  893. dest := filepath.Join(hiddenAPIDir, p.Base())
  894. builder.CopyToSnapshot(p, dest)
  895. dests = append(dests, dest)
  896. }
  897. hiddenAPISet.AddProperty(category.PropertyName, dests)
  898. }
  899. }
  900. }
  901. copyOptionalPath := func(path android.OptionalPath, property string) {
  902. if path.Valid() {
  903. p := path.Path()
  904. dest := filepath.Join(hiddenAPIDir, p.Base())
  905. builder.CopyToSnapshot(p, dest)
  906. hiddenAPISet.AddProperty(property, dest)
  907. }
  908. }
  909. // Copy all the generated files, if available.
  910. copyOptionalPath(b.Annotation_flags_path, "annotation_flags")
  911. copyOptionalPath(b.Metadata_path, "metadata")
  912. copyOptionalPath(b.Index_path, "index")
  913. copyOptionalPath(b.Stub_flags_path, "stub_flags")
  914. copyOptionalPath(b.All_flags_path, "all_flags")
  915. copyOptionalPath(b.Signature_patterns_path, "signature_patterns")
  916. copyOptionalPath(b.Filtered_stub_flags_path, "filtered_stub_flags")
  917. copyOptionalPath(b.Filtered_flags_path, "filtered_flags")
  918. }
  919. var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
  920. // prebuiltBootclasspathFragmentProperties contains additional prebuilt_bootclasspath_fragment
  921. // specific properties.
  922. type prebuiltBootclasspathFragmentProperties struct {
  923. Hidden_api struct {
  924. // The path to the annotation-flags.csv file created by the bootclasspath_fragment.
  925. Annotation_flags *string `android:"path"`
  926. // The path to the metadata.csv file created by the bootclasspath_fragment.
  927. Metadata *string `android:"path"`
  928. // The path to the index.csv file created by the bootclasspath_fragment.
  929. Index *string `android:"path"`
  930. // The path to the signature-patterns.csv file created by the bootclasspath_fragment.
  931. Signature_patterns *string `android:"path"`
  932. // The path to the stub-flags.csv file created by the bootclasspath_fragment.
  933. Stub_flags *string `android:"path"`
  934. // The path to the all-flags.csv file created by the bootclasspath_fragment.
  935. All_flags *string `android:"path"`
  936. // The path to the filtered-stub-flags.csv file created by the bootclasspath_fragment.
  937. Filtered_stub_flags *string `android:"path"`
  938. // The path to the filtered-flags.csv file created by the bootclasspath_fragment.
  939. Filtered_flags *string `android:"path"`
  940. }
  941. }
  942. // A prebuilt version of the bootclasspath_fragment module.
  943. //
  944. // At the moment this is basically just a bootclasspath_fragment module that can be used as a
  945. // prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
  946. // type from the various singletons then this will diverge.
  947. type PrebuiltBootclasspathFragmentModule struct {
  948. BootclasspathFragmentModule
  949. prebuilt android.Prebuilt
  950. // Additional prebuilt specific properties.
  951. prebuiltProperties prebuiltBootclasspathFragmentProperties
  952. }
  953. func (module *PrebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
  954. return &module.prebuilt
  955. }
  956. func (module *PrebuiltBootclasspathFragmentModule) Name() string {
  957. return module.prebuilt.Name(module.ModuleBase.Name())
  958. }
  959. // produceHiddenAPIOutput returns a path to the prebuilt all-flags.csv or nil if none is specified.
  960. func (module *PrebuiltBootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
  961. pathForOptionalSrc := func(src *string, defaultPath android.Path) android.Path {
  962. if src == nil {
  963. return defaultPath
  964. }
  965. return android.PathForModuleSrc(ctx, *src)
  966. }
  967. pathForSrc := func(property string, src *string) android.Path {
  968. if src == nil {
  969. ctx.PropertyErrorf(property, "is required but was not specified")
  970. return android.PathForModuleSrc(ctx, "missing", property)
  971. }
  972. return android.PathForModuleSrc(ctx, *src)
  973. }
  974. // Retrieve the dex files directly from the content modules. They in turn should retrieve the
  975. // encoded dex jars from the prebuilt .apex files.
  976. encodedBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, contents)
  977. output := HiddenAPIOutput{
  978. HiddenAPIFlagOutput: HiddenAPIFlagOutput{
  979. AnnotationFlagsPath: pathForSrc("hidden_api.annotation_flags", module.prebuiltProperties.Hidden_api.Annotation_flags),
  980. MetadataPath: pathForSrc("hidden_api.metadata", module.prebuiltProperties.Hidden_api.Metadata),
  981. IndexPath: pathForSrc("hidden_api.index", module.prebuiltProperties.Hidden_api.Index),
  982. SignaturePatternsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Signature_patterns, nil),
  983. // TODO: Temporarily handle stub_flags/all_flags properties until prebuilts have been updated.
  984. StubFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags, nil),
  985. AllFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags, nil),
  986. },
  987. EncodedBootDexFilesByModule: encodedBootDexJarsByModule,
  988. }
  989. // TODO: Temporarily fallback to stub_flags/all_flags properties until prebuilts have been updated.
  990. output.FilteredStubFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_stub_flags, output.StubFlagsPath)
  991. output.FilteredFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_flags, output.AllFlagsPath)
  992. return &output
  993. }
  994. // produceBootImageFiles extracts the boot image files from the APEX if available.
  995. func (module *PrebuiltBootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
  996. if !shouldCopyBootFilesToPredefinedLocations(ctx, imageConfig) {
  997. return bootImageOutputs{}
  998. }
  999. di := android.FindDeapexerProviderForModule(ctx)
  1000. if di == nil {
  1001. return bootImageOutputs{} // An error has been reported by FindDeapexerProviderForModule.
  1002. }
  1003. profile := (android.WritablePath)(nil)
  1004. if imageConfig.profileInstallPathInApex != "" {
  1005. profile = di.PrebuiltExportPath(imageConfig.profileInstallPathInApex)
  1006. }
  1007. // Build the boot image files for the host variants. These are always built from the dex files
  1008. // provided by the contents of this module as prebuilt versions of the host boot image files are
  1009. // not available, i.e. there is no host specific prebuilt apex containing them. This has to be
  1010. // built without a profile as the prebuilt modules do not provide a profile.
  1011. buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
  1012. if profile == nil && imageConfig.isProfileGuided() {
  1013. ctx.ModuleErrorf("Unable to produce boot image files: profiles not found in the prebuilt apex")
  1014. return bootImageOutputs{}
  1015. }
  1016. // Build boot image files for the android variants from the dex files provided by the contents
  1017. // of this module.
  1018. return buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
  1019. }
  1020. func (b *PrebuiltBootclasspathFragmentModule) getImageName() *string {
  1021. return b.properties.Image_name
  1022. }
  1023. func (b *PrebuiltBootclasspathFragmentModule) getProfilePath() android.Path {
  1024. return b.profilePath
  1025. }
  1026. var _ commonBootclasspathFragment = (*PrebuiltBootclasspathFragmentModule)(nil)
  1027. // RequiredFilesFromPrebuiltApex returns the list of all files the prebuilt_bootclasspath_fragment
  1028. // requires from a prebuilt .apex file.
  1029. //
  1030. // If there is no image config associated with this fragment then it returns nil. Otherwise, it
  1031. // returns the files that are listed in the image config.
  1032. func (module *PrebuiltBootclasspathFragmentModule) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
  1033. imageConfig := module.getImageConfig(ctx)
  1034. if imageConfig != nil {
  1035. files := []string{}
  1036. if imageConfig.profileInstallPathInApex != "" {
  1037. // Add the boot image profile.
  1038. files = append(files, imageConfig.profileInstallPathInApex)
  1039. }
  1040. return files
  1041. }
  1042. return nil
  1043. }
  1044. var _ android.RequiredFilesFromPrebuiltApex = (*PrebuiltBootclasspathFragmentModule)(nil)
  1045. func prebuiltBootclasspathFragmentFactory() android.Module {
  1046. m := &PrebuiltBootclasspathFragmentModule{}
  1047. m.AddProperties(&m.properties, &m.prebuiltProperties)
  1048. // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
  1049. // array.
  1050. android.InitPrebuiltModule(m, &[]string{"placeholder"})
  1051. android.InitApexModule(m)
  1052. android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
  1053. // Initialize the contents property from the image_name.
  1054. android.AddLoadHook(m, func(ctx android.LoadHookContext) {
  1055. bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
  1056. })
  1057. return m
  1058. }