blob: e5bb120109a0a4e1cc543ff8bfc1c323a9550e61 [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 },
Yu Liufc603162022-03-01 15:44:08 -0800115 sdk_version: "current",
116 min_sdk_version: "29",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000117
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000118 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000119}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500120 expectedBazelTargets: []string{
121 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
122 "export_includes": `[
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000123 "dir-1",
124 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000125 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000126 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
127 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
128 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000129 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500130 })`,
131 "implementation_deps": `[
Liz Kammer5fad5012021-09-09 14:08:21 -0400132 ":lib-1",
133 ":lib-2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500134 ]`,
Yu Liufc603162022-03-01 15:44:08 -0800135 "sdk_version": `"current"`,
136 "min_sdk_version": `"29"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500137 }),
138 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200139 })
140}
141
Liz Kammer8337ea42021-09-10 10:06:32 -0400142func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) {
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200143 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400144 description: "cc_library_headers test with os-specific header_libs props",
145 moduleTypeUnderTest: "cc_library_headers",
146 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
147 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200148 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400149cc_library_headers {
150 name: "android-lib",
151 bazel_module: { bp2build_available: false },
152}
153cc_library_headers {
154 name: "base-lib",
155 bazel_module: { bp2build_available: false },
156}
157cc_library_headers {
158 name: "darwin-lib",
159 bazel_module: { bp2build_available: false },
160}
161cc_library_headers {
162 name: "linux-lib",
163 bazel_module: { bp2build_available: false },
164}
165cc_library_headers {
166 name: "linux_bionic-lib",
167 bazel_module: { bp2build_available: false },
168}
169cc_library_headers {
170 name: "windows-lib",
171 bazel_module: { bp2build_available: false },
172}
Jingwen Chen91220d72021-03-24 02:18:33 -0400173cc_library_headers {
174 name: "foo_headers",
175 header_libs: ["base-lib"],
176 target: {
177 android: { header_libs: ["android-lib"] },
178 darwin: { header_libs: ["darwin-lib"] },
Jingwen Chen91220d72021-03-24 02:18:33 -0400179 linux_bionic: { header_libs: ["linux_bionic-lib"] },
180 linux_glibc: { header_libs: ["linux-lib"] },
181 windows: { header_libs: ["windows-lib"] },
182 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400183 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400184}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500185 expectedBazelTargets: []string{
186 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
187 "implementation_deps": `[":base-lib"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000188 "//build/bazel/platforms/os:android": [":android-lib"],
189 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000190 "//build/bazel/platforms/os:linux": [":linux-lib"],
191 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
192 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400193 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500194 })`,
195 }),
196 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200197 })
198}
199
200func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
201 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400202 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
203 moduleTypeUnderTest: "cc_library_headers",
204 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
205 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200206 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -0400207cc_library_headers {
208 name: "android-lib",
209 bazel_module: { bp2build_available: false },
210 }
211cc_library_headers {
212 name: "exported-lib",
213 bazel_module: { bp2build_available: false },
214}
Jingwen Chen91220d72021-03-24 02:18:33 -0400215cc_library_headers {
216 name: "foo_headers",
217 target: {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400218 android: {
219 header_libs: ["android-lib", "exported-lib"],
220 export_header_lib_headers: ["exported-lib"]
221 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400222 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400223 include_build_directory: false,
Jingwen Chen91220d72021-03-24 02:18:33 -0400224}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500225 expectedBazelTargets: []string{
226 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
227 "deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400228 "//build/bazel/platforms/os:android": [":exported-lib"],
229 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 })`,
231 "implementation_deps": `select({
Chris Parsonsd6358772021-05-18 18:35:24 -0400232 "//build/bazel/platforms/os:android": [":android-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400233 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500234 })`,
235 }),
236 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200237 })
238}
239
240func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
241 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400242 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
243 moduleTypeUnderTest: "cc_library_headers",
244 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
245 filesystem: map[string]string{},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200246 blueprint: soongCcLibraryPreamble + `cc_library_headers {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400247 name: "foo_headers",
248 export_system_include_dirs: [
Liz Kammer8337ea42021-09-10 10:06:32 -0400249 "shared_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400250 ],
251 target: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400252 android: {
253 export_system_include_dirs: [
254 "android_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400255 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400256 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400257 linux_glibc: {
258 export_system_include_dirs: [
259 "linux_include_dir",
260 ],
261 },
262 darwin: {
263 export_system_include_dirs: [
264 "darwin_include_dir",
265 ],
266 },
267 },
268 arch: {
269 arm: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400270 export_system_include_dirs: [
271 "arm_include_dir",
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400272 ],
Liz Kammer8337ea42021-09-10 10:06:32 -0400273 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400274 x86_64: {
275 export_system_include_dirs: [
276 "x86_64_include_dir",
277 ],
278 },
279 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400280 include_build_directory: false,
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400281}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500282 expectedBazelTargets: []string{
283 makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
284 "export_system_includes": `["shared_include_dir"] + select({
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400285 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
286 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
287 "//conditions:default": [],
288 }) + select({
289 "//build/bazel/platforms/os:android": ["android_include_dir"],
290 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
291 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
292 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500293 })`,
294 }),
295 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200296 })
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000297}
Liz Kammerd366c902021-06-03 13:43:01 -0400298
299func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) {
300 runCcLibraryHeadersTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400301 description: "cc_library_headers test",
302 moduleTypeUnderTest: "cc_library_headers",
303 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
Liz Kammerd366c902021-06-03 13:43:01 -0400304 filesystem: map[string]string{
305 "lib-1/lib1a.h": "",
306 "lib-1/lib1b.h": "",
307 "lib-2/lib2a.h": "",
308 "lib-2/lib2b.h": "",
309 "dir-1/dir1a.h": "",
310 "dir-1/dir1b.h": "",
311 "dir-2/dir2a.h": "",
312 "dir-2/dir2b.h": "",
313 "arch_arm64_exported_include_dir/a.h": "",
314 "arch_x86_exported_include_dir/b.h": "",
315 "arch_x86_64_exported_include_dir/c.h": "",
316 },
317 blueprint: soongCcLibraryHeadersPreamble + `
318cc_library_headers {
319 name: "lib-1",
320 export_include_dirs: ["lib-1"],
321 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400322 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -0400323}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500324 expectedBazelTargets: []string{
325 makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{
326 "export_includes": `["lib-1"]`,
327 }),
328 },
Liz Kammerd366c902021-06-03 13:43:01 -0400329 })
330}