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 ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 22 | type soongConfigTestDefaultsModuleProperties struct { |
| 23 | } |
| 24 | |
| 25 | type soongConfigTestDefaultsModule struct { |
| 26 | ModuleBase |
| 27 | DefaultsModuleBase |
| 28 | } |
| 29 | |
| 30 | func soongConfigTestDefaultsModuleFactory() Module { |
| 31 | m := &soongConfigTestDefaultsModule{} |
| 32 | m.AddProperties(&soongConfigTestModuleProperties{}) |
| 33 | InitDefaultsModule(m) |
| 34 | return m |
| 35 | } |
| 36 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 37 | type soongConfigTestModule struct { |
| 38 | ModuleBase |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 39 | DefaultableModuleBase |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 40 | props soongConfigTestModuleProperties |
| 41 | } |
| 42 | |
| 43 | type soongConfigTestModuleProperties struct { |
| 44 | Cflags []string |
| 45 | } |
| 46 | |
| 47 | func soongConfigTestModuleFactory() Module { |
| 48 | m := &soongConfigTestModule{} |
| 49 | m.AddProperties(&m.props) |
| 50 | InitAndroidModule(m) |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 51 | InitDefaultableModule(m) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 52 | return m |
| 53 | } |
| 54 | |
| 55 | func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {} |
| 56 | |
| 57 | func TestSoongConfigModule(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame] | 58 | t.Parallel() |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 59 | configBp := ` |
| 60 | soong_config_module_type { |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 61 | name: "acme_test", |
| 62 | module_type: "test", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 63 | config_namespace: "acme", |
Dan Willemsen | 2b8b89c | 2020-03-23 19:39:34 -0700 | [diff] [blame] | 64 | variables: ["board", "feature1", "FEATURE3"], |
| 65 | bool_variables: ["feature2"], |
Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 66 | value_variables: ["size"], |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 67 | properties: ["cflags", "srcs", "defaults"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 68 | } |
| 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 Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 80 | name: "FEATURE3", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 81 | } |
| 82 | ` |
| 83 | |
| 84 | importBp := ` |
| 85 | soong_config_module_type_import { |
| 86 | from: "SoongConfig.bp", |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 87 | module_types: ["acme_test"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 88 | } |
| 89 | ` |
| 90 | |
| 91 | bp := ` |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 92 | test_defaults { |
| 93 | name: "foo_defaults", |
| 94 | cflags: ["DEFAULT"], |
| 95 | } |
| 96 | |
| 97 | acme_test { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 98 | name: "foo", |
| 99 | cflags: ["-DGENERIC"], |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 100 | defaults: ["foo_defaults"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 101 | soong_config_variables: { |
| 102 | board: { |
| 103 | soc_a: { |
| 104 | cflags: ["-DSOC_A"], |
| 105 | }, |
| 106 | soc_b: { |
| 107 | cflags: ["-DSOC_B"], |
| 108 | }, |
| 109 | }, |
Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 110 | size: { |
| 111 | cflags: ["-DSIZE=%s"], |
| 112 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 113 | feature1: { |
| 114 | cflags: ["-DFEATURE1"], |
| 115 | }, |
| 116 | feature2: { |
| 117 | cflags: ["-DFEATURE2"], |
| 118 | }, |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 119 | FEATURE3: { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 120 | cflags: ["-DFEATURE3"], |
| 121 | }, |
| 122 | }, |
| 123 | } |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 124 | |
| 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 Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 164 | ` |
| 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 Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 172 | "size": "42", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 173 | "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 Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 184 | ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) |
| 185 | ctx.RegisterModuleType("test", soongConfigTestModuleFactory) |
| 186 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 187 | 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 Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 194 | basicCFlags := []string{"DEFAULT", "-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"} |
| 195 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 196 | foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule) |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 197 | if g, w := foo.props.Cflags, basicCFlags; !reflect.DeepEqual(g, w) { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 198 | t.Errorf("wanted foo cflags %q, got %q", w, g) |
| 199 | } |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame^] | 200 | |
| 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 Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 206 | } |
| 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 | } |