blob: 49f1f42ba161c36d4987f4dd4d1610e006b81d06 [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
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020043func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040044 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020045 runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
46}
47
48func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
49 cc.RegisterCCBuildComponents(ctx)
50 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
51 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
52 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
53}
54
55func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040056 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057 dir := "."
58 filesystem := make(map[string][]byte)
59 toParse := []string{
60 "Android.bp",
61 }
62 for f, content := range tc.filesystem {
63 if strings.HasSuffix(f, "Android.bp") {
64 toParse = append(toParse, f)
65 }
66 filesystem[f] = []byte(content)
67 }
68 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
69 ctx := android.NewTestContext(config)
70
71 registerModuleTypes(ctx)
72 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
73 ctx.RegisterBp2BuildConfig(bp2buildConfig)
74 for _, m := range tc.depsMutators {
75 ctx.DepsBp2BuildMutators(m)
76 }
77 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
78 ctx.RegisterForBazelConversion()
79
80 _, errs := ctx.ParseFileList(dir, toParse)
81 if errored(t, tc.description, errs) {
82 return
83 }
84 _, errs = ctx.ResolveDependencies(config)
85 if errored(t, tc.description, errs) {
86 return
87 }
88
89 checkDir := dir
90 if tc.dir != "" {
91 checkDir = tc.dir
92 }
93 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
94 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
95 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
96 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
97 } else {
98 for i, target := range bazelTargets {
99 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
100 t.Errorf(
101 "%s: Expected generated Bazel target to be '%s', got '%s'",
102 tc.description,
103 w,
104 g,
105 )
106 }
107 }
108 }
109}
110
111func TestCcLibrarySimple(t *testing.T) {
112 runCcLibraryTestCase(t, bp2buildTestCase{
113 description: "cc_library - simple example",
114 moduleTypeUnderTest: "cc_library",
115 moduleTypeUnderTestFactory: cc.LibraryFactory,
116 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
117 filesystem: map[string]string{
118 "android.cpp": "",
119 "darwin.cpp": "",
120 // Refer to cc.headerExts for the supported header extensions in Soong.
121 "header.h": "",
122 "header.hh": "",
123 "header.hpp": "",
124 "header.hxx": "",
125 "header.h++": "",
126 "header.inl": "",
127 "header.inc": "",
128 "header.ipp": "",
129 "header.h.generic": "",
130 "impl.cpp": "",
131 "linux.cpp": "",
132 "x86.cpp": "",
133 "x86_64.cpp": "",
134 "foo-dir/a.h": "",
135 },
136 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400137cc_library_headers { name: "some-headers" }
138cc_library {
139 name: "foo-lib",
140 srcs: ["impl.cpp"],
141 cflags: ["-Wall"],
142 header_libs: ["some-headers"],
143 export_include_dirs: ["foo-dir"],
144 ldflags: ["-Wl,--exclude-libs=bar.a"],
145 arch: {
146 x86: {
147 ldflags: ["-Wl,--exclude-libs=baz.a"],
148 srcs: ["x86.cpp"],
149 },
150 x86_64: {
151 ldflags: ["-Wl,--exclude-libs=qux.a"],
152 srcs: ["x86_64.cpp"],
153 },
154 },
155 target: {
156 android: {
157 srcs: ["android.cpp"],
158 },
159 linux_glibc: {
160 srcs: ["linux.cpp"],
161 },
162 darwin: {
163 srcs: ["darwin.cpp"],
164 },
165 },
166}
167`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200168 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400169 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000170 copts = [
171 "-Wall",
172 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400173 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000174 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400175 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000176 includes = ["foo-dir"],
177 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
178 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
179 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
180 "//conditions:default": [],
181 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000182 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000183 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
184 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400185 "//conditions:default": [],
186 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000187 "//build/bazel/platforms/os:android": ["android.cpp"],
188 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
189 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400190 "//conditions:default": [],
191 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200192)`}})
193}
194
195func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
196 runCcLibraryTestCase(t, bp2buildTestCase{
197 description: "cc_library - trimmed example of //bionic/linker:ld-android",
198 moduleTypeUnderTest: "cc_library",
199 moduleTypeUnderTestFactory: cc.LibraryFactory,
200 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
201 filesystem: map[string]string{
202 "ld-android.cpp": "",
203 "linked_list.h": "",
204 "linker.h": "",
205 "linker_block_allocator.h": "",
206 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400207 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200208 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400209cc_library_headers { name: "libc_headers" }
210cc_library {
211 name: "fake-ld-android",
212 srcs: ["ld_android.cpp"],
213 cflags: [
214 "-Wall",
215 "-Wextra",
216 "-Wunused",
217 "-Werror",
218 ],
219 header_libs: ["libc_headers"],
220 ldflags: [
221 "-Wl,--exclude-libs=libgcc.a",
222 "-Wl,--exclude-libs=libgcc_stripped.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
226 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
227 ],
228 arch: {
229 x86: {
230 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
231 },
232 x86_64: {
233 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
234 },
235 },
236}
237`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200238 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400239 name = "fake-ld-android",
240 copts = [
241 "-Wall",
242 "-Wextra",
243 "-Wunused",
244 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000245 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400246 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400247 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400248 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400249 linkopts = [
250 "-Wl,--exclude-libs=libgcc.a",
251 "-Wl,--exclude-libs=libgcc_stripped.a",
252 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
253 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
254 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
255 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
256 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000257 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
258 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400259 "//conditions:default": [],
260 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000261 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400262)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200263 })
264}
265
266func TestCcLibraryExcludeSrcs(t *testing.T) {
267 runCcLibraryTestCase(t, bp2buildTestCase{
268 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
269 moduleTypeUnderTest: "cc_library",
270 moduleTypeUnderTestFactory: cc.LibraryFactory,
271 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
272 dir: "external",
273 filesystem: map[string]string{
274 "external/math/cosf.c": "",
275 "external/math/erf.c": "",
276 "external/math/erf_data.c": "",
277 "external/math/erff.c": "",
278 "external/math/erff_data.c": "",
279 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000280cc_library {
281 name: "fake-libarm-optimized-routines-math",
282 exclude_srcs: [
283 // Provided by:
284 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
285 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
286 "math/erf.c",
287 "math/erf_data.c",
288 "math/erff.c",
289 "math/erff_data.c",
290 ],
291 srcs: [
292 "math/*.c",
293 ],
294 // arch-specific settings
295 arch: {
296 arm64: {
297 cflags: [
298 "-DHAVE_FAST_FMA=1",
299 ],
300 },
301 },
302 bazel_module: { bp2build_available: true },
303}
304`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200305 },
306 blueprint: soongCcLibraryPreamble,
307 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000308 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400309 copts = [
310 "-Iexternal",
311 "-I$(BINDIR)/external",
312 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000313 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
314 "//conditions:default": [],
315 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400316 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000317)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200318 })
319}
320
321func TestCcLibrarySharedStaticProps(t *testing.T) {
322 runCcLibraryTestCase(t, bp2buildTestCase{
323 description: "cc_library shared/static props",
324 moduleTypeUnderTest: "cc_library",
325 moduleTypeUnderTestFactory: cc.LibraryFactory,
326 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
327 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
328 dir: "foo/bar",
329 filesystem: map[string]string{
330 "foo/bar/both.cpp": "",
331 "foo/bar/sharedonly.cpp": "",
332 "foo/bar/staticonly.cpp": "",
333 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000334cc_library {
335 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400336 srcs: ["both.cpp"],
337 cflags: ["bothflag"],
338 shared_libs: ["shared_dep_for_both"],
339 static_libs: ["static_dep_for_both"],
340 whole_static_libs: ["whole_static_lib_for_both"],
341 static: {
342 srcs: ["staticonly.cpp"],
343 cflags: ["staticflag"],
344 shared_libs: ["shared_dep_for_static"],
345 static_libs: ["static_dep_for_static"],
346 whole_static_libs: ["whole_static_lib_for_static"],
347 },
348 shared: {
349 srcs: ["sharedonly.cpp"],
350 cflags: ["sharedflag"],
351 shared_libs: ["shared_dep_for_shared"],
352 static_libs: ["static_dep_for_shared"],
353 whole_static_libs: ["whole_static_lib_for_shared"],
354 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000355 bazel_module: { bp2build_available: true },
356}
357
Chris Parsons08648312021-05-06 16:23:19 -0400358cc_library_static { name: "static_dep_for_shared" }
359
360cc_library_static { name: "static_dep_for_static" }
361
362cc_library_static { name: "static_dep_for_both" }
363
364cc_library_static { name: "whole_static_lib_for_shared" }
365
366cc_library_static { name: "whole_static_lib_for_static" }
367
368cc_library_static { name: "whole_static_lib_for_both" }
369
370cc_library { name: "shared_dep_for_shared" }
371
372cc_library { name: "shared_dep_for_static" }
373
374cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000375`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200376 },
377 blueprint: soongCcLibraryPreamble,
378 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000379 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400380 copts = [
381 "bothflag",
382 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400383 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400384 ],
Chris Parsons08648312021-05-06 16:23:19 -0400385 dynamic_deps = [":shared_dep_for_both"],
386 dynamic_deps_for_shared = [":shared_dep_for_shared"],
387 dynamic_deps_for_static = [":shared_dep_for_static"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400388 implementation_deps = [":static_dep_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400389 shared_copts = ["sharedflag"],
390 shared_srcs = ["sharedonly.cpp"],
391 srcs = ["both.cpp"],
392 static_copts = ["staticflag"],
393 static_deps_for_shared = [":static_dep_for_shared"],
394 static_deps_for_static = [":static_dep_for_static"],
395 static_srcs = ["staticonly.cpp"],
396 whole_archive_deps = [":whole_static_lib_for_both"],
397 whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
398 whole_archive_deps_for_static = [":whole_static_lib_for_static"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000399)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200400 })
401}
402
Jingwen Chenbcf53042021-05-26 04:42:42 +0000403func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
404 runCcLibraryTestCase(t, bp2buildTestCase{
405 description: "cc_library shared/static props in arch",
406 moduleTypeUnderTest: "cc_library",
407 moduleTypeUnderTestFactory: cc.LibraryFactory,
408 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
409 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
410 dir: "foo/bar",
411 filesystem: map[string]string{
412 "foo/bar/arm.cpp": "",
413 "foo/bar/x86.cpp": "",
414 "foo/bar/sharedonly.cpp": "",
415 "foo/bar/staticonly.cpp": "",
416 "foo/bar/Android.bp": `
417cc_library {
418 name: "a",
419 arch: {
420 arm: {
421 shared: {
422 srcs: ["arm_shared.cpp"],
423 cflags: ["-DARM_SHARED"],
424 static_libs: ["arm_static_dep_for_shared"],
425 whole_static_libs: ["arm_whole_static_dep_for_shared"],
426 shared_libs: ["arm_shared_dep_for_shared"],
427 },
428 },
429 x86: {
430 static: {
431 srcs: ["x86_static.cpp"],
432 cflags: ["-DX86_STATIC"],
433 static_libs: ["x86_dep_for_static"],
434 },
435 },
436 },
437 target: {
438 android: {
439 shared: {
440 srcs: ["android_shared.cpp"],
441 cflags: ["-DANDROID_SHARED"],
442 static_libs: ["android_dep_for_shared"],
443 },
444 },
445 android_arm: {
446 shared: {
447 cflags: ["-DANDROID_ARM_SHARED"],
448 },
449 },
450 },
451 srcs: ["both.cpp"],
452 cflags: ["bothflag"],
453 static_libs: ["static_dep_for_both"],
454 static: {
455 srcs: ["staticonly.cpp"],
456 cflags: ["staticflag"],
457 static_libs: ["static_dep_for_static"],
458 },
459 shared: {
460 srcs: ["sharedonly.cpp"],
461 cflags: ["sharedflag"],
462 static_libs: ["static_dep_for_shared"],
463 },
464 bazel_module: { bp2build_available: true },
465}
466
467cc_library_static { name: "static_dep_for_shared" }
468cc_library_static { name: "static_dep_for_static" }
469cc_library_static { name: "static_dep_for_both" }
470
471cc_library_static { name: "arm_static_dep_for_shared" }
472cc_library_static { name: "arm_whole_static_dep_for_shared" }
473cc_library_static { name: "arm_shared_dep_for_shared" }
474
475cc_library_static { name: "x86_dep_for_static" }
476
477cc_library_static { name: "android_dep_for_shared" }
478`,
479 },
480 blueprint: soongCcLibraryPreamble,
481 expectedBazelTargets: []string{`cc_library(
482 name = "a",
483 copts = [
484 "bothflag",
485 "-Ifoo/bar",
486 "-I$(BINDIR)/foo/bar",
487 ],
488 dynamic_deps_for_shared = select({
489 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
490 "//conditions:default": [],
491 }),
492 implementation_deps = [":static_dep_for_both"],
493 shared_copts = ["sharedflag"] + select({
494 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
495 "//conditions:default": [],
496 }) + select({
497 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
498 "//conditions:default": [],
499 }) + select({
500 "//build/bazel/platforms:android_arm": ["-DANDROID_ARM_SHARED"],
501 "//conditions:default": [],
502 }),
503 shared_srcs = ["sharedonly.cpp"] + select({
504 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
505 "//conditions:default": [],
506 }) + select({
507 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
508 "//conditions:default": [],
509 }),
510 srcs = ["both.cpp"],
511 static_copts = ["staticflag"] + select({
512 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
513 "//conditions:default": [],
514 }),
515 static_deps_for_shared = [":static_dep_for_shared"] + select({
516 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
517 "//conditions:default": [],
518 }) + select({
519 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
520 "//conditions:default": [],
521 }),
522 static_deps_for_static = [":static_dep_for_static"] + select({
523 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
524 "//conditions:default": [],
525 }),
526 static_srcs = ["staticonly.cpp"] + select({
527 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
528 "//conditions:default": [],
529 }),
530 whole_archive_deps_for_shared = select({
531 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
532 "//conditions:default": [],
533 }),
534)`},
535 })
536}
537
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200538func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
539 runCcLibraryTestCase(t, bp2buildTestCase{
540 description: "cc_library non-configured version script",
541 moduleTypeUnderTest: "cc_library",
542 moduleTypeUnderTestFactory: cc.LibraryFactory,
543 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
544 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
545 dir: "foo/bar",
546 filesystem: map[string]string{
547 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200548cc_library {
549 name: "a",
550 srcs: ["a.cpp"],
551 version_script: "v.map",
552 bazel_module: { bp2build_available: true },
553}
554`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200555 },
556 blueprint: soongCcLibraryPreamble,
557 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200558 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400559 copts = [
560 "-Ifoo/bar",
561 "-I$(BINDIR)/foo/bar",
562 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200563 srcs = ["a.cpp"],
564 version_script = "v.map",
565)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200566 })
567}
568
569func TestCcLibraryConfiguredVersionScript(t *testing.T) {
570 runCcLibraryTestCase(t, bp2buildTestCase{
571 description: "cc_library configured version script",
572 moduleTypeUnderTest: "cc_library",
573 moduleTypeUnderTestFactory: cc.LibraryFactory,
574 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
575 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
576 dir: "foo/bar",
577 filesystem: map[string]string{
578 "foo/bar/Android.bp": `
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200579 cc_library {
580 name: "a",
581 srcs: ["a.cpp"],
582 arch: {
583 arm: {
584 version_script: "arm.map",
585 },
586 arm64: {
587 version_script: "arm64.map",
588 },
589 },
590
591 bazel_module: { bp2build_available: true },
592 }
593 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200594 },
595 blueprint: soongCcLibraryPreamble,
596 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200597 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400598 copts = [
599 "-Ifoo/bar",
600 "-I$(BINDIR)/foo/bar",
601 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200602 srcs = ["a.cpp"],
603 version_script = select({
604 "//build/bazel/platforms/arch:arm": "arm.map",
605 "//build/bazel/platforms/arch:arm64": "arm64.map",
606 "//conditions:default": None,
607 }),
608)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200609 })
610}
611
612func TestCcLibrarySharedLibs(t *testing.T) {
613 runCcLibraryTestCase(t, bp2buildTestCase{
614 description: "cc_library shared_libs",
615 moduleTypeUnderTest: "cc_library",
616 moduleTypeUnderTestFactory: cc.LibraryFactory,
617 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
618 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
619 dir: "foo/bar",
620 filesystem: map[string]string{
621 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400622cc_library {
623 name: "mylib",
624 bazel_module: { bp2build_available: true },
625}
626
627cc_library {
628 name: "a",
629 shared_libs: ["mylib",],
630 bazel_module: { bp2build_available: true },
631}
632`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200633 },
634 blueprint: soongCcLibraryPreamble,
635 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400636 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400637 copts = [
638 "-Ifoo/bar",
639 "-I$(BINDIR)/foo/bar",
640 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400641 dynamic_deps = [":mylib"],
642)`, `cc_library(
643 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400644 copts = [
645 "-Ifoo/bar",
646 "-I$(BINDIR)/foo/bar",
647 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400648)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200649 })
650}
651
652func TestCcLibraryPackRelocations(t *testing.T) {
653 runCcLibraryTestCase(t, bp2buildTestCase{
654 description: "cc_library pack_relocations test",
655 moduleTypeUnderTest: "cc_library",
656 moduleTypeUnderTestFactory: cc.LibraryFactory,
657 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
658 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
659 dir: "foo/bar",
660 filesystem: map[string]string{
661 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400662cc_library {
663 name: "a",
664 srcs: ["a.cpp"],
665 pack_relocations: false,
666 bazel_module: { bp2build_available: true },
667}
668
669cc_library {
670 name: "b",
671 srcs: ["b.cpp"],
672 arch: {
673 x86_64: {
674 pack_relocations: false,
675 },
676 },
677 bazel_module: { bp2build_available: true },
678}
679
680cc_library {
681 name: "c",
682 srcs: ["c.cpp"],
683 target: {
684 darwin: {
685 pack_relocations: false,
686 },
687 },
688 bazel_module: { bp2build_available: true },
689}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200690 },
691 blueprint: soongCcLibraryPreamble,
692 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400693 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400694 copts = [
695 "-Ifoo/bar",
696 "-I$(BINDIR)/foo/bar",
697 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400698 linkopts = ["-Wl,--pack-dyn-relocs=none"],
699 srcs = ["a.cpp"],
700)`, `cc_library(
701 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400702 copts = [
703 "-Ifoo/bar",
704 "-I$(BINDIR)/foo/bar",
705 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400706 linkopts = select({
707 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
708 "//conditions:default": [],
709 }),
710 srcs = ["b.cpp"],
711)`, `cc_library(
712 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400713 copts = [
714 "-Ifoo/bar",
715 "-I$(BINDIR)/foo/bar",
716 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400717 linkopts = select({
718 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
719 "//conditions:default": [],
720 }),
721 srcs = ["c.cpp"],
722)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200723 })
724}
725
726func TestCcLibrarySpacesInCopts(t *testing.T) {
727 runCcLibraryTestCase(t, bp2buildTestCase{
728 description: "cc_library spaces in copts",
729 moduleTypeUnderTest: "cc_library",
730 moduleTypeUnderTestFactory: cc.LibraryFactory,
731 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
732 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
733 dir: "foo/bar",
734 filesystem: map[string]string{
735 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000736cc_library {
737 name: "a",
738 cflags: ["-include header.h",],
739 bazel_module: { bp2build_available: true },
740}
741`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200742 },
743 blueprint: soongCcLibraryPreamble,
744 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000745 name = "a",
746 copts = [
747 "-include",
748 "header.h",
749 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400750 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000751 ],
752)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200753 })
754}
755
756func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
757 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400758 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200759 moduleTypeUnderTest: "cc_library",
760 moduleTypeUnderTestFactory: cc.LibraryFactory,
761 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
762 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
763 dir: "foo/bar",
764 filesystem: map[string]string{
765 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000766 name: "a",
767 srcs: ["a.cpp"],
768 cflags: [
769 "-Wall",
770 ],
771 cppflags: [
772 "-fsigned-char",
773 "-pedantic",
774 ],
775 arch: {
776 arm64: {
777 cppflags: ["-DARM64=1"],
778 },
779 },
780 target: {
781 android: {
782 cppflags: ["-DANDROID=1"],
783 },
784 },
785 bazel_module: { bp2build_available: true },
786}
787`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200788 },
789 blueprint: soongCcLibraryPreamble,
790 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000791 name = "a",
792 copts = [
793 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000794 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400795 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400796 ],
797 cppflags = [
798 "-fsigned-char",
799 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000800 ] + select({
801 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
802 "//conditions:default": [],
803 }) + select({
804 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
805 "//conditions:default": [],
806 }),
807 srcs = ["a.cpp"],
808)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200809 })
Jingwen Chen63930982021-03-24 10:04:33 -0400810}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400811
812func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
813 runCcLibraryTestCase(t, bp2buildTestCase{
814 description: "cc_library GetTargetProperties on a LabelAttribute",
815 moduleTypeUnderTest: "cc_library",
816 moduleTypeUnderTestFactory: cc.LibraryFactory,
817 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
818 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
819 dir: "foo/bar",
820 filesystem: map[string]string{
821 "foo/bar/Android.bp": `
822 cc_library {
823 name: "a",
824 srcs: ["a.cpp"],
825 target: {
826 android_arm: {
827 version_script: "android_arm.map",
828 },
829 linux_bionic_arm64: {
830 version_script: "linux_bionic_arm64.map",
831 },
832 },
833
834 bazel_module: { bp2build_available: true },
835 }
836 `,
837 },
838 blueprint: soongCcLibraryPreamble,
839 expectedBazelTargets: []string{`cc_library(
840 name = "a",
841 copts = [
842 "-Ifoo/bar",
843 "-I$(BINDIR)/foo/bar",
844 ],
845 srcs = ["a.cpp"],
846 version_script = select({
847 "//build/bazel/platforms:android_arm": "android_arm.map",
848 "//build/bazel/platforms:linux_bionic_arm64": "linux_bionic_arm64.map",
849 "//conditions:default": None,
850 }),
851)`},
852 })
853}