blob: d828168ac76ad7e06c94609e2fe03acd6053c148 [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
26 soongCcLibraryPreamble = `
27cc_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: "",
40}
41
42toolchain_library {
43 name: "libatomic",
44 defaults: ["linux_bionic_supported"],
45 vendor_available: true,
46 vendor_ramdisk_available: true,
47 product_available: true,
48 recovery_available: true,
49 native_bridge_supported: true,
50 src: "",
51}`
52)
53
54func TestCcLibraryHeadersLoadStatement(t *testing.T) {
55 testCases := []struct {
56 bazelTargets BazelTargets
57 expectedLoadStatements string
58 }{
59 {
60 bazelTargets: BazelTargets{
61 BazelTarget{
62 name: "cc_library_headers_target",
63 ruleClass: "cc_library_headers",
64 // Note: no bzlLoadLocation for native rules
65 },
66 },
67 expectedLoadStatements: ``,
68 },
69 }
70
71 for _, testCase := range testCases {
72 actual := testCase.bazelTargets.LoadStatements()
73 expected := testCase.expectedLoadStatements
74 if actual != expected {
75 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
76 }
77 }
78
79}
80
81func TestCcLibraryHeadersBp2Build(t *testing.T) {
82 testCases := []struct {
83 description string
84 moduleTypeUnderTest string
85 moduleTypeUnderTestFactory android.ModuleFactory
86 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
87 preArchMutators []android.RegisterMutatorFunc
88 depsMutators []android.RegisterMutatorFunc
89 bp string
90 expectedBazelTargets []string
91 filesystem map[string]string
92 dir string
93 }{
94 {
95 description: "cc_library_headers test",
96 moduleTypeUnderTest: "cc_library_headers",
97 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
98 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
99 filesystem: map[string]string{
100 "lib-1/lib1a.h": "",
101 "lib-1/lib1b.h": "",
102 "lib-2/lib2a.h": "",
103 "lib-2/lib2b.h": "",
104 "dir-1/dir1a.h": "",
105 "dir-1/dir1b.h": "",
106 "dir-2/dir2a.h": "",
107 "dir-2/dir2b.h": "",
108 },
109 bp: soongCcLibraryPreamble + `
110cc_library_headers {
111 name: "lib-1",
112 export_include_dirs: ["lib-1"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000113}
114
115cc_library_headers {
116 name: "lib-2",
117 export_include_dirs: ["lib-2"],
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000118}
119
120cc_library_headers {
121 name: "foo_headers",
122 export_include_dirs: ["dir-1", "dir-2"],
123 header_libs: ["lib-1", "lib-2"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000124
125 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000126}`,
127 expectedBazelTargets: []string{`cc_library_headers(
128 name = "foo_headers",
129 deps = [
130 ":lib-1",
131 ":lib-2",
132 ],
133 hdrs = [
134 "dir-1/dir1a.h",
135 "dir-1/dir1b.h",
136 "dir-2/dir2a.h",
137 "dir-2/dir2b.h",
138 ],
139 includes = [
140 "dir-1",
141 "dir-2",
142 ],
143)`, `cc_library_headers(
144 name = "lib-1",
145 hdrs = [
146 "lib-1/lib1a.h",
147 "lib-1/lib1b.h",
148 ],
149 includes = [
150 "lib-1",
151 ],
152)`, `cc_library_headers(
153 name = "lib-2",
154 hdrs = [
155 "lib-2/lib2a.h",
156 "lib-2/lib2b.h",
157 ],
158 includes = [
159 "lib-2",
160 ],
161)`},
162 },
Jingwen Chen91220d72021-03-24 02:18:33 -0400163 {
164 description: "cc_library_headers test with os-specific header_libs props",
165 moduleTypeUnderTest: "cc_library_headers",
166 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
167 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
168 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
169 filesystem: map[string]string{},
170 bp: soongCcLibraryPreamble + `
171cc_library_headers { name: "android-lib" }
172cc_library_headers { name: "base-lib" }
173cc_library_headers { name: "darwin-lib" }
174cc_library_headers { name: "fuchsia-lib" }
175cc_library_headers { name: "linux-lib" }
176cc_library_headers { name: "linux_bionic-lib" }
177cc_library_headers { name: "windows-lib" }
178cc_library_headers {
179 name: "foo_headers",
180 header_libs: ["base-lib"],
181 target: {
182 android: { header_libs: ["android-lib"] },
183 darwin: { header_libs: ["darwin-lib"] },
184 fuchsia: { header_libs: ["fuchsia-lib"] },
185 linux_bionic: { header_libs: ["linux_bionic-lib"] },
186 linux_glibc: { header_libs: ["linux-lib"] },
187 windows: { header_libs: ["windows-lib"] },
188 },
189 bazel_module: { bp2build_available: true },
190}`,
191 expectedBazelTargets: []string{`cc_library_headers(
192 name = "android-lib",
193)`, `cc_library_headers(
194 name = "base-lib",
195)`, `cc_library_headers(
196 name = "darwin-lib",
197)`, `cc_library_headers(
198 name = "foo_headers",
199 deps = [
200 ":base-lib",
201 ] + select({
202 "//build/bazel/platforms/os:android": [
203 ":android-lib",
204 ],
205 "//build/bazel/platforms/os:darwin": [
206 ":darwin-lib",
207 ],
208 "//build/bazel/platforms/os:fuchsia": [
209 ":fuchsia-lib",
210 ],
211 "//build/bazel/platforms/os:linux": [
212 ":linux-lib",
213 ],
214 "//build/bazel/platforms/os:linux_bionic": [
215 ":linux_bionic-lib",
216 ],
217 "//build/bazel/platforms/os:windows": [
218 ":windows-lib",
219 ],
220 "//conditions:default": [],
221 }),
222)`, `cc_library_headers(
223 name = "fuchsia-lib",
224)`, `cc_library_headers(
225 name = "linux-lib",
226)`, `cc_library_headers(
227 name = "linux_bionic-lib",
228)`, `cc_library_headers(
229 name = "windows-lib",
230)`},
231 },
232 {
233 description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
234 moduleTypeUnderTest: "cc_library_headers",
235 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
236 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
237 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
238 filesystem: map[string]string{},
239 bp: soongCcLibraryPreamble + `
240cc_library_headers { name: "android-lib" }
241cc_library_headers { name: "exported-lib" }
242cc_library_headers {
243 name: "foo_headers",
244 target: {
245 android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
246 },
247}`,
248 expectedBazelTargets: []string{`cc_library_headers(
249 name = "android-lib",
250)`, `cc_library_headers(
251 name = "exported-lib",
252)`, `cc_library_headers(
253 name = "foo_headers",
254 deps = [] + select({
255 "//build/bazel/platforms/os:android": [
256 ":android-lib",
257 ":exported-lib",
258 ],
259 "//conditions:default": [],
260 }),
261)`},
262 },
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000263 }
264
265 dir := "."
266 for _, testCase := range testCases {
267 filesystem := make(map[string][]byte)
268 toParse := []string{
269 "Android.bp",
270 }
271 for f, content := range testCase.filesystem {
272 if strings.HasSuffix(f, "Android.bp") {
273 toParse = append(toParse, f)
274 }
275 filesystem[f] = []byte(content)
276 }
277 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
278 ctx := android.NewTestContext(config)
279
Jingwen Chen91220d72021-03-24 02:18:33 -0400280 // TODO(jingwen): make this default for all bp2build tests
281 ctx.RegisterBp2BuildConfig(bp2buildConfig)
282
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000283 cc.RegisterCCBuildComponents(ctx)
284 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
285
286 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
287 for _, m := range testCase.depsMutators {
288 ctx.DepsBp2BuildMutators(m)
289 }
290 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
291 ctx.RegisterForBazelConversion()
292
293 _, errs := ctx.ParseFileList(dir, toParse)
294 if Errored(t, testCase.description, errs) {
295 continue
296 }
297 _, errs = ctx.ResolveDependencies(config)
298 if Errored(t, testCase.description, errs) {
299 continue
300 }
301
302 checkDir := dir
303 if testCase.dir != "" {
304 checkDir = testCase.dir
305 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500306 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500307 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000308 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
309 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
310 } else {
311 for i, target := range bazelTargets {
312 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
313 t.Errorf(
314 "%s: Expected generated Bazel target to be '%s', got '%s'",
315 testCase.description,
316 w,
317 g,
318 )
319 }
320 }
321 }
322 }
323}