blob: af9ddd3dc55f88baeb63b157b0a3d9ae270ad04d [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"
Yu Liu2cc802a2023-09-05 17:19:45 -070019 "android/soong/bazel"
Joe Onoratofee845a2023-05-09 08:14:14 -070020 "github.com/google/blueprint"
21)
22
Joe Onorato981c9262023-06-21 15:16:23 -070023// Properties for "aconfig_value_set"
Joe Onoratofee845a2023-05-09 08:14:14 -070024type ValueSetModule struct {
25 android.ModuleBase
26 android.DefaultableModuleBase
Yu Liu2cc802a2023-09-05 17:19:45 -070027 android.BazelModuleBase
Joe Onoratofee845a2023-05-09 08:14:14 -070028
29 properties struct {
Joe Onorato981c9262023-06-21 15:16:23 -070030 // aconfig_values modules
Joe Onoratofee845a2023-05-09 08:14:14 -070031 Values []string
32 }
33}
34
35func ValueSetFactory() android.Module {
36 module := &ValueSetModule{}
37
38 android.InitAndroidModule(module)
39 android.InitDefaultableModule(module)
40 module.AddProperties(&module.properties)
Yu Liu2cc802a2023-09-05 17:19:45 -070041 android.InitBazelModule(module)
Joe Onoratofee845a2023-05-09 08:14:14 -070042
43 return module
44}
45
46// Dependency tag for values property
47type valueSetType struct {
48 blueprint.BaseDependencyTag
49}
50
51var valueSetTag = valueSetType{}
52
Joe Onorato981c9262023-06-21 15:16:23 -070053// Provider published by aconfig_value_set
Joe Onoratofee845a2023-05-09 08:14:14 -070054type valueSetProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -070055 // The package of each of the
Joe Onorato981c9262023-06-21 15:16:23 -070056 // (map of package --> aconfig_module)
Joe Onorato81b25ed2023-06-21 13:49:37 -070057 AvailablePackages map[string]android.Paths
Joe Onoratofee845a2023-05-09 08:14:14 -070058}
59
60var valueSetProviderKey = blueprint.NewProvider(valueSetProviderData{})
61
62func (module *ValueSetModule) DepsMutator(ctx android.BottomUpMutatorContext) {
63 deps := ctx.AddDependency(ctx.Module(), valueSetTag, module.properties.Values...)
64 for _, dep := range deps {
65 _, ok := dep.(*ValuesModule)
66 if !ok {
Joe Onorato981c9262023-06-21 15:16:23 -070067 ctx.PropertyErrorf("values", "values must be a aconfig_values module")
Joe Onoratofee845a2023-05-09 08:14:14 -070068 return
69 }
70 }
71}
72
73func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Joe Onorato81b25ed2023-06-21 13:49:37 -070074 // Accumulate the packages of the values modules listed, and set that as an
Joe Onorato981c9262023-06-21 15:16:23 -070075 // valueSetProviderKey provider that aconfig modules can read and use
Joe Onoratofee845a2023-05-09 08:14:14 -070076 // to append values to their aconfig actions.
Joe Onorato81b25ed2023-06-21 13:49:37 -070077 packages := make(map[string]android.Paths)
Joe Onoratofee845a2023-05-09 08:14:14 -070078 ctx.VisitDirectDeps(func(dep android.Module) {
79 if !ctx.OtherModuleHasProvider(dep, valuesProviderKey) {
80 // Other modules get injected as dependencies too, for example the license modules
81 return
82 }
83 depData := ctx.OtherModuleProvider(dep, valuesProviderKey).(valuesProviderData)
84
85 srcs := make([]android.Path, len(depData.Values))
86 copy(srcs, depData.Values)
Joe Onorato81b25ed2023-06-21 13:49:37 -070087 packages[depData.Package] = srcs
Joe Onoratofee845a2023-05-09 08:14:14 -070088
89 })
90 ctx.SetProvider(valueSetProviderKey, valueSetProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -070091 AvailablePackages: packages,
Joe Onoratofee845a2023-05-09 08:14:14 -070092 })
93}
Yu Liu2cc802a2023-09-05 17:19:45 -070094
95type bazelAconfigValueSetAttributes struct {
96 Values bazel.LabelListAttribute
97}
98
99func (module *ValueSetModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
100 if ctx.ModuleType() != "aconfig_value_set" {
101 return
102 }
103
104 attrs := bazelAconfigValueSetAttributes{
105 Values: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, module.properties.Values)),
106 }
107 props := bazel.BazelTargetModuleProperties{
108 Rule_class: "aconfig_value_set",
109 Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_value_set.bzl",
110 }
111
112 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
113}