blob: ab2e4f78af64dc35f4d73a0112de5790c33a4b30 [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
15package device_config
16
17import (
18 "android/soong/android"
19 "fmt"
20 "github.com/google/blueprint"
21 "strings"
22)
23
24type DefinitionsModule struct {
25 android.ModuleBase
26 android.DefaultableModuleBase
27
28 // Properties for "device_config_definitions"
29 properties struct {
30 // aconfig files, relative to this Android.bp file
31 Srcs []string `android:"path"`
32
33 // Release config flag namespace
34 Namespace string
35
36 // Values from TARGET_RELEASE / RELEASE_DEVICE_CONFIG_VALUE_SETS
37 Values []string `blueprint:"mutated"`
38 }
39
40 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070041}
42
43func DefinitionsFactory() android.Module {
44 module := &DefinitionsModule{}
45
46 android.InitAndroidModule(module)
47 android.InitDefaultableModule(module)
48 module.AddProperties(&module.properties)
49 // TODO: bp2build
50 //android.InitBazelModule(module)
51
52 return module
53}
54
55type implicitValuesTagType struct {
56 blueprint.BaseDependencyTag
57}
58
59var implicitValuesTag = implicitValuesTagType{}
60
61func (module *DefinitionsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
62 // Validate Properties
63 if len(module.properties.Srcs) == 0 {
64 ctx.PropertyErrorf("srcs", "missing source files")
65 return
66 }
67 if len(module.properties.Namespace) == 0 {
68 ctx.PropertyErrorf("namespace", "missing namespace property")
69 }
70
71 // Add a dependency on the device_config_value_sets defined in
72 // RELEASE_DEVICE_CONFIG_VALUE_SETS, and add any device_config_values that
73 // match our namespace.
74 valuesFromConfig := ctx.Config().ReleaseDeviceConfigValueSets()
75 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
76}
77
78func (module *DefinitionsModule) OutputFiles(tag string) (android.Paths, error) {
79 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070080 case "":
81 // The default output of this module is the intermediates format, which is
82 // not installable and in a private format that no other rules can handle
83 // correctly.
84 return []android.Path{module.intermediatePath}, nil
85 default:
86 return nil, fmt.Errorf("unsupported device_config_definitions module reference tag %q", tag)
87 }
88}
89
90func joinAndPrefix(prefix string, values []string) string {
91 var sb strings.Builder
92 for _, v := range values {
93 sb.WriteString(prefix)
94 sb.WriteString(v)
95 }
96 return sb.String()
97}
98
Joe Onorato175073c2023-06-01 14:42:59 -070099// Provider published by device_config_value_set
100type definitionsProviderData struct {
101 namespace string
102 intermediatePath android.WritablePath
103}
104
105var definitionsProviderKey = blueprint.NewProvider(definitionsProviderData{})
106
Joe Onoratofee845a2023-05-09 08:14:14 -0700107func (module *DefinitionsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
108 // Get the values that came from the global RELEASE_DEVICE_CONFIG_VALUE_SETS flag
109 ctx.VisitDirectDeps(func(dep android.Module) {
110 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
111 // Other modules get injected as dependencies too, for example the license modules
112 return
113 }
114 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
115 valuesFiles, ok := depData.AvailableNamespaces[module.properties.Namespace]
116 if ok {
117 for _, path := range valuesFiles {
118 module.properties.Values = append(module.properties.Values, path.String())
119 }
120 }
121 })
122
123 // Intermediate format
124 inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200125 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Joe Onoratofee845a2023-05-09 08:14:14 -0700126 ctx.Build(pctx, android.BuildParams{
127 Rule: aconfigRule,
128 Inputs: inputFiles,
Joe Onorato175073c2023-06-01 14:42:59 -0700129 Output: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700130 Description: "device_config_definitions",
131 Args: map[string]string{
132 "release_version": ctx.Config().ReleaseVersion(),
133 "namespace": module.properties.Namespace,
134 "values": joinAndPrefix(" --values ", module.properties.Values),
135 },
136 })
137
Joe Onorato175073c2023-06-01 14:42:59 -0700138 ctx.SetProvider(definitionsProviderKey, definitionsProviderData{
139 namespace: module.properties.Namespace,
140 intermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700141 })
142
Joe Onoratofee845a2023-05-09 08:14:14 -0700143}