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