blob: 7f6982cc3108e948bb5718ba1db258d17af71260 [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 {
Liz Kammerd366c902021-06-03 13:43:01 -040028 name: "linux_bionic_supported",
Jingwen Chen63930982021-03-24 10:04:33 -040029}
30
31toolchain_library {
Liz Kammerd366c902021-06-03 13:43:01 -040032 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: "",
Jingwen Chen63930982021-03-24 10:04:33 -040040}`
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)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040052 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020053 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
54 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
55}
56
57func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040058 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059 dir := "."
60 filesystem := make(map[string][]byte)
61 toParse := []string{
62 "Android.bp",
63 }
64 for f, content := range tc.filesystem {
65 if strings.HasSuffix(f, "Android.bp") {
66 toParse = append(toParse, f)
67 }
68 filesystem[f] = []byte(content)
69 }
70 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
71 ctx := android.NewTestContext(config)
72
73 registerModuleTypes(ctx)
74 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
75 ctx.RegisterBp2BuildConfig(bp2buildConfig)
76 for _, m := range tc.depsMutators {
77 ctx.DepsBp2BuildMutators(m)
78 }
79 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
80 ctx.RegisterForBazelConversion()
81
82 _, errs := ctx.ParseFileList(dir, toParse)
83 if errored(t, tc.description, errs) {
84 return
85 }
86 _, errs = ctx.ResolveDependencies(config)
87 if errored(t, tc.description, errs) {
88 return
89 }
90
91 checkDir := dir
92 if tc.dir != "" {
93 checkDir = tc.dir
94 }
95 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
96 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
97 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
98 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
99 } else {
100 for i, target := range bazelTargets {
101 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
102 t.Errorf(
103 "%s: Expected generated Bazel target to be '%s', got '%s'",
104 tc.description,
105 w,
106 g,
107 )
108 }
109 }
110 }
111}
112
113func TestCcLibrarySimple(t *testing.T) {
114 runCcLibraryTestCase(t, bp2buildTestCase{
115 description: "cc_library - simple example",
116 moduleTypeUnderTest: "cc_library",
117 moduleTypeUnderTestFactory: cc.LibraryFactory,
118 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
119 filesystem: map[string]string{
120 "android.cpp": "",
121 "darwin.cpp": "",
122 // Refer to cc.headerExts for the supported header extensions in Soong.
123 "header.h": "",
124 "header.hh": "",
125 "header.hpp": "",
126 "header.hxx": "",
127 "header.h++": "",
128 "header.inl": "",
129 "header.inc": "",
130 "header.ipp": "",
131 "header.h.generic": "",
132 "impl.cpp": "",
133 "linux.cpp": "",
134 "x86.cpp": "",
135 "x86_64.cpp": "",
136 "foo-dir/a.h": "",
137 },
138 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400139cc_library_headers { name: "some-headers" }
140cc_library {
141 name: "foo-lib",
142 srcs: ["impl.cpp"],
143 cflags: ["-Wall"],
144 header_libs: ["some-headers"],
145 export_include_dirs: ["foo-dir"],
146 ldflags: ["-Wl,--exclude-libs=bar.a"],
147 arch: {
148 x86: {
149 ldflags: ["-Wl,--exclude-libs=baz.a"],
150 srcs: ["x86.cpp"],
151 },
152 x86_64: {
153 ldflags: ["-Wl,--exclude-libs=qux.a"],
154 srcs: ["x86_64.cpp"],
155 },
156 },
157 target: {
158 android: {
159 srcs: ["android.cpp"],
160 },
161 linux_glibc: {
162 srcs: ["linux.cpp"],
163 },
164 darwin: {
165 srcs: ["darwin.cpp"],
166 },
167 },
168}
169`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200170 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400171 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000172 copts = [
173 "-Wall",
174 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400175 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000176 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400177 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000178 includes = ["foo-dir"],
179 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
180 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
181 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
182 "//conditions:default": [],
183 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000184 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000185 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
186 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400187 "//conditions:default": [],
188 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000189 "//build/bazel/platforms/os:android": ["android.cpp"],
190 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
191 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400192 "//conditions:default": [],
193 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200194)`}})
195}
196
197func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
198 runCcLibraryTestCase(t, bp2buildTestCase{
199 description: "cc_library - trimmed example of //bionic/linker:ld-android",
200 moduleTypeUnderTest: "cc_library",
201 moduleTypeUnderTestFactory: cc.LibraryFactory,
202 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
203 filesystem: map[string]string{
204 "ld-android.cpp": "",
205 "linked_list.h": "",
206 "linker.h": "",
207 "linker_block_allocator.h": "",
208 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400209 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200210 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400211cc_library_headers { name: "libc_headers" }
212cc_library {
213 name: "fake-ld-android",
214 srcs: ["ld_android.cpp"],
215 cflags: [
216 "-Wall",
217 "-Wextra",
218 "-Wunused",
219 "-Werror",
220 ],
221 header_libs: ["libc_headers"],
222 ldflags: [
223 "-Wl,--exclude-libs=libgcc.a",
224 "-Wl,--exclude-libs=libgcc_stripped.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
226 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
227 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
228 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
229 ],
230 arch: {
231 x86: {
232 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
233 },
234 x86_64: {
235 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
236 },
237 },
238}
239`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200240 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400241 name = "fake-ld-android",
242 copts = [
243 "-Wall",
244 "-Wextra",
245 "-Wunused",
246 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000247 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400248 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400249 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400250 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400251 linkopts = [
252 "-Wl,--exclude-libs=libgcc.a",
253 "-Wl,--exclude-libs=libgcc_stripped.a",
254 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
255 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
256 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
257 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
258 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000259 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
260 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400261 "//conditions:default": [],
262 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000263 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400264)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200265 })
266}
267
268func TestCcLibraryExcludeSrcs(t *testing.T) {
269 runCcLibraryTestCase(t, bp2buildTestCase{
270 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
271 moduleTypeUnderTest: "cc_library",
272 moduleTypeUnderTestFactory: cc.LibraryFactory,
273 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
274 dir: "external",
275 filesystem: map[string]string{
276 "external/math/cosf.c": "",
277 "external/math/erf.c": "",
278 "external/math/erf_data.c": "",
279 "external/math/erff.c": "",
280 "external/math/erff_data.c": "",
281 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000282cc_library {
283 name: "fake-libarm-optimized-routines-math",
284 exclude_srcs: [
285 // Provided by:
286 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
287 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
288 "math/erf.c",
289 "math/erf_data.c",
290 "math/erff.c",
291 "math/erff_data.c",
292 ],
293 srcs: [
294 "math/*.c",
295 ],
296 // arch-specific settings
297 arch: {
298 arm64: {
299 cflags: [
300 "-DHAVE_FAST_FMA=1",
301 ],
302 },
303 },
304 bazel_module: { bp2build_available: true },
305}
306`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200307 },
308 blueprint: soongCcLibraryPreamble,
309 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000310 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400311 copts = [
312 "-Iexternal",
313 "-I$(BINDIR)/external",
314 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000315 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
316 "//conditions:default": [],
317 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400318 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000319)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200320 })
321}
322
323func TestCcLibrarySharedStaticProps(t *testing.T) {
324 runCcLibraryTestCase(t, bp2buildTestCase{
325 description: "cc_library shared/static props",
326 moduleTypeUnderTest: "cc_library",
327 moduleTypeUnderTestFactory: cc.LibraryFactory,
328 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
329 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
330 dir: "foo/bar",
331 filesystem: map[string]string{
332 "foo/bar/both.cpp": "",
333 "foo/bar/sharedonly.cpp": "",
334 "foo/bar/staticonly.cpp": "",
335 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000336cc_library {
337 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400338 srcs: ["both.cpp"],
339 cflags: ["bothflag"],
340 shared_libs: ["shared_dep_for_both"],
341 static_libs: ["static_dep_for_both"],
342 whole_static_libs: ["whole_static_lib_for_both"],
343 static: {
344 srcs: ["staticonly.cpp"],
345 cflags: ["staticflag"],
346 shared_libs: ["shared_dep_for_static"],
347 static_libs: ["static_dep_for_static"],
348 whole_static_libs: ["whole_static_lib_for_static"],
349 },
350 shared: {
351 srcs: ["sharedonly.cpp"],
352 cflags: ["sharedflag"],
353 shared_libs: ["shared_dep_for_shared"],
354 static_libs: ["static_dep_for_shared"],
355 whole_static_libs: ["whole_static_lib_for_shared"],
356 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000357 bazel_module: { bp2build_available: true },
358}
359
Chris Parsons08648312021-05-06 16:23:19 -0400360cc_library_static { name: "static_dep_for_shared" }
361
362cc_library_static { name: "static_dep_for_static" }
363
364cc_library_static { name: "static_dep_for_both" }
365
366cc_library_static { name: "whole_static_lib_for_shared" }
367
368cc_library_static { name: "whole_static_lib_for_static" }
369
370cc_library_static { name: "whole_static_lib_for_both" }
371
372cc_library { name: "shared_dep_for_shared" }
373
374cc_library { name: "shared_dep_for_static" }
375
376cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000377`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200378 },
379 blueprint: soongCcLibraryPreamble,
380 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000381 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400382 copts = [
383 "bothflag",
384 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400385 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400386 ],
Chris Parsons08648312021-05-06 16:23:19 -0400387 dynamic_deps = [":shared_dep_for_both"],
388 dynamic_deps_for_shared = [":shared_dep_for_shared"],
389 dynamic_deps_for_static = [":shared_dep_for_static"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400390 implementation_deps = [":static_dep_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400391 shared_copts = ["sharedflag"],
392 shared_srcs = ["sharedonly.cpp"],
393 srcs = ["both.cpp"],
394 static_copts = ["staticflag"],
395 static_deps_for_shared = [":static_dep_for_shared"],
396 static_deps_for_static = [":static_dep_for_static"],
397 static_srcs = ["staticonly.cpp"],
398 whole_archive_deps = [":whole_static_lib_for_both"],
399 whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
400 whole_archive_deps_for_static = [":whole_static_lib_for_static"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000401)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200402 })
403}
404
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400405func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
406 runCcLibraryTestCase(t, bp2buildTestCase{
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/Android.bp": `
414cc_library {
415 name: "a",
416 whole_static_libs: ["whole_static_lib_for_both"],
417 static: {
418 whole_static_libs: ["whole_static_lib_for_static"],
419 },
420 shared: {
421 whole_static_libs: ["whole_static_lib_for_shared"],
422 },
423 bazel_module: { bp2build_available: true },
424}
425
426cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
427
428cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
429
430cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
431`,
432 },
433 blueprint: soongCcLibraryPreamble,
434 expectedBazelTargets: []string{`cc_library(
435 name = "a",
436 copts = [
437 "-Ifoo/bar",
438 "-I$(BINDIR)/foo/bar",
439 ],
440 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
441 whole_archive_deps_for_shared = [":whole_static_lib_for_shared_alwayslink"],
442 whole_archive_deps_for_static = [":whole_static_lib_for_static_alwayslink"],
443)`},
444 })
445}
446
Jingwen Chenbcf53042021-05-26 04:42:42 +0000447func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
448 runCcLibraryTestCase(t, bp2buildTestCase{
449 description: "cc_library shared/static props in arch",
450 moduleTypeUnderTest: "cc_library",
451 moduleTypeUnderTestFactory: cc.LibraryFactory,
452 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
453 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
454 dir: "foo/bar",
455 filesystem: map[string]string{
456 "foo/bar/arm.cpp": "",
457 "foo/bar/x86.cpp": "",
458 "foo/bar/sharedonly.cpp": "",
459 "foo/bar/staticonly.cpp": "",
460 "foo/bar/Android.bp": `
461cc_library {
462 name: "a",
463 arch: {
464 arm: {
465 shared: {
466 srcs: ["arm_shared.cpp"],
467 cflags: ["-DARM_SHARED"],
468 static_libs: ["arm_static_dep_for_shared"],
469 whole_static_libs: ["arm_whole_static_dep_for_shared"],
470 shared_libs: ["arm_shared_dep_for_shared"],
471 },
472 },
473 x86: {
474 static: {
475 srcs: ["x86_static.cpp"],
476 cflags: ["-DX86_STATIC"],
477 static_libs: ["x86_dep_for_static"],
478 },
479 },
480 },
481 target: {
482 android: {
483 shared: {
484 srcs: ["android_shared.cpp"],
485 cflags: ["-DANDROID_SHARED"],
486 static_libs: ["android_dep_for_shared"],
487 },
488 },
489 android_arm: {
490 shared: {
491 cflags: ["-DANDROID_ARM_SHARED"],
492 },
493 },
494 },
495 srcs: ["both.cpp"],
496 cflags: ["bothflag"],
497 static_libs: ["static_dep_for_both"],
498 static: {
499 srcs: ["staticonly.cpp"],
500 cflags: ["staticflag"],
501 static_libs: ["static_dep_for_static"],
502 },
503 shared: {
504 srcs: ["sharedonly.cpp"],
505 cflags: ["sharedflag"],
506 static_libs: ["static_dep_for_shared"],
507 },
508 bazel_module: { bp2build_available: true },
509}
510
511cc_library_static { name: "static_dep_for_shared" }
512cc_library_static { name: "static_dep_for_static" }
513cc_library_static { name: "static_dep_for_both" }
514
515cc_library_static { name: "arm_static_dep_for_shared" }
516cc_library_static { name: "arm_whole_static_dep_for_shared" }
517cc_library_static { name: "arm_shared_dep_for_shared" }
518
519cc_library_static { name: "x86_dep_for_static" }
520
521cc_library_static { name: "android_dep_for_shared" }
522`,
523 },
524 blueprint: soongCcLibraryPreamble,
525 expectedBazelTargets: []string{`cc_library(
526 name = "a",
527 copts = [
528 "bothflag",
529 "-Ifoo/bar",
530 "-I$(BINDIR)/foo/bar",
531 ],
532 dynamic_deps_for_shared = select({
533 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
534 "//conditions:default": [],
535 }),
536 implementation_deps = [":static_dep_for_both"],
537 shared_copts = ["sharedflag"] + select({
538 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
539 "//conditions:default": [],
540 }) + select({
541 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
542 "//conditions:default": [],
543 }) + select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400544 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000545 "//conditions:default": [],
546 }),
547 shared_srcs = ["sharedonly.cpp"] + select({
548 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
549 "//conditions:default": [],
550 }) + select({
551 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
552 "//conditions:default": [],
553 }),
554 srcs = ["both.cpp"],
555 static_copts = ["staticflag"] + select({
556 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
557 "//conditions:default": [],
558 }),
559 static_deps_for_shared = [":static_dep_for_shared"] + select({
560 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
561 "//conditions:default": [],
562 }) + select({
563 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
564 "//conditions:default": [],
565 }),
566 static_deps_for_static = [":static_dep_for_static"] + select({
567 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
568 "//conditions:default": [],
569 }),
570 static_srcs = ["staticonly.cpp"] + select({
571 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
572 "//conditions:default": [],
573 }),
574 whole_archive_deps_for_shared = select({
575 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
576 "//conditions:default": [],
577 }),
578)`},
579 })
580}
581
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000582func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
583 runCcLibraryTestCase(t, bp2buildTestCase{
584 description: "cc_library shared/static props with c/cpp/s mixed sources",
585 moduleTypeUnderTest: "cc_library",
586 moduleTypeUnderTestFactory: cc.LibraryFactory,
587 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
588 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
589 dir: "foo/bar",
590 filesystem: map[string]string{
591 "foo/bar/both_source.cpp": "",
592 "foo/bar/both_source.cc": "",
593 "foo/bar/both_source.c": "",
594 "foo/bar/both_source.s": "",
595 "foo/bar/both_source.S": "",
596 "foo/bar/shared_source.cpp": "",
597 "foo/bar/shared_source.cc": "",
598 "foo/bar/shared_source.c": "",
599 "foo/bar/shared_source.s": "",
600 "foo/bar/shared_source.S": "",
601 "foo/bar/static_source.cpp": "",
602 "foo/bar/static_source.cc": "",
603 "foo/bar/static_source.c": "",
604 "foo/bar/static_source.s": "",
605 "foo/bar/static_source.S": "",
606 "foo/bar/Android.bp": `
607cc_library {
608 name: "a",
609 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400610 "both_source.cpp",
611 "both_source.cc",
612 "both_source.c",
613 "both_source.s",
614 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000615 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400616 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000617 static: {
Liz Kammerd366c902021-06-03 13:43:01 -0400618 srcs: [
619 "static_source.cpp",
620 "static_source.cc",
621 "static_source.c",
622 "static_source.s",
623 "static_source.S",
624 ":static_filegroup",
625 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000626 },
627 shared: {
Liz Kammerd366c902021-06-03 13:43:01 -0400628 srcs: [
629 "shared_source.cpp",
630 "shared_source.cc",
631 "shared_source.c",
632 "shared_source.s",
633 "shared_source.S",
634 ":shared_filegroup",
635 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000636 },
637 bazel_module: { bp2build_available: true },
638}
639
640filegroup {
641 name: "both_filegroup",
642 srcs: [
643 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400644 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000645}
646
647filegroup {
648 name: "shared_filegroup",
649 srcs: [
650 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400651 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000652}
653
654filegroup {
655 name: "static_filegroup",
656 srcs: [
657 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400658 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000659}
660`,
661 },
662 blueprint: soongCcLibraryPreamble,
663 expectedBazelTargets: []string{`cc_library(
664 name = "a",
665 copts = [
666 "-Ifoo/bar",
667 "-I$(BINDIR)/foo/bar",
668 ],
669 shared_srcs = [
670 ":shared_filegroup_cpp_srcs",
671 "shared_source.cc",
672 "shared_source.cpp",
673 ],
674 shared_srcs_as = [
675 "shared_source.s",
676 "shared_source.S",
677 ":shared_filegroup_as_srcs",
678 ],
679 shared_srcs_c = [
680 "shared_source.c",
681 ":shared_filegroup_c_srcs",
682 ],
683 srcs = [
684 ":both_filegroup_cpp_srcs",
685 "both_source.cc",
686 "both_source.cpp",
687 ],
688 srcs_as = [
689 "both_source.s",
690 "both_source.S",
691 ":both_filegroup_as_srcs",
692 ],
693 srcs_c = [
694 "both_source.c",
695 ":both_filegroup_c_srcs",
696 ],
697 static_srcs = [
698 ":static_filegroup_cpp_srcs",
699 "static_source.cc",
700 "static_source.cpp",
701 ],
702 static_srcs_as = [
703 "static_source.s",
704 "static_source.S",
705 ":static_filegroup_as_srcs",
706 ],
707 static_srcs_c = [
708 "static_source.c",
709 ":static_filegroup_c_srcs",
710 ],
711)`},
712 })
713}
714
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200715func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
716 runCcLibraryTestCase(t, bp2buildTestCase{
717 description: "cc_library non-configured version script",
718 moduleTypeUnderTest: "cc_library",
719 moduleTypeUnderTestFactory: cc.LibraryFactory,
720 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
721 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
722 dir: "foo/bar",
723 filesystem: map[string]string{
724 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200725cc_library {
726 name: "a",
727 srcs: ["a.cpp"],
728 version_script: "v.map",
729 bazel_module: { bp2build_available: true },
730}
731`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200732 },
733 blueprint: soongCcLibraryPreamble,
734 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200735 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400736 copts = [
737 "-Ifoo/bar",
738 "-I$(BINDIR)/foo/bar",
739 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200740 srcs = ["a.cpp"],
741 version_script = "v.map",
742)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200743 })
744}
745
746func TestCcLibraryConfiguredVersionScript(t *testing.T) {
747 runCcLibraryTestCase(t, bp2buildTestCase{
748 description: "cc_library configured version script",
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": `
Liz Kammerd366c902021-06-03 13:43:01 -0400756 cc_library {
757 name: "a",
758 srcs: ["a.cpp"],
759 arch: {
760 arm: {
761 version_script: "arm.map",
762 },
763 arm64: {
764 version_script: "arm64.map",
765 },
766 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200767
Liz Kammerd366c902021-06-03 13:43:01 -0400768 bazel_module: { bp2build_available: true },
769 }
770 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200771 },
772 blueprint: soongCcLibraryPreamble,
773 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200774 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400775 copts = [
776 "-Ifoo/bar",
777 "-I$(BINDIR)/foo/bar",
778 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200779 srcs = ["a.cpp"],
780 version_script = select({
781 "//build/bazel/platforms/arch:arm": "arm.map",
782 "//build/bazel/platforms/arch:arm64": "arm64.map",
783 "//conditions:default": None,
784 }),
785)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200786 })
787}
788
789func TestCcLibrarySharedLibs(t *testing.T) {
790 runCcLibraryTestCase(t, bp2buildTestCase{
791 description: "cc_library shared_libs",
792 moduleTypeUnderTest: "cc_library",
793 moduleTypeUnderTestFactory: cc.LibraryFactory,
794 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
795 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
796 dir: "foo/bar",
797 filesystem: map[string]string{
798 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400799cc_library {
800 name: "mylib",
801 bazel_module: { bp2build_available: true },
802}
803
804cc_library {
805 name: "a",
806 shared_libs: ["mylib",],
807 bazel_module: { bp2build_available: true },
808}
809`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200810 },
811 blueprint: soongCcLibraryPreamble,
812 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400813 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400814 copts = [
815 "-Ifoo/bar",
816 "-I$(BINDIR)/foo/bar",
817 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400818 dynamic_deps = [":mylib"],
819)`, `cc_library(
820 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400821 copts = [
822 "-Ifoo/bar",
823 "-I$(BINDIR)/foo/bar",
824 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400825)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200826 })
827}
828
829func TestCcLibraryPackRelocations(t *testing.T) {
830 runCcLibraryTestCase(t, bp2buildTestCase{
831 description: "cc_library pack_relocations test",
832 moduleTypeUnderTest: "cc_library",
833 moduleTypeUnderTestFactory: cc.LibraryFactory,
834 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
835 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
836 dir: "foo/bar",
837 filesystem: map[string]string{
838 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400839cc_library {
840 name: "a",
841 srcs: ["a.cpp"],
842 pack_relocations: false,
843 bazel_module: { bp2build_available: true },
844}
845
846cc_library {
847 name: "b",
848 srcs: ["b.cpp"],
849 arch: {
850 x86_64: {
Liz Kammerd366c902021-06-03 13:43:01 -0400851 pack_relocations: false,
852 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400853 },
854 bazel_module: { bp2build_available: true },
855}
856
857cc_library {
858 name: "c",
859 srcs: ["c.cpp"],
860 target: {
861 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400862 pack_relocations: false,
863 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400864 },
865 bazel_module: { bp2build_available: true },
866}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200867 },
868 blueprint: soongCcLibraryPreamble,
869 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400870 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400871 copts = [
872 "-Ifoo/bar",
873 "-I$(BINDIR)/foo/bar",
874 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400875 linkopts = ["-Wl,--pack-dyn-relocs=none"],
876 srcs = ["a.cpp"],
877)`, `cc_library(
878 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400879 copts = [
880 "-Ifoo/bar",
881 "-I$(BINDIR)/foo/bar",
882 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400883 linkopts = select({
884 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
885 "//conditions:default": [],
886 }),
887 srcs = ["b.cpp"],
888)`, `cc_library(
889 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400890 copts = [
891 "-Ifoo/bar",
892 "-I$(BINDIR)/foo/bar",
893 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400894 linkopts = select({
895 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
896 "//conditions:default": [],
897 }),
898 srcs = ["c.cpp"],
899)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200900 })
901}
902
903func TestCcLibrarySpacesInCopts(t *testing.T) {
904 runCcLibraryTestCase(t, bp2buildTestCase{
905 description: "cc_library spaces in copts",
906 moduleTypeUnderTest: "cc_library",
907 moduleTypeUnderTestFactory: cc.LibraryFactory,
908 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
909 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
910 dir: "foo/bar",
911 filesystem: map[string]string{
912 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000913cc_library {
914 name: "a",
915 cflags: ["-include header.h",],
916 bazel_module: { bp2build_available: true },
917}
918`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200919 },
920 blueprint: soongCcLibraryPreamble,
921 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000922 name = "a",
923 copts = [
924 "-include",
925 "header.h",
926 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400927 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000928 ],
929)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200930 })
931}
932
933func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
934 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400935 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200936 moduleTypeUnderTest: "cc_library",
937 moduleTypeUnderTestFactory: cc.LibraryFactory,
938 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
939 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
940 dir: "foo/bar",
941 filesystem: map[string]string{
942 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000943 name: "a",
944 srcs: ["a.cpp"],
945 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400946 "-Wall",
947 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000948 cppflags: [
949 "-fsigned-char",
950 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400951 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000952 arch: {
953 arm64: {
954 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400955 },
956 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000957 target: {
958 android: {
959 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400960 },
961 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000962 bazel_module: { bp2build_available: true },
963}
964`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200965 },
966 blueprint: soongCcLibraryPreamble,
967 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000968 name = "a",
969 copts = [
970 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000971 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400972 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400973 ],
974 cppflags = [
975 "-fsigned-char",
976 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000977 ] + select({
978 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
979 "//conditions:default": [],
980 }) + select({
981 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
982 "//conditions:default": [],
983 }),
984 srcs = ["a.cpp"],
985)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200986 })
Jingwen Chen63930982021-03-24 10:04:33 -0400987}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400988
989func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
990 runCcLibraryTestCase(t, bp2buildTestCase{
991 description: "cc_library GetTargetProperties on a LabelAttribute",
992 moduleTypeUnderTest: "cc_library",
993 moduleTypeUnderTestFactory: cc.LibraryFactory,
994 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
995 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
996 dir: "foo/bar",
997 filesystem: map[string]string{
998 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400999 cc_library {
1000 name: "a",
1001 srcs: ["a.cpp"],
1002 target: {
1003 android_arm: {
1004 version_script: "android_arm.map",
1005 },
1006 linux_bionic_arm64: {
1007 version_script: "linux_bionic_arm64.map",
1008 },
1009 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001010
Liz Kammerd366c902021-06-03 13:43:01 -04001011 bazel_module: { bp2build_available: true },
1012 }
1013 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001014 },
1015 blueprint: soongCcLibraryPreamble,
1016 expectedBazelTargets: []string{`cc_library(
1017 name = "a",
1018 copts = [
1019 "-Ifoo/bar",
1020 "-I$(BINDIR)/foo/bar",
1021 ],
1022 srcs = ["a.cpp"],
1023 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -04001024 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
1025 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001026 "//conditions:default": None,
1027 }),
1028)`},
1029 })
1030}
Liz Kammer47535c52021-06-02 16:02:22 -04001031
1032func TestCcLibraryExcludeLibs(t *testing.T) {
1033 runCcLibraryTestCase(t, bp2buildTestCase{
1034 moduleTypeUnderTest: "cc_library",
1035 moduleTypeUnderTestFactory: cc.LibraryFactory,
1036 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1037 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1038 filesystem: map[string]string{},
1039 blueprint: soongCcLibraryStaticPreamble + `
1040cc_library {
1041 name: "foo_static",
1042 srcs: ["common.c"],
1043 whole_static_libs: [
1044 "arm_whole_static_lib_excludes",
1045 "malloc_not_svelte_whole_static_lib_excludes"
1046 ],
1047 static_libs: [
1048 "arm_static_lib_excludes",
1049 "malloc_not_svelte_static_lib_excludes"
1050 ],
1051 shared_libs: [
1052 "arm_shared_lib_excludes",
1053 ],
1054 arch: {
1055 arm: {
1056 exclude_shared_libs: [
1057 "arm_shared_lib_excludes",
1058 ],
1059 exclude_static_libs: [
1060 "arm_static_lib_excludes",
1061 "arm_whole_static_lib_excludes",
1062 ],
1063 },
1064 },
1065 product_variables: {
1066 malloc_not_svelte: {
1067 shared_libs: ["malloc_not_svelte_shared_lib"],
1068 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1069 exclude_static_libs: [
1070 "malloc_not_svelte_static_lib_excludes",
1071 "malloc_not_svelte_whole_static_lib_excludes",
1072 ],
1073 },
1074 },
1075}
1076
1077cc_library {
1078 name: "arm_whole_static_lib_excludes",
1079 bazel_module: { bp2build_available: false },
1080}
1081
1082cc_library {
1083 name: "malloc_not_svelte_whole_static_lib",
1084 bazel_module: { bp2build_available: false },
1085}
1086
1087cc_library {
1088 name: "malloc_not_svelte_whole_static_lib_excludes",
1089 bazel_module: { bp2build_available: false },
1090}
1091
1092cc_library {
1093 name: "arm_static_lib_excludes",
1094 bazel_module: { bp2build_available: false },
1095}
1096
1097cc_library {
1098 name: "malloc_not_svelte_static_lib_excludes",
1099 bazel_module: { bp2build_available: false },
1100}
1101
1102cc_library {
1103 name: "arm_shared_lib_excludes",
1104 bazel_module: { bp2build_available: false },
1105}
1106
1107cc_library {
1108 name: "malloc_not_svelte_shared_lib",
1109 bazel_module: { bp2build_available: false },
1110}
1111`,
1112 expectedBazelTargets: []string{
1113 `cc_library(
1114 name = "foo_static",
1115 copts = [
1116 "-I.",
1117 "-I$(BINDIR)/.",
1118 ],
1119 dynamic_deps = select({
1120 "//build/bazel/platforms/arch:arm": [],
1121 "//conditions:default": [":arm_shared_lib_excludes"],
1122 }) + select({
1123 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1124 "//conditions:default": [],
1125 }),
1126 implementation_deps = select({
1127 "//build/bazel/platforms/arch:arm": [],
1128 "//conditions:default": [":arm_static_lib_excludes"],
1129 }) + select({
1130 "//build/bazel/product_variables:malloc_not_svelte": [],
1131 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1132 }),
1133 srcs_c = ["common.c"],
1134 whole_archive_deps = select({
1135 "//build/bazel/platforms/arch:arm": [],
1136 "//conditions:default": [":arm_whole_static_lib_excludes"],
1137 }) + select({
1138 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1139 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1140 }),
1141)`,
1142 },
1143 })
1144}
Liz Kammerd366c902021-06-03 13:43:01 -04001145
1146func TestCCLibraryNoCrtTrue(t *testing.T) {
1147 runCcLibraryTestCase(t, bp2buildTestCase{
1148 description: "cc_library - simple example",
1149 moduleTypeUnderTest: "cc_library",
1150 moduleTypeUnderTestFactory: cc.LibraryFactory,
1151 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1152 filesystem: map[string]string{
1153 "impl.cpp": "",
1154 },
1155 blueprint: soongCcLibraryPreamble + `
1156cc_library_headers { name: "some-headers" }
1157cc_library {
1158 name: "foo-lib",
1159 srcs: ["impl.cpp"],
1160 no_libcrt: true,
1161}
1162`,
1163 expectedBazelTargets: []string{`cc_library(
1164 name = "foo-lib",
1165 copts = [
1166 "-I.",
1167 "-I$(BINDIR)/.",
1168 ],
1169 srcs = ["impl.cpp"],
1170 use_libcrt = False,
1171)`}})
1172}
1173
1174func TestCCLibraryNoCrtFalse(t *testing.T) {
1175 runCcLibraryTestCase(t, bp2buildTestCase{
1176 moduleTypeUnderTest: "cc_library",
1177 moduleTypeUnderTestFactory: cc.LibraryFactory,
1178 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1179 filesystem: map[string]string{
1180 "impl.cpp": "",
1181 },
1182 blueprint: soongCcLibraryPreamble + `
1183cc_library_headers { name: "some-headers" }
1184cc_library {
1185 name: "foo-lib",
1186 srcs: ["impl.cpp"],
1187 no_libcrt: false,
1188}
1189`,
1190 expectedBazelTargets: []string{`cc_library(
1191 name = "foo-lib",
1192 copts = [
1193 "-I.",
1194 "-I$(BINDIR)/.",
1195 ],
1196 srcs = ["impl.cpp"],
1197 use_libcrt = True,
1198)`}})
1199}
1200
1201func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1202 runCcLibraryTestCase(t, bp2buildTestCase{
1203 moduleTypeUnderTest: "cc_library",
1204 moduleTypeUnderTestFactory: cc.LibraryFactory,
1205 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1206 filesystem: map[string]string{
1207 "impl.cpp": "",
1208 },
1209 blueprint: soongCcLibraryPreamble + `
1210cc_library_headers { name: "some-headers" }
1211cc_library {
1212 name: "foo-lib",
1213 srcs: ["impl.cpp"],
1214 arch: {
1215 arm: {
1216 no_libcrt: true,
1217 },
1218 x86: {
1219 no_libcrt: true,
1220 },
1221 },
1222}
1223`,
1224 expectedBazelTargets: []string{`cc_library(
1225 name = "foo-lib",
1226 copts = [
1227 "-I.",
1228 "-I$(BINDIR)/.",
1229 ],
1230 srcs = ["impl.cpp"],
1231 use_libcrt = select({
1232 "//build/bazel/platforms/arch:arm": False,
1233 "//build/bazel/platforms/arch:x86": False,
1234 "//conditions:default": None,
1235 }),
1236)`}})
1237}
1238
1239func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1240 runCcLibraryTestCase(t, bp2buildTestCase{
1241 moduleTypeUnderTest: "cc_library",
1242 moduleTypeUnderTestFactory: cc.LibraryFactory,
1243 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1244 filesystem: map[string]string{
1245 "impl.cpp": "",
1246 },
1247 blueprint: soongCcLibraryPreamble + `
1248cc_library_headers { name: "some-headers" }
1249cc_library {
1250 name: "foo-lib",
1251 srcs: ["impl.cpp"],
1252 no_libcrt: false,
1253 arch: {
1254 arm: {
1255 no_libcrt: true,
1256 },
1257 x86: {
1258 no_libcrt: true,
1259 },
1260 },
1261}
1262`,
1263 expectedBazelTargets: []string{`cc_library(
1264 name = "foo-lib",
1265 copts = [
1266 "-I.",
1267 "-I$(BINDIR)/.",
1268 ],
1269 srcs = ["impl.cpp"],
1270 use_libcrt = select({
1271 "//build/bazel/platforms/arch:arm": False,
1272 "//build/bazel/platforms/arch:x86": False,
1273 "//conditions:default": True,
1274 }),
1275)`}})
1276}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001277
1278func TestCcLibraryStrip(t *testing.T) {
1279 runCcLibraryTestCase(t, bp2buildTestCase{
1280 description: "cc_library strip args",
1281 moduleTypeUnderTest: "cc_library",
1282 moduleTypeUnderTestFactory: cc.LibraryFactory,
1283 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1284 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1285 dir: "foo/bar",
1286 filesystem: map[string]string{
1287 "foo/bar/Android.bp": `
1288cc_library {
1289 name: "nothing",
1290 bazel_module: { bp2build_available: true },
1291}
1292cc_library {
1293 name: "keep_symbols",
1294 bazel_module: { bp2build_available: true },
1295 strip: {
1296 keep_symbols: true,
1297 }
1298}
1299cc_library {
1300 name: "keep_symbols_and_debug_frame",
1301 bazel_module: { bp2build_available: true },
1302 strip: {
1303 keep_symbols_and_debug_frame: true,
1304 }
1305}
1306cc_library {
1307 name: "none",
1308 bazel_module: { bp2build_available: true },
1309 strip: {
1310 none: true,
1311 }
1312}
1313cc_library {
1314 name: "keep_symbols_list",
1315 bazel_module: { bp2build_available: true },
1316 strip: {
1317 keep_symbols_list: ["symbol"],
1318 }
1319}
1320cc_library {
1321 name: "all",
1322 bazel_module: { bp2build_available: true },
1323 strip: {
1324 all: true,
1325 }
1326}
1327`,
1328 },
1329 blueprint: soongCcLibraryPreamble,
1330 expectedBazelTargets: []string{`cc_library(
1331 name = "all",
1332 copts = [
1333 "-Ifoo/bar",
1334 "-I$(BINDIR)/foo/bar",
1335 ],
1336 strip = {
1337 "all": True,
1338 },
1339)`, `cc_library(
1340 name = "keep_symbols",
1341 copts = [
1342 "-Ifoo/bar",
1343 "-I$(BINDIR)/foo/bar",
1344 ],
1345 strip = {
1346 "keep_symbols": True,
1347 },
1348)`, `cc_library(
1349 name = "keep_symbols_and_debug_frame",
1350 copts = [
1351 "-Ifoo/bar",
1352 "-I$(BINDIR)/foo/bar",
1353 ],
1354 strip = {
1355 "keep_symbols_and_debug_frame": True,
1356 },
1357)`, `cc_library(
1358 name = "keep_symbols_list",
1359 copts = [
1360 "-Ifoo/bar",
1361 "-I$(BINDIR)/foo/bar",
1362 ],
1363 strip = {
1364 "keep_symbols_list": ["symbol"],
1365 },
1366)`, `cc_library(
1367 name = "none",
1368 copts = [
1369 "-Ifoo/bar",
1370 "-I$(BINDIR)/foo/bar",
1371 ],
1372 strip = {
1373 "none": True,
1374 },
1375)`, `cc_library(
1376 name = "nothing",
1377 copts = [
1378 "-Ifoo/bar",
1379 "-I$(BINDIR)/foo/bar",
1380 ],
1381)`},
1382 })
1383}
1384
1385func TestCcLibraryStripWithArch(t *testing.T) {
1386 runCcLibraryTestCase(t, bp2buildTestCase{
1387 description: "cc_library strip args",
1388 moduleTypeUnderTest: "cc_library",
1389 moduleTypeUnderTestFactory: cc.LibraryFactory,
1390 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1391 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1392 dir: "foo/bar",
1393 filesystem: map[string]string{
1394 "foo/bar/Android.bp": `
1395cc_library {
1396 name: "multi-arch",
1397 bazel_module: { bp2build_available: true },
1398 target: {
1399 darwin: {
1400 strip: {
1401 keep_symbols_list: ["foo", "bar"]
1402 }
1403 },
1404 },
1405 arch: {
1406 arm: {
1407 strip: {
1408 keep_symbols_and_debug_frame: true,
1409 },
1410 },
1411 arm64: {
1412 strip: {
1413 keep_symbols: true,
1414 },
1415 },
1416 }
1417}
1418`,
1419 },
1420 blueprint: soongCcLibraryPreamble,
1421 expectedBazelTargets: []string{`cc_library(
1422 name = "multi-arch",
1423 copts = [
1424 "-Ifoo/bar",
1425 "-I$(BINDIR)/foo/bar",
1426 ],
1427 strip = {
1428 "keep_symbols": select({
1429 "//build/bazel/platforms/arch:arm64": True,
1430 "//conditions:default": None,
1431 }),
1432 "keep_symbols_and_debug_frame": select({
1433 "//build/bazel/platforms/arch:arm": True,
1434 "//conditions:default": None,
1435 }),
1436 "keep_symbols_list": select({
1437 "//build/bazel/platforms/os:darwin": [
1438 "foo",
1439 "bar",
1440 ],
1441 "//conditions:default": [],
1442 }),
1443 },
1444)`},
1445 })
1446}