blob: 2e7fdc2c114425632efb1f2142abdac1087c4138 [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",
53 srcs: ["foo.aconfig"],
54 }
55
56 cc_library {
57 name: "server_configurable_flags",
58 srcs: ["server_configurable_flags.cc"],
59 }
60
Ted Bauerf0f18592024-04-23 18:25:26 +000061 cc_library {
62 name: "libbase",
63 srcs: ["libbase.cc"],
64 }
65
66 cc_library {
67 name: "liblog",
68 srcs: ["liblog.cc"],
69 }
70
71 cc_library {
72 name: "libaconfig_storage_read_api_cc",
73 srcs: ["libaconfig_storage_read_api_cc.cc"],
74 }
75
76 cc_library {
77 name: "libaconfig_storage_protos_cc",
78 srcs: ["libaconfig_storage_protos_cc.cc"],
79 }
80
Dennis Shenc5e39f52023-09-14 18:52:49 +000081 cc_aconfig_library {
82 name: "my_cc_aconfig_library",
83 aconfig_declarations: "my_aconfig_declarations",
84 %s
85 }
86 `, bpMode))
87
88 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
89 rule := module.Rule("cc_aconfig_library")
90 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
91}
Zi Wang275f6542023-11-09 14:59:31 -080092
93var incorrectCCCodegenModeTestData = []struct {
94 setting, expectedErr string
95}{
96 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080097}
98
99func TestIncorrectCCCodegenMode(t *testing.T) {
100 for _, testData := range incorrectCCCodegenModeTestData {
101 testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
102 }
103}
104
105func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
106 t.Helper()
107 android.GroupFixturePreparers(
108 PrepareForTestWithAconfigBuildComponents,
109 cc.PrepareForTestWithCcDefaultModules).
110 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
111 RunTestWithBp(t, fmt.Sprintf(`
112 aconfig_declarations {
113 name: "my_aconfig_declarations",
114 package: "com.example.package",
115 srcs: ["foo.aconfig"],
116 }
117
118 cc_library {
119 name: "server_configurable_flags",
120 srcs: ["server_configurable_flags.cc"],
121 }
122
Ted Bauerf0f18592024-04-23 18:25:26 +0000123 cc_library {
124 name: "libbase",
125 srcs: ["libbase.cc"],
126 }
127
128 cc_library {
129 name: "liblog",
130 srcs: ["liblog.cc"],
131 }
132
133 cc_library {
134 name: "libaconfig_storage_read_api_cc",
135 srcs: ["libaconfig_storage_read_api_cc.cc"],
136 }
137
138 cc_library {
139 name: "libaconfig_storage_protos_cc",
140 srcs: ["libaconfig_storage_protos_cc.cc"],
141 }
142
143
Zi Wang275f6542023-11-09 14:59:31 -0800144 cc_aconfig_library {
145 name: "my_cc_aconfig_library",
146 aconfig_declarations: "my_aconfig_declarations",
147 %s
148 }
149 `, bpMode))
150}
Yu Liu6dc93f92023-12-14 01:19:35 +0000151
152func TestAndroidMkCcLibrary(t *testing.T) {
153 bp := `
154 aconfig_declarations {
155 name: "my_aconfig_declarations_foo",
156 package: "com.example.package",
157 srcs: ["foo.aconfig"],
158 container: "vendor",
159 }
160
161 cc_aconfig_library {
162 name: "my_cc_aconfig_library_foo",
163 aconfig_declarations: "my_aconfig_declarations_foo",
164 vendor_available: true,
165 }
166
167 aconfig_declarations {
168 name: "my_aconfig_declarations_bar",
169 package: "com.example.package",
170 srcs: ["bar.aconfig"],
171 }
172
173 cc_aconfig_library {
174 name: "my_cc_aconfig_library_bar",
175 aconfig_declarations: "my_aconfig_declarations_bar",
176 vendor_available: true,
177 }
178
179 cc_library {
180 name: "my_cc_library",
181 srcs: [
182 "src/foo.cc",
183 ],
184 static_libs: [
185 "my_cc_aconfig_library_foo",
186 "my_cc_aconfig_library_bar",
187 ],
188 vendor: true,
189 }
190
191 cc_library {
192 name: "server_configurable_flags",
193 srcs: ["server_configurable_flags.cc"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900194 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000195 }
Ted Bauerf0f18592024-04-23 18:25:26 +0000196
197 cc_library {
198 name: "libbase",
199 srcs: ["libbase.cc"],
200 vendor_available: true,
201 }
202
203 cc_library {
204 name: "liblog",
205 srcs: ["liblog.cc"],
206 vendor_available: true,
207 }
208
209 cc_library {
210 name: "libaconfig_storage_read_api_cc",
211 srcs: ["libaconfig_storage_read_api_cc.cc"],
212 vendor_available: true,
213 }
214
215 cc_library {
216 name: "libaconfig_storage_protos_cc",
217 srcs: ["libaconfig_storage_protos_cc.cc"],
218 vendor_available: true,
219 }
Yu Liu6dc93f92023-12-14 01:19:35 +0000220 `
221 result := android.GroupFixturePreparers(
222 PrepareForTestWithAconfigBuildComponents,
223 cc.PrepareForTestWithCcDefaultModules).
224 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
225
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900226 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000227
228 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
229
230 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Yu Liu6dc93f92023-12-14 01:19:35 +0000231 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
232}
Dennis Shenc6dc5512024-01-10 14:07:35 +0000233
234func TestForceReadOnly(t *testing.T) {
235 t.Helper()
236 result := android.GroupFixturePreparers(
237 PrepareForTestWithAconfigBuildComponents,
238 cc.PrepareForTestWithCcDefaultModules).
239 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
240 RunTestWithBp(t, fmt.Sprintf(`
241 aconfig_declarations {
242 name: "my_aconfig_declarations",
243 package: "com.example.package",
244 srcs: ["foo.aconfig"],
245 }
246
247 cc_aconfig_library {
248 name: "my_cc_aconfig_library",
249 aconfig_declarations: "my_aconfig_declarations",
250 mode: "force-read-only",
251 }
252 `))
253
254 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module()
255 dependOnBaseLib := false
256 result.VisitDirectDeps(module, func(dep blueprint.Module) {
257 if dep.Name() == baseLibDep {
258 dependOnBaseLib = true
259 }
260 })
261 android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags",
262 dependOnBaseLib, false)
263}