blob: 9bf75649a7a4fa5b3a1e00cb8c22e74da32ef2cf [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"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000023
Vinh Tran457ddef2023-08-02 13:50:26 -040024 "github.com/google/blueprint"
Joe Onoratofee845a2023-05-09 08:14:14 -070025)
26
Joe Onorato981c9262023-06-21 15:16:23 -070027type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070028 android.ModuleBase
29 android.DefaultableModuleBase
Yu Liu2cc802a2023-09-05 17:19:45 -070030 android.BazelModuleBase
Joe Onoratofee845a2023-05-09 08:14:14 -070031
Joe Onorato981c9262023-06-21 15:16:23 -070032 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070033 properties struct {
34 // aconfig files, relative to this Android.bp file
35 Srcs []string `android:"path"`
36
Joe Onorato81b25ed2023-06-21 13:49:37 -070037 // Release config flag package
38 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070039
Joe Onorato981c9262023-06-21 15:16:23 -070040 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070041 Values []string `blueprint:"mutated"`
Yu Liueae7b362023-11-16 17:05:47 -080042
43 // Container(system/vendor/apex) that this module belongs to
44 Container string
Joe Onoratofee845a2023-05-09 08:14:14 -070045 }
46
47 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070048}
49
Joe Onorato981c9262023-06-21 15:16:23 -070050func DeclarationsFactory() android.Module {
51 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070052
53 android.InitAndroidModule(module)
54 android.InitDefaultableModule(module)
55 module.AddProperties(&module.properties)
Yu Liu2cc802a2023-09-05 17:19:45 -070056 android.InitBazelModule(module)
Joe Onoratofee845a2023-05-09 08:14:14 -070057
58 return module
59}
60
61type implicitValuesTagType struct {
62 blueprint.BaseDependencyTag
63}
64
65var implicitValuesTag = implicitValuesTagType{}
66
Joe Onorato981c9262023-06-21 15:16:23 -070067func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070068 // Validate Properties
69 if len(module.properties.Srcs) == 0 {
70 ctx.PropertyErrorf("srcs", "missing source files")
71 return
72 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070073 if len(module.properties.Package) == 0 {
74 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070075 }
Yu Liueae7b362023-11-16 17:05:47 -080076 // TODO(b/311155208): Add mandatory check for container after all pre-existing
77 // ones are changed.
Joe Onoratofee845a2023-05-09 08:14:14 -070078
Joe Onorato981c9262023-06-21 15:16:23 -070079 // Add a dependency on the aconfig_value_sets defined in
80 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070081 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070082 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070083 if len(valuesFromConfig) > 0 {
84 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070085 }
Joe Onoratofee845a2023-05-09 08:14:14 -070086}
87
Joe Onorato981c9262023-06-21 15:16:23 -070088func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070089 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070090 case "":
91 // The default output of this module is the intermediates format, which is
92 // not installable and in a private format that no other rules can handle
93 // correctly.
94 return []android.Path{module.intermediatePath}, nil
95 default:
Joe Onorato981c9262023-06-21 15:16:23 -070096 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070097 }
98}
99
100func joinAndPrefix(prefix string, values []string) string {
101 var sb strings.Builder
102 for _, v := range values {
103 sb.WriteString(prefix)
104 sb.WriteString(v)
105 }
106 return sb.String()
107}
108
Zhi Dou3f65a412023-08-10 21:47:40 +0000109func optionalVariable(prefix string, value string) string {
110 var sb strings.Builder
111 if value != "" {
112 sb.WriteString(prefix)
113 sb.WriteString(value)
114 }
115 return sb.String()
116}
117
Joe Onorato981c9262023-06-21 15:16:23 -0700118// Provider published by aconfig_value_set
Yu Liueae7b362023-11-16 17:05:47 -0800119type DeclarationsProviderData struct {
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000120 Package string
121 Container string
122 IntermediateCacheOutputPath android.WritablePath
123 IntermediateDumpOutputPath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700124}
125
Yu Liueae7b362023-11-16 17:05:47 -0800126var DeclarationsProviderKey = blueprint.NewProvider(DeclarationsProviderData{})
127
128// This is used to collect the aconfig declarations info on the transitive closure,
129// the data is keyed on the container.
130type TransitiveDeclarationsInfo struct {
Colin Crossd788b3e2023-11-28 13:14:56 -0800131 AconfigFiles map[string]android.Paths
Yu Liueae7b362023-11-16 17:05:47 -0800132}
133
134var TransitiveDeclarationsInfoProvider = blueprint.NewProvider(TransitiveDeclarationsInfo{})
Joe Onorato175073c2023-06-01 14:42:59 -0700135
Joe Onorato981c9262023-06-21 15:16:23 -0700136func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
137 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700138 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700139 ctx.VisitDirectDeps(func(dep android.Module) {
140 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
141 // Other modules get injected as dependencies too, for example the license modules
142 return
143 }
144 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato4551ea12023-08-19 19:02:15 -0700145 paths, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700146 if ok {
Joe Onorato4551ea12023-08-19 19:02:15 -0700147 valuesFiles = append(valuesFiles, paths...)
148 for _, path := range paths {
Joe Onoratofee845a2023-05-09 08:14:14 -0700149 module.properties.Values = append(module.properties.Values, path.String())
150 }
151 }
152 })
153
154 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700155 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000156 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000157 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700158 inputFiles := make([]android.Path, len(declarationFiles))
159 copy(inputFiles, declarationFiles)
160 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700161 ctx.Build(pctx, android.BuildParams{
162 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000163 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700164 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700165 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700166 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000167 "release_version": ctx.Config().ReleaseVersion(),
168 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700169 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000170 "values": joinAndPrefix(" --values ", module.properties.Values),
171 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700172 },
173 })
174
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000175 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
176 ctx.Build(pctx, android.BuildParams{
177 Rule: aconfigTextRule,
178 Output: intermediateDumpFilePath,
179 Inputs: android.Paths{intermediateCacheFilePath},
180 Description: "aconfig_text",
181 })
182
Yu Liueae7b362023-11-16 17:05:47 -0800183 ctx.SetProvider(DeclarationsProviderKey, DeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000184 Package: module.properties.Package,
185 Container: module.properties.Container,
186 IntermediateCacheOutputPath: intermediateCacheFilePath,
187 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700188 })
189
Joe Onoratofee845a2023-05-09 08:14:14 -0700190}
Colin Crossd788b3e2023-11-28 13:14:56 -0800191func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) {
192 if *mergedAconfigFiles == nil {
193 *mergedAconfigFiles = make(map[string]android.Paths)
Yu Liueae7b362023-11-16 17:05:47 -0800194 }
195 ctx.VisitDirectDeps(func(module android.Module) {
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000196 if dep := ctx.OtherModuleProvider(module, DeclarationsProviderKey).(DeclarationsProviderData); dep.IntermediateCacheOutputPath != nil {
197 (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath)
Yu Liueae7b362023-11-16 17:05:47 -0800198 return
199 }
200 if dep := ctx.OtherModuleProvider(module, TransitiveDeclarationsInfoProvider).(TransitiveDeclarationsInfo); len(dep.AconfigFiles) > 0 {
Colin Crossd788b3e2023-11-28 13:14:56 -0800201 for container, v := range dep.AconfigFiles {
202 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
203 }
Yu Liueae7b362023-11-16 17:05:47 -0800204 }
205 })
206
Colin Crossd788b3e2023-11-28 13:14:56 -0800207 for container, aconfigFiles := range *mergedAconfigFiles {
208 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
209 }
210
Yu Liueae7b362023-11-16 17:05:47 -0800211 ctx.SetProvider(TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
Colin Crossd788b3e2023-11-28 13:14:56 -0800212 AconfigFiles: *mergedAconfigFiles,
Yu Liueae7b362023-11-16 17:05:47 -0800213 })
214}
215
Colin Crossd788b3e2023-11-28 13:14:56 -0800216func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
217 if len(inputs) == 1 {
218 return android.Paths{inputs[0]}
Yu Liueae7b362023-11-16 17:05:47 -0800219 }
Colin Crossd788b3e2023-11-28 13:14:56 -0800220
221 output := android.PathForModuleOut(ctx, "aconfig_merged.pb")
222
223 ctx.Build(pctx, android.BuildParams{
224 Rule: mergeAconfigFilesRule,
225 Description: "merge aconfig files",
226 Inputs: inputs,
227 Output: output,
228 Args: map[string]string{
229 "flags": android.JoinWithPrefix(inputs.Strings(), "--cache "),
230 },
231 })
232
233 return android.Paths{output}
Yu Liueae7b362023-11-16 17:05:47 -0800234}
235
Yu Liu2cc802a2023-09-05 17:19:45 -0700236type bazelAconfigDeclarationsAttributes struct {
237 Srcs bazel.LabelListAttribute
238 Package string
239}
240
Chris Parsons637458d2023-09-19 20:09:00 +0000241func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Yu Liu2cc802a2023-09-05 17:19:45 -0700242 if ctx.ModuleType() != "aconfig_declarations" {
243 return
244 }
245 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs))
246
247 attrs := bazelAconfigDeclarationsAttributes{
248 Srcs: srcs,
249 Package: module.properties.Package,
250 }
251 props := bazel.BazelTargetModuleProperties{
252 Rule_class: "aconfig_declarations",
253 Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl",
254 }
255
256 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
257}