blob: 8b490f88584c11deab06a872bf7a6a21e9dc0ffd [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",
Chris Parsons484e50a2021-05-13 15:13:04 -0400133 copts = [
134 "-I.",
135 "-I$(BINDIR)/.",
136 ],
Liz Kammer5fad5012021-09-09 14:08:21 -0400137 export_includes = [
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000138 "dir-1",
139 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000140 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000141 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
142 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
143 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000144 "//conditions:default": [],
145 }),
Liz Kammer5fad5012021-09-09 14:08:21 -0400146 implementation_deps = [
147 ":lib-1",
148 ":lib-2",
149 ],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000150)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200151 })
152}
153
Liz Kammer8337ea42021-09-10 10:06:32 -0400154func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200155 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
156 description: "cc_library_headers test with os-specific header_libs props",
157 moduleTypeUnderTest: "cc_library_headers",
158 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
159 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200160 filesystem: map[string]string{},
161 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400162cc_library_headers {
163 name: "android-lib",
164 bazel_module: { bp2build_available: false },
165}
166cc_library_headers {
167 name: "base-lib",
168 bazel_module: { bp2build_available: false },
169}
170cc_library_headers {
171 name: "darwin-lib",
172 bazel_module: { bp2build_available: false },
173}
174cc_library_headers {
175 name: "linux-lib",
176 bazel_module: { bp2build_available: false },
177}
178cc_library_headers {
179 name: "linux_bionic-lib",
180 bazel_module: { bp2build_available: false },
181}
182cc_library_headers {
183 name: "windows-lib",
184 bazel_module: { bp2build_available: false },
185}
Jingwen Chen91220d72021-03-24 02:18:33 -0400186cc_library_headers {
187 name: "foo_headers",
188 header_libs: ["base-lib"],
189 target: {
190 android: { header_libs: ["android-lib"] },
191 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400192 linux_bionic: { header_libs: ["linux_bionic-lib"] },
193 linux_glibc: { header_libs: ["linux-lib"] },
194 windows: { header_libs: ["windows-lib"] },
195 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400196 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400197}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200198 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400199 name = "foo_headers",
Chris Parsonsd6358772021-05-18 18:35:24 -0400200 implementation_deps = [":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000201 "//build/bazel/platforms/os:android": [":android-lib"],
202 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000203 "//build/bazel/platforms/os:linux": [":linux-lib"],
204 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
205 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400206 "//conditions:default": [],
207 }),
Jingwen Chen91220d72021-03-24 02:18:33 -0400208)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200209 })
210}
211
212func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
213 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
214 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
215 moduleTypeUnderTest: "cc_library_headers",
216 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
217 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200218 filesystem: map[string]string{},
219 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400220cc_library_headers {
221 name: "android-lib",
222 bazel_module: { bp2build_available: false },
223 }
224cc_library_headers {
225 name: "exported-lib",
226 bazel_module: { bp2build_available: false },
227}
Jingwen Chen91220d72021-03-24 02:18:33 -0400228cc_library_headers {
229 name: "foo_headers",
230 target: {
231 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
232 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400233 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400234}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200235 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400236 name = "foo_headers",
Jingwen Chen63930982021-03-24 10:04:33 -0400237 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400238 "//build/bazel/platforms/os:android": [":exported-lib"],
239 "//conditions:default": [],
240 }),
241 implementation_deps = select({
242 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400243 "//conditions:default": [],
244 }),
245)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200246 })
247}
248
249func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
250 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
251 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
252 moduleTypeUnderTest: "cc_library_headers",
253 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
254 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200255 filesystem: map[string]string{},
256 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400257 name: "foo_headers",
258 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400259 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400260 ],
261 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400262 android: {
263 export_system_include_dirs: [
264 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400265 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400266 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400267 linux_glibc: {
268 export_system_include_dirs: [
269 "linux_include_dir",
270 ],
271 },
272 darwin: {
273 export_system_include_dirs: [
274 "darwin_include_dir",
275 ],
276 },
277 },
278 arch: {
279 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400280 export_system_include_dirs: [
281 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400282 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400283 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400284 x86_64: {
285 export_system_include_dirs: [
286 "x86_64_include_dir",
287 ],
288 },
289 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400290 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400291}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200292 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400293 name = "foo_headers",
Liz Kammer5fad5012021-09-09 14:08:21 -0400294 export_system_includes = ["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400295 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
296 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
297 "//conditions:default": [],
298 }) + select({
299 "//build/bazel/platforms/os:android": ["android_include_dir"],
300 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
301 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
302 "//conditions:default": [],
303 }),
304)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200305 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000306}
Liz Kammerd366c902021-06-03 13:43:01 -0400307
308func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
309 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
310 description: "cc_library_headers test",
311 moduleTypeUnderTest: "cc_library_headers",
312 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
313 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
314 filesystem: map[string]string{
315 "lib-1/lib1a.h": "",
316 "lib-1/lib1b.h": "",
317 "lib-2/lib2a.h": "",
318 "lib-2/lib2b.h": "",
319 "dir-1/dir1a.h": "",
320 "dir-1/dir1b.h": "",
321 "dir-2/dir2a.h": "",
322 "dir-2/dir2b.h": "",
323 "arch_arm64_exported_include_dir/a.h": "",
324 "arch_x86_exported_include_dir/b.h": "",
325 "arch_x86_64_exported_include_dir/c.h": "",
326 },
327 blueprint: soongCcLibraryHeadersPreamble + `
328cc_library_headers {
329 name: "lib-1",
330 export_include_dirs: ["lib-1"],
331 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400332 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400333}`,
334 expectedBazelTargets: []string{`cc_library_headers(
335 name = "lib-1",
Liz Kammer5fad5012021-09-09 14:08:21 -0400336 export_includes = ["lib-1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400337)`},
338 })
339}