blob: 162079281e755bc078f50ffc8bfdbe77b8f40685 [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
35
Joe Onorato981c9262023-06-21 15:16:23 -070036 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070037 properties struct {
38 // aconfig files, relative to this Android.bp file
39 Srcs []string `android:"path"`
40
Joe Onorato81b25ed2023-06-21 13:49:37 -070041 // Release config flag package
42 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070043
LaMont Jones21d04d92024-06-11 11:28:54 -070044 // Values for release configs / RELEASE_ACONFIG_VALUE_SETS
45 // The current release config is `ReleaseConfig: ""`, others
46 // are from RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS.
47 ReleaseConfigValues []AconfigReleaseConfigValue
Yu Liueae7b362023-11-16 17:05:47 -080048
49 // Container(system/vendor/apex) that this module belongs to
50 Container string
Zi Wang0e5d16c2024-02-08 06:19:34 +000051
52 // The flags will only be repackaged if this prop is true.
53 Exportable bool
Joe Onoratofee845a2023-05-09 08:14:14 -070054 }
Joe Onoratofee845a2023-05-09 08:14:14 -070055}
56
Joe Onorato981c9262023-06-21 15:16:23 -070057func DeclarationsFactory() android.Module {
58 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070059
60 android.InitAndroidModule(module)
61 android.InitDefaultableModule(module)
62 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070063
64 return module
65}
66
67type implicitValuesTagType struct {
68 blueprint.BaseDependencyTag
LaMont Jones21d04d92024-06-11 11:28:54 -070069
70 // The release config name for these values.
71 // Empty string for the actual current release config.
72 ReleaseConfig string
Joe Onoratofee845a2023-05-09 08:14:14 -070073}
74
75var implicitValuesTag = implicitValuesTagType{}
76
Joe Onorato981c9262023-06-21 15:16:23 -070077func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070078 // Validate Properties
79 if len(module.properties.Srcs) == 0 {
80 ctx.PropertyErrorf("srcs", "missing source files")
81 return
82 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070083 if len(module.properties.Package) == 0 {
84 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070085 }
Yu Liu315a53c2024-04-24 16:41:57 +000086 if len(module.properties.Container) == 0 {
87 ctx.PropertyErrorf("container", "missing container property")
88 }
Joe Onoratofee845a2023-05-09 08:14:14 -070089
Joe Onorato981c9262023-06-21 15:16:23 -070090 // Add a dependency on the aconfig_value_sets defined in
91 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070092 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070093 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070094 if len(valuesFromConfig) > 0 {
95 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070096 }
LaMont Jones21d04d92024-06-11 11:28:54 -070097 for rcName, valueSets := range ctx.Config().ReleaseAconfigExtraReleaseConfigsValueSets() {
98 if len(valueSets) > 0 {
99 ctx.AddDependency(ctx.Module(), implicitValuesTagType{ReleaseConfig: rcName}, valueSets...)
100 }
101 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700102}
103
Joe Onoratofee845a2023-05-09 08:14:14 -0700104func joinAndPrefix(prefix string, values []string) string {
105 var sb strings.Builder
106 for _, v := range values {
107 sb.WriteString(prefix)
108 sb.WriteString(v)
109 }
110 return sb.String()
111}
112
Zhi Dou3f65a412023-08-10 21:47:40 +0000113func optionalVariable(prefix string, value string) string {
114 var sb strings.Builder
115 if value != "" {
116 sb.WriteString(prefix)
117 sb.WriteString(value)
118 }
119 return sb.String()
120}
121
LaMont Jones21d04d92024-06-11 11:28:54 -0700122// Assemble the actual filename.
123// If `rcName` is not empty, then insert "-{rcName}" into the path before the
124// file extension.
125func assembleFileName(rcName, path string) string {
126 if rcName == "" {
127 return path
128 }
129 dir, file := filepath.Split(path)
130 rcName = "-" + rcName
131 ext := filepath.Ext(file)
132 base := file[:len(file)-len(ext)]
133 return dir + base + rcName + ext
134}
135
Joe Onorato981c9262023-06-21 15:16:23 -0700136func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
LaMont Jones21d04d92024-06-11 11:28:54 -0700137 // Determine which release configs we are processing.
138 //
139 // We always process the current release config (empty string).
140 // We may have been told to also create artifacts for some others.
141 configs := append([]string{""}, ctx.Config().ReleaseAconfigExtraReleaseConfigs()...)
142 slices.Sort(configs)
143
144 values := make(map[string][]string)
145 valuesFiles := make(map[string][]android.Path, 0)
146 providerData := android.AconfigReleaseDeclarationsProviderData{}
Joe Onoratofee845a2023-05-09 08:14:14 -0700147 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800148 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
LaMont Jones21d04d92024-06-11 11:28:54 -0700149 depTag := ctx.OtherModuleDependencyTag(dep)
150 for _, config := range configs {
151 tag := implicitValuesTagType{ReleaseConfig: config}
152 if depTag == tag {
153 paths, ok := depData.AvailablePackages[module.properties.Package]
154 if ok {
155 valuesFiles[config] = append(valuesFiles[config], paths...)
156 for _, path := range paths {
157 values[config] = append(values[config], path.String())
158 }
159 }
Colin Cross313aa542023-12-13 13:47:44 -0800160 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700161 }
162 }
163 })
LaMont Jones21d04d92024-06-11 11:28:54 -0700164 for _, config := range configs {
165 module.properties.ReleaseConfigValues = append(module.properties.ReleaseConfigValues, AconfigReleaseConfigValue{
166 ReleaseConfig: config,
167 Values: values[config],
168 })
Joe Onoratofee845a2023-05-09 08:14:14 -0700169
LaMont Jones21d04d92024-06-11 11:28:54 -0700170 // Intermediate format
171 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
172 intermediateCacheFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.pb"))
173 var defaultPermission string
174 defaultPermission = ctx.Config().ReleaseAconfigFlagDefaultPermission()
175 if config != "" {
176 if confPerm, ok := ctx.Config().GetBuildFlag("RELEASE_ACONFIG_FLAG_DEFAULT_PERMISSION_" + config); ok {
177 defaultPermission = confPerm
178 }
179 }
180 inputFiles := make([]android.Path, len(declarationFiles))
181 copy(inputFiles, declarationFiles)
182 inputFiles = append(inputFiles, valuesFiles[config]...)
183 args := map[string]string{
184 "release_version": ctx.Config().ReleaseVersion(),
185 "package": module.properties.Package,
186 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
187 "values": joinAndPrefix(" --values ", values[config]),
188 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
189 }
190 if len(module.properties.Container) > 0 {
191 args["container"] = "--container " + module.properties.Container
192 }
193 ctx.Build(pctx, android.BuildParams{
194 Rule: aconfigRule,
195 Output: intermediateCacheFilePath,
196 Inputs: inputFiles,
197 Description: "aconfig_declarations",
198 Args: args,
199 })
200
201 intermediateDumpFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.txt"))
202 ctx.Build(pctx, android.BuildParams{
203 Rule: aconfigTextRule,
204 Output: intermediateDumpFilePath,
205 Inputs: android.Paths{intermediateCacheFilePath},
206 Description: "aconfig_text",
207 })
208
209 providerData[config] = android.AconfigDeclarationsProviderData{
210 Package: module.properties.Package,
211 Container: module.properties.Container,
212 Exportable: module.properties.Exportable,
213 IntermediateCacheOutputPath: intermediateCacheFilePath,
214 IntermediateDumpOutputPath: intermediateDumpFilePath,
215 }
Yu Liueeff2222024-03-19 23:07:34 +0000216 }
LaMont Jones21d04d92024-06-11 11:28:54 -0700217 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, providerData[""])
218 android.SetProvider(ctx, android.AconfigReleaseDeclarationsProviderKey, providerData)
Joe Onoratofee845a2023-05-09 08:14:14 -0700219}