blob: d72ec48ff3a314322ce459ecb450c3297fabfd49 [file] [log] [blame]
Joe Onoratofee845a2023-05-09 08:14:14 -07001// 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 Onorato981c9262023-06-21 15:16:23 -070015package aconfig
Joe Onoratofee845a2023-05-09 08:14:14 -070016
17import (
18 "android/soong/android"
Jihoon Kang4fbee9a2024-09-12 22:28:32 +000019 "fmt"
20 "strings"
21
Joe Onoratofee845a2023-05-09 08:14:14 -070022 "github.com/google/blueprint"
23)
24
Joe Onorato981c9262023-06-21 15:16:23 -070025// Properties for "aconfig_value_set"
Joe Onoratofee845a2023-05-09 08:14:14 -070026type ValueSetModule struct {
27 android.ModuleBase
28 android.DefaultableModuleBase
29
30 properties struct {
Joe Onorato981c9262023-06-21 15:16:23 -070031 // aconfig_values modules
Joe Onoratofee845a2023-05-09 08:14:14 -070032 Values []string
Jihoon Kang4fbee9a2024-09-12 22:28:32 +000033
34 // Paths to the Android.bp files where the aconfig_values modules are defined.
35 Srcs []string
Joe Onoratofee845a2023-05-09 08:14:14 -070036 }
37}
38
39func ValueSetFactory() android.Module {
40 module := &ValueSetModule{}
41
42 android.InitAndroidModule(module)
43 android.InitDefaultableModule(module)
44 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070045
46 return module
47}
48
49// Dependency tag for values property
50type valueSetType struct {
51 blueprint.BaseDependencyTag
52}
53
54var valueSetTag = valueSetType{}
55
Joe Onorato981c9262023-06-21 15:16:23 -070056// Provider published by aconfig_value_set
Joe Onoratofee845a2023-05-09 08:14:14 -070057type valueSetProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -070058 // The package of each of the
Joe Onorato981c9262023-06-21 15:16:23 -070059 // (map of package --> aconfig_module)
Joe Onorato81b25ed2023-06-21 13:49:37 -070060 AvailablePackages map[string]android.Paths
Joe Onoratofee845a2023-05-09 08:14:14 -070061}
62
Colin Crossbc7d76c2023-12-12 16:39:03 -080063var valueSetProviderKey = blueprint.NewProvider[valueSetProviderData]()
Joe Onoratofee845a2023-05-09 08:14:14 -070064
Jihoon Kang4fbee9a2024-09-12 22:28:32 +000065func (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 Onoratofee845a2023-05-09 08:14:14 -070082func (module *ValueSetModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang4fbee9a2024-09-12 22:28:32 +000083
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 Onoratofee845a2023-05-09 08:14:14 -070094 deps := ctx.AddDependency(ctx.Module(), valueSetTag, module.properties.Values...)
95 for _, dep := range deps {
96 _, ok := dep.(*ValuesModule)
97 if !ok {
Joe Onorato981c9262023-06-21 15:16:23 -070098 ctx.PropertyErrorf("values", "values must be a aconfig_values module")
Joe Onoratofee845a2023-05-09 08:14:14 -070099 return
100 }
101 }
102}
103
104func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700105 // Accumulate the packages of the values modules listed, and set that as an
Joe Onorato981c9262023-06-21 15:16:23 -0700106 // valueSetProviderKey provider that aconfig modules can read and use
Joe Onoratofee845a2023-05-09 08:14:14 -0700107 // to append values to their aconfig actions.
Joe Onorato81b25ed2023-06-21 13:49:37 -0700108 packages := make(map[string]android.Paths)
Joe Onoratofee845a2023-05-09 08:14:14 -0700109 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800110 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 Onoratofee845a2023-05-09 08:14:14 -0700114 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700115
116 })
Colin Cross40213022023-12-13 15:19:49 -0800117 android.SetProvider(ctx, valueSetProviderKey, valueSetProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700118 AvailablePackages: packages,
Joe Onoratofee845a2023-05-09 08:14:14 -0700119 })
120}