bootclasspath.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2021 Google Inc. All rights reserved.
  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. "android/soong/android"
  17. "github.com/google/blueprint"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. // Contains code that is common to both platform_bootclasspath and bootclasspath_fragment.
  21. func init() {
  22. registerBootclasspathBuildComponents(android.InitRegistrationContext)
  23. }
  24. func registerBootclasspathBuildComponents(ctx android.RegistrationContext) {
  25. ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
  26. ctx.BottomUp("bootclasspath_deps", bootclasspathDepsMutator).Parallel()
  27. })
  28. }
  29. // BootclasspathDepsMutator is the interface that a module must implement if it wants to add
  30. // dependencies onto APEX specific variants of bootclasspath fragments or bootclasspath contents.
  31. type BootclasspathDepsMutator interface {
  32. // BootclasspathDepsMutator implementations should add dependencies using
  33. // addDependencyOntoApexModulePair and addDependencyOntoApexVariants.
  34. BootclasspathDepsMutator(ctx android.BottomUpMutatorContext)
  35. }
  36. // bootclasspathDepsMutator is called during the final deps phase after all APEX variants have
  37. // been created so can add dependencies onto specific APEX variants of modules.
  38. func bootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
  39. m := ctx.Module()
  40. if p, ok := m.(BootclasspathDepsMutator); ok {
  41. p.BootclasspathDepsMutator(ctx)
  42. }
  43. }
  44. // addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
  45. // the module as specified in the ApexVariantReference list.
  46. func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
  47. for i, ref := range refs {
  48. apex := proptools.StringDefault(ref.Apex, "platform")
  49. if ref.Module == nil {
  50. ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
  51. continue
  52. }
  53. name := proptools.String(ref.Module)
  54. addDependencyOntoApexModulePair(ctx, apex, name, tag)
  55. }
  56. }
  57. // addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
  58. // specified module.
  59. //
  60. // If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the
  61. // module. This adds dependencies onto the prebuilt and source modules with the specified name,
  62. // depending on which ones are available. Visiting must use isActiveModule to select the preferred
  63. // module when both source and prebuilt modules are available.
  64. //
  65. // Use gatherApexModulePairDepsWithTag to retrieve the dependencies.
  66. func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
  67. var variations []blueprint.Variation
  68. if apex != "platform" && apex != "system_ext" {
  69. // Pick the correct apex variant.
  70. variations = []blueprint.Variation{
  71. {Mutator: "apex", Variation: apex},
  72. }
  73. }
  74. target := ctx.Module().Target()
  75. variations = append(variations, target.Variations()...)
  76. addedDep := false
  77. if ctx.OtherModuleDependencyVariantExists(variations, name) {
  78. ctx.AddFarVariationDependencies(variations, tag, name)
  79. addedDep = true
  80. }
  81. // Add a dependency on the prebuilt module if it exists.
  82. prebuiltName := android.PrebuiltNameFromSource(name)
  83. if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
  84. ctx.AddVariationDependencies(variations, tag, prebuiltName)
  85. addedDep = true
  86. }
  87. // If no appropriate variant existing for this, so no dependency could be added, then it is an
  88. // error, unless missing dependencies are allowed. The simplest way to handle that is to add a
  89. // dependency that will not be satisfied and the default behavior will handle it.
  90. if !addedDep {
  91. // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
  92. // not exist. The resulting error message will contain useful information about the available
  93. // variants.
  94. reportMissingVariationDependency(ctx, variations, name)
  95. // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
  96. // so that information about its available variants will be reported too.
  97. if ctx.OtherModuleExists(prebuiltName) {
  98. reportMissingVariationDependency(ctx, variations, prebuiltName)
  99. }
  100. }
  101. }
  102. // reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
  103. // to generate an appropriate error message with information about the available variations.
  104. func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
  105. ctx.AddFarVariationDependencies(variations, nil, name)
  106. }
  107. // gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
  108. // added by addDependencyOntoApexModulePair.
  109. func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tag blueprint.DependencyTag) []android.Module {
  110. var modules []android.Module
  111. ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
  112. t := ctx.OtherModuleDependencyTag(module)
  113. if t == tag {
  114. modules = append(modules, module)
  115. }
  116. })
  117. return modules
  118. }
  119. // ApexVariantReference specifies a particular apex variant of a module.
  120. type ApexVariantReference struct {
  121. android.BpPrintableBase
  122. // The name of the module apex variant, i.e. the apex containing the module variant.
  123. //
  124. // If this is not specified then it defaults to "platform" which will cause a dependency to be
  125. // added to the module's platform variant.
  126. //
  127. // A value of system_ext should be used for any module that will be part of the system_ext
  128. // partition.
  129. Apex *string
  130. // The name of the module.
  131. Module *string
  132. }
  133. // BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
  134. type BootclasspathFragmentsDepsProperties struct {
  135. // The names of the bootclasspath_fragment modules that form part of this module.
  136. Fragments []ApexVariantReference
  137. }
  138. // addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
  139. // structure.
  140. func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
  141. addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
  142. }
  143. // bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
  144. // prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
  145. // modules.
  146. type bootclasspathDependencyTag struct {
  147. blueprint.BaseDependencyTag
  148. name string
  149. }
  150. func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
  151. }
  152. // Dependencies that use the bootclasspathDependencyTag instances are only added after all the
  153. // visibility checking has been done so this has no functional effect. However, it does make it
  154. // clear that visibility is not being enforced on these tags.
  155. var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
  156. // The tag used for dependencies onto bootclasspath_fragments.
  157. var bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}
  158. // BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
  159. // bootclasspath that are nested within the main BootclasspathAPIProperties.
  160. type BootclasspathNestedAPIProperties struct {
  161. // java_library or preferably, java_sdk_library modules providing stub classes that define the
  162. // APIs provided by this bootclasspath_fragment.
  163. Stub_libs []string
  164. }
  165. // BootclasspathAPIProperties defines properties for defining the API provided by parts of the
  166. // bootclasspath.
  167. type BootclasspathAPIProperties struct {
  168. // Api properties provide information about the APIs provided by the bootclasspath_fragment.
  169. // Properties in this section apply to public, system and test api scopes. They DO NOT apply to
  170. // core_platform as that is a special, ART specific scope, that does not follow the pattern and so
  171. // has its own section. It is in the process of being deprecated and replaced by the system scope
  172. // but this will remain for the foreseeable future to maintain backwards compatibility.
  173. //
  174. // Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
  175. // specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
  176. // methods being inaccessible to other parts of Android, including but not limited to
  177. // applications.
  178. Api BootclasspathNestedAPIProperties
  179. // Properties related to the core platform API surface.
  180. //
  181. // This must only be used by the following modules:
  182. // * ART
  183. // * Conscrypt
  184. // * I18N
  185. //
  186. // The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
  187. // and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
  188. // those methods being inaccessible to the other modules in the list.
  189. Core_platform_api BootclasspathNestedAPIProperties
  190. }
  191. // apiScopeToStubLibs calculates the stub library modules for each relevant *HiddenAPIScope from the
  192. // Stub_libs properties.
  193. func (p BootclasspathAPIProperties) apiScopeToStubLibs() map[*HiddenAPIScope][]string {
  194. m := map[*HiddenAPIScope][]string{}
  195. for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
  196. m[apiScope] = p.Api.Stub_libs
  197. }
  198. m[CorePlatformHiddenAPIScope] = p.Core_platform_api.Stub_libs
  199. return m
  200. }