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" |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 22 | |
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 |
| 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 | } |
| 47 | |
| 48 | intermediatePath android.WritablePath |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 51 | func DeclarationsFactory() android.Module { |
| 52 | module := &DeclarationsModule{} |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 53 | |
| 54 | android.InitAndroidModule(module) |
| 55 | android.InitDefaultableModule(module) |
| 56 | module.AddProperties(&module.properties) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 57 | |
| 58 | return module |
| 59 | } |
| 60 | |
| 61 | type implicitValuesTagType struct { |
| 62 | blueprint.BaseDependencyTag |
| 63 | } |
| 64 | |
| 65 | var implicitValuesTag = implicitValuesTagType{} |
| 66 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 67 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 68 | // Validate Properties |
| 69 | if len(module.properties.Srcs) == 0 { |
| 70 | ctx.PropertyErrorf("srcs", "missing source files") |
| 71 | return |
| 72 | } |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 73 | if len(module.properties.Package) == 0 { |
| 74 | ctx.PropertyErrorf("package", "missing package property") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 75 | } |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 76 | if len(module.properties.Container) == 0 { |
| 77 | ctx.PropertyErrorf("container", "missing container property") |
| 78 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 79 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 80 | // Add a dependency on the aconfig_value_sets defined in |
| 81 | // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 82 | // match our package. |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 83 | valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() |
Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 84 | if len(valuesFromConfig) > 0 { |
| 85 | ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 86 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 89 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 90 | switch tag { |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 91 | case "": |
| 92 | // The default output of this module is the intermediates format, which is |
| 93 | // not installable and in a private format that no other rules can handle |
| 94 | // correctly. |
| 95 | return []android.Path{module.intermediatePath}, nil |
| 96 | default: |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 97 | return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | func joinAndPrefix(prefix string, values []string) string { |
| 102 | var sb strings.Builder |
| 103 | for _, v := range values { |
| 104 | sb.WriteString(prefix) |
| 105 | sb.WriteString(v) |
| 106 | } |
| 107 | return sb.String() |
| 108 | } |
| 109 | |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 110 | func optionalVariable(prefix string, value string) string { |
| 111 | var sb strings.Builder |
| 112 | if value != "" { |
| 113 | sb.WriteString(prefix) |
| 114 | sb.WriteString(value) |
| 115 | } |
| 116 | return sb.String() |
| 117 | } |
| 118 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 119 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 120 | // 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] | 121 | valuesFiles := make([]android.Path, 0) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 122 | ctx.VisitDirectDeps(func(dep android.Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 123 | if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok { |
| 124 | paths, ok := depData.AvailablePackages[module.properties.Package] |
| 125 | if ok { |
| 126 | valuesFiles = append(valuesFiles, paths...) |
| 127 | for _, path := range paths { |
| 128 | module.properties.Values = append(module.properties.Values, path.String()) |
| 129 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | }) |
| 133 | |
| 134 | // Intermediate format |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 135 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 136 | intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb") |
Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 137 | defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 138 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 139 | copy(inputFiles, declarationFiles) |
| 140 | inputFiles = append(inputFiles, valuesFiles...) |
Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 141 | args := map[string]string{ |
| 142 | "release_version": ctx.Config().ReleaseVersion(), |
| 143 | "package": module.properties.Package, |
| 144 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "), |
| 145 | "values": joinAndPrefix(" --values ", module.properties.Values), |
| 146 | "default-permission": optionalVariable(" --default-permission ", defaultPermission), |
| 147 | } |
| 148 | if len(module.properties.Container) > 0 { |
| 149 | args["container"] = "--container " + module.properties.Container |
| 150 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 151 | ctx.Build(pctx, android.BuildParams{ |
| 152 | Rule: aconfigRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 153 | Output: intermediateCacheFilePath, |
Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 154 | Inputs: inputFiles, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 155 | Description: "aconfig_declarations", |
Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 156 | Args: args, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 157 | }) |
| 158 | |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 159 | intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt") |
| 160 | ctx.Build(pctx, android.BuildParams{ |
| 161 | Rule: aconfigTextRule, |
| 162 | Output: intermediateDumpFilePath, |
| 163 | Inputs: android.Paths{intermediateCacheFilePath}, |
| 164 | Description: "aconfig_text", |
| 165 | }) |
| 166 | |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 167 | android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{ |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 168 | Package: module.properties.Package, |
| 169 | Container: module.properties.Container, |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 170 | Exportable: module.properties.Exportable, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 171 | IntermediateCacheOutputPath: intermediateCacheFilePath, |
| 172 | IntermediateDumpOutputPath: intermediateDumpFilePath, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 173 | }) |
| 174 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 175 | } |