blob: 2251b08473dbf41b69e73647514578d8ae03ab77 [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" }
Jingwen Chen91220d72021-03-24 02:18:33 -0400190cc_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"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400199 linux_bionic: { header_libs: ["linux_bionic-lib"] },
200 linux_glibc: { header_libs: ["linux-lib"] },
201 windows: { header_libs: ["windows-lib"] },
202 },
203 bazel_module: { bp2build_available: true },
204}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200205 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400206 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400207 copts = [
208 "-I.",
209 "-I$(BINDIR)/.",
210 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400211)`, `cc_library_headers(
212 name = "base-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400213 copts = [
214 "-I.",
215 "-I$(BINDIR)/.",
216 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400217)`, `cc_library_headers(
218 name = "darwin-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400219 copts = [
220 "-I.",
221 "-I$(BINDIR)/.",
222 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400223)`, `cc_library_headers(
224 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400225 copts = [
226 "-I.",
227 "-I$(BINDIR)/.",
228 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400229 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000230 "//build/bazel/platforms/os:android": [":android-lib"],
231 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000232 "//build/bazel/platforms/os:linux": [":linux-lib"],
233 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
234 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400235 "//conditions:default": [],
236 }),
237)`, `cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400238 name = "linux-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400239 copts = [
240 "-I.",
241 "-I$(BINDIR)/.",
242 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400243)`, `cc_library_headers(
244 name = "linux_bionic-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400245 copts = [
246 "-I.",
247 "-I$(BINDIR)/.",
248 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400249)`, `cc_library_headers(
250 name = "windows-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400251 copts = [
252 "-I.",
253 "-I$(BINDIR)/.",
254 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400255)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200256 })
257}
258
259func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
260 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
261 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
262 moduleTypeUnderTest: "cc_library_headers",
263 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
264 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
265 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
266 filesystem: map[string]string{},
267 blueprint: soongCcLibraryPreamble + `
Jingwen Chen91220d72021-03-24 02:18:33 -0400268cc_library_headers { name: "android-lib" }
269cc_library_headers { name: "exported-lib" }
270cc_library_headers {
271 name: "foo_headers",
272 target: {
273 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
274 },
275}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200276 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400277 name = "android-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400278 copts = [
279 "-I.",
280 "-I$(BINDIR)/.",
281 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400282)`, `cc_library_headers(
283 name = "exported-lib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400284 copts = [
285 "-I.",
286 "-I$(BINDIR)/.",
287 ],
Jingwen Chen91220d72021-03-24 02:18:33 -0400288)`, `cc_library_headers(
289 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400290 copts = [
291 "-I.",
292 "-I$(BINDIR)/.",
293 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400294 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400295 "//build/bazel/platforms/os:android": [":exported-lib"],
296 "//conditions:default": [],
297 }),
298 implementation_deps = select({
299 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400300 "//conditions:default": [],
301 }),
302)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200303 })
304}
305
306func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
307 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
308 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
309 moduleTypeUnderTest: "cc_library_headers",
310 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
311 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
312 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
313 filesystem: map[string]string{},
314 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400315 name: "foo_headers",
316 export_system_include_dirs: [
317 "shared_include_dir",
318 ],
319 target: {
320 android: {
321 export_system_include_dirs: [
322 "android_include_dir",
323 ],
324 },
325 linux_glibc: {
326 export_system_include_dirs: [
327 "linux_include_dir",
328 ],
329 },
330 darwin: {
331 export_system_include_dirs: [
332 "darwin_include_dir",
333 ],
334 },
335 },
336 arch: {
337 arm: {
338 export_system_include_dirs: [
339 "arm_include_dir",
340 ],
341 },
342 x86_64: {
343 export_system_include_dirs: [
344 "x86_64_include_dir",
345 ],
346 },
347 },
348}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200349 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400350 name = "foo_headers",
Chris Parsons484e50a2021-05-13 15:13:04 -0400351 copts = [
352 "-I.",
353 "-I$(BINDIR)/.",
354 ],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400355 includes = ["shared_include_dir"] + select({
356 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
357 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
358 "//conditions:default": [],
359 }) + select({
360 "//build/bazel/platforms/os:android": ["android_include_dir"],
361 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
362 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
363 "//conditions:default": [],
364 }),
365)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200366 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000367}
Liz Kammerd366c902021-06-03 13:43:01 -0400368
369func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
370 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
371 description: "cc_library_headers test",
372 moduleTypeUnderTest: "cc_library_headers",
373 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
374 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
375 filesystem: map[string]string{
376 "lib-1/lib1a.h": "",
377 "lib-1/lib1b.h": "",
378 "lib-2/lib2a.h": "",
379 "lib-2/lib2b.h": "",
380 "dir-1/dir1a.h": "",
381 "dir-1/dir1b.h": "",
382 "dir-2/dir2a.h": "",
383 "dir-2/dir2b.h": "",
384 "arch_arm64_exported_include_dir/a.h": "",
385 "arch_x86_exported_include_dir/b.h": "",
386 "arch_x86_64_exported_include_dir/c.h": "",
387 },
388 blueprint: soongCcLibraryHeadersPreamble + `
389cc_library_headers {
390 name: "lib-1",
391 export_include_dirs: ["lib-1"],
392 no_libcrt: true,
393}`,
394 expectedBazelTargets: []string{`cc_library_headers(
395 name = "lib-1",
396 copts = [
397 "-I.",
398 "-I$(BINDIR)/.",
399 ],
400 includes = ["lib-1"],
401)`},
402 })
403}