blob: ea2c10a9707bd47f267a8d050b3d341a3688c57d [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
43func TestCcLibraryHeadersLoadStatement(t *testing.T) {
44 testCases := []struct {
45 bazelTargets BazelTargets
46 expectedLoadStatements string
47 }{
48 {
49 bazelTargets: BazelTargets{
50 BazelTarget{
51 name: "cc_library_headers_target",
52 ruleClass: "cc_library_headers",
53 // Note: no bzlLoadLocation for native rules
54 },
55 },
56 expectedLoadStatements: ``,
57 },
58 }
59
60 for _, testCase := range testCases {
61 actual := testCase.bazelTargets.LoadStatements()
62 expected := testCase.expectedLoadStatements
63 if actual != expected {
64 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
65 }
66 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000067}
68
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020069func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
70 cc.RegisterCCBuildComponents(ctx)
71 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
72}
73
74func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040075 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020076 runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
77}
78
79func TestCcLibraryHeadersSimple(t *testing.T) {
80 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
81 description: "cc_library_headers test",
82 moduleTypeUnderTest: "cc_library_headers",
83 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
84 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
85 filesystem: map[string]string{
86 "lib-1/lib1a.h": "",
87 "lib-1/lib1b.h": "",
88 "lib-2/lib2a.h": "",
89 "lib-2/lib2b.h": "",
90 "dir-1/dir1a.h": "",
91 "dir-1/dir1b.h": "",
92 "dir-2/dir2a.h": "",
93 "dir-2/dir2b.h": "",
94 "arch_arm64_exported_include_dir/a.h": "",
95 "arch_x86_exported_include_dir/b.h": "",
96 "arch_x86_64_exported_include_dir/c.h": "",
97 },
98 blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000099cc_library_headers {
100 name: "lib-1",
101 export_include_dirs: ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000102}
103
104cc_library_headers {
105 name: "lib-2",
106 export_include_dirs: ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000107}
108
109cc_library_headers {
110 name: "foo_headers",
111 export_include_dirs: ["dir-1", "dir-2"],
112 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000113
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000114 arch: {
115 arm64: {
116 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
117 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
118 },
119 x86: {
120 export_include_dirs: ["arch_x86_exported_include_dir"],
121 },
122 x86_64: {
123 export_include_dirs: ["arch_x86_64_exported_include_dir"],
124 },
125 },
126
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000127 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000128}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200129 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000130 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400131 copts = [
132 "-I.",
133 "-I$(BINDIR)/.",
134 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400135 implementation_deps = [
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000136 ":lib-1",
137 ":lib-2",
138 ],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000139 includes = [
140 "dir-1",
141 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000142 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000143 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
144 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
145 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000146 "//conditions:default": [],
147 }),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000148)`, `cc_library_headers(
149 name = "lib-1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400150 copts = [
151 "-I.",
152 "-I$(BINDIR)/.",
153 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000154 includes = ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000155)`, `cc_library_headers(
156 name = "lib-2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400157 copts = [
158 "-I.",
159 "-I$(BINDIR)/.",
160 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000161 includes = ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000162)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200163 })
164}
165
166func TestCcLibraryHeadersOSSpecificHeader(t *testing.T) {
167 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
168 description: "cc_library_headers test with os-specific header_libs props",
169 moduleTypeUnderTest: "cc_library_headers",
170 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
171 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200172 filesystem: map[string]string{},
173 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400174cc_library_headers { name: "android-lib" }
175cc_library_headers { name: "base-lib" }
176cc_library_headers { name: "darwin-lib" }
Jingwen Chen91220d72021-03-24 02:18:33 -0400177cc_library_headers { name: "linux-lib" }
178cc_library_headers { name: "linux_bionic-lib" }
179cc_library_headers { name: "windows-lib" }
180cc_library_headers {
181 name: "foo_headers",
182 header_libs: ["base-lib"],
183 target: {
184 android: { header_libs: ["android-lib"] },
185 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400186 linux_bionic: { header_libs: ["linux_bionic-lib"] },
187 linux_glibc: { header_libs: ["linux-lib"] },
188 windows: { header_libs: ["windows-lib"] },
189 },
190 bazel_module: { bp2build_available: true },
191}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200192 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400193 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400194 copts = [
195 "-I.",
196 "-I$(BINDIR)/.",
197 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400198)`, `cc_library_headers(
199 name = "base-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400200 copts = [
201 "-I.",
202 "-I$(BINDIR)/.",
203 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400204)`, `cc_library_headers(
205 name = "darwin-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400206 copts = [
207 "-I.",
208 "-I$(BINDIR)/.",
209 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400210)`, `cc_library_headers(
211 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400212 copts = [
213 "-I.",
214 "-I$(BINDIR)/.",
215 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400216 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000217 "//build/bazel/platforms/os:android": [":android-lib"],
218 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000219 "//build/bazel/platforms/os:linux": [":linux-lib"],
220 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
221 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400222 "//conditions:default": [],
223 }),
224)`, `cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400225 name = "linux-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400226 copts = [
227 "-I.",
228 "-I$(BINDIR)/.",
229 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400230)`, `cc_library_headers(
231 name = "linux_bionic-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400232 copts = [
233 "-I.",
234 "-I$(BINDIR)/.",
235 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400236)`, `cc_library_headers(
237 name = "windows-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400238 copts = [
239 "-I.",
240 "-I$(BINDIR)/.",
241 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400242)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200243 })
244}
245
246func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
247 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
248 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
249 moduleTypeUnderTest: "cc_library_headers",
250 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
251 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200252 filesystem: map[string]string{},
253 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400254cc_library_headers { name: "android-lib" }
255cc_library_headers { name: "exported-lib" }
256cc_library_headers {
257 name: "foo_headers",
258 target: {
259 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
260 },
261}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200262 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400263 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400264 copts = [
265 "-I.",
266 "-I$(BINDIR)/.",
267 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400268)`, `cc_library_headers(
269 name = "exported-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400270 copts = [
271 "-I.",
272 "-I$(BINDIR)/.",
273 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400274)`, `cc_library_headers(
275 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400276 copts = [
277 "-I.",
278 "-I$(BINDIR)/.",
279 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400280 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400281 "//build/bazel/platforms/os:android": [":exported-lib"],
282 "//conditions:default": [],
283 }),
284 implementation_deps = select({
285 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400286 "//conditions:default": [],
287 }),
288)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200289 })
290}
291
292func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
293 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
294 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
295 moduleTypeUnderTest: "cc_library_headers",
296 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
297 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200298 filesystem: map[string]string{},
299 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400300 name: "foo_headers",
301 export_system_include_dirs: [
302 "shared_include_dir",
303 ],
304 target: {
305 android: {
306 export_system_include_dirs: [
307 "android_include_dir",
308 ],
309 },
310 linux_glibc: {
311 export_system_include_dirs: [
312 "linux_include_dir",
313 ],
314 },
315 darwin: {
316 export_system_include_dirs: [
317 "darwin_include_dir",
318 ],
319 },
320 },
321 arch: {
322 arm: {
323 export_system_include_dirs: [
324 "arm_include_dir",
325 ],
326 },
327 x86_64: {
328 export_system_include_dirs: [
329 "x86_64_include_dir",
330 ],
331 },
332 },
333}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200334 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400335 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400336 copts = [
337 "-I.",
338 "-I$(BINDIR)/.",
339 ],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400340 includes = ["shared_include_dir"] + select({
341 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
342 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
343 "//conditions:default": [],
344 }) + select({
345 "//build/bazel/platforms/os:android": ["android_include_dir"],
346 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
347 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
348 "//conditions:default": [],
349 }),
350)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200351 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000352}
Liz Kammerd366c902021-06-03 13:43:01 -0400353
354func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
355 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
356 description: "cc_library_headers test",
357 moduleTypeUnderTest: "cc_library_headers",
358 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
359 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
360 filesystem: map[string]string{
361 "lib-1/lib1a.h": "",
362 "lib-1/lib1b.h": "",
363 "lib-2/lib2a.h": "",
364 "lib-2/lib2b.h": "",
365 "dir-1/dir1a.h": "",
366 "dir-1/dir1b.h": "",
367 "dir-2/dir2a.h": "",
368 "dir-2/dir2b.h": "",
369 "arch_arm64_exported_include_dir/a.h": "",
370 "arch_x86_exported_include_dir/b.h": "",
371 "arch_x86_64_exported_include_dir/c.h": "",
372 },
373 blueprint: soongCcLibraryHeadersPreamble + `
374cc_library_headers {
375 name: "lib-1",
376 export_include_dirs: ["lib-1"],
377 no_libcrt: true,
378}`,
379 expectedBazelTargets: []string{`cc_library_headers(
380 name = "lib-1",
381 copts = [
382 "-I.",
383 "-I$(BINDIR)/.",
384 ],
385 includes = ["lib-1"],
386)`},
387 })
388}