blob: b3a10531eabfafd8995722b380e68db6001f5322 [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 Kammer8337ea42021-09-10 10:06:32 -040028 name: "linux_bionic_supported",
Jingwen Chen63930982021-03-24 10:04:33 -040029}
30
31toolchain_library {
Liz Kammer8337ea42021-09-10 10:06:32 -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 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400116 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400117}
118`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200119 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400120 name = "foo-lib",
Liz Kammer8337ea42021-09-10 10:06:32 -0400121 copts = ["-Wall"],
Liz Kammer5fad5012021-09-09 14:08:21 -0400122 export_includes = ["foo-dir"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400123 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000124 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
125 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
126 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
127 "//conditions:default": [],
128 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000129 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000130 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
131 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400132 "//conditions:default": [],
133 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000134 "//build/bazel/platforms/os:android": ["android.cpp"],
135 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
136 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400137 "//conditions:default": [],
Liz Kammer01a16e82021-07-16 16:33:47 -0400138 }) + select({
139 "//build/bazel/platforms/os:bionic": ["bionic.cpp"],
140 "//conditions:default": [],
Jingwen Chen63930982021-03-24 10:04:33 -0400141 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200142)`}})
143}
144
145func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
146 runCcLibraryTestCase(t, bp2buildTestCase{
147 description: "cc_library - trimmed example of //bionic/linker:ld-android",
148 moduleTypeUnderTest: "cc_library",
149 moduleTypeUnderTestFactory: cc.LibraryFactory,
150 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
151 filesystem: map[string]string{
152 "ld-android.cpp": "",
153 "linked_list.h": "",
154 "linker.h": "",
155 "linker_block_allocator.h": "",
156 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400157 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200158 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400159cc_library_headers { name: "libc_headers" }
160cc_library {
161 name: "fake-ld-android",
162 srcs: ["ld_android.cpp"],
163 cflags: [
164 "-Wall",
165 "-Wextra",
166 "-Wunused",
167 "-Werror",
168 ],
169 header_libs: ["libc_headers"],
170 ldflags: [
171 "-Wl,--exclude-libs=libgcc.a",
172 "-Wl,--exclude-libs=libgcc_stripped.a",
173 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
174 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
175 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
176 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
177 ],
178 arch: {
179 x86: {
180 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
181 },
182 x86_64: {
183 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
184 },
185 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400186 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400187}
188`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200189 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400190 name = "fake-ld-android",
191 copts = [
192 "-Wall",
193 "-Wextra",
194 "-Wunused",
195 "-Werror",
196 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400197 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400198 linkopts = [
199 "-Wl,--exclude-libs=libgcc.a",
200 "-Wl,--exclude-libs=libgcc_stripped.a",
201 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
202 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
203 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
204 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
205 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000206 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
207 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400208 "//conditions:default": [],
209 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000210 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400211)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200212 })
213}
214
215func TestCcLibraryExcludeSrcs(t *testing.T) {
216 runCcLibraryTestCase(t, bp2buildTestCase{
217 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
218 moduleTypeUnderTest: "cc_library",
219 moduleTypeUnderTestFactory: cc.LibraryFactory,
220 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
221 dir: "external",
222 filesystem: map[string]string{
223 "external/math/cosf.c": "",
224 "external/math/erf.c": "",
225 "external/math/erf_data.c": "",
226 "external/math/erff.c": "",
227 "external/math/erff_data.c": "",
228 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000229cc_library {
230 name: "fake-libarm-optimized-routines-math",
231 exclude_srcs: [
232 // Provided by:
233 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
234 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
235 "math/erf.c",
236 "math/erf_data.c",
237 "math/erff.c",
238 "math/erff_data.c",
239 ],
240 srcs: [
241 "math/*.c",
242 ],
243 // arch-specific settings
244 arch: {
245 arm64: {
246 cflags: [
247 "-DHAVE_FAST_FMA=1",
248 ],
249 },
250 },
251 bazel_module: { bp2build_available: true },
252}
253`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200254 },
255 blueprint: soongCcLibraryPreamble,
256 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000257 name = "fake-libarm-optimized-routines-math",
Liz Kammer35687bc2021-09-10 10:07:07 -0400258 copts = select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000259 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
260 "//conditions:default": [],
261 }),
Liz Kammer35687bc2021-09-10 10:07:07 -0400262 local_includes = ["."],
Chris Parsons990c4f42021-05-25 12:10:58 -0400263 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000264)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200265 })
266}
267
268func TestCcLibrarySharedStaticProps(t *testing.T) {
269 runCcLibraryTestCase(t, bp2buildTestCase{
270 description: "cc_library shared/static props",
271 moduleTypeUnderTest: "cc_library",
272 moduleTypeUnderTestFactory: cc.LibraryFactory,
273 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200274 filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400275 "both.cpp": "",
276 "sharedonly.cpp": "",
277 "staticonly.cpp": "",
278 },
279 blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000280cc_library {
281 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400282 srcs: ["both.cpp"],
283 cflags: ["bothflag"],
284 shared_libs: ["shared_dep_for_both"],
285 static_libs: ["static_dep_for_both"],
286 whole_static_libs: ["whole_static_lib_for_both"],
287 static: {
288 srcs: ["staticonly.cpp"],
289 cflags: ["staticflag"],
290 shared_libs: ["shared_dep_for_static"],
291 static_libs: ["static_dep_for_static"],
292 whole_static_libs: ["whole_static_lib_for_static"],
293 },
294 shared: {
295 srcs: ["sharedonly.cpp"],
296 cflags: ["sharedflag"],
297 shared_libs: ["shared_dep_for_shared"],
298 static_libs: ["static_dep_for_shared"],
299 whole_static_libs: ["whole_static_lib_for_shared"],
300 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400301 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000302}
303
Liz Kammer8337ea42021-09-10 10:06:32 -0400304cc_library_static {
305 name: "static_dep_for_shared",
306 bazel_module: { bp2build_available: false },
307}
Chris Parsons08648312021-05-06 16:23:19 -0400308
Liz Kammer8337ea42021-09-10 10:06:32 -0400309cc_library_static {
310 name: "static_dep_for_static",
311 bazel_module: { bp2build_available: false },
312}
Chris Parsons08648312021-05-06 16:23:19 -0400313
Liz Kammer8337ea42021-09-10 10:06:32 -0400314cc_library_static {
315 name: "static_dep_for_both",
316 bazel_module: { bp2build_available: false },
317}
Chris Parsons08648312021-05-06 16:23:19 -0400318
Liz Kammer8337ea42021-09-10 10:06:32 -0400319cc_library_static {
320 name: "whole_static_lib_for_shared",
321 bazel_module: { bp2build_available: false },
322}
Chris Parsons08648312021-05-06 16:23:19 -0400323
Liz Kammer8337ea42021-09-10 10:06:32 -0400324cc_library_static {
325 name: "whole_static_lib_for_static",
326 bazel_module: { bp2build_available: false },
327}
Chris Parsons08648312021-05-06 16:23:19 -0400328
Liz Kammer8337ea42021-09-10 10:06:32 -0400329cc_library_static {
330 name: "whole_static_lib_for_both",
331 bazel_module: { bp2build_available: false },
332}
Chris Parsons08648312021-05-06 16:23:19 -0400333
Liz Kammer8337ea42021-09-10 10:06:32 -0400334cc_library {
335 name: "shared_dep_for_shared",
336 bazel_module: { bp2build_available: false },
337}
Chris Parsons08648312021-05-06 16:23:19 -0400338
Liz Kammer8337ea42021-09-10 10:06:32 -0400339cc_library {
340 name: "shared_dep_for_static",
341 bazel_module: { bp2build_available: false },
342}
Chris Parsons08648312021-05-06 16:23:19 -0400343
Liz Kammer8337ea42021-09-10 10:06:32 -0400344cc_library {
345 name: "shared_dep_for_both",
346 bazel_module: { bp2build_available: false },
347}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000348`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200349 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000350 name = "a",
Liz Kammer8337ea42021-09-10 10:06:32 -0400351 copts = ["bothflag"],
Chris Parsons08648312021-05-06 16:23:19 -0400352 dynamic_deps = [":shared_dep_for_both"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400353 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000354 shared = {
355 "copts": ["sharedflag"],
356 "dynamic_deps": [":shared_dep_for_shared"],
357 "srcs": ["sharedonly.cpp"],
358 "static_deps": [":static_dep_for_shared"],
359 "whole_archive_deps": [":whole_static_lib_for_shared"],
360 },
Chris Parsons08648312021-05-06 16:23:19 -0400361 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000362 static = {
363 "copts": ["staticflag"],
364 "dynamic_deps": [":shared_dep_for_static"],
365 "srcs": ["staticonly.cpp"],
366 "static_deps": [":static_dep_for_static"],
367 "whole_archive_deps": [":whole_static_lib_for_static"],
368 },
Chris Parsons08648312021-05-06 16:23:19 -0400369 whole_archive_deps = [":whole_static_lib_for_both"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000370)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200371 })
372}
373
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400374func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
375 runCcLibraryTestCase(t, bp2buildTestCase{
376 moduleTypeUnderTest: "cc_library",
377 moduleTypeUnderTestFactory: cc.LibraryFactory,
378 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400379 dir: "foo/bar",
380 filesystem: map[string]string{
381 "foo/bar/Android.bp": `
382cc_library {
383 name: "a",
384 whole_static_libs: ["whole_static_lib_for_both"],
385 static: {
386 whole_static_libs: ["whole_static_lib_for_static"],
387 },
388 shared: {
389 whole_static_libs: ["whole_static_lib_for_shared"],
390 },
391 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400392 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400393}
394
395cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
396
397cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
398
399cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
400`,
401 },
402 blueprint: soongCcLibraryPreamble,
403 expectedBazelTargets: []string{`cc_library(
404 name = "a",
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000405 shared = {
406 "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
407 },
408 static = {
409 "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
410 },
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400411 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400412)`},
413 })
414}
415
Jingwen Chenbcf53042021-05-26 04:42:42 +0000416func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
417 runCcLibraryTestCase(t, bp2buildTestCase{
418 description: "cc_library shared/static props in arch",
419 moduleTypeUnderTest: "cc_library",
420 moduleTypeUnderTestFactory: cc.LibraryFactory,
421 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chenbcf53042021-05-26 04:42:42 +0000422 dir: "foo/bar",
423 filesystem: map[string]string{
424 "foo/bar/arm.cpp": "",
425 "foo/bar/x86.cpp": "",
426 "foo/bar/sharedonly.cpp": "",
427 "foo/bar/staticonly.cpp": "",
428 "foo/bar/Android.bp": `
429cc_library {
430 name: "a",
431 arch: {
432 arm: {
433 shared: {
434 srcs: ["arm_shared.cpp"],
435 cflags: ["-DARM_SHARED"],
436 static_libs: ["arm_static_dep_for_shared"],
437 whole_static_libs: ["arm_whole_static_dep_for_shared"],
438 shared_libs: ["arm_shared_dep_for_shared"],
439 },
440 },
441 x86: {
442 static: {
443 srcs: ["x86_static.cpp"],
444 cflags: ["-DX86_STATIC"],
445 static_libs: ["x86_dep_for_static"],
446 },
447 },
448 },
449 target: {
450 android: {
451 shared: {
452 srcs: ["android_shared.cpp"],
453 cflags: ["-DANDROID_SHARED"],
454 static_libs: ["android_dep_for_shared"],
455 },
456 },
457 android_arm: {
458 shared: {
459 cflags: ["-DANDROID_ARM_SHARED"],
460 },
461 },
462 },
463 srcs: ["both.cpp"],
464 cflags: ["bothflag"],
465 static_libs: ["static_dep_for_both"],
466 static: {
467 srcs: ["staticonly.cpp"],
468 cflags: ["staticflag"],
469 static_libs: ["static_dep_for_static"],
470 },
471 shared: {
472 srcs: ["sharedonly.cpp"],
473 cflags: ["sharedflag"],
474 static_libs: ["static_dep_for_shared"],
475 },
476 bazel_module: { bp2build_available: true },
477}
478
479cc_library_static { name: "static_dep_for_shared" }
480cc_library_static { name: "static_dep_for_static" }
481cc_library_static { name: "static_dep_for_both" }
482
483cc_library_static { name: "arm_static_dep_for_shared" }
484cc_library_static { name: "arm_whole_static_dep_for_shared" }
485cc_library_static { name: "arm_shared_dep_for_shared" }
486
487cc_library_static { name: "x86_dep_for_static" }
488
489cc_library_static { name: "android_dep_for_shared" }
490`,
491 },
492 blueprint: soongCcLibraryPreamble,
493 expectedBazelTargets: []string{`cc_library(
494 name = "a",
Liz Kammer35687bc2021-09-10 10:07:07 -0400495 copts = ["bothflag"],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000496 implementation_deps = [":static_dep_for_both"],
Liz Kammer35687bc2021-09-10 10:07:07 -0400497 local_includes = ["."],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000498 shared = {
499 "copts": ["sharedflag"] + select({
500 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
501 "//conditions:default": [],
502 }) + select({
503 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
504 "//conditions:default": [],
505 }) + select({
506 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
507 "//conditions:default": [],
508 }),
509 "dynamic_deps": select({
510 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
511 "//conditions:default": [],
512 }),
513 "srcs": ["sharedonly.cpp"] + select({
514 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
515 "//conditions:default": [],
516 }) + select({
517 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
518 "//conditions:default": [],
519 }),
520 "static_deps": [":static_dep_for_shared"] + select({
521 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
522 "//conditions:default": [],
523 }) + select({
524 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
525 "//conditions:default": [],
526 }),
527 "whole_archive_deps": select({
528 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
529 "//conditions:default": [],
530 }),
531 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000532 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000533 static = {
534 "copts": ["staticflag"] + select({
535 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
536 "//conditions:default": [],
537 }),
538 "srcs": ["staticonly.cpp"] + select({
539 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
540 "//conditions:default": [],
541 }),
542 "static_deps": [":static_dep_for_static"] + select({
543 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
544 "//conditions:default": [],
545 }),
546 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000547)`},
548 })
549}
550
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000551func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
552 runCcLibraryTestCase(t, bp2buildTestCase{
553 description: "cc_library shared/static props with c/cpp/s mixed sources",
554 moduleTypeUnderTest: "cc_library",
555 moduleTypeUnderTestFactory: cc.LibraryFactory,
556 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000557 dir: "foo/bar",
558 filesystem: map[string]string{
559 "foo/bar/both_source.cpp": "",
560 "foo/bar/both_source.cc": "",
561 "foo/bar/both_source.c": "",
562 "foo/bar/both_source.s": "",
563 "foo/bar/both_source.S": "",
564 "foo/bar/shared_source.cpp": "",
565 "foo/bar/shared_source.cc": "",
566 "foo/bar/shared_source.c": "",
567 "foo/bar/shared_source.s": "",
568 "foo/bar/shared_source.S": "",
569 "foo/bar/static_source.cpp": "",
570 "foo/bar/static_source.cc": "",
571 "foo/bar/static_source.c": "",
572 "foo/bar/static_source.s": "",
573 "foo/bar/static_source.S": "",
574 "foo/bar/Android.bp": `
575cc_library {
576 name: "a",
577 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400578 "both_source.cpp",
579 "both_source.cc",
580 "both_source.c",
581 "both_source.s",
582 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000583 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400584 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000585 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400586 srcs: [
587 "static_source.cpp",
588 "static_source.cc",
589 "static_source.c",
590 "static_source.s",
591 "static_source.S",
592 ":static_filegroup",
593 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000594 },
595 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400596 srcs: [
597 "shared_source.cpp",
598 "shared_source.cc",
599 "shared_source.c",
600 "shared_source.s",
601 "shared_source.S",
602 ":shared_filegroup",
603 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000604 },
605 bazel_module: { bp2build_available: true },
606}
607
608filegroup {
609 name: "both_filegroup",
610 srcs: [
611 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400612 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000613}
614
615filegroup {
616 name: "shared_filegroup",
617 srcs: [
618 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400619 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000620}
621
622filegroup {
623 name: "static_filegroup",
624 srcs: [
625 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400626 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000627}
628`,
629 },
630 blueprint: soongCcLibraryPreamble,
631 expectedBazelTargets: []string{`cc_library(
632 name = "a",
Liz Kammer35687bc2021-09-10 10:07:07 -0400633 local_includes = ["."],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000634 shared = {
635 "srcs": [
636 ":shared_filegroup_cpp_srcs",
637 "shared_source.cc",
638 "shared_source.cpp",
639 ],
640 "srcs_as": [
641 "shared_source.s",
642 "shared_source.S",
643 ":shared_filegroup_as_srcs",
644 ],
645 "srcs_c": [
646 "shared_source.c",
647 ":shared_filegroup_c_srcs",
648 ],
649 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000650 srcs = [
651 ":both_filegroup_cpp_srcs",
652 "both_source.cc",
653 "both_source.cpp",
654 ],
655 srcs_as = [
656 "both_source.s",
657 "both_source.S",
658 ":both_filegroup_as_srcs",
659 ],
660 srcs_c = [
661 "both_source.c",
662 ":both_filegroup_c_srcs",
663 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000664 static = {
665 "srcs": [
666 ":static_filegroup_cpp_srcs",
667 "static_source.cc",
668 "static_source.cpp",
669 ],
670 "srcs_as": [
671 "static_source.s",
672 "static_source.S",
673 ":static_filegroup_as_srcs",
674 ],
675 "srcs_c": [
676 "static_source.c",
677 ":static_filegroup_c_srcs",
678 ],
679 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000680)`},
681 })
682}
683
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200684func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
685 runCcLibraryTestCase(t, bp2buildTestCase{
686 description: "cc_library non-configured version script",
687 moduleTypeUnderTest: "cc_library",
688 moduleTypeUnderTestFactory: cc.LibraryFactory,
689 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200690 dir: "foo/bar",
691 filesystem: map[string]string{
692 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200693cc_library {
694 name: "a",
695 srcs: ["a.cpp"],
696 version_script: "v.map",
697 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400698 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200699}
700`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200701 },
702 blueprint: soongCcLibraryPreamble,
703 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200704 name = "a",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200705 srcs = ["a.cpp"],
706 version_script = "v.map",
707)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200708 })
709}
710
711func TestCcLibraryConfiguredVersionScript(t *testing.T) {
712 runCcLibraryTestCase(t, bp2buildTestCase{
713 description: "cc_library configured version script",
714 moduleTypeUnderTest: "cc_library",
715 moduleTypeUnderTestFactory: cc.LibraryFactory,
716 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200717 dir: "foo/bar",
718 filesystem: map[string]string{
719 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400720cc_library {
721 name: "a",
722 srcs: ["a.cpp"],
723 arch: {
724 arm: {
725 version_script: "arm.map",
726 },
727 arm64: {
728 version_script: "arm64.map",
729 },
730 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200731
Liz Kammer8337ea42021-09-10 10:06:32 -0400732 bazel_module: { bp2build_available: true },
733 include_build_directory: false,
734}
Liz Kammerd366c902021-06-03 13:43:01 -0400735 `,
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",
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200740 srcs = ["a.cpp"],
741 version_script = select({
742 "//build/bazel/platforms/arch:arm": "arm.map",
743 "//build/bazel/platforms/arch:arm64": "arm64.map",
744 "//conditions:default": None,
745 }),
746)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200747 })
748}
749
750func TestCcLibrarySharedLibs(t *testing.T) {
751 runCcLibraryTestCase(t, bp2buildTestCase{
752 description: "cc_library shared_libs",
753 moduleTypeUnderTest: "cc_library",
754 moduleTypeUnderTestFactory: cc.LibraryFactory,
755 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400756 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400757cc_library {
758 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -0400759 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400760}
761
762cc_library {
763 name: "a",
764 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -0400765 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400766}
767`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200768 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400769 name = "a",
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400770 dynamic_deps = [":mylib"],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400771)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200772 })
773}
774
775func TestCcLibraryPackRelocations(t *testing.T) {
776 runCcLibraryTestCase(t, bp2buildTestCase{
777 description: "cc_library pack_relocations test",
778 moduleTypeUnderTest: "cc_library",
779 moduleTypeUnderTestFactory: cc.LibraryFactory,
780 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400781 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400782cc_library {
783 name: "a",
784 srcs: ["a.cpp"],
785 pack_relocations: false,
Liz Kammer8337ea42021-09-10 10:06:32 -0400786 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400787}
788
789cc_library {
790 name: "b",
791 srcs: ["b.cpp"],
792 arch: {
793 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400794 pack_relocations: false,
795 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400796 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400797 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400798}
799
800cc_library {
801 name: "c",
802 srcs: ["c.cpp"],
803 target: {
804 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400805 pack_relocations: false,
806 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400807 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400808 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400809}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200810 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400811 name = "a",
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400812 linkopts = ["-Wl,--pack-dyn-relocs=none"],
813 srcs = ["a.cpp"],
814)`, `cc_library(
815 name = "b",
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400816 linkopts = select({
817 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
818 "//conditions:default": [],
819 }),
820 srcs = ["b.cpp"],
821)`, `cc_library(
822 name = "c",
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400823 linkopts = select({
824 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
825 "//conditions:default": [],
826 }),
827 srcs = ["c.cpp"],
828)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200829 })
830}
831
832func TestCcLibrarySpacesInCopts(t *testing.T) {
833 runCcLibraryTestCase(t, bp2buildTestCase{
834 description: "cc_library spaces in copts",
835 moduleTypeUnderTest: "cc_library",
836 moduleTypeUnderTestFactory: cc.LibraryFactory,
837 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400838 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000839cc_library {
840 name: "a",
841 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -0400842 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +0000843}
844`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200845 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000846 name = "a",
847 copts = [
848 "-include",
849 "header.h",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000850 ],
851)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200852 })
853}
854
855func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
856 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400857 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200858 moduleTypeUnderTest: "cc_library",
859 moduleTypeUnderTestFactory: cc.LibraryFactory,
860 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400861 blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000862 name: "a",
863 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400864 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000865 cppflags: [
866 "-fsigned-char",
867 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -0400868 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000869 arch: {
870 arm64: {
871 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400872 },
Liz Kammerd366c902021-06-03 13:43:01 -0400873 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000874 target: {
875 android: {
876 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -0400877 },
Liz Kammerd366c902021-06-03 13:43:01 -0400878 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400879 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000880}
881`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200882 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000883 name = "a",
Liz Kammer8337ea42021-09-10 10:06:32 -0400884 copts = ["-Wall"],
Chris Parsons990c4f42021-05-25 12:10:58 -0400885 cppflags = [
886 "-fsigned-char",
887 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000888 ] + select({
889 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
890 "//conditions:default": [],
891 }) + select({
892 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
893 "//conditions:default": [],
894 }),
895 srcs = ["a.cpp"],
896)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200897 })
Jingwen Chen63930982021-03-24 10:04:33 -0400898}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400899
900func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
901 runCcLibraryTestCase(t, bp2buildTestCase{
902 description: "cc_library GetTargetProperties on a LabelAttribute",
903 moduleTypeUnderTest: "cc_library",
904 moduleTypeUnderTestFactory: cc.LibraryFactory,
905 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400906 blueprint: soongCcLibraryPreamble + `
907cc_library {
908 name: "a",
909 srcs: ["a.cpp"],
910 target: {
911 android_arm: {
912 version_script: "android_arm.map",
913 },
914 linux_bionic_arm64: {
915 version_script: "linux_bionic_arm64.map",
916 },
917 },
918 include_build_directory: false,
919}
Liz Kammerd366c902021-06-03 13:43:01 -0400920 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400921 expectedBazelTargets: []string{`cc_library(
922 name = "a",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400923 srcs = ["a.cpp"],
924 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400925 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
926 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400927 "//conditions:default": None,
928 }),
929)`},
930 })
931}
Liz Kammer47535c52021-06-02 16:02:22 -0400932
933func TestCcLibraryExcludeLibs(t *testing.T) {
934 runCcLibraryTestCase(t, bp2buildTestCase{
935 moduleTypeUnderTest: "cc_library",
936 moduleTypeUnderTestFactory: cc.LibraryFactory,
937 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer47535c52021-06-02 16:02:22 -0400938 filesystem: map[string]string{},
939 blueprint: soongCcLibraryStaticPreamble + `
940cc_library {
941 name: "foo_static",
942 srcs: ["common.c"],
943 whole_static_libs: [
944 "arm_whole_static_lib_excludes",
945 "malloc_not_svelte_whole_static_lib_excludes"
946 ],
947 static_libs: [
948 "arm_static_lib_excludes",
949 "malloc_not_svelte_static_lib_excludes"
950 ],
951 shared_libs: [
952 "arm_shared_lib_excludes",
953 ],
954 arch: {
955 arm: {
956 exclude_shared_libs: [
957 "arm_shared_lib_excludes",
958 ],
959 exclude_static_libs: [
960 "arm_static_lib_excludes",
961 "arm_whole_static_lib_excludes",
962 ],
963 },
964 },
965 product_variables: {
966 malloc_not_svelte: {
967 shared_libs: ["malloc_not_svelte_shared_lib"],
968 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
969 exclude_static_libs: [
970 "malloc_not_svelte_static_lib_excludes",
971 "malloc_not_svelte_whole_static_lib_excludes",
972 ],
973 },
974 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400975 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -0400976}
977
978cc_library {
979 name: "arm_whole_static_lib_excludes",
980 bazel_module: { bp2build_available: false },
981}
982
983cc_library {
984 name: "malloc_not_svelte_whole_static_lib",
985 bazel_module: { bp2build_available: false },
986}
987
988cc_library {
989 name: "malloc_not_svelte_whole_static_lib_excludes",
990 bazel_module: { bp2build_available: false },
991}
992
993cc_library {
994 name: "arm_static_lib_excludes",
995 bazel_module: { bp2build_available: false },
996}
997
998cc_library {
999 name: "malloc_not_svelte_static_lib_excludes",
1000 bazel_module: { bp2build_available: false },
1001}
1002
1003cc_library {
1004 name: "arm_shared_lib_excludes",
1005 bazel_module: { bp2build_available: false },
1006}
1007
1008cc_library {
1009 name: "malloc_not_svelte_shared_lib",
1010 bazel_module: { bp2build_available: false },
1011}
1012`,
1013 expectedBazelTargets: []string{
1014 `cc_library(
1015 name = "foo_static",
Liz Kammer47535c52021-06-02 16:02:22 -04001016 dynamic_deps = select({
1017 "//build/bazel/platforms/arch:arm": [],
1018 "//conditions:default": [":arm_shared_lib_excludes"],
1019 }) + select({
1020 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1021 "//conditions:default": [],
1022 }),
1023 implementation_deps = select({
1024 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001025 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001026 }) + select({
1027 "//build/bazel/product_variables:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001028 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001029 }),
1030 srcs_c = ["common.c"],
1031 whole_archive_deps = select({
1032 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001033 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001034 }) + select({
Chris Parsons953b3562021-09-20 15:14:39 -04001035 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
1036 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001037 }),
1038)`,
1039 },
1040 })
1041}
Liz Kammerd366c902021-06-03 13:43:01 -04001042
1043func TestCCLibraryNoCrtTrue(t *testing.T) {
1044 runCcLibraryTestCase(t, bp2buildTestCase{
1045 description: "cc_library - simple example",
1046 moduleTypeUnderTest: "cc_library",
1047 moduleTypeUnderTestFactory: cc.LibraryFactory,
1048 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1049 filesystem: map[string]string{
1050 "impl.cpp": "",
1051 },
1052 blueprint: soongCcLibraryPreamble + `
1053cc_library_headers { name: "some-headers" }
1054cc_library {
1055 name: "foo-lib",
1056 srcs: ["impl.cpp"],
1057 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001058 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001059}
1060`,
1061 expectedBazelTargets: []string{`cc_library(
1062 name = "foo-lib",
Liz Kammerd366c902021-06-03 13:43:01 -04001063 srcs = ["impl.cpp"],
1064 use_libcrt = False,
1065)`}})
1066}
1067
1068func TestCCLibraryNoCrtFalse(t *testing.T) {
1069 runCcLibraryTestCase(t, bp2buildTestCase{
1070 moduleTypeUnderTest: "cc_library",
1071 moduleTypeUnderTestFactory: cc.LibraryFactory,
1072 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1073 filesystem: map[string]string{
1074 "impl.cpp": "",
1075 },
1076 blueprint: soongCcLibraryPreamble + `
1077cc_library_headers { name: "some-headers" }
1078cc_library {
1079 name: "foo-lib",
1080 srcs: ["impl.cpp"],
1081 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001082 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001083}
1084`,
1085 expectedBazelTargets: []string{`cc_library(
1086 name = "foo-lib",
Liz Kammerd366c902021-06-03 13:43:01 -04001087 srcs = ["impl.cpp"],
1088 use_libcrt = True,
1089)`}})
1090}
1091
1092func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1093 runCcLibraryTestCase(t, bp2buildTestCase{
1094 moduleTypeUnderTest: "cc_library",
1095 moduleTypeUnderTestFactory: cc.LibraryFactory,
1096 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1097 filesystem: map[string]string{
1098 "impl.cpp": "",
1099 },
1100 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001101cc_library {
1102 name: "foo-lib",
1103 srcs: ["impl.cpp"],
1104 arch: {
1105 arm: {
1106 no_libcrt: true,
1107 },
1108 x86: {
1109 no_libcrt: true,
1110 },
1111 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001112 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001113}
1114`,
1115 expectedBazelTargets: []string{`cc_library(
1116 name = "foo-lib",
Liz Kammerd366c902021-06-03 13:43:01 -04001117 srcs = ["impl.cpp"],
1118 use_libcrt = select({
1119 "//build/bazel/platforms/arch:arm": False,
1120 "//build/bazel/platforms/arch:x86": False,
1121 "//conditions:default": None,
1122 }),
1123)`}})
1124}
1125
1126func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1127 runCcLibraryTestCase(t, bp2buildTestCase{
1128 moduleTypeUnderTest: "cc_library",
1129 moduleTypeUnderTestFactory: cc.LibraryFactory,
1130 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1131 filesystem: map[string]string{
1132 "impl.cpp": "",
1133 },
1134 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001135cc_library {
1136 name: "foo-lib",
1137 srcs: ["impl.cpp"],
1138 no_libcrt: false,
1139 arch: {
1140 arm: {
1141 no_libcrt: true,
1142 },
1143 x86: {
1144 no_libcrt: true,
1145 },
1146 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001147 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001148}
1149`,
1150 expectedBazelTargets: []string{`cc_library(
1151 name = "foo-lib",
Liz Kammerd366c902021-06-03 13:43:01 -04001152 srcs = ["impl.cpp"],
1153 use_libcrt = select({
1154 "//build/bazel/platforms/arch:arm": False,
1155 "//build/bazel/platforms/arch:x86": False,
1156 "//conditions:default": True,
1157 }),
1158)`}})
1159}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001160
1161func TestCcLibraryStrip(t *testing.T) {
1162 runCcLibraryTestCase(t, bp2buildTestCase{
1163 description: "cc_library strip args",
1164 moduleTypeUnderTest: "cc_library",
1165 moduleTypeUnderTestFactory: cc.LibraryFactory,
1166 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001167 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001168cc_library {
1169 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001170 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001171}
1172cc_library {
1173 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001174 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001175 keep_symbols: true,
1176 },
1177 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001178}
1179cc_library {
1180 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001181 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001182 keep_symbols_and_debug_frame: true,
1183 },
1184 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001185}
1186cc_library {
1187 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001188 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001189 none: true,
1190 },
1191 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001192}
1193cc_library {
1194 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001195 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001196 keep_symbols_list: ["symbol"],
1197 },
1198 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001199}
1200cc_library {
1201 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001202 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001203 all: true,
1204 },
1205 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001206}
1207`,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001208 expectedBazelTargets: []string{`cc_library(
1209 name = "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001210 strip = {
1211 "all": True,
1212 },
1213)`, `cc_library(
1214 name = "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001215 strip = {
1216 "keep_symbols": True,
1217 },
1218)`, `cc_library(
1219 name = "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001220 strip = {
1221 "keep_symbols_and_debug_frame": True,
1222 },
1223)`, `cc_library(
1224 name = "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001225 strip = {
1226 "keep_symbols_list": ["symbol"],
1227 },
1228)`, `cc_library(
1229 name = "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001230 strip = {
1231 "none": True,
1232 },
1233)`, `cc_library(
1234 name = "nothing",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001235)`},
1236 })
1237}
1238
1239func TestCcLibraryStripWithArch(t *testing.T) {
1240 runCcLibraryTestCase(t, bp2buildTestCase{
1241 description: "cc_library strip args",
1242 moduleTypeUnderTest: "cc_library",
1243 moduleTypeUnderTestFactory: cc.LibraryFactory,
1244 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001245 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001246cc_library {
1247 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001248 target: {
1249 darwin: {
1250 strip: {
1251 keep_symbols_list: ["foo", "bar"]
1252 }
1253 },
1254 },
1255 arch: {
1256 arm: {
1257 strip: {
1258 keep_symbols_and_debug_frame: true,
1259 },
1260 },
1261 arm64: {
1262 strip: {
1263 keep_symbols: true,
1264 },
1265 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001266 },
1267 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001268}
1269`,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001270 expectedBazelTargets: []string{`cc_library(
1271 name = "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001272 strip = {
1273 "keep_symbols": select({
1274 "//build/bazel/platforms/arch:arm64": True,
1275 "//conditions:default": None,
1276 }),
1277 "keep_symbols_and_debug_frame": select({
1278 "//build/bazel/platforms/arch:arm": True,
1279 "//conditions:default": None,
1280 }),
1281 "keep_symbols_list": select({
1282 "//build/bazel/platforms/os:darwin": [
1283 "foo",
1284 "bar",
1285 ],
1286 "//conditions:default": [],
1287 }),
1288 },
1289)`},
1290 })
1291}
Chris Parsons51f8c392021-08-03 21:01:05 -04001292
1293func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1294 runCcLibraryTestCase(t, bp2buildTestCase{
1295 description: "cc_library system_shared_libs empty at root",
1296 moduleTypeUnderTest: "cc_library",
1297 moduleTypeUnderTestFactory: cc.LibraryFactory,
1298 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1299 blueprint: soongCcLibraryPreamble + `
1300cc_library {
1301 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001302 system_shared_libs: [],
1303 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001304}
1305`,
1306 expectedBazelTargets: []string{`cc_library(
1307 name = "root_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001308 system_dynamic_deps = [],
1309)`},
1310 })
1311}
1312
1313func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1314 runCcLibraryTestCase(t, bp2buildTestCase{
1315 description: "cc_library system_shared_libs empty for static variant",
1316 moduleTypeUnderTest: "cc_library",
1317 moduleTypeUnderTestFactory: cc.LibraryFactory,
1318 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1319 blueprint: soongCcLibraryPreamble + `
1320cc_library {
1321 name: "static_empty",
1322 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001323 system_shared_libs: [],
1324 },
1325 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001326}
1327`,
1328 expectedBazelTargets: []string{`cc_library(
1329 name = "static_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001330 static = {
1331 "system_dynamic_deps": [],
1332 },
1333)`},
1334 })
1335}
1336
1337func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
1338 runCcLibraryTestCase(t, bp2buildTestCase{
1339 description: "cc_library system_shared_libs empty for shared variant",
1340 moduleTypeUnderTest: "cc_library",
1341 moduleTypeUnderTestFactory: cc.LibraryFactory,
1342 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1343 blueprint: soongCcLibraryPreamble + `
1344cc_library {
1345 name: "shared_empty",
1346 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001347 system_shared_libs: [],
1348 },
1349 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001350}
1351`,
1352 expectedBazelTargets: []string{`cc_library(
1353 name = "shared_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001354 shared = {
1355 "system_dynamic_deps": [],
1356 },
1357)`},
1358 })
1359}
1360
1361func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
1362 runCcLibraryTestCase(t, bp2buildTestCase{
1363 description: "cc_library system_shared_libs empty for shared, bionic variant",
1364 moduleTypeUnderTest: "cc_library",
1365 moduleTypeUnderTestFactory: cc.LibraryFactory,
1366 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1367 blueprint: soongCcLibraryPreamble + `
1368cc_library {
1369 name: "shared_empty",
1370 target: {
1371 bionic: {
1372 shared: {
1373 system_shared_libs: [],
1374 }
1375 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001376 },
1377 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001378}
1379`,
1380 expectedBazelTargets: []string{`cc_library(
1381 name = "shared_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001382 shared = {
1383 "system_dynamic_deps": [],
1384 },
1385)`},
1386 })
1387}
1388
1389func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1390 // Note that this behavior is technically incorrect (it's a simplification).
1391 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1392 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1393 // b/195791252 tracks the fix.
1394 runCcLibraryTestCase(t, bp2buildTestCase{
1395 description: "cc_library system_shared_libs empty for linux_bionic variant",
1396 moduleTypeUnderTest: "cc_library",
1397 moduleTypeUnderTestFactory: cc.LibraryFactory,
1398 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1399 blueprint: soongCcLibraryPreamble + `
1400cc_library {
1401 name: "target_linux_bionic_empty",
1402 target: {
1403 linux_bionic: {
1404 system_shared_libs: [],
1405 },
1406 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001407 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001408}
1409`,
1410 expectedBazelTargets: []string{`cc_library(
1411 name = "target_linux_bionic_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001412 system_dynamic_deps = [],
1413)`},
1414 })
1415}
1416
1417func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1418 runCcLibraryTestCase(t, bp2buildTestCase{
1419 description: "cc_library system_shared_libs empty for bionic variant",
1420 moduleTypeUnderTest: "cc_library",
1421 moduleTypeUnderTestFactory: cc.LibraryFactory,
1422 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1423 blueprint: soongCcLibraryPreamble + `
1424cc_library {
1425 name: "target_bionic_empty",
1426 target: {
1427 bionic: {
1428 system_shared_libs: [],
1429 },
1430 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001431 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001432}
1433`,
1434 expectedBazelTargets: []string{`cc_library(
1435 name = "target_bionic_empty",
Chris Parsons51f8c392021-08-03 21:01:05 -04001436 system_dynamic_deps = [],
1437)`},
1438 })
1439}
1440
1441func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
1442 runCcLibraryTestCase(t, bp2buildTestCase{
1443 description: "cc_library system_shared_libs set for shared and root",
1444 moduleTypeUnderTest: "cc_library",
1445 moduleTypeUnderTestFactory: cc.LibraryFactory,
1446 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1447 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001448cc_library {
1449 name: "libc",
1450 bazel_module: { bp2build_available: false },
1451}
1452cc_library {
1453 name: "libm",
1454 bazel_module: { bp2build_available: false },
1455}
Chris Parsons51f8c392021-08-03 21:01:05 -04001456
1457cc_library {
1458 name: "foo",
1459 system_shared_libs: ["libc"],
1460 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001461 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001462 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001463 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001464}
1465`,
1466 expectedBazelTargets: []string{`cc_library(
1467 name = "foo",
Chris Parsons51f8c392021-08-03 21:01:05 -04001468 shared = {
1469 "system_dynamic_deps": [":libm"],
1470 },
1471 system_dynamic_deps = [":libc"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001472)`},
1473 })
1474}