blob: 76fdab2f4d7c9d8611ee5b6e8d297519611680c6 [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}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500131 expectedBazelTargets: []string{
132 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
133 "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": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500141 })`,
142 "implementation_deps": `[
Liz Kammer5fad5012021-09-09 14:08:21 -0400143 ":lib-1",
144 ":lib-2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500145 ]`,
146 }),
147 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200148 })
149}
150
Liz Kammer8337ea42021-09-10 10:06:32 -0400151func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200152 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
153 description: "cc_library_headers test with os-specific header_libs props",
154 moduleTypeUnderTest: "cc_library_headers",
155 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
156 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200157 filesystem: map[string]string{},
158 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400159cc_library_headers {
160 name: "android-lib",
161 bazel_module: { bp2build_available: false },
162}
163cc_library_headers {
164 name: "base-lib",
165 bazel_module: { bp2build_available: false },
166}
167cc_library_headers {
168 name: "darwin-lib",
169 bazel_module: { bp2build_available: false },
170}
171cc_library_headers {
172 name: "linux-lib",
173 bazel_module: { bp2build_available: false },
174}
175cc_library_headers {
176 name: "linux_bionic-lib",
177 bazel_module: { bp2build_available: false },
178}
179cc_library_headers {
180 name: "windows-lib",
181 bazel_module: { bp2build_available: false },
182}
Jingwen Chen91220d72021-03-24 02:18:33 -0400183cc_library_headers {
184 name: "foo_headers",
185 header_libs: ["base-lib"],
186 target: {
187 android: { header_libs: ["android-lib"] },
188 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400189 linux_bionic: { header_libs: ["linux_bionic-lib"] },
190 linux_glibc: { header_libs: ["linux-lib"] },
191 windows: { header_libs: ["windows-lib"] },
192 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400193 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400194}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500195 expectedBazelTargets: []string{
196 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
197 "implementation_deps": `[":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000198 "//build/bazel/platforms/os:android": [":android-lib"],
199 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000200 "//build/bazel/platforms/os:linux": [":linux-lib"],
201 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
202 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400203 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500204 })`,
205 }),
206 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200207 })
208}
209
210func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
211 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
212 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
213 moduleTypeUnderTest: "cc_library_headers",
214 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
215 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200216 filesystem: map[string]string{},
217 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400218cc_library_headers {
219 name: "android-lib",
220 bazel_module: { bp2build_available: false },
221 }
222cc_library_headers {
223 name: "exported-lib",
224 bazel_module: { bp2build_available: false },
225}
Jingwen Chen91220d72021-03-24 02:18:33 -0400226cc_library_headers {
227 name: "foo_headers",
228 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400229 android: {
230 header_libs: ["android-lib", "exported-lib"],
231 export_header_lib_headers: ["exported-lib"]
232 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400233 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400234 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400235}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500236 expectedBazelTargets: []string{
237 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
238 "deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400239 "//build/bazel/platforms/os:android": [":exported-lib"],
240 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500241 })`,
242 "implementation_deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400243 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400244 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500245 })`,
246 }),
247 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200248 })
249}
250
251func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
252 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
253 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
254 moduleTypeUnderTest: "cc_library_headers",
255 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
256 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200257 filesystem: map[string]string{},
258 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400259 name: "foo_headers",
260 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400261 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400262 ],
263 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400264 android: {
265 export_system_include_dirs: [
266 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400267 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400268 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400269 linux_glibc: {
270 export_system_include_dirs: [
271 "linux_include_dir",
272 ],
273 },
274 darwin: {
275 export_system_include_dirs: [
276 "darwin_include_dir",
277 ],
278 },
279 },
280 arch: {
281 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400282 export_system_include_dirs: [
283 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400284 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400285 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400286 x86_64: {
287 export_system_include_dirs: [
288 "x86_64_include_dir",
289 ],
290 },
291 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400292 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400293}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 expectedBazelTargets: []string{
295 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
296 "export_system_includes": `["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400297 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
298 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
299 "//conditions:default": [],
300 }) + select({
301 "//build/bazel/platforms/os:android": ["android_include_dir"],
302 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
303 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
304 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 })`,
306 }),
307 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200308 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000309}
Liz Kammerd366c902021-06-03 13:43:01 -0400310
311func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
312 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
313 description: "cc_library_headers test",
314 moduleTypeUnderTest: "cc_library_headers",
315 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
316 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
317 filesystem: map[string]string{
318 "lib-1/lib1a.h": "",
319 "lib-1/lib1b.h": "",
320 "lib-2/lib2a.h": "",
321 "lib-2/lib2b.h": "",
322 "dir-1/dir1a.h": "",
323 "dir-1/dir1b.h": "",
324 "dir-2/dir2a.h": "",
325 "dir-2/dir2b.h": "",
326 "arch_arm64_exported_include_dir/a.h": "",
327 "arch_x86_exported_include_dir/b.h": "",
328 "arch_x86_64_exported_include_dir/c.h": "",
329 },
330 blueprint: soongCcLibraryHeadersPreamble + `
331cc_library_headers {
332 name: "lib-1",
333 export_include_dirs: ["lib-1"],
334 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400335 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400336}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500337 expectedBazelTargets: []string{
338 makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{
339 "export_includes": `["lib-1"]`,
340 }),
341 },
Liz Kammerd366c902021-06-03 13:43:01 -0400342 })
343}