blob: f0f14626e31fe4c3db42168c89258790447c5063 [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
22type soongConfigTestModule struct {
23 ModuleBase
24 props soongConfigTestModuleProperties
25}
26
27type soongConfigTestModuleProperties struct {
28 Cflags []string
29}
30
31func soongConfigTestModuleFactory() Module {
32 m := &soongConfigTestModule{}
33 m.AddProperties(&m.props)
34 InitAndroidModule(m)
35 return m
36}
37
38func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {}
39
40func TestSoongConfigModule(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070041 t.Parallel()
Colin Cross9d34f352019-11-22 16:03:51 -080042 configBp := `
43 soong_config_module_type {
44 name: "acme_test_defaults",
45 module_type: "test_defaults",
46 config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -070047 variables: ["board", "feature1", "FEATURE3"],
48 bool_variables: ["feature2"],
Dan Willemsenb0935db2020-03-23 19:42:18 -070049 value_variables: ["size"],
Colin Cross9d34f352019-11-22 16:03:51 -080050 properties: ["cflags", "srcs"],
51 }
52
53 soong_config_string_variable {
54 name: "board",
55 values: ["soc_a", "soc_b"],
56 }
57
58 soong_config_bool_variable {
59 name: "feature1",
60 }
61
62 soong_config_bool_variable {
Colin Cross3beeb1e2020-02-05 16:27:47 -080063 name: "FEATURE3",
Colin Cross9d34f352019-11-22 16:03:51 -080064 }
65 `
66
67 importBp := `
68 soong_config_module_type_import {
69 from: "SoongConfig.bp",
70 module_types: ["acme_test_defaults"],
71 }
72 `
73
74 bp := `
75 acme_test_defaults {
76 name: "foo",
77 cflags: ["-DGENERIC"],
78 soong_config_variables: {
79 board: {
80 soc_a: {
81 cflags: ["-DSOC_A"],
82 },
83 soc_b: {
84 cflags: ["-DSOC_B"],
85 },
86 },
Dan Willemsenb0935db2020-03-23 19:42:18 -070087 size: {
88 cflags: ["-DSIZE=%s"],
89 },
Colin Cross9d34f352019-11-22 16:03:51 -080090 feature1: {
91 cflags: ["-DFEATURE1"],
92 },
93 feature2: {
94 cflags: ["-DFEATURE2"],
95 },
Colin Cross3beeb1e2020-02-05 16:27:47 -080096 FEATURE3: {
Colin Cross9d34f352019-11-22 16:03:51 -080097 cflags: ["-DFEATURE3"],
98 },
99 },
100 }
101 `
102
103 run := func(t *testing.T, bp string, fs map[string][]byte) {
104 config := TestConfig(buildDir, nil, bp, fs)
105
106 config.TestProductVariables.VendorVars = map[string]map[string]string{
107 "acme": map[string]string{
108 "board": "soc_a",
Dan Willemsenb0935db2020-03-23 19:42:18 -0700109 "size": "42",
Colin Cross9d34f352019-11-22 16:03:51 -0800110 "feature1": "true",
111 "feature2": "false",
112 // FEATURE3 unset
113 },
114 }
115
116 ctx := NewTestContext()
117 ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
118 ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
119 ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
120 ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
121 ctx.RegisterModuleType("test_defaults", soongConfigTestModuleFactory)
122 ctx.Register(config)
123
124 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
125 FailIfErrored(t, errs)
126 _, errs = ctx.PrepareBuildActions(config)
127 FailIfErrored(t, errs)
128
129 foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
Dan Willemsenb0935db2020-03-23 19:42:18 -0700130 if g, w := foo.props.Cflags, []string{"-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"}; !reflect.DeepEqual(g, w) {
Colin Cross9d34f352019-11-22 16:03:51 -0800131 t.Errorf("wanted foo cflags %q, got %q", w, g)
132 }
133 }
134
135 t.Run("single file", func(t *testing.T) {
136 run(t, configBp+bp, nil)
137 })
138
139 t.Run("import", func(t *testing.T) {
140 run(t, importBp+bp, map[string][]byte{
141 "SoongConfig.bp": []byte(configBp),
142 })
143 })
144}