platform_compat_config.go 9.4 KB

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