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 ( |
| 18 | "android/soong/android" |
| 19 | "fmt" |
| 20 | "github.com/google/blueprint" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 24 | type DeclarationsModule struct { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 25 | android.ModuleBase |
| 26 | android.DefaultableModuleBase |
| 27 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 28 | // Properties for "aconfig_declarations" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 29 | properties struct { |
| 30 | // aconfig files, relative to this Android.bp file |
| 31 | Srcs []string `android:"path"` |
| 32 | |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 33 | // Release config flag package |
| 34 | Package string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 35 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 36 | // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 37 | Values []string `blueprint:"mutated"` |
| 38 | } |
| 39 | |
| 40 | intermediatePath android.WritablePath |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 43 | func DeclarationsFactory() android.Module { |
| 44 | module := &DeclarationsModule{} |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 45 | |
| 46 | android.InitAndroidModule(module) |
| 47 | android.InitDefaultableModule(module) |
| 48 | module.AddProperties(&module.properties) |
| 49 | // TODO: bp2build |
| 50 | //android.InitBazelModule(module) |
| 51 | |
| 52 | return module |
| 53 | } |
| 54 | |
| 55 | type implicitValuesTagType struct { |
| 56 | blueprint.BaseDependencyTag |
| 57 | } |
| 58 | |
| 59 | var implicitValuesTag = implicitValuesTagType{} |
| 60 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 61 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 62 | // Validate Properties |
| 63 | if len(module.properties.Srcs) == 0 { |
| 64 | ctx.PropertyErrorf("srcs", "missing source files") |
| 65 | return |
| 66 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 67 | if len(module.properties.Package) == 0 { |
| 68 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 71 | // Add a dependency on the aconfig_value_sets defined in |
| 72 | // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 73 | // match our package. |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 74 | valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 75 | ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) |
| 76 | } |
| 77 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 78 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 79 | switch tag { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 80 | case "": |
| 81 | // The default output of this module is the intermediates format, which is |
| 82 | // not installable and in a private format that no other rules can handle |
| 83 | // correctly. |
| 84 | return []android.Path{module.intermediatePath}, nil |
| 85 | default: |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 86 | return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | func joinAndPrefix(prefix string, values []string) string { |
| 91 | var sb strings.Builder |
| 92 | for _, v := range values { |
| 93 | sb.WriteString(prefix) |
| 94 | sb.WriteString(v) |
| 95 | } |
| 96 | return sb.String() |
| 97 | } |
| 98 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 99 | // Provider published by aconfig_value_set |
| 100 | type declarationsProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 101 | Package string |
| 102 | IntermediatePath android.WritablePath |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 105 | var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{}) |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 106 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 107 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 108 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 109 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 110 | if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) { |
| 111 | // Other modules get injected as dependencies too, for example the license modules |
| 112 | return |
| 113 | } |
| 114 | depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData) |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 115 | valuesFiles, ok := depData.AvailablePackages[module.properties.Package] |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 116 | if ok { |
| 117 | for _, path := range valuesFiles { |
| 118 | module.properties.Values = append(module.properties.Values, path.String()) |
| 119 | } |
| 120 | } |
| 121 | }) |
| 122 | |
| 123 | // Intermediate format |
| 124 | inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
MÃ¥rten Kongstad | c89e924 | 2023-06-21 08:32:53 +0200 | [diff] [blame] | 125 | intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 126 | ctx.Build(pctx, android.BuildParams{ |
| 127 | Rule: aconfigRule, |
| 128 | Inputs: inputFiles, |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 129 | Output: intermediatePath, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 130 | Description: "aconfig_declarations", |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 131 | Args: map[string]string{ |
| 132 | "release_version": ctx.Config().ReleaseVersion(), |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 133 | "package": module.properties.Package, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 134 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 135 | }, |
| 136 | }) |
| 137 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame^] | 138 | ctx.SetProvider(declarationsProviderKey, declarationsProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 139 | Package: module.properties.Package, |
| 140 | IntermediatePath: intermediatePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 141 | }) |
| 142 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 143 | } |