blob: 05a6371296c27bd8803b46d5c7cf658e7fe29cc2 [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 {
Colin Crossd788b3e2023-11-28 13:14:56 -0800129 AconfigFiles map[string]android.Paths
Yu Liueae7b362023-11-16 17:05:47 -0800130}
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}
Colin Crossd788b3e2023-11-28 13:14:56 -0800180func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) {
181 if *mergedAconfigFiles == nil {
182 *mergedAconfigFiles = make(map[string]android.Paths)
Yu Liueae7b362023-11-16 17:05:47 -0800183 }
184 ctx.VisitDirectDeps(func(module android.Module) {
185 if dep := ctx.OtherModuleProvider(module, DeclarationsProviderKey).(DeclarationsProviderData); dep.IntermediatePath != nil {
Colin Crossd788b3e2023-11-28 13:14:56 -0800186 (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediatePath)
Yu Liueae7b362023-11-16 17:05:47 -0800187 return
188 }
189 if dep := ctx.OtherModuleProvider(module, TransitiveDeclarationsInfoProvider).(TransitiveDeclarationsInfo); len(dep.AconfigFiles) > 0 {
Colin Crossd788b3e2023-11-28 13:14:56 -0800190 for container, v := range dep.AconfigFiles {
191 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
192 }
Yu Liueae7b362023-11-16 17:05:47 -0800193 }
194 })
195
Colin Crossd788b3e2023-11-28 13:14:56 -0800196 for container, aconfigFiles := range *mergedAconfigFiles {
197 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
198 }
199
Yu Liueae7b362023-11-16 17:05:47 -0800200 ctx.SetProvider(TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
Colin Crossd788b3e2023-11-28 13:14:56 -0800201 AconfigFiles: *mergedAconfigFiles,
Yu Liueae7b362023-11-16 17:05:47 -0800202 })
203}
204
Colin Crossd788b3e2023-11-28 13:14:56 -0800205func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
206 if len(inputs) == 1 {
207 return android.Paths{inputs[0]}
Yu Liueae7b362023-11-16 17:05:47 -0800208 }
Colin Crossd788b3e2023-11-28 13:14:56 -0800209
210 output := android.PathForModuleOut(ctx, "aconfig_merged.pb")
211
212 ctx.Build(pctx, android.BuildParams{
213 Rule: mergeAconfigFilesRule,
214 Description: "merge aconfig files",
215 Inputs: inputs,
216 Output: output,
217 Args: map[string]string{
218 "flags": android.JoinWithPrefix(inputs.Strings(), "--cache "),
219 },
220 })
221
222 return android.Paths{output}
Yu Liueae7b362023-11-16 17:05:47 -0800223}
224
Yu Liu2cc802a2023-09-05 17:19:45 -0700225type bazelAconfigDeclarationsAttributes struct {
226 Srcs bazel.LabelListAttribute
227 Package string
228}
229
Chris Parsons637458d2023-09-19 20:09:00 +0000230func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Yu Liu2cc802a2023-09-05 17:19:45 -0700231 if ctx.ModuleType() != "aconfig_declarations" {
232 return
233 }
234 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs))
235
236 attrs := bazelAconfigDeclarationsAttributes{
237 Srcs: srcs,
238 Package: module.properties.Package,
239 }
240 props := bazel.BazelTargetModuleProperties{
241 Rule_class: "aconfig_declarations",
242 Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl",
243 }
244
245 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
246}