blob: a592ca97873c9c3a69316a375a81e377b674bc56 [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 {
Liz Kammer8337ea42021-09-10 10:06:32 -040028 name: "linux_bionic_supported",
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000029}`
30)
31
32func TestCcLibraryHeadersLoadStatement(t *testing.T) {
33 testCases := []struct {
34 bazelTargets BazelTargets
35 expectedLoadStatements string
36 }{
37 {
38 bazelTargets: BazelTargets{
39 BazelTarget{
40 name: "cc_library_headers_target",
41 ruleClass: "cc_library_headers",
42 // Note: no bzlLoadLocation for native rules
43 },
44 },
45 expectedLoadStatements: ``,
46 },
47 }
48
49 for _, testCase := range testCases {
50 actual := testCase.bazelTargets.LoadStatements()
51 expected := testCase.expectedLoadStatements
52 if actual != expected {
53 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
54 }
55 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000056}
57
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020058func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
59 cc.RegisterCCBuildComponents(ctx)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020060}
61
Sam Delmerico3177a6e2022-06-21 19:28:33 +000062func runCcLibraryHeadersTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040063 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000064 RunBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020065}
66
67func TestCcLibraryHeadersSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000068 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
69 Description: "cc_library_headers test",
70 ModuleTypeUnderTest: "cc_library_headers",
71 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
72 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020073 "lib-1/lib1a.h": "",
74 "lib-1/lib1b.h": "",
75 "lib-2/lib2a.h": "",
76 "lib-2/lib2b.h": "",
77 "dir-1/dir1a.h": "",
78 "dir-1/dir1b.h": "",
79 "dir-2/dir2a.h": "",
80 "dir-2/dir2b.h": "",
81 "arch_arm64_exported_include_dir/a.h": "",
82 "arch_x86_exported_include_dir/b.h": "",
83 "arch_x86_64_exported_include_dir/c.h": "",
84 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000085 Blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000086cc_library_headers {
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000087 name: "foo_headers",
88 export_include_dirs: ["dir-1", "dir-2"],
89 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000090
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000091 arch: {
92 arm64: {
Liz Kammer8337ea42021-09-10 10:06:32 -040093 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000094 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
95 },
96 x86: {
97 export_include_dirs: ["arch_x86_exported_include_dir"],
98 },
99 x86_64: {
100 export_include_dirs: ["arch_x86_64_exported_include_dir"],
101 },
102 },
Yu Liufc603162022-03-01 15:44:08 -0800103 sdk_version: "current",
104 min_sdk_version: "29",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000105
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000106 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000107}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000108 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000109 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Zi Wang1cb11802022-12-09 16:08:54 -0800110 "export_includes": `select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000111 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
112 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
113 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000114 "//conditions:default": [],
Zi Wang1cb11802022-12-09 16:08:54 -0800115 }) + [
116 "dir-1",
117 "dir-2",
118 ]`,
Trevor Radcliffefb6c5222022-06-17 19:42:28 +0000119 "sdk_version": `"current"`,
120 "min_sdk_version": `"29"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500121 }),
122 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200123 })
124}
125
Zi Wang9f609db2023-01-04 11:06:54 -0800126// header_libs has "variant_prepend" tag. In bp2build output,
127// variant info(select) should go before general info.
Liz Kammer8337ea42021-09-10 10:06:32 -0400128func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000129 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
130 Description: "cc_library_headers test with os-specific header_libs props",
131 ModuleTypeUnderTest: "cc_library_headers",
132 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
133 Filesystem: map[string]string{},
134 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400135cc_library_headers {
136 name: "android-lib",
137 bazel_module: { bp2build_available: false },
138}
139cc_library_headers {
140 name: "base-lib",
141 bazel_module: { bp2build_available: false },
142}
143cc_library_headers {
144 name: "darwin-lib",
145 bazel_module: { bp2build_available: false },
146}
147cc_library_headers {
148 name: "linux-lib",
149 bazel_module: { bp2build_available: false },
150}
151cc_library_headers {
152 name: "linux_bionic-lib",
153 bazel_module: { bp2build_available: false },
154}
155cc_library_headers {
156 name: "windows-lib",
157 bazel_module: { bp2build_available: false },
158}
Jingwen Chen91220d72021-03-24 02:18:33 -0400159cc_library_headers {
160 name: "foo_headers",
161 header_libs: ["base-lib"],
Trevor Radcliffefb6c5222022-06-17 19:42:28 +0000162 export_header_lib_headers: ["base-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400163 target: {
Trevor Radcliffefb6c5222022-06-17 19:42:28 +0000164 android: {
165 header_libs: ["android-lib"],
166 export_header_lib_headers: ["android-lib"],
167 },
168 darwin: {
169 header_libs: ["darwin-lib"],
170 export_header_lib_headers: ["darwin-lib"],
171 },
172 linux_bionic: {
173 header_libs: ["linux_bionic-lib"],
174 export_header_lib_headers: ["linux_bionic-lib"],
175 },
176 linux_glibc: {
177 header_libs: ["linux-lib"],
178 export_header_lib_headers: ["linux-lib"],
179 },
180 windows: {
181 header_libs: ["windows-lib"],
182 export_header_lib_headers: ["windows-lib"],
183 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400184 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400185 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400186}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000187 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000188 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Zi Wang9f609db2023-01-04 11:06:54 -0800189 "deps": `select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000190 "//build/bazel/platforms/os:android": [":android-lib"],
191 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000192 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
Colin Cross133782e2022-12-20 15:29:31 -0800193 "//build/bazel/platforms/os:linux_glibc": [":linux-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000194 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400195 "//conditions:default": [],
Zi Wang9f609db2023-01-04 11:06:54 -0800196 }) + [":base-lib"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500197 }),
198 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200199 })
200}
201
202func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000203 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
204 Description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
205 ModuleTypeUnderTest: "cc_library_headers",
206 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
207 Filesystem: map[string]string{},
208 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400209cc_library_headers {
210 name: "android-lib",
211 bazel_module: { bp2build_available: false },
212 }
213cc_library_headers {
214 name: "exported-lib",
215 bazel_module: { bp2build_available: false },
216}
Jingwen Chen91220d72021-03-24 02:18:33 -0400217cc_library_headers {
218 name: "foo_headers",
219 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400220 android: {
221 header_libs: ["android-lib", "exported-lib"],
222 export_header_lib_headers: ["exported-lib"]
223 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400224 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400225 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400226}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000227 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000228 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500229 "deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400230 "//build/bazel/platforms/os:android": [":exported-lib"],
231 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500232 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500233 }),
234 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200235 })
236}
237
238func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000239 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
240 Description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
241 ModuleTypeUnderTest: "cc_library_headers",
242 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
243 Filesystem: map[string]string{},
244 Blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400245 name: "foo_headers",
246 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400247 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400248 ],
249 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400250 android: {
251 export_system_include_dirs: [
252 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400253 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400254 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400255 linux_glibc: {
256 export_system_include_dirs: [
257 "linux_include_dir",
258 ],
259 },
260 darwin: {
261 export_system_include_dirs: [
262 "darwin_include_dir",
263 ],
264 },
265 },
266 arch: {
267 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400268 export_system_include_dirs: [
269 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400270 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400271 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400272 x86_64: {
273 export_system_include_dirs: [
274 "x86_64_include_dir",
275 ],
276 },
277 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400278 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400279}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000280 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000281 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Zi Wang1cb11802022-12-09 16:08:54 -0800282 "export_system_includes": `select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400283 "//build/bazel/platforms/os:android": ["android_include_dir"],
284 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
Colin Cross133782e2022-12-20 15:29:31 -0800285 "//build/bazel/platforms/os:linux_glibc": ["linux_include_dir"],
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400286 "//conditions:default": [],
Zi Wang1cb11802022-12-09 16:08:54 -0800287 }) + select({
288 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
289 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
290 "//conditions:default": [],
291 }) + ["shared_include_dir"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500292 }),
293 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200294 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000295}
Liz Kammerd366c902021-06-03 13:43:01 -0400296
297func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000298 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
299 Description: "cc_library_headers test",
300 ModuleTypeUnderTest: "cc_library_headers",
301 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
302 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -0400303 "lib-1/lib1a.h": "",
304 "lib-1/lib1b.h": "",
305 "lib-2/lib2a.h": "",
306 "lib-2/lib2b.h": "",
307 "dir-1/dir1a.h": "",
308 "dir-1/dir1b.h": "",
309 "dir-2/dir2a.h": "",
310 "dir-2/dir2b.h": "",
311 "arch_arm64_exported_include_dir/a.h": "",
312 "arch_x86_exported_include_dir/b.h": "",
313 "arch_x86_64_exported_include_dir/c.h": "",
314 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000315 Blueprint: soongCcLibraryHeadersPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -0400316cc_library_headers {
317 name: "lib-1",
318 export_include_dirs: ["lib-1"],
319 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400320 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400321}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000322 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000323 MakeBazelTarget("cc_library_headers", "lib-1", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500324 "export_includes": `["lib-1"]`,
325 }),
326 },
Liz Kammerd366c902021-06-03 13:43:01 -0400327 })
328}
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000329
330func TestCcLibraryHeadersExportedStaticLibHeadersReexported(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000331 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
332 Description: "cc_library_headers exported_static_lib_headers is reexported",
333 ModuleTypeUnderTest: "cc_library_headers",
334 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
335 Filesystem: map[string]string{},
336 Blueprint: soongCcLibraryHeadersPreamble + `
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000337cc_library_headers {
338 name: "foo_headers",
339 export_static_lib_headers: ["foo_export"],
340 static_libs: ["foo_export", "foo_no_reexport"],
341 bazel_module: { bp2build_available: true },
342}
343` + simpleModuleDoNotConvertBp2build("cc_library_headers", "foo_export"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000344 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000345 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000346 "deps": `[":foo_export"]`,
347 }),
348 },
349 })
350}
351
352func TestCcLibraryHeadersExportedSharedLibHeadersReexported(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000353 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
354 Description: "cc_library_headers exported_shared_lib_headers is reexported",
355 ModuleTypeUnderTest: "cc_library_headers",
356 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
357 Filesystem: map[string]string{},
358 Blueprint: soongCcLibraryHeadersPreamble + `
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000359cc_library_headers {
360 name: "foo_headers",
361 export_shared_lib_headers: ["foo_export"],
362 shared_libs: ["foo_export", "foo_no_reexport"],
363 bazel_module: { bp2build_available: true },
364}
365` + simpleModuleDoNotConvertBp2build("cc_library_headers", "foo_export"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000366 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000367 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000368 "deps": `[":foo_export"]`,
369 }),
370 },
371 })
372}
373
374func TestCcLibraryHeadersExportedHeaderLibHeadersReexported(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000375 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
376 Description: "cc_library_headers exported_header_lib_headers is reexported",
377 ModuleTypeUnderTest: "cc_library_headers",
378 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
379 Filesystem: map[string]string{},
380 Blueprint: soongCcLibraryHeadersPreamble + `
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000381cc_library_headers {
382 name: "foo_headers",
383 export_header_lib_headers: ["foo_export"],
384 header_libs: ["foo_export", "foo_no_reexport"],
385 bazel_module: { bp2build_available: true },
386}
387` + simpleModuleDoNotConvertBp2build("cc_library_headers", "foo_export"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000388 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000389 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000390 "deps": `[":foo_export"]`,
391 }),
392 },
393 })
394}
Trevor Radcliffe7f897fc2022-08-18 15:53:00 +0000395
396func TestCcLibraryHeadersWholeStaticLibsReexported(t *testing.T) {
397 runCcLibraryHeadersTestCase(t, Bp2buildTestCase{
398 Description: "cc_library_headers whole_static_libs is reexported",
399 ModuleTypeUnderTest: "cc_library_headers",
400 ModuleTypeUnderTestFactory: cc.LibraryHeaderFactory,
401 Filesystem: map[string]string{},
402 Blueprint: soongCcLibraryHeadersPreamble + `
403cc_library_headers {
404 name: "foo_headers",
405 whole_static_libs: ["foo_export"],
406 bazel_module: { bp2build_available: true },
407}
408` + simpleModuleDoNotConvertBp2build("cc_library_headers", "foo_export"),
409 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000410 MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{
Trevor Radcliffe7f897fc2022-08-18 15:53:00 +0000411 "deps": `[":foo_export"]`,
412 }),
413 },
414 })
415}