blob: c5657666c185b0fb9c9fee82abd99e8bb71da9ce [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
41 srcJarPath android.WritablePath
42}
43
44func DefinitionsFactory() android.Module {
45 module := &DefinitionsModule{}
46
47 android.InitAndroidModule(module)
48 android.InitDefaultableModule(module)
49 module.AddProperties(&module.properties)
50 // TODO: bp2build
51 //android.InitBazelModule(module)
52
53 return module
54}
55
56type implicitValuesTagType struct {
57 blueprint.BaseDependencyTag
58}
59
60var implicitValuesTag = implicitValuesTagType{}
61
62func (module *DefinitionsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
63 // Validate Properties
64 if len(module.properties.Srcs) == 0 {
65 ctx.PropertyErrorf("srcs", "missing source files")
66 return
67 }
68 if len(module.properties.Namespace) == 0 {
69 ctx.PropertyErrorf("namespace", "missing namespace property")
70 }
71
72 // Add a dependency on the device_config_value_sets defined in
73 // RELEASE_DEVICE_CONFIG_VALUE_SETS, and add any device_config_values that
74 // match our namespace.
75 valuesFromConfig := ctx.Config().ReleaseDeviceConfigValueSets()
76 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
77}
78
79func (module *DefinitionsModule) OutputFiles(tag string) (android.Paths, error) {
80 switch tag {
81 case ".srcjar":
82 return []android.Path{module.srcJarPath}, nil
83 case "":
84 // The default output of this module is the intermediates format, which is
85 // not installable and in a private format that no other rules can handle
86 // correctly.
87 return []android.Path{module.intermediatePath}, nil
88 default:
89 return nil, fmt.Errorf("unsupported device_config_definitions module reference tag %q", tag)
90 }
91}
92
93func joinAndPrefix(prefix string, values []string) string {
94 var sb strings.Builder
95 for _, v := range values {
96 sb.WriteString(prefix)
97 sb.WriteString(v)
98 }
99 return sb.String()
100}
101
102func (module *DefinitionsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
103 // Get the values that came from the global RELEASE_DEVICE_CONFIG_VALUE_SETS flag
104 ctx.VisitDirectDeps(func(dep android.Module) {
105 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
106 // Other modules get injected as dependencies too, for example the license modules
107 return
108 }
109 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
110 valuesFiles, ok := depData.AvailableNamespaces[module.properties.Namespace]
111 if ok {
112 for _, path := range valuesFiles {
113 module.properties.Values = append(module.properties.Values, path.String())
114 }
115 }
116 })
117
118 // Intermediate format
119 inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
120 module.intermediatePath = android.PathForModuleOut(ctx, "intermediate.json")
121 ctx.Build(pctx, android.BuildParams{
122 Rule: aconfigRule,
123 Inputs: inputFiles,
124 Output: module.intermediatePath,
125 Description: "device_config_definitions",
126 Args: map[string]string{
127 "release_version": ctx.Config().ReleaseVersion(),
128 "namespace": module.properties.Namespace,
129 "values": joinAndPrefix(" --values ", module.properties.Values),
130 },
131 })
132
133 // Generated java inside a srcjar
134 module.srcJarPath = android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
135 ctx.Build(pctx, android.BuildParams{
136 Rule: srcJarRule,
137 Input: module.intermediatePath,
138 Output: module.srcJarPath,
139 Description: "device_config.srcjar",
140 })
141
142 // TODO: C++
143
144 // Phony target for debugging convenience
145 ctx.Build(pctx, android.BuildParams{
146 Rule: blueprint.Phony,
147 Output: android.PathForPhony(ctx, ctx.ModuleName()),
148 Inputs: []android.Path{module.srcJarPath}, // TODO: C++
149 })
150}