blob: 594c05009b795e28fb93bbcc69f473d00283007b [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{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040081 description: "cc_library_headers test",
82 moduleTypeUnderTest: "cc_library_headers",
83 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020084 filesystem: map[string]string{
85 "lib-1/lib1a.h": "",
86 "lib-1/lib1b.h": "",
87 "lib-2/lib2a.h": "",
88 "lib-2/lib2b.h": "",
89 "dir-1/dir1a.h": "",
90 "dir-1/dir1b.h": "",
91 "dir-2/dir2a.h": "",
92 "dir-2/dir2b.h": "",
93 "arch_arm64_exported_include_dir/a.h": "",
94 "arch_x86_exported_include_dir/b.h": "",
95 "arch_x86_64_exported_include_dir/c.h": "",
96 },
97 blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000098cc_library_headers {
99 name: "lib-1",
100 export_include_dirs: ["lib-1"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400101 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000102}
103
104cc_library_headers {
105 name: "lib-2",
106 export_include_dirs: ["lib-2"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400107 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000108}
109
110cc_library_headers {
111 name: "foo_headers",
112 export_include_dirs: ["dir-1", "dir-2"],
113 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000114
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000115 arch: {
116 arm64: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400117 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000118 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
119 },
120 x86: {
121 export_include_dirs: ["arch_x86_exported_include_dir"],
122 },
123 x86_64: {
124 export_include_dirs: ["arch_x86_64_exported_include_dir"],
125 },
126 },
127
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000128 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000129}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500130 expectedBazelTargets: []string{
131 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
132 "export_includes": `[
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000133 "dir-1",
134 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000135 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000136 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
137 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
138 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000139 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500140 })`,
141 "implementation_deps": `[
Liz Kammer5fad5012021-09-09 14:08:21 -0400142 ":lib-1",
143 ":lib-2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500144 ]`,
145 }),
146 },
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{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400152 description: "cc_library_headers test with os-specific header_libs props",
153 moduleTypeUnderTest: "cc_library_headers",
154 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
155 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200156 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400157cc_library_headers {
158 name: "android-lib",
159 bazel_module: { bp2build_available: false },
160}
161cc_library_headers {
162 name: "base-lib",
163 bazel_module: { bp2build_available: false },
164}
165cc_library_headers {
166 name: "darwin-lib",
167 bazel_module: { bp2build_available: false },
168}
169cc_library_headers {
170 name: "linux-lib",
171 bazel_module: { bp2build_available: false },
172}
173cc_library_headers {
174 name: "linux_bionic-lib",
175 bazel_module: { bp2build_available: false },
176}
177cc_library_headers {
178 name: "windows-lib",
179 bazel_module: { bp2build_available: false },
180}
Jingwen Chen91220d72021-03-24 02:18:33 -0400181cc_library_headers {
182 name: "foo_headers",
183 header_libs: ["base-lib"],
184 target: {
185 android: { header_libs: ["android-lib"] },
186 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400187 linux_bionic: { header_libs: ["linux_bionic-lib"] },
188 linux_glibc: { header_libs: ["linux-lib"] },
189 windows: { header_libs: ["windows-lib"] },
190 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400191 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400192}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500193 expectedBazelTargets: []string{
194 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
195 "implementation_deps": `[":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000196 "//build/bazel/platforms/os:android": [":android-lib"],
197 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000198 "//build/bazel/platforms/os:linux": [":linux-lib"],
199 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
200 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400201 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500202 })`,
203 }),
204 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200205 })
206}
207
208func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
209 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400210 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 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200214 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400215cc_library_headers {
216 name: "android-lib",
217 bazel_module: { bp2build_available: false },
218 }
219cc_library_headers {
220 name: "exported-lib",
221 bazel_module: { bp2build_available: false },
222}
Jingwen Chen91220d72021-03-24 02:18:33 -0400223cc_library_headers {
224 name: "foo_headers",
225 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400226 android: {
227 header_libs: ["android-lib", "exported-lib"],
228 export_header_lib_headers: ["exported-lib"]
229 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400230 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400231 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400232}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500233 expectedBazelTargets: []string{
234 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
235 "deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400236 "//build/bazel/platforms/os:android": [":exported-lib"],
237 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500238 })`,
239 "implementation_deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400240 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400241 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500242 })`,
243 }),
244 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200245 })
246}
247
248func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
249 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400250 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 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200254 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400255 name: "foo_headers",
256 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400257 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400258 ],
259 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400260 android: {
261 export_system_include_dirs: [
262 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400263 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400264 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400265 linux_glibc: {
266 export_system_include_dirs: [
267 "linux_include_dir",
268 ],
269 },
270 darwin: {
271 export_system_include_dirs: [
272 "darwin_include_dir",
273 ],
274 },
275 },
276 arch: {
277 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400278 export_system_include_dirs: [
279 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400280 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400281 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400282 x86_64: {
283 export_system_include_dirs: [
284 "x86_64_include_dir",
285 ],
286 },
287 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400288 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400289}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500290 expectedBazelTargets: []string{
291 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
292 "export_system_includes": `["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400293 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
294 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
295 "//conditions:default": [],
296 }) + select({
297 "//build/bazel/platforms/os:android": ["android_include_dir"],
298 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
299 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
300 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500301 })`,
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{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400309 description: "cc_library_headers test",
310 moduleTypeUnderTest: "cc_library_headers",
311 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
Liz Kammerd366c902021-06-03 13:43:01 -0400312 filesystem: map[string]string{
313 "lib-1/lib1a.h": "",
314 "lib-1/lib1b.h": "",
315 "lib-2/lib2a.h": "",
316 "lib-2/lib2b.h": "",
317 "dir-1/dir1a.h": "",
318 "dir-1/dir1b.h": "",
319 "dir-2/dir2a.h": "",
320 "dir-2/dir2b.h": "",
321 "arch_arm64_exported_include_dir/a.h": "",
322 "arch_x86_exported_include_dir/b.h": "",
323 "arch_x86_64_exported_include_dir/c.h": "",
324 },
325 blueprint: soongCcLibraryHeadersPreamble + `
326cc_library_headers {
327 name: "lib-1",
328 export_include_dirs: ["lib-1"],
329 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400330 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400331}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500332 expectedBazelTargets: []string{
333 makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{
334 "export_includes": `["lib-1"]`,
335 }),
336 },
Liz Kammerd366c902021-06-03 13:43:01 -0400337 })
338}