platform_compat_config.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2019 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. "fmt"
  17. "path/filepath"
  18. "android/soong/android"
  19. "android/soong/bazel"
  20. "github.com/google/blueprint"
  21. "github.com/google/blueprint/proptools"
  22. )
  23. func init() {
  24. registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext)
  25. android.RegisterSdkMemberType(CompatConfigSdkMemberType)
  26. }
  27. var CompatConfigSdkMemberType = &compatConfigMemberType{
  28. SdkMemberTypeBase: android.SdkMemberTypeBase{
  29. PropertyName: "compat_configs",
  30. SupportsSdk: true,
  31. },
  32. }
  33. func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
  34. ctx.RegisterParallelSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
  35. ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
  36. ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
  37. ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
  38. }
  39. var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
  40. func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
  41. return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
  42. }
  43. type platformCompatConfigProperties struct {
  44. Src *string `android:"path"`
  45. }
  46. type platformCompatConfig struct {
  47. android.ModuleBase
  48. android.BazelModuleBase
  49. properties platformCompatConfigProperties
  50. installDirPath android.InstallPath
  51. configFile android.OutputPath
  52. metadataFile android.OutputPath
  53. }
  54. func (p *platformCompatConfig) compatConfigMetadata() android.Path {
  55. return p.metadataFile
  56. }
  57. func (p *platformCompatConfig) CompatConfig() android.OutputPath {
  58. return p.configFile
  59. }
  60. func (p *platformCompatConfig) SubDir() string {
  61. return "compatconfig"
  62. }
  63. type platformCompatConfigMetadataProvider interface {
  64. compatConfigMetadata() android.Path
  65. }
  66. type PlatformCompatConfigIntf interface {
  67. android.Module
  68. CompatConfig() android.OutputPath
  69. // Sub dir under etc dir.
  70. SubDir() string
  71. }
  72. var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
  73. var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
  74. func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  75. rule := android.NewRuleBuilder(pctx, ctx)
  76. configFileName := p.Name() + ".xml"
  77. metadataFileName := p.Name() + "_meta.xml"
  78. p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
  79. p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
  80. path := android.PathForModuleSrc(ctx, String(p.properties.Src))
  81. rule.Command().
  82. BuiltTool("process-compat-config").
  83. FlagWithInput("--jar ", path).
  84. FlagWithOutput("--device-config ", p.configFile).
  85. FlagWithOutput("--merged-config ", p.metadataFile)
  86. p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
  87. rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
  88. }
  89. func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
  90. return []android.AndroidMkEntries{android.AndroidMkEntries{
  91. Class: "ETC",
  92. OutputFile: android.OptionalPathForPath(p.configFile),
  93. Include: "$(BUILD_PREBUILT)",
  94. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  95. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  96. entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
  97. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
  98. },
  99. },
  100. }}
  101. }
  102. type bazelPlatformCompatConfigAttributes struct {
  103. Src bazel.LabelAttribute
  104. }
  105. func (p *platformCompatConfig) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  106. props := bazel.BazelTargetModuleProperties{
  107. Rule_class: "platform_compat_config",
  108. Bzl_load_location: "//build/bazel/rules/java:platform_compat_config.bzl",
  109. }
  110. attr := &bazelPlatformCompatConfigAttributes{
  111. Src: *bazel.MakeLabelAttribute(
  112. android.BazelLabelForModuleSrcSingle(ctx, proptools.String(p.properties.Src)).Label),
  113. }
  114. ctx.CreateBazelTargetModule(props, android.CommonAttributes{
  115. Name: p.Name(),
  116. }, attr)
  117. }
  118. func PlatformCompatConfigFactory() android.Module {
  119. module := &platformCompatConfig{}
  120. module.AddProperties(&module.properties)
  121. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
  122. android.InitBazelModule(module)
  123. return module
  124. }
  125. type compatConfigMemberType struct {
  126. android.SdkMemberTypeBase
  127. }
  128. func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
  129. ctx.AddVariationDependencies(nil, dependencyTag, names...)
  130. }
  131. func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
  132. _, ok := module.(*platformCompatConfig)
  133. return ok
  134. }
  135. func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
  136. return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
  137. }
  138. func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
  139. return &compatConfigSdkMemberProperties{}
  140. }
  141. type compatConfigSdkMemberProperties struct {
  142. android.SdkMemberPropertiesBase
  143. Metadata android.Path
  144. }
  145. func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
  146. module := variant.(*platformCompatConfig)
  147. b.Metadata = module.metadataFile
  148. }
  149. func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
  150. builder := ctx.SnapshotBuilder()
  151. if b.Metadata != nil {
  152. snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
  153. builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
  154. propertySet.AddProperty("metadata", snapshotRelativePath)
  155. }
  156. }
  157. var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
  158. // A prebuilt version of the platform compat config module.
  159. type prebuiltCompatConfigModule struct {
  160. android.ModuleBase
  161. prebuilt android.Prebuilt
  162. properties prebuiltCompatConfigProperties
  163. metadataFile android.Path
  164. }
  165. type prebuiltCompatConfigProperties struct {
  166. Metadata *string `android:"path"`
  167. }
  168. func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
  169. return &module.prebuilt
  170. }
  171. func (module *prebuiltCompatConfigModule) Name() string {
  172. return module.prebuilt.Name(module.ModuleBase.Name())
  173. }
  174. func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
  175. return module.metadataFile
  176. }
  177. var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
  178. func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  179. module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
  180. }
  181. // A prebuilt version of platform_compat_config that provides the metadata.
  182. func prebuiltCompatConfigFactory() android.Module {
  183. m := &prebuiltCompatConfigModule{}
  184. m.AddProperties(&m.properties)
  185. android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
  186. android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
  187. return m
  188. }
  189. // compat singleton rules
  190. type platformCompatConfigSingleton struct {
  191. metadata android.Path
  192. }
  193. func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  194. var compatConfigMetadata android.Paths
  195. ctx.VisitAllModules(func(module android.Module) {
  196. if !module.Enabled() {
  197. return
  198. }
  199. if c, ok := module.(platformCompatConfigMetadataProvider); ok {
  200. if !android.IsModulePreferred(module) {
  201. return
  202. }
  203. metadata := c.compatConfigMetadata()
  204. compatConfigMetadata = append(compatConfigMetadata, metadata)
  205. }
  206. })
  207. if compatConfigMetadata == nil {
  208. // nothing to do.
  209. return
  210. }
  211. rule := android.NewRuleBuilder(pctx, ctx)
  212. outputPath := platformCompatConfigPath(ctx)
  213. rule.Command().
  214. BuiltTool("process-compat-config").
  215. FlagForEachInput("--xml ", compatConfigMetadata).
  216. FlagWithOutput("--merged-config ", outputPath)
  217. rule.Build("merged-compat-config", "Merge compat config")
  218. p.metadata = outputPath
  219. }
  220. func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
  221. if p.metadata != nil {
  222. ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
  223. }
  224. }
  225. func platformCompatConfigSingletonFactory() android.Singleton {
  226. return &platformCompatConfigSingleton{}
  227. }
  228. // ============== merged_compat_config =================
  229. type globalCompatConfigProperties struct {
  230. // name of the file into which the metadata will be copied.
  231. Filename *string
  232. }
  233. type globalCompatConfig struct {
  234. android.ModuleBase
  235. properties globalCompatConfigProperties
  236. outputFilePath android.OutputPath
  237. }
  238. func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  239. filename := String(c.properties.Filename)
  240. inputPath := platformCompatConfigPath(ctx)
  241. c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
  242. // This ensures that outputFilePath has the correct name for others to
  243. // use, as the source file may have a different name.
  244. ctx.Build(pctx, android.BuildParams{
  245. Rule: android.Cp,
  246. Output: c.outputFilePath,
  247. Input: inputPath,
  248. })
  249. }
  250. func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
  251. switch tag {
  252. case "":
  253. return android.Paths{h.outputFilePath}, nil
  254. default:
  255. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  256. }
  257. }
  258. // global_compat_config provides access to the merged compat config xml file generated by the build.
  259. func globalCompatConfigFactory() android.Module {
  260. module := &globalCompatConfig{}
  261. module.AddProperties(&module.properties)
  262. android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
  263. return module
  264. }