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 | "strings" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 19 | |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 21 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 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 |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 28 | blueprint.IncrementalModule |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 29 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 30 | // Properties for "aconfig_declarations" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 31 | properties struct { |
| 32 | // aconfig files, relative to this Android.bp file |
| 33 | Srcs []string `android:"path"` |
| 34 | |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 35 | // Release config flag package |
| 36 | Package string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 37 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 38 | // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 39 | Values []string `blueprint:"mutated"` |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 40 | |
| 41 | // Container(system/vendor/apex) that this module belongs to |
| 42 | Container string |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 43 | |
| 44 | // The flags will only be repackaged if this prop is true. |
| 45 | Exportable bool |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 46 | } |
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) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 55 | |
| 56 | return module |
| 57 | } |
| 58 | |
| 59 | type implicitValuesTagType struct { |
| 60 | blueprint.BaseDependencyTag |
| 61 | } |
| 62 | |
| 63 | var implicitValuesTag = implicitValuesTagType{} |
| 64 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 65 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 66 | // Validate Properties |
| 67 | if len(module.properties.Srcs) == 0 { |
| 68 | ctx.PropertyErrorf("srcs", "missing source files") |
| 69 | return |
| 70 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 71 | if len(module.properties.Package) == 0 { |
| 72 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 73 | } |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 74 | if len(module.properties.Container) == 0 { |
| 75 | ctx.PropertyErrorf("container", "missing container property") |
| 76 | } |
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 | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 87 | func joinAndPrefix(prefix string, values []string) string { |
| 88 | var sb strings.Builder |
| 89 | for _, v := range values { |
| 90 | sb.WriteString(prefix) |
| 91 | sb.WriteString(v) |
| 92 | } |
| 93 | return sb.String() |
| 94 | } |
| 95 | |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 96 | func optionalVariable(prefix string, value string) string { |
| 97 | var sb strings.Builder |
| 98 | if value != "" { |
| 99 | sb.WriteString(prefix) |
| 100 | sb.WriteString(value) |
| 101 | } |
| 102 | return sb.String() |
| 103 | } |
| 104 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 105 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 106 | // 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] | 107 | valuesFiles := make([]android.Path, 0) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 108 | ctx.VisitDirectDeps(func(dep android.Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 109 | if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok { |
| 110 | paths, ok := depData.AvailablePackages[module.properties.Package] |
| 111 | if ok { |
| 112 | valuesFiles = append(valuesFiles, paths...) |
| 113 | for _, path := range paths { |
| 114 | module.properties.Values = append(module.properties.Values, path.String()) |
| 115 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | }) |
| 119 | |
| 120 | // Intermediate format |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 121 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 122 | intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 123 | defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 124 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 125 | copy(inputFiles, declarationFiles) |
| 126 | inputFiles = append(inputFiles, valuesFiles...) |
Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 127 | args := map[string]string{ |
| 128 | "release_version": ctx.Config().ReleaseVersion(), |
| 129 | "package": module.properties.Package, |
| 130 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), |
| 131 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 132 | "default-permission": optionalVariable(" --default-permission ", defaultPermission), |
| 133 | } |
| 134 | if len(module.properties.Container) > 0 { |
| 135 | args["container"] = "--container " + module.properties.Container |
| 136 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 137 | ctx.Build(pctx, android.BuildParams{ |
| 138 | Rule: aconfigRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 139 | Output: intermediateCacheFilePath, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 140 | Inputs: inputFiles, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 141 | Description: "aconfig_declarations", |
Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 142 | Args: args, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 143 | }) |
| 144 | |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 145 | intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt") |
| 146 | ctx.Build(pctx, android.BuildParams{ |
| 147 | Rule: aconfigTextRule, |
| 148 | Output: intermediateDumpFilePath, |
| 149 | Inputs: android.Paths{intermediateCacheFilePath}, |
| 150 | Description: "aconfig_text", |
| 151 | }) |
| 152 | |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 153 | android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{ |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 154 | Package: module.properties.Package, |
| 155 | Container: module.properties.Container, |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 156 | Exportable: module.properties.Exportable, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 157 | IntermediateCacheOutputPath: intermediateCacheFilePath, |
| 158 | IntermediateDumpOutputPath: intermediateDumpFilePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 159 | }) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 160 | } |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 161 | |
| 162 | func (module *DeclarationsModule) BuildActionProviderKeys() []blueprint.AnyProviderKey { |
| 163 | return []blueprint.AnyProviderKey{android.AconfigDeclarationsProviderKey} |
| 164 | } |
| 165 | |
| 166 | func (module *DeclarationsModule) PackageContextPath() string { |
| 167 | return pkgPath |
| 168 | } |
| 169 | |
| 170 | func (module *DeclarationsModule) CachedRules() []blueprint.Rule { |
| 171 | return []blueprint.Rule{aconfigRule, aconfigTextRule} |
| 172 | } |
| 173 | |
| 174 | var _ blueprint.Incremental = &DeclarationsModule{} |