blob: 45463fd31ceb2127d422ee3ee155c5fbe710df20 [file] [log] [blame]
Colin Cross9d34f352019-11-22 16:03:51 -08001// Copyright 2019 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 android
16
17import (
18 "reflect"
19 "testing"
20)
21
Liz Kammerdbd48092020-09-21 22:24:17 +000022type soongConfigTestDefaultsModuleProperties struct {
23}
24
25type soongConfigTestDefaultsModule struct {
26 ModuleBase
27 DefaultsModuleBase
28}
29
30func soongConfigTestDefaultsModuleFactory() Module {
31 m := &soongConfigTestDefaultsModule{}
32 m.AddProperties(&soongConfigTestModuleProperties{})
33 InitDefaultsModule(m)
34 return m
35}
36
Colin Cross9d34f352019-11-22 16:03:51 -080037type soongConfigTestModule struct {
38 ModuleBase
Liz Kammerdbd48092020-09-21 22:24:17 +000039 DefaultableModuleBase
Colin Cross9d34f352019-11-22 16:03:51 -080040 props soongConfigTestModuleProperties
41}
42
43type soongConfigTestModuleProperties struct {
44 Cflags []string
45}
46
47func soongConfigTestModuleFactory() Module {
48 m := &soongConfigTestModule{}
49 m.AddProperties(&m.props)
50 InitAndroidModule(m)
Liz Kammerdbd48092020-09-21 22:24:17 +000051 InitDefaultableModule(m)
Colin Cross9d34f352019-11-22 16:03:51 -080052 return m
53}
54
55func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {}
56
57func TestSoongConfigModule(t *testing.T) {
58 configBp := `
59 soong_config_module_type {
Liz Kammerdbd48092020-09-21 22:24:17 +000060 name: "acme_test",
61 module_type: "test",
Colin Cross9d34f352019-11-22 16:03:51 -080062 config_namespace: "acme",
Liz Kammer432bd592020-12-16 12:42:02 -080063 variables: ["board", "feature1", "FEATURE3", "unused_string_var"],
64 bool_variables: ["feature2", "unused_feature"],
65 value_variables: ["size", "unused_size"],
Liz Kammerdbd48092020-09-21 22:24:17 +000066 properties: ["cflags", "srcs", "defaults"],
Colin Cross9d34f352019-11-22 16:03:51 -080067 }
68
69 soong_config_string_variable {
70 name: "board",
Liz Kammer432bd592020-12-16 12:42:02 -080071 values: ["soc_a", "soc_b", "soc_c", "soc_d"],
72 }
73
74 soong_config_string_variable {
75 name: "unused_string_var",
76 values: ["a", "b"],
Colin Cross9d34f352019-11-22 16:03:51 -080077 }
78
79 soong_config_bool_variable {
80 name: "feature1",
81 }
82
83 soong_config_bool_variable {
Colin Cross3beeb1e2020-02-05 16:27:47 -080084 name: "FEATURE3",
Colin Cross9d34f352019-11-22 16:03:51 -080085 }
86 `
87
88 importBp := `
89 soong_config_module_type_import {
90 from: "SoongConfig.bp",
Liz Kammerdbd48092020-09-21 22:24:17 +000091 module_types: ["acme_test"],
Colin Cross9d34f352019-11-22 16:03:51 -080092 }
93 `
94
95 bp := `
Liz Kammerdbd48092020-09-21 22:24:17 +000096 test_defaults {
97 name: "foo_defaults",
98 cflags: ["DEFAULT"],
99 }
100
101 acme_test {
Colin Cross9d34f352019-11-22 16:03:51 -0800102 name: "foo",
103 cflags: ["-DGENERIC"],
Liz Kammerdbd48092020-09-21 22:24:17 +0000104 defaults: ["foo_defaults"],
Colin Cross9d34f352019-11-22 16:03:51 -0800105 soong_config_variables: {
106 board: {
107 soc_a: {
108 cflags: ["-DSOC_A"],
109 },
110 soc_b: {
111 cflags: ["-DSOC_B"],
112 },
Liz Kammer432bd592020-12-16 12:42:02 -0800113 soc_c: {},
114 conditions_default: {
115 cflags: ["-DSOC_CONDITIONS_DEFAULT"],
116 },
Colin Cross9d34f352019-11-22 16:03:51 -0800117 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700118 size: {
119 cflags: ["-DSIZE=%s"],
Liz Kammer432bd592020-12-16 12:42:02 -0800120 conditions_default: {
121 cflags: ["-DSIZE=CONDITIONS_DEFAULT"],
122 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700123 },
Colin Cross9d34f352019-11-22 16:03:51 -0800124 feature1: {
Liz Kammer432bd592020-12-16 12:42:02 -0800125 conditions_default: {
126 cflags: ["-DF1_CONDITIONS_DEFAULT"],
127 },
Colin Cross9d34f352019-11-22 16:03:51 -0800128 cflags: ["-DFEATURE1"],
129 },
130 feature2: {
131 cflags: ["-DFEATURE2"],
Liz Kammer432bd592020-12-16 12:42:02 -0800132 conditions_default: {
133 cflags: ["-DF2_CONDITIONS_DEFAULT"],
134 },
Colin Cross9d34f352019-11-22 16:03:51 -0800135 },
Colin Cross3beeb1e2020-02-05 16:27:47 -0800136 FEATURE3: {
Colin Cross9d34f352019-11-22 16:03:51 -0800137 cflags: ["-DFEATURE3"],
138 },
139 },
140 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000141
142 test_defaults {
143 name: "foo_defaults_a",
144 cflags: ["DEFAULT_A"],
145 }
146
147 test_defaults {
148 name: "foo_defaults_b",
149 cflags: ["DEFAULT_B"],
150 }
151
152 acme_test {
153 name: "foo_with_defaults",
154 cflags: ["-DGENERIC"],
155 defaults: ["foo_defaults"],
156 soong_config_variables: {
157 board: {
158 soc_a: {
159 cflags: ["-DSOC_A"],
160 defaults: ["foo_defaults_a"],
161 },
162 soc_b: {
163 cflags: ["-DSOC_B"],
164 defaults: ["foo_defaults_b"],
165 },
Liz Kammer432bd592020-12-16 12:42:02 -0800166 soc_c: {},
Liz Kammerdbd48092020-09-21 22:24:17 +0000167 },
168 size: {
169 cflags: ["-DSIZE=%s"],
170 },
171 feature1: {
172 cflags: ["-DFEATURE1"],
173 },
174 feature2: {
175 cflags: ["-DFEATURE2"],
176 },
177 FEATURE3: {
178 cflags: ["-DFEATURE3"],
179 },
180 },
181 }
Colin Cross9d34f352019-11-22 16:03:51 -0800182 `
183
184 run := func(t *testing.T, bp string, fs map[string][]byte) {
Liz Kammer432bd592020-12-16 12:42:02 -0800185 testCases := []struct {
186 name string
187 config Config
188 fooExpectedFlags []string
189 fooDefaultsExpectedFlags []string
190 }{
191 {
192 name: "withValues",
193 config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{
194 "acme": map[string]string{
195 "board": "soc_a",
196 "size": "42",
197 "feature1": "true",
198 "feature2": "false",
199 // FEATURE3 unset
200 "unused_feature": "true", // unused
201 "unused_size": "1", // unused
202 "unused_string_var": "a", // unused
203 },
204 }),
205 fooExpectedFlags: []string{
206 "DEFAULT",
207 "-DGENERIC",
208 "-DF2_CONDITIONS_DEFAULT",
209 "-DSIZE=42",
210 "-DSOC_A",
211 "-DFEATURE1",
212 },
213 fooDefaultsExpectedFlags: []string{
214 "DEFAULT_A",
215 "DEFAULT",
216 "-DGENERIC",
217 "-DSIZE=42",
218 "-DSOC_A",
219 "-DFEATURE1",
220 },
221 },
222 {
223 name: "empty_prop_for_string_var",
224 config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{
225 "acme": map[string]string{"board": "soc_c"}}),
226 fooExpectedFlags: []string{
227 "DEFAULT",
228 "-DGENERIC",
229 "-DF2_CONDITIONS_DEFAULT",
230 "-DSIZE=CONDITIONS_DEFAULT",
231 "-DF1_CONDITIONS_DEFAULT",
232 },
233 fooDefaultsExpectedFlags: []string{
234 "DEFAULT",
235 "-DGENERIC",
236 },
237 },
238 {
239 name: "unused_string_var",
240 config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{
241 "acme": map[string]string{"board": "soc_d"}}),
242 fooExpectedFlags: []string{
243 "DEFAULT",
244 "-DGENERIC",
245 "-DF2_CONDITIONS_DEFAULT",
246 "-DSIZE=CONDITIONS_DEFAULT",
247 "-DSOC_CONDITIONS_DEFAULT", // foo does not contain a prop "soc_d", so we use the default
248 "-DF1_CONDITIONS_DEFAULT",
249 },
250 fooDefaultsExpectedFlags: []string{
251 "DEFAULT",
252 "-DGENERIC",
253 },
254 },
Colin Cross9d34f352019-11-22 16:03:51 -0800255
Liz Kammer432bd592020-12-16 12:42:02 -0800256 {
257 name: "conditions_default",
258 config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{}),
259 fooExpectedFlags: []string{
260 "DEFAULT",
261 "-DGENERIC",
262 "-DF2_CONDITIONS_DEFAULT",
263 "-DSIZE=CONDITIONS_DEFAULT",
264 "-DSOC_CONDITIONS_DEFAULT",
265 "-DF1_CONDITIONS_DEFAULT",
266 },
267 fooDefaultsExpectedFlags: []string{
268 "DEFAULT",
269 "-DGENERIC",
270 },
Colin Cross9d34f352019-11-22 16:03:51 -0800271 },
272 }
273
Liz Kammer432bd592020-12-16 12:42:02 -0800274 for _, tc := range testCases {
275 ctx := NewTestContext(tc.config)
276 ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
277 ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
278 ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
279 ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
280 ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory)
281 ctx.RegisterModuleType("test", soongConfigTestModuleFactory)
282 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
283 ctx.Register()
Colin Cross9d34f352019-11-22 16:03:51 -0800284
Liz Kammer432bd592020-12-16 12:42:02 -0800285 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
286 FailIfErrored(t, errs)
287 _, errs = ctx.PrepareBuildActions(tc.config)
288 FailIfErrored(t, errs)
Colin Cross9d34f352019-11-22 16:03:51 -0800289
Liz Kammer432bd592020-12-16 12:42:02 -0800290 foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
291 if g, w := foo.props.Cflags, tc.fooExpectedFlags; !reflect.DeepEqual(g, w) {
292 t.Errorf("%s: wanted foo cflags %q, got %q", tc.name, w, g)
293 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000294
Liz Kammer432bd592020-12-16 12:42:02 -0800295 fooDefaults := ctx.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
296 if g, w := fooDefaults.props.Cflags, tc.fooDefaultsExpectedFlags; !reflect.DeepEqual(g, w) {
297 t.Errorf("%s: wanted foo_with_defaults cflags %q, got %q", tc.name, w, g)
298 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000299 }
300
Colin Cross9d34f352019-11-22 16:03:51 -0800301 }
302
303 t.Run("single file", func(t *testing.T) {
304 run(t, configBp+bp, nil)
305 })
306
307 t.Run("import", func(t *testing.T) {
308 run(t, importBp+bp, map[string][]byte{
309 "SoongConfig.bp": []byte(configBp),
310 })
311 })
312}
Liz Kammer432bd592020-12-16 12:42:02 -0800313
314func testConfigWithVendorVars(buildDir, bp string, fs map[string][]byte, vendorVars map[string]map[string]string) Config {
315 config := TestConfig(buildDir, nil, bp, fs)
316
317 config.TestProductVariables.VendorVars = vendorVars
318
319 return config
320}