blob: 252908fa34abc0e20102e57119ba630f8138d5bf [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"
19 "github.com/google/blueprint"
20)
21
Joe Onorato981c9262023-06-21 15:16:23 -070022// Properties for "aconfig_value_set"
Joe Onoratofee845a2023-05-09 08:14:14 -070023type ValueSetModule struct {
24 android.ModuleBase
25 android.DefaultableModuleBase
26
27 properties struct {
Joe Onorato981c9262023-06-21 15:16:23 -070028 // aconfig_values modules
Joe Onoratofee845a2023-05-09 08:14:14 -070029 Values []string
30 }
31}
32
33func ValueSetFactory() android.Module {
34 module := &ValueSetModule{}
35
36 android.InitAndroidModule(module)
37 android.InitDefaultableModule(module)
38 module.AddProperties(&module.properties)
39 // TODO: bp2build
40 //android.InitBazelModule(module)
41
42 return module
43}
44
45// Dependency tag for values property
46type valueSetType struct {
47 blueprint.BaseDependencyTag
48}
49
50var valueSetTag = valueSetType{}
51
Joe Onorato981c9262023-06-21 15:16:23 -070052// Provider published by aconfig_value_set
Joe Onoratofee845a2023-05-09 08:14:14 -070053type valueSetProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -070054 // The package of each of the
Joe Onorato981c9262023-06-21 15:16:23 -070055 // (map of package --> aconfig_module)
Joe Onorato81b25ed2023-06-21 13:49:37 -070056 AvailablePackages map[string]android.Paths
Joe Onoratofee845a2023-05-09 08:14:14 -070057}
58
59var valueSetProviderKey = blueprint.NewProvider(valueSetProviderData{})
60
61func (module *ValueSetModule) DepsMutator(ctx android.BottomUpMutatorContext) {
62 deps := ctx.AddDependency(ctx.Module(), valueSetTag, module.properties.Values...)
63 for _, dep := range deps {
64 _, ok := dep.(*ValuesModule)
65 if !ok {
Joe Onorato981c9262023-06-21 15:16:23 -070066 ctx.PropertyErrorf("values", "values must be a aconfig_values module")
Joe Onoratofee845a2023-05-09 08:14:14 -070067 return
68 }
69 }
70}
71
72func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Joe Onorato81b25ed2023-06-21 13:49:37 -070073 // Accumulate the packages of the values modules listed, and set that as an
Joe Onorato981c9262023-06-21 15:16:23 -070074 // valueSetProviderKey provider that aconfig modules can read and use
Joe Onoratofee845a2023-05-09 08:14:14 -070075 // to append values to their aconfig actions.
Joe Onorato81b25ed2023-06-21 13:49:37 -070076 packages := make(map[string]android.Paths)
Joe Onoratofee845a2023-05-09 08:14:14 -070077 ctx.VisitDirectDeps(func(dep android.Module) {
78 if !ctx.OtherModuleHasProvider(dep, valuesProviderKey) {
79 // Other modules get injected as dependencies too, for example the license modules
80 return
81 }
82 depData := ctx.OtherModuleProvider(dep, valuesProviderKey).(valuesProviderData)
83
84 srcs := make([]android.Path, len(depData.Values))
85 copy(srcs, depData.Values)
Joe Onorato81b25ed2023-06-21 13:49:37 -070086 packages[depData.Package] = srcs
Joe Onoratofee845a2023-05-09 08:14:14 -070087
88 })
89 ctx.SetProvider(valueSetProviderKey, valueSetProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -070090 AvailablePackages: packages,
Joe Onoratofee845a2023-05-09 08:14:14 -070091 })
92}