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 15package aconfig 16 17import ( 18 "android/soong/android" 19 "path/filepath" 20 "slices" 21 "strconv" 22 "strings" 23 24 "github.com/google/blueprint" 25) 26 27type AconfigReleaseConfigValue struct { 28 ReleaseConfig string 29 Values []string `blueprint:"mutated"` 30} 31 32type DeclarationsModule struct { 33 android.ModuleBase 34 android.DefaultableModuleBase 35 blueprint.IncrementalModule 36 37 // Properties for "aconfig_declarations" 38 properties struct { 39 // aconfig files, relative to this Android.bp file 40 Srcs []string `android:"path"` 41 42 // Release config flag package 43 Package string 44 45 // 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 49 50 // Container(system/vendor/apex) that this module belongs to 51 Container string 52 53 // The flags will only be repackaged if this prop is true. 54 Exportable bool 55 } 56} 57 58func DeclarationsFactory() android.Module { 59 module := &DeclarationsModule{} 60 61 android.InitAndroidModule(module) 62 android.InitDefaultableModule(module) 63 module.AddProperties(&module.properties) 64 65 return module 66} 67 68type implicitValuesTagType struct { 69 blueprint.BaseDependencyTag 70 71 // The release config name for these values. 72 // Empty string for the actual current release config. 73 ReleaseConfig string 74} 75 76var implicitValuesTag = implicitValuesTagType{} 77 78func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { 79 // Validate Properties 80 if len(module.properties.Srcs) == 0 { 81 ctx.PropertyErrorf("srcs", "missing source files") 82 return 83 } 84 if len(module.properties.Package) == 0 { 85 ctx.PropertyErrorf("package", "missing package property") 86 } 87 if len(module.properties.Container) == 0 { 88 ctx.PropertyErrorf("container", "missing container property") 89 } 90 91 // treating system_ext as system partition as we are combining them as one container 92 // TODO remove this logic once we start enforcing that system_ext cannot be specified as 93 // container in the container field. 94 if module.properties.Container == "system_ext" { 95 module.properties.Container = "system" 96 } 97 98 // Add a dependency on the aconfig_value_sets defined in 99 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that 100 // match our package. 101 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() 102 if len(valuesFromConfig) > 0 { 103 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) 104 } 105 for rcName, valueSets := range ctx.Config().ReleaseAconfigExtraReleaseConfigsValueSets() { 106 if len(valueSets) > 0 { 107 ctx.AddDependency(ctx.Module(), implicitValuesTagType{ReleaseConfig: rcName}, valueSets...) 108 } 109 } 110} 111 112func joinAndPrefix(prefix string, values []string) string { 113 var sb strings.Builder 114 for _, v := range values { 115 sb.WriteString(prefix) 116 sb.WriteString(v) 117 } 118 return sb.String() 119} 120 121func optionalVariable(prefix string, value string) string { 122 var sb strings.Builder 123 if value != "" { 124 sb.WriteString(prefix) 125 sb.WriteString(value) 126 } 127 return sb.String() 128} 129 130// Assemble the actual filename. 131// If `rcName` is not empty, then insert "-{rcName}" into the path before the 132// file extension. 133func assembleFileName(rcName, path string) string { 134 if rcName == "" { 135 return path 136 } 137 dir, file := filepath.Split(path) 138 rcName = "-" + rcName 139 ext := filepath.Ext(file) 140 base := file[:len(file)-len(ext)] 141 return dir + base + rcName + ext 142} 143 144func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { 145 // Determine which release configs we are processing. 146 // 147 // We always process the current release config (empty string). 148 // We may have been told to also create artifacts for some others. 149 configs := append([]string{""}, ctx.Config().ReleaseAconfigExtraReleaseConfigs()...) 150 slices.Sort(configs) 151 152 values := make(map[string][]string) 153 valuesFiles := make(map[string][]android.Path, 0) 154 providerData := android.AconfigReleaseDeclarationsProviderData{} 155 ctx.VisitDirectDeps(func(dep android.Module) { 156 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok { 157 depTag := ctx.OtherModuleDependencyTag(dep) 158 for _, config := range configs { 159 tag := implicitValuesTagType{ReleaseConfig: config} 160 if depTag == tag { 161 paths, ok := depData.AvailablePackages[module.properties.Package] 162 if ok { 163 valuesFiles[config] = append(valuesFiles[config], paths...) 164 for _, path := range paths { 165 values[config] = append(values[config], path.String()) 166 } 167 } 168 } 169 } 170 } 171 }) 172 for _, config := range configs { 173 module.properties.ReleaseConfigValues = append(module.properties.ReleaseConfigValues, AconfigReleaseConfigValue{ 174 ReleaseConfig: config, 175 Values: values[config], 176 }) 177 178 // Intermediate format 179 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) 180 intermediateCacheFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.pb")) 181 var defaultPermission string 182 defaultPermission = ctx.Config().ReleaseAconfigFlagDefaultPermission() 183 if config != "" { 184 if confPerm, ok := ctx.Config().GetBuildFlag("RELEASE_ACONFIG_FLAG_DEFAULT_PERMISSION_" + config); ok { 185 defaultPermission = confPerm 186 } 187 } 188 var allowReadWrite bool 189 if requireAllReadOnly, ok := ctx.Config().GetBuildFlag("RELEASE_ACONFIG_REQUIRE_ALL_READ_ONLY"); ok { 190 // The build flag (RELEASE_ACONFIG_REQUIRE_ALL_READ_ONLY) is the negation of the aconfig flag 191 // (allow-read-write) for historical reasons. 192 // Bool build flags are always "" for false, and generally "true" for true. 193 allowReadWrite = requireAllReadOnly == "" 194 } 195 inputFiles := make([]android.Path, len(declarationFiles)) 196 copy(inputFiles, declarationFiles) 197 inputFiles = append(inputFiles, valuesFiles[config]...) 198 args := map[string]string{ 199 "release_version": ctx.Config().ReleaseVersion(), 200 "package": module.properties.Package, 201 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), 202 "values": joinAndPrefix(" --values ", values[config]), 203 "default-permission": optionalVariable(" --default-permission ", defaultPermission), 204 "allow-read-write": optionalVariable(" --allow-read-write ", strconv.FormatBool(allowReadWrite)), 205 } 206 if len(module.properties.Container) > 0 { 207 args["container"] = "--container " + module.properties.Container 208 } 209 ctx.Build(pctx, android.BuildParams{ 210 Rule: aconfigRule, 211 Output: intermediateCacheFilePath, 212 Inputs: inputFiles, 213 Description: "aconfig_declarations", 214 Args: args, 215 }) 216 217 intermediateDumpFilePath := android.PathForModuleOut(ctx, assembleFileName(config, "intermediate.txt")) 218 ctx.Build(pctx, android.BuildParams{ 219 Rule: aconfigTextRule, 220 Output: intermediateDumpFilePath, 221 Inputs: android.Paths{intermediateCacheFilePath}, 222 Description: "aconfig_text", 223 }) 224 225 providerData[config] = android.AconfigDeclarationsProviderData{ 226 Package: module.properties.Package, 227 Container: module.properties.Container, 228 Exportable: module.properties.Exportable, 229 IntermediateCacheOutputPath: intermediateCacheFilePath, 230 IntermediateDumpOutputPath: intermediateDumpFilePath, 231 } 232 } 233 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, providerData[""]) 234 android.SetProvider(ctx, android.AconfigReleaseDeclarationsProviderKey, providerData) 235} 236 237var _ blueprint.Incremental = &DeclarationsModule{} 238