blob: e662f1da4e9883a1fbce2dcc7149bdbe91d1c493 [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 (
Joe Onoratofee845a2023-05-09 08:14:14 -070018 "fmt"
Joe Onoratofee845a2023-05-09 08:14:14 -070019 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040020
Yu Liu2cc802a2023-09-05 17:19:45 -070021 "android/soong/android"
22 "android/soong/bazel"
Vinh Tran457ddef2023-08-02 13:50:26 -040023 "github.com/google/blueprint"
Joe Onoratofee845a2023-05-09 08:14:14 -070024)
25
Joe Onorato981c9262023-06-21 15:16:23 -070026type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070027 android.ModuleBase
28 android.DefaultableModuleBase
Yu Liu2cc802a2023-09-05 17:19:45 -070029 android.BazelModuleBase
Joe Onoratofee845a2023-05-09 08:14:14 -070030
Joe Onorato981c9262023-06-21 15:16:23 -070031 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070032 properties struct {
33 // aconfig files, relative to this Android.bp file
34 Srcs []string `android:"path"`
35
Joe Onorato81b25ed2023-06-21 13:49:37 -070036 // Release config flag package
37 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070038
Joe Onorato981c9262023-06-21 15:16:23 -070039 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070040 Values []string `blueprint:"mutated"`
Yu Liueae7b362023-11-16 17:05:47 -080041
42 // Container(system/vendor/apex) that this module belongs to
43 Container string
Joe Onoratofee845a2023-05-09 08:14:14 -070044 }
45
46 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070047}
48
Joe Onorato981c9262023-06-21 15:16:23 -070049func DeclarationsFactory() android.Module {
50 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070051
52 android.InitAndroidModule(module)
53 android.InitDefaultableModule(module)
54 module.AddProperties(&module.properties)
Yu Liu2cc802a2023-09-05 17:19:45 -070055 android.InitBazelModule(module)
Joe Onoratofee845a2023-05-09 08:14:14 -070056
57 return module
58}
59
60type implicitValuesTagType struct {
61 blueprint.BaseDependencyTag
62}
63
64var implicitValuesTag = implicitValuesTagType{}
65
Joe Onorato981c9262023-06-21 15:16:23 -070066func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070067 // Validate Properties
68 if len(module.properties.Srcs) == 0 {
69 ctx.PropertyErrorf("srcs", "missing source files")
70 return
71 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070072 if len(module.properties.Package) == 0 {
73 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070074 }
Yu Liueae7b362023-11-16 17:05:47 -080075 // TODO(b/311155208): Add mandatory check for container after all pre-existing
76 // ones are changed.
Joe Onoratofee845a2023-05-09 08:14:14 -070077
Joe Onorato981c9262023-06-21 15:16:23 -070078 // Add a dependency on the aconfig_value_sets defined in
79 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070080 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070081 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070082 if len(valuesFromConfig) > 0 {
83 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070084 }
Joe Onoratofee845a2023-05-09 08:14:14 -070085}
86
Joe Onorato981c9262023-06-21 15:16:23 -070087func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070088 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070089 case "":
90 // The default output of this module is the intermediates format, which is
91 // not installable and in a private format that no other rules can handle
92 // correctly.
93 return []android.Path{module.intermediatePath}, nil
94 default:
Joe Onorato981c9262023-06-21 15:16:23 -070095 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070096 }
97}
98
99func joinAndPrefix(prefix string, values []string) string {
100 var sb strings.Builder
101 for _, v := range values {
102 sb.WriteString(prefix)
103 sb.WriteString(v)
104 }
105 return sb.String()
106}
107
Zhi Dou3f65a412023-08-10 21:47:40 +0000108func optionalVariable(prefix string, value string) string {
109 var sb strings.Builder
110 if value != "" {
111 sb.WriteString(prefix)
112 sb.WriteString(value)
113 }
114 return sb.String()
115}
116
Joe Onorato981c9262023-06-21 15:16:23 -0700117// Provider published by aconfig_value_set
Yu Liueae7b362023-11-16 17:05:47 -0800118type DeclarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700119 Package string
Yu Liueae7b362023-11-16 17:05:47 -0800120 Container string
Joe Onorato81b25ed2023-06-21 13:49:37 -0700121 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700122}
123
Yu Liueae7b362023-11-16 17:05:47 -0800124var DeclarationsProviderKey = blueprint.NewProvider(DeclarationsProviderData{})
125
126// This is used to collect the aconfig declarations info on the transitive closure,
127// the data is keyed on the container.
128type TransitiveDeclarationsInfo struct {
129 AconfigFiles map[string]*android.DepSet[android.Path]
130}
131
132var TransitiveDeclarationsInfoProvider = blueprint.NewProvider(TransitiveDeclarationsInfo{})
Joe Onorato175073c2023-06-01 14:42:59 -0700133
Joe Onorato981c9262023-06-21 15:16:23 -0700134func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
135 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700136 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700137 ctx.VisitDirectDeps(func(dep android.Module) {
138 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
139 // Other modules get injected as dependencies too, for example the license modules
140 return
141 }
142 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato4551ea12023-08-19 19:02:15 -0700143 paths, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700144 if ok {
Joe Onorato4551ea12023-08-19 19:02:15 -0700145 valuesFiles = append(valuesFiles, paths...)
146 for _, path := range paths {
Joe Onoratofee845a2023-05-09 08:14:14 -0700147 module.properties.Values = append(module.properties.Values, path.String())
148 }
149 }
150 })
151
152 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700153 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200154 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000155 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700156 inputFiles := make([]android.Path, len(declarationFiles))
157 copy(inputFiles, declarationFiles)
158 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700159 ctx.Build(pctx, android.BuildParams{
160 Rule: aconfigRule,
Joe Onorato175073c2023-06-01 14:42:59 -0700161 Output: intermediatePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700162 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700163 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700164 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000165 "release_version": ctx.Config().ReleaseVersion(),
166 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700167 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000168 "values": joinAndPrefix(" --values ", module.properties.Values),
169 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700170 },
171 })
172
Yu Liueae7b362023-11-16 17:05:47 -0800173 ctx.SetProvider(DeclarationsProviderKey, DeclarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700174 Package: module.properties.Package,
Yu Liueae7b362023-11-16 17:05:47 -0800175 Container: module.properties.Container,
Joe Onorato81b25ed2023-06-21 13:49:37 -0700176 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700177 })
178
Joe Onoratofee845a2023-05-09 08:14:14 -0700179}
Yu Liu2cc802a2023-09-05 17:19:45 -0700180
Yu Liueae7b362023-11-16 17:05:47 -0800181func CollectTransitiveAconfigFiles(ctx android.ModuleContext, transitiveAconfigFiles *map[string]*android.DepSet[android.Path]) {
182 if *transitiveAconfigFiles == nil {
183 *transitiveAconfigFiles = make(map[string]*android.DepSet[android.Path])
184 }
185 ctx.VisitDirectDeps(func(module android.Module) {
186 if dep := ctx.OtherModuleProvider(module, DeclarationsProviderKey).(DeclarationsProviderData); dep.IntermediatePath != nil {
187 aconfigMap := make(map[string]*android.DepSet[android.Path])
188 aconfigMap[dep.Container] = android.NewDepSet(android.POSTORDER, []android.Path{dep.IntermediatePath}, nil)
189 mergeTransitiveAconfigFiles(aconfigMap, *transitiveAconfigFiles)
190 return
191 }
192 if dep := ctx.OtherModuleProvider(module, TransitiveDeclarationsInfoProvider).(TransitiveDeclarationsInfo); len(dep.AconfigFiles) > 0 {
193 mergeTransitiveAconfigFiles(dep.AconfigFiles, *transitiveAconfigFiles)
194 }
195 })
196
197 ctx.SetProvider(TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
198 AconfigFiles: *transitiveAconfigFiles,
199 })
200}
201
202func mergeTransitiveAconfigFiles(from, to map[string]*android.DepSet[android.Path]) {
203 for fromKey, fromValue := range from {
204 if fromValue == nil {
205 continue
206 }
207 toValue, ok := to[fromKey]
208 if !ok {
209 to[fromKey] = fromValue
210 } else {
211 to[fromKey] = android.NewDepSet(android.POSTORDER, toValue.ToList(), []*android.DepSet[android.Path]{fromValue})
212 }
213 }
214}
215
Yu Liu2cc802a2023-09-05 17:19:45 -0700216type bazelAconfigDeclarationsAttributes struct {
217 Srcs bazel.LabelListAttribute
218 Package string
219}
220
Chris Parsons637458d2023-09-19 20:09:00 +0000221func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Yu Liu2cc802a2023-09-05 17:19:45 -0700222 if ctx.ModuleType() != "aconfig_declarations" {
223 return
224 }
225 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs))
226
227 attrs := bazelAconfigDeclarationsAttributes{
228 Srcs: srcs,
229 Package: module.properties.Package,
230 }
231 props := bazel.BazelTargetModuleProperties{
232 Rule_class: "aconfig_declarations",
233 Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl",
234 }
235
236 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
237}