xref: /aosp_15_r20/build/soong/aconfig/codegen/cc_aconfig_library_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2023 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 codegen
16
17import (
18	"fmt"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/cc"
23
24	"github.com/google/blueprint"
25)
26
27var ccCodegenModeTestData = []struct {
28	setting, expected string
29}{
30	{"", "production"},
31	{"mode: `production`,", "production"},
32	{"mode: `test`,", "test"},
33	{"mode: `exported`,", "exported"},
34	{"mode: `force-read-only`,", "force-read-only"},
35}
36
37func TestCCCodegenMode(t *testing.T) {
38	for _, testData := range ccCodegenModeTestData {
39		testCCCodegenModeHelper(t, testData.setting, testData.expected)
40	}
41}
42
43func testCCCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
44	t.Helper()
45	result := android.GroupFixturePreparers(
46		PrepareForTestWithAconfigBuildComponents,
47		cc.PrepareForTestWithCcDefaultModules).
48		ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
49		RunTestWithBp(t, fmt.Sprintf(`
50			aconfig_declarations {
51				name: "my_aconfig_declarations",
52				package: "com.example.package",
53				container: "com.android.foo",
54				srcs: ["foo.aconfig"],
55			}
56
57			cc_library {
58    		name: "server_configurable_flags",
59    		srcs: ["server_configurable_flags.cc"],
60			}
61
62			cc_library {
63    		name: "libbase",
64    		srcs: ["libbase.cc"],
65			}
66
67			cc_library {
68    		name: "liblog",
69    		srcs: ["liblog.cc"],
70			}
71
72			cc_library {
73    		name: "libaconfig_storage_read_api_cc",
74    		srcs: ["libaconfig_storage_read_api_cc.cc"],
75			}
76
77			cc_aconfig_library {
78				name: "my_cc_aconfig_library",
79				aconfig_declarations: "my_aconfig_declarations",
80				%s
81			}
82		`, bpMode))
83
84	module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
85	rule := module.Rule("cc_aconfig_library")
86	android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
87}
88
89var incorrectCCCodegenModeTestData = []struct {
90	setting, expectedErr string
91}{
92	{"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
93}
94
95func TestIncorrectCCCodegenMode(t *testing.T) {
96	for _, testData := range incorrectCCCodegenModeTestData {
97		testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
98	}
99}
100
101func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
102	t.Helper()
103	android.GroupFixturePreparers(
104		PrepareForTestWithAconfigBuildComponents,
105		cc.PrepareForTestWithCcDefaultModules).
106		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
107		RunTestWithBp(t, fmt.Sprintf(`
108			aconfig_declarations {
109				name: "my_aconfig_declarations",
110				package: "com.example.package",
111				container: "com.android.foo",
112				srcs: ["foo.aconfig"],
113			}
114
115			cc_library {
116    		name: "server_configurable_flags",
117    		srcs: ["server_configurable_flags.cc"],
118			}
119
120			cc_library {
121    		name: "libbase",
122    		srcs: ["libbase.cc"],
123			}
124
125			cc_library {
126    		name: "liblog",
127    		srcs: ["liblog.cc"],
128			}
129
130			cc_library {
131    		name: "libaconfig_storage_read_api_cc",
132    		srcs: ["libaconfig_storage_read_api_cc.cc"],
133			}
134
135			cc_aconfig_library {
136				name: "my_cc_aconfig_library",
137				aconfig_declarations: "my_aconfig_declarations",
138				%s
139			}
140		`, bpMode))
141}
142
143func TestAndroidMkCcLibrary(t *testing.T) {
144	bp := `
145		aconfig_declarations {
146			name: "my_aconfig_declarations_foo",
147			package: "com.example.package",
148			srcs: ["foo.aconfig"],
149			container: "vendor",
150		}
151
152		cc_aconfig_library {
153			name: "my_cc_aconfig_library_foo",
154			aconfig_declarations: "my_aconfig_declarations_foo",
155			vendor_available: true,
156		}
157
158		aconfig_declarations {
159			name: "my_aconfig_declarations_bar",
160			package: "com.example.package",
161			container: "com.android.foo",
162			srcs: ["bar.aconfig"],
163		}
164
165		cc_aconfig_library {
166			name: "my_cc_aconfig_library_bar",
167			aconfig_declarations: "my_aconfig_declarations_bar",
168			vendor_available: true,
169		}
170
171		cc_library {
172			name: "my_cc_library",
173			srcs: [
174				"src/foo.cc",
175			],
176			static_libs: [
177				"my_cc_aconfig_library_foo",
178				"my_cc_aconfig_library_bar",
179			],
180			vendor: true,
181		}
182
183		cc_library {
184			name: "server_configurable_flags",
185			srcs: ["server_configurable_flags.cc"],
186			vendor_available: true,
187		}
188
189		cc_library {
190			name: "libbase",
191			srcs: ["libbase.cc"],
192			vendor_available: true,
193		}
194
195		cc_library {
196			name: "liblog",
197			srcs: ["liblog.cc"],
198			vendor_available: true,
199		}
200
201		cc_library {
202			name: "libaconfig_storage_read_api_cc",
203			srcs: ["libaconfig_storage_read_api_cc.cc"],
204			vendor_available: true,
205		}
206	`
207	result := android.GroupFixturePreparers(
208		PrepareForTestWithAconfigBuildComponents,
209		cc.PrepareForTestWithCcDefaultModules).
210		ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
211
212	module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
213
214	entry := android.AndroidMkInfoForTest(t, result.TestContext, module).PrimaryInfo
215
216	makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
217	android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
218}
219
220func TestForceReadOnly(t *testing.T) {
221	t.Helper()
222	result := android.GroupFixturePreparers(
223		PrepareForTestWithAconfigBuildComponents,
224		cc.PrepareForTestWithCcDefaultModules).
225		ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
226		RunTestWithBp(t, fmt.Sprintf(`
227			aconfig_declarations {
228				name: "my_aconfig_declarations",
229				package: "com.example.package",
230				container: "com.android.foo",
231				srcs: ["foo.aconfig"],
232			}
233
234			cc_aconfig_library {
235				name: "my_cc_aconfig_library",
236				aconfig_declarations: "my_aconfig_declarations",
237				mode: "force-read-only",
238			}
239
240
241			cc_library {
242    		                name: "libbase",
243    		                srcs: ["libbase.cc"],
244			}
245
246			cc_library {
247    		                name: "liblog",
248    		                srcs: ["liblog.cc"],
249			}
250
251			cc_library {
252    		                name: "libaconfig_storage_read_api_cc",
253    		                srcs: ["libaconfig_storage_read_api_cc.cc"],
254			}
255		`))
256
257	module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module()
258	dependOnBaseLib := false
259	result.VisitDirectDeps(module, func(dep blueprint.Module) {
260		if dep.Name() == baseLibDep {
261			dependOnBaseLib = true
262		}
263	})
264	android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags",
265		dependOnBaseLib, false)
266}
267