blob: b1810b3052abe7ab2abce2a50cc12eb7b0217c20 [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",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -070063 variables: ["board", "feature1", "FEATURE3"],
64 bool_variables: ["feature2"],
Dan Willemsenb0935db2020-03-23 19:42:18 -070065 value_variables: ["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",
71 values: ["soc_a", "soc_b"],
72 }
73
74 soong_config_bool_variable {
75 name: "feature1",
76 }
77
78 soong_config_bool_variable {
Colin Cross3beeb1e2020-02-05 16:27:47 -080079 name: "FEATURE3",
Colin Cross9d34f352019-11-22 16:03:51 -080080 }
81 `
82
83 importBp := `
84 soong_config_module_type_import {
85 from: "SoongConfig.bp",
Liz Kammerdbd48092020-09-21 22:24:17 +000086 module_types: ["acme_test"],
Colin Cross9d34f352019-11-22 16:03:51 -080087 }
88 `
89
90 bp := `
Liz Kammerdbd48092020-09-21 22:24:17 +000091 test_defaults {
92 name: "foo_defaults",
93 cflags: ["DEFAULT"],
94 }
95
96 acme_test {
Colin Cross9d34f352019-11-22 16:03:51 -080097 name: "foo",
98 cflags: ["-DGENERIC"],
Liz Kammerdbd48092020-09-21 22:24:17 +000099 defaults: ["foo_defaults"],
Colin Cross9d34f352019-11-22 16:03:51 -0800100 soong_config_variables: {
101 board: {
102 soc_a: {
103 cflags: ["-DSOC_A"],
104 },
105 soc_b: {
106 cflags: ["-DSOC_B"],
107 },
108 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700109 size: {
110 cflags: ["-DSIZE=%s"],
111 },
Colin Cross9d34f352019-11-22 16:03:51 -0800112 feature1: {
113 cflags: ["-DFEATURE1"],
114 },
115 feature2: {
116 cflags: ["-DFEATURE2"],
117 },
Colin Cross3beeb1e2020-02-05 16:27:47 -0800118 FEATURE3: {
Colin Cross9d34f352019-11-22 16:03:51 -0800119 cflags: ["-DFEATURE3"],
120 },
121 },
122 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000123
124 test_defaults {
125 name: "foo_defaults_a",
126 cflags: ["DEFAULT_A"],
127 }
128
129 test_defaults {
130 name: "foo_defaults_b",
131 cflags: ["DEFAULT_B"],
132 }
133
134 acme_test {
135 name: "foo_with_defaults",
136 cflags: ["-DGENERIC"],
137 defaults: ["foo_defaults"],
138 soong_config_variables: {
139 board: {
140 soc_a: {
141 cflags: ["-DSOC_A"],
142 defaults: ["foo_defaults_a"],
143 },
144 soc_b: {
145 cflags: ["-DSOC_B"],
146 defaults: ["foo_defaults_b"],
147 },
148 },
149 size: {
150 cflags: ["-DSIZE=%s"],
151 },
152 feature1: {
153 cflags: ["-DFEATURE1"],
154 },
155 feature2: {
156 cflags: ["-DFEATURE2"],
157 },
158 FEATURE3: {
159 cflags: ["-DFEATURE3"],
160 },
161 },
162 }
Colin Cross9d34f352019-11-22 16:03:51 -0800163 `
164
165 run := func(t *testing.T, bp string, fs map[string][]byte) {
166 config := TestConfig(buildDir, nil, bp, fs)
167
168 config.TestProductVariables.VendorVars = map[string]map[string]string{
169 "acme": map[string]string{
170 "board": "soc_a",
Dan Willemsenb0935db2020-03-23 19:42:18 -0700171 "size": "42",
Colin Cross9d34f352019-11-22 16:03:51 -0800172 "feature1": "true",
173 "feature2": "false",
174 // FEATURE3 unset
175 },
176 }
177
Colin Crossae8600b2020-10-29 17:09:13 -0700178 ctx := NewTestContext(config)
Colin Cross9d34f352019-11-22 16:03:51 -0800179 ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
180 ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
181 ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
182 ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
Liz Kammerdbd48092020-09-21 22:24:17 +0000183 ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory)
184 ctx.RegisterModuleType("test", soongConfigTestModuleFactory)
185 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
Colin Crossae8600b2020-10-29 17:09:13 -0700186 ctx.Register()
Colin Cross9d34f352019-11-22 16:03:51 -0800187
188 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
189 FailIfErrored(t, errs)
190 _, errs = ctx.PrepareBuildActions(config)
191 FailIfErrored(t, errs)
192
Liz Kammerdbd48092020-09-21 22:24:17 +0000193 basicCFlags := []string{"DEFAULT", "-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"}
194
Colin Cross9d34f352019-11-22 16:03:51 -0800195 foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
Liz Kammerdbd48092020-09-21 22:24:17 +0000196 if g, w := foo.props.Cflags, basicCFlags; !reflect.DeepEqual(g, w) {
Colin Cross9d34f352019-11-22 16:03:51 -0800197 t.Errorf("wanted foo cflags %q, got %q", w, g)
198 }
Liz Kammerdbd48092020-09-21 22:24:17 +0000199
200 fooDefaults := ctx.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
201 if g, w := fooDefaults.props.Cflags, append([]string{"DEFAULT_A"}, basicCFlags...); !reflect.DeepEqual(g, w) {
202 t.Errorf("wanted foo_with_defaults cflags %q, got %q", w, g)
203 }
204
Colin Cross9d34f352019-11-22 16:03:51 -0800205 }
206
207 t.Run("single file", func(t *testing.T) {
208 run(t, configBp+bp, nil)
209 })
210
211 t.Run("import", func(t *testing.T) {
212 run(t, importBp+bp, map[string][]byte{
213 "SoongConfig.bp": []byte(configBp),
214 })
215 })
216}