Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 1 | // 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 Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 15 | package aconfig |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 18 | "path/filepath" |
| 19 | "slices" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 20 | "strings" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 21 | |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 23 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 27 | type AconfigReleaseConfigValue struct { |
| 28 | ReleaseConfig string |
| 29 | Values []string `blueprint:"mutated"` |
| 30 | } |
| 31 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 32 | type DeclarationsModule struct { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 33 | android.ModuleBase |
| 34 | android.DefaultableModuleBase |
| 35 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 36 | // Properties for "aconfig_declarations" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 37 | properties struct { |
| 38 | // aconfig files, relative to this Android.bp file |
| 39 | Srcs []string `android:"path"` |
| 40 | |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 41 | // Release config flag package |
| 42 | Package string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 43 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 44 | // 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 48 | |
| 49 | // Container(system/vendor/apex) that this module belongs to |
| 50 | Container string |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 51 | |
| 52 | // The flags will only be repackaged if this prop is true. |
| 53 | Exportable bool |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 54 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 57 | func DeclarationsFactory() android.Module { |
| 58 | module := &DeclarationsModule{} |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 59 | |
| 60 | android.InitAndroidModule(module) |
| 61 | android.InitDefaultableModule(module) |
| 62 | module.AddProperties(&module.properties) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 63 | |
| 64 | return module |
| 65 | } |
| 66 | |
| 67 | type implicitValuesTagType struct { |
| 68 | blueprint.BaseDependencyTag |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 69 | |
| 70 | // The release config name for these values. |
| 71 | // Empty string for the actual current release config. |
| 72 | ReleaseConfig string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | var implicitValuesTag = implicitValuesTagType{} |
| 76 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 77 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 78 | // Validate Properties |
| 79 | if len(module.properties.Srcs) == 0 { |
| 80 | ctx.PropertyErrorf("srcs", "missing source files") |
| 81 | return |
| 82 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 83 | if len(module.properties.Package) == 0 { |
| 84 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 85 | } |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 86 | if len(module.properties.Container) == 0 { |
| 87 | ctx.PropertyErrorf("container", "missing container property") |
| 88 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 89 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 90 | // Add a dependency on the aconfig_value_sets defined in |
| 91 | // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 92 | // match our package. |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 93 | valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 94 | if len(valuesFromConfig) > 0 { |
| 95 | ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 96 | } |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 97 | for rcName, valueSets := range ctx.Config().ReleaseAconfigExtraReleaseConfigsValueSets() { |
| 98 | if len(valueSets) > 0 { |
| 99 | ctx.AddDependency(ctx.Module(), implicitValuesTagType{ReleaseConfig: rcName}, valueSets...) |
| 100 | } |
| 101 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 104 | func 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 Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 113 | func 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 Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 122 | // Assemble the actual filename. |
| 123 | // If `rcName` is not empty, then insert "-{rcName}" into the path before the |
| 124 | // file extension. |
| 125 | func 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 Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 136 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 137 | // 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 Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 147 | ctx.VisitDirectDeps(func(dep android.Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 148 | if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok { |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 149 | 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 Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 160 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | }) |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 164 | for _, config := range configs { |
| 165 | module.properties.ReleaseConfigValues = append(module.properties.ReleaseConfigValues, AconfigReleaseConfigValue{ |
| 166 | ReleaseConfig: config, |
| 167 | Values: values[config], |
| 168 | }) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 169 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 170 | // 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 Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 216 | } |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame^] | 217 | android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, providerData[""]) |
| 218 | android.SetProvider(ctx, android.AconfigReleaseDeclarationsProviderKey, providerData) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 219 | } |