blob: 00042abcb3ed8df791058e29754ce9eef62cb673 [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 (
18 "android/soong/android"
19 "android/soong/cc"
20 "strings"
21 "testing"
22)
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 {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 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 }
67
68}
69
70func TestCcLibraryHeadersBp2Build(t *testing.T) {
71 testCases := []struct {
72 description string
73 moduleTypeUnderTest string
74 moduleTypeUnderTestFactory android.ModuleFactory
75 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
76 preArchMutators []android.RegisterMutatorFunc
77 depsMutators []android.RegisterMutatorFunc
78 bp string
79 expectedBazelTargets []string
80 filesystem map[string]string
81 dir string
82 }{
83 {
84 description: "cc_library_headers test",
85 moduleTypeUnderTest: "cc_library_headers",
86 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
87 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
88 filesystem: map[string]string{
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000089 "lib-1/lib1a.h": "",
90 "lib-1/lib1b.h": "",
91 "lib-2/lib2a.h": "",
92 "lib-2/lib2b.h": "",
93 "dir-1/dir1a.h": "",
94 "dir-1/dir1b.h": "",
95 "dir-2/dir2a.h": "",
96 "dir-2/dir2b.h": "",
97 "arch_arm64_exported_include_dir/a.h": "",
98 "arch_x86_exported_include_dir/b.h": "",
99 "arch_x86_64_exported_include_dir/c.h": "",
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000100 },
Jingwen Chen63930982021-03-24 10:04:33 -0400101 bp: soongCcLibraryHeadersPreamble + `
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000102cc_library_headers {
103 name: "lib-1",
104 export_include_dirs: ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000105}
106
107cc_library_headers {
108 name: "lib-2",
109 export_include_dirs: ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000110}
111
112cc_library_headers {
113 name: "foo_headers",
114 export_include_dirs: ["dir-1", "dir-2"],
115 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000116
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000117 arch: {
118 arm64: {
119 // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs
120 export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"],
121 },
122 x86: {
123 export_include_dirs: ["arch_x86_exported_include_dir"],
124 },
125 x86_64: {
126 export_include_dirs: ["arch_x86_64_exported_include_dir"],
127 },
128 },
129
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000130 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000131}`,
132 expectedBazelTargets: []string{`cc_library_headers(
133 name = "foo_headers",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000134 copts = ["-I."],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000135 deps = [
136 ":lib-1",
137 ":lib-2",
138 ],
139 hdrs = [
140 "dir-1/dir1a.h",
141 "dir-1/dir1b.h",
142 "dir-2/dir2a.h",
143 "dir-2/dir2b.h",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000144 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000145 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir/a.h"],
146 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir/b.h"],
147 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir/c.h"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000148 "//conditions:default": [],
149 }),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000150 includes = [
151 "dir-1",
152 "dir-2",
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000153 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000154 "//build/bazel/platforms/arch:arm64": ["arch_arm64_exported_include_dir"],
155 "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
156 "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000157 "//conditions:default": [],
158 }),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000159)`, `cc_library_headers(
160 name = "lib-1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000161 copts = ["-I."],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000162 hdrs = [
163 "lib-1/lib1a.h",
164 "lib-1/lib1b.h",
165 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000166 includes = ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000167)`, `cc_library_headers(
168 name = "lib-2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000169 copts = ["-I."],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000170 hdrs = [
171 "lib-2/lib2a.h",
172 "lib-2/lib2b.h",
173 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000174 includes = ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000175)`},
176 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400177 {
178 description: "cc_library_headers test with os-specific header_libs props",
179 moduleTypeUnderTest: "cc_library_headers",
180 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
181 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
182 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
183 filesystem: map[string]string{},
184 bp: soongCcLibraryPreamble + `
185cc_library_headers { name: "android-lib" }
186cc_library_headers { name: "base-lib" }
187cc_library_headers { name: "darwin-lib" }
188cc_library_headers { name: "fuchsia-lib" }
189cc_library_headers { name: "linux-lib" }
190cc_library_headers { name: "linux_bionic-lib" }
191cc_library_headers { name: "windows-lib" }
192cc_library_headers {
193 name: "foo_headers",
194 header_libs: ["base-lib"],
195 target: {
196 android: { header_libs: ["android-lib"] },
197 darwin: { header_libs: ["darwin-lib"] },
198 fuchsia: { header_libs: ["fuchsia-lib"] },
199 linux_bionic: { header_libs: ["linux_bionic-lib"] },
200 linux_glibc: { header_libs: ["linux-lib"] },
201 windows: { header_libs: ["windows-lib"] },
202 },
203 bazel_module: { bp2build_available: true },
204}`,
205 expectedBazelTargets: []string{`cc_library_headers(
206 name = "android-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000207 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400208)`, `cc_library_headers(
209 name = "base-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000210 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400211)`, `cc_library_headers(
212 name = "darwin-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000213 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400214)`, `cc_library_headers(
215 name = "foo_headers",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000216 copts = ["-I."],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000217 deps = [":base-lib"] + select({
218 "//build/bazel/platforms/os:android": [":android-lib"],
219 "//build/bazel/platforms/os:darwin": [":darwin-lib"],
220 "//build/bazel/platforms/os:fuchsia": [":fuchsia-lib"],
221 "//build/bazel/platforms/os:linux": [":linux-lib"],
222 "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
223 "//build/bazel/platforms/os:windows": [":windows-lib"],
Jingwen Chen91220d72021-03-24 02:18:33 -0400224 "//conditions:default": [],
225 }),
226)`, `cc_library_headers(
227 name = "fuchsia-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000228 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400229)`, `cc_library_headers(
230 name = "linux-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000231 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400232)`, `cc_library_headers(
233 name = "linux_bionic-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000234 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400235)`, `cc_library_headers(
236 name = "windows-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000237 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400238)`},
239 },
240 {
241 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
242 moduleTypeUnderTest: "cc_library_headers",
243 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
244 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
245 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
246 filesystem: map[string]string{},
247 bp: soongCcLibraryPreamble + `
248cc_library_headers { name: "android-lib" }
249cc_library_headers { name: "exported-lib" }
250cc_library_headers {
251 name: "foo_headers",
252 target: {
253 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
254 },
255}`,
256 expectedBazelTargets: []string{`cc_library_headers(
257 name = "android-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000258 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400259)`, `cc_library_headers(
260 name = "exported-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000261 copts = ["-I."],
Jingwen Chen91220d72021-03-24 02:18:33 -0400262)`, `cc_library_headers(
263 name = "foo_headers",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000264 copts = ["-I."],
Jingwen Chen63930982021-03-24 10:04:33 -0400265 deps = select({
Jingwen Chen91220d72021-03-24 02:18:33 -0400266 "//build/bazel/platforms/os:android": [
267 ":android-lib",
268 ":exported-lib",
269 ],
270 "//conditions:default": [],
271 }),
272)`},
273 },
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400274 {
275 description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
276 moduleTypeUnderTest: "cc_library_headers",
277 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
278 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
279 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
280 filesystem: map[string]string{},
281 bp: soongCcLibraryPreamble + `cc_library_headers {
282 name: "foo_headers",
283 export_system_include_dirs: [
284 "shared_include_dir",
285 ],
286 target: {
287 android: {
288 export_system_include_dirs: [
289 "android_include_dir",
290 ],
291 },
292 linux_glibc: {
293 export_system_include_dirs: [
294 "linux_include_dir",
295 ],
296 },
297 darwin: {
298 export_system_include_dirs: [
299 "darwin_include_dir",
300 ],
301 },
302 },
303 arch: {
304 arm: {
305 export_system_include_dirs: [
306 "arm_include_dir",
307 ],
308 },
309 x86_64: {
310 export_system_include_dirs: [
311 "x86_64_include_dir",
312 ],
313 },
314 },
315}`,
316 expectedBazelTargets: []string{`cc_library_headers(
317 name = "foo_headers",
318 copts = ["-I."],
319 includes = ["shared_include_dir"] + select({
320 "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
321 "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
322 "//conditions:default": [],
323 }) + select({
324 "//build/bazel/platforms/os:android": ["android_include_dir"],
325 "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
326 "//build/bazel/platforms/os:linux": ["linux_include_dir"],
327 "//conditions:default": [],
328 }),
329)`},
330 },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000331 }
332
333 dir := "."
334 for _, testCase := range testCases {
335 filesystem := make(map[string][]byte)
336 toParse := []string{
337 "Android.bp",
338 }
339 for f, content := range testCase.filesystem {
340 if strings.HasSuffix(f, "Android.bp") {
341 toParse = append(toParse, f)
342 }
343 filesystem[f] = []byte(content)
344 }
345 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
346 ctx := android.NewTestContext(config)
347
Jingwen Chen91220d72021-03-24 02:18:33 -0400348 // TODO(jingwen): make this default for all bp2build tests
349 ctx.RegisterBp2BuildConfig(bp2buildConfig)
350
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000351 cc.RegisterCCBuildComponents(ctx)
352 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
353
354 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
355 for _, m := range testCase.depsMutators {
356 ctx.DepsBp2BuildMutators(m)
357 }
358 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
359 ctx.RegisterForBazelConversion()
360
361 _, errs := ctx.ParseFileList(dir, toParse)
362 if Errored(t, testCase.description, errs) {
363 continue
364 }
365 _, errs = ctx.ResolveDependencies(config)
366 if Errored(t, testCase.description, errs) {
367 continue
368 }
369
370 checkDir := dir
371 if testCase.dir != "" {
372 checkDir = testCase.dir
373 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500374 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500375 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000376 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
377 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
378 } else {
379 for i, target := range bazelTargets {
380 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
381 t.Errorf(
382 "%s: Expected generated Bazel target to be '%s', got '%s'",
383 testCase.description,
384 w,
385 g,
386 )
387 }
388 }
389 }
390 }
391}