blob: c8ae031a26f5def40f1332a2a02732403743ec89 [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)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000050 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020051 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
52 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
53 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
54}
55
56func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040057 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020058 dir := "."
59 filesystem := make(map[string][]byte)
60 toParse := []string{
61 "Android.bp",
62 }
63 for f, content := range tc.filesystem {
64 if strings.HasSuffix(f, "Android.bp") {
65 toParse = append(toParse, f)
66 }
67 filesystem[f] = []byte(content)
68 }
69 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
70 ctx := android.NewTestContext(config)
71
72 registerModuleTypes(ctx)
73 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
74 ctx.RegisterBp2BuildConfig(bp2buildConfig)
75 for _, m := range tc.depsMutators {
76 ctx.DepsBp2BuildMutators(m)
77 }
78 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
79 ctx.RegisterForBazelConversion()
80
81 _, errs := ctx.ParseFileList(dir, toParse)
82 if errored(t, tc.description, errs) {
83 return
84 }
85 _, errs = ctx.ResolveDependencies(config)
86 if errored(t, tc.description, errs) {
87 return
88 }
89
90 checkDir := dir
91 if tc.dir != "" {
92 checkDir = tc.dir
93 }
94 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
95 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
96 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
97 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
98 } else {
99 for i, target := range bazelTargets {
100 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
101 t.Errorf(
102 "%s: Expected generated Bazel target to be '%s', got '%s'",
103 tc.description,
104 w,
105 g,
106 )
107 }
108 }
109 }
110}
111
112func TestCcLibrarySimple(t *testing.T) {
113 runCcLibraryTestCase(t, bp2buildTestCase{
114 description: "cc_library - simple example",
115 moduleTypeUnderTest: "cc_library",
116 moduleTypeUnderTestFactory: cc.LibraryFactory,
117 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
118 filesystem: map[string]string{
119 "android.cpp": "",
120 "darwin.cpp": "",
121 // Refer to cc.headerExts for the supported header extensions in Soong.
122 "header.h": "",
123 "header.hh": "",
124 "header.hpp": "",
125 "header.hxx": "",
126 "header.h++": "",
127 "header.inl": "",
128 "header.inc": "",
129 "header.ipp": "",
130 "header.h.generic": "",
131 "impl.cpp": "",
132 "linux.cpp": "",
133 "x86.cpp": "",
134 "x86_64.cpp": "",
135 "foo-dir/a.h": "",
136 },
137 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400138cc_library_headers { name: "some-headers" }
139cc_library {
140 name: "foo-lib",
141 srcs: ["impl.cpp"],
142 cflags: ["-Wall"],
143 header_libs: ["some-headers"],
144 export_include_dirs: ["foo-dir"],
145 ldflags: ["-Wl,--exclude-libs=bar.a"],
146 arch: {
147 x86: {
148 ldflags: ["-Wl,--exclude-libs=baz.a"],
149 srcs: ["x86.cpp"],
150 },
151 x86_64: {
152 ldflags: ["-Wl,--exclude-libs=qux.a"],
153 srcs: ["x86_64.cpp"],
154 },
155 },
156 target: {
157 android: {
158 srcs: ["android.cpp"],
159 },
160 linux_glibc: {
161 srcs: ["linux.cpp"],
162 },
163 darwin: {
164 srcs: ["darwin.cpp"],
165 },
166 },
167}
168`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200169 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400170 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000171 copts = [
172 "-Wall",
173 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400174 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000175 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400176 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000177 includes = ["foo-dir"],
178 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
179 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
180 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
181 "//conditions:default": [],
182 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000183 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000184 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
185 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400186 "//conditions:default": [],
187 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000188 "//build/bazel/platforms/os:android": ["android.cpp"],
189 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
190 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400191 "//conditions:default": [],
192 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200193)`}})
194}
195
196func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
197 runCcLibraryTestCase(t, bp2buildTestCase{
198 description: "cc_library - trimmed example of //bionic/linker:ld-android",
199 moduleTypeUnderTest: "cc_library",
200 moduleTypeUnderTestFactory: cc.LibraryFactory,
201 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
202 filesystem: map[string]string{
203 "ld-android.cpp": "",
204 "linked_list.h": "",
205 "linker.h": "",
206 "linker_block_allocator.h": "",
207 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400208 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200209 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400210cc_library_headers { name: "libc_headers" }
211cc_library {
212 name: "fake-ld-android",
213 srcs: ["ld_android.cpp"],
214 cflags: [
215 "-Wall",
216 "-Wextra",
217 "-Wunused",
218 "-Werror",
219 ],
220 header_libs: ["libc_headers"],
221 ldflags: [
222 "-Wl,--exclude-libs=libgcc.a",
223 "-Wl,--exclude-libs=libgcc_stripped.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
226 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
227 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
228 ],
229 arch: {
230 x86: {
231 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
232 },
233 x86_64: {
234 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
235 },
236 },
237}
238`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200239 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400240 name = "fake-ld-android",
241 copts = [
242 "-Wall",
243 "-Wextra",
244 "-Wunused",
245 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000246 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400247 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400248 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400249 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400250 linkopts = [
251 "-Wl,--exclude-libs=libgcc.a",
252 "-Wl,--exclude-libs=libgcc_stripped.a",
253 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
254 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
255 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
256 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
257 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000258 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
259 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400260 "//conditions:default": [],
261 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000262 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400263)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200264 })
265}
266
267func TestCcLibraryExcludeSrcs(t *testing.T) {
268 runCcLibraryTestCase(t, bp2buildTestCase{
269 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
270 moduleTypeUnderTest: "cc_library",
271 moduleTypeUnderTestFactory: cc.LibraryFactory,
272 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
273 dir: "external",
274 filesystem: map[string]string{
275 "external/math/cosf.c": "",
276 "external/math/erf.c": "",
277 "external/math/erf_data.c": "",
278 "external/math/erff.c": "",
279 "external/math/erff_data.c": "",
280 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000281cc_library {
282 name: "fake-libarm-optimized-routines-math",
283 exclude_srcs: [
284 // Provided by:
285 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
286 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
287 "math/erf.c",
288 "math/erf_data.c",
289 "math/erff.c",
290 "math/erff_data.c",
291 ],
292 srcs: [
293 "math/*.c",
294 ],
295 // arch-specific settings
296 arch: {
297 arm64: {
298 cflags: [
299 "-DHAVE_FAST_FMA=1",
300 ],
301 },
302 },
303 bazel_module: { bp2build_available: true },
304}
305`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200306 },
307 blueprint: soongCcLibraryPreamble,
308 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000309 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400310 copts = [
311 "-Iexternal",
312 "-I$(BINDIR)/external",
313 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000314 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
315 "//conditions:default": [],
316 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400317 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000318)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200319 })
320}
321
322func TestCcLibrarySharedStaticProps(t *testing.T) {
323 runCcLibraryTestCase(t, bp2buildTestCase{
324 description: "cc_library shared/static props",
325 moduleTypeUnderTest: "cc_library",
326 moduleTypeUnderTestFactory: cc.LibraryFactory,
327 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
328 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
329 dir: "foo/bar",
330 filesystem: map[string]string{
331 "foo/bar/both.cpp": "",
332 "foo/bar/sharedonly.cpp": "",
333 "foo/bar/staticonly.cpp": "",
334 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000335cc_library {
336 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400337 srcs: ["both.cpp"],
338 cflags: ["bothflag"],
339 shared_libs: ["shared_dep_for_both"],
340 static_libs: ["static_dep_for_both"],
341 whole_static_libs: ["whole_static_lib_for_both"],
342 static: {
343 srcs: ["staticonly.cpp"],
344 cflags: ["staticflag"],
345 shared_libs: ["shared_dep_for_static"],
346 static_libs: ["static_dep_for_static"],
347 whole_static_libs: ["whole_static_lib_for_static"],
348 },
349 shared: {
350 srcs: ["sharedonly.cpp"],
351 cflags: ["sharedflag"],
352 shared_libs: ["shared_dep_for_shared"],
353 static_libs: ["static_dep_for_shared"],
354 whole_static_libs: ["whole_static_lib_for_shared"],
355 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000356 bazel_module: { bp2build_available: true },
357}
358
Chris Parsons08648312021-05-06 16:23:19 -0400359cc_library_static { name: "static_dep_for_shared" }
360
361cc_library_static { name: "static_dep_for_static" }
362
363cc_library_static { name: "static_dep_for_both" }
364
365cc_library_static { name: "whole_static_lib_for_shared" }
366
367cc_library_static { name: "whole_static_lib_for_static" }
368
369cc_library_static { name: "whole_static_lib_for_both" }
370
371cc_library { name: "shared_dep_for_shared" }
372
373cc_library { name: "shared_dep_for_static" }
374
375cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000376`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200377 },
378 blueprint: soongCcLibraryPreamble,
379 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000380 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400381 copts = [
382 "bothflag",
383 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400384 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400385 ],
Chris Parsons08648312021-05-06 16:23:19 -0400386 dynamic_deps = [":shared_dep_for_both"],
387 dynamic_deps_for_shared = [":shared_dep_for_shared"],
388 dynamic_deps_for_static = [":shared_dep_for_static"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400389 implementation_deps = [":static_dep_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400390 shared_copts = ["sharedflag"],
391 shared_srcs = ["sharedonly.cpp"],
392 srcs = ["both.cpp"],
393 static_copts = ["staticflag"],
394 static_deps_for_shared = [":static_dep_for_shared"],
395 static_deps_for_static = [":static_dep_for_static"],
396 static_srcs = ["staticonly.cpp"],
397 whole_archive_deps = [":whole_static_lib_for_both"],
398 whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
399 whole_archive_deps_for_static = [":whole_static_lib_for_static"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000400)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200401 })
402}
403
Jingwen Chenbcf53042021-05-26 04:42:42 +0000404func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
405 runCcLibraryTestCase(t, bp2buildTestCase{
406 description: "cc_library shared/static props in arch",
407 moduleTypeUnderTest: "cc_library",
408 moduleTypeUnderTestFactory: cc.LibraryFactory,
409 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
410 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
411 dir: "foo/bar",
412 filesystem: map[string]string{
413 "foo/bar/arm.cpp": "",
414 "foo/bar/x86.cpp": "",
415 "foo/bar/sharedonly.cpp": "",
416 "foo/bar/staticonly.cpp": "",
417 "foo/bar/Android.bp": `
418cc_library {
419 name: "a",
420 arch: {
421 arm: {
422 shared: {
423 srcs: ["arm_shared.cpp"],
424 cflags: ["-DARM_SHARED"],
425 static_libs: ["arm_static_dep_for_shared"],
426 whole_static_libs: ["arm_whole_static_dep_for_shared"],
427 shared_libs: ["arm_shared_dep_for_shared"],
428 },
429 },
430 x86: {
431 static: {
432 srcs: ["x86_static.cpp"],
433 cflags: ["-DX86_STATIC"],
434 static_libs: ["x86_dep_for_static"],
435 },
436 },
437 },
438 target: {
439 android: {
440 shared: {
441 srcs: ["android_shared.cpp"],
442 cflags: ["-DANDROID_SHARED"],
443 static_libs: ["android_dep_for_shared"],
444 },
445 },
446 android_arm: {
447 shared: {
448 cflags: ["-DANDROID_ARM_SHARED"],
449 },
450 },
451 },
452 srcs: ["both.cpp"],
453 cflags: ["bothflag"],
454 static_libs: ["static_dep_for_both"],
455 static: {
456 srcs: ["staticonly.cpp"],
457 cflags: ["staticflag"],
458 static_libs: ["static_dep_for_static"],
459 },
460 shared: {
461 srcs: ["sharedonly.cpp"],
462 cflags: ["sharedflag"],
463 static_libs: ["static_dep_for_shared"],
464 },
465 bazel_module: { bp2build_available: true },
466}
467
468cc_library_static { name: "static_dep_for_shared" }
469cc_library_static { name: "static_dep_for_static" }
470cc_library_static { name: "static_dep_for_both" }
471
472cc_library_static { name: "arm_static_dep_for_shared" }
473cc_library_static { name: "arm_whole_static_dep_for_shared" }
474cc_library_static { name: "arm_shared_dep_for_shared" }
475
476cc_library_static { name: "x86_dep_for_static" }
477
478cc_library_static { name: "android_dep_for_shared" }
479`,
480 },
481 blueprint: soongCcLibraryPreamble,
482 expectedBazelTargets: []string{`cc_library(
483 name = "a",
484 copts = [
485 "bothflag",
486 "-Ifoo/bar",
487 "-I$(BINDIR)/foo/bar",
488 ],
489 dynamic_deps_for_shared = select({
490 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
491 "//conditions:default": [],
492 }),
493 implementation_deps = [":static_dep_for_both"],
494 shared_copts = ["sharedflag"] + select({
495 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
496 "//conditions:default": [],
497 }) + select({
498 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
499 "//conditions:default": [],
500 }) + select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400501 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000502 "//conditions:default": [],
503 }),
504 shared_srcs = ["sharedonly.cpp"] + select({
505 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
506 "//conditions:default": [],
507 }) + select({
508 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
509 "//conditions:default": [],
510 }),
511 srcs = ["both.cpp"],
512 static_copts = ["staticflag"] + select({
513 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
514 "//conditions:default": [],
515 }),
516 static_deps_for_shared = [":static_dep_for_shared"] + select({
517 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
518 "//conditions:default": [],
519 }) + select({
520 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
521 "//conditions:default": [],
522 }),
523 static_deps_for_static = [":static_dep_for_static"] + select({
524 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
525 "//conditions:default": [],
526 }),
527 static_srcs = ["staticonly.cpp"] + select({
528 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
529 "//conditions:default": [],
530 }),
531 whole_archive_deps_for_shared = select({
532 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
533 "//conditions:default": [],
534 }),
535)`},
536 })
537}
538
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000539func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
540 runCcLibraryTestCase(t, bp2buildTestCase{
541 description: "cc_library shared/static props with c/cpp/s mixed sources",
542 moduleTypeUnderTest: "cc_library",
543 moduleTypeUnderTestFactory: cc.LibraryFactory,
544 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
545 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
546 dir: "foo/bar",
547 filesystem: map[string]string{
548 "foo/bar/both_source.cpp": "",
549 "foo/bar/both_source.cc": "",
550 "foo/bar/both_source.c": "",
551 "foo/bar/both_source.s": "",
552 "foo/bar/both_source.S": "",
553 "foo/bar/shared_source.cpp": "",
554 "foo/bar/shared_source.cc": "",
555 "foo/bar/shared_source.c": "",
556 "foo/bar/shared_source.s": "",
557 "foo/bar/shared_source.S": "",
558 "foo/bar/static_source.cpp": "",
559 "foo/bar/static_source.cc": "",
560 "foo/bar/static_source.c": "",
561 "foo/bar/static_source.s": "",
562 "foo/bar/static_source.S": "",
563 "foo/bar/Android.bp": `
564cc_library {
565 name: "a",
566 srcs: [
567 "both_source.cpp",
568 "both_source.cc",
569 "both_source.c",
570 "both_source.s",
571 "both_source.S",
572 ":both_filegroup",
573 ],
574 static: {
575 srcs: [
576 "static_source.cpp",
577 "static_source.cc",
578 "static_source.c",
579 "static_source.s",
580 "static_source.S",
581 ":static_filegroup",
582 ],
583 },
584 shared: {
585 srcs: [
586 "shared_source.cpp",
587 "shared_source.cc",
588 "shared_source.c",
589 "shared_source.s",
590 "shared_source.S",
591 ":shared_filegroup",
592 ],
593 },
594 bazel_module: { bp2build_available: true },
595}
596
597filegroup {
598 name: "both_filegroup",
599 srcs: [
600 // Not relevant, handled by filegroup macro
601 ],
602}
603
604filegroup {
605 name: "shared_filegroup",
606 srcs: [
607 // Not relevant, handled by filegroup macro
608 ],
609}
610
611filegroup {
612 name: "static_filegroup",
613 srcs: [
614 // Not relevant, handled by filegroup macro
615 ],
616}
617`,
618 },
619 blueprint: soongCcLibraryPreamble,
620 expectedBazelTargets: []string{`cc_library(
621 name = "a",
622 copts = [
623 "-Ifoo/bar",
624 "-I$(BINDIR)/foo/bar",
625 ],
626 shared_srcs = [
627 ":shared_filegroup_cpp_srcs",
628 "shared_source.cc",
629 "shared_source.cpp",
630 ],
631 shared_srcs_as = [
632 "shared_source.s",
633 "shared_source.S",
634 ":shared_filegroup_as_srcs",
635 ],
636 shared_srcs_c = [
637 "shared_source.c",
638 ":shared_filegroup_c_srcs",
639 ],
640 srcs = [
641 ":both_filegroup_cpp_srcs",
642 "both_source.cc",
643 "both_source.cpp",
644 ],
645 srcs_as = [
646 "both_source.s",
647 "both_source.S",
648 ":both_filegroup_as_srcs",
649 ],
650 srcs_c = [
651 "both_source.c",
652 ":both_filegroup_c_srcs",
653 ],
654 static_srcs = [
655 ":static_filegroup_cpp_srcs",
656 "static_source.cc",
657 "static_source.cpp",
658 ],
659 static_srcs_as = [
660 "static_source.s",
661 "static_source.S",
662 ":static_filegroup_as_srcs",
663 ],
664 static_srcs_c = [
665 "static_source.c",
666 ":static_filegroup_c_srcs",
667 ],
668)`},
669 })
670}
671
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200672func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
673 runCcLibraryTestCase(t, bp2buildTestCase{
674 description: "cc_library non-configured version script",
675 moduleTypeUnderTest: "cc_library",
676 moduleTypeUnderTestFactory: cc.LibraryFactory,
677 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
678 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
679 dir: "foo/bar",
680 filesystem: map[string]string{
681 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200682cc_library {
683 name: "a",
684 srcs: ["a.cpp"],
685 version_script: "v.map",
686 bazel_module: { bp2build_available: true },
687}
688`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200689 },
690 blueprint: soongCcLibraryPreamble,
691 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200692 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400693 copts = [
694 "-Ifoo/bar",
695 "-I$(BINDIR)/foo/bar",
696 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200697 srcs = ["a.cpp"],
698 version_script = "v.map",
699)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200700 })
701}
702
703func TestCcLibraryConfiguredVersionScript(t *testing.T) {
704 runCcLibraryTestCase(t, bp2buildTestCase{
705 description: "cc_library configured version script",
706 moduleTypeUnderTest: "cc_library",
707 moduleTypeUnderTestFactory: cc.LibraryFactory,
708 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
709 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
710 dir: "foo/bar",
711 filesystem: map[string]string{
712 "foo/bar/Android.bp": `
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200713 cc_library {
714 name: "a",
715 srcs: ["a.cpp"],
716 arch: {
717 arm: {
718 version_script: "arm.map",
719 },
720 arm64: {
721 version_script: "arm64.map",
722 },
723 },
724
725 bazel_module: { bp2build_available: true },
726 }
727 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200728 },
729 blueprint: soongCcLibraryPreamble,
730 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200731 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400732 copts = [
733 "-Ifoo/bar",
734 "-I$(BINDIR)/foo/bar",
735 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200736 srcs = ["a.cpp"],
737 version_script = select({
738 "//build/bazel/platforms/arch:arm": "arm.map",
739 "//build/bazel/platforms/arch:arm64": "arm64.map",
740 "//conditions:default": None,
741 }),
742)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200743 })
744}
745
746func TestCcLibrarySharedLibs(t *testing.T) {
747 runCcLibraryTestCase(t, bp2buildTestCase{
748 description: "cc_library shared_libs",
749 moduleTypeUnderTest: "cc_library",
750 moduleTypeUnderTestFactory: cc.LibraryFactory,
751 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
752 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
753 dir: "foo/bar",
754 filesystem: map[string]string{
755 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400756cc_library {
757 name: "mylib",
758 bazel_module: { bp2build_available: true },
759}
760
761cc_library {
762 name: "a",
763 shared_libs: ["mylib",],
764 bazel_module: { bp2build_available: true },
765}
766`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200767 },
768 blueprint: soongCcLibraryPreamble,
769 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400770 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400771 copts = [
772 "-Ifoo/bar",
773 "-I$(BINDIR)/foo/bar",
774 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400775 dynamic_deps = [":mylib"],
776)`, `cc_library(
777 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400778 copts = [
779 "-Ifoo/bar",
780 "-I$(BINDIR)/foo/bar",
781 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400782)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200783 })
784}
785
786func TestCcLibraryPackRelocations(t *testing.T) {
787 runCcLibraryTestCase(t, bp2buildTestCase{
788 description: "cc_library pack_relocations test",
789 moduleTypeUnderTest: "cc_library",
790 moduleTypeUnderTestFactory: cc.LibraryFactory,
791 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
792 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
793 dir: "foo/bar",
794 filesystem: map[string]string{
795 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400796cc_library {
797 name: "a",
798 srcs: ["a.cpp"],
799 pack_relocations: false,
800 bazel_module: { bp2build_available: true },
801}
802
803cc_library {
804 name: "b",
805 srcs: ["b.cpp"],
806 arch: {
807 x86_64: {
808 pack_relocations: false,
809 },
810 },
811 bazel_module: { bp2build_available: true },
812}
813
814cc_library {
815 name: "c",
816 srcs: ["c.cpp"],
817 target: {
818 darwin: {
819 pack_relocations: false,
820 },
821 },
822 bazel_module: { bp2build_available: true },
823}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200824 },
825 blueprint: soongCcLibraryPreamble,
826 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400827 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400828 copts = [
829 "-Ifoo/bar",
830 "-I$(BINDIR)/foo/bar",
831 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400832 linkopts = ["-Wl,--pack-dyn-relocs=none"],
833 srcs = ["a.cpp"],
834)`, `cc_library(
835 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400836 copts = [
837 "-Ifoo/bar",
838 "-I$(BINDIR)/foo/bar",
839 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400840 linkopts = select({
841 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
842 "//conditions:default": [],
843 }),
844 srcs = ["b.cpp"],
845)`, `cc_library(
846 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400847 copts = [
848 "-Ifoo/bar",
849 "-I$(BINDIR)/foo/bar",
850 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400851 linkopts = select({
852 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
853 "//conditions:default": [],
854 }),
855 srcs = ["c.cpp"],
856)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200857 })
858}
859
860func TestCcLibrarySpacesInCopts(t *testing.T) {
861 runCcLibraryTestCase(t, bp2buildTestCase{
862 description: "cc_library spaces in copts",
863 moduleTypeUnderTest: "cc_library",
864 moduleTypeUnderTestFactory: cc.LibraryFactory,
865 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
866 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
867 dir: "foo/bar",
868 filesystem: map[string]string{
869 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000870cc_library {
871 name: "a",
872 cflags: ["-include header.h",],
873 bazel_module: { bp2build_available: true },
874}
875`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200876 },
877 blueprint: soongCcLibraryPreamble,
878 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000879 name = "a",
880 copts = [
881 "-include",
882 "header.h",
883 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400884 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000885 ],
886)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200887 })
888}
889
890func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
891 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400892 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200893 moduleTypeUnderTest: "cc_library",
894 moduleTypeUnderTestFactory: cc.LibraryFactory,
895 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
896 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
897 dir: "foo/bar",
898 filesystem: map[string]string{
899 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000900 name: "a",
901 srcs: ["a.cpp"],
902 cflags: [
903 "-Wall",
904 ],
905 cppflags: [
906 "-fsigned-char",
907 "-pedantic",
908 ],
909 arch: {
910 arm64: {
911 cppflags: ["-DARM64=1"],
912 },
913 },
914 target: {
915 android: {
916 cppflags: ["-DANDROID=1"],
917 },
918 },
919 bazel_module: { bp2build_available: true },
920}
921`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200922 },
923 blueprint: soongCcLibraryPreamble,
924 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000925 name = "a",
926 copts = [
927 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000928 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400929 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400930 ],
931 cppflags = [
932 "-fsigned-char",
933 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000934 ] + select({
935 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
936 "//conditions:default": [],
937 }) + select({
938 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
939 "//conditions:default": [],
940 }),
941 srcs = ["a.cpp"],
942)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200943 })
Jingwen Chen63930982021-03-24 10:04:33 -0400944}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400945
946func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
947 runCcLibraryTestCase(t, bp2buildTestCase{
948 description: "cc_library GetTargetProperties on a LabelAttribute",
949 moduleTypeUnderTest: "cc_library",
950 moduleTypeUnderTestFactory: cc.LibraryFactory,
951 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
952 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
953 dir: "foo/bar",
954 filesystem: map[string]string{
955 "foo/bar/Android.bp": `
956 cc_library {
957 name: "a",
958 srcs: ["a.cpp"],
959 target: {
960 android_arm: {
961 version_script: "android_arm.map",
962 },
963 linux_bionic_arm64: {
964 version_script: "linux_bionic_arm64.map",
965 },
966 },
967
968 bazel_module: { bp2build_available: true },
969 }
970 `,
971 },
972 blueprint: soongCcLibraryPreamble,
973 expectedBazelTargets: []string{`cc_library(
974 name = "a",
975 copts = [
976 "-Ifoo/bar",
977 "-I$(BINDIR)/foo/bar",
978 ],
979 srcs = ["a.cpp"],
980 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400981 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
982 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400983 "//conditions:default": None,
984 }),
985)`},
986 })
987}