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" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 19 | "android/soong/bazel" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | ) |
| 22 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 23 | // Properties for "aconfig_value_set" |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 24 | type ValueSetModule struct { |
| 25 | android.ModuleBase |
| 26 | android.DefaultableModuleBase |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 27 | android.BazelModuleBase |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 28 | |
| 29 | properties struct { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 30 | // aconfig_values modules |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 31 | Values []string |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func ValueSetFactory() android.Module { |
| 36 | module := &ValueSetModule{} |
| 37 | |
| 38 | android.InitAndroidModule(module) |
| 39 | android.InitDefaultableModule(module) |
| 40 | module.AddProperties(&module.properties) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 41 | android.InitBazelModule(module) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 42 | |
| 43 | return module |
| 44 | } |
| 45 | |
| 46 | // Dependency tag for values property |
| 47 | type valueSetType struct { |
| 48 | blueprint.BaseDependencyTag |
| 49 | } |
| 50 | |
| 51 | var valueSetTag = valueSetType{} |
| 52 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 53 | // Provider published by aconfig_value_set |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 54 | type valueSetProviderData struct { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 55 | // The package of each of the |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 56 | // (map of package --> aconfig_module) |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 57 | AvailablePackages map[string]android.Paths |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | var valueSetProviderKey = blueprint.NewProvider(valueSetProviderData{}) |
| 61 | |
| 62 | func (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 Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 67 | ctx.PropertyErrorf("values", "values must be a aconfig_values module") |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 68 | return |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 74 | // 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] | 75 | // valueSetProviderKey provider that aconfig modules can read and use |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 76 | // to append values to their aconfig actions. |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 77 | packages := make(map[string]android.Paths) |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 78 | 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 Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 87 | packages[depData.Package] = srcs |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 88 | |
| 89 | }) |
| 90 | ctx.SetProvider(valueSetProviderKey, valueSetProviderData{ |
Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 91 | AvailablePackages: packages, |
Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 92 | }) |
| 93 | } |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 94 | |
| 95 | type bazelAconfigValueSetAttributes struct { |
| 96 | Values bazel.LabelListAttribute |
| 97 | } |
| 98 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 99 | func (module *ValueSetModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 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 | } |