platform_compat_config.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. "path/filepath"
  17. "android/soong/android"
  18. "github.com/google/blueprint"
  19. "fmt"
  20. )
  21. func init() {
  22. registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext)
  23. android.RegisterSdkMemberType(&compatConfigMemberType{
  24. SdkMemberTypeBase: android.SdkMemberTypeBase{
  25. PropertyName: "compat_configs",
  26. SupportsSdk: true,
  27. },
  28. })
  29. }
  30. func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
  31. ctx.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
  32. ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
  33. ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
  34. ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
  35. }
  36. var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
  37. func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
  38. return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
  39. }
  40. type platformCompatConfigProperties struct {
  41. Src *string `android:"path"`
  42. }
  43. type platformCompatConfig struct {
  44. android.ModuleBase
  45. android.SdkBase
  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.InitSdkAwareModule(module)
  103. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
  104. return module
  105. }
  106. type compatConfigMemberType struct {
  107. android.SdkMemberTypeBase
  108. }
  109. func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
  110. ctx.AddVariationDependencies(nil, dependencyTag, names...)
  111. }
  112. func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
  113. _, ok := module.(*platformCompatConfig)
  114. return ok
  115. }
  116. func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
  117. return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
  118. }
  119. func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
  120. return &compatConfigSdkMemberProperties{}
  121. }
  122. type compatConfigSdkMemberProperties struct {
  123. android.SdkMemberPropertiesBase
  124. Metadata android.Path
  125. }
  126. func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
  127. module := variant.(*platformCompatConfig)
  128. b.Metadata = module.metadataFile
  129. }
  130. func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
  131. builder := ctx.SnapshotBuilder()
  132. if b.Metadata != nil {
  133. snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
  134. builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
  135. propertySet.AddProperty("metadata", snapshotRelativePath)
  136. }
  137. }
  138. var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
  139. // A prebuilt version of the platform compat config module.
  140. type prebuiltCompatConfigModule struct {
  141. android.ModuleBase
  142. android.SdkBase
  143. prebuilt android.Prebuilt
  144. properties prebuiltCompatConfigProperties
  145. metadataFile android.Path
  146. }
  147. type prebuiltCompatConfigProperties struct {
  148. Metadata *string `android:"path"`
  149. }
  150. func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
  151. return &module.prebuilt
  152. }
  153. func (module *prebuiltCompatConfigModule) Name() string {
  154. return module.prebuilt.Name(module.ModuleBase.Name())
  155. }
  156. func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
  157. return module.metadataFile
  158. }
  159. var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
  160. func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  161. module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
  162. }
  163. // A prebuilt version of platform_compat_config that provides the metadata.
  164. func prebuiltCompatConfigFactory() android.Module {
  165. m := &prebuiltCompatConfigModule{}
  166. m.AddProperties(&m.properties)
  167. android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
  168. android.InitSdkAwareModule(m)
  169. android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
  170. return m
  171. }
  172. // compat singleton rules
  173. type platformCompatConfigSingleton struct {
  174. metadata android.Path
  175. }
  176. // isModulePreferredByCompatConfig checks to see whether the module is preferred for use by
  177. // platform compat config.
  178. func isModulePreferredByCompatConfig(module android.Module) bool {
  179. // A versioned prebuilt_platform_compat_config, i.e. foo-platform-compat-config@current should be
  180. // ignored.
  181. if android.IsModuleInVersionedSdk(module) {
  182. return false
  183. }
  184. return android.IsModulePreferred(module)
  185. }
  186. func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  187. var compatConfigMetadata android.Paths
  188. ctx.VisitAllModules(func(module android.Module) {
  189. if !module.Enabled() {
  190. return
  191. }
  192. if c, ok := module.(platformCompatConfigMetadataProvider); ok {
  193. if !isModulePreferredByCompatConfig(module) {
  194. return
  195. }
  196. metadata := c.compatConfigMetadata()
  197. compatConfigMetadata = append(compatConfigMetadata, metadata)
  198. }
  199. })
  200. if compatConfigMetadata == nil {
  201. // nothing to do.
  202. return
  203. }
  204. rule := android.NewRuleBuilder(pctx, ctx)
  205. outputPath := platformCompatConfigPath(ctx)
  206. rule.Command().
  207. BuiltTool("process-compat-config").
  208. FlagForEachInput("--xml ", compatConfigMetadata).
  209. FlagWithOutput("--merged-config ", outputPath)
  210. rule.Build("merged-compat-config", "Merge compat config")
  211. p.metadata = outputPath
  212. }
  213. func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
  214. if p.metadata != nil {
  215. ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
  216. }
  217. }
  218. func platformCompatConfigSingletonFactory() android.Singleton {
  219. return &platformCompatConfigSingleton{}
  220. }
  221. //============== merged_compat_config =================
  222. type globalCompatConfigProperties struct {
  223. // name of the file into which the metadata will be copied.
  224. Filename *string
  225. }
  226. type globalCompatConfig struct {
  227. android.ModuleBase
  228. properties globalCompatConfigProperties
  229. outputFilePath android.OutputPath
  230. }
  231. func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  232. filename := String(c.properties.Filename)
  233. inputPath := platformCompatConfigPath(ctx)
  234. c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
  235. // This ensures that outputFilePath has the correct name for others to
  236. // use, as the source file may have a different name.
  237. ctx.Build(pctx, android.BuildParams{
  238. Rule: android.Cp,
  239. Output: c.outputFilePath,
  240. Input: inputPath,
  241. })
  242. }
  243. func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
  244. switch tag {
  245. case "":
  246. return android.Paths{h.outputFilePath}, nil
  247. default:
  248. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  249. }
  250. }
  251. // global_compat_config provides access to the merged compat config xml file generated by the build.
  252. func globalCompatConfigFactory() android.Module {
  253. module := &globalCompatConfig{}
  254. module.AddProperties(&module.properties)
  255. android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
  256. return module
  257. }