blob: d1d1578f6afd2180e790e97b4c016dfb657cc129 [file] [log] [blame]
Joe Onoratofee845a2023-05-09 08:14:14 -07001// 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
Joe Onorato981c9262023-06-21 15:16:23 -070015package aconfig
Joe Onoratofee845a2023-05-09 08:14:14 -070016
17import (
18 "android/soong/android"
19 "fmt"
Joe Onoratofee845a2023-05-09 08:14:14 -070020 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040021
22 "github.com/google/blueprint"
Joe Onoratofee845a2023-05-09 08:14:14 -070023)
24
Joe Onorato981c9262023-06-21 15:16:23 -070025type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070026 android.ModuleBase
27 android.DefaultableModuleBase
28
Joe Onorato981c9262023-06-21 15:16:23 -070029 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070030 properties struct {
31 // aconfig files, relative to this Android.bp file
32 Srcs []string `android:"path"`
33
Joe Onorato81b25ed2023-06-21 13:49:37 -070034 // Release config flag package
35 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070036
Joe Onorato981c9262023-06-21 15:16:23 -070037 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070038 Values []string `blueprint:"mutated"`
39 }
40
41 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070042}
43
Joe Onorato981c9262023-06-21 15:16:23 -070044func DeclarationsFactory() android.Module {
45 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070046
47 android.InitAndroidModule(module)
48 android.InitDefaultableModule(module)
49 module.AddProperties(&module.properties)
50 // TODO: bp2build
51 //android.InitBazelModule(module)
52
53 return module
54}
55
56type implicitValuesTagType struct {
57 blueprint.BaseDependencyTag
58}
59
60var implicitValuesTag = implicitValuesTagType{}
61
Joe Onorato981c9262023-06-21 15:16:23 -070062func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070063 // Validate Properties
64 if len(module.properties.Srcs) == 0 {
65 ctx.PropertyErrorf("srcs", "missing source files")
66 return
67 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070068 if len(module.properties.Package) == 0 {
69 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070070 }
71
Joe Onorato981c9262023-06-21 15:16:23 -070072 // Add a dependency on the aconfig_value_sets defined in
73 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070074 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070075 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Joe Onoratofee845a2023-05-09 08:14:14 -070076 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
77}
78
Joe Onorato981c9262023-06-21 15:16:23 -070079func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070080 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070081 case "":
82 // The default output of this module is the intermediates format, which is
83 // not installable and in a private format that no other rules can handle
84 // correctly.
85 return []android.Path{module.intermediatePath}, nil
86 default:
Joe Onorato981c9262023-06-21 15:16:23 -070087 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070088 }
89}
90
91func joinAndPrefix(prefix string, values []string) string {
92 var sb strings.Builder
93 for _, v := range values {
94 sb.WriteString(prefix)
95 sb.WriteString(v)
96 }
97 return sb.String()
98}
99
Joe Onorato981c9262023-06-21 15:16:23 -0700100// Provider published by aconfig_value_set
101type declarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700102 Package string
103 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700104}
105
Joe Onorato981c9262023-06-21 15:16:23 -0700106var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
Joe Onorato175073c2023-06-01 14:42:59 -0700107
Joe Onorato981c9262023-06-21 15:16:23 -0700108func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onoratofee845a2023-05-09 08:14:14 -0700110 ctx.VisitDirectDeps(func(dep android.Module) {
111 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
112 // Other modules get injected as dependencies too, for example the license modules
113 return
114 }
115 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato81b25ed2023-06-21 13:49:37 -0700116 valuesFiles, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700117 if ok {
118 for _, path := range valuesFiles {
119 module.properties.Values = append(module.properties.Values, path.String())
120 }
121 }
122 })
123
124 // Intermediate format
125 inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200126 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Joe Onoratofee845a2023-05-09 08:14:14 -0700127 ctx.Build(pctx, android.BuildParams{
128 Rule: aconfigRule,
Joe Onorato175073c2023-06-01 14:42:59 -0700129 Output: intermediatePath,
Joe Onorato981c9262023-06-21 15:16:23 -0700130 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700131 Args: map[string]string{
132 "release_version": ctx.Config().ReleaseVersion(),
Joe Onorato81b25ed2023-06-21 13:49:37 -0700133 "package": module.properties.Package,
Zhi Dou8a35a6f2023-07-19 18:04:24 +0000134 "declarations": android.JoinPathsWithPrefix(inputFiles, "--declarations "),
Joe Onoratofee845a2023-05-09 08:14:14 -0700135 "values": joinAndPrefix(" --values ", module.properties.Values),
136 },
137 })
138
Joe Onorato981c9262023-06-21 15:16:23 -0700139 ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700140 Package: module.properties.Package,
141 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700142 })
143
Joe Onoratofee845a2023-05-09 08:14:14 -0700144}