blob: 1cf060dc04bb3bfa27f4b19294691ba3719009a5 [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) {
41 configBp := `
42 soong_config_module_type {
43 name: "acme_test_defaults",
44 module_type: "test_defaults",
45 config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -070046 variables: ["board", "feature1", "FEATURE3"],
47 bool_variables: ["feature2"],
Colin Cross9d34f352019-11-22 16:03:51 -080048 properties: ["cflags", "srcs"],
49 }
50
51 soong_config_string_variable {
52 name: "board",
53 values: ["soc_a", "soc_b"],
54 }
55
56 soong_config_bool_variable {
57 name: "feature1",
58 }
59
60 soong_config_bool_variable {
Colin Cross3beeb1e2020-02-05 16:27:47 -080061 name: "FEATURE3",
Colin Cross9d34f352019-11-22 16:03:51 -080062 }
63 `
64
65 importBp := `
66 soong_config_module_type_import {
67 from: "SoongConfig.bp",
68 module_types: ["acme_test_defaults"],
69 }
70 `
71
72 bp := `
73 acme_test_defaults {
74 name: "foo",
75 cflags: ["-DGENERIC"],
76 soong_config_variables: {
77 board: {
78 soc_a: {
79 cflags: ["-DSOC_A"],
80 },
81 soc_b: {
82 cflags: ["-DSOC_B"],
83 },
84 },
85 feature1: {
86 cflags: ["-DFEATURE1"],
87 },
88 feature2: {
89 cflags: ["-DFEATURE2"],
90 },
Colin Cross3beeb1e2020-02-05 16:27:47 -080091 FEATURE3: {
Colin Cross9d34f352019-11-22 16:03:51 -080092 cflags: ["-DFEATURE3"],
93 },
94 },
95 }
96 `
97
98 run := func(t *testing.T, bp string, fs map[string][]byte) {
99 config := TestConfig(buildDir, nil, bp, fs)
100
101 config.TestProductVariables.VendorVars = map[string]map[string]string{
102 "acme": map[string]string{
103 "board": "soc_a",
104 "feature1": "true",
105 "feature2": "false",
106 // FEATURE3 unset
107 },
108 }
109
110 ctx := NewTestContext()
111 ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
112 ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
113 ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
114 ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
115 ctx.RegisterModuleType("test_defaults", soongConfigTestModuleFactory)
116 ctx.Register(config)
117
118 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
119 FailIfErrored(t, errs)
120 _, errs = ctx.PrepareBuildActions(config)
121 FailIfErrored(t, errs)
122
123 foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
124 if g, w := foo.props.Cflags, []string{"-DGENERIC", "-DSOC_A", "-DFEATURE1"}; !reflect.DeepEqual(g, w) {
125 t.Errorf("wanted foo cflags %q, got %q", w, g)
126 }
127 }
128
129 t.Run("single file", func(t *testing.T) {
130 run(t, configBp+bp, nil)
131 })
132
133 t.Run("import", func(t *testing.T) {
134 run(t, importBp+bp, map[string][]byte{
135 "SoongConfig.bp": []byte(configBp),
136 })
137 })
138}