blob: e4cfa358b7763051a88eab4114e7f89f6cf6df54 [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)
31
32func TestCcLibraryHeadersLoadStatement(t *testing.T) {
33 testCases := []struct {
34 bazelTargets BazelTargets
35 expectedLoadStatements string
36 }{
37 {
38 bazelTargets: BazelTargets{
39 BazelTarget{
40 name: "cc_library_headers_target",
41 ruleClass: "cc_library_headers",
42 // Note: no bzlLoadLocation for native rules
43 },
44 },
45 expectedLoadStatements: ``,
46 },
47 }
48
49 for _, testCase := range testCases {
50 actual := testCase.bazelTargets.LoadStatements()
51 expected := testCase.expectedLoadStatements
52 if actual != expected {
53 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
54 }
55 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000056}
57
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020058func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
59 cc.RegisterCCBuildComponents(ctx)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020060}
61
62func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040063 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
65}
66
67func TestCcLibraryHeadersSimple(t *testing.T) {
68 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040069 description: "cc_library_headers test",
70 moduleTypeUnderTest: "cc_library_headers",
71 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020072 filesystem: map[string]string{
73 "lib-1/lib1a.h": "",
74 "lib-1/lib1b.h": "",
75 "lib-2/lib2a.h": "",
76 "lib-2/lib2b.h": "",
77 "dir-1/dir1a.h": "",
78 "dir-1/dir1b.h": "",
79 "dir-2/dir2a.h": "",
80 "dir-2/dir2b.h": "",
81 "arch_arm64_exported_include_dir/a.h": "",
82 "arch_x86_exported_include_dir/b.h": "",
83 "arch_x86_64_exported_include_dir/c.h": "",
84 },
85 blueprint: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000086cc_library_headers {
87 name: "lib-1",
88 export_include_dirs: ["lib-1"],
Liz Kammer8337ea42021-09-10 10:06:32 -040089 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000090}
91
92cc_library_headers {
93 name: "lib-2",
94 export_include_dirs: ["lib-2"],
Liz Kammer8337ea42021-09-10 10:06:32 -040095 bazel_module: { bp2build_available: false },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000096}
97
98cc_library_headers {
99 name: "foo_headers",
100 export_include_dirs: ["dir-1", "dir-2"],
101 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000102
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000103 arch: {
104 arm64: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400105 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000106 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
107 },
108 x86: {
109 export_include_dirs: ["arch_x86_exported_include_dir"],
110 },
111 x86_64: {
112 export_include_dirs: ["arch_x86_64_exported_include_dir"],
113 },
114 },
115
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000116 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000117}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500118 expectedBazelTargets: []string{
119 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
120 "export_includes": `[
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000121 "dir-1",
122 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000123 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000124 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
125 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
126 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000127 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500128 })`,
129 "implementation_deps": `[
Liz Kammer5fad5012021-09-09 14:08:21 -0400130 ":lib-1",
131 ":lib-2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500132 ]`,
133 }),
134 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200135 })
136}
137
Liz Kammer8337ea42021-09-10 10:06:32 -0400138func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200139 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400140 description: "cc_library_headers test with os-specific header_libs props",
141 moduleTypeUnderTest: "cc_library_headers",
142 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
143 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200144 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400145cc_library_headers {
146 name: "android-lib",
147 bazel_module: { bp2build_available: false },
148}
149cc_library_headers {
150 name: "base-lib",
151 bazel_module: { bp2build_available: false },
152}
153cc_library_headers {
154 name: "darwin-lib",
155 bazel_module: { bp2build_available: false },
156}
157cc_library_headers {
158 name: "linux-lib",
159 bazel_module: { bp2build_available: false },
160}
161cc_library_headers {
162 name: "linux_bionic-lib",
163 bazel_module: { bp2build_available: false },
164}
165cc_library_headers {
166 name: "windows-lib",
167 bazel_module: { bp2build_available: false },
168}
Jingwen Chen91220d72021-03-24 02:18:33 -0400169cc_library_headers {
170 name: "foo_headers",
171 header_libs: ["base-lib"],
172 target: {
173 android: { header_libs: ["android-lib"] },
174 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400175 linux_bionic: { header_libs: ["linux_bionic-lib"] },
176 linux_glibc: { header_libs: ["linux-lib"] },
177 windows: { header_libs: ["windows-lib"] },
178 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400179 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400180}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500181 expectedBazelTargets: []string{
182 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
183 "implementation_deps": `[":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000184 "//build/bazel/platforms/os:android": [":android-lib"],
185 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000186 "//build/bazel/platforms/os:linux": [":linux-lib"],
187 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
188 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400189 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500190 })`,
191 }),
192 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200193 })
194}
195
196func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
197 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400198 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
199 moduleTypeUnderTest: "cc_library_headers",
200 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
201 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200202 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400203cc_library_headers {
204 name: "android-lib",
205 bazel_module: { bp2build_available: false },
206 }
207cc_library_headers {
208 name: "exported-lib",
209 bazel_module: { bp2build_available: false },
210}
Jingwen Chen91220d72021-03-24 02:18:33 -0400211cc_library_headers {
212 name: "foo_headers",
213 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400214 android: {
215 header_libs: ["android-lib", "exported-lib"],
216 export_header_lib_headers: ["exported-lib"]
217 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400218 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400219 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400220}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500221 expectedBazelTargets: []string{
222 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
223 "deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400224 "//build/bazel/platforms/os:android": [":exported-lib"],
225 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500226 })`,
227 "implementation_deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400228 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400229 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 })`,
231 }),
232 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200233 })
234}
235
236func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
237 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400238 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
239 moduleTypeUnderTest: "cc_library_headers",
240 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
241 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200242 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400243 name: "foo_headers",
244 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400245 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400246 ],
247 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400248 android: {
249 export_system_include_dirs: [
250 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400251 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400252 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400253 linux_glibc: {
254 export_system_include_dirs: [
255 "linux_include_dir",
256 ],
257 },
258 darwin: {
259 export_system_include_dirs: [
260 "darwin_include_dir",
261 ],
262 },
263 },
264 arch: {
265 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400266 export_system_include_dirs: [
267 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400268 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400269 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400270 x86_64: {
271 export_system_include_dirs: [
272 "x86_64_include_dir",
273 ],
274 },
275 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400276 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400277}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500278 expectedBazelTargets: []string{
279 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
280 "export_system_includes": `["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400281 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
282 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
283 "//conditions:default": [],
284 }) + select({
285 "//build/bazel/platforms/os:android": ["android_include_dir"],
286 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
287 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
288 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500289 })`,
290 }),
291 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200292 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000293}
Liz Kammerd366c902021-06-03 13:43:01 -0400294
295func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
296 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400297 description: "cc_library_headers test",
298 moduleTypeUnderTest: "cc_library_headers",
299 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
Liz Kammerd366c902021-06-03 13:43:01 -0400300 filesystem: map[string]string{
301 "lib-1/lib1a.h": "",
302 "lib-1/lib1b.h": "",
303 "lib-2/lib2a.h": "",
304 "lib-2/lib2b.h": "",
305 "dir-1/dir1a.h": "",
306 "dir-1/dir1b.h": "",
307 "dir-2/dir2a.h": "",
308 "dir-2/dir2b.h": "",
309 "arch_arm64_exported_include_dir/a.h": "",
310 "arch_x86_exported_include_dir/b.h": "",
311 "arch_x86_64_exported_include_dir/c.h": "",
312 },
313 blueprint: soongCcLibraryHeadersPreamble + `
314cc_library_headers {
315 name: "lib-1",
316 export_include_dirs: ["lib-1"],
317 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400318 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400319}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500320 expectedBazelTargets: []string{
321 makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{
322 "export_includes": `["lib-1"]`,
323 }),
324 },
Liz Kammerd366c902021-06-03 13:43:01 -0400325 })
326}