aconfig_declarations.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2023 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 aconfig
  15. import (
  16. "android/soong/android"
  17. "fmt"
  18. "github.com/google/blueprint"
  19. "strings"
  20. )
  21. type DeclarationsModule struct {
  22. android.ModuleBase
  23. android.DefaultableModuleBase
  24. // Properties for "aconfig_declarations"
  25. properties struct {
  26. // aconfig files, relative to this Android.bp file
  27. Srcs []string `android:"path"`
  28. // Release config flag package
  29. Package string
  30. // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
  31. Values []string `blueprint:"mutated"`
  32. }
  33. intermediatePath android.WritablePath
  34. }
  35. func DeclarationsFactory() android.Module {
  36. module := &DeclarationsModule{}
  37. android.InitAndroidModule(module)
  38. android.InitDefaultableModule(module)
  39. module.AddProperties(&module.properties)
  40. // TODO: bp2build
  41. //android.InitBazelModule(module)
  42. return module
  43. }
  44. type implicitValuesTagType struct {
  45. blueprint.BaseDependencyTag
  46. }
  47. var implicitValuesTag = implicitValuesTagType{}
  48. func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
  49. // Validate Properties
  50. if len(module.properties.Srcs) == 0 {
  51. ctx.PropertyErrorf("srcs", "missing source files")
  52. return
  53. }
  54. if len(module.properties.Package) == 0 {
  55. ctx.PropertyErrorf("package", "missing package property")
  56. }
  57. // Add a dependency on the aconfig_value_sets defined in
  58. // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
  59. // match our package.
  60. valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
  61. ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
  62. }
  63. func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
  64. switch tag {
  65. case "":
  66. // The default output of this module is the intermediates format, which is
  67. // not installable and in a private format that no other rules can handle
  68. // correctly.
  69. return []android.Path{module.intermediatePath}, nil
  70. default:
  71. return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
  72. }
  73. }
  74. func joinAndPrefix(prefix string, values []string) string {
  75. var sb strings.Builder
  76. for _, v := range values {
  77. sb.WriteString(prefix)
  78. sb.WriteString(v)
  79. }
  80. return sb.String()
  81. }
  82. // Provider published by aconfig_value_set
  83. type declarationsProviderData struct {
  84. Package string
  85. IntermediatePath android.WritablePath
  86. }
  87. var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
  88. func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  89. // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
  90. ctx.VisitDirectDeps(func(dep android.Module) {
  91. if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
  92. // Other modules get injected as dependencies too, for example the license modules
  93. return
  94. }
  95. depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
  96. valuesFiles, ok := depData.AvailablePackages[module.properties.Package]
  97. if ok {
  98. for _, path := range valuesFiles {
  99. module.properties.Values = append(module.properties.Values, path.String())
  100. }
  101. }
  102. })
  103. // Intermediate format
  104. inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
  105. intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
  106. ctx.Build(pctx, android.BuildParams{
  107. Rule: aconfigRule,
  108. Inputs: inputFiles,
  109. Output: intermediatePath,
  110. Description: "aconfig_declarations",
  111. Args: map[string]string{
  112. "release_version": ctx.Config().ReleaseVersion(),
  113. "package": module.properties.Package,
  114. "values": joinAndPrefix(" --values ", module.properties.Values),
  115. },
  116. })
  117. ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
  118. Package: module.properties.Package,
  119. IntermediatePath: intermediatePath,
  120. })
  121. }