| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package android | 
|  | 16 |  | 
|  | 17 | import ( | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 18 | "fmt" | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 19 | "path/filepath" | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 20 | "testing" | 
|  | 21 | ) | 
|  | 22 |  | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 23 | type soongConfigTestDefaultsModule struct { | 
|  | 24 | ModuleBase | 
|  | 25 | DefaultsModuleBase | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | func soongConfigTestDefaultsModuleFactory() Module { | 
|  | 29 | m := &soongConfigTestDefaultsModule{} | 
|  | 30 | m.AddProperties(&soongConfigTestModuleProperties{}) | 
|  | 31 | InitDefaultsModule(m) | 
|  | 32 | return m | 
|  | 33 | } | 
|  | 34 |  | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 35 | type soongConfigTestModule struct { | 
|  | 36 | ModuleBase | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 37 | DefaultableModuleBase | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 38 | props      soongConfigTestModuleProperties | 
|  | 39 | outputPath ModuleOutPath | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
|  | 42 | type soongConfigTestModuleProperties struct { | 
|  | 43 | Cflags []string | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | func soongConfigTestModuleFactory() Module { | 
|  | 47 | m := &soongConfigTestModule{} | 
|  | 48 | m.AddProperties(&m.props) | 
|  | 49 | InitAndroidModule(m) | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 50 | InitDefaultableModule(m) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 51 | return m | 
|  | 52 | } | 
|  | 53 |  | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 54 | func (t *soongConfigTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 55 | t.outputPath = PathForModuleOut(ctx, "test") | 
|  | 56 | } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 57 |  | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 58 | var prepareForSoongConfigTestModule = FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 59 | ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) | 
|  | 60 | ctx.RegisterModuleType("test", soongConfigTestModuleFactory) | 
|  | 61 | }) | 
|  | 62 |  | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 63 | func TestSoongConfigModule(t *testing.T) { | 
|  | 64 | configBp := ` | 
|  | 65 | soong_config_module_type { | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 66 | name: "acme_test", | 
|  | 67 | module_type: "test", | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 68 | config_namespace: "acme", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 69 | variables: ["board", "feature1", "FEATURE3", "unused_string_var"], | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 70 | bool_variables: ["feature2", "unused_feature", "always_true"], | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 71 | value_variables: ["size", "unused_size"], | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 72 | properties: ["cflags", "srcs", "defaults"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | soong_config_string_variable { | 
|  | 76 | name: "board", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 77 | values: ["soc_a", "soc_b", "soc_c", "soc_d"], | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | soong_config_string_variable { | 
|  | 81 | name: "unused_string_var", | 
|  | 82 | values: ["a", "b"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | soong_config_bool_variable { | 
|  | 86 | name: "feature1", | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | soong_config_bool_variable { | 
| Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 90 | name: "FEATURE3", | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 91 | } | 
|  | 92 | ` | 
|  | 93 |  | 
|  | 94 | importBp := ` | 
|  | 95 | soong_config_module_type_import { | 
|  | 96 | from: "SoongConfig.bp", | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 97 | module_types: ["acme_test"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 98 | } | 
|  | 99 | ` | 
|  | 100 |  | 
|  | 101 | bp := ` | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 102 | test_defaults { | 
|  | 103 | name: "foo_defaults", | 
|  | 104 | cflags: ["DEFAULT"], | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | acme_test { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 108 | name: "foo", | 
|  | 109 | cflags: ["-DGENERIC"], | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 110 | defaults: ["foo_defaults"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 111 | soong_config_variables: { | 
|  | 112 | board: { | 
|  | 113 | soc_a: { | 
|  | 114 | cflags: ["-DSOC_A"], | 
|  | 115 | }, | 
|  | 116 | soc_b: { | 
|  | 117 | cflags: ["-DSOC_B"], | 
|  | 118 | }, | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 119 | soc_c: {}, | 
|  | 120 | conditions_default: { | 
|  | 121 | cflags: ["-DSOC_CONDITIONS_DEFAULT"], | 
|  | 122 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 123 | }, | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 124 | size: { | 
|  | 125 | cflags: ["-DSIZE=%s"], | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 126 | conditions_default: { | 
|  | 127 | cflags: ["-DSIZE=CONDITIONS_DEFAULT"], | 
|  | 128 | }, | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 129 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 130 | feature1: { | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 131 | conditions_default: { | 
|  | 132 | cflags: ["-DF1_CONDITIONS_DEFAULT"], | 
|  | 133 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 134 | cflags: ["-DFEATURE1"], | 
|  | 135 | }, | 
|  | 136 | feature2: { | 
|  | 137 | cflags: ["-DFEATURE2"], | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 138 | conditions_default: { | 
|  | 139 | cflags: ["-DF2_CONDITIONS_DEFAULT"], | 
|  | 140 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 141 | }, | 
| Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 142 | FEATURE3: { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 143 | cflags: ["-DFEATURE3"], | 
|  | 144 | }, | 
|  | 145 | }, | 
|  | 146 | } | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 147 |  | 
|  | 148 | test_defaults { | 
|  | 149 | name: "foo_defaults_a", | 
|  | 150 | cflags: ["DEFAULT_A"], | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | test_defaults { | 
|  | 154 | name: "foo_defaults_b", | 
|  | 155 | cflags: ["DEFAULT_B"], | 
|  | 156 | } | 
|  | 157 |  | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 158 | test_defaults { | 
|  | 159 | name: "foo_defaults_always_true", | 
|  | 160 | cflags: ["DEFAULT_ALWAYS_TRUE"], | 
|  | 161 | } | 
|  | 162 |  | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 163 | acme_test { | 
|  | 164 | name: "foo_with_defaults", | 
|  | 165 | cflags: ["-DGENERIC"], | 
|  | 166 | defaults: ["foo_defaults"], | 
|  | 167 | soong_config_variables: { | 
|  | 168 | board: { | 
|  | 169 | soc_a: { | 
|  | 170 | cflags: ["-DSOC_A"], | 
|  | 171 | defaults: ["foo_defaults_a"], | 
|  | 172 | }, | 
|  | 173 | soc_b: { | 
|  | 174 | cflags: ["-DSOC_B"], | 
|  | 175 | defaults: ["foo_defaults_b"], | 
|  | 176 | }, | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 177 | soc_c: {}, | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 178 | }, | 
|  | 179 | size: { | 
|  | 180 | cflags: ["-DSIZE=%s"], | 
|  | 181 | }, | 
|  | 182 | feature1: { | 
|  | 183 | cflags: ["-DFEATURE1"], | 
|  | 184 | }, | 
|  | 185 | feature2: { | 
|  | 186 | cflags: ["-DFEATURE2"], | 
|  | 187 | }, | 
|  | 188 | FEATURE3: { | 
|  | 189 | cflags: ["-DFEATURE3"], | 
|  | 190 | }, | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 191 | always_true: { | 
|  | 192 | defaults: ["foo_defaults_always_true"], | 
|  | 193 | conditions_default: { | 
|  | 194 | // verify that conditions_default is skipped if the | 
|  | 195 | // soong config variable is true by specifying a | 
|  | 196 | // non-existent module in conditions_default | 
|  | 197 | defaults: ["//nonexistent:defaults"], | 
|  | 198 | } | 
|  | 199 | }, | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 200 | }, | 
|  | 201 | } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 202 | ` | 
|  | 203 |  | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 204 | fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { | 
|  | 205 | return FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 206 | variables.VendorVars = vars | 
|  | 207 | }) | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | run := func(t *testing.T, bp string, fs MockFS) { | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 211 | testCases := []struct { | 
|  | 212 | name                     string | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 213 | preparer                 FixturePreparer | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 214 | fooExpectedFlags         []string | 
|  | 215 | fooDefaultsExpectedFlags []string | 
|  | 216 | }{ | 
|  | 217 | { | 
|  | 218 | name: "withValues", | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 219 | preparer: fixtureForVendorVars(map[string]map[string]string{ | 
|  | 220 | "acme": { | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 221 | "board":    "soc_a", | 
|  | 222 | "size":     "42", | 
|  | 223 | "feature1": "true", | 
|  | 224 | "feature2": "false", | 
|  | 225 | // FEATURE3 unset | 
|  | 226 | "unused_feature":    "true", // unused | 
|  | 227 | "unused_size":       "1",    // unused | 
|  | 228 | "unused_string_var": "a",    // unused | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 229 | "always_true":       "true", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 230 | }, | 
|  | 231 | }), | 
|  | 232 | fooExpectedFlags: []string{ | 
|  | 233 | "DEFAULT", | 
|  | 234 | "-DGENERIC", | 
|  | 235 | "-DF2_CONDITIONS_DEFAULT", | 
|  | 236 | "-DSIZE=42", | 
|  | 237 | "-DSOC_A", | 
|  | 238 | "-DFEATURE1", | 
|  | 239 | }, | 
|  | 240 | fooDefaultsExpectedFlags: []string{ | 
|  | 241 | "DEFAULT_A", | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 242 | "DEFAULT_ALWAYS_TRUE", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 243 | "DEFAULT", | 
|  | 244 | "-DGENERIC", | 
|  | 245 | "-DSIZE=42", | 
|  | 246 | "-DSOC_A", | 
|  | 247 | "-DFEATURE1", | 
|  | 248 | }, | 
|  | 249 | }, | 
|  | 250 | { | 
|  | 251 | name: "empty_prop_for_string_var", | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 252 | preparer: fixtureForVendorVars(map[string]map[string]string{ | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 253 | "acme": { | 
|  | 254 | "board":       "soc_c", | 
|  | 255 | "always_true": "true", | 
|  | 256 | }}), | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 257 | fooExpectedFlags: []string{ | 
|  | 258 | "DEFAULT", | 
|  | 259 | "-DGENERIC", | 
|  | 260 | "-DF2_CONDITIONS_DEFAULT", | 
|  | 261 | "-DSIZE=CONDITIONS_DEFAULT", | 
|  | 262 | "-DF1_CONDITIONS_DEFAULT", | 
|  | 263 | }, | 
|  | 264 | fooDefaultsExpectedFlags: []string{ | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 265 | "DEFAULT_ALWAYS_TRUE", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 266 | "DEFAULT", | 
|  | 267 | "-DGENERIC", | 
|  | 268 | }, | 
|  | 269 | }, | 
|  | 270 | { | 
|  | 271 | name: "unused_string_var", | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 272 | preparer: fixtureForVendorVars(map[string]map[string]string{ | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 273 | "acme": { | 
|  | 274 | "board":       "soc_d", | 
|  | 275 | "always_true": "true", | 
|  | 276 | }}), | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 277 | fooExpectedFlags: []string{ | 
|  | 278 | "DEFAULT", | 
|  | 279 | "-DGENERIC", | 
|  | 280 | "-DF2_CONDITIONS_DEFAULT", | 
|  | 281 | "-DSIZE=CONDITIONS_DEFAULT", | 
|  | 282 | "-DSOC_CONDITIONS_DEFAULT", // foo does not contain a prop "soc_d", so we use the default | 
|  | 283 | "-DF1_CONDITIONS_DEFAULT", | 
|  | 284 | }, | 
|  | 285 | fooDefaultsExpectedFlags: []string{ | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 286 | "DEFAULT_ALWAYS_TRUE", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 287 | "DEFAULT", | 
|  | 288 | "-DGENERIC", | 
|  | 289 | }, | 
|  | 290 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 291 |  | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 292 | { | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 293 | name: "conditions_default", | 
|  | 294 | preparer: fixtureForVendorVars(map[string]map[string]string{ | 
|  | 295 | "acme": { | 
|  | 296 | "always_true": "true", | 
|  | 297 | }}), | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 298 | fooExpectedFlags: []string{ | 
|  | 299 | "DEFAULT", | 
|  | 300 | "-DGENERIC", | 
|  | 301 | "-DF2_CONDITIONS_DEFAULT", | 
|  | 302 | "-DSIZE=CONDITIONS_DEFAULT", | 
|  | 303 | "-DSOC_CONDITIONS_DEFAULT", | 
|  | 304 | "-DF1_CONDITIONS_DEFAULT", | 
|  | 305 | }, | 
|  | 306 | fooDefaultsExpectedFlags: []string{ | 
| Liz Kammer | bdce0df | 2021-10-15 13:34:27 -0400 | [diff] [blame] | 307 | "DEFAULT_ALWAYS_TRUE", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 308 | "DEFAULT", | 
|  | 309 | "-DGENERIC", | 
|  | 310 | }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 311 | }, | 
|  | 312 | } | 
|  | 313 |  | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 314 | for _, tc := range testCases { | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 315 | t.Run(tc.name, func(t *testing.T) { | 
| Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 316 | result := GroupFixturePreparers( | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 317 | tc.preparer, | 
|  | 318 | PrepareForTestWithDefaults, | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 319 | PrepareForTestWithSoongConfigModuleBuildComponents, | 
|  | 320 | prepareForSoongConfigTestModule, | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 321 | fs.AddToFixture(), | 
|  | 322 | FixtureWithRootAndroidBp(bp), | 
| Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 323 | ).RunTest(t) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 324 |  | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 325 | foo := result.ModuleForTests("foo", "").Module().(*soongConfigTestModule) | 
|  | 326 | AssertDeepEquals(t, "foo cflags", tc.fooExpectedFlags, foo.props.Cflags) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 327 |  | 
| Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 328 | fooDefaults := result.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule) | 
|  | 329 | AssertDeepEquals(t, "foo_with_defaults cflags", tc.fooDefaultsExpectedFlags, fooDefaults.props.Cflags) | 
|  | 330 | }) | 
| Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 331 | } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 332 | } | 
|  | 333 |  | 
|  | 334 | t.Run("single file", func(t *testing.T) { | 
|  | 335 | run(t, configBp+bp, nil) | 
|  | 336 | }) | 
|  | 337 |  | 
|  | 338 | t.Run("import", func(t *testing.T) { | 
|  | 339 | run(t, importBp+bp, map[string][]byte{ | 
|  | 340 | "SoongConfig.bp": []byte(configBp), | 
|  | 341 | }) | 
|  | 342 | }) | 
|  | 343 | } | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 344 |  | 
| Liz Kammer | 0fc1e13 | 2021-07-15 12:34:59 -0400 | [diff] [blame] | 345 | func TestNonExistentPropertyInSoongConfigModule(t *testing.T) { | 
|  | 346 | bp := ` | 
|  | 347 | soong_config_module_type { | 
|  | 348 | name: "acme_test", | 
|  | 349 | module_type: "test", | 
|  | 350 | config_namespace: "acme", | 
|  | 351 | bool_variables: ["feature1"], | 
|  | 352 | properties: ["made_up_property"], | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | acme_test { | 
|  | 356 | name: "foo", | 
|  | 357 | cflags: ["-DGENERIC"], | 
|  | 358 | soong_config_variables: { | 
|  | 359 | feature1: { | 
|  | 360 | made_up_property: true, | 
|  | 361 | }, | 
|  | 362 | }, | 
|  | 363 | } | 
|  | 364 | ` | 
|  | 365 |  | 
|  | 366 | fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { | 
|  | 367 | return FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 368 | variables.VendorVars = vars | 
|  | 369 | }) | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | GroupFixturePreparers( | 
|  | 373 | fixtureForVendorVars(map[string]map[string]string{"acme": {"feature1": "1"}}), | 
|  | 374 | PrepareForTestWithDefaults, | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 375 | PrepareForTestWithSoongConfigModuleBuildComponents, | 
|  | 376 | prepareForSoongConfigTestModule, | 
| Liz Kammer | 0fc1e13 | 2021-07-15 12:34:59 -0400 | [diff] [blame] | 377 | FixtureWithRootAndroidBp(bp), | 
|  | 378 | ).ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{ | 
|  | 379 | // TODO(b/171232169): improve the error message for non-existent properties | 
|  | 380 | `unrecognized property "soong_config_variables`, | 
|  | 381 | })).RunTest(t) | 
|  | 382 | } | 
|  | 383 |  | 
| Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 384 | func TestDuplicateStringValueInSoongConfigStringVariable(t *testing.T) { | 
|  | 385 | bp := ` | 
|  | 386 | soong_config_string_variable { | 
|  | 387 | name: "board", | 
|  | 388 | values: ["soc_a", "soc_b", "soc_c", "soc_a"], | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | soong_config_module_type { | 
|  | 392 | name: "acme_test", | 
|  | 393 | module_type: "test", | 
|  | 394 | config_namespace: "acme", | 
|  | 395 | variables: ["board"], | 
|  | 396 | properties: ["cflags", "srcs", "defaults"], | 
|  | 397 | } | 
|  | 398 | ` | 
|  | 399 |  | 
|  | 400 | fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { | 
|  | 401 | return FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 402 | variables.VendorVars = vars | 
|  | 403 | }) | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | GroupFixturePreparers( | 
|  | 407 | fixtureForVendorVars(map[string]map[string]string{"acme": {"feature1": "1"}}), | 
|  | 408 | PrepareForTestWithDefaults, | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 409 | PrepareForTestWithSoongConfigModuleBuildComponents, | 
|  | 410 | prepareForSoongConfigTestModule, | 
| Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 411 | FixtureWithRootAndroidBp(bp), | 
|  | 412 | ).ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{ | 
|  | 413 | // TODO(b/171232169): improve the error message for non-existent properties | 
|  | 414 | `Android.bp: soong_config_string_variable: values property error: duplicate value: "soc_a"`, | 
|  | 415 | })).RunTest(t) | 
|  | 416 | } | 
|  | 417 |  | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 418 | type soongConfigTestSingletonModule struct { | 
|  | 419 | SingletonModuleBase | 
|  | 420 | props soongConfigTestSingletonModuleProperties | 
|  | 421 | } | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 422 |  | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 423 | type soongConfigTestSingletonModuleProperties struct { | 
|  | 424 | Fragments []struct { | 
|  | 425 | Apex   string | 
|  | 426 | Module string | 
|  | 427 | } | 
|  | 428 | } | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 429 |  | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 430 | func soongConfigTestSingletonModuleFactory() SingletonModule { | 
|  | 431 | m := &soongConfigTestSingletonModule{} | 
|  | 432 | m.AddProperties(&m.props) | 
|  | 433 | InitAndroidModule(m) | 
|  | 434 | return m | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | func (t *soongConfigTestSingletonModule) GenerateAndroidBuildActions(ModuleContext) {} | 
|  | 438 |  | 
|  | 439 | func (t *soongConfigTestSingletonModule) GenerateSingletonBuildActions(SingletonContext) {} | 
|  | 440 |  | 
|  | 441 | var prepareForSoongConfigTestSingletonModule = FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 442 | ctx.RegisterSingletonModuleType("test_singleton", soongConfigTestSingletonModuleFactory) | 
|  | 443 | }) | 
|  | 444 |  | 
|  | 445 | func TestSoongConfigModuleSingletonModule(t *testing.T) { | 
|  | 446 | bp := ` | 
|  | 447 | soong_config_module_type { | 
|  | 448 | name: "acme_test_singleton", | 
|  | 449 | module_type: "test_singleton", | 
|  | 450 | config_namespace: "acme", | 
|  | 451 | bool_variables: ["coyote"], | 
|  | 452 | properties: ["fragments"], | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | acme_test_singleton { | 
|  | 456 | name: "wiley", | 
|  | 457 | fragments: [ | 
|  | 458 | { | 
|  | 459 | apex: "com.android.acme", | 
|  | 460 | module: "road-runner", | 
|  | 461 | }, | 
|  | 462 | ], | 
|  | 463 | soong_config_variables: { | 
|  | 464 | coyote: { | 
|  | 465 | fragments: [ | 
|  | 466 | { | 
|  | 467 | apex: "com.android.acme", | 
|  | 468 | module: "wiley", | 
|  | 469 | }, | 
|  | 470 | ], | 
|  | 471 | }, | 
|  | 472 | }, | 
|  | 473 | } | 
|  | 474 | ` | 
|  | 475 |  | 
|  | 476 | for _, test := range []struct { | 
|  | 477 | coyote            bool | 
|  | 478 | expectedFragments string | 
|  | 479 | }{ | 
|  | 480 | { | 
|  | 481 | coyote:            false, | 
|  | 482 | expectedFragments: "[{Apex:com.android.acme Module:road-runner}]", | 
|  | 483 | }, | 
|  | 484 | { | 
|  | 485 | coyote:            true, | 
|  | 486 | expectedFragments: "[{Apex:com.android.acme Module:road-runner} {Apex:com.android.acme Module:wiley}]", | 
|  | 487 | }, | 
|  | 488 | } { | 
|  | 489 | t.Run(fmt.Sprintf("coyote:%t", test.coyote), func(t *testing.T) { | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 490 | result := GroupFixturePreparers( | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 491 | PrepareForTestWithSoongConfigModuleBuildComponents, | 
|  | 492 | prepareForSoongConfigTestSingletonModule, | 
|  | 493 | FixtureWithRootAndroidBp(bp), | 
|  | 494 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 495 | variables.VendorVars = map[string]map[string]string{ | 
|  | 496 | "acme": { | 
|  | 497 | "coyote": fmt.Sprintf("%t", test.coyote), | 
|  | 498 | }, | 
|  | 499 | } | 
|  | 500 | }), | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 501 | ).RunTest(t) | 
|  | 502 |  | 
|  | 503 | // Make sure that the singleton was created. | 
|  | 504 | result.SingletonForTests("test_singleton") | 
|  | 505 | m := result.ModuleForTests("wiley", "").module.(*soongConfigTestSingletonModule) | 
|  | 506 | AssertStringEquals(t, "fragments", test.expectedFragments, fmt.Sprintf("%+v", m.props.Fragments)) | 
| Paul Duffin | f1e6a04 | 2023-01-09 15:43:29 +0000 | [diff] [blame] | 507 | }) | 
|  | 508 | } | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 509 | } | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 510 |  | 
|  | 511 | func TestSoongConfigModuleTrace(t *testing.T) { | 
|  | 512 | bp := ` | 
|  | 513 | soong_config_module_type { | 
|  | 514 | name: "acme_test", | 
|  | 515 | module_type: "test", | 
|  | 516 | config_namespace: "acme", | 
|  | 517 | variables: ["board", "feature1", "FEATURE3", "unused_string_var"], | 
|  | 518 | bool_variables: ["feature2", "unused_feature", "always_true"], | 
|  | 519 | value_variables: ["size", "unused_size"], | 
|  | 520 | properties: ["cflags", "srcs", "defaults"], | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | soong_config_module_type { | 
|  | 524 | name: "acme_test_defaults", | 
|  | 525 | module_type: "test_defaults", | 
|  | 526 | config_namespace: "acme", | 
|  | 527 | variables: ["board", "feature1", "FEATURE3", "unused_string_var"], | 
|  | 528 | bool_variables: ["feature2", "unused_feature", "always_true"], | 
|  | 529 | value_variables: ["size", "unused_size"], | 
|  | 530 | properties: ["cflags", "srcs", "defaults"], | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | soong_config_string_variable { | 
|  | 534 | name: "board", | 
|  | 535 | values: ["soc_a", "soc_b", "soc_c"], | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | soong_config_string_variable { | 
|  | 539 | name: "unused_string_var", | 
|  | 540 | values: ["a", "b"], | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | soong_config_bool_variable { | 
|  | 544 | name: "feature1", | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | soong_config_bool_variable { | 
|  | 548 | name: "FEATURE3", | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | test_defaults { | 
|  | 552 | name: "test_defaults", | 
|  | 553 | cflags: ["DEFAULT"], | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | test { | 
|  | 557 | name: "normal", | 
|  | 558 | defaults: ["test_defaults"], | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | acme_test { | 
|  | 562 | name: "board_1", | 
|  | 563 | defaults: ["test_defaults"], | 
|  | 564 | soong_config_variables: { | 
|  | 565 | board: { | 
|  | 566 | soc_a: { | 
|  | 567 | cflags: ["-DSOC_A"], | 
|  | 568 | }, | 
|  | 569 | }, | 
|  | 570 | }, | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | acme_test { | 
|  | 574 | name: "board_2", | 
|  | 575 | defaults: ["test_defaults"], | 
|  | 576 | soong_config_variables: { | 
|  | 577 | board: { | 
|  | 578 | soc_a: { | 
|  | 579 | cflags: ["-DSOC_A"], | 
|  | 580 | }, | 
|  | 581 | }, | 
|  | 582 | }, | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | acme_test { | 
|  | 586 | name: "size", | 
|  | 587 | defaults: ["test_defaults"], | 
|  | 588 | soong_config_variables: { | 
|  | 589 | size: { | 
|  | 590 | cflags: ["-DSIZE=%s"], | 
|  | 591 | }, | 
|  | 592 | }, | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | acme_test { | 
|  | 596 | name: "board_and_size", | 
|  | 597 | defaults: ["test_defaults"], | 
|  | 598 | soong_config_variables: { | 
|  | 599 | board: { | 
|  | 600 | soc_a: { | 
|  | 601 | cflags: ["-DSOC_A"], | 
|  | 602 | }, | 
|  | 603 | }, | 
|  | 604 | size: { | 
|  | 605 | cflags: ["-DSIZE=%s"], | 
|  | 606 | }, | 
|  | 607 | }, | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | acme_test_defaults { | 
|  | 611 | name: "board_defaults", | 
|  | 612 | soong_config_variables: { | 
|  | 613 | board: { | 
|  | 614 | soc_a: { | 
|  | 615 | cflags: ["-DSOC_A"], | 
|  | 616 | }, | 
|  | 617 | }, | 
|  | 618 | }, | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | acme_test_defaults { | 
|  | 622 | name: "size_defaults", | 
|  | 623 | soong_config_variables: { | 
|  | 624 | size: { | 
|  | 625 | cflags: ["-DSIZE=%s"], | 
|  | 626 | }, | 
|  | 627 | }, | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | test { | 
|  | 631 | name: "board_and_size_with_defaults", | 
|  | 632 | defaults: ["board_defaults", "size_defaults"], | 
|  | 633 | } | 
|  | 634 | ` | 
|  | 635 |  | 
|  | 636 | fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { | 
|  | 637 | return FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 638 | variables.VendorVars = vars | 
|  | 639 | }) | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | preparer := fixtureForVendorVars(map[string]map[string]string{ | 
|  | 643 | "acme": { | 
|  | 644 | "board":    "soc_a", | 
|  | 645 | "size":     "42", | 
|  | 646 | "feature1": "true", | 
|  | 647 | "feature2": "false", | 
|  | 648 | // FEATURE3 unset | 
|  | 649 | "unused_feature":    "true", // unused | 
|  | 650 | "unused_size":       "1",    // unused | 
|  | 651 | "unused_string_var": "a",    // unused | 
|  | 652 | "always_true":       "true", | 
|  | 653 | }, | 
|  | 654 | }) | 
|  | 655 |  | 
|  | 656 | t.Run("soong config trace hash", func(t *testing.T) { | 
|  | 657 | result := GroupFixturePreparers( | 
|  | 658 | preparer, | 
|  | 659 | PrepareForTestWithDefaults, | 
|  | 660 | PrepareForTestWithSoongConfigModuleBuildComponents, | 
|  | 661 | prepareForSoongConfigTestModule, | 
|  | 662 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 663 | ctx.FinalDepsMutators(registerSoongConfigTraceMutator) | 
|  | 664 | }), | 
|  | 665 | FixtureWithRootAndroidBp(bp), | 
|  | 666 | ).RunTest(t) | 
|  | 667 |  | 
|  | 668 | // Hashes of modules not using soong config should be empty | 
|  | 669 | normal := result.ModuleForTests("normal", "").Module().(*soongConfigTestModule) | 
|  | 670 | AssertDeepEquals(t, "normal hash", normal.base().commonProperties.SoongConfigTraceHash, "") | 
|  | 671 | AssertDeepEquals(t, "normal hash out", normal.outputPath.RelativeToTop().String(), "out/soong/.intermediates/normal/test") | 
|  | 672 |  | 
|  | 673 | board1 := result.ModuleForTests("board_1", "").Module().(*soongConfigTestModule) | 
|  | 674 | board2 := result.ModuleForTests("board_2", "").Module().(*soongConfigTestModule) | 
|  | 675 | size := result.ModuleForTests("size", "").Module().(*soongConfigTestModule) | 
|  | 676 |  | 
|  | 677 | // Trace mutator sets soong config trace hash correctly | 
|  | 678 | board1Hash := board1.base().commonProperties.SoongConfigTrace.hash() | 
|  | 679 | board1Output := board1.outputPath.RelativeToTop().String() | 
|  | 680 | AssertDeepEquals(t, "board hash calc", board1Hash, board1.base().commonProperties.SoongConfigTraceHash) | 
|  | 681 | AssertDeepEquals(t, "board hash path", board1Output, filepath.Join("out/soong/.intermediates/board_1", board1Hash, "test")) | 
|  | 682 |  | 
|  | 683 | sizeHash := size.base().commonProperties.SoongConfigTrace.hash() | 
|  | 684 | sizeOutput := size.outputPath.RelativeToTop().String() | 
|  | 685 | AssertDeepEquals(t, "size hash calc", sizeHash, size.base().commonProperties.SoongConfigTraceHash) | 
|  | 686 | AssertDeepEquals(t, "size hash path", sizeOutput, filepath.Join("out/soong/.intermediates/size", sizeHash, "test")) | 
|  | 687 |  | 
|  | 688 | // Trace should be identical for modules using the same set of variables | 
|  | 689 | AssertDeepEquals(t, "board trace", board1.base().commonProperties.SoongConfigTrace, board2.base().commonProperties.SoongConfigTrace) | 
|  | 690 | AssertDeepEquals(t, "board hash", board1.base().commonProperties.SoongConfigTraceHash, board2.base().commonProperties.SoongConfigTraceHash) | 
|  | 691 |  | 
|  | 692 | // Trace hash should be different for different sets of soong variables | 
|  | 693 | AssertBoolEquals(t, "board hash not equal to size hash", board1.base().commonProperties.SoongConfigTraceHash == size.commonProperties.SoongConfigTraceHash, false) | 
|  | 694 |  | 
|  | 695 | boardSize := result.ModuleForTests("board_and_size", "").Module().(*soongConfigTestModule) | 
|  | 696 | boardSizeDefaults := result.ModuleForTests("board_and_size_with_defaults", "").Module() | 
|  | 697 |  | 
|  | 698 | // Trace should propagate | 
|  | 699 | AssertDeepEquals(t, "board_size hash calc", boardSize.base().commonProperties.SoongConfigTrace.hash(), boardSize.base().commonProperties.SoongConfigTraceHash) | 
|  | 700 | AssertDeepEquals(t, "board_size trace", boardSize.base().commonProperties.SoongConfigTrace, boardSizeDefaults.base().commonProperties.SoongConfigTrace) | 
|  | 701 | AssertDeepEquals(t, "board_size hash", boardSize.base().commonProperties.SoongConfigTraceHash, boardSizeDefaults.base().commonProperties.SoongConfigTraceHash) | 
|  | 702 | }) | 
|  | 703 | } |