blob: e43672beaefd8426227c68982a40bec75a7e7a59 [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
31toolchain_library {
Liz Kammer8337ea42021-09-10 10:06:32 -040032 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"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400102 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000103}
104
105cc_library_headers {
106 name: "lib-2",
107 export_include_dirs: ["lib-2"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400108 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000109}
110
111cc_library_headers {
112 name: "foo_headers",
113 export_include_dirs: ["dir-1", "dir-2"],
114 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000115
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000116 arch: {
117 arm64: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400118 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000119 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
120 },
121 x86: {
122 export_include_dirs: ["arch_x86_exported_include_dir"],
123 },
124 x86_64: {
125 export_include_dirs: ["arch_x86_64_exported_include_dir"],
126 },
127 },
128
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000129 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000130}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200131 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000132 name = "foo_headers",
Liz Kammer5fad5012021-09-09 14:08:21 -0400133 export_includes = [
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000134 "dir-1",
135 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000136 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000137 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
138 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
139 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000140 "//conditions:default": [],
141 }),
Liz Kammer5fad5012021-09-09 14:08:21 -0400142 implementation_deps = [
143 ":lib-1",
144 ":lib-2",
145 ],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000146)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200147 })
148}
149
Liz Kammer8337ea42021-09-10 10:06:32 -0400150func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200151 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
152 description: "cc_library_headers test with os-specific header_libs props",
153 moduleTypeUnderTest: "cc_library_headers",
154 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
155 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200156 filesystem: map[string]string{},
157 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400158cc_library_headers {
159 name: "android-lib",
160 bazel_module: { bp2build_available: false },
161}
162cc_library_headers {
163 name: "base-lib",
164 bazel_module: { bp2build_available: false },
165}
166cc_library_headers {
167 name: "darwin-lib",
168 bazel_module: { bp2build_available: false },
169}
170cc_library_headers {
171 name: "linux-lib",
172 bazel_module: { bp2build_available: false },
173}
174cc_library_headers {
175 name: "linux_bionic-lib",
176 bazel_module: { bp2build_available: false },
177}
178cc_library_headers {
179 name: "windows-lib",
180 bazel_module: { bp2build_available: false },
181}
Jingwen Chen91220d72021-03-24 02:18:33 -0400182cc_library_headers {
183 name: "foo_headers",
184 header_libs: ["base-lib"],
185 target: {
186 android: { header_libs: ["android-lib"] },
187 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400188 linux_bionic: { header_libs: ["linux_bionic-lib"] },
189 linux_glibc: { header_libs: ["linux-lib"] },
190 windows: { header_libs: ["windows-lib"] },
191 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400192 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400193}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200194 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400195 name = "foo_headers",
Chris Parsonsd6358772021-05-18 18:35:24 -0400196 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000197 "//build/bazel/platforms/os:android": [":android-lib"],
198 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000199 "//build/bazel/platforms/os:linux": [":linux-lib"],
200 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
201 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400202 "//conditions:default": [],
203 }),
Jingwen Chen91220d72021-03-24 02:18:33 -0400204)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200205 })
206}
207
208func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
209 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
210 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
211 moduleTypeUnderTest: "cc_library_headers",
212 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
213 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200214 filesystem: map[string]string{},
215 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400216cc_library_headers {
217 name: "android-lib",
218 bazel_module: { bp2build_available: false },
219 }
220cc_library_headers {
221 name: "exported-lib",
222 bazel_module: { bp2build_available: false },
223}
Jingwen Chen91220d72021-03-24 02:18:33 -0400224cc_library_headers {
225 name: "foo_headers",
226 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400227 android: {
228 header_libs: ["android-lib", "exported-lib"],
229 export_header_lib_headers: ["exported-lib"]
230 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400231 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400232 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400233}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200234 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400235 name = "foo_headers",
Jingwen Chen63930982021-03-24 10:04:33 -0400236 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400237 "//build/bazel/platforms/os:android": [":exported-lib"],
238 "//conditions:default": [],
239 }),
240 implementation_deps = select({
241 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400242 "//conditions:default": [],
243 }),
244)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200245 })
246}
247
248func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
249 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
250 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
251 moduleTypeUnderTest: "cc_library_headers",
252 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
253 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200254 filesystem: map[string]string{},
255 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400256 name: "foo_headers",
257 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400258 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400259 ],
260 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400261 android: {
262 export_system_include_dirs: [
263 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400264 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400265 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400266 linux_glibc: {
267 export_system_include_dirs: [
268 "linux_include_dir",
269 ],
270 },
271 darwin: {
272 export_system_include_dirs: [
273 "darwin_include_dir",
274 ],
275 },
276 },
277 arch: {
278 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400279 export_system_include_dirs: [
280 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400281 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400282 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400283 x86_64: {
284 export_system_include_dirs: [
285 "x86_64_include_dir",
286 ],
287 },
288 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400289 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400290}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200291 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400292 name = "foo_headers",
Liz Kammer5fad5012021-09-09 14:08:21 -0400293 export_system_includes = ["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400294 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
295 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
296 "//conditions:default": [],
297 }) + select({
298 "//build/bazel/platforms/os:android": ["android_include_dir"],
299 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
300 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
301 "//conditions:default": [],
302 }),
303)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200304 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000305}
Liz Kammerd366c902021-06-03 13:43:01 -0400306
307func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
308 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
309 description: "cc_library_headers test",
310 moduleTypeUnderTest: "cc_library_headers",
311 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
312 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
313 filesystem: map[string]string{
314 "lib-1/lib1a.h": "",
315 "lib-1/lib1b.h": "",
316 "lib-2/lib2a.h": "",
317 "lib-2/lib2b.h": "",
318 "dir-1/dir1a.h": "",
319 "dir-1/dir1b.h": "",
320 "dir-2/dir2a.h": "",
321 "dir-2/dir2b.h": "",
322 "arch_arm64_exported_include_dir/a.h": "",
323 "arch_x86_exported_include_dir/b.h": "",
324 "arch_x86_64_exported_include_dir/c.h": "",
325 },
326 blueprint: soongCcLibraryHeadersPreamble + `
327cc_library_headers {
328 name: "lib-1",
329 export_include_dirs: ["lib-1"],
330 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400331 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400332}`,
333 expectedBazelTargets: []string{`cc_library_headers(
334 name = "lib-1",
Liz Kammer5fad5012021-09-09 14:08:21 -0400335 export_includes = ["lib-1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400336)`},
337 })
338}