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 ( |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 18 | "fmt" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 19 | "strings" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 20 | |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | "android/soong/bazel" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 26 | type DeclarationsModule struct { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 27 | android.ModuleBase |
| 28 | android.DefaultableModuleBase |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 29 | android.BazelModuleBase |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 30 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 31 | // Properties for "aconfig_declarations" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 32 | properties struct { |
| 33 | // aconfig files, relative to this Android.bp file |
| 34 | Srcs []string `android:"path"` |
| 35 | |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 36 | // Release config flag package |
| 37 | Package string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 38 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 39 | // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 40 | Values []string `blueprint:"mutated"` |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 41 | |
| 42 | // Container(system/vendor/apex) that this module belongs to |
| 43 | Container string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | intermediatePath android.WritablePath |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 49 | func DeclarationsFactory() android.Module { |
| 50 | module := &DeclarationsModule{} |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 51 | |
| 52 | android.InitAndroidModule(module) |
| 53 | android.InitDefaultableModule(module) |
| 54 | module.AddProperties(&module.properties) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 55 | android.InitBazelModule(module) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 56 | |
| 57 | return module |
| 58 | } |
| 59 | |
| 60 | type implicitValuesTagType struct { |
| 61 | blueprint.BaseDependencyTag |
| 62 | } |
| 63 | |
| 64 | var implicitValuesTag = implicitValuesTagType{} |
| 65 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 66 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 67 | // Validate Properties |
| 68 | if len(module.properties.Srcs) == 0 { |
| 69 | ctx.PropertyErrorf("srcs", "missing source files") |
| 70 | return |
| 71 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 72 | if len(module.properties.Package) == 0 { |
| 73 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 74 | } |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 75 | // TODO(b/311155208): Add mandatory check for container after all pre-existing |
| 76 | // ones are changed. |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 77 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 78 | // Add a dependency on the aconfig_value_sets defined in |
| 79 | // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 80 | // match our package. |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 81 | valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 82 | if len(valuesFromConfig) > 0 { |
| 83 | ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 84 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 87 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 88 | switch tag { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 89 | case "": |
| 90 | // The default output of this module is the intermediates format, which is |
| 91 | // not installable and in a private format that no other rules can handle |
| 92 | // correctly. |
| 93 | return []android.Path{module.intermediatePath}, nil |
| 94 | default: |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 95 | return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | func joinAndPrefix(prefix string, values []string) string { |
| 100 | var sb strings.Builder |
| 101 | for _, v := range values { |
| 102 | sb.WriteString(prefix) |
| 103 | sb.WriteString(v) |
| 104 | } |
| 105 | return sb.String() |
| 106 | } |
| 107 | |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 108 | func optionalVariable(prefix string, value string) string { |
| 109 | var sb strings.Builder |
| 110 | if value != "" { |
| 111 | sb.WriteString(prefix) |
| 112 | sb.WriteString(value) |
| 113 | } |
| 114 | return sb.String() |
| 115 | } |
| 116 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 117 | // Provider published by aconfig_value_set |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 118 | type DeclarationsProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 119 | Package string |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 120 | Container string |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 121 | IntermediatePath android.WritablePath |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 124 | var DeclarationsProviderKey = blueprint.NewProvider(DeclarationsProviderData{}) |
| 125 | |
| 126 | // This is used to collect the aconfig declarations info on the transitive closure, |
| 127 | // the data is keyed on the container. |
| 128 | type TransitiveDeclarationsInfo struct { |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 129 | AconfigFiles map[string]android.Paths |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | var TransitiveDeclarationsInfoProvider = blueprint.NewProvider(TransitiveDeclarationsInfo{}) |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 133 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 134 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 135 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 136 | valuesFiles := make([]android.Path, 0) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 137 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 138 | if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) { |
| 139 | // Other modules get injected as dependencies too, for example the license modules |
| 140 | return |
| 141 | } |
| 142 | depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData) |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 143 | paths, ok := depData.AvailablePackages[module.properties.Package] |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 144 | if ok { |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 145 | valuesFiles = append(valuesFiles, paths...) |
| 146 | for _, path := range paths { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 147 | module.properties.Values = append(module.properties.Values, path.String()) |
| 148 | } |
| 149 | } |
| 150 | }) |
| 151 | |
| 152 | // Intermediate format |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 153 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
MÃ¥rten Kongstad | c89e924 | 2023-06-21 08:32:53 +0200 | [diff] [blame] | 154 | intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 155 | defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 156 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 157 | copy(inputFiles, declarationFiles) |
| 158 | inputFiles = append(inputFiles, valuesFiles...) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 159 | ctx.Build(pctx, android.BuildParams{ |
| 160 | Rule: aconfigRule, |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 161 | Output: intermediatePath, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 162 | Inputs: inputFiles, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 163 | Description: "aconfig_declarations", |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 164 | Args: map[string]string{ |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 165 | "release_version": ctx.Config().ReleaseVersion(), |
| 166 | "package": module.properties.Package, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 167 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 168 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 169 | "default-permission": optionalVariable(" --default-permission ", defaultPermission), |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 170 | }, |
| 171 | }) |
| 172 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 173 | ctx.SetProvider(DeclarationsProviderKey, DeclarationsProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 174 | Package: module.properties.Package, |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 175 | Container: module.properties.Container, |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 176 | IntermediatePath: intermediatePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 177 | }) |
| 178 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 179 | } |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 180 | func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) { |
| 181 | if *mergedAconfigFiles == nil { |
| 182 | *mergedAconfigFiles = make(map[string]android.Paths) |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 183 | } |
| 184 | ctx.VisitDirectDeps(func(module android.Module) { |
| 185 | if dep := ctx.OtherModuleProvider(module, DeclarationsProviderKey).(DeclarationsProviderData); dep.IntermediatePath != nil { |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 186 | (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediatePath) |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 187 | return |
| 188 | } |
| 189 | if dep := ctx.OtherModuleProvider(module, TransitiveDeclarationsInfoProvider).(TransitiveDeclarationsInfo); len(dep.AconfigFiles) > 0 { |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 190 | for container, v := range dep.AconfigFiles { |
| 191 | (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...) |
| 192 | } |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 193 | } |
| 194 | }) |
| 195 | |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 196 | for container, aconfigFiles := range *mergedAconfigFiles { |
| 197 | (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles) |
| 198 | } |
| 199 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 200 | ctx.SetProvider(TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{ |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 201 | AconfigFiles: *mergedAconfigFiles, |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 202 | }) |
| 203 | } |
| 204 | |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 205 | func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths { |
| 206 | if len(inputs) == 1 { |
| 207 | return android.Paths{inputs[0]} |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 208 | } |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 209 | |
| 210 | output := android.PathForModuleOut(ctx, "aconfig_merged.pb") |
| 211 | |
| 212 | ctx.Build(pctx, android.BuildParams{ |
| 213 | Rule: mergeAconfigFilesRule, |
| 214 | Description: "merge aconfig files", |
| 215 | Inputs: inputs, |
| 216 | Output: output, |
| 217 | Args: map[string]string{ |
| 218 | "flags": android.JoinWithPrefix(inputs.Strings(), "--cache "), |
| 219 | }, |
| 220 | }) |
| 221 | |
| 222 | return android.Paths{output} |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 225 | type bazelAconfigDeclarationsAttributes struct { |
| 226 | Srcs bazel.LabelListAttribute |
| 227 | Package string |
| 228 | } |
| 229 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 230 | func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 231 | if ctx.ModuleType() != "aconfig_declarations" { |
| 232 | return |
| 233 | } |
| 234 | srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) |
| 235 | |
| 236 | attrs := bazelAconfigDeclarationsAttributes{ |
| 237 | Srcs: srcs, |
| 238 | Package: module.properties.Package, |
| 239 | } |
| 240 | props := bazel.BazelTargetModuleProperties{ |
| 241 | Rule_class: "aconfig_declarations", |
| 242 | Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl", |
| 243 | } |
| 244 | |
| 245 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) |
| 246 | } |