blob: 272ab038eafda02eadd23c9704ba5fd370ba673c [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) {
137 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
138 // Other modules get injected as dependencies too, for example the license modules
139 return
140 }
141 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato4551ea12023-08-19 19:02:15 -0700142 paths, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700143 if ok {
Joe Onorato4551ea12023-08-19 19:02:15 -0700144 valuesFiles = append(valuesFiles, paths...)
145 for _, path := range paths {
Joe Onoratofee845a2023-05-09 08:14:14 -0700146 module.properties.Values = append(module.properties.Values, path.String())
147 }
148 }
149 })
150
151 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700152 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000153 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000154 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700155 inputFiles := make([]android.Path, len(declarationFiles))
156 copy(inputFiles, declarationFiles)
157 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700158 ctx.Build(pctx, android.BuildParams{
159 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000160 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700161 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700162 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700163 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000164 "release_version": ctx.Config().ReleaseVersion(),
165 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700166 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000167 "values": joinAndPrefix(" --values ", module.properties.Values),
168 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700169 },
170 })
171
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000172 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
173 ctx.Build(pctx, android.BuildParams{
174 Rule: aconfigTextRule,
175 Output: intermediateDumpFilePath,
176 Inputs: android.Paths{intermediateCacheFilePath},
177 Description: "aconfig_text",
178 })
179
Yu Liueae7b362023-11-16 17:05:47 -0800180 ctx.SetProvider(DeclarationsProviderKey, DeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000181 Package: module.properties.Package,
182 Container: module.properties.Container,
183 IntermediateCacheOutputPath: intermediateCacheFilePath,
184 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700185 })
186
Joe Onoratofee845a2023-05-09 08:14:14 -0700187}
Colin Crossd788b3e2023-11-28 13:14:56 -0800188func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) {
189 if *mergedAconfigFiles == nil {
190 *mergedAconfigFiles = make(map[string]android.Paths)
Yu Liueae7b362023-11-16 17:05:47 -0800191 }
192 ctx.VisitDirectDeps(func(module android.Module) {
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000193 if dep := ctx.OtherModuleProvider(module, DeclarationsProviderKey).(DeclarationsProviderData); dep.IntermediateCacheOutputPath != nil {
194 (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath)
Yu Liueae7b362023-11-16 17:05:47 -0800195 return
196 }
197 if dep := ctx.OtherModuleProvider(module, TransitiveDeclarationsInfoProvider).(TransitiveDeclarationsInfo); len(dep.AconfigFiles) > 0 {
Colin Crossd788b3e2023-11-28 13:14:56 -0800198 for container, v := range dep.AconfigFiles {
199 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
200 }
Yu Liueae7b362023-11-16 17:05:47 -0800201 }
202 })
203
Colin Crossd788b3e2023-11-28 13:14:56 -0800204 for container, aconfigFiles := range *mergedAconfigFiles {
205 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
206 }
207
Yu Liueae7b362023-11-16 17:05:47 -0800208 ctx.SetProvider(TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
Colin Crossd788b3e2023-11-28 13:14:56 -0800209 AconfigFiles: *mergedAconfigFiles,
Yu Liueae7b362023-11-16 17:05:47 -0800210 })
211}
212
Colin Crossd788b3e2023-11-28 13:14:56 -0800213func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
Yu Liucec0e412023-11-30 16:45:50 -0800214 inputs = android.LastUniquePaths(inputs)
Colin Crossd788b3e2023-11-28 13:14:56 -0800215 if len(inputs) == 1 {
216 return android.Paths{inputs[0]}
Yu Liueae7b362023-11-16 17:05:47 -0800217 }
Colin Crossd788b3e2023-11-28 13:14:56 -0800218
219 output := android.PathForModuleOut(ctx, "aconfig_merged.pb")
220
221 ctx.Build(pctx, android.BuildParams{
222 Rule: mergeAconfigFilesRule,
223 Description: "merge aconfig files",
224 Inputs: inputs,
225 Output: output,
226 Args: map[string]string{
227 "flags": android.JoinWithPrefix(inputs.Strings(), "--cache "),
228 },
229 })
230
231 return android.Paths{output}
Yu Liueae7b362023-11-16 17:05:47 -0800232}
Yu Liu6dc93f92023-12-14 01:19:35 +0000233
234func SetAconfigFileMkEntries(m *android.ModuleBase, entries *android.AndroidMkEntries, aconfigFiles map[string]android.Paths) {
235 if m.InstallInVendor() {
236 entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles["vendor"])
237 } else {
238 // TODO(b/311155208): The container here should be system.
239 entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[""])
240 }
241}