blob: 37d806cb17f7882a1e3ea5b3856b831a78c67bb6 [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: {
227 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
228 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400229 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400230}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200231 expectedBazelTargets: []string{`cc_library_headers(
Jingwen Chen91220d72021-03-24 02:18:33 -0400232 name = "foo_headers",
Jingwen Chen63930982021-03-24 10:04:33 -0400233 deps = select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400234 "//build/bazel/platforms/os:android": [":exported-lib"],
235 "//conditions:default": [],
236 }),
237 implementation_deps = select({
238 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400239 "//conditions:default": [],
240 }),
241)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200242 })
243}
244
245func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
246 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
247 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
248 moduleTypeUnderTest: "cc_library_headers",
249 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
250 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200251 filesystem: map[string]string{},
252 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400253 name: "foo_headers",
254 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400255 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400256 ],
257 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400258 android: {
259 export_system_include_dirs: [
260 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400261 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400262 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400263 linux_glibc: {
264 export_system_include_dirs: [
265 "linux_include_dir",
266 ],
267 },
268 darwin: {
269 export_system_include_dirs: [
270 "darwin_include_dir",
271 ],
272 },
273 },
274 arch: {
275 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400276 export_system_include_dirs: [
277 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400278 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400279 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400280 x86_64: {
281 export_system_include_dirs: [
282 "x86_64_include_dir",
283 ],
284 },
285 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400286 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400287}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200288 expectedBazelTargets: []string{`cc_library_headers(
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400289 name = "foo_headers",
Liz Kammer5fad5012021-09-09 14:08:21 -0400290 export_system_includes = ["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400291 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
292 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
293 "//conditions:default": [],
294 }) + select({
295 "//build/bazel/platforms/os:android": ["android_include_dir"],
296 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
297 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
298 "//conditions:default": [],
299 }),
300)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200301 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000302}
Liz Kammerd366c902021-06-03 13:43:01 -0400303
304func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
305 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
306 description: "cc_library_headers test",
307 moduleTypeUnderTest: "cc_library_headers",
308 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
309 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
310 filesystem: map[string]string{
311 "lib-1/lib1a.h": "",
312 "lib-1/lib1b.h": "",
313 "lib-2/lib2a.h": "",
314 "lib-2/lib2b.h": "",
315 "dir-1/dir1a.h": "",
316 "dir-1/dir1b.h": "",
317 "dir-2/dir2a.h": "",
318 "dir-2/dir2b.h": "",
319 "arch_arm64_exported_include_dir/a.h": "",
320 "arch_x86_exported_include_dir/b.h": "",
321 "arch_x86_64_exported_include_dir/c.h": "",
322 },
323 blueprint: soongCcLibraryHeadersPreamble + `
324cc_library_headers {
325 name: "lib-1",
326 export_include_dirs: ["lib-1"],
327 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400328 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400329}`,
330 expectedBazelTargets: []string{`cc_library_headers(
331 name = "lib-1",
Liz Kammer5fad5012021-09-09 14:08:21 -0400332 export_includes = ["lib-1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400333)`},
334 })
335}