blob: 250fd56d1263833ed21393efbf3691984d32a367 [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 (
LaMont Jones21d04d92024-06-11 11:28:54 -070018 "path/filepath"
19 "slices"
Joe Onoratofee845a2023-05-09 08:14:14 -070020 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040021
Yu Liu2cc802a2023-09-05 17:19:45 -070022 "android/soong/android"
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
LaMont Jones21d04d92024-06-11 11:28:54 -070027type AconfigReleaseConfigValue struct {
28 ReleaseConfig string
29 Values []string `blueprint:"mutated"`
30}
31
Joe Onorato981c9262023-06-21 15:16:23 -070032type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070033 android.ModuleBase
34 android.DefaultableModuleBase
Yu Liufa297642024-06-11 00:13:02 +000035 blueprint.IncrementalModule
Joe Onoratofee845a2023-05-09 08:14:14 -070036
Joe Onorato981c9262023-06-21 15:16:23 -070037 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070038 properties struct {
39 // aconfig files, relative to this Android.bp file
40 Srcs []string `android:"path"`
41
Joe Onorato81b25ed2023-06-21 13:49:37 -070042 // Release config flag package
43 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070044
LaMont Jones21d04d92024-06-11 11:28:54 -070045 // Values for release configs / RELEASE_ACONFIG_VALUE_SETS
46 // The current release config is `ReleaseConfig: ""`, others
47 // are from RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS.
48 ReleaseConfigValues []AconfigReleaseConfigValue
Yu Liueae7b362023-11-16 17:05:47 -080049
50 // Container(system/vendor/apex) that this module belongs to
51 Container string
Zi Wang0e5d16c2024-02-08 06:19:34 +000052
53 // The flags will only be repackaged if this prop is true.
54 Exportable bool
Joe Onoratofee845a2023-05-09 08:14:14 -070055 }
Joe Onoratofee845a2023-05-09 08:14:14 -070056}
57
Joe Onorato981c9262023-06-21 15:16:23 -070058func DeclarationsFactory() android.Module {
59 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070060
61 android.InitAndroidModule(module)
62 android.InitDefaultableModule(module)
63 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070064
65 return module
66}
67
68type implicitValuesTagType struct {
69 blueprint.BaseDependencyTag
LaMont Jones21d04d92024-06-11 11:28:54 -070070
71 // The release config name for these values.
72 // Empty string for the actual current release config.
73 ReleaseConfig string
Joe Onoratofee845a2023-05-09 08:14:14 -070074}
75
76var implicitValuesTag = implicitValuesTagType{}
77
Joe Onorato981c9262023-06-21 15:16:23 -070078func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070079 // Validate Properties
80 if len(module.properties.Srcs) == 0 {
81 ctx.PropertyErrorf("srcs", "missing source files")
82 return
83 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070084 if len(module.properties.Package) == 0 {
85 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070086 }
Yu Liu315a53c2024-04-24 16:41:57 +000087 if len(module.properties.Container) == 0 {
88 ctx.PropertyErrorf("container", "missing container property")
89 }
Joe Onoratofee845a2023-05-09 08:14:14 -070090
Joe Onorato981c9262023-06-21 15:16:23 -070091 // Add a dependency on the aconfig_value_sets defined in
92 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070093 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070094 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070095 if len(valuesFromConfig) > 0 {
96 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070097 }
LaMont Jones21d04d92024-06-11 11:28:54 -070098 for rcName, valueSets := range ctx.Config().ReleaseAconfigExtraReleaseConfigsValueSets() {
99 if len(valueSets) > 0 {
100 ctx.AddDependency(ctx.Module(), implicitValuesTagType{ReleaseConfig: rcName}, valueSets...)
101 }
102 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700103}
104
Joe Onoratofee845a2023-05-09 08:14:14 -0700105func joinAndPrefix(prefix string, values []string) string {
106 var sb strings.Builder
107 for _, v := range values {
108 sb.WriteString(prefix)
109 sb.WriteString(v)
110 }
111 return sb.String()
112}
113
Zhi Dou3f65a412023-08-10 21:47:40 +0000114func optionalVariable(prefix string, value string) string {
115 var sb strings.Builder
116 if value != "" {
117 sb.WriteString(prefix)
118 sb.WriteString(value)
119 }
120 return sb.String()
121}
122
LaMont Jones21d04d92024-06-11 11:28:54 -0700123// Assemble the actual filename.
124// If `rcName` is not empty, then insert "-{rcName}" into the path before the
125// file extension.
126func assembleFileName(rcName, path string) string {
127 if rcName == "" {
128 return path
129 }
130 dir, file := filepath.Split(path)
131 rcName = "-" + rcName
132 ext := filepath.Ext(file)
133 base := file[:len(file)-len(ext)]
134 return dir + base + rcName + ext
135}
136
Joe Onorato981c9262023-06-21 15:16:23 -0700137func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
LaMont Jones21d04d92024-06-11 11:28:54 -0700138 // Determine which release configs we are processing.
139 //
140 // We always process the current release config (empty string).
141 // We may have been told to also create artifacts for some others.
142 configs := append([]string{""}, ctx.Config().ReleaseAconfigExtraReleaseConfigs()...)
143 slices.Sort(configs)
144
145 values := make(map[string][]string)
146 valuesFiles := make(map[string][]android.Path, 0)
147 providerData := android.AconfigReleaseDeclarationsProviderData{}
Joe Onoratofee845a2023-05-09 08:14:14 -0700148 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800149 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
LaMont Jones21d04d92024-06-11 11:28:54 -0700150 depTag := ctx.OtherModuleDependencyTag(dep)
151 for _, config := range configs {
152 tag := implicitValuesTagType{ReleaseConfig: config}
153 if depTag == tag {
154 paths, ok := depData.AvailablePackages[module.properties.Package]
155 if ok {
156 valuesFiles[config] = append(valuesFiles[config], paths...)
157 for _, path := range paths {
158 values[config] = append(values[config], path.String())
159 }
160 }
Colin Cross313aa542023-12-13 13:47:44 -0800161 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700162 }
163 }
164 })
LaMont Jones21d04d92024-06-11 11:28:54 -0700165 for _, config := range configs {
166 module.properties.ReleaseConfigValues = append(module.properties.ReleaseConfigValues, AconfigReleaseConfigValue{
167 ReleaseConfig: config,
168 Values: values[config],
169 })
Joe Onoratofee845a2023-05-09 08:14:14 -0700170
LaMont Jones21d04d92024-06-11 11:28:54 -0700171 // Intermediate format
172 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
173 intermediateCacheFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.pb"))
174 var defaultPermission string
175 defaultPermission = ctx.Config().ReleaseAconfigFlagDefaultPermission()
176 if config != "" {
177 if confPerm, ok := ctx.Config().GetBuildFlag("RELEASE_ACONFIG_FLAG_DEFAULT_PERMISSION_" + config); ok {
178 defaultPermission = confPerm
179 }
180 }
181 inputFiles := make([]android.Path, len(declarationFiles))
182 copy(inputFiles, declarationFiles)
183 inputFiles = append(inputFiles, valuesFiles[config]...)
184 args := map[string]string{
185 "release_version": ctx.Config().ReleaseVersion(),
186 "package": module.properties.Package,
187 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
188 "values": joinAndPrefix(" --values ", values[config]),
189 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
190 }
191 if len(module.properties.Container) > 0 {
192 args["container"] = "--container " + module.properties.Container
193 }
194 ctx.Build(pctx, android.BuildParams{
195 Rule: aconfigRule,
196 Output: intermediateCacheFilePath,
197 Inputs: inputFiles,
198 Description: "aconfig_declarations",
199 Args: args,
200 })
201
202 intermediateDumpFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.txt"))
203 ctx.Build(pctx, android.BuildParams{
204 Rule: aconfigTextRule,
205 Output: intermediateDumpFilePath,
206 Inputs: android.Paths{intermediateCacheFilePath},
207 Description: "aconfig_text",
208 })
209
210 providerData[config] = android.AconfigDeclarationsProviderData{
211 Package: module.properties.Package,
212 Container: module.properties.Container,
213 Exportable: module.properties.Exportable,
214 IntermediateCacheOutputPath: intermediateCacheFilePath,
215 IntermediateDumpOutputPath: intermediateDumpFilePath,
216 }
Yu Liueeff2222024-03-19 23:07:34 +0000217 }
LaMont Jones21d04d92024-06-11 11:28:54 -0700218 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, providerData[""])
219 android.SetProvider(ctx, android.AconfigReleaseDeclarationsProviderKey, providerData)
Joe Onoratofee845a2023-05-09 08:14:14 -0700220}
Yu Liufa297642024-06-11 00:13:02 +0000221
Yu Liufa297642024-06-11 00:13:02 +0000222var _ blueprint.Incremental = &DeclarationsModule{}