blob: d01d13b6131d729646a4be0bb3ed27f9d11d02da [file] [log] [blame]
Dennis Shenc5e39f52023-09-14 18:52:49 +00001// 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
Yu Liueae7b362023-11-16 17:05:47 -080015package codegen
Dennis Shenc5e39f52023-09-14 18:52:49 +000016
17import (
18 "fmt"
19 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
Dennis Shenc6dc5512024-01-10 14:07:35 +000023
24 "github.com/google/blueprint"
Dennis Shenc5e39f52023-09-14 18:52:49 +000025)
26
Zi Wang275f6542023-11-09 14:59:31 -080027var ccCodegenModeTestData = []struct {
Dennis Shenc5e39f52023-09-14 18:52:49 +000028 setting, expected string
29}{
30 {"", "production"},
Zi Wang275f6542023-11-09 14:59:31 -080031 {"mode: `production`,", "production"},
32 {"mode: `test`,", "test"},
33 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000034 {"mode: `force-read-only`,", "force-read-only"},
Dennis Shenc5e39f52023-09-14 18:52:49 +000035}
36
37func TestCCCodegenMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -080038 for _, testData := range ccCodegenModeTestData {
Dennis Shenc5e39f52023-09-14 18:52:49 +000039 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",
Yu Liu315a53c2024-04-24 16:41:57 +000053 container: "com.android.foo",
Dennis Shenc5e39f52023-09-14 18:52:49 +000054 srcs: ["foo.aconfig"],
55 }
56
57 cc_library {
58 name: "server_configurable_flags",
59 srcs: ["server_configurable_flags.cc"],
60 }
61
Ted Bauerf0f18592024-04-23 18:25:26 +000062 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_library {
78 name: "libaconfig_storage_protos_cc",
79 srcs: ["libaconfig_storage_protos_cc.cc"],
80 }
81
Dennis Shenc5e39f52023-09-14 18:52:49 +000082 cc_aconfig_library {
83 name: "my_cc_aconfig_library",
84 aconfig_declarations: "my_aconfig_declarations",
85 %s
86 }
87 `, bpMode))
88
89 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
90 rule := module.Rule("cc_aconfig_library")
91 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
92}
Zi Wang275f6542023-11-09 14:59:31 -080093
94var incorrectCCCodegenModeTestData = []struct {
95 setting, expectedErr string
96}{
97 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080098}
99
100func TestIncorrectCCCodegenMode(t *testing.T) {
101 for _, testData := range incorrectCCCodegenModeTestData {
102 testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
103 }
104}
105
106func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
107 t.Helper()
108 android.GroupFixturePreparers(
109 PrepareForTestWithAconfigBuildComponents,
110 cc.PrepareForTestWithCcDefaultModules).
111 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
112 RunTestWithBp(t, fmt.Sprintf(`
113 aconfig_declarations {
114 name: "my_aconfig_declarations",
115 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000116 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -0800117 srcs: ["foo.aconfig"],
118 }
119
120 cc_library {
121 name: "server_configurable_flags",
122 srcs: ["server_configurable_flags.cc"],
123 }
124
Ted Bauerf0f18592024-04-23 18:25:26 +0000125 cc_library {
126 name: "libbase",
127 srcs: ["libbase.cc"],
128 }
129
130 cc_library {
131 name: "liblog",
132 srcs: ["liblog.cc"],
133 }
134
135 cc_library {
136 name: "libaconfig_storage_read_api_cc",
137 srcs: ["libaconfig_storage_read_api_cc.cc"],
138 }
139
140 cc_library {
141 name: "libaconfig_storage_protos_cc",
142 srcs: ["libaconfig_storage_protos_cc.cc"],
143 }
144
145
Zi Wang275f6542023-11-09 14:59:31 -0800146 cc_aconfig_library {
147 name: "my_cc_aconfig_library",
148 aconfig_declarations: "my_aconfig_declarations",
149 %s
150 }
151 `, bpMode))
152}
Yu Liu6dc93f92023-12-14 01:19:35 +0000153
154func TestAndroidMkCcLibrary(t *testing.T) {
155 bp := `
156 aconfig_declarations {
157 name: "my_aconfig_declarations_foo",
158 package: "com.example.package",
159 srcs: ["foo.aconfig"],
160 container: "vendor",
161 }
162
163 cc_aconfig_library {
164 name: "my_cc_aconfig_library_foo",
165 aconfig_declarations: "my_aconfig_declarations_foo",
166 vendor_available: true,
167 }
168
169 aconfig_declarations {
170 name: "my_aconfig_declarations_bar",
171 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000172 container: "com.android.foo",
Yu Liu6dc93f92023-12-14 01:19:35 +0000173 srcs: ["bar.aconfig"],
174 }
175
176 cc_aconfig_library {
177 name: "my_cc_aconfig_library_bar",
178 aconfig_declarations: "my_aconfig_declarations_bar",
179 vendor_available: true,
180 }
181
182 cc_library {
183 name: "my_cc_library",
184 srcs: [
185 "src/foo.cc",
186 ],
187 static_libs: [
188 "my_cc_aconfig_library_foo",
189 "my_cc_aconfig_library_bar",
190 ],
191 vendor: true,
192 }
193
194 cc_library {
195 name: "server_configurable_flags",
196 srcs: ["server_configurable_flags.cc"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900197 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000198 }
Ted Bauerf0f18592024-04-23 18:25:26 +0000199
200 cc_library {
201 name: "libbase",
202 srcs: ["libbase.cc"],
203 vendor_available: true,
204 }
205
206 cc_library {
207 name: "liblog",
208 srcs: ["liblog.cc"],
209 vendor_available: true,
210 }
211
212 cc_library {
213 name: "libaconfig_storage_read_api_cc",
214 srcs: ["libaconfig_storage_read_api_cc.cc"],
215 vendor_available: true,
216 }
217
218 cc_library {
219 name: "libaconfig_storage_protos_cc",
220 srcs: ["libaconfig_storage_protos_cc.cc"],
221 vendor_available: true,
222 }
Yu Liu6dc93f92023-12-14 01:19:35 +0000223 `
224 result := android.GroupFixturePreparers(
225 PrepareForTestWithAconfigBuildComponents,
226 cc.PrepareForTestWithCcDefaultModules).
227 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
228
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900229 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000230
231 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
232
233 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Yu Liu6dc93f92023-12-14 01:19:35 +0000234 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
235}
Dennis Shenc6dc5512024-01-10 14:07:35 +0000236
237func TestForceReadOnly(t *testing.T) {
238 t.Helper()
239 result := android.GroupFixturePreparers(
240 PrepareForTestWithAconfigBuildComponents,
241 cc.PrepareForTestWithCcDefaultModules).
242 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
243 RunTestWithBp(t, fmt.Sprintf(`
244 aconfig_declarations {
245 name: "my_aconfig_declarations",
246 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000247 container: "com.android.foo",
Dennis Shenc6dc5512024-01-10 14:07:35 +0000248 srcs: ["foo.aconfig"],
249 }
250
251 cc_aconfig_library {
252 name: "my_cc_aconfig_library",
253 aconfig_declarations: "my_aconfig_declarations",
254 mode: "force-read-only",
255 }
Ted Bauerc3031c52024-05-01 00:30:17 +0000256
257
258 cc_library {
259 name: "libbase",
260 srcs: ["libbase.cc"],
261 }
262
263 cc_library {
264 name: "liblog",
265 srcs: ["liblog.cc"],
266 }
267
268 cc_library {
269 name: "libaconfig_storage_read_api_cc",
270 srcs: ["libaconfig_storage_read_api_cc.cc"],
271 }
Dennis Shenc6dc5512024-01-10 14:07:35 +0000272 `))
273
274 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module()
275 dependOnBaseLib := false
276 result.VisitDirectDeps(module, func(dep blueprint.Module) {
277 if dep.Name() == baseLibDep {
278 dependOnBaseLib = true
279 }
280 })
281 android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags",
282 dependOnBaseLib, false)
283}