blob: 4f720f56cd9e843ec5bab315d9153312eee0e0d4 [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",
Chris Parsons69fa9f92021-07-13 11:47:44 -0400677 asflags = [
678 "-Ifoo/bar",
679 "-I$(BINDIR)/foo/bar",
680 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000681 copts = [
682 "-Ifoo/bar",
683 "-I$(BINDIR)/foo/bar",
684 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000685 shared = {
686 "srcs": [
687 ":shared_filegroup_cpp_srcs",
688 "shared_source.cc",
689 "shared_source.cpp",
690 ],
691 "srcs_as": [
692 "shared_source.s",
693 "shared_source.S",
694 ":shared_filegroup_as_srcs",
695 ],
696 "srcs_c": [
697 "shared_source.c",
698 ":shared_filegroup_c_srcs",
699 ],
700 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000701 srcs = [
702 ":both_filegroup_cpp_srcs",
703 "both_source.cc",
704 "both_source.cpp",
705 ],
706 srcs_as = [
707 "both_source.s",
708 "both_source.S",
709 ":both_filegroup_as_srcs",
710 ],
711 srcs_c = [
712 "both_source.c",
713 ":both_filegroup_c_srcs",
714 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000715 static = {
716 "srcs": [
717 ":static_filegroup_cpp_srcs",
718 "static_source.cc",
719 "static_source.cpp",
720 ],
721 "srcs_as": [
722 "static_source.s",
723 "static_source.S",
724 ":static_filegroup_as_srcs",
725 ],
726 "srcs_c": [
727 "static_source.c",
728 ":static_filegroup_c_srcs",
729 ],
730 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000731)`},
732 })
733}
734
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200735func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
736 runCcLibraryTestCase(t, bp2buildTestCase{
737 description: "cc_library non-configured version script",
738 moduleTypeUnderTest: "cc_library",
739 moduleTypeUnderTestFactory: cc.LibraryFactory,
740 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
741 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
742 dir: "foo/bar",
743 filesystem: map[string]string{
744 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200745cc_library {
746 name: "a",
747 srcs: ["a.cpp"],
748 version_script: "v.map",
749 bazel_module: { bp2build_available: true },
750}
751`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200752 },
753 blueprint: soongCcLibraryPreamble,
754 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200755 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400756 copts = [
757 "-Ifoo/bar",
758 "-I$(BINDIR)/foo/bar",
759 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200760 srcs = ["a.cpp"],
761 version_script = "v.map",
762)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200763 })
764}
765
766func TestCcLibraryConfiguredVersionScript(t *testing.T) {
767 runCcLibraryTestCase(t, bp2buildTestCase{
768 description: "cc_library configured version script",
769 moduleTypeUnderTest: "cc_library",
770 moduleTypeUnderTestFactory: cc.LibraryFactory,
771 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
772 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
773 dir: "foo/bar",
774 filesystem: map[string]string{
775 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400776 cc_library {
777 name: "a",
778 srcs: ["a.cpp"],
779 arch: {
780 arm: {
781 version_script: "arm.map",
782 },
783 arm64: {
784 version_script: "arm64.map",
785 },
786 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200787
Liz Kammerd366c902021-06-03 13:43:01 -0400788 bazel_module: { bp2build_available: true },
789 }
790 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200791 },
792 blueprint: soongCcLibraryPreamble,
793 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200794 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400795 copts = [
796 "-Ifoo/bar",
797 "-I$(BINDIR)/foo/bar",
798 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200799 srcs = ["a.cpp"],
800 version_script = select({
801 "//build/bazel/platforms/arch:arm": "arm.map",
802 "//build/bazel/platforms/arch:arm64": "arm64.map",
803 "//conditions:default": None,
804 }),
805)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200806 })
807}
808
809func TestCcLibrarySharedLibs(t *testing.T) {
810 runCcLibraryTestCase(t, bp2buildTestCase{
811 description: "cc_library shared_libs",
812 moduleTypeUnderTest: "cc_library",
813 moduleTypeUnderTestFactory: cc.LibraryFactory,
814 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
815 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
816 dir: "foo/bar",
817 filesystem: map[string]string{
818 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400819cc_library {
820 name: "mylib",
821 bazel_module: { bp2build_available: true },
822}
823
824cc_library {
825 name: "a",
826 shared_libs: ["mylib",],
827 bazel_module: { bp2build_available: true },
828}
829`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200830 },
831 blueprint: soongCcLibraryPreamble,
832 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400833 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400834 copts = [
835 "-Ifoo/bar",
836 "-I$(BINDIR)/foo/bar",
837 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400838 dynamic_deps = [":mylib"],
839)`, `cc_library(
840 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400841 copts = [
842 "-Ifoo/bar",
843 "-I$(BINDIR)/foo/bar",
844 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400845)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200846 })
847}
848
849func TestCcLibraryPackRelocations(t *testing.T) {
850 runCcLibraryTestCase(t, bp2buildTestCase{
851 description: "cc_library pack_relocations test",
852 moduleTypeUnderTest: "cc_library",
853 moduleTypeUnderTestFactory: cc.LibraryFactory,
854 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
855 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
856 dir: "foo/bar",
857 filesystem: map[string]string{
858 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400859cc_library {
860 name: "a",
861 srcs: ["a.cpp"],
862 pack_relocations: false,
863 bazel_module: { bp2build_available: true },
864}
865
866cc_library {
867 name: "b",
868 srcs: ["b.cpp"],
869 arch: {
870 x86_64: {
Liz Kammerd366c902021-06-03 13:43:01 -0400871 pack_relocations: false,
872 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400873 },
874 bazel_module: { bp2build_available: true },
875}
876
877cc_library {
878 name: "c",
879 srcs: ["c.cpp"],
880 target: {
881 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400882 pack_relocations: false,
883 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400884 },
885 bazel_module: { bp2build_available: true },
886}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200887 },
888 blueprint: soongCcLibraryPreamble,
889 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400890 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400891 copts = [
892 "-Ifoo/bar",
893 "-I$(BINDIR)/foo/bar",
894 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400895 linkopts = ["-Wl,--pack-dyn-relocs=none"],
896 srcs = ["a.cpp"],
897)`, `cc_library(
898 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400899 copts = [
900 "-Ifoo/bar",
901 "-I$(BINDIR)/foo/bar",
902 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400903 linkopts = select({
904 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
905 "//conditions:default": [],
906 }),
907 srcs = ["b.cpp"],
908)`, `cc_library(
909 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400910 copts = [
911 "-Ifoo/bar",
912 "-I$(BINDIR)/foo/bar",
913 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400914 linkopts = select({
915 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
916 "//conditions:default": [],
917 }),
918 srcs = ["c.cpp"],
919)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200920 })
921}
922
923func TestCcLibrarySpacesInCopts(t *testing.T) {
924 runCcLibraryTestCase(t, bp2buildTestCase{
925 description: "cc_library spaces in copts",
926 moduleTypeUnderTest: "cc_library",
927 moduleTypeUnderTestFactory: cc.LibraryFactory,
928 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
929 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
930 dir: "foo/bar",
931 filesystem: map[string]string{
932 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000933cc_library {
934 name: "a",
935 cflags: ["-include header.h",],
936 bazel_module: { bp2build_available: true },
937}
938`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200939 },
940 blueprint: soongCcLibraryPreamble,
941 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000942 name = "a",
943 copts = [
944 "-include",
945 "header.h",
946 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400947 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000948 ],
949)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200950 })
951}
952
953func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
954 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400955 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200956 moduleTypeUnderTest: "cc_library",
957 moduleTypeUnderTestFactory: cc.LibraryFactory,
958 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
959 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
960 dir: "foo/bar",
961 filesystem: map[string]string{
962 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000963 name: "a",
964 srcs: ["a.cpp"],
965 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400966 "-Wall",
967 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000968 cppflags: [
969 "-fsigned-char",
970 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400971 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000972 arch: {
973 arm64: {
974 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400975 },
976 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000977 target: {
978 android: {
979 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400980 },
981 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000982 bazel_module: { bp2build_available: true },
983}
984`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200985 },
986 blueprint: soongCcLibraryPreamble,
987 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000988 name = "a",
989 copts = [
990 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000991 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400992 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400993 ],
994 cppflags = [
995 "-fsigned-char",
996 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000997 ] + select({
998 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
999 "//conditions:default": [],
1000 }) + select({
1001 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1002 "//conditions:default": [],
1003 }),
1004 srcs = ["a.cpp"],
1005)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001006 })
Jingwen Chen63930982021-03-24 10:04:33 -04001007}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001008
1009func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
1010 runCcLibraryTestCase(t, bp2buildTestCase{
1011 description: "cc_library GetTargetProperties on a LabelAttribute",
1012 moduleTypeUnderTest: "cc_library",
1013 moduleTypeUnderTestFactory: cc.LibraryFactory,
1014 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1015 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1016 dir: "foo/bar",
1017 filesystem: map[string]string{
1018 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -04001019 cc_library {
1020 name: "a",
1021 srcs: ["a.cpp"],
1022 target: {
1023 android_arm: {
1024 version_script: "android_arm.map",
1025 },
1026 linux_bionic_arm64: {
1027 version_script: "linux_bionic_arm64.map",
1028 },
1029 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001030
Liz Kammerd366c902021-06-03 13:43:01 -04001031 bazel_module: { bp2build_available: true },
1032 }
1033 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001034 },
1035 blueprint: soongCcLibraryPreamble,
1036 expectedBazelTargets: []string{`cc_library(
1037 name = "a",
1038 copts = [
1039 "-Ifoo/bar",
1040 "-I$(BINDIR)/foo/bar",
1041 ],
1042 srcs = ["a.cpp"],
1043 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -04001044 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
1045 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001046 "//conditions:default": None,
1047 }),
1048)`},
1049 })
1050}
Liz Kammer47535c52021-06-02 16:02:22 -04001051
1052func TestCcLibraryExcludeLibs(t *testing.T) {
1053 runCcLibraryTestCase(t, bp2buildTestCase{
1054 moduleTypeUnderTest: "cc_library",
1055 moduleTypeUnderTestFactory: cc.LibraryFactory,
1056 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1057 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1058 filesystem: map[string]string{},
1059 blueprint: soongCcLibraryStaticPreamble + `
1060cc_library {
1061 name: "foo_static",
1062 srcs: ["common.c"],
1063 whole_static_libs: [
1064 "arm_whole_static_lib_excludes",
1065 "malloc_not_svelte_whole_static_lib_excludes"
1066 ],
1067 static_libs: [
1068 "arm_static_lib_excludes",
1069 "malloc_not_svelte_static_lib_excludes"
1070 ],
1071 shared_libs: [
1072 "arm_shared_lib_excludes",
1073 ],
1074 arch: {
1075 arm: {
1076 exclude_shared_libs: [
1077 "arm_shared_lib_excludes",
1078 ],
1079 exclude_static_libs: [
1080 "arm_static_lib_excludes",
1081 "arm_whole_static_lib_excludes",
1082 ],
1083 },
1084 },
1085 product_variables: {
1086 malloc_not_svelte: {
1087 shared_libs: ["malloc_not_svelte_shared_lib"],
1088 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1089 exclude_static_libs: [
1090 "malloc_not_svelte_static_lib_excludes",
1091 "malloc_not_svelte_whole_static_lib_excludes",
1092 ],
1093 },
1094 },
1095}
1096
1097cc_library {
1098 name: "arm_whole_static_lib_excludes",
1099 bazel_module: { bp2build_available: false },
1100}
1101
1102cc_library {
1103 name: "malloc_not_svelte_whole_static_lib",
1104 bazel_module: { bp2build_available: false },
1105}
1106
1107cc_library {
1108 name: "malloc_not_svelte_whole_static_lib_excludes",
1109 bazel_module: { bp2build_available: false },
1110}
1111
1112cc_library {
1113 name: "arm_static_lib_excludes",
1114 bazel_module: { bp2build_available: false },
1115}
1116
1117cc_library {
1118 name: "malloc_not_svelte_static_lib_excludes",
1119 bazel_module: { bp2build_available: false },
1120}
1121
1122cc_library {
1123 name: "arm_shared_lib_excludes",
1124 bazel_module: { bp2build_available: false },
1125}
1126
1127cc_library {
1128 name: "malloc_not_svelte_shared_lib",
1129 bazel_module: { bp2build_available: false },
1130}
1131`,
1132 expectedBazelTargets: []string{
1133 `cc_library(
1134 name = "foo_static",
1135 copts = [
1136 "-I.",
1137 "-I$(BINDIR)/.",
1138 ],
1139 dynamic_deps = select({
1140 "//build/bazel/platforms/arch:arm": [],
1141 "//conditions:default": [":arm_shared_lib_excludes"],
1142 }) + select({
1143 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1144 "//conditions:default": [],
1145 }),
1146 implementation_deps = select({
1147 "//build/bazel/platforms/arch:arm": [],
1148 "//conditions:default": [":arm_static_lib_excludes"],
1149 }) + select({
1150 "//build/bazel/product_variables:malloc_not_svelte": [],
1151 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1152 }),
1153 srcs_c = ["common.c"],
1154 whole_archive_deps = select({
1155 "//build/bazel/platforms/arch:arm": [],
1156 "//conditions:default": [":arm_whole_static_lib_excludes"],
1157 }) + select({
1158 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1159 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1160 }),
1161)`,
1162 },
1163 })
1164}
Liz Kammerd366c902021-06-03 13:43:01 -04001165
1166func TestCCLibraryNoCrtTrue(t *testing.T) {
1167 runCcLibraryTestCase(t, bp2buildTestCase{
1168 description: "cc_library - simple example",
1169 moduleTypeUnderTest: "cc_library",
1170 moduleTypeUnderTestFactory: cc.LibraryFactory,
1171 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1172 filesystem: map[string]string{
1173 "impl.cpp": "",
1174 },
1175 blueprint: soongCcLibraryPreamble + `
1176cc_library_headers { name: "some-headers" }
1177cc_library {
1178 name: "foo-lib",
1179 srcs: ["impl.cpp"],
1180 no_libcrt: true,
1181}
1182`,
1183 expectedBazelTargets: []string{`cc_library(
1184 name = "foo-lib",
1185 copts = [
1186 "-I.",
1187 "-I$(BINDIR)/.",
1188 ],
1189 srcs = ["impl.cpp"],
1190 use_libcrt = False,
1191)`}})
1192}
1193
1194func TestCCLibraryNoCrtFalse(t *testing.T) {
1195 runCcLibraryTestCase(t, bp2buildTestCase{
1196 moduleTypeUnderTest: "cc_library",
1197 moduleTypeUnderTestFactory: cc.LibraryFactory,
1198 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1199 filesystem: map[string]string{
1200 "impl.cpp": "",
1201 },
1202 blueprint: soongCcLibraryPreamble + `
1203cc_library_headers { name: "some-headers" }
1204cc_library {
1205 name: "foo-lib",
1206 srcs: ["impl.cpp"],
1207 no_libcrt: false,
1208}
1209`,
1210 expectedBazelTargets: []string{`cc_library(
1211 name = "foo-lib",
1212 copts = [
1213 "-I.",
1214 "-I$(BINDIR)/.",
1215 ],
1216 srcs = ["impl.cpp"],
1217 use_libcrt = True,
1218)`}})
1219}
1220
1221func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1222 runCcLibraryTestCase(t, bp2buildTestCase{
1223 moduleTypeUnderTest: "cc_library",
1224 moduleTypeUnderTestFactory: cc.LibraryFactory,
1225 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1226 filesystem: map[string]string{
1227 "impl.cpp": "",
1228 },
1229 blueprint: soongCcLibraryPreamble + `
1230cc_library_headers { name: "some-headers" }
1231cc_library {
1232 name: "foo-lib",
1233 srcs: ["impl.cpp"],
1234 arch: {
1235 arm: {
1236 no_libcrt: true,
1237 },
1238 x86: {
1239 no_libcrt: true,
1240 },
1241 },
1242}
1243`,
1244 expectedBazelTargets: []string{`cc_library(
1245 name = "foo-lib",
1246 copts = [
1247 "-I.",
1248 "-I$(BINDIR)/.",
1249 ],
1250 srcs = ["impl.cpp"],
1251 use_libcrt = select({
1252 "//build/bazel/platforms/arch:arm": False,
1253 "//build/bazel/platforms/arch:x86": False,
1254 "//conditions:default": None,
1255 }),
1256)`}})
1257}
1258
1259func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1260 runCcLibraryTestCase(t, bp2buildTestCase{
1261 moduleTypeUnderTest: "cc_library",
1262 moduleTypeUnderTestFactory: cc.LibraryFactory,
1263 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1264 filesystem: map[string]string{
1265 "impl.cpp": "",
1266 },
1267 blueprint: soongCcLibraryPreamble + `
1268cc_library_headers { name: "some-headers" }
1269cc_library {
1270 name: "foo-lib",
1271 srcs: ["impl.cpp"],
1272 no_libcrt: false,
1273 arch: {
1274 arm: {
1275 no_libcrt: true,
1276 },
1277 x86: {
1278 no_libcrt: true,
1279 },
1280 },
1281}
1282`,
1283 expectedBazelTargets: []string{`cc_library(
1284 name = "foo-lib",
1285 copts = [
1286 "-I.",
1287 "-I$(BINDIR)/.",
1288 ],
1289 srcs = ["impl.cpp"],
1290 use_libcrt = select({
1291 "//build/bazel/platforms/arch:arm": False,
1292 "//build/bazel/platforms/arch:x86": False,
1293 "//conditions:default": True,
1294 }),
1295)`}})
1296}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001297
1298func TestCcLibraryStrip(t *testing.T) {
1299 runCcLibraryTestCase(t, bp2buildTestCase{
1300 description: "cc_library strip args",
1301 moduleTypeUnderTest: "cc_library",
1302 moduleTypeUnderTestFactory: cc.LibraryFactory,
1303 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1304 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1305 dir: "foo/bar",
1306 filesystem: map[string]string{
1307 "foo/bar/Android.bp": `
1308cc_library {
1309 name: "nothing",
1310 bazel_module: { bp2build_available: true },
1311}
1312cc_library {
1313 name: "keep_symbols",
1314 bazel_module: { bp2build_available: true },
1315 strip: {
1316 keep_symbols: true,
1317 }
1318}
1319cc_library {
1320 name: "keep_symbols_and_debug_frame",
1321 bazel_module: { bp2build_available: true },
1322 strip: {
1323 keep_symbols_and_debug_frame: true,
1324 }
1325}
1326cc_library {
1327 name: "none",
1328 bazel_module: { bp2build_available: true },
1329 strip: {
1330 none: true,
1331 }
1332}
1333cc_library {
1334 name: "keep_symbols_list",
1335 bazel_module: { bp2build_available: true },
1336 strip: {
1337 keep_symbols_list: ["symbol"],
1338 }
1339}
1340cc_library {
1341 name: "all",
1342 bazel_module: { bp2build_available: true },
1343 strip: {
1344 all: true,
1345 }
1346}
1347`,
1348 },
1349 blueprint: soongCcLibraryPreamble,
1350 expectedBazelTargets: []string{`cc_library(
1351 name = "all",
1352 copts = [
1353 "-Ifoo/bar",
1354 "-I$(BINDIR)/foo/bar",
1355 ],
1356 strip = {
1357 "all": True,
1358 },
1359)`, `cc_library(
1360 name = "keep_symbols",
1361 copts = [
1362 "-Ifoo/bar",
1363 "-I$(BINDIR)/foo/bar",
1364 ],
1365 strip = {
1366 "keep_symbols": True,
1367 },
1368)`, `cc_library(
1369 name = "keep_symbols_and_debug_frame",
1370 copts = [
1371 "-Ifoo/bar",
1372 "-I$(BINDIR)/foo/bar",
1373 ],
1374 strip = {
1375 "keep_symbols_and_debug_frame": True,
1376 },
1377)`, `cc_library(
1378 name = "keep_symbols_list",
1379 copts = [
1380 "-Ifoo/bar",
1381 "-I$(BINDIR)/foo/bar",
1382 ],
1383 strip = {
1384 "keep_symbols_list": ["symbol"],
1385 },
1386)`, `cc_library(
1387 name = "none",
1388 copts = [
1389 "-Ifoo/bar",
1390 "-I$(BINDIR)/foo/bar",
1391 ],
1392 strip = {
1393 "none": True,
1394 },
1395)`, `cc_library(
1396 name = "nothing",
1397 copts = [
1398 "-Ifoo/bar",
1399 "-I$(BINDIR)/foo/bar",
1400 ],
1401)`},
1402 })
1403}
1404
1405func TestCcLibraryStripWithArch(t *testing.T) {
1406 runCcLibraryTestCase(t, bp2buildTestCase{
1407 description: "cc_library strip args",
1408 moduleTypeUnderTest: "cc_library",
1409 moduleTypeUnderTestFactory: cc.LibraryFactory,
1410 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1411 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1412 dir: "foo/bar",
1413 filesystem: map[string]string{
1414 "foo/bar/Android.bp": `
1415cc_library {
1416 name: "multi-arch",
1417 bazel_module: { bp2build_available: true },
1418 target: {
1419 darwin: {
1420 strip: {
1421 keep_symbols_list: ["foo", "bar"]
1422 }
1423 },
1424 },
1425 arch: {
1426 arm: {
1427 strip: {
1428 keep_symbols_and_debug_frame: true,
1429 },
1430 },
1431 arm64: {
1432 strip: {
1433 keep_symbols: true,
1434 },
1435 },
1436 }
1437}
1438`,
1439 },
1440 blueprint: soongCcLibraryPreamble,
1441 expectedBazelTargets: []string{`cc_library(
1442 name = "multi-arch",
1443 copts = [
1444 "-Ifoo/bar",
1445 "-I$(BINDIR)/foo/bar",
1446 ],
1447 strip = {
1448 "keep_symbols": select({
1449 "//build/bazel/platforms/arch:arm64": True,
1450 "//conditions:default": None,
1451 }),
1452 "keep_symbols_and_debug_frame": select({
1453 "//build/bazel/platforms/arch:arm": True,
1454 "//conditions:default": None,
1455 }),
1456 "keep_symbols_list": select({
1457 "//build/bazel/platforms/os:darwin": [
1458 "foo",
1459 "bar",
1460 ],
1461 "//conditions:default": [],
1462 }),
1463 },
1464)`},
1465 })
1466}