blob: 086bafe9fee394d6d1f05b7596f90004864febc4 [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 (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000018 "testing"
19
Jingwen Chen63930982021-03-24 10:04:33 -040020 "android/soong/android"
21 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040022)
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
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057func TestCcLibrarySimple(t *testing.T) {
58 runCcLibraryTestCase(t, bp2buildTestCase{
59 description: "cc_library - simple example",
60 moduleTypeUnderTest: "cc_library",
61 moduleTypeUnderTestFactory: cc.LibraryFactory,
62 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
63 filesystem: map[string]string{
64 "android.cpp": "",
Liz Kammer01a16e82021-07-16 16:33:47 -040065 "bionic.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020066 "darwin.cpp": "",
67 // Refer to cc.headerExts for the supported header extensions in Soong.
68 "header.h": "",
69 "header.hh": "",
70 "header.hpp": "",
71 "header.hxx": "",
72 "header.h++": "",
73 "header.inl": "",
74 "header.inc": "",
75 "header.ipp": "",
76 "header.h.generic": "",
77 "impl.cpp": "",
78 "linux.cpp": "",
79 "x86.cpp": "",
80 "x86_64.cpp": "",
81 "foo-dir/a.h": "",
82 },
83 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -040084cc_library_headers { name: "some-headers" }
85cc_library {
86 name: "foo-lib",
87 srcs: ["impl.cpp"],
88 cflags: ["-Wall"],
89 header_libs: ["some-headers"],
90 export_include_dirs: ["foo-dir"],
91 ldflags: ["-Wl,--exclude-libs=bar.a"],
92 arch: {
93 x86: {
94 ldflags: ["-Wl,--exclude-libs=baz.a"],
95 srcs: ["x86.cpp"],
96 },
97 x86_64: {
98 ldflags: ["-Wl,--exclude-libs=qux.a"],
99 srcs: ["x86_64.cpp"],
100 },
101 },
102 target: {
103 android: {
104 srcs: ["android.cpp"],
105 },
106 linux_glibc: {
107 srcs: ["linux.cpp"],
108 },
109 darwin: {
110 srcs: ["darwin.cpp"],
111 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400112 bionic: {
113 srcs: ["bionic.cpp"]
114 },
Jingwen Chen63930982021-03-24 10:04:33 -0400115 },
116}
117`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200118 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400119 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000120 copts = [
121 "-Wall",
122 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400123 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000124 ],
Liz Kammer5fad5012021-09-09 14:08:21 -0400125 export_includes = ["foo-dir"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400126 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000127 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
128 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
129 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
130 "//conditions:default": [],
131 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000132 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000133 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
134 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400135 "//conditions:default": [],
136 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000137 "//build/bazel/platforms/os:android": ["android.cpp"],
138 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
139 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400140 "//conditions:default": [],
Liz Kammer01a16e82021-07-16 16:33:47 -0400141 }) + select({
142 "//build/bazel/platforms/os:bionic": ["bionic.cpp"],
143 "//conditions:default": [],
Jingwen Chen63930982021-03-24 10:04:33 -0400144 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200145)`}})
146}
147
148func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
149 runCcLibraryTestCase(t, bp2buildTestCase{
150 description: "cc_library - trimmed example of //bionic/linker:ld-android",
151 moduleTypeUnderTest: "cc_library",
152 moduleTypeUnderTestFactory: cc.LibraryFactory,
153 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
154 filesystem: map[string]string{
155 "ld-android.cpp": "",
156 "linked_list.h": "",
157 "linker.h": "",
158 "linker_block_allocator.h": "",
159 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400160 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200161 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400162cc_library_headers { name: "libc_headers" }
163cc_library {
164 name: "fake-ld-android",
165 srcs: ["ld_android.cpp"],
166 cflags: [
167 "-Wall",
168 "-Wextra",
169 "-Wunused",
170 "-Werror",
171 ],
172 header_libs: ["libc_headers"],
173 ldflags: [
174 "-Wl,--exclude-libs=libgcc.a",
175 "-Wl,--exclude-libs=libgcc_stripped.a",
176 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
177 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
178 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
179 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
180 ],
181 arch: {
182 x86: {
183 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
184 },
185 x86_64: {
186 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
187 },
188 },
189}
190`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200191 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400192 name = "fake-ld-android",
193 copts = [
194 "-Wall",
195 "-Wextra",
196 "-Wunused",
197 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000198 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400199 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400200 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400201 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400202 linkopts = [
203 "-Wl,--exclude-libs=libgcc.a",
204 "-Wl,--exclude-libs=libgcc_stripped.a",
205 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
206 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
207 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
208 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
209 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000210 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
211 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400212 "//conditions:default": [],
213 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000214 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400215)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200216 })
217}
218
219func TestCcLibraryExcludeSrcs(t *testing.T) {
220 runCcLibraryTestCase(t, bp2buildTestCase{
221 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
222 moduleTypeUnderTest: "cc_library",
223 moduleTypeUnderTestFactory: cc.LibraryFactory,
224 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
225 dir: "external",
226 filesystem: map[string]string{
227 "external/math/cosf.c": "",
228 "external/math/erf.c": "",
229 "external/math/erf_data.c": "",
230 "external/math/erff.c": "",
231 "external/math/erff_data.c": "",
232 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000233cc_library {
234 name: "fake-libarm-optimized-routines-math",
235 exclude_srcs: [
236 // Provided by:
237 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
238 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
239 "math/erf.c",
240 "math/erf_data.c",
241 "math/erff.c",
242 "math/erff_data.c",
243 ],
244 srcs: [
245 "math/*.c",
246 ],
247 // arch-specific settings
248 arch: {
249 arm64: {
250 cflags: [
251 "-DHAVE_FAST_FMA=1",
252 ],
253 },
254 },
255 bazel_module: { bp2build_available: true },
256}
257`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200258 },
259 blueprint: soongCcLibraryPreamble,
260 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000261 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400262 copts = [
263 "-Iexternal",
264 "-I$(BINDIR)/external",
265 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000266 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
267 "//conditions:default": [],
268 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400269 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000270)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200271 })
272}
273
274func TestCcLibrarySharedStaticProps(t *testing.T) {
275 runCcLibraryTestCase(t, bp2buildTestCase{
276 description: "cc_library shared/static props",
277 moduleTypeUnderTest: "cc_library",
278 moduleTypeUnderTestFactory: cc.LibraryFactory,
279 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200280 dir: "foo/bar",
281 filesystem: map[string]string{
282 "foo/bar/both.cpp": "",
283 "foo/bar/sharedonly.cpp": "",
284 "foo/bar/staticonly.cpp": "",
285 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000286cc_library {
287 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400288 srcs: ["both.cpp"],
289 cflags: ["bothflag"],
290 shared_libs: ["shared_dep_for_both"],
291 static_libs: ["static_dep_for_both"],
292 whole_static_libs: ["whole_static_lib_for_both"],
293 static: {
294 srcs: ["staticonly.cpp"],
295 cflags: ["staticflag"],
296 shared_libs: ["shared_dep_for_static"],
297 static_libs: ["static_dep_for_static"],
298 whole_static_libs: ["whole_static_lib_for_static"],
299 },
300 shared: {
301 srcs: ["sharedonly.cpp"],
302 cflags: ["sharedflag"],
303 shared_libs: ["shared_dep_for_shared"],
304 static_libs: ["static_dep_for_shared"],
305 whole_static_libs: ["whole_static_lib_for_shared"],
306 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000307 bazel_module: { bp2build_available: true },
308}
309
Chris Parsons08648312021-05-06 16:23:19 -0400310cc_library_static { name: "static_dep_for_shared" }
311
312cc_library_static { name: "static_dep_for_static" }
313
314cc_library_static { name: "static_dep_for_both" }
315
316cc_library_static { name: "whole_static_lib_for_shared" }
317
318cc_library_static { name: "whole_static_lib_for_static" }
319
320cc_library_static { name: "whole_static_lib_for_both" }
321
322cc_library { name: "shared_dep_for_shared" }
323
324cc_library { name: "shared_dep_for_static" }
325
326cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000327`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200328 },
329 blueprint: soongCcLibraryPreamble,
330 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000331 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400332 copts = [
333 "bothflag",
334 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400335 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400336 ],
Chris Parsons08648312021-05-06 16:23:19 -0400337 dynamic_deps = [":shared_dep_for_both"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400338 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000339 shared = {
340 "copts": ["sharedflag"],
341 "dynamic_deps": [":shared_dep_for_shared"],
342 "srcs": ["sharedonly.cpp"],
343 "static_deps": [":static_dep_for_shared"],
344 "whole_archive_deps": [":whole_static_lib_for_shared"],
345 },
Chris Parsons08648312021-05-06 16:23:19 -0400346 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000347 static = {
348 "copts": ["staticflag"],
349 "dynamic_deps": [":shared_dep_for_static"],
350 "srcs": ["staticonly.cpp"],
351 "static_deps": [":static_dep_for_static"],
352 "whole_archive_deps": [":whole_static_lib_for_static"],
353 },
Chris Parsons08648312021-05-06 16:23:19 -0400354 whole_archive_deps = [":whole_static_lib_for_both"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000355)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200356 })
357}
358
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400359func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
360 runCcLibraryTestCase(t, bp2buildTestCase{
361 moduleTypeUnderTest: "cc_library",
362 moduleTypeUnderTestFactory: cc.LibraryFactory,
363 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400364 dir: "foo/bar",
365 filesystem: map[string]string{
366 "foo/bar/Android.bp": `
367cc_library {
368 name: "a",
369 whole_static_libs: ["whole_static_lib_for_both"],
370 static: {
371 whole_static_libs: ["whole_static_lib_for_static"],
372 },
373 shared: {
374 whole_static_libs: ["whole_static_lib_for_shared"],
375 },
376 bazel_module: { bp2build_available: true },
377}
378
379cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
380
381cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
382
383cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
384`,
385 },
386 blueprint: soongCcLibraryPreamble,
387 expectedBazelTargets: []string{`cc_library(
388 name = "a",
389 copts = [
390 "-Ifoo/bar",
391 "-I$(BINDIR)/foo/bar",
392 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000393 shared = {
394 "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
395 },
396 static = {
397 "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
398 },
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400399 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400400)`},
401 })
402}
403
Jingwen Chenbcf53042021-05-26 04:42:42 +0000404func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
405 runCcLibraryTestCase(t, bp2buildTestCase{
406 description: "cc_library shared/static props in arch",
407 moduleTypeUnderTest: "cc_library",
408 moduleTypeUnderTestFactory: cc.LibraryFactory,
409 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chenbcf53042021-05-26 04:42:42 +0000410 dir: "foo/bar",
411 filesystem: map[string]string{
412 "foo/bar/arm.cpp": "",
413 "foo/bar/x86.cpp": "",
414 "foo/bar/sharedonly.cpp": "",
415 "foo/bar/staticonly.cpp": "",
416 "foo/bar/Android.bp": `
417cc_library {
418 name: "a",
419 arch: {
420 arm: {
421 shared: {
422 srcs: ["arm_shared.cpp"],
423 cflags: ["-DARM_SHARED"],
424 static_libs: ["arm_static_dep_for_shared"],
425 whole_static_libs: ["arm_whole_static_dep_for_shared"],
426 shared_libs: ["arm_shared_dep_for_shared"],
427 },
428 },
429 x86: {
430 static: {
431 srcs: ["x86_static.cpp"],
432 cflags: ["-DX86_STATIC"],
433 static_libs: ["x86_dep_for_static"],
434 },
435 },
436 },
437 target: {
438 android: {
439 shared: {
440 srcs: ["android_shared.cpp"],
441 cflags: ["-DANDROID_SHARED"],
442 static_libs: ["android_dep_for_shared"],
443 },
444 },
445 android_arm: {
446 shared: {
447 cflags: ["-DANDROID_ARM_SHARED"],
448 },
449 },
450 },
451 srcs: ["both.cpp"],
452 cflags: ["bothflag"],
453 static_libs: ["static_dep_for_both"],
454 static: {
455 srcs: ["staticonly.cpp"],
456 cflags: ["staticflag"],
457 static_libs: ["static_dep_for_static"],
458 },
459 shared: {
460 srcs: ["sharedonly.cpp"],
461 cflags: ["sharedflag"],
462 static_libs: ["static_dep_for_shared"],
463 },
464 bazel_module: { bp2build_available: true },
465}
466
467cc_library_static { name: "static_dep_for_shared" }
468cc_library_static { name: "static_dep_for_static" }
469cc_library_static { name: "static_dep_for_both" }
470
471cc_library_static { name: "arm_static_dep_for_shared" }
472cc_library_static { name: "arm_whole_static_dep_for_shared" }
473cc_library_static { name: "arm_shared_dep_for_shared" }
474
475cc_library_static { name: "x86_dep_for_static" }
476
477cc_library_static { name: "android_dep_for_shared" }
478`,
479 },
480 blueprint: soongCcLibraryPreamble,
481 expectedBazelTargets: []string{`cc_library(
482 name = "a",
483 copts = [
484 "bothflag",
485 "-Ifoo/bar",
486 "-I$(BINDIR)/foo/bar",
487 ],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000488 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000489 shared = {
490 "copts": ["sharedflag"] + select({
491 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
492 "//conditions:default": [],
493 }) + select({
494 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
495 "//conditions:default": [],
496 }) + select({
497 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
498 "//conditions:default": [],
499 }),
500 "dynamic_deps": select({
501 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
502 "//conditions:default": [],
503 }),
504 "srcs": ["sharedonly.cpp"] + select({
505 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
506 "//conditions:default": [],
507 }) + select({
508 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
509 "//conditions:default": [],
510 }),
511 "static_deps": [":static_dep_for_shared"] + select({
512 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
513 "//conditions:default": [],
514 }) + select({
515 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
516 "//conditions:default": [],
517 }),
518 "whole_archive_deps": select({
519 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
520 "//conditions:default": [],
521 }),
522 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000523 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000524 static = {
525 "copts": ["staticflag"] + select({
526 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
527 "//conditions:default": [],
528 }),
529 "srcs": ["staticonly.cpp"] + select({
530 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
531 "//conditions:default": [],
532 }),
533 "static_deps": [":static_dep_for_static"] + select({
534 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
535 "//conditions:default": [],
536 }),
537 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000538)`},
539 })
540}
541
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000542func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
543 runCcLibraryTestCase(t, bp2buildTestCase{
544 description: "cc_library shared/static props with c/cpp/s mixed sources",
545 moduleTypeUnderTest: "cc_library",
546 moduleTypeUnderTestFactory: cc.LibraryFactory,
547 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000548 dir: "foo/bar",
549 filesystem: map[string]string{
550 "foo/bar/both_source.cpp": "",
551 "foo/bar/both_source.cc": "",
552 "foo/bar/both_source.c": "",
553 "foo/bar/both_source.s": "",
554 "foo/bar/both_source.S": "",
555 "foo/bar/shared_source.cpp": "",
556 "foo/bar/shared_source.cc": "",
557 "foo/bar/shared_source.c": "",
558 "foo/bar/shared_source.s": "",
559 "foo/bar/shared_source.S": "",
560 "foo/bar/static_source.cpp": "",
561 "foo/bar/static_source.cc": "",
562 "foo/bar/static_source.c": "",
563 "foo/bar/static_source.s": "",
564 "foo/bar/static_source.S": "",
565 "foo/bar/Android.bp": `
566cc_library {
567 name: "a",
568 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400569 "both_source.cpp",
570 "both_source.cc",
571 "both_source.c",
572 "both_source.s",
573 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000574 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400575 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000576 static: {
Liz Kammerd366c902021-06-03 13:43:01 -0400577 srcs: [
578 "static_source.cpp",
579 "static_source.cc",
580 "static_source.c",
581 "static_source.s",
582 "static_source.S",
583 ":static_filegroup",
584 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000585 },
586 shared: {
Liz Kammerd366c902021-06-03 13:43:01 -0400587 srcs: [
588 "shared_source.cpp",
589 "shared_source.cc",
590 "shared_source.c",
591 "shared_source.s",
592 "shared_source.S",
593 ":shared_filegroup",
594 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000595 },
596 bazel_module: { bp2build_available: true },
597}
598
599filegroup {
600 name: "both_filegroup",
601 srcs: [
602 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400603 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000604}
605
606filegroup {
607 name: "shared_filegroup",
608 srcs: [
609 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400610 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000611}
612
613filegroup {
614 name: "static_filegroup",
615 srcs: [
616 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400617 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000618}
619`,
620 },
621 blueprint: soongCcLibraryPreamble,
622 expectedBazelTargets: []string{`cc_library(
623 name = "a",
Chris Parsons69fa9f92021-07-13 11:47:44 -0400624 asflags = [
625 "-Ifoo/bar",
626 "-I$(BINDIR)/foo/bar",
627 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000628 copts = [
629 "-Ifoo/bar",
630 "-I$(BINDIR)/foo/bar",
631 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000632 shared = {
633 "srcs": [
634 ":shared_filegroup_cpp_srcs",
635 "shared_source.cc",
636 "shared_source.cpp",
637 ],
638 "srcs_as": [
639 "shared_source.s",
640 "shared_source.S",
641 ":shared_filegroup_as_srcs",
642 ],
643 "srcs_c": [
644 "shared_source.c",
645 ":shared_filegroup_c_srcs",
646 ],
647 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000648 srcs = [
649 ":both_filegroup_cpp_srcs",
650 "both_source.cc",
651 "both_source.cpp",
652 ],
653 srcs_as = [
654 "both_source.s",
655 "both_source.S",
656 ":both_filegroup_as_srcs",
657 ],
658 srcs_c = [
659 "both_source.c",
660 ":both_filegroup_c_srcs",
661 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000662 static = {
663 "srcs": [
664 ":static_filegroup_cpp_srcs",
665 "static_source.cc",
666 "static_source.cpp",
667 ],
668 "srcs_as": [
669 "static_source.s",
670 "static_source.S",
671 ":static_filegroup_as_srcs",
672 ],
673 "srcs_c": [
674 "static_source.c",
675 ":static_filegroup_c_srcs",
676 ],
677 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000678)`},
679 })
680}
681
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200682func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
683 runCcLibraryTestCase(t, bp2buildTestCase{
684 description: "cc_library non-configured version script",
685 moduleTypeUnderTest: "cc_library",
686 moduleTypeUnderTestFactory: cc.LibraryFactory,
687 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200688 dir: "foo/bar",
689 filesystem: map[string]string{
690 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200691cc_library {
692 name: "a",
693 srcs: ["a.cpp"],
694 version_script: "v.map",
695 bazel_module: { bp2build_available: true },
696}
697`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200698 },
699 blueprint: soongCcLibraryPreamble,
700 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200701 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400702 copts = [
703 "-Ifoo/bar",
704 "-I$(BINDIR)/foo/bar",
705 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200706 srcs = ["a.cpp"],
707 version_script = "v.map",
708)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200709 })
710}
711
712func TestCcLibraryConfiguredVersionScript(t *testing.T) {
713 runCcLibraryTestCase(t, bp2buildTestCase{
714 description: "cc_library configured version script",
715 moduleTypeUnderTest: "cc_library",
716 moduleTypeUnderTestFactory: cc.LibraryFactory,
717 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200718 dir: "foo/bar",
719 filesystem: map[string]string{
720 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400721 cc_library {
722 name: "a",
723 srcs: ["a.cpp"],
724 arch: {
725 arm: {
726 version_script: "arm.map",
727 },
728 arm64: {
729 version_script: "arm64.map",
730 },
731 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200732
Liz Kammerd366c902021-06-03 13:43:01 -0400733 bazel_module: { bp2build_available: true },
734 }
735 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200736 },
737 blueprint: soongCcLibraryPreamble,
738 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200739 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400740 copts = [
741 "-Ifoo/bar",
742 "-I$(BINDIR)/foo/bar",
743 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200744 srcs = ["a.cpp"],
745 version_script = select({
746 "//build/bazel/platforms/arch:arm": "arm.map",
747 "//build/bazel/platforms/arch:arm64": "arm64.map",
748 "//conditions:default": None,
749 }),
750)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200751 })
752}
753
754func TestCcLibrarySharedLibs(t *testing.T) {
755 runCcLibraryTestCase(t, bp2buildTestCase{
756 description: "cc_library shared_libs",
757 moduleTypeUnderTest: "cc_library",
758 moduleTypeUnderTestFactory: cc.LibraryFactory,
759 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200760 dir: "foo/bar",
761 filesystem: map[string]string{
762 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400763cc_library {
764 name: "mylib",
765 bazel_module: { bp2build_available: true },
766}
767
768cc_library {
769 name: "a",
770 shared_libs: ["mylib",],
771 bazel_module: { bp2build_available: true },
772}
773`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200774 },
775 blueprint: soongCcLibraryPreamble,
776 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400777 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400778 copts = [
779 "-Ifoo/bar",
780 "-I$(BINDIR)/foo/bar",
781 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400782 dynamic_deps = [":mylib"],
783)`, `cc_library(
784 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400785 copts = [
786 "-Ifoo/bar",
787 "-I$(BINDIR)/foo/bar",
788 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400789)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200790 })
791}
792
793func TestCcLibraryPackRelocations(t *testing.T) {
794 runCcLibraryTestCase(t, bp2buildTestCase{
795 description: "cc_library pack_relocations test",
796 moduleTypeUnderTest: "cc_library",
797 moduleTypeUnderTestFactory: cc.LibraryFactory,
798 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200799 dir: "foo/bar",
800 filesystem: map[string]string{
801 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400802cc_library {
803 name: "a",
804 srcs: ["a.cpp"],
805 pack_relocations: false,
806 bazel_module: { bp2build_available: true },
807}
808
809cc_library {
810 name: "b",
811 srcs: ["b.cpp"],
812 arch: {
813 x86_64: {
Liz Kammerd366c902021-06-03 13:43:01 -0400814 pack_relocations: false,
815 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400816 },
817 bazel_module: { bp2build_available: true },
818}
819
820cc_library {
821 name: "c",
822 srcs: ["c.cpp"],
823 target: {
824 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400825 pack_relocations: false,
826 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400827 },
828 bazel_module: { bp2build_available: true },
829}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200830 },
831 blueprint: soongCcLibraryPreamble,
832 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400833 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400834 copts = [
835 "-Ifoo/bar",
836 "-I$(BINDIR)/foo/bar",
837 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400838 linkopts = ["-Wl,--pack-dyn-relocs=none"],
839 srcs = ["a.cpp"],
840)`, `cc_library(
841 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400842 copts = [
843 "-Ifoo/bar",
844 "-I$(BINDIR)/foo/bar",
845 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400846 linkopts = select({
847 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
848 "//conditions:default": [],
849 }),
850 srcs = ["b.cpp"],
851)`, `cc_library(
852 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400853 copts = [
854 "-Ifoo/bar",
855 "-I$(BINDIR)/foo/bar",
856 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400857 linkopts = select({
858 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
859 "//conditions:default": [],
860 }),
861 srcs = ["c.cpp"],
862)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200863 })
864}
865
866func TestCcLibrarySpacesInCopts(t *testing.T) {
867 runCcLibraryTestCase(t, bp2buildTestCase{
868 description: "cc_library spaces in copts",
869 moduleTypeUnderTest: "cc_library",
870 moduleTypeUnderTestFactory: cc.LibraryFactory,
871 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200872 dir: "foo/bar",
873 filesystem: map[string]string{
874 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000875cc_library {
876 name: "a",
877 cflags: ["-include header.h",],
878 bazel_module: { bp2build_available: true },
879}
880`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200881 },
882 blueprint: soongCcLibraryPreamble,
883 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000884 name = "a",
885 copts = [
886 "-include",
887 "header.h",
888 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400889 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000890 ],
891)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200892 })
893}
894
895func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
896 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400897 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200898 moduleTypeUnderTest: "cc_library",
899 moduleTypeUnderTestFactory: cc.LibraryFactory,
900 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200901 dir: "foo/bar",
902 filesystem: map[string]string{
903 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000904 name: "a",
905 srcs: ["a.cpp"],
906 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400907 "-Wall",
908 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000909 cppflags: [
910 "-fsigned-char",
911 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400912 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000913 arch: {
914 arm64: {
915 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400916 },
917 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000918 target: {
919 android: {
920 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400921 },
922 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000923 bazel_module: { bp2build_available: true },
924}
925`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200926 },
927 blueprint: soongCcLibraryPreamble,
928 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000929 name = "a",
930 copts = [
931 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000932 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400933 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400934 ],
935 cppflags = [
936 "-fsigned-char",
937 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000938 ] + select({
939 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
940 "//conditions:default": [],
941 }) + select({
942 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
943 "//conditions:default": [],
944 }),
945 srcs = ["a.cpp"],
946)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200947 })
Jingwen Chen63930982021-03-24 10:04:33 -0400948}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400949
950func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
951 runCcLibraryTestCase(t, bp2buildTestCase{
952 description: "cc_library GetTargetProperties on a LabelAttribute",
953 moduleTypeUnderTest: "cc_library",
954 moduleTypeUnderTestFactory: cc.LibraryFactory,
955 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400956 dir: "foo/bar",
957 filesystem: map[string]string{
958 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400959 cc_library {
960 name: "a",
961 srcs: ["a.cpp"],
962 target: {
963 android_arm: {
964 version_script: "android_arm.map",
965 },
966 linux_bionic_arm64: {
967 version_script: "linux_bionic_arm64.map",
968 },
969 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400970
Liz Kammerd366c902021-06-03 13:43:01 -0400971 bazel_module: { bp2build_available: true },
972 }
973 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400974 },
975 blueprint: soongCcLibraryPreamble,
976 expectedBazelTargets: []string{`cc_library(
977 name = "a",
978 copts = [
979 "-Ifoo/bar",
980 "-I$(BINDIR)/foo/bar",
981 ],
982 srcs = ["a.cpp"],
983 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400984 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
985 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400986 "//conditions:default": None,
987 }),
988)`},
989 })
990}
Liz Kammer47535c52021-06-02 16:02:22 -0400991
992func TestCcLibraryExcludeLibs(t *testing.T) {
993 runCcLibraryTestCase(t, bp2buildTestCase{
994 moduleTypeUnderTest: "cc_library",
995 moduleTypeUnderTestFactory: cc.LibraryFactory,
996 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer47535c52021-06-02 16:02:22 -0400997 filesystem: map[string]string{},
998 blueprint: soongCcLibraryStaticPreamble + `
999cc_library {
1000 name: "foo_static",
1001 srcs: ["common.c"],
1002 whole_static_libs: [
1003 "arm_whole_static_lib_excludes",
1004 "malloc_not_svelte_whole_static_lib_excludes"
1005 ],
1006 static_libs: [
1007 "arm_static_lib_excludes",
1008 "malloc_not_svelte_static_lib_excludes"
1009 ],
1010 shared_libs: [
1011 "arm_shared_lib_excludes",
1012 ],
1013 arch: {
1014 arm: {
1015 exclude_shared_libs: [
1016 "arm_shared_lib_excludes",
1017 ],
1018 exclude_static_libs: [
1019 "arm_static_lib_excludes",
1020 "arm_whole_static_lib_excludes",
1021 ],
1022 },
1023 },
1024 product_variables: {
1025 malloc_not_svelte: {
1026 shared_libs: ["malloc_not_svelte_shared_lib"],
1027 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1028 exclude_static_libs: [
1029 "malloc_not_svelte_static_lib_excludes",
1030 "malloc_not_svelte_whole_static_lib_excludes",
1031 ],
1032 },
1033 },
1034}
1035
1036cc_library {
1037 name: "arm_whole_static_lib_excludes",
1038 bazel_module: { bp2build_available: false },
1039}
1040
1041cc_library {
1042 name: "malloc_not_svelte_whole_static_lib",
1043 bazel_module: { bp2build_available: false },
1044}
1045
1046cc_library {
1047 name: "malloc_not_svelte_whole_static_lib_excludes",
1048 bazel_module: { bp2build_available: false },
1049}
1050
1051cc_library {
1052 name: "arm_static_lib_excludes",
1053 bazel_module: { bp2build_available: false },
1054}
1055
1056cc_library {
1057 name: "malloc_not_svelte_static_lib_excludes",
1058 bazel_module: { bp2build_available: false },
1059}
1060
1061cc_library {
1062 name: "arm_shared_lib_excludes",
1063 bazel_module: { bp2build_available: false },
1064}
1065
1066cc_library {
1067 name: "malloc_not_svelte_shared_lib",
1068 bazel_module: { bp2build_available: false },
1069}
1070`,
1071 expectedBazelTargets: []string{
1072 `cc_library(
1073 name = "foo_static",
1074 copts = [
1075 "-I.",
1076 "-I$(BINDIR)/.",
1077 ],
1078 dynamic_deps = select({
1079 "//build/bazel/platforms/arch:arm": [],
1080 "//conditions:default": [":arm_shared_lib_excludes"],
1081 }) + select({
1082 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1083 "//conditions:default": [],
1084 }),
1085 implementation_deps = select({
1086 "//build/bazel/platforms/arch:arm": [],
1087 "//conditions:default": [":arm_static_lib_excludes"],
1088 }) + select({
1089 "//build/bazel/product_variables:malloc_not_svelte": [],
1090 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1091 }),
1092 srcs_c = ["common.c"],
1093 whole_archive_deps = select({
1094 "//build/bazel/platforms/arch:arm": [],
1095 "//conditions:default": [":arm_whole_static_lib_excludes"],
1096 }) + select({
1097 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1098 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1099 }),
1100)`,
1101 },
1102 })
1103}
Liz Kammerd366c902021-06-03 13:43:01 -04001104
1105func TestCCLibraryNoCrtTrue(t *testing.T) {
1106 runCcLibraryTestCase(t, bp2buildTestCase{
1107 description: "cc_library - simple example",
1108 moduleTypeUnderTest: "cc_library",
1109 moduleTypeUnderTestFactory: cc.LibraryFactory,
1110 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1111 filesystem: map[string]string{
1112 "impl.cpp": "",
1113 },
1114 blueprint: soongCcLibraryPreamble + `
1115cc_library_headers { name: "some-headers" }
1116cc_library {
1117 name: "foo-lib",
1118 srcs: ["impl.cpp"],
1119 no_libcrt: true,
1120}
1121`,
1122 expectedBazelTargets: []string{`cc_library(
1123 name = "foo-lib",
1124 copts = [
1125 "-I.",
1126 "-I$(BINDIR)/.",
1127 ],
1128 srcs = ["impl.cpp"],
1129 use_libcrt = False,
1130)`}})
1131}
1132
1133func TestCCLibraryNoCrtFalse(t *testing.T) {
1134 runCcLibraryTestCase(t, bp2buildTestCase{
1135 moduleTypeUnderTest: "cc_library",
1136 moduleTypeUnderTestFactory: cc.LibraryFactory,
1137 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1138 filesystem: map[string]string{
1139 "impl.cpp": "",
1140 },
1141 blueprint: soongCcLibraryPreamble + `
1142cc_library_headers { name: "some-headers" }
1143cc_library {
1144 name: "foo-lib",
1145 srcs: ["impl.cpp"],
1146 no_libcrt: false,
1147}
1148`,
1149 expectedBazelTargets: []string{`cc_library(
1150 name = "foo-lib",
1151 copts = [
1152 "-I.",
1153 "-I$(BINDIR)/.",
1154 ],
1155 srcs = ["impl.cpp"],
1156 use_libcrt = True,
1157)`}})
1158}
1159
1160func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1161 runCcLibraryTestCase(t, bp2buildTestCase{
1162 moduleTypeUnderTest: "cc_library",
1163 moduleTypeUnderTestFactory: cc.LibraryFactory,
1164 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1165 filesystem: map[string]string{
1166 "impl.cpp": "",
1167 },
1168 blueprint: soongCcLibraryPreamble + `
1169cc_library_headers { name: "some-headers" }
1170cc_library {
1171 name: "foo-lib",
1172 srcs: ["impl.cpp"],
1173 arch: {
1174 arm: {
1175 no_libcrt: true,
1176 },
1177 x86: {
1178 no_libcrt: true,
1179 },
1180 },
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 = select({
1191 "//build/bazel/platforms/arch:arm": False,
1192 "//build/bazel/platforms/arch:x86": False,
1193 "//conditions:default": None,
1194 }),
1195)`}})
1196}
1197
1198func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1199 runCcLibraryTestCase(t, bp2buildTestCase{
1200 moduleTypeUnderTest: "cc_library",
1201 moduleTypeUnderTestFactory: cc.LibraryFactory,
1202 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1203 filesystem: map[string]string{
1204 "impl.cpp": "",
1205 },
1206 blueprint: soongCcLibraryPreamble + `
1207cc_library_headers { name: "some-headers" }
1208cc_library {
1209 name: "foo-lib",
1210 srcs: ["impl.cpp"],
1211 no_libcrt: false,
1212 arch: {
1213 arm: {
1214 no_libcrt: true,
1215 },
1216 x86: {
1217 no_libcrt: true,
1218 },
1219 },
1220}
1221`,
1222 expectedBazelTargets: []string{`cc_library(
1223 name = "foo-lib",
1224 copts = [
1225 "-I.",
1226 "-I$(BINDIR)/.",
1227 ],
1228 srcs = ["impl.cpp"],
1229 use_libcrt = select({
1230 "//build/bazel/platforms/arch:arm": False,
1231 "//build/bazel/platforms/arch:x86": False,
1232 "//conditions:default": True,
1233 }),
1234)`}})
1235}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001236
1237func TestCcLibraryStrip(t *testing.T) {
1238 runCcLibraryTestCase(t, bp2buildTestCase{
1239 description: "cc_library strip args",
1240 moduleTypeUnderTest: "cc_library",
1241 moduleTypeUnderTestFactory: cc.LibraryFactory,
1242 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001243 dir: "foo/bar",
1244 filesystem: map[string]string{
1245 "foo/bar/Android.bp": `
1246cc_library {
1247 name: "nothing",
1248 bazel_module: { bp2build_available: true },
1249}
1250cc_library {
1251 name: "keep_symbols",
1252 bazel_module: { bp2build_available: true },
1253 strip: {
1254 keep_symbols: true,
1255 }
1256}
1257cc_library {
1258 name: "keep_symbols_and_debug_frame",
1259 bazel_module: { bp2build_available: true },
1260 strip: {
1261 keep_symbols_and_debug_frame: true,
1262 }
1263}
1264cc_library {
1265 name: "none",
1266 bazel_module: { bp2build_available: true },
1267 strip: {
1268 none: true,
1269 }
1270}
1271cc_library {
1272 name: "keep_symbols_list",
1273 bazel_module: { bp2build_available: true },
1274 strip: {
1275 keep_symbols_list: ["symbol"],
1276 }
1277}
1278cc_library {
1279 name: "all",
1280 bazel_module: { bp2build_available: true },
1281 strip: {
1282 all: true,
1283 }
1284}
1285`,
1286 },
1287 blueprint: soongCcLibraryPreamble,
1288 expectedBazelTargets: []string{`cc_library(
1289 name = "all",
1290 copts = [
1291 "-Ifoo/bar",
1292 "-I$(BINDIR)/foo/bar",
1293 ],
1294 strip = {
1295 "all": True,
1296 },
1297)`, `cc_library(
1298 name = "keep_symbols",
1299 copts = [
1300 "-Ifoo/bar",
1301 "-I$(BINDIR)/foo/bar",
1302 ],
1303 strip = {
1304 "keep_symbols": True,
1305 },
1306)`, `cc_library(
1307 name = "keep_symbols_and_debug_frame",
1308 copts = [
1309 "-Ifoo/bar",
1310 "-I$(BINDIR)/foo/bar",
1311 ],
1312 strip = {
1313 "keep_symbols_and_debug_frame": True,
1314 },
1315)`, `cc_library(
1316 name = "keep_symbols_list",
1317 copts = [
1318 "-Ifoo/bar",
1319 "-I$(BINDIR)/foo/bar",
1320 ],
1321 strip = {
1322 "keep_symbols_list": ["symbol"],
1323 },
1324)`, `cc_library(
1325 name = "none",
1326 copts = [
1327 "-Ifoo/bar",
1328 "-I$(BINDIR)/foo/bar",
1329 ],
1330 strip = {
1331 "none": True,
1332 },
1333)`, `cc_library(
1334 name = "nothing",
1335 copts = [
1336 "-Ifoo/bar",
1337 "-I$(BINDIR)/foo/bar",
1338 ],
1339)`},
1340 })
1341}
1342
1343func TestCcLibraryStripWithArch(t *testing.T) {
1344 runCcLibraryTestCase(t, bp2buildTestCase{
1345 description: "cc_library strip args",
1346 moduleTypeUnderTest: "cc_library",
1347 moduleTypeUnderTestFactory: cc.LibraryFactory,
1348 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001349 dir: "foo/bar",
1350 filesystem: map[string]string{
1351 "foo/bar/Android.bp": `
1352cc_library {
1353 name: "multi-arch",
1354 bazel_module: { bp2build_available: true },
1355 target: {
1356 darwin: {
1357 strip: {
1358 keep_symbols_list: ["foo", "bar"]
1359 }
1360 },
1361 },
1362 arch: {
1363 arm: {
1364 strip: {
1365 keep_symbols_and_debug_frame: true,
1366 },
1367 },
1368 arm64: {
1369 strip: {
1370 keep_symbols: true,
1371 },
1372 },
1373 }
1374}
1375`,
1376 },
1377 blueprint: soongCcLibraryPreamble,
1378 expectedBazelTargets: []string{`cc_library(
1379 name = "multi-arch",
1380 copts = [
1381 "-Ifoo/bar",
1382 "-I$(BINDIR)/foo/bar",
1383 ],
1384 strip = {
1385 "keep_symbols": select({
1386 "//build/bazel/platforms/arch:arm64": True,
1387 "//conditions:default": None,
1388 }),
1389 "keep_symbols_and_debug_frame": select({
1390 "//build/bazel/platforms/arch:arm": True,
1391 "//conditions:default": None,
1392 }),
1393 "keep_symbols_list": select({
1394 "//build/bazel/platforms/os:darwin": [
1395 "foo",
1396 "bar",
1397 ],
1398 "//conditions:default": [],
1399 }),
1400 },
1401)`},
1402 })
1403}
Chris Parsons51f8c392021-08-03 21:01:05 -04001404
1405func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1406 runCcLibraryTestCase(t, bp2buildTestCase{
1407 description: "cc_library system_shared_libs empty at root",
1408 moduleTypeUnderTest: "cc_library",
1409 moduleTypeUnderTestFactory: cc.LibraryFactory,
1410 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1411 blueprint: soongCcLibraryPreamble + `
1412cc_library {
1413 name: "root_empty",
1414 system_shared_libs: [],
1415}
1416`,
1417 expectedBazelTargets: []string{`cc_library(
1418 name = "root_empty",
1419 copts = [
1420 "-I.",
1421 "-I$(BINDIR)/.",
1422 ],
1423 system_dynamic_deps = [],
1424)`},
1425 })
1426}
1427
1428func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1429 runCcLibraryTestCase(t, bp2buildTestCase{
1430 description: "cc_library system_shared_libs empty for static variant",
1431 moduleTypeUnderTest: "cc_library",
1432 moduleTypeUnderTestFactory: cc.LibraryFactory,
1433 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1434 blueprint: soongCcLibraryPreamble + `
1435cc_library {
1436 name: "static_empty",
1437 static: {
1438 system_shared_libs: [],
1439 },
1440}
1441`,
1442 expectedBazelTargets: []string{`cc_library(
1443 name = "static_empty",
1444 copts = [
1445 "-I.",
1446 "-I$(BINDIR)/.",
1447 ],
1448 static = {
1449 "system_dynamic_deps": [],
1450 },
1451)`},
1452 })
1453}
1454
1455func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
1456 runCcLibraryTestCase(t, bp2buildTestCase{
1457 description: "cc_library system_shared_libs empty for shared variant",
1458 moduleTypeUnderTest: "cc_library",
1459 moduleTypeUnderTestFactory: cc.LibraryFactory,
1460 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1461 blueprint: soongCcLibraryPreamble + `
1462cc_library {
1463 name: "shared_empty",
1464 shared: {
1465 system_shared_libs: [],
1466 },
1467}
1468`,
1469 expectedBazelTargets: []string{`cc_library(
1470 name = "shared_empty",
1471 copts = [
1472 "-I.",
1473 "-I$(BINDIR)/.",
1474 ],
1475 shared = {
1476 "system_dynamic_deps": [],
1477 },
1478)`},
1479 })
1480}
1481
1482func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
1483 runCcLibraryTestCase(t, bp2buildTestCase{
1484 description: "cc_library system_shared_libs empty for shared, bionic variant",
1485 moduleTypeUnderTest: "cc_library",
1486 moduleTypeUnderTestFactory: cc.LibraryFactory,
1487 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1488 blueprint: soongCcLibraryPreamble + `
1489cc_library {
1490 name: "shared_empty",
1491 target: {
1492 bionic: {
1493 shared: {
1494 system_shared_libs: [],
1495 }
1496 }
1497 },
1498}
1499`,
1500 expectedBazelTargets: []string{`cc_library(
1501 name = "shared_empty",
1502 copts = [
1503 "-I.",
1504 "-I$(BINDIR)/.",
1505 ],
1506 shared = {
1507 "system_dynamic_deps": [],
1508 },
1509)`},
1510 })
1511}
1512
1513func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1514 // Note that this behavior is technically incorrect (it's a simplification).
1515 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1516 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1517 // b/195791252 tracks the fix.
1518 runCcLibraryTestCase(t, bp2buildTestCase{
1519 description: "cc_library system_shared_libs empty for linux_bionic variant",
1520 moduleTypeUnderTest: "cc_library",
1521 moduleTypeUnderTestFactory: cc.LibraryFactory,
1522 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1523 blueprint: soongCcLibraryPreamble + `
1524cc_library {
1525 name: "target_linux_bionic_empty",
1526 target: {
1527 linux_bionic: {
1528 system_shared_libs: [],
1529 },
1530 },
1531}
1532`,
1533 expectedBazelTargets: []string{`cc_library(
1534 name = "target_linux_bionic_empty",
1535 copts = [
1536 "-I.",
1537 "-I$(BINDIR)/.",
1538 ],
1539 system_dynamic_deps = [],
1540)`},
1541 })
1542}
1543
1544func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1545 runCcLibraryTestCase(t, bp2buildTestCase{
1546 description: "cc_library system_shared_libs empty for bionic variant",
1547 moduleTypeUnderTest: "cc_library",
1548 moduleTypeUnderTestFactory: cc.LibraryFactory,
1549 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1550 blueprint: soongCcLibraryPreamble + `
1551cc_library {
1552 name: "target_bionic_empty",
1553 target: {
1554 bionic: {
1555 system_shared_libs: [],
1556 },
1557 },
1558}
1559`,
1560 expectedBazelTargets: []string{`cc_library(
1561 name = "target_bionic_empty",
1562 copts = [
1563 "-I.",
1564 "-I$(BINDIR)/.",
1565 ],
1566 system_dynamic_deps = [],
1567)`},
1568 })
1569}
1570
1571func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
1572 runCcLibraryTestCase(t, bp2buildTestCase{
1573 description: "cc_library system_shared_libs set for shared and root",
1574 moduleTypeUnderTest: "cc_library",
1575 moduleTypeUnderTestFactory: cc.LibraryFactory,
1576 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1577 blueprint: soongCcLibraryPreamble + `
1578cc_library {name: "libc"}
1579cc_library {name: "libm"}
1580
1581cc_library {
1582 name: "foo",
1583 system_shared_libs: ["libc"],
1584 shared: {
1585 system_shared_libs: ["libm"],
1586 },
1587}
1588`,
1589 expectedBazelTargets: []string{`cc_library(
1590 name = "foo",
1591 copts = [
1592 "-I.",
1593 "-I$(BINDIR)/.",
1594 ],
1595 shared = {
1596 "system_dynamic_deps": [":libm"],
1597 },
1598 system_dynamic_deps = [":libc"],
1599)`, `cc_library(
1600 name = "libc",
1601 copts = [
1602 "-I.",
1603 "-I$(BINDIR)/.",
1604 ],
1605)`, `cc_library(
1606 name = "libm",
1607 copts = [
1608 "-I.",
1609 "-I$(BINDIR)/.",
1610 ],
1611)`},
1612 })
1613}