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" |
Jihoon Kang | 4fbee9a | 2024-09-12 22:28:32 +0000 | [diff] [blame] | 19 | "fmt" |
| 20 | "strings" |
| 21 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 25 | // Properties for "aconfig_value_set" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 26 | type ValueSetModule struct { |
| 27 | android.ModuleBase |
| 28 | android.DefaultableModuleBase |
| 29 | |
| 30 | properties struct { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 31 | // aconfig_values modules |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 32 | Values []string |
Jihoon Kang | 4fbee9a | 2024-09-12 22:28:32 +0000 | [diff] [blame] | 33 | |
| 34 | // Paths to the Android.bp files where the aconfig_values modules are defined. |
| 35 | Srcs []string |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | func ValueSetFactory() android.Module { |
| 40 | module := &ValueSetModule{} |
| 41 | |
| 42 | android.InitAndroidModule(module) |
| 43 | android.InitDefaultableModule(module) |
| 44 | module.AddProperties(&module.properties) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 45 | |
| 46 | return module |
| 47 | } |
| 48 | |
| 49 | // Dependency tag for values property |
| 50 | type valueSetType struct { |
| 51 | blueprint.BaseDependencyTag |
| 52 | } |
| 53 | |
| 54 | var valueSetTag = valueSetType{} |
| 55 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 56 | // Provider published by aconfig_value_set |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 57 | type valueSetProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 58 | // The package of each of the |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 59 | // (map of package --> aconfig_module) |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 60 | AvailablePackages map[string]android.Paths |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 63 | var valueSetProviderKey = blueprint.NewProvider[valueSetProviderData]() |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 64 | |
Jihoon Kang | 4fbee9a | 2024-09-12 22:28:32 +0000 | [diff] [blame] | 65 | func (module *ValueSetModule) FindAconfigValuesFromSrc(ctx android.BottomUpMutatorContext) map[string]android.Path { |
| 66 | moduleDir := ctx.ModuleDir() |
| 67 | srcs := android.PathsForModuleSrcExcludes(ctx, module.properties.Srcs, []string{ctx.BlueprintsFile()}) |
| 68 | |
| 69 | aconfigValuesPrefix := strings.Replace(module.Name(), "aconfig_value_set", "aconfig-values", 1) |
| 70 | moduleNamesSrcMap := make(map[string]android.Path) |
| 71 | for _, src := range srcs { |
| 72 | subDir := strings.TrimPrefix(src.String(), moduleDir+"/") |
| 73 | packageName, _, found := strings.Cut(subDir, "/") |
| 74 | if found { |
| 75 | moduleName := fmt.Sprintf("%s-%s-all", aconfigValuesPrefix, packageName) |
| 76 | moduleNamesSrcMap[moduleName] = src |
| 77 | } |
| 78 | } |
| 79 | return moduleNamesSrcMap |
| 80 | } |
| 81 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 82 | func (module *ValueSetModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jihoon Kang | 4fbee9a | 2024-09-12 22:28:32 +0000 | [diff] [blame] | 83 | |
| 84 | // TODO: b/366285733 - Replace the file path based solution with more robust solution. |
| 85 | aconfigValuesMap := module.FindAconfigValuesFromSrc(ctx) |
| 86 | for _, moduleName := range android.SortedKeys(aconfigValuesMap) { |
| 87 | if ctx.OtherModuleExists(moduleName) { |
| 88 | ctx.AddDependency(ctx.Module(), valueSetTag, moduleName) |
| 89 | } else { |
| 90 | ctx.ModuleErrorf("module %q not found. Rename the aconfig_values module defined in %q to %q", moduleName, aconfigValuesMap[moduleName], moduleName) |
| 91 | } |
| 92 | } |
| 93 | |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 94 | deps := ctx.AddDependency(ctx.Module(), valueSetTag, module.properties.Values...) |
| 95 | for _, dep := range deps { |
| 96 | _, ok := dep.(*ValuesModule) |
| 97 | if !ok { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 98 | ctx.PropertyErrorf("values", "values must be a aconfig_values module") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 99 | return |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 105 | // Accumulate the packages of the values modules listed, and set that as an |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 106 | // valueSetProviderKey provider that aconfig modules can read and use |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 107 | // to append values to their aconfig actions. |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 108 | packages := make(map[string]android.Paths) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 109 | ctx.VisitDirectDeps(func(dep android.Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 110 | if depData, ok := android.OtherModuleProvider(ctx, dep, valuesProviderKey); ok { |
| 111 | srcs := make([]android.Path, len(depData.Values)) |
| 112 | copy(srcs, depData.Values) |
| 113 | packages[depData.Package] = srcs |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 114 | } |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 115 | |
| 116 | }) |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 117 | android.SetProvider(ctx, valueSetProviderKey, valueSetProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 118 | AvailablePackages: packages, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 119 | }) |
| 120 | } |