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 | |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 100 | func optionalVariable(prefix string, value string) string { |
| 101 | var sb strings.Builder |
| 102 | if value != "" { |
| 103 | sb.WriteString(prefix) |
| 104 | sb.WriteString(value) |
| 105 | } |
| 106 | return sb.String() |
| 107 | } |
| 108 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 109 | // Provider published by aconfig_value_set |
| 110 | type declarationsProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 111 | Package string |
| 112 | IntermediatePath android.WritablePath |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 115 | var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{}) |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 116 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 117 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 118 | // 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] | 119 | valuesFiles := make([]android.Path, 0) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 120 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 121 | if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) { |
| 122 | // Other modules get injected as dependencies too, for example the license modules |
| 123 | return |
| 124 | } |
| 125 | depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData) |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 126 | paths, ok := depData.AvailablePackages[module.properties.Package] |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 127 | if ok { |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 128 | valuesFiles = append(valuesFiles, paths...) |
| 129 | for _, path := range paths { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 130 | module.properties.Values = append(module.properties.Values, path.String()) |
| 131 | } |
| 132 | } |
| 133 | }) |
| 134 | |
| 135 | // Intermediate format |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 136 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
MÃ¥rten Kongstad | c89e924 | 2023-06-21 08:32:53 +0200 | [diff] [blame] | 137 | intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 138 | defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 139 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 140 | copy(inputFiles, declarationFiles) |
| 141 | inputFiles = append(inputFiles, valuesFiles...) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 142 | ctx.Build(pctx, android.BuildParams{ |
| 143 | Rule: aconfigRule, |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 144 | Output: intermediatePath, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 145 | Inputs: inputFiles, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 146 | Description: "aconfig_declarations", |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 147 | Args: map[string]string{ |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 148 | "release_version": ctx.Config().ReleaseVersion(), |
| 149 | "package": module.properties.Package, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 150 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 151 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 152 | "default-permission": optionalVariable(" --default-permission ", defaultPermission), |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 153 | }, |
| 154 | }) |
| 155 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 156 | ctx.SetProvider(declarationsProviderKey, declarationsProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 157 | Package: module.properties.Package, |
| 158 | IntermediatePath: intermediatePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 159 | }) |
| 160 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 161 | } |