blob: 712d0bd5a9d5d1acbb2aa44b2dc51a00b772d226 [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)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020048 blueprint string
49 expectedBazelTargets []string
50 filesystem map[string]string
51 dir string
52}
53
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000054func TestCcLibraryHeadersLoadStatement(t *testing.T) {
55 testCases := []struct {
56 bazelTargets BazelTargets
57 expectedLoadStatements string
58 }{
59 {
60 bazelTargets: BazelTargets{
61 BazelTarget{
62 name: "cc_library_headers_target",
63 ruleClass: "cc_library_headers",
64 // Note: no bzlLoadLocation for native rules
65 },
66 },
67 expectedLoadStatements: ``,
68 },
69 }
70
71 for _, testCase := range testCases {
72 actual := testCase.bazelTargets.LoadStatements()
73 expected := testCase.expectedLoadStatements
74 if actual != expected {
75 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
76 }
77 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000078}
79
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020080func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
81 cc.RegisterCCBuildComponents(ctx)
82 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
83}
84
85func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040086 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020087 runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
88}
89
90func TestCcLibraryHeadersSimple(t *testing.T) {
91 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
92 description: "cc_library_headers test",
93 moduleTypeUnderTest: "cc_library_headers",
94 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
95 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
96 filesystem: map[string]string{
97 "lib-1/lib1a.h": "",
98 "lib-1/lib1b.h": "",
99 "lib-2/lib2a.h": "",
100 "lib-2/lib2b.h": "",
101 "dir-1/dir1a.h": "",
102 "dir-1/dir1b.h": "",
103 "dir-2/dir2a.h": "",
104 "dir-2/dir2b.h": "",
105 "arch_arm64_exported_include_dir/a.h": "",
106 "arch_x86_exported_include_dir/b.h": "",
107 "arch_x86_64_exported_include_dir/c.h": "",
108 },
109 blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000110cc_library_headers {
111 name: "lib-1",
112 export_include_dirs: ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000113}
114
115cc_library_headers {
116 name: "lib-2",
117 export_include_dirs: ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000118}
119
120cc_library_headers {
121 name: "foo_headers",
122 export_include_dirs: ["dir-1", "dir-2"],
123 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000124
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000125 arch: {
126 arm64: {
127 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
128 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
129 },
130 x86: {
131 export_include_dirs: ["arch_x86_exported_include_dir"],
132 },
133 x86_64: {
134 export_include_dirs: ["arch_x86_64_exported_include_dir"],
135 },
136 },
137
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000138 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000139}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200140 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000141 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400142 copts = [
143 "-I.",
144 "-I$(BINDIR)/.",
145 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400146 implementation_deps = [
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000147 ":lib-1",
148 ":lib-2",
149 ],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000150 includes = [
151 "dir-1",
152 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000153 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000154 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
155 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
156 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000157 "//conditions:default": [],
158 }),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000159)`, `cc_library_headers(
160 name = "lib-1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400161 copts = [
162 "-I.",
163 "-I$(BINDIR)/.",
164 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000165 includes = ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000166)`, `cc_library_headers(
167 name = "lib-2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400168 copts = [
169 "-I.",
170 "-I$(BINDIR)/.",
171 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000172 includes = ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000173)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200174 })
175}
176
177func TestCcLibraryHeadersOSSpecificHeader(t *testing.T) {
178 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
179 description: "cc_library_headers test with os-specific header_libs props",
180 moduleTypeUnderTest: "cc_library_headers",
181 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
182 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200183 filesystem: map[string]string{},
184 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400185cc_library_headers { name: "android-lib" }
186cc_library_headers { name: "base-lib" }
187cc_library_headers { name: "darwin-lib" }
Jingwen Chen91220d72021-03-24 02:18:33 -0400188cc_library_headers { name: "linux-lib" }
189cc_library_headers { name: "linux_bionic-lib" }
190cc_library_headers { name: "windows-lib" }
191cc_library_headers {
192 name: "foo_headers",
193 header_libs: ["base-lib"],
194 target: {
195 android: { header_libs: ["android-lib"] },
196 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400197 linux_bionic: { header_libs: ["linux_bionic-lib"] },
198 linux_glibc: { header_libs: ["linux-lib"] },
199 windows: { header_libs: ["windows-lib"] },
200 },
201 bazel_module: { bp2build_available: true },
202}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200203 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400204 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400205 copts = [
206 "-I.",
207 "-I$(BINDIR)/.",
208 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400209)`, `cc_library_headers(
210 name = "base-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400211 copts = [
212 "-I.",
213 "-I$(BINDIR)/.",
214 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400215)`, `cc_library_headers(
216 name = "darwin-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400217 copts = [
218 "-I.",
219 "-I$(BINDIR)/.",
220 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400221)`, `cc_library_headers(
222 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400223 copts = [
224 "-I.",
225 "-I$(BINDIR)/.",
226 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400227 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000228 "//build/bazel/platforms/os:android": [":android-lib"],
229 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000230 "//build/bazel/platforms/os:linux": [":linux-lib"],
231 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
232 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400233 "//conditions:default": [],
234 }),
235)`, `cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400236 name = "linux-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400237 copts = [
238 "-I.",
239 "-I$(BINDIR)/.",
240 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400241)`, `cc_library_headers(
242 name = "linux_bionic-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400243 copts = [
244 "-I.",
245 "-I$(BINDIR)/.",
246 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400247)`, `cc_library_headers(
248 name = "windows-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400249 copts = [
250 "-I.",
251 "-I$(BINDIR)/.",
252 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400253)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200254 })
255}
256
257func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
258 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
259 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
260 moduleTypeUnderTest: "cc_library_headers",
261 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
262 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200263 filesystem: map[string]string{},
264 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400265cc_library_headers { name: "android-lib" }
266cc_library_headers { name: "exported-lib" }
267cc_library_headers {
268 name: "foo_headers",
269 target: {
270 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
271 },
272}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200273 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400274 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400275 copts = [
276 "-I.",
277 "-I$(BINDIR)/.",
278 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400279)`, `cc_library_headers(
280 name = "exported-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400281 copts = [
282 "-I.",
283 "-I$(BINDIR)/.",
284 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400285)`, `cc_library_headers(
286 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400287 copts = [
288 "-I.",
289 "-I$(BINDIR)/.",
290 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400291 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400292 "//build/bazel/platforms/os:android": [":exported-lib"],
293 "//conditions:default": [],
294 }),
295 implementation_deps = select({
296 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400297 "//conditions:default": [],
298 }),
299)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200300 })
301}
302
303func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
304 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
305 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
306 moduleTypeUnderTest: "cc_library_headers",
307 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
308 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200309 filesystem: map[string]string{},
310 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400311 name: "foo_headers",
312 export_system_include_dirs: [
313 "shared_include_dir",
314 ],
315 target: {
316 android: {
317 export_system_include_dirs: [
318 "android_include_dir",
319 ],
320 },
321 linux_glibc: {
322 export_system_include_dirs: [
323 "linux_include_dir",
324 ],
325 },
326 darwin: {
327 export_system_include_dirs: [
328 "darwin_include_dir",
329 ],
330 },
331 },
332 arch: {
333 arm: {
334 export_system_include_dirs: [
335 "arm_include_dir",
336 ],
337 },
338 x86_64: {
339 export_system_include_dirs: [
340 "x86_64_include_dir",
341 ],
342 },
343 },
344}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200345 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400346 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400347 copts = [
348 "-I.",
349 "-I$(BINDIR)/.",
350 ],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400351 includes = ["shared_include_dir"] + select({
352 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
353 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
354 "//conditions:default": [],
355 }) + select({
356 "//build/bazel/platforms/os:android": ["android_include_dir"],
357 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
358 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
359 "//conditions:default": [],
360 }),
361)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200362 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000363}
Liz Kammerd366c902021-06-03 13:43:01 -0400364
365func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
366 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
367 description: "cc_library_headers test",
368 moduleTypeUnderTest: "cc_library_headers",
369 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
370 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
371 filesystem: map[string]string{
372 "lib-1/lib1a.h": "",
373 "lib-1/lib1b.h": "",
374 "lib-2/lib2a.h": "",
375 "lib-2/lib2b.h": "",
376 "dir-1/dir1a.h": "",
377 "dir-1/dir1b.h": "",
378 "dir-2/dir2a.h": "",
379 "dir-2/dir2b.h": "",
380 "arch_arm64_exported_include_dir/a.h": "",
381 "arch_x86_exported_include_dir/b.h": "",
382 "arch_x86_64_exported_include_dir/c.h": "",
383 },
384 blueprint: soongCcLibraryHeadersPreamble + `
385cc_library_headers {
386 name: "lib-1",
387 export_include_dirs: ["lib-1"],
388 no_libcrt: true,
389}`,
390 expectedBazelTargets: []string{`cc_library_headers(
391 name = "lib-1",
392 copts = [
393 "-I.",
394 "-I$(BINDIR)/.",
395 ],
396 includes = ["lib-1"],
397)`},
398 })
399}