blob: 2f6c1a643dd301ad22ea70abae83c1e9998bc376 [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
Dennis Shenc5e39f52023-09-14 18:52:49 +000077 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}
Zi Wang275f6542023-11-09 14:59:31 -080088
89var incorrectCCCodegenModeTestData = []struct {
90 setting, expectedErr string
91}{
92 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080093}
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",
Yu Liu315a53c2024-04-24 16:41:57 +0000111 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -0800112 srcs: ["foo.aconfig"],
113 }
114
115 cc_library {
116 name: "server_configurable_flags",
117 srcs: ["server_configurable_flags.cc"],
118 }
119
Ted Bauerf0f18592024-04-23 18:25:26 +0000120 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
Zi Wang275f6542023-11-09 14:59:31 -0800135 cc_aconfig_library {
136 name: "my_cc_aconfig_library",
137 aconfig_declarations: "my_aconfig_declarations",
138 %s
139 }
140 `, bpMode))
141}
Yu Liu6dc93f92023-12-14 01:19:35 +0000142
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",
Yu Liu315a53c2024-04-24 16:41:57 +0000161 container: "com.android.foo",
Yu Liu6dc93f92023-12-14 01:19:35 +0000162 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"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900186 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000187 }
Ted Bauerf0f18592024-04-23 18:25:26 +0000188
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 }
Yu Liu6dc93f92023-12-14 01:19:35 +0000206 `
207 result := android.GroupFixturePreparers(
208 PrepareForTestWithAconfigBuildComponents,
209 cc.PrepareForTestWithCcDefaultModules).
210 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
211
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900212 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000213
214 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
215
216 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Yu Liu6dc93f92023-12-14 01:19:35 +0000217 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
218}
Dennis Shenc6dc5512024-01-10 14:07:35 +0000219
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",
Yu Liu315a53c2024-04-24 16:41:57 +0000230 container: "com.android.foo",
Dennis Shenc6dc5512024-01-10 14:07:35 +0000231 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 }
Ted Bauerc3031c52024-05-01 00:30:17 +0000239
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 }
Dennis Shenc6dc5512024-01-10 14:07:35 +0000255 `))
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}