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"` |
| 41 | } |
| 42 | |
| 43 | intermediatePath android.WritablePath |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 46 | func DeclarationsFactory() android.Module { |
| 47 | module := &DeclarationsModule{} |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 48 | |
| 49 | android.InitAndroidModule(module) |
| 50 | android.InitDefaultableModule(module) |
| 51 | module.AddProperties(&module.properties) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 52 | android.InitBazelModule(module) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 53 | |
| 54 | return module |
| 55 | } |
| 56 | |
| 57 | type implicitValuesTagType struct { |
| 58 | blueprint.BaseDependencyTag |
| 59 | } |
| 60 | |
| 61 | var implicitValuesTag = implicitValuesTagType{} |
| 62 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 63 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 64 | // Validate Properties |
| 65 | if len(module.properties.Srcs) == 0 { |
| 66 | ctx.PropertyErrorf("srcs", "missing source files") |
| 67 | return |
| 68 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 69 | if len(module.properties.Package) == 0 { |
| 70 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 73 | // Add a dependency on the aconfig_value_sets defined in |
| 74 | // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 75 | // match our package. |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 76 | valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 77 | if len(valuesFromConfig) > 0 { |
| 78 | ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 79 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 82 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 83 | switch tag { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 84 | case "": |
| 85 | // The default output of this module is the intermediates format, which is |
| 86 | // not installable and in a private format that no other rules can handle |
| 87 | // correctly. |
| 88 | return []android.Path{module.intermediatePath}, nil |
| 89 | default: |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 90 | return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | func joinAndPrefix(prefix string, values []string) string { |
| 95 | var sb strings.Builder |
| 96 | for _, v := range values { |
| 97 | sb.WriteString(prefix) |
| 98 | sb.WriteString(v) |
| 99 | } |
| 100 | return sb.String() |
| 101 | } |
| 102 | |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 103 | func optionalVariable(prefix string, value string) string { |
| 104 | var sb strings.Builder |
| 105 | if value != "" { |
| 106 | sb.WriteString(prefix) |
| 107 | sb.WriteString(value) |
| 108 | } |
| 109 | return sb.String() |
| 110 | } |
| 111 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 112 | // Provider published by aconfig_value_set |
| 113 | type declarationsProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 114 | Package string |
| 115 | IntermediatePath android.WritablePath |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 118 | var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{}) |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 119 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 120 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 121 | // 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] | 122 | valuesFiles := make([]android.Path, 0) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 123 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 124 | if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) { |
| 125 | // Other modules get injected as dependencies too, for example the license modules |
| 126 | return |
| 127 | } |
| 128 | depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData) |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 129 | paths, ok := depData.AvailablePackages[module.properties.Package] |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 130 | if ok { |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 131 | valuesFiles = append(valuesFiles, paths...) |
| 132 | for _, path := range paths { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 133 | module.properties.Values = append(module.properties.Values, path.String()) |
| 134 | } |
| 135 | } |
| 136 | }) |
| 137 | |
| 138 | // Intermediate format |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 139 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
MÃ¥rten Kongstad | c89e924 | 2023-06-21 08:32:53 +0200 | [diff] [blame] | 140 | intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 141 | defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 142 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 143 | copy(inputFiles, declarationFiles) |
| 144 | inputFiles = append(inputFiles, valuesFiles...) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 145 | ctx.Build(pctx, android.BuildParams{ |
| 146 | Rule: aconfigRule, |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 147 | Output: intermediatePath, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 148 | Inputs: inputFiles, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 149 | Description: "aconfig_declarations", |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 150 | Args: map[string]string{ |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 151 | "release_version": ctx.Config().ReleaseVersion(), |
| 152 | "package": module.properties.Package, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 153 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 154 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 155 | "default-permission": optionalVariable(" --default-permission ", defaultPermission), |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 156 | }, |
| 157 | }) |
| 158 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 159 | ctx.SetProvider(declarationsProviderKey, declarationsProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 160 | Package: module.properties.Package, |
| 161 | IntermediatePath: intermediatePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 162 | }) |
| 163 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 164 | } |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 165 | |
| 166 | type bazelAconfigDeclarationsAttributes struct { |
| 167 | Srcs bazel.LabelListAttribute |
| 168 | Package string |
| 169 | } |
| 170 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 171 | func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 172 | if ctx.ModuleType() != "aconfig_declarations" { |
| 173 | return |
| 174 | } |
| 175 | srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) |
| 176 | |
| 177 | attrs := bazelAconfigDeclarationsAttributes{ |
| 178 | Srcs: srcs, |
| 179 | Package: module.properties.Package, |
| 180 | } |
| 181 | props := bazel.BazelTargetModuleProperties{ |
| 182 | Rule_class: "aconfig_declarations", |
| 183 | Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl", |
| 184 | } |
| 185 | |
| 186 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) |
| 187 | } |