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 | |
| 22 | type soongConfigTestModule struct { |
| 23 | ModuleBase |
| 24 | props soongConfigTestModuleProperties |
| 25 | } |
| 26 | |
| 27 | type soongConfigTestModuleProperties struct { |
| 28 | Cflags []string |
| 29 | } |
| 30 | |
| 31 | func soongConfigTestModuleFactory() Module { |
| 32 | m := &soongConfigTestModule{} |
| 33 | m.AddProperties(&m.props) |
| 34 | InitAndroidModule(m) |
| 35 | return m |
| 36 | } |
| 37 | |
| 38 | func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {} |
| 39 | |
| 40 | func TestSoongConfigModule(t *testing.T) { |
| 41 | configBp := ` |
| 42 | soong_config_module_type { |
| 43 | name: "acme_test_defaults", |
| 44 | module_type: "test_defaults", |
| 45 | config_namespace: "acme", |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame^] | 46 | variables: ["board", "feature1", "feature2", "FEATURE3"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 47 | properties: ["cflags", "srcs"], |
| 48 | } |
| 49 | |
| 50 | soong_config_string_variable { |
| 51 | name: "board", |
| 52 | values: ["soc_a", "soc_b"], |
| 53 | } |
| 54 | |
| 55 | soong_config_bool_variable { |
| 56 | name: "feature1", |
| 57 | } |
| 58 | |
| 59 | soong_config_bool_variable { |
| 60 | name: "feature2", |
| 61 | } |
| 62 | |
| 63 | soong_config_bool_variable { |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame^] | 64 | name: "FEATURE3", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 65 | } |
| 66 | ` |
| 67 | |
| 68 | importBp := ` |
| 69 | soong_config_module_type_import { |
| 70 | from: "SoongConfig.bp", |
| 71 | module_types: ["acme_test_defaults"], |
| 72 | } |
| 73 | ` |
| 74 | |
| 75 | bp := ` |
| 76 | acme_test_defaults { |
| 77 | name: "foo", |
| 78 | cflags: ["-DGENERIC"], |
| 79 | soong_config_variables: { |
| 80 | board: { |
| 81 | soc_a: { |
| 82 | cflags: ["-DSOC_A"], |
| 83 | }, |
| 84 | soc_b: { |
| 85 | cflags: ["-DSOC_B"], |
| 86 | }, |
| 87 | }, |
| 88 | feature1: { |
| 89 | cflags: ["-DFEATURE1"], |
| 90 | }, |
| 91 | feature2: { |
| 92 | cflags: ["-DFEATURE2"], |
| 93 | }, |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame^] | 94 | FEATURE3: { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 95 | cflags: ["-DFEATURE3"], |
| 96 | }, |
| 97 | }, |
| 98 | } |
| 99 | ` |
| 100 | |
| 101 | run := func(t *testing.T, bp string, fs map[string][]byte) { |
| 102 | config := TestConfig(buildDir, nil, bp, fs) |
| 103 | |
| 104 | config.TestProductVariables.VendorVars = map[string]map[string]string{ |
| 105 | "acme": map[string]string{ |
| 106 | "board": "soc_a", |
| 107 | "feature1": "true", |
| 108 | "feature2": "false", |
| 109 | // FEATURE3 unset |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | ctx := NewTestContext() |
| 114 | ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) |
| 115 | ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) |
| 116 | ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) |
| 117 | ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) |
| 118 | ctx.RegisterModuleType("test_defaults", soongConfigTestModuleFactory) |
| 119 | ctx.Register(config) |
| 120 | |
| 121 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 122 | FailIfErrored(t, errs) |
| 123 | _, errs = ctx.PrepareBuildActions(config) |
| 124 | FailIfErrored(t, errs) |
| 125 | |
| 126 | foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule) |
| 127 | if g, w := foo.props.Cflags, []string{"-DGENERIC", "-DSOC_A", "-DFEATURE1"}; !reflect.DeepEqual(g, w) { |
| 128 | t.Errorf("wanted foo cflags %q, got %q", w, g) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | t.Run("single file", func(t *testing.T) { |
| 133 | run(t, configBp+bp, nil) |
| 134 | }) |
| 135 | |
| 136 | t.Run("import", func(t *testing.T) { |
| 137 | run(t, importBp+bp, map[string][]byte{ |
| 138 | "SoongConfig.bp": []byte(configBp), |
| 139 | }) |
| 140 | }) |
| 141 | } |