blob: 2859bab706ea7f2c7fcb04c1a8006d2e95f22a10 [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) {
87 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,
183 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
184 filesystem: map[string]string{},
185 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400186cc_library_headers { name: "android-lib" }
187cc_library_headers { name: "base-lib" }
188cc_library_headers { name: "darwin-lib" }
189cc_library_headers { name: "fuchsia-lib" }
190cc_library_headers { name: "linux-lib" }
191cc_library_headers { name: "linux_bionic-lib" }
192cc_library_headers { name: "windows-lib" }
193cc_library_headers {
194 name: "foo_headers",
195 header_libs: ["base-lib"],
196 target: {
197 android: { header_libs: ["android-lib"] },
198 darwin: { header_libs: ["darwin-lib"] },
199 fuchsia: { header_libs: ["fuchsia-lib"] },
200 linux_bionic: { header_libs: ["linux_bionic-lib"] },
201 linux_glibc: { header_libs: ["linux-lib"] },
202 windows: { header_libs: ["windows-lib"] },
203 },
204 bazel_module: { bp2build_available: true },
205}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200206 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400207 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400208 copts = [
209 "-I.",
210 "-I$(BINDIR)/.",
211 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400212)`, `cc_library_headers(
213 name = "base-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400214 copts = [
215 "-I.",
216 "-I$(BINDIR)/.",
217 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400218)`, `cc_library_headers(
219 name = "darwin-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400220 copts = [
221 "-I.",
222 "-I$(BINDIR)/.",
223 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400224)`, `cc_library_headers(
225 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400226 copts = [
227 "-I.",
228 "-I$(BINDIR)/.",
229 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400230 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000231 "//build/bazel/platforms/os:android": [":android-lib"],
232 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
233 "//build/bazel/platforms/os:fuchsia": [":fuchsia-lib"],
234 "//build/bazel/platforms/os:linux": [":linux-lib"],
235 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
236 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400237 "//conditions:default": [],
238 }),
239)`, `cc_library_headers(
240 name = "fuchsia-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400241 copts = [
242 "-I.",
243 "-I$(BINDIR)/.",
244 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400245)`, `cc_library_headers(
246 name = "linux-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400247 copts = [
248 "-I.",
249 "-I$(BINDIR)/.",
250 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400251)`, `cc_library_headers(
252 name = "linux_bionic-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400253 copts = [
254 "-I.",
255 "-I$(BINDIR)/.",
256 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400257)`, `cc_library_headers(
258 name = "windows-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400259 copts = [
260 "-I.",
261 "-I$(BINDIR)/.",
262 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400263)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200264 })
265}
266
267func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
268 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
269 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
270 moduleTypeUnderTest: "cc_library_headers",
271 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
272 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
273 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
274 filesystem: map[string]string{},
275 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400276cc_library_headers { name: "android-lib" }
277cc_library_headers { name: "exported-lib" }
278cc_library_headers {
279 name: "foo_headers",
280 target: {
281 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
282 },
283}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200284 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400285 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400286 copts = [
287 "-I.",
288 "-I$(BINDIR)/.",
289 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400290)`, `cc_library_headers(
291 name = "exported-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400292 copts = [
293 "-I.",
294 "-I$(BINDIR)/.",
295 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400296)`, `cc_library_headers(
297 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400298 copts = [
299 "-I.",
300 "-I$(BINDIR)/.",
301 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400302 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400303 "//build/bazel/platforms/os:android": [":exported-lib"],
304 "//conditions:default": [],
305 }),
306 implementation_deps = select({
307 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400308 "//conditions:default": [],
309 }),
310)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200311 })
312}
313
314func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
315 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
316 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
317 moduleTypeUnderTest: "cc_library_headers",
318 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
319 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
320 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
321 filesystem: map[string]string{},
322 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400323 name: "foo_headers",
324 export_system_include_dirs: [
325 "shared_include_dir",
326 ],
327 target: {
328 android: {
329 export_system_include_dirs: [
330 "android_include_dir",
331 ],
332 },
333 linux_glibc: {
334 export_system_include_dirs: [
335 "linux_include_dir",
336 ],
337 },
338 darwin: {
339 export_system_include_dirs: [
340 "darwin_include_dir",
341 ],
342 },
343 },
344 arch: {
345 arm: {
346 export_system_include_dirs: [
347 "arm_include_dir",
348 ],
349 },
350 x86_64: {
351 export_system_include_dirs: [
352 "x86_64_include_dir",
353 ],
354 },
355 },
356}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200357 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400358 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400359 copts = [
360 "-I.",
361 "-I$(BINDIR)/.",
362 ],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400363 includes = ["shared_include_dir"] + select({
364 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
365 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
366 "//conditions:default": [],
367 }) + select({
368 "//build/bazel/platforms/os:android": ["android_include_dir"],
369 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
370 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
371 "//conditions:default": [],
372 }),
373)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200374 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000375}