platform_compat_config.go 9.9 KB

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