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