blob: 364929cb7731c003db499b79ed2f6d9eb58e64d9 [file] [log] [blame]
Jingwen Chen63930982021-03-24 10:04:33 -04001// 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)
42
43func TestCcLibraryBp2Build(t *testing.T) {
44 testCases := []struct {
45 description string
46 moduleTypeUnderTest string
47 moduleTypeUnderTestFactory android.ModuleFactory
48 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
49 bp string
50 expectedBazelTargets []string
51 filesystem map[string]string
52 dir string
Jingwen Chen53681ef2021-04-29 08:15:13 +000053 depsMutators []android.RegisterMutatorFunc
Jingwen Chen63930982021-03-24 10:04:33 -040054 }{
55 {
56 description: "cc_library - simple example",
57 moduleTypeUnderTest: "cc_library",
58 moduleTypeUnderTestFactory: cc.LibraryFactory,
59 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
60 filesystem: map[string]string{
61 "android.cpp": "",
62 "darwin.cpp": "",
63 // Refer to cc.headerExts for the supported header extensions in Soong.
64 "header.h": "",
65 "header.hh": "",
66 "header.hpp": "",
67 "header.hxx": "",
68 "header.h++": "",
69 "header.inl": "",
70 "header.inc": "",
71 "header.ipp": "",
72 "header.h.generic": "",
73 "impl.cpp": "",
74 "linux.cpp": "",
75 "x86.cpp": "",
76 "x86_64.cpp": "",
77 "foo-dir/a.h": "",
78 },
79 bp: soongCcLibraryPreamble + `
80cc_library_headers { name: "some-headers" }
81cc_library {
82 name: "foo-lib",
83 srcs: ["impl.cpp"],
84 cflags: ["-Wall"],
85 header_libs: ["some-headers"],
86 export_include_dirs: ["foo-dir"],
87 ldflags: ["-Wl,--exclude-libs=bar.a"],
88 arch: {
89 x86: {
90 ldflags: ["-Wl,--exclude-libs=baz.a"],
91 srcs: ["x86.cpp"],
92 },
93 x86_64: {
94 ldflags: ["-Wl,--exclude-libs=qux.a"],
95 srcs: ["x86_64.cpp"],
96 },
97 },
98 target: {
99 android: {
100 srcs: ["android.cpp"],
101 },
102 linux_glibc: {
103 srcs: ["linux.cpp"],
104 },
105 darwin: {
106 srcs: ["darwin.cpp"],
107 },
108 },
109}
110`,
111 expectedBazelTargets: []string{`cc_library(
112 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000113 copts = [
114 "-Wall",
115 "-I.",
116 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000117 deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000118 includes = ["foo-dir"],
119 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
120 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
121 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
122 "//conditions:default": [],
123 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000124 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000125 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
126 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400127 "//conditions:default": [],
128 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000129 "//build/bazel/platforms/os:android": ["android.cpp"],
130 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
131 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400132 "//conditions:default": [],
133 }),
134)`},
135 },
136 {
137 description: "cc_library - trimmed example of //bionic/linker:ld-android",
138 moduleTypeUnderTest: "cc_library",
139 moduleTypeUnderTestFactory: cc.LibraryFactory,
140 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
141 filesystem: map[string]string{
142 "ld-android.cpp": "",
143 "linked_list.h": "",
144 "linker.h": "",
145 "linker_block_allocator.h": "",
146 "linker_cfi.h": "",
147 },
148 bp: soongCcLibraryPreamble + `
149cc_library_headers { name: "libc_headers" }
150cc_library {
151 name: "fake-ld-android",
152 srcs: ["ld_android.cpp"],
153 cflags: [
154 "-Wall",
155 "-Wextra",
156 "-Wunused",
157 "-Werror",
158 ],
159 header_libs: ["libc_headers"],
160 ldflags: [
161 "-Wl,--exclude-libs=libgcc.a",
162 "-Wl,--exclude-libs=libgcc_stripped.a",
163 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
164 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
165 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
166 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
167 ],
168 arch: {
169 x86: {
170 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
171 },
172 x86_64: {
173 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
174 },
175 },
176}
177`,
178 expectedBazelTargets: []string{`cc_library(
179 name = "fake-ld-android",
180 copts = [
181 "-Wall",
182 "-Wextra",
183 "-Wunused",
184 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000185 "-I.",
Jingwen Chen63930982021-03-24 10:04:33 -0400186 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000187 deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400188 linkopts = [
189 "-Wl,--exclude-libs=libgcc.a",
190 "-Wl,--exclude-libs=libgcc_stripped.a",
191 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
192 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
193 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
194 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
195 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000196 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
197 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400198 "//conditions:default": [],
199 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000200 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400201)`},
202 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000203 {
204 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
205 moduleTypeUnderTest: "cc_library",
206 moduleTypeUnderTestFactory: cc.LibraryFactory,
207 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
208 dir: "external",
209 filesystem: map[string]string{
210 "external/math/cosf.c": "",
211 "external/math/erf.c": "",
212 "external/math/erf_data.c": "",
213 "external/math/erff.c": "",
214 "external/math/erff_data.c": "",
215 "external/Android.bp": `
216cc_library {
217 name: "fake-libarm-optimized-routines-math",
218 exclude_srcs: [
219 // Provided by:
220 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
221 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
222 "math/erf.c",
223 "math/erf_data.c",
224 "math/erff.c",
225 "math/erff_data.c",
226 ],
227 srcs: [
228 "math/*.c",
229 ],
230 // arch-specific settings
231 arch: {
232 arm64: {
233 cflags: [
234 "-DHAVE_FAST_FMA=1",
235 ],
236 },
237 },
238 bazel_module: { bp2build_available: true },
239}
240`,
241 },
242 bp: soongCcLibraryPreamble,
243 expectedBazelTargets: []string{`cc_library(
244 name = "fake-libarm-optimized-routines-math",
245 copts = ["-Iexternal"] + select({
246 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
247 "//conditions:default": [],
248 }),
249 srcs = ["math/cosf.c"],
250)`},
251 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000252 {
253 description: "cc_library shared/static props",
254 moduleTypeUnderTest: "cc_library",
255 moduleTypeUnderTestFactory: cc.LibraryFactory,
256 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
257 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
258 dir: "foo/bar",
259 filesystem: map[string]string{
260 "foo/bar/a.cpp": "",
261 "foo/bar/Android.bp": `
262cc_library {
263 name: "a",
264 shared: { whole_static_libs: ["b"] },
265 static: { srcs: ["a.cpp"] },
266 bazel_module: { bp2build_available: true },
267}
268
269cc_library_static { name: "b" }
270`,
271 },
272 bp: soongCcLibraryPreamble,
273 expectedBazelTargets: []string{`cc_library(
274 name = "a",
275 copts = ["-Ifoo/bar"],
276 srcs = ["a.cpp"],
277 static_deps_for_shared = [":b"],
278)`},
279 },
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200280 {
281 description: "cc_library non-configured version script",
282 moduleTypeUnderTest: "cc_library",
283 moduleTypeUnderTestFactory: cc.LibraryFactory,
284 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
285 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
286 dir: "foo/bar",
287 filesystem: map[string]string{
288 "foo/bar/Android.bp": `
289cc_library {
290 name: "a",
291 srcs: ["a.cpp"],
292 version_script: "v.map",
293 bazel_module: { bp2build_available: true },
294}
295`,
296 },
297 bp: soongCcLibraryPreamble,
298 expectedBazelTargets: []string{`cc_library(
299 name = "a",
300 copts = ["-Ifoo/bar"],
301 srcs = ["a.cpp"],
302 version_script = "v.map",
303)`},
304 },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400305 {
306 description: "cc_library shared_libs",
307 moduleTypeUnderTest: "cc_library",
308 moduleTypeUnderTestFactory: cc.LibraryFactory,
309 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
310 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
311 dir: "foo/bar",
312 filesystem: map[string]string{
313 "foo/bar/Android.bp": `
314cc_library {
315 name: "mylib",
316 bazel_module: { bp2build_available: true },
317}
318
319cc_library {
320 name: "a",
321 shared_libs: ["mylib",],
322 bazel_module: { bp2build_available: true },
323}
324`,
325 },
326 bp: soongCcLibraryPreamble,
327 expectedBazelTargets: []string{`cc_library(
328 name = "a",
329 copts = ["-Ifoo/bar"],
330 dynamic_deps = [":mylib"],
331)`, `cc_library(
332 name = "mylib",
333 copts = ["-Ifoo/bar"],
334)`},
335 },
Jingwen Chen63930982021-03-24 10:04:33 -0400336 }
337
338 dir := "."
339 for _, testCase := range testCases {
340 filesystem := make(map[string][]byte)
341 toParse := []string{
342 "Android.bp",
343 }
344 for f, content := range testCase.filesystem {
345 if strings.HasSuffix(f, "Android.bp") {
346 toParse = append(toParse, f)
347 }
348 filesystem[f] = []byte(content)
349 }
350 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
351 ctx := android.NewTestContext(config)
352
353 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000354 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Jingwen Chen63930982021-03-24 10:04:33 -0400355 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
356 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
357 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
358 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
359 ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests
Jingwen Chen53681ef2021-04-29 08:15:13 +0000360 for _, m := range testCase.depsMutators {
361 ctx.DepsBp2BuildMutators(m)
362 }
Jingwen Chen63930982021-03-24 10:04:33 -0400363 ctx.RegisterForBazelConversion()
364
365 _, errs := ctx.ParseFileList(dir, toParse)
366 if Errored(t, testCase.description, errs) {
367 continue
368 }
369 _, errs = ctx.ResolveDependencies(config)
370 if Errored(t, testCase.description, errs) {
371 continue
372 }
373
374 checkDir := dir
375 if testCase.dir != "" {
376 checkDir = testCase.dir
377 }
378 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
379 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
380 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
381 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
382 } else {
383 for i, target := range bazelTargets {
384 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
385 t.Errorf(
386 "%s: Expected generated Bazel target to be '%s', got '%s'",
387 testCase.description,
388 w,
389 g,
390 )
391 }
392 }
393 }
394 }
395}