blob: db344decc8e62b58ee071c7127f6f7d7ebf8672d [file] [log] [blame]
Rupert Shuttleworth54e78412021-02-15 11:04:32 +00001// Copyright 2021 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 bp2build
16
17import (
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020018 "testing"
19
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000020 "android/soong/android"
21 "android/soong/cc"
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000022)
23
24const (
25 // See cc/testing.go for more context
Jingwen Chen63930982021-03-24 10:04:33 -040026 soongCcLibraryHeadersPreamble = `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000027cc_defaults {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 name: "libclang_rt.builtins-x86_64-android",
33 defaults: ["linux_bionic_supported"],
34 vendor_available: true,
35 vendor_ramdisk_available: true,
36 product_available: true,
37 recovery_available: true,
38 native_bridge_supported: true,
39 src: "",
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000040}`
41)
42
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020043type bp2buildTestCase struct {
44 description string
45 moduleTypeUnderTest string
46 moduleTypeUnderTestFactory android.ModuleFactory
47 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
48 depsMutators []android.RegisterMutatorFunc
49 blueprint string
50 expectedBazelTargets []string
51 filesystem map[string]string
52 dir string
53}
54
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000055func TestCcLibraryHeadersLoadStatement(t *testing.T) {
56 testCases := []struct {
57 bazelTargets BazelTargets
58 expectedLoadStatements string
59 }{
60 {
61 bazelTargets: BazelTargets{
62 BazelTarget{
63 name: "cc_library_headers_target",
64 ruleClass: "cc_library_headers",
65 // Note: no bzlLoadLocation for native rules
66 },
67 },
68 expectedLoadStatements: ``,
69 },
70 }
71
72 for _, testCase := range testCases {
73 actual := testCase.bazelTargets.LoadStatements()
74 expected := testCase.expectedLoadStatements
75 if actual != expected {
76 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
77 }
78 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000079}
80
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020081func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
82 cc.RegisterCCBuildComponents(ctx)
83 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
84}
85
86func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040087 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020088 runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
89}
90
91func TestCcLibraryHeadersSimple(t *testing.T) {
92 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
93 description: "cc_library_headers test",
94 moduleTypeUnderTest: "cc_library_headers",
95 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
96 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
97 filesystem: map[string]string{
98 "lib-1/lib1a.h": "",
99 "lib-1/lib1b.h": "",
100 "lib-2/lib2a.h": "",
101 "lib-2/lib2b.h": "",
102 "dir-1/dir1a.h": "",
103 "dir-1/dir1b.h": "",
104 "dir-2/dir2a.h": "",
105 "dir-2/dir2b.h": "",
106 "arch_arm64_exported_include_dir/a.h": "",
107 "arch_x86_exported_include_dir/b.h": "",
108 "arch_x86_64_exported_include_dir/c.h": "",
109 },
110 blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000111cc_library_headers {
112 name: "lib-1",
113 export_include_dirs: ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000114}
115
116cc_library_headers {
117 name: "lib-2",
118 export_include_dirs: ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000119}
120
121cc_library_headers {
122 name: "foo_headers",
123 export_include_dirs: ["dir-1", "dir-2"],
124 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000125
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000126 arch: {
127 arm64: {
128 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
129 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
130 },
131 x86: {
132 export_include_dirs: ["arch_x86_exported_include_dir"],
133 },
134 x86_64: {
135 export_include_dirs: ["arch_x86_64_exported_include_dir"],
136 },
137 },
138
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000139 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000140}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200141 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000142 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400143 copts = [
144 "-I.",
145 "-I$(BINDIR)/.",
146 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400147 implementation_deps = [
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000148 ":lib-1",
149 ":lib-2",
150 ],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000151 includes = [
152 "dir-1",
153 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000154 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000155 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
156 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
157 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000158 "//conditions:default": [],
159 }),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000160)`, `cc_library_headers(
161 name = "lib-1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400162 copts = [
163 "-I.",
164 "-I$(BINDIR)/.",
165 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000166 includes = ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000167)`, `cc_library_headers(
168 name = "lib-2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400169 copts = [
170 "-I.",
171 "-I$(BINDIR)/.",
172 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000173 includes = ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000174)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200175 })
176}
177
178func TestCcLibraryHeadersOSSpecificHeader(t *testing.T) {
179 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
180 description: "cc_library_headers test with os-specific header_libs props",
181 moduleTypeUnderTest: "cc_library_headers",
182 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
183 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
184 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
185 filesystem: map[string]string{},
186 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400187cc_library_headers { name: "android-lib" }
188cc_library_headers { name: "base-lib" }
189cc_library_headers { name: "darwin-lib" }
190cc_library_headers { name: "fuchsia-lib" }
191cc_library_headers { name: "linux-lib" }
192cc_library_headers { name: "linux_bionic-lib" }
193cc_library_headers { name: "windows-lib" }
194cc_library_headers {
195 name: "foo_headers",
196 header_libs: ["base-lib"],
197 target: {
198 android: { header_libs: ["android-lib"] },
199 darwin: { header_libs: ["darwin-lib"] },
200 fuchsia: { header_libs: ["fuchsia-lib"] },
201 linux_bionic: { header_libs: ["linux_bionic-lib"] },
202 linux_glibc: { header_libs: ["linux-lib"] },
203 windows: { header_libs: ["windows-lib"] },
204 },
205 bazel_module: { bp2build_available: true },
206}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200207 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400208 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400209 copts = [
210 "-I.",
211 "-I$(BINDIR)/.",
212 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400213)`, `cc_library_headers(
214 name = "base-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400215 copts = [
216 "-I.",
217 "-I$(BINDIR)/.",
218 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400219)`, `cc_library_headers(
220 name = "darwin-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400221 copts = [
222 "-I.",
223 "-I$(BINDIR)/.",
224 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400225)`, `cc_library_headers(
226 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400227 copts = [
228 "-I.",
229 "-I$(BINDIR)/.",
230 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400231 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000232 "//build/bazel/platforms/os:android": [":android-lib"],
233 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
234 "//build/bazel/platforms/os:fuchsia": [":fuchsia-lib"],
235 "//build/bazel/platforms/os:linux": [":linux-lib"],
236 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
237 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400238 "//conditions:default": [],
239 }),
240)`, `cc_library_headers(
241 name = "fuchsia-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400242 copts = [
243 "-I.",
244 "-I$(BINDIR)/.",
245 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400246)`, `cc_library_headers(
247 name = "linux-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400248 copts = [
249 "-I.",
250 "-I$(BINDIR)/.",
251 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400252)`, `cc_library_headers(
253 name = "linux_bionic-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400254 copts = [
255 "-I.",
256 "-I$(BINDIR)/.",
257 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400258)`, `cc_library_headers(
259 name = "windows-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400260 copts = [
261 "-I.",
262 "-I$(BINDIR)/.",
263 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400264)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200265 })
266}
267
268func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
269 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
270 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
271 moduleTypeUnderTest: "cc_library_headers",
272 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
273 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
274 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
275 filesystem: map[string]string{},
276 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400277cc_library_headers { name: "android-lib" }
278cc_library_headers { name: "exported-lib" }
279cc_library_headers {
280 name: "foo_headers",
281 target: {
282 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
283 },
284}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200285 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400286 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400287 copts = [
288 "-I.",
289 "-I$(BINDIR)/.",
290 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400291)`, `cc_library_headers(
292 name = "exported-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400293 copts = [
294 "-I.",
295 "-I$(BINDIR)/.",
296 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400297)`, `cc_library_headers(
298 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400299 copts = [
300 "-I.",
301 "-I$(BINDIR)/.",
302 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400303 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400304 "//build/bazel/platforms/os:android": [":exported-lib"],
305 "//conditions:default": [],
306 }),
307 implementation_deps = select({
308 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400309 "//conditions:default": [],
310 }),
311)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200312 })
313}
314
315func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
316 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
317 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
318 moduleTypeUnderTest: "cc_library_headers",
319 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
320 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
321 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
322 filesystem: map[string]string{},
323 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400324 name: "foo_headers",
325 export_system_include_dirs: [
326 "shared_include_dir",
327 ],
328 target: {
329 android: {
330 export_system_include_dirs: [
331 "android_include_dir",
332 ],
333 },
334 linux_glibc: {
335 export_system_include_dirs: [
336 "linux_include_dir",
337 ],
338 },
339 darwin: {
340 export_system_include_dirs: [
341 "darwin_include_dir",
342 ],
343 },
344 },
345 arch: {
346 arm: {
347 export_system_include_dirs: [
348 "arm_include_dir",
349 ],
350 },
351 x86_64: {
352 export_system_include_dirs: [
353 "x86_64_include_dir",
354 ],
355 },
356 },
357}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200358 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400359 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400360 copts = [
361 "-I.",
362 "-I$(BINDIR)/.",
363 ],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400364 includes = ["shared_include_dir"] + select({
365 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
366 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
367 "//conditions:default": [],
368 }) + select({
369 "//build/bazel/platforms/os:android": ["android_include_dir"],
370 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
371 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
372 "//conditions:default": [],
373 }),
374)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200375 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000376}
Liz Kammerd366c902021-06-03 13:43:01 -0400377
378func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
379 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
380 description: "cc_library_headers test",
381 moduleTypeUnderTest: "cc_library_headers",
382 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
383 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
384 filesystem: map[string]string{
385 "lib-1/lib1a.h": "",
386 "lib-1/lib1b.h": "",
387 "lib-2/lib2a.h": "",
388 "lib-2/lib2b.h": "",
389 "dir-1/dir1a.h": "",
390 "dir-1/dir1b.h": "",
391 "dir-2/dir2a.h": "",
392 "dir-2/dir2b.h": "",
393 "arch_arm64_exported_include_dir/a.h": "",
394 "arch_x86_exported_include_dir/b.h": "",
395 "arch_x86_64_exported_include_dir/c.h": "",
396 },
397 blueprint: soongCcLibraryHeadersPreamble + `
398cc_library_headers {
399 name: "lib-1",
400 export_include_dirs: ["lib-1"],
401 no_libcrt: true,
402}`,
403 expectedBazelTargets: []string{`cc_library_headers(
404 name = "lib-1",
405 copts = [
406 "-I.",
407 "-I$(BINDIR)/.",
408 ],
409 includes = ["lib-1"],
410)`},
411 })
412}