blob: 3cd8c22f320e14f4a75b85918e5a6a44f3bd4e36 [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) {
Colin Cross323dc602020-09-18 14:25:31 -070058 t.Parallel()
Colin Cross9d34f352019-11-22 16:03:51 -080059 configBp := `
60 soong_config_module_type {
Liz Kammerdbd48092020-09-21 22:24:17 +000061 name: "acme_test",
62 module_type: "test",
Colin Cross9d34f352019-11-22 16:03:51 -080063 config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -070064 variables: ["board", "feature1", "FEATURE3"],
65 bool_variables: ["feature2"],
Dan Willemsenb0935db2020-03-23 19:42:18 -070066 value_variables: ["size"],
Liz Kammerdbd48092020-09-21 22:24:17 +000067 properties: ["cflags", "srcs", "defaults"],
Colin Cross9d34f352019-11-22 16:03:51 -080068 }
69
70 soong_config_string_variable {
71 name: "board",
72 values: ["soc_a", "soc_b"],
73 }
74
75 soong_config_bool_variable {
76 name: "feature1",
77 }
78
79 soong_config_bool_variable {
Colin Cross3beeb1e2020-02-05 16:27:47 -080080 name: "FEATURE3",
Colin Cross9d34f352019-11-22 16:03:51 -080081 }
82 `
83
84 importBp := `
85 soong_config_module_type_import {
86 from: "SoongConfig.bp",
Liz Kammerdbd48092020-09-21 22:24:17 +000087 module_types: ["acme_test"],
Colin Cross9d34f352019-11-22 16:03:51 -080088 }
89 `
90
91 bp := `
Liz Kammerdbd48092020-09-21 22:24:17 +000092 test_defaults {
93 name: "foo_defaults",
94 cflags: ["DEFAULT"],
95 }
96
97 acme_test {
Colin Cross9d34f352019-11-22 16:03:51 -080098 name: "foo",
99 cflags: ["-DGENERIC"],
Liz Kammerdbd48092020-09-21 22:24:17 +0000100 defaults: ["foo_defaults"],
Colin Cross9d34f352019-11-22 16:03:51 -0800101 soong_config_variables: {
102 board: {
103 soc_a: {
104 cflags: ["-DSOC_A"],
105 },
106 soc_b: {
107 cflags: ["-DSOC_B"],
108 },
109 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700110 size: {
111 cflags: ["-DSIZE=%s"],
112 },
Colin Cross9d34f352019-11-22 16:03:51 -0800113 feature1: {
114 cflags: ["-DFEATURE1"],
115 },
116 feature2: {
117 cflags: ["-DFEATURE2"],
118 },
Colin Cross3beeb1e2020-02-05 16:27:47 -0800119 FEATURE3: {
Colin Cross9d34f352019-11-22 16:03:51 -0800120 cflags: ["-DFEATURE3"],
121 },
122 },
123 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000124
125 test_defaults {
126 name: "foo_defaults_a",
127 cflags: ["DEFAULT_A"],
128 }
129
130 test_defaults {
131 name: "foo_defaults_b",
132 cflags: ["DEFAULT_B"],
133 }
134
135 acme_test {
136 name: "foo_with_defaults",
137 cflags: ["-DGENERIC"],
138 defaults: ["foo_defaults"],
139 soong_config_variables: {
140 board: {
141 soc_a: {
142 cflags: ["-DSOC_A"],
143 defaults: ["foo_defaults_a"],
144 },
145 soc_b: {
146 cflags: ["-DSOC_B"],
147 defaults: ["foo_defaults_b"],
148 },
149 },
150 size: {
151 cflags: ["-DSIZE=%s"],
152 },
153 feature1: {
154 cflags: ["-DFEATURE1"],
155 },
156 feature2: {
157 cflags: ["-DFEATURE2"],
158 },
159 FEATURE3: {
160 cflags: ["-DFEATURE3"],
161 },
162 },
163 }
Colin Cross9d34f352019-11-22 16:03:51 -0800164 `
165
166 run := func(t *testing.T, bp string, fs map[string][]byte) {
167 config := TestConfig(buildDir, nil, bp, fs)
168
169 config.TestProductVariables.VendorVars = map[string]map[string]string{
170 "acme": map[string]string{
171 "board": "soc_a",
Dan Willemsenb0935db2020-03-23 19:42:18 -0700172 "size": "42",
Colin Cross9d34f352019-11-22 16:03:51 -0800173 "feature1": "true",
174 "feature2": "false",
175 // FEATURE3 unset
176 },
177 }
178
179 ctx := NewTestContext()
180 ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
181 ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
182 ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
183 ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
Liz Kammerdbd48092020-09-21 22:24:17 +0000184 ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory)
185 ctx.RegisterModuleType("test", soongConfigTestModuleFactory)
186 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
Colin Cross9d34f352019-11-22 16:03:51 -0800187 ctx.Register(config)
188
189 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
190 FailIfErrored(t, errs)
191 _, errs = ctx.PrepareBuildActions(config)
192 FailIfErrored(t, errs)
193
Liz Kammerdbd48092020-09-21 22:24:17 +0000194 basicCFlags := []string{"DEFAULT", "-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"}
195
Colin Cross9d34f352019-11-22 16:03:51 -0800196 foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
Liz Kammerdbd48092020-09-21 22:24:17 +0000197 if g, w := foo.props.Cflags, basicCFlags; !reflect.DeepEqual(g, w) {
Colin Cross9d34f352019-11-22 16:03:51 -0800198 t.Errorf("wanted foo cflags %q, got %q", w, g)
199 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000200
201 fooDefaults := ctx.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
202 if g, w := fooDefaults.props.Cflags, append([]string{"DEFAULT_A"}, basicCFlags...); !reflect.DeepEqual(g, w) {
203 t.Errorf("wanted foo_with_defaults cflags %q, got %q", w, g)
204 }
205
Colin Cross9d34f352019-11-22 16:03:51 -0800206 }
207
208 t.Run("single file", func(t *testing.T) {
209 run(t, configBp+bp, nil)
210 })
211
212 t.Run("import", func(t *testing.T) {
213 run(t, importBp+bp, map[string][]byte{
214 "SoongConfig.bp": []byte(configBp),
215 })
216 })
217}