blob: 285677a1cf84d404d60df2194d1f9b12fe3b4148 [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"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400388 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000389 shared = {
390 "copts": ["sharedflag"],
391 "dynamic_deps": [":shared_dep_for_shared"],
392 "srcs": ["sharedonly.cpp"],
393 "static_deps": [":static_dep_for_shared"],
394 "whole_archive_deps": [":whole_static_lib_for_shared"],
395 },
Chris Parsons08648312021-05-06 16:23:19 -0400396 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000397 static = {
398 "copts": ["staticflag"],
399 "dynamic_deps": [":shared_dep_for_static"],
400 "srcs": ["staticonly.cpp"],
401 "static_deps": [":static_dep_for_static"],
402 "whole_archive_deps": [":whole_static_lib_for_static"],
403 },
Chris Parsons08648312021-05-06 16:23:19 -0400404 whole_archive_deps = [":whole_static_lib_for_both"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000405)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200406 })
407}
408
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400409func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
410 runCcLibraryTestCase(t, bp2buildTestCase{
411 moduleTypeUnderTest: "cc_library",
412 moduleTypeUnderTestFactory: cc.LibraryFactory,
413 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
414 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
415 dir: "foo/bar",
416 filesystem: map[string]string{
417 "foo/bar/Android.bp": `
418cc_library {
419 name: "a",
420 whole_static_libs: ["whole_static_lib_for_both"],
421 static: {
422 whole_static_libs: ["whole_static_lib_for_static"],
423 },
424 shared: {
425 whole_static_libs: ["whole_static_lib_for_shared"],
426 },
427 bazel_module: { bp2build_available: true },
428}
429
430cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
431
432cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
433
434cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
435`,
436 },
437 blueprint: soongCcLibraryPreamble,
438 expectedBazelTargets: []string{`cc_library(
439 name = "a",
440 copts = [
441 "-Ifoo/bar",
442 "-I$(BINDIR)/foo/bar",
443 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000444 shared = {
445 "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
446 },
447 static = {
448 "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
449 },
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400450 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400451)`},
452 })
453}
454
Jingwen Chenbcf53042021-05-26 04:42:42 +0000455func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
456 runCcLibraryTestCase(t, bp2buildTestCase{
457 description: "cc_library shared/static props in arch",
458 moduleTypeUnderTest: "cc_library",
459 moduleTypeUnderTestFactory: cc.LibraryFactory,
460 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
461 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
462 dir: "foo/bar",
463 filesystem: map[string]string{
464 "foo/bar/arm.cpp": "",
465 "foo/bar/x86.cpp": "",
466 "foo/bar/sharedonly.cpp": "",
467 "foo/bar/staticonly.cpp": "",
468 "foo/bar/Android.bp": `
469cc_library {
470 name: "a",
471 arch: {
472 arm: {
473 shared: {
474 srcs: ["arm_shared.cpp"],
475 cflags: ["-DARM_SHARED"],
476 static_libs: ["arm_static_dep_for_shared"],
477 whole_static_libs: ["arm_whole_static_dep_for_shared"],
478 shared_libs: ["arm_shared_dep_for_shared"],
479 },
480 },
481 x86: {
482 static: {
483 srcs: ["x86_static.cpp"],
484 cflags: ["-DX86_STATIC"],
485 static_libs: ["x86_dep_for_static"],
486 },
487 },
488 },
489 target: {
490 android: {
491 shared: {
492 srcs: ["android_shared.cpp"],
493 cflags: ["-DANDROID_SHARED"],
494 static_libs: ["android_dep_for_shared"],
495 },
496 },
497 android_arm: {
498 shared: {
499 cflags: ["-DANDROID_ARM_SHARED"],
500 },
501 },
502 },
503 srcs: ["both.cpp"],
504 cflags: ["bothflag"],
505 static_libs: ["static_dep_for_both"],
506 static: {
507 srcs: ["staticonly.cpp"],
508 cflags: ["staticflag"],
509 static_libs: ["static_dep_for_static"],
510 },
511 shared: {
512 srcs: ["sharedonly.cpp"],
513 cflags: ["sharedflag"],
514 static_libs: ["static_dep_for_shared"],
515 },
516 bazel_module: { bp2build_available: true },
517}
518
519cc_library_static { name: "static_dep_for_shared" }
520cc_library_static { name: "static_dep_for_static" }
521cc_library_static { name: "static_dep_for_both" }
522
523cc_library_static { name: "arm_static_dep_for_shared" }
524cc_library_static { name: "arm_whole_static_dep_for_shared" }
525cc_library_static { name: "arm_shared_dep_for_shared" }
526
527cc_library_static { name: "x86_dep_for_static" }
528
529cc_library_static { name: "android_dep_for_shared" }
530`,
531 },
532 blueprint: soongCcLibraryPreamble,
533 expectedBazelTargets: []string{`cc_library(
534 name = "a",
535 copts = [
536 "bothflag",
537 "-Ifoo/bar",
538 "-I$(BINDIR)/foo/bar",
539 ],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000540 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000541 shared = {
542 "copts": ["sharedflag"] + select({
543 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
544 "//conditions:default": [],
545 }) + select({
546 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
547 "//conditions:default": [],
548 }) + select({
549 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
550 "//conditions:default": [],
551 }),
552 "dynamic_deps": select({
553 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
554 "//conditions:default": [],
555 }),
556 "srcs": ["sharedonly.cpp"] + select({
557 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
558 "//conditions:default": [],
559 }) + select({
560 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
561 "//conditions:default": [],
562 }),
563 "static_deps": [":static_dep_for_shared"] + select({
564 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
565 "//conditions:default": [],
566 }) + select({
567 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
568 "//conditions:default": [],
569 }),
570 "whole_archive_deps": select({
571 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
572 "//conditions:default": [],
573 }),
574 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000575 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000576 static = {
577 "copts": ["staticflag"] + select({
578 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
579 "//conditions:default": [],
580 }),
581 "srcs": ["staticonly.cpp"] + select({
582 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
583 "//conditions:default": [],
584 }),
585 "static_deps": [":static_dep_for_static"] + select({
586 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
587 "//conditions:default": [],
588 }),
589 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000590)`},
591 })
592}
593
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000594func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
595 runCcLibraryTestCase(t, bp2buildTestCase{
596 description: "cc_library shared/static props with c/cpp/s mixed sources",
597 moduleTypeUnderTest: "cc_library",
598 moduleTypeUnderTestFactory: cc.LibraryFactory,
599 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
600 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
601 dir: "foo/bar",
602 filesystem: map[string]string{
603 "foo/bar/both_source.cpp": "",
604 "foo/bar/both_source.cc": "",
605 "foo/bar/both_source.c": "",
606 "foo/bar/both_source.s": "",
607 "foo/bar/both_source.S": "",
608 "foo/bar/shared_source.cpp": "",
609 "foo/bar/shared_source.cc": "",
610 "foo/bar/shared_source.c": "",
611 "foo/bar/shared_source.s": "",
612 "foo/bar/shared_source.S": "",
613 "foo/bar/static_source.cpp": "",
614 "foo/bar/static_source.cc": "",
615 "foo/bar/static_source.c": "",
616 "foo/bar/static_source.s": "",
617 "foo/bar/static_source.S": "",
618 "foo/bar/Android.bp": `
619cc_library {
620 name: "a",
621 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400622 "both_source.cpp",
623 "both_source.cc",
624 "both_source.c",
625 "both_source.s",
626 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000627 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400628 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000629 static: {
Liz Kammerd366c902021-06-03 13:43:01 -0400630 srcs: [
631 "static_source.cpp",
632 "static_source.cc",
633 "static_source.c",
634 "static_source.s",
635 "static_source.S",
636 ":static_filegroup",
637 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000638 },
639 shared: {
Liz Kammerd366c902021-06-03 13:43:01 -0400640 srcs: [
641 "shared_source.cpp",
642 "shared_source.cc",
643 "shared_source.c",
644 "shared_source.s",
645 "shared_source.S",
646 ":shared_filegroup",
647 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000648 },
649 bazel_module: { bp2build_available: true },
650}
651
652filegroup {
653 name: "both_filegroup",
654 srcs: [
655 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400656 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000657}
658
659filegroup {
660 name: "shared_filegroup",
661 srcs: [
662 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400663 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000664}
665
666filegroup {
667 name: "static_filegroup",
668 srcs: [
669 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400670 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000671}
672`,
673 },
674 blueprint: soongCcLibraryPreamble,
675 expectedBazelTargets: []string{`cc_library(
676 name = "a",
677 copts = [
678 "-Ifoo/bar",
679 "-I$(BINDIR)/foo/bar",
680 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000681 shared = {
682 "srcs": [
683 ":shared_filegroup_cpp_srcs",
684 "shared_source.cc",
685 "shared_source.cpp",
686 ],
687 "srcs_as": [
688 "shared_source.s",
689 "shared_source.S",
690 ":shared_filegroup_as_srcs",
691 ],
692 "srcs_c": [
693 "shared_source.c",
694 ":shared_filegroup_c_srcs",
695 ],
696 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000697 srcs = [
698 ":both_filegroup_cpp_srcs",
699 "both_source.cc",
700 "both_source.cpp",
701 ],
702 srcs_as = [
703 "both_source.s",
704 "both_source.S",
705 ":both_filegroup_as_srcs",
706 ],
707 srcs_c = [
708 "both_source.c",
709 ":both_filegroup_c_srcs",
710 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000711 static = {
712 "srcs": [
713 ":static_filegroup_cpp_srcs",
714 "static_source.cc",
715 "static_source.cpp",
716 ],
717 "srcs_as": [
718 "static_source.s",
719 "static_source.S",
720 ":static_filegroup_as_srcs",
721 ],
722 "srcs_c": [
723 "static_source.c",
724 ":static_filegroup_c_srcs",
725 ],
726 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000727)`},
728 })
729}
730
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200731func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
732 runCcLibraryTestCase(t, bp2buildTestCase{
733 description: "cc_library non-configured version script",
734 moduleTypeUnderTest: "cc_library",
735 moduleTypeUnderTestFactory: cc.LibraryFactory,
736 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
737 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
738 dir: "foo/bar",
739 filesystem: map[string]string{
740 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200741cc_library {
742 name: "a",
743 srcs: ["a.cpp"],
744 version_script: "v.map",
745 bazel_module: { bp2build_available: true },
746}
747`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200748 },
749 blueprint: soongCcLibraryPreamble,
750 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200751 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400752 copts = [
753 "-Ifoo/bar",
754 "-I$(BINDIR)/foo/bar",
755 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200756 srcs = ["a.cpp"],
757 version_script = "v.map",
758)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200759 })
760}
761
762func TestCcLibraryConfiguredVersionScript(t *testing.T) {
763 runCcLibraryTestCase(t, bp2buildTestCase{
764 description: "cc_library configured version script",
765 moduleTypeUnderTest: "cc_library",
766 moduleTypeUnderTestFactory: cc.LibraryFactory,
767 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
768 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
769 dir: "foo/bar",
770 filesystem: map[string]string{
771 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400772 cc_library {
773 name: "a",
774 srcs: ["a.cpp"],
775 arch: {
776 arm: {
777 version_script: "arm.map",
778 },
779 arm64: {
780 version_script: "arm64.map",
781 },
782 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200783
Liz Kammerd366c902021-06-03 13:43:01 -0400784 bazel_module: { bp2build_available: true },
785 }
786 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200787 },
788 blueprint: soongCcLibraryPreamble,
789 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200790 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400791 copts = [
792 "-Ifoo/bar",
793 "-I$(BINDIR)/foo/bar",
794 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200795 srcs = ["a.cpp"],
796 version_script = select({
797 "//build/bazel/platforms/arch:arm": "arm.map",
798 "//build/bazel/platforms/arch:arm64": "arm64.map",
799 "//conditions:default": None,
800 }),
801)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200802 })
803}
804
805func TestCcLibrarySharedLibs(t *testing.T) {
806 runCcLibraryTestCase(t, bp2buildTestCase{
807 description: "cc_library shared_libs",
808 moduleTypeUnderTest: "cc_library",
809 moduleTypeUnderTestFactory: cc.LibraryFactory,
810 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
811 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
812 dir: "foo/bar",
813 filesystem: map[string]string{
814 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400815cc_library {
816 name: "mylib",
817 bazel_module: { bp2build_available: true },
818}
819
820cc_library {
821 name: "a",
822 shared_libs: ["mylib",],
823 bazel_module: { bp2build_available: true },
824}
825`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200826 },
827 blueprint: soongCcLibraryPreamble,
828 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400829 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400830 copts = [
831 "-Ifoo/bar",
832 "-I$(BINDIR)/foo/bar",
833 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400834 dynamic_deps = [":mylib"],
835)`, `cc_library(
836 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400837 copts = [
838 "-Ifoo/bar",
839 "-I$(BINDIR)/foo/bar",
840 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400841)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200842 })
843}
844
845func TestCcLibraryPackRelocations(t *testing.T) {
846 runCcLibraryTestCase(t, bp2buildTestCase{
847 description: "cc_library pack_relocations test",
848 moduleTypeUnderTest: "cc_library",
849 moduleTypeUnderTestFactory: cc.LibraryFactory,
850 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
851 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
852 dir: "foo/bar",
853 filesystem: map[string]string{
854 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400855cc_library {
856 name: "a",
857 srcs: ["a.cpp"],
858 pack_relocations: false,
859 bazel_module: { bp2build_available: true },
860}
861
862cc_library {
863 name: "b",
864 srcs: ["b.cpp"],
865 arch: {
866 x86_64: {
Liz Kammerd366c902021-06-03 13:43:01 -0400867 pack_relocations: false,
868 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400869 },
870 bazel_module: { bp2build_available: true },
871}
872
873cc_library {
874 name: "c",
875 srcs: ["c.cpp"],
876 target: {
877 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400878 pack_relocations: false,
879 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400880 },
881 bazel_module: { bp2build_available: true },
882}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200883 },
884 blueprint: soongCcLibraryPreamble,
885 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400886 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400887 copts = [
888 "-Ifoo/bar",
889 "-I$(BINDIR)/foo/bar",
890 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400891 linkopts = ["-Wl,--pack-dyn-relocs=none"],
892 srcs = ["a.cpp"],
893)`, `cc_library(
894 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400895 copts = [
896 "-Ifoo/bar",
897 "-I$(BINDIR)/foo/bar",
898 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400899 linkopts = select({
900 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
901 "//conditions:default": [],
902 }),
903 srcs = ["b.cpp"],
904)`, `cc_library(
905 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400906 copts = [
907 "-Ifoo/bar",
908 "-I$(BINDIR)/foo/bar",
909 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400910 linkopts = select({
911 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
912 "//conditions:default": [],
913 }),
914 srcs = ["c.cpp"],
915)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200916 })
917}
918
919func TestCcLibrarySpacesInCopts(t *testing.T) {
920 runCcLibraryTestCase(t, bp2buildTestCase{
921 description: "cc_library spaces in copts",
922 moduleTypeUnderTest: "cc_library",
923 moduleTypeUnderTestFactory: cc.LibraryFactory,
924 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
925 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
926 dir: "foo/bar",
927 filesystem: map[string]string{
928 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000929cc_library {
930 name: "a",
931 cflags: ["-include header.h",],
932 bazel_module: { bp2build_available: true },
933}
934`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200935 },
936 blueprint: soongCcLibraryPreamble,
937 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000938 name = "a",
939 copts = [
940 "-include",
941 "header.h",
942 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400943 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000944 ],
945)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200946 })
947}
948
949func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
950 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400951 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200952 moduleTypeUnderTest: "cc_library",
953 moduleTypeUnderTestFactory: cc.LibraryFactory,
954 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
955 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
956 dir: "foo/bar",
957 filesystem: map[string]string{
958 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000959 name: "a",
960 srcs: ["a.cpp"],
961 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400962 "-Wall",
963 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000964 cppflags: [
965 "-fsigned-char",
966 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400967 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000968 arch: {
969 arm64: {
970 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400971 },
972 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000973 target: {
974 android: {
975 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400976 },
977 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000978 bazel_module: { bp2build_available: true },
979}
980`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200981 },
982 blueprint: soongCcLibraryPreamble,
983 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000984 name = "a",
985 copts = [
986 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000987 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400988 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400989 ],
990 cppflags = [
991 "-fsigned-char",
992 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000993 ] + select({
994 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
995 "//conditions:default": [],
996 }) + select({
997 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
998 "//conditions:default": [],
999 }),
1000 srcs = ["a.cpp"],
1001)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001002 })
Jingwen Chen63930982021-03-24 10:04:33 -04001003}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001004
1005func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
1006 runCcLibraryTestCase(t, bp2buildTestCase{
1007 description: "cc_library GetTargetProperties on a LabelAttribute",
1008 moduleTypeUnderTest: "cc_library",
1009 moduleTypeUnderTestFactory: cc.LibraryFactory,
1010 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1011 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1012 dir: "foo/bar",
1013 filesystem: map[string]string{
1014 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -04001015 cc_library {
1016 name: "a",
1017 srcs: ["a.cpp"],
1018 target: {
1019 android_arm: {
1020 version_script: "android_arm.map",
1021 },
1022 linux_bionic_arm64: {
1023 version_script: "linux_bionic_arm64.map",
1024 },
1025 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001026
Liz Kammerd366c902021-06-03 13:43:01 -04001027 bazel_module: { bp2build_available: true },
1028 }
1029 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001030 },
1031 blueprint: soongCcLibraryPreamble,
1032 expectedBazelTargets: []string{`cc_library(
1033 name = "a",
1034 copts = [
1035 "-Ifoo/bar",
1036 "-I$(BINDIR)/foo/bar",
1037 ],
1038 srcs = ["a.cpp"],
1039 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -04001040 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
1041 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001042 "//conditions:default": None,
1043 }),
1044)`},
1045 })
1046}
Liz Kammer47535c52021-06-02 16:02:22 -04001047
1048func TestCcLibraryExcludeLibs(t *testing.T) {
1049 runCcLibraryTestCase(t, bp2buildTestCase{
1050 moduleTypeUnderTest: "cc_library",
1051 moduleTypeUnderTestFactory: cc.LibraryFactory,
1052 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1053 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1054 filesystem: map[string]string{},
1055 blueprint: soongCcLibraryStaticPreamble + `
1056cc_library {
1057 name: "foo_static",
1058 srcs: ["common.c"],
1059 whole_static_libs: [
1060 "arm_whole_static_lib_excludes",
1061 "malloc_not_svelte_whole_static_lib_excludes"
1062 ],
1063 static_libs: [
1064 "arm_static_lib_excludes",
1065 "malloc_not_svelte_static_lib_excludes"
1066 ],
1067 shared_libs: [
1068 "arm_shared_lib_excludes",
1069 ],
1070 arch: {
1071 arm: {
1072 exclude_shared_libs: [
1073 "arm_shared_lib_excludes",
1074 ],
1075 exclude_static_libs: [
1076 "arm_static_lib_excludes",
1077 "arm_whole_static_lib_excludes",
1078 ],
1079 },
1080 },
1081 product_variables: {
1082 malloc_not_svelte: {
1083 shared_libs: ["malloc_not_svelte_shared_lib"],
1084 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1085 exclude_static_libs: [
1086 "malloc_not_svelte_static_lib_excludes",
1087 "malloc_not_svelte_whole_static_lib_excludes",
1088 ],
1089 },
1090 },
1091}
1092
1093cc_library {
1094 name: "arm_whole_static_lib_excludes",
1095 bazel_module: { bp2build_available: false },
1096}
1097
1098cc_library {
1099 name: "malloc_not_svelte_whole_static_lib",
1100 bazel_module: { bp2build_available: false },
1101}
1102
1103cc_library {
1104 name: "malloc_not_svelte_whole_static_lib_excludes",
1105 bazel_module: { bp2build_available: false },
1106}
1107
1108cc_library {
1109 name: "arm_static_lib_excludes",
1110 bazel_module: { bp2build_available: false },
1111}
1112
1113cc_library {
1114 name: "malloc_not_svelte_static_lib_excludes",
1115 bazel_module: { bp2build_available: false },
1116}
1117
1118cc_library {
1119 name: "arm_shared_lib_excludes",
1120 bazel_module: { bp2build_available: false },
1121}
1122
1123cc_library {
1124 name: "malloc_not_svelte_shared_lib",
1125 bazel_module: { bp2build_available: false },
1126}
1127`,
1128 expectedBazelTargets: []string{
1129 `cc_library(
1130 name = "foo_static",
1131 copts = [
1132 "-I.",
1133 "-I$(BINDIR)/.",
1134 ],
1135 dynamic_deps = select({
1136 "//build/bazel/platforms/arch:arm": [],
1137 "//conditions:default": [":arm_shared_lib_excludes"],
1138 }) + select({
1139 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1140 "//conditions:default": [],
1141 }),
1142 implementation_deps = select({
1143 "//build/bazel/platforms/arch:arm": [],
1144 "//conditions:default": [":arm_static_lib_excludes"],
1145 }) + select({
1146 "//build/bazel/product_variables:malloc_not_svelte": [],
1147 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1148 }),
1149 srcs_c = ["common.c"],
1150 whole_archive_deps = select({
1151 "//build/bazel/platforms/arch:arm": [],
1152 "//conditions:default": [":arm_whole_static_lib_excludes"],
1153 }) + select({
1154 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1155 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1156 }),
1157)`,
1158 },
1159 })
1160}
Liz Kammerd366c902021-06-03 13:43:01 -04001161
1162func TestCCLibraryNoCrtTrue(t *testing.T) {
1163 runCcLibraryTestCase(t, bp2buildTestCase{
1164 description: "cc_library - simple example",
1165 moduleTypeUnderTest: "cc_library",
1166 moduleTypeUnderTestFactory: cc.LibraryFactory,
1167 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1168 filesystem: map[string]string{
1169 "impl.cpp": "",
1170 },
1171 blueprint: soongCcLibraryPreamble + `
1172cc_library_headers { name: "some-headers" }
1173cc_library {
1174 name: "foo-lib",
1175 srcs: ["impl.cpp"],
1176 no_libcrt: true,
1177}
1178`,
1179 expectedBazelTargets: []string{`cc_library(
1180 name = "foo-lib",
1181 copts = [
1182 "-I.",
1183 "-I$(BINDIR)/.",
1184 ],
1185 srcs = ["impl.cpp"],
1186 use_libcrt = False,
1187)`}})
1188}
1189
1190func TestCCLibraryNoCrtFalse(t *testing.T) {
1191 runCcLibraryTestCase(t, bp2buildTestCase{
1192 moduleTypeUnderTest: "cc_library",
1193 moduleTypeUnderTestFactory: cc.LibraryFactory,
1194 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1195 filesystem: map[string]string{
1196 "impl.cpp": "",
1197 },
1198 blueprint: soongCcLibraryPreamble + `
1199cc_library_headers { name: "some-headers" }
1200cc_library {
1201 name: "foo-lib",
1202 srcs: ["impl.cpp"],
1203 no_libcrt: false,
1204}
1205`,
1206 expectedBazelTargets: []string{`cc_library(
1207 name = "foo-lib",
1208 copts = [
1209 "-I.",
1210 "-I$(BINDIR)/.",
1211 ],
1212 srcs = ["impl.cpp"],
1213 use_libcrt = True,
1214)`}})
1215}
1216
1217func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1218 runCcLibraryTestCase(t, bp2buildTestCase{
1219 moduleTypeUnderTest: "cc_library",
1220 moduleTypeUnderTestFactory: cc.LibraryFactory,
1221 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1222 filesystem: map[string]string{
1223 "impl.cpp": "",
1224 },
1225 blueprint: soongCcLibraryPreamble + `
1226cc_library_headers { name: "some-headers" }
1227cc_library {
1228 name: "foo-lib",
1229 srcs: ["impl.cpp"],
1230 arch: {
1231 arm: {
1232 no_libcrt: true,
1233 },
1234 x86: {
1235 no_libcrt: true,
1236 },
1237 },
1238}
1239`,
1240 expectedBazelTargets: []string{`cc_library(
1241 name = "foo-lib",
1242 copts = [
1243 "-I.",
1244 "-I$(BINDIR)/.",
1245 ],
1246 srcs = ["impl.cpp"],
1247 use_libcrt = select({
1248 "//build/bazel/platforms/arch:arm": False,
1249 "//build/bazel/platforms/arch:x86": False,
1250 "//conditions:default": None,
1251 }),
1252)`}})
1253}
1254
1255func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1256 runCcLibraryTestCase(t, bp2buildTestCase{
1257 moduleTypeUnderTest: "cc_library",
1258 moduleTypeUnderTestFactory: cc.LibraryFactory,
1259 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1260 filesystem: map[string]string{
1261 "impl.cpp": "",
1262 },
1263 blueprint: soongCcLibraryPreamble + `
1264cc_library_headers { name: "some-headers" }
1265cc_library {
1266 name: "foo-lib",
1267 srcs: ["impl.cpp"],
1268 no_libcrt: false,
1269 arch: {
1270 arm: {
1271 no_libcrt: true,
1272 },
1273 x86: {
1274 no_libcrt: true,
1275 },
1276 },
1277}
1278`,
1279 expectedBazelTargets: []string{`cc_library(
1280 name = "foo-lib",
1281 copts = [
1282 "-I.",
1283 "-I$(BINDIR)/.",
1284 ],
1285 srcs = ["impl.cpp"],
1286 use_libcrt = select({
1287 "//build/bazel/platforms/arch:arm": False,
1288 "//build/bazel/platforms/arch:x86": False,
1289 "//conditions:default": True,
1290 }),
1291)`}})
1292}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001293
1294func TestCcLibraryStrip(t *testing.T) {
1295 runCcLibraryTestCase(t, bp2buildTestCase{
1296 description: "cc_library strip args",
1297 moduleTypeUnderTest: "cc_library",
1298 moduleTypeUnderTestFactory: cc.LibraryFactory,
1299 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1300 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1301 dir: "foo/bar",
1302 filesystem: map[string]string{
1303 "foo/bar/Android.bp": `
1304cc_library {
1305 name: "nothing",
1306 bazel_module: { bp2build_available: true },
1307}
1308cc_library {
1309 name: "keep_symbols",
1310 bazel_module: { bp2build_available: true },
1311 strip: {
1312 keep_symbols: true,
1313 }
1314}
1315cc_library {
1316 name: "keep_symbols_and_debug_frame",
1317 bazel_module: { bp2build_available: true },
1318 strip: {
1319 keep_symbols_and_debug_frame: true,
1320 }
1321}
1322cc_library {
1323 name: "none",
1324 bazel_module: { bp2build_available: true },
1325 strip: {
1326 none: true,
1327 }
1328}
1329cc_library {
1330 name: "keep_symbols_list",
1331 bazel_module: { bp2build_available: true },
1332 strip: {
1333 keep_symbols_list: ["symbol"],
1334 }
1335}
1336cc_library {
1337 name: "all",
1338 bazel_module: { bp2build_available: true },
1339 strip: {
1340 all: true,
1341 }
1342}
1343`,
1344 },
1345 blueprint: soongCcLibraryPreamble,
1346 expectedBazelTargets: []string{`cc_library(
1347 name = "all",
1348 copts = [
1349 "-Ifoo/bar",
1350 "-I$(BINDIR)/foo/bar",
1351 ],
1352 strip = {
1353 "all": True,
1354 },
1355)`, `cc_library(
1356 name = "keep_symbols",
1357 copts = [
1358 "-Ifoo/bar",
1359 "-I$(BINDIR)/foo/bar",
1360 ],
1361 strip = {
1362 "keep_symbols": True,
1363 },
1364)`, `cc_library(
1365 name = "keep_symbols_and_debug_frame",
1366 copts = [
1367 "-Ifoo/bar",
1368 "-I$(BINDIR)/foo/bar",
1369 ],
1370 strip = {
1371 "keep_symbols_and_debug_frame": True,
1372 },
1373)`, `cc_library(
1374 name = "keep_symbols_list",
1375 copts = [
1376 "-Ifoo/bar",
1377 "-I$(BINDIR)/foo/bar",
1378 ],
1379 strip = {
1380 "keep_symbols_list": ["symbol"],
1381 },
1382)`, `cc_library(
1383 name = "none",
1384 copts = [
1385 "-Ifoo/bar",
1386 "-I$(BINDIR)/foo/bar",
1387 ],
1388 strip = {
1389 "none": True,
1390 },
1391)`, `cc_library(
1392 name = "nothing",
1393 copts = [
1394 "-Ifoo/bar",
1395 "-I$(BINDIR)/foo/bar",
1396 ],
1397)`},
1398 })
1399}
1400
1401func TestCcLibraryStripWithArch(t *testing.T) {
1402 runCcLibraryTestCase(t, bp2buildTestCase{
1403 description: "cc_library strip args",
1404 moduleTypeUnderTest: "cc_library",
1405 moduleTypeUnderTestFactory: cc.LibraryFactory,
1406 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1407 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1408 dir: "foo/bar",
1409 filesystem: map[string]string{
1410 "foo/bar/Android.bp": `
1411cc_library {
1412 name: "multi-arch",
1413 bazel_module: { bp2build_available: true },
1414 target: {
1415 darwin: {
1416 strip: {
1417 keep_symbols_list: ["foo", "bar"]
1418 }
1419 },
1420 },
1421 arch: {
1422 arm: {
1423 strip: {
1424 keep_symbols_and_debug_frame: true,
1425 },
1426 },
1427 arm64: {
1428 strip: {
1429 keep_symbols: true,
1430 },
1431 },
1432 }
1433}
1434`,
1435 },
1436 blueprint: soongCcLibraryPreamble,
1437 expectedBazelTargets: []string{`cc_library(
1438 name = "multi-arch",
1439 copts = [
1440 "-Ifoo/bar",
1441 "-I$(BINDIR)/foo/bar",
1442 ],
1443 strip = {
1444 "keep_symbols": select({
1445 "//build/bazel/platforms/arch:arm64": True,
1446 "//conditions:default": None,
1447 }),
1448 "keep_symbols_and_debug_frame": select({
1449 "//build/bazel/platforms/arch:arm": True,
1450 "//conditions:default": None,
1451 }),
1452 "keep_symbols_list": select({
1453 "//build/bazel/platforms/os:darwin": [
1454 "foo",
1455 "bar",
1456 ],
1457 "//conditions:default": [],
1458 }),
1459 },
1460)`},
1461 })
1462}