blob: 14438c82f736576c3a2512a68fc33d145c7b9595 [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"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000022
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
29
Joe Onorato981c9262023-06-21 15:16:23 -070030 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070031 properties struct {
32 // aconfig files, relative to this Android.bp file
33 Srcs []string `android:"path"`
34
Joe Onorato81b25ed2023-06-21 13:49:37 -070035 // Release config flag package
36 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070037
Joe Onorato981c9262023-06-21 15:16:23 -070038 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070039 Values []string `blueprint:"mutated"`
Yu Liueae7b362023-11-16 17:05:47 -080040
41 // Container(system/vendor/apex) that this module belongs to
42 Container string
Joe Onoratofee845a2023-05-09 08:14:14 -070043 }
44
45 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070046}
47
Joe Onorato981c9262023-06-21 15:16:23 -070048func DeclarationsFactory() android.Module {
49 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070050
51 android.InitAndroidModule(module)
52 android.InitDefaultableModule(module)
53 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070054
55 return module
56}
57
58type implicitValuesTagType struct {
59 blueprint.BaseDependencyTag
60}
61
62var implicitValuesTag = implicitValuesTagType{}
63
Joe Onorato981c9262023-06-21 15:16:23 -070064func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070065 // Validate Properties
66 if len(module.properties.Srcs) == 0 {
67 ctx.PropertyErrorf("srcs", "missing source files")
68 return
69 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070070 if len(module.properties.Package) == 0 {
71 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070072 }
Yu Liueae7b362023-11-16 17:05:47 -080073 // TODO(b/311155208): Add mandatory check for container after all pre-existing
74 // ones are changed.
Joe Onoratofee845a2023-05-09 08:14:14 -070075
Joe Onorato981c9262023-06-21 15:16:23 -070076 // Add a dependency on the aconfig_value_sets defined in
77 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070078 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070079 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070080 if len(valuesFromConfig) > 0 {
81 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070082 }
Joe Onoratofee845a2023-05-09 08:14:14 -070083}
84
Joe Onorato981c9262023-06-21 15:16:23 -070085func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070086 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070087 case "":
88 // The default output of this module is the intermediates format, which is
89 // not installable and in a private format that no other rules can handle
90 // correctly.
91 return []android.Path{module.intermediatePath}, nil
92 default:
Joe Onorato981c9262023-06-21 15:16:23 -070093 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070094 }
95}
96
97func joinAndPrefix(prefix string, values []string) string {
98 var sb strings.Builder
99 for _, v := range values {
100 sb.WriteString(prefix)
101 sb.WriteString(v)
102 }
103 return sb.String()
104}
105
Zhi Dou3f65a412023-08-10 21:47:40 +0000106func optionalVariable(prefix string, value string) string {
107 var sb strings.Builder
108 if value != "" {
109 sb.WriteString(prefix)
110 sb.WriteString(value)
111 }
112 return sb.String()
113}
114
Joe Onorato981c9262023-06-21 15:16:23 -0700115// Provider published by aconfig_value_set
Yu Liueae7b362023-11-16 17:05:47 -0800116type DeclarationsProviderData struct {
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000117 Package string
118 Container string
119 IntermediateCacheOutputPath android.WritablePath
120 IntermediateDumpOutputPath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700121}
122
Colin Crossbc7d76c2023-12-12 16:39:03 -0800123var DeclarationsProviderKey = blueprint.NewProvider[DeclarationsProviderData]()
Yu Liueae7b362023-11-16 17:05:47 -0800124
125// This is used to collect the aconfig declarations info on the transitive closure,
126// the data is keyed on the container.
127type TransitiveDeclarationsInfo struct {
Colin Crossd788b3e2023-11-28 13:14:56 -0800128 AconfigFiles map[string]android.Paths
Yu Liueae7b362023-11-16 17:05:47 -0800129}
130
Colin Crossbc7d76c2023-12-12 16:39:03 -0800131var TransitiveDeclarationsInfoProvider = blueprint.NewProvider[TransitiveDeclarationsInfo]()
Joe Onorato175073c2023-06-01 14:42:59 -0700132
Joe Onorato981c9262023-06-21 15:16:23 -0700133func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
134 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700135 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700136 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800137 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
138 paths, ok := depData.AvailablePackages[module.properties.Package]
139 if ok {
140 valuesFiles = append(valuesFiles, paths...)
141 for _, path := range paths {
142 module.properties.Values = append(module.properties.Values, path.String())
143 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700144 }
145 }
146 })
147
148 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700149 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000150 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000151 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700152 inputFiles := make([]android.Path, len(declarationFiles))
153 copy(inputFiles, declarationFiles)
154 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700155 ctx.Build(pctx, android.BuildParams{
156 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000157 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700158 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700159 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700160 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000161 "release_version": ctx.Config().ReleaseVersion(),
162 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700163 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000164 "values": joinAndPrefix(" --values ", module.properties.Values),
165 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700166 },
167 })
168
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000169 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
170 ctx.Build(pctx, android.BuildParams{
171 Rule: aconfigTextRule,
172 Output: intermediateDumpFilePath,
173 Inputs: android.Paths{intermediateCacheFilePath},
174 Description: "aconfig_text",
175 })
176
Colin Cross40213022023-12-13 15:19:49 -0800177 android.SetProvider(ctx, DeclarationsProviderKey, DeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000178 Package: module.properties.Package,
179 Container: module.properties.Container,
180 IntermediateCacheOutputPath: intermediateCacheFilePath,
181 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700182 })
183
Joe Onoratofee845a2023-05-09 08:14:14 -0700184}
Colin Crossd788b3e2023-11-28 13:14:56 -0800185func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) {
186 if *mergedAconfigFiles == nil {
187 *mergedAconfigFiles = make(map[string]android.Paths)
Yu Liueae7b362023-11-16 17:05:47 -0800188 }
189 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800190 if dep, _ := android.OtherModuleProvider(ctx, module, DeclarationsProviderKey); dep.IntermediateCacheOutputPath != nil {
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000191 (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath)
Yu Liueae7b362023-11-16 17:05:47 -0800192 return
193 }
Colin Cross313aa542023-12-13 13:47:44 -0800194 if dep, _ := android.OtherModuleProvider(ctx, module, TransitiveDeclarationsInfoProvider); len(dep.AconfigFiles) > 0 {
Colin Crossd788b3e2023-11-28 13:14:56 -0800195 for container, v := range dep.AconfigFiles {
196 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
197 }
Yu Liueae7b362023-11-16 17:05:47 -0800198 }
199 })
200
Colin Crossd788b3e2023-11-28 13:14:56 -0800201 for container, aconfigFiles := range *mergedAconfigFiles {
202 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
203 }
204
Colin Cross40213022023-12-13 15:19:49 -0800205 android.SetProvider(ctx, TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
Colin Crossd788b3e2023-11-28 13:14:56 -0800206 AconfigFiles: *mergedAconfigFiles,
Yu Liueae7b362023-11-16 17:05:47 -0800207 })
208}
209
Colin Crossd788b3e2023-11-28 13:14:56 -0800210func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
Yu Liucec0e412023-11-30 16:45:50 -0800211 inputs = android.LastUniquePaths(inputs)
Colin Crossd788b3e2023-11-28 13:14:56 -0800212 if len(inputs) == 1 {
213 return android.Paths{inputs[0]}
Yu Liueae7b362023-11-16 17:05:47 -0800214 }
Colin Crossd788b3e2023-11-28 13:14:56 -0800215
216 output := android.PathForModuleOut(ctx, "aconfig_merged.pb")
217
218 ctx.Build(pctx, android.BuildParams{
219 Rule: mergeAconfigFilesRule,
220 Description: "merge aconfig files",
221 Inputs: inputs,
222 Output: output,
223 Args: map[string]string{
224 "flags": android.JoinWithPrefix(inputs.Strings(), "--cache "),
225 },
226 })
227
228 return android.Paths{output}
Yu Liueae7b362023-11-16 17:05:47 -0800229}
Yu Liu6dc93f92023-12-14 01:19:35 +0000230
231func SetAconfigFileMkEntries(m *android.ModuleBase, entries *android.AndroidMkEntries, aconfigFiles map[string]android.Paths) {
Yu Liu22e32f12023-12-18 14:44:34 -0800232 // TODO(b/311155208): The default container here should be system.
233 container := ""
234
235 if m.SocSpecific() {
236 container = "vendor"
237 } else if m.ProductSpecific() {
238 container = "product"
239 } else if m.SystemExtSpecific() {
240 container = "system_ext"
Yu Liu6dc93f92023-12-14 01:19:35 +0000241 }
Yu Liu22e32f12023-12-18 14:44:34 -0800242
243 entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[container])
Yu Liu6dc93f92023-12-14 01:19:35 +0000244}