blob: aa8200b963b1617e5c2c1075f03a017a785d6cdb [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 },
Jingwen Chen63930982021-03-24 10:04:33 -0400280 }
281
282 dir := "."
283 for _, testCase := range testCases {
284 filesystem := make(map[string][]byte)
285 toParse := []string{
286 "Android.bp",
287 }
288 for f, content := range testCase.filesystem {
289 if strings.HasSuffix(f, "Android.bp") {
290 toParse = append(toParse, f)
291 }
292 filesystem[f] = []byte(content)
293 }
294 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
295 ctx := android.NewTestContext(config)
296
297 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000298 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Jingwen Chen63930982021-03-24 10:04:33 -0400299 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
300 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
301 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
302 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
303 ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests
Jingwen Chen53681ef2021-04-29 08:15:13 +0000304 for _, m := range testCase.depsMutators {
305 ctx.DepsBp2BuildMutators(m)
306 }
Jingwen Chen63930982021-03-24 10:04:33 -0400307 ctx.RegisterForBazelConversion()
308
309 _, errs := ctx.ParseFileList(dir, toParse)
310 if Errored(t, testCase.description, errs) {
311 continue
312 }
313 _, errs = ctx.ResolveDependencies(config)
314 if Errored(t, testCase.description, errs) {
315 continue
316 }
317
318 checkDir := dir
319 if testCase.dir != "" {
320 checkDir = testCase.dir
321 }
322 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
323 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
324 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
325 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
326 } else {
327 for i, target := range bazelTargets {
328 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
329 t.Errorf(
330 "%s: Expected generated Bazel target to be '%s', got '%s'",
331 testCase.description,
332 w,
333 g,
334 )
335 }
336 }
337 }
338 }
339}