blob: 2f912a99d4ca2600385fe404ee790810ab75a4d9 [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 (
Jingwen Chen6ada5892021-09-17 11:38:09 +000018 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000019 "testing"
20
Jingwen Chen63930982021-03-24 10:04:33 -040021 "android/soong/android"
22 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040023)
24
25const (
26 // See cc/testing.go for more context
27 soongCcLibraryPreamble = `
28cc_defaults {
Liz Kammer8337ea42021-09-10 10:06:32 -040029 name: "linux_bionic_supported",
Jingwen Chen63930982021-03-24 10:04:33 -040030}`
Liz Kammer12615db2021-09-28 09:19:17 -040031
32 soongCcProtoLibraries = `
33cc_library {
34 name: "libprotobuf-cpp-lite",
35 bazel_module: { bp2build_available: false },
36}
37
38cc_library {
39 name: "libprotobuf-cpp-full",
40 bazel_module: { bp2build_available: false },
41}`
42
43 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040044)
45
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020046func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040047 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020048 runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
49}
50
51func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
52 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000053 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020054 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040055 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020056 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
57}
58
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059func TestCcLibrarySimple(t *testing.T) {
60 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040061 description: "cc_library - simple example",
62 moduleTypeUnderTest: "cc_library",
63 moduleTypeUnderTestFactory: cc.LibraryFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 filesystem: map[string]string{
65 "android.cpp": "",
Liz Kammer01a16e82021-07-16 16:33:47 -040066 "bionic.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020067 "darwin.cpp": "",
68 // Refer to cc.headerExts for the supported header extensions in Soong.
69 "header.h": "",
70 "header.hh": "",
71 "header.hpp": "",
72 "header.hxx": "",
73 "header.h++": "",
74 "header.inl": "",
75 "header.inc": "",
76 "header.ipp": "",
77 "header.h.generic": "",
78 "impl.cpp": "",
79 "linux.cpp": "",
80 "x86.cpp": "",
81 "x86_64.cpp": "",
82 "foo-dir/a.h": "",
83 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050084 blueprint: soongCcLibraryPreamble +
85 simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -040086cc_library {
87 name: "foo-lib",
88 srcs: ["impl.cpp"],
89 cflags: ["-Wall"],
90 header_libs: ["some-headers"],
91 export_include_dirs: ["foo-dir"],
92 ldflags: ["-Wl,--exclude-libs=bar.a"],
93 arch: {
94 x86: {
95 ldflags: ["-Wl,--exclude-libs=baz.a"],
96 srcs: ["x86.cpp"],
97 },
98 x86_64: {
99 ldflags: ["-Wl,--exclude-libs=qux.a"],
100 srcs: ["x86_64.cpp"],
101 },
102 },
103 target: {
104 android: {
105 srcs: ["android.cpp"],
106 },
107 linux_glibc: {
108 srcs: ["linux.cpp"],
109 },
110 darwin: {
111 srcs: ["darwin.cpp"],
112 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400113 bionic: {
114 srcs: ["bionic.cpp"]
115 },
Jingwen Chen63930982021-03-24 10:04:33 -0400116 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400117 include_build_directory: false,
Yu Liufc603162022-03-01 15:44:08 -0800118 sdk_version: "current",
119 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700120 use_version_lib: true,
Jingwen Chen63930982021-03-24 10:04:33 -0400121}
122`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500123 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
124 "copts": `["-Wall"]`,
125 "export_includes": `["foo-dir"]`,
126 "implementation_deps": `[":some-headers"]`,
127 "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000128 "//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": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500131 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500132 "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({
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400137 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400138 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -0400139 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400140 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000141 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
142 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400143 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Liz Kammer01a16e82021-07-16 16:33:47 -0400144 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500145 })`,
Yu Liua79c9462022-03-22 16:35:22 -0700146 "sdk_version": `"current"`,
147 "min_sdk_version": `"29"`,
148 "use_version_lib": `True`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500149 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500150 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200151}
152
153func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
154 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400155 description: "cc_library - trimmed example of //bionic/linker:ld-android",
156 moduleTypeUnderTest: "cc_library",
157 moduleTypeUnderTestFactory: cc.LibraryFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200158 filesystem: map[string]string{
159 "ld-android.cpp": "",
160 "linked_list.h": "",
161 "linker.h": "",
162 "linker_block_allocator.h": "",
163 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400164 },
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400165 blueprint: soongCcLibraryPreamble +
166 simpleModuleDoNotConvertBp2build("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400167cc_library {
168 name: "fake-ld-android",
169 srcs: ["ld_android.cpp"],
170 cflags: [
171 "-Wall",
172 "-Wextra",
173 "-Wunused",
174 "-Werror",
175 ],
176 header_libs: ["libc_headers"],
177 ldflags: [
178 "-Wl,--exclude-libs=libgcc.a",
179 "-Wl,--exclude-libs=libgcc_stripped.a",
180 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
181 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
182 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
183 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
184 ],
185 arch: {
186 x86: {
187 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
188 },
189 x86_64: {
190 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
191 },
192 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400193 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400194}
195`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500196 expectedBazelTargets: makeCcLibraryTargets("fake-ld-android", attrNameToString{
197 "srcs": `["ld_android.cpp"]`,
198 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400199 "-Wall",
200 "-Wextra",
201 "-Wunused",
202 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500203 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500204 "implementation_deps": `[":libc_headers"]`,
205 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400206 "-Wl,--exclude-libs=libgcc.a",
207 "-Wl,--exclude-libs=libgcc_stripped.a",
208 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
209 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
210 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
211 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
212 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000213 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
214 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400215 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500216 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500217 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200218 })
219}
220
221func TestCcLibraryExcludeSrcs(t *testing.T) {
222 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400223 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
224 moduleTypeUnderTest: "cc_library",
225 moduleTypeUnderTestFactory: cc.LibraryFactory,
226 dir: "external",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200227 filesystem: map[string]string{
228 "external/math/cosf.c": "",
229 "external/math/erf.c": "",
230 "external/math/erf_data.c": "",
231 "external/math/erff.c": "",
232 "external/math/erff_data.c": "",
233 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000234cc_library {
235 name: "fake-libarm-optimized-routines-math",
236 exclude_srcs: [
237 // Provided by:
238 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
239 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
240 "math/erf.c",
241 "math/erf_data.c",
242 "math/erff.c",
243 "math/erff_data.c",
244 ],
245 srcs: [
246 "math/*.c",
247 ],
248 // arch-specific settings
249 arch: {
250 arm64: {
251 cflags: [
252 "-DHAVE_FAST_FMA=1",
253 ],
254 },
255 },
256 bazel_module: { bp2build_available: true },
257}
258`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200259 },
260 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500261 expectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", attrNameToString{
262 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000263 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
264 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500265 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500266 "local_includes": `["."]`,
267 "srcs_c": `["math/cosf.c"]`,
268 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200269 })
270}
271
272func TestCcLibrarySharedStaticProps(t *testing.T) {
273 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400274 description: "cc_library shared/static props",
275 moduleTypeUnderTest: "cc_library",
276 moduleTypeUnderTestFactory: cc.LibraryFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200277 filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400278 "both.cpp": "",
279 "sharedonly.cpp": "",
280 "staticonly.cpp": "",
281 },
282 blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000283cc_library {
284 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400285 srcs: ["both.cpp"],
286 cflags: ["bothflag"],
287 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400288 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
289 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400290 static: {
291 srcs: ["staticonly.cpp"],
292 cflags: ["staticflag"],
293 shared_libs: ["shared_dep_for_static"],
294 static_libs: ["static_dep_for_static"],
295 whole_static_libs: ["whole_static_lib_for_static"],
296 },
297 shared: {
298 srcs: ["sharedonly.cpp"],
299 cflags: ["sharedflag"],
300 shared_libs: ["shared_dep_for_shared"],
301 static_libs: ["static_dep_for_shared"],
302 whole_static_libs: ["whole_static_lib_for_shared"],
303 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400304 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000305}
306
Liz Kammer8337ea42021-09-10 10:06:32 -0400307cc_library_static {
308 name: "static_dep_for_shared",
309 bazel_module: { bp2build_available: false },
310}
Chris Parsons08648312021-05-06 16:23:19 -0400311
Liz Kammer8337ea42021-09-10 10:06:32 -0400312cc_library_static {
313 name: "static_dep_for_static",
314 bazel_module: { bp2build_available: false },
315}
Chris Parsons08648312021-05-06 16:23:19 -0400316
Liz Kammer8337ea42021-09-10 10:06:32 -0400317cc_library_static {
318 name: "static_dep_for_both",
319 bazel_module: { bp2build_available: false },
320}
Chris Parsons08648312021-05-06 16:23:19 -0400321
Liz Kammer8337ea42021-09-10 10:06:32 -0400322cc_library_static {
323 name: "whole_static_lib_for_shared",
324 bazel_module: { bp2build_available: false },
325}
Chris Parsons08648312021-05-06 16:23:19 -0400326
Liz Kammer8337ea42021-09-10 10:06:32 -0400327cc_library_static {
328 name: "whole_static_lib_for_static",
329 bazel_module: { bp2build_available: false },
330}
Chris Parsons08648312021-05-06 16:23:19 -0400331
Liz Kammer8337ea42021-09-10 10:06:32 -0400332cc_library_static {
333 name: "whole_static_lib_for_both",
334 bazel_module: { bp2build_available: false },
335}
Chris Parsons08648312021-05-06 16:23:19 -0400336
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400337cc_library_static {
338 name: "whole_and_static_lib_for_both",
339 bazel_module: { bp2build_available: false },
340}
341
Liz Kammer8337ea42021-09-10 10:06:32 -0400342cc_library {
343 name: "shared_dep_for_shared",
344 bazel_module: { bp2build_available: false },
345}
Chris Parsons08648312021-05-06 16:23:19 -0400346
Liz Kammer8337ea42021-09-10 10:06:32 -0400347cc_library {
348 name: "shared_dep_for_static",
349 bazel_module: { bp2build_available: false },
350}
Chris Parsons08648312021-05-06 16:23:19 -0400351
Liz Kammer8337ea42021-09-10 10:06:32 -0400352cc_library {
353 name: "shared_dep_for_both",
354 bazel_module: { bp2build_available: false },
355}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000356`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500357 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500358 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
359 "copts": `[
360 "bothflag",
361 "staticflag",
362 ]`,
363 "implementation_deps": `[
364 ":static_dep_for_both",
365 ":static_dep_for_static",
366 ]`,
367 "implementation_dynamic_deps": `[
368 ":shared_dep_for_both",
369 ":shared_dep_for_static",
370 ]`,
371 "srcs": `[
372 "both.cpp",
373 "staticonly.cpp",
374 ]`,
375 "whole_archive_deps": `[
376 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400377 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500378 ":whole_static_lib_for_static",
379 ]`}),
380 makeBazelTarget("cc_library_shared", "a", attrNameToString{
381 "copts": `[
382 "bothflag",
383 "sharedflag",
384 ]`,
385 "implementation_deps": `[
386 ":static_dep_for_both",
387 ":static_dep_for_shared",
388 ]`,
389 "implementation_dynamic_deps": `[
390 ":shared_dep_for_both",
391 ":shared_dep_for_shared",
392 ]`,
393 "srcs": `[
394 "both.cpp",
395 "sharedonly.cpp",
396 ]`,
397 "whole_archive_deps": `[
398 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400399 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500400 ":whole_static_lib_for_shared",
401 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500402 }),
403 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200404 })
405}
406
Liz Kammer7a210ac2021-09-22 15:52:58 -0400407func TestCcLibraryDeps(t *testing.T) {
408 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400409 description: "cc_library shared/static props",
410 moduleTypeUnderTest: "cc_library",
411 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer7a210ac2021-09-22 15:52:58 -0400412 filesystem: map[string]string{
413 "both.cpp": "",
414 "sharedonly.cpp": "",
415 "staticonly.cpp": "",
416 },
417 blueprint: soongCcLibraryPreamble + `
418cc_library {
419 name: "a",
420 srcs: ["both.cpp"],
421 cflags: ["bothflag"],
422 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
423 export_shared_lib_headers: ["shared_dep_for_both"],
424 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
425 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
426 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
427 static: {
428 srcs: ["staticonly.cpp"],
429 cflags: ["staticflag"],
430 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
431 export_shared_lib_headers: ["shared_dep_for_static"],
432 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
433 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
434 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
435 },
436 shared: {
437 srcs: ["sharedonly.cpp"],
438 cflags: ["sharedflag"],
439 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
440 export_shared_lib_headers: ["shared_dep_for_shared"],
441 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
442 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
443 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
444 },
445 include_build_directory: false,
446}
447` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
448 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
449 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
450 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
451 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
452 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
453 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
454 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
455 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
456 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
457 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
458 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
459 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
460 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
461 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
462 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
463 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
464 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500465 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500466 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
467 "copts": `[
468 "bothflag",
469 "staticflag",
470 ]`,
471 "deps": `[
472 ":static_dep_for_both",
473 ":static_dep_for_static",
474 ]`,
475 "dynamic_deps": `[
476 ":shared_dep_for_both",
477 ":shared_dep_for_static",
478 ]`,
479 "implementation_deps": `[
480 ":implementation_static_dep_for_both",
481 ":implementation_static_dep_for_static",
482 ]`,
483 "implementation_dynamic_deps": `[
484 ":implementation_shared_dep_for_both",
485 ":implementation_shared_dep_for_static",
486 ]`,
487 "srcs": `[
488 "both.cpp",
489 "staticonly.cpp",
490 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500491 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400492 ":not_explicitly_exported_whole_static_dep_for_both",
493 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500494 ":not_explicitly_exported_whole_static_dep_for_static",
495 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500496 ]`,
497 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -0500498 makeBazelTarget("cc_library_shared", "a", attrNameToString{
499 "copts": `[
500 "bothflag",
501 "sharedflag",
502 ]`,
503 "deps": `[
504 ":static_dep_for_both",
505 ":static_dep_for_shared",
506 ]`,
507 "dynamic_deps": `[
508 ":shared_dep_for_both",
509 ":shared_dep_for_shared",
510 ]`,
511 "implementation_deps": `[
512 ":implementation_static_dep_for_both",
513 ":implementation_static_dep_for_shared",
514 ]`,
515 "implementation_dynamic_deps": `[
516 ":implementation_shared_dep_for_both",
517 ":implementation_shared_dep_for_shared",
518 ]`,
519 "srcs": `[
520 "both.cpp",
521 "sharedonly.cpp",
522 ]`,
523 "whole_archive_deps": `[
524 ":not_explicitly_exported_whole_static_dep_for_both",
525 ":whole_static_dep_for_both",
526 ":not_explicitly_exported_whole_static_dep_for_shared",
527 ":whole_static_dep_for_shared",
528 ]`,
529 })},
530 },
531 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400532}
533
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400534func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
535 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400536 moduleTypeUnderTest: "cc_library",
537 moduleTypeUnderTestFactory: cc.LibraryFactory,
538 dir: "foo/bar",
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400539 filesystem: map[string]string{
540 "foo/bar/Android.bp": `
541cc_library {
542 name: "a",
543 whole_static_libs: ["whole_static_lib_for_both"],
544 static: {
545 whole_static_libs: ["whole_static_lib_for_static"],
546 },
547 shared: {
548 whole_static_libs: ["whole_static_lib_for_shared"],
549 },
550 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400551 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400552}
553
554cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
555
556cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
557
558cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
559`,
560 },
561 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500562 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500563 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
564 "whole_archive_deps": `[
565 ":whole_static_lib_for_both_alwayslink",
566 ":whole_static_lib_for_static_alwayslink",
567 ]`,
568 }),
569 makeBazelTarget("cc_library_shared", "a", attrNameToString{
570 "whole_archive_deps": `[
571 ":whole_static_lib_for_both_alwayslink",
572 ":whole_static_lib_for_shared_alwayslink",
573 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500574 }),
575 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500576 },
577 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400578}
579
Jingwen Chenbcf53042021-05-26 04:42:42 +0000580func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
581 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400582 description: "cc_library shared/static props in arch",
583 moduleTypeUnderTest: "cc_library",
584 moduleTypeUnderTestFactory: cc.LibraryFactory,
585 dir: "foo/bar",
Jingwen Chenbcf53042021-05-26 04:42:42 +0000586 filesystem: map[string]string{
587 "foo/bar/arm.cpp": "",
588 "foo/bar/x86.cpp": "",
589 "foo/bar/sharedonly.cpp": "",
590 "foo/bar/staticonly.cpp": "",
591 "foo/bar/Android.bp": `
592cc_library {
593 name: "a",
594 arch: {
595 arm: {
596 shared: {
597 srcs: ["arm_shared.cpp"],
598 cflags: ["-DARM_SHARED"],
599 static_libs: ["arm_static_dep_for_shared"],
600 whole_static_libs: ["arm_whole_static_dep_for_shared"],
601 shared_libs: ["arm_shared_dep_for_shared"],
602 },
603 },
604 x86: {
605 static: {
606 srcs: ["x86_static.cpp"],
607 cflags: ["-DX86_STATIC"],
608 static_libs: ["x86_dep_for_static"],
609 },
610 },
611 },
612 target: {
613 android: {
614 shared: {
615 srcs: ["android_shared.cpp"],
616 cflags: ["-DANDROID_SHARED"],
617 static_libs: ["android_dep_for_shared"],
618 },
619 },
620 android_arm: {
621 shared: {
622 cflags: ["-DANDROID_ARM_SHARED"],
623 },
624 },
625 },
626 srcs: ["both.cpp"],
627 cflags: ["bothflag"],
628 static_libs: ["static_dep_for_both"],
629 static: {
630 srcs: ["staticonly.cpp"],
631 cflags: ["staticflag"],
632 static_libs: ["static_dep_for_static"],
633 },
634 shared: {
635 srcs: ["sharedonly.cpp"],
636 cflags: ["sharedflag"],
637 static_libs: ["static_dep_for_shared"],
638 },
639 bazel_module: { bp2build_available: true },
640}
641
642cc_library_static { name: "static_dep_for_shared" }
643cc_library_static { name: "static_dep_for_static" }
644cc_library_static { name: "static_dep_for_both" }
645
646cc_library_static { name: "arm_static_dep_for_shared" }
647cc_library_static { name: "arm_whole_static_dep_for_shared" }
648cc_library_static { name: "arm_shared_dep_for_shared" }
649
650cc_library_static { name: "x86_dep_for_static" }
651
652cc_library_static { name: "android_dep_for_shared" }
653`,
654 },
655 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500656 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500657 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
658 "copts": `[
659 "bothflag",
660 "staticflag",
661 ] + select({
662 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
663 "//conditions:default": [],
664 })`,
665 "implementation_deps": `[
666 ":static_dep_for_both",
667 ":static_dep_for_static",
668 ] + select({
669 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
670 "//conditions:default": [],
671 })`,
672 "local_includes": `["."]`,
673 "srcs": `[
674 "both.cpp",
675 "staticonly.cpp",
676 ] + select({
677 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
678 "//conditions:default": [],
679 })`,
680 }),
681 makeBazelTarget("cc_library_shared", "a", attrNameToString{
682 "copts": `[
683 "bothflag",
684 "sharedflag",
685 ] + select({
686 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
687 "//conditions:default": [],
688 }) + select({
689 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
690 "//conditions:default": [],
691 }) + select({
692 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
693 "//conditions:default": [],
694 })`,
695 "implementation_deps": `[
696 ":static_dep_for_both",
697 ":static_dep_for_shared",
698 ] + select({
699 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
700 "//conditions:default": [],
701 }) + select({
702 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
703 "//conditions:default": [],
704 })`,
705 "implementation_dynamic_deps": `select({
706 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
707 "//conditions:default": [],
708 })`,
709 "local_includes": `["."]`,
710 "srcs": `[
711 "both.cpp",
712 "sharedonly.cpp",
713 ] + select({
714 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
715 "//conditions:default": [],
716 }) + select({
717 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
718 "//conditions:default": [],
719 })`,
720 "whole_archive_deps": `select({
721 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
722 "//conditions:default": [],
723 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500724 }),
725 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500726 },
727 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000728}
729
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000730func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
731 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400732 description: "cc_library shared/static props with c/cpp/s mixed sources",
733 moduleTypeUnderTest: "cc_library",
734 moduleTypeUnderTestFactory: cc.LibraryFactory,
735 dir: "foo/bar",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000736 filesystem: map[string]string{
737 "foo/bar/both_source.cpp": "",
738 "foo/bar/both_source.cc": "",
739 "foo/bar/both_source.c": "",
740 "foo/bar/both_source.s": "",
741 "foo/bar/both_source.S": "",
742 "foo/bar/shared_source.cpp": "",
743 "foo/bar/shared_source.cc": "",
744 "foo/bar/shared_source.c": "",
745 "foo/bar/shared_source.s": "",
746 "foo/bar/shared_source.S": "",
747 "foo/bar/static_source.cpp": "",
748 "foo/bar/static_source.cc": "",
749 "foo/bar/static_source.c": "",
750 "foo/bar/static_source.s": "",
751 "foo/bar/static_source.S": "",
752 "foo/bar/Android.bp": `
753cc_library {
754 name: "a",
755 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400756 "both_source.cpp",
757 "both_source.cc",
758 "both_source.c",
759 "both_source.s",
760 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400761 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400762 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000763 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400764 srcs: [
765 "static_source.cpp",
766 "static_source.cc",
767 "static_source.c",
768 "static_source.s",
769 "static_source.S",
770 ":static_filegroup",
771 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000772 },
773 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400774 srcs: [
775 "shared_source.cpp",
776 "shared_source.cc",
777 "shared_source.c",
778 "shared_source.s",
779 "shared_source.S",
780 ":shared_filegroup",
781 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000782 },
783 bazel_module: { bp2build_available: true },
784}
785
786filegroup {
787 name: "both_filegroup",
788 srcs: [
789 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400790 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000791}
792
793filegroup {
794 name: "shared_filegroup",
795 srcs: [
796 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400797 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000798}
799
800filegroup {
801 name: "static_filegroup",
802 srcs: [
803 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400804 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000805}
806`,
807 },
808 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500809 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500810 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500811 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500812 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000813 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400814 "both_source.cc",
815 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500816 "static_source.cpp",
817 "static_source.cc",
818 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500819 ]`,
820 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000821 "both_source.s",
822 "both_source.S",
823 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500824 "static_source.s",
825 "static_source.S",
826 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500827 ]`,
828 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000829 "both_source.c",
830 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500831 "static_source.c",
832 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500833 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500834 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -0500835 makeBazelTarget("cc_library_shared", "a", attrNameToString{
836 "local_includes": `["."]`,
837 "srcs": `[
838 "both_source.cpp",
839 "both_source.cc",
840 ":both_filegroup_cpp_srcs",
841 "shared_source.cpp",
842 "shared_source.cc",
843 ":shared_filegroup_cpp_srcs",
844 ]`,
845 "srcs_as": `[
846 "both_source.s",
847 "both_source.S",
848 ":both_filegroup_as_srcs",
849 "shared_source.s",
850 "shared_source.S",
851 ":shared_filegroup_as_srcs",
852 ]`,
853 "srcs_c": `[
854 "both_source.c",
855 ":both_filegroup_c_srcs",
856 "shared_source.c",
857 ":shared_filegroup_c_srcs",
858 ]`,
859 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000860}
861
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200862func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
863 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400864 description: "cc_library non-configured version script",
865 moduleTypeUnderTest: "cc_library",
866 moduleTypeUnderTestFactory: cc.LibraryFactory,
867 dir: "foo/bar",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200868 filesystem: map[string]string{
869 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200870cc_library {
871 name: "a",
872 srcs: ["a.cpp"],
873 version_script: "v.map",
874 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400875 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200876}
877`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200878 },
879 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500880 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
881 "additional_linker_inputs": `["v.map"]`,
882 "linkopts": `["-Wl,--version-script,$(location v.map)"]`,
883 "srcs": `["a.cpp"]`,
884 }),
885 },
886 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200887}
888
889func TestCcLibraryConfiguredVersionScript(t *testing.T) {
890 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400891 description: "cc_library configured version script",
892 moduleTypeUnderTest: "cc_library",
893 moduleTypeUnderTestFactory: cc.LibraryFactory,
894 dir: "foo/bar",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200895 filesystem: map[string]string{
896 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400897cc_library {
898 name: "a",
899 srcs: ["a.cpp"],
900 arch: {
901 arm: {
902 version_script: "arm.map",
903 },
904 arm64: {
905 version_script: "arm64.map",
906 },
907 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200908
Liz Kammer8337ea42021-09-10 10:06:32 -0400909 bazel_module: { bp2build_available: true },
910 include_build_directory: false,
911}
Liz Kammerd366c902021-06-03 13:43:01 -0400912 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200913 },
914 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500915 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
916 "additional_linker_inputs": `select({
Liz Kammerd2871182021-10-04 13:54:37 -0400917 "//build/bazel/platforms/arch:arm": ["arm.map"],
918 "//build/bazel/platforms/arch:arm64": ["arm64.map"],
919 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500920 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500921 "linkopts": `select({
Liz Kammerd2871182021-10-04 13:54:37 -0400922 "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"],
923 "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"],
924 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500925 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500926 "srcs": `["a.cpp"]`,
927 }),
928 },
929 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200930}
931
932func TestCcLibrarySharedLibs(t *testing.T) {
933 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400934 description: "cc_library shared_libs",
935 moduleTypeUnderTest: "cc_library",
936 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -0400937 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400938cc_library {
939 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -0400940 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400941}
942
943cc_library {
944 name: "a",
945 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -0400946 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400947}
948`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500949 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
950 "implementation_dynamic_deps": `[":mylib"]`,
951 }),
952 },
953 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200954}
955
Liz Kammer0eae52e2021-10-06 10:32:26 -0400956func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -0500957 expected_targets := []string{}
958 expected_targets = append(expected_targets, makeCcLibraryTargets("a", attrNameToString{
959 "features": `[
960 "disable_pack_relocations",
961 "-no_undefined_symbols",
Yu Liu8d82ac52022-05-17 15:13:28 -0700962 "-coverage",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500963 ]`,
964 "srcs": `["a.cpp"]`,
965 })...)
966 expected_targets = append(expected_targets, makeCcLibraryTargets("b", attrNameToString{
Yu Liu8d82ac52022-05-17 15:13:28 -0700967 "features": `["-coverage"] + select({
Chris Parsons77acf2e2021-12-03 17:27:16 -0500968 "//build/bazel/platforms/arch:x86_64": [
969 "disable_pack_relocations",
970 "-no_undefined_symbols",
971 ],
972 "//conditions:default": [],
973 })`,
974 "srcs": `["b.cpp"]`,
975 })...)
976 expected_targets = append(expected_targets, makeCcLibraryTargets("c", attrNameToString{
977 "features": `select({
978 "//build/bazel/platforms/os:darwin": [
979 "disable_pack_relocations",
980 "-no_undefined_symbols",
981 ],
982 "//conditions:default": [],
983 })`,
984 "srcs": `["c.cpp"]`,
985 })...)
986
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200987 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400988 description: "cc_library pack_relocations test",
989 moduleTypeUnderTest: "cc_library",
990 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -0400991 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400992cc_library {
993 name: "a",
994 srcs: ["a.cpp"],
995 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -0400996 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -0400997 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -0700998 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400999}
1000
1001cc_library {
1002 name: "b",
1003 srcs: ["b.cpp"],
1004 arch: {
1005 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001006 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001007 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001008 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001009 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001010 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001011 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001012}
1013
1014cc_library {
1015 name: "c",
1016 srcs: ["c.cpp"],
1017 target: {
1018 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001019 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001020 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001021 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001022 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001023 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001024}`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001025 expectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001026 })
1027}
1028
1029func TestCcLibrarySpacesInCopts(t *testing.T) {
1030 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001031 description: "cc_library spaces in copts",
1032 moduleTypeUnderTest: "cc_library",
1033 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -04001034 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001035cc_library {
1036 name: "a",
1037 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001038 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001039}
1040`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001041 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
1042 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001043 "-include",
1044 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001045 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001046 }),
1047 },
1048 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001049}
1050
1051func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
1052 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001053 description: "cc_library cppflags usage",
1054 moduleTypeUnderTest: "cc_library",
1055 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -04001056 blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001057 name: "a",
1058 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001059 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001060 cppflags: [
1061 "-fsigned-char",
1062 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001063 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001064 arch: {
1065 arm64: {
1066 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001067 },
Liz Kammerd366c902021-06-03 13:43:01 -04001068 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001069 target: {
1070 android: {
1071 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001072 },
Liz Kammerd366c902021-06-03 13:43:01 -04001073 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001074 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001075}
1076`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001077 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
1078 "copts": `["-Wall"]`,
1079 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001080 "-fsigned-char",
1081 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001082 ] + select({
1083 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1084 "//conditions:default": [],
1085 }) + select({
1086 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1087 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001088 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001089 "srcs": `["a.cpp"]`,
1090 }),
1091 },
1092 )
Jingwen Chen63930982021-03-24 10:04:33 -04001093}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001094
Liz Kammer47535c52021-06-02 16:02:22 -04001095func TestCcLibraryExcludeLibs(t *testing.T) {
1096 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001097 moduleTypeUnderTest: "cc_library",
1098 moduleTypeUnderTestFactory: cc.LibraryFactory,
1099 filesystem: map[string]string{},
Liz Kammer47535c52021-06-02 16:02:22 -04001100 blueprint: soongCcLibraryStaticPreamble + `
1101cc_library {
1102 name: "foo_static",
1103 srcs: ["common.c"],
1104 whole_static_libs: [
1105 "arm_whole_static_lib_excludes",
1106 "malloc_not_svelte_whole_static_lib_excludes"
1107 ],
1108 static_libs: [
1109 "arm_static_lib_excludes",
1110 "malloc_not_svelte_static_lib_excludes"
1111 ],
1112 shared_libs: [
1113 "arm_shared_lib_excludes",
1114 ],
1115 arch: {
1116 arm: {
1117 exclude_shared_libs: [
1118 "arm_shared_lib_excludes",
1119 ],
1120 exclude_static_libs: [
1121 "arm_static_lib_excludes",
1122 "arm_whole_static_lib_excludes",
1123 ],
1124 },
1125 },
1126 product_variables: {
1127 malloc_not_svelte: {
1128 shared_libs: ["malloc_not_svelte_shared_lib"],
1129 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1130 exclude_static_libs: [
1131 "malloc_not_svelte_static_lib_excludes",
1132 "malloc_not_svelte_whole_static_lib_excludes",
1133 ],
1134 },
1135 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001136 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001137}
1138
1139cc_library {
1140 name: "arm_whole_static_lib_excludes",
1141 bazel_module: { bp2build_available: false },
1142}
1143
1144cc_library {
1145 name: "malloc_not_svelte_whole_static_lib",
1146 bazel_module: { bp2build_available: false },
1147}
1148
1149cc_library {
1150 name: "malloc_not_svelte_whole_static_lib_excludes",
1151 bazel_module: { bp2build_available: false },
1152}
1153
1154cc_library {
1155 name: "arm_static_lib_excludes",
1156 bazel_module: { bp2build_available: false },
1157}
1158
1159cc_library {
1160 name: "malloc_not_svelte_static_lib_excludes",
1161 bazel_module: { bp2build_available: false },
1162}
1163
1164cc_library {
1165 name: "arm_shared_lib_excludes",
1166 bazel_module: { bp2build_available: false },
1167}
1168
1169cc_library {
1170 name: "malloc_not_svelte_shared_lib",
1171 bazel_module: { bp2build_available: false },
1172}
1173`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001174 expectedBazelTargets: makeCcLibraryTargets("foo_static", attrNameToString{
1175 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001176 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001177 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001178 }) + select({
1179 "//build/bazel/product_variables:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001180 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001181 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001182 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001183 "//build/bazel/platforms/arch:arm": [],
1184 "//conditions:default": [":arm_shared_lib_excludes"],
1185 }) + select({
1186 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1187 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001188 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001189 "srcs_c": `["common.c"]`,
1190 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001191 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001192 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001193 }) + select({
Chris Parsons953b3562021-09-20 15:14:39 -04001194 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
1195 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001196 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001197 }),
1198 },
1199 )
Liz Kammer47535c52021-06-02 16:02:22 -04001200}
Liz Kammerd366c902021-06-03 13:43:01 -04001201
1202func TestCCLibraryNoCrtTrue(t *testing.T) {
1203 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001204 description: "cc_library - nocrt: true emits attribute",
1205 moduleTypeUnderTest: "cc_library",
1206 moduleTypeUnderTestFactory: cc.LibraryFactory,
Jingwen Chen6ada5892021-09-17 11:38:09 +00001207 filesystem: map[string]string{
1208 "impl.cpp": "",
1209 },
1210 blueprint: soongCcLibraryPreamble + `
1211cc_library {
1212 name: "foo-lib",
1213 srcs: ["impl.cpp"],
1214 nocrt: true,
1215 include_build_directory: false,
1216}
1217`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001218 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1219 "link_crt": `False`,
1220 "srcs": `["impl.cpp"]`,
1221 }),
1222 },
1223 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001224}
1225
1226func TestCCLibraryNoCrtFalse(t *testing.T) {
1227 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001228 description: "cc_library - nocrt: false - does not emit attribute",
1229 moduleTypeUnderTest: "cc_library",
1230 moduleTypeUnderTestFactory: cc.LibraryFactory,
Jingwen Chen6ada5892021-09-17 11:38:09 +00001231 filesystem: map[string]string{
1232 "impl.cpp": "",
1233 },
1234 blueprint: soongCcLibraryPreamble + `
1235cc_library {
1236 name: "foo-lib",
1237 srcs: ["impl.cpp"],
1238 nocrt: false,
1239 include_build_directory: false,
1240}
1241`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001242 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1243 "srcs": `["impl.cpp"]`,
1244 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001245 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001246}
1247
1248func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1249 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001250 description: "cc_library - nocrt in select",
1251 moduleTypeUnderTest: "cc_library",
1252 moduleTypeUnderTestFactory: cc.LibraryFactory,
Jingwen Chen6ada5892021-09-17 11:38:09 +00001253 filesystem: map[string]string{
1254 "impl.cpp": "",
1255 },
1256 blueprint: soongCcLibraryPreamble + `
1257cc_library {
1258 name: "foo-lib",
1259 srcs: ["impl.cpp"],
1260 arch: {
1261 arm: {
1262 nocrt: true,
1263 },
1264 x86: {
1265 nocrt: false,
1266 },
1267 },
1268 include_build_directory: false,
1269}
1270`,
Liz Kammer718eb272022-01-07 10:53:37 -05001271 expectedErr: fmt.Errorf("module \"foo-lib\": nocrt is not supported for arch variants"),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001272 })
1273}
1274
1275func TestCCLibraryNoLibCrtTrue(t *testing.T) {
1276 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001277 moduleTypeUnderTest: "cc_library",
1278 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammerd366c902021-06-03 13:43:01 -04001279 filesystem: map[string]string{
1280 "impl.cpp": "",
1281 },
1282 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001283cc_library {
1284 name: "foo-lib",
1285 srcs: ["impl.cpp"],
1286 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001287 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001288}
1289`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001290 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1291 "srcs": `["impl.cpp"]`,
1292 "use_libcrt": `False`,
1293 }),
1294 })
1295}
1296
1297func makeCcLibraryTargets(name string, attrs attrNameToString) []string {
1298 STATIC_ONLY_ATTRS := map[string]bool{}
1299 SHARED_ONLY_ATTRS := map[string]bool{
1300 "link_crt": true,
1301 "additional_linker_inputs": true,
1302 "linkopts": true,
1303 "strip": true,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00001304 "stubs_symbol_file": true,
1305 "stubs_versions": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001306 "inject_bssl_hash": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001307 }
1308 sharedAttrs := attrNameToString{}
1309 staticAttrs := attrNameToString{}
1310 for key, val := range attrs {
1311 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1312 sharedAttrs[key] = val
1313 }
1314 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1315 staticAttrs[key] = val
1316 }
1317 }
1318 sharedTarget := makeBazelTarget("cc_library_shared", name, sharedAttrs)
1319 staticTarget := makeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
1320
1321 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001322}
1323
Jingwen Chen6ada5892021-09-17 11:38:09 +00001324func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Liz Kammerd366c902021-06-03 13:43:01 -04001325 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001326 moduleTypeUnderTest: "cc_library",
1327 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammerd366c902021-06-03 13:43:01 -04001328 filesystem: map[string]string{
1329 "impl.cpp": "",
1330 },
1331 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001332cc_library {
1333 name: "foo-lib",
1334 srcs: ["impl.cpp"],
1335 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001336 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001337}
1338`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001339 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1340 "srcs": `["impl.cpp"]`,
1341 "use_libcrt": `True`,
1342 }),
1343 })
Liz Kammerd366c902021-06-03 13:43:01 -04001344}
1345
Jingwen Chen6ada5892021-09-17 11:38:09 +00001346func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Liz Kammerd366c902021-06-03 13:43:01 -04001347 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001348 moduleTypeUnderTest: "cc_library",
1349 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammerd366c902021-06-03 13:43:01 -04001350 filesystem: map[string]string{
1351 "impl.cpp": "",
1352 },
1353 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001354cc_library {
1355 name: "foo-lib",
1356 srcs: ["impl.cpp"],
1357 arch: {
1358 arm: {
1359 no_libcrt: true,
1360 },
1361 x86: {
1362 no_libcrt: true,
1363 },
1364 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001365 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001366}
1367`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001368 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1369 "srcs": `["impl.cpp"]`,
1370 "use_libcrt": `select({
Liz Kammerd366c902021-06-03 13:43:01 -04001371 "//build/bazel/platforms/arch:arm": False,
1372 "//build/bazel/platforms/arch:x86": False,
1373 "//conditions:default": None,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001374 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001375 }),
1376 })
Liz Kammerd366c902021-06-03 13:43:01 -04001377}
1378
Chris Parsons58852a02021-12-09 18:10:18 -05001379func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
1380 runCcLibraryTestCase(t, bp2buildTestCase{
1381 moduleTypeUnderTest: "cc_library",
1382 moduleTypeUnderTestFactory: cc.LibraryFactory,
1383 filesystem: map[string]string{
1384 "impl.cpp": "",
1385 },
1386 blueprint: soongCcLibraryPreamble + `
1387cc_library {
1388 name: "foo-lib",
1389 srcs: ["impl.cpp"],
1390 arch: {
1391 arm: {
1392 no_libcrt: true,
1393 },
1394 x86: {
1395 no_libcrt: true,
1396 },
1397 },
1398 target: {
1399 darwin: {
1400 no_libcrt: true,
1401 }
1402 },
1403 include_build_directory: false,
1404}
1405`,
1406 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1407 "srcs": `["impl.cpp"]`,
1408 "use_libcrt": `select({
1409 "//build/bazel/platforms/os_arch:android_arm": False,
1410 "//build/bazel/platforms/os_arch:android_x86": False,
1411 "//build/bazel/platforms/os_arch:darwin_arm64": False,
1412 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1413 "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
1414 "//build/bazel/platforms/os_arch:linux_musl_x86": False,
1415 "//build/bazel/platforms/os_arch:windows_x86": False,
1416 "//conditions:default": None,
1417 })`,
1418 }),
1419 })
1420}
1421
1422func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
1423 runCcLibraryTestCase(t, bp2buildTestCase{
1424 moduleTypeUnderTest: "cc_library",
1425 moduleTypeUnderTestFactory: cc.LibraryFactory,
1426 filesystem: map[string]string{
1427 "impl.cpp": "",
1428 },
1429 blueprint: soongCcLibraryPreamble + `
1430cc_library {
1431 name: "foo-lib",
1432 srcs: ["impl.cpp"],
1433 arch: {
1434 arm: {
1435 no_libcrt: true,
1436 },
1437 // This is expected to override the value for darwin_x86_64.
1438 x86_64: {
1439 no_libcrt: true,
1440 },
1441 },
1442 target: {
1443 darwin: {
1444 no_libcrt: false,
1445 }
1446 },
1447 include_build_directory: false,
1448}
1449`,
1450 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1451 "srcs": `["impl.cpp"]`,
1452 "use_libcrt": `select({
1453 "//build/bazel/platforms/os_arch:android_arm": False,
1454 "//build/bazel/platforms/os_arch:android_x86_64": False,
1455 "//build/bazel/platforms/os_arch:darwin_arm64": True,
1456 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1457 "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
1458 "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
1459 "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
1460 "//build/bazel/platforms/os_arch:windows_x86_64": False,
1461 "//conditions:default": None,
1462 })`,
1463 }),
1464 })
1465}
1466
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001467func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001468 expectedTargets := []string{}
1469 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", attrNameToString{
1470 "strip": `{
1471 "all": True,
1472 }`,
1473 })...)
1474 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", attrNameToString{
1475 "strip": `{
1476 "keep_symbols": True,
1477 }`,
1478 })...)
1479 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", attrNameToString{
1480 "strip": `{
1481 "keep_symbols_and_debug_frame": True,
1482 }`,
1483 })...)
1484 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", attrNameToString{
1485 "strip": `{
1486 "keep_symbols_list": ["symbol"],
1487 }`,
1488 })...)
1489 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", attrNameToString{
1490 "strip": `{
1491 "none": True,
1492 }`,
1493 })...)
1494 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", attrNameToString{})...)
1495
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001496 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001497 description: "cc_library strip args",
1498 moduleTypeUnderTest: "cc_library",
1499 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -04001500 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001501cc_library {
1502 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001503 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001504}
1505cc_library {
1506 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001507 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001508 keep_symbols: true,
1509 },
1510 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001511}
1512cc_library {
1513 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001514 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001515 keep_symbols_and_debug_frame: true,
1516 },
1517 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001518}
1519cc_library {
1520 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001521 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001522 none: true,
1523 },
1524 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001525}
1526cc_library {
1527 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001528 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001529 keep_symbols_list: ["symbol"],
1530 },
1531 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001532}
1533cc_library {
1534 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001535 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001536 all: true,
1537 },
1538 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001539}
1540`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001541 expectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001542 })
1543}
1544
1545func TestCcLibraryStripWithArch(t *testing.T) {
1546 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001547 description: "cc_library strip args",
1548 moduleTypeUnderTest: "cc_library",
1549 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer8337ea42021-09-10 10:06:32 -04001550 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001551cc_library {
1552 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001553 target: {
1554 darwin: {
1555 strip: {
1556 keep_symbols_list: ["foo", "bar"]
1557 }
1558 },
1559 },
1560 arch: {
1561 arm: {
1562 strip: {
1563 keep_symbols_and_debug_frame: true,
1564 },
1565 },
1566 arm64: {
1567 strip: {
1568 keep_symbols: true,
1569 },
1570 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001571 },
1572 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001573}
1574`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001575 expectedBazelTargets: makeCcLibraryTargets("multi-arch", attrNameToString{
1576 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001577 "keep_symbols": select({
1578 "//build/bazel/platforms/arch:arm64": True,
1579 "//conditions:default": None,
1580 }),
1581 "keep_symbols_and_debug_frame": select({
1582 "//build/bazel/platforms/arch:arm": True,
1583 "//conditions:default": None,
1584 }),
1585 "keep_symbols_list": select({
1586 "//build/bazel/platforms/os:darwin": [
1587 "foo",
1588 "bar",
1589 ],
1590 "//conditions:default": [],
1591 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001592 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001593 }),
1594 },
1595 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001596}
Chris Parsons51f8c392021-08-03 21:01:05 -04001597
1598func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1599 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001600 description: "cc_library system_shared_libs empty at root",
1601 moduleTypeUnderTest: "cc_library",
1602 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001603 blueprint: soongCcLibraryPreamble + `
1604cc_library {
1605 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001606 system_shared_libs: [],
1607 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001608}
1609`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001610 expectedBazelTargets: makeCcLibraryTargets("root_empty", attrNameToString{
1611 "system_dynamic_deps": `[]`,
1612 }),
1613 },
1614 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001615}
1616
1617func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1618 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001619 description: "cc_library system_shared_libs empty for static variant",
1620 moduleTypeUnderTest: "cc_library",
1621 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001622 blueprint: soongCcLibraryPreamble + `
1623cc_library {
1624 name: "static_empty",
1625 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001626 system_shared_libs: [],
1627 },
1628 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001629}
1630`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001631 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001632 makeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", attrNameToString{
1633 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001634 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001635 makeBazelTarget("cc_library_shared", "static_empty", attrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001636 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001637 })
1638}
1639
1640func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
1641 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001642 description: "cc_library system_shared_libs empty for shared variant",
1643 moduleTypeUnderTest: "cc_library",
1644 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001645 blueprint: soongCcLibraryPreamble + `
1646cc_library {
1647 name: "shared_empty",
1648 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001649 system_shared_libs: [],
1650 },
1651 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001652}
1653`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001654 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001655 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}),
1656 makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{
1657 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001658 }),
1659 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001660 })
1661}
1662
1663func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
1664 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001665 description: "cc_library system_shared_libs empty for shared, bionic variant",
1666 moduleTypeUnderTest: "cc_library",
1667 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001668 blueprint: soongCcLibraryPreamble + `
1669cc_library {
1670 name: "shared_empty",
1671 target: {
1672 bionic: {
1673 shared: {
1674 system_shared_libs: [],
1675 }
1676 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001677 },
1678 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001679}
1680`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001681 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001682 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}),
1683 makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{
1684 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001685 }),
1686 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001687 })
1688}
1689
1690func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1691 // Note that this behavior is technically incorrect (it's a simplification).
1692 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1693 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1694 // b/195791252 tracks the fix.
1695 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001696 description: "cc_library system_shared_libs empty for linux_bionic variant",
1697 moduleTypeUnderTest: "cc_library",
1698 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001699 blueprint: soongCcLibraryPreamble + `
1700cc_library {
1701 name: "target_linux_bionic_empty",
1702 target: {
1703 linux_bionic: {
1704 system_shared_libs: [],
1705 },
1706 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001707 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001708}
1709`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001710 expectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", attrNameToString{
1711 "system_dynamic_deps": `[]`,
1712 }),
1713 },
1714 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001715}
1716
1717func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1718 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001719 description: "cc_library system_shared_libs empty for bionic variant",
1720 moduleTypeUnderTest: "cc_library",
1721 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001722 blueprint: soongCcLibraryPreamble + `
1723cc_library {
1724 name: "target_bionic_empty",
1725 target: {
1726 bionic: {
1727 system_shared_libs: [],
1728 },
1729 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001730 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001731}
1732`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001733 expectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", attrNameToString{
1734 "system_dynamic_deps": `[]`,
1735 }),
1736 },
1737 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001738}
1739
1740func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
1741 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001742 description: "cc_library system_shared_libs set for shared and root",
1743 moduleTypeUnderTest: "cc_library",
1744 moduleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons51f8c392021-08-03 21:01:05 -04001745 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001746cc_library {
1747 name: "libc",
1748 bazel_module: { bp2build_available: false },
1749}
1750cc_library {
1751 name: "libm",
1752 bazel_module: { bp2build_available: false },
1753}
Chris Parsons51f8c392021-08-03 21:01:05 -04001754
1755cc_library {
1756 name: "foo",
1757 system_shared_libs: ["libc"],
1758 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001759 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001760 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001761 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001762}
1763`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001764 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001765 makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001766 "system_dynamic_deps": `[":libc"]`,
1767 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001768 makeBazelTarget("cc_library_shared", "foo", attrNameToString{
1769 "system_dynamic_deps": `[
1770 ":libc",
1771 ":libm",
1772 ]`,
1773 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001774 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001775 })
1776}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001777
1778func TestCcLibraryOsSelects(t *testing.T) {
1779 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001780 description: "cc_library - selects for all os targets",
1781 moduleTypeUnderTest: "cc_library",
1782 moduleTypeUnderTestFactory: cc.LibraryFactory,
1783 filesystem: map[string]string{},
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001784 blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001785cc_library {
1786 name: "foo-lib",
1787 srcs: ["base.cpp"],
1788 target: {
1789 android: {
1790 srcs: ["android.cpp"],
1791 },
1792 linux: {
1793 srcs: ["linux.cpp"],
1794 },
1795 linux_glibc: {
1796 srcs: ["linux_glibc.cpp"],
1797 },
1798 darwin: {
1799 srcs: ["darwin.cpp"],
1800 },
1801 bionic: {
1802 srcs: ["bionic.cpp"],
1803 },
1804 linux_musl: {
1805 srcs: ["linux_musl.cpp"],
1806 },
1807 windows: {
1808 srcs: ["windows.cpp"],
1809 },
1810 },
1811 include_build_directory: false,
1812}
1813`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001814 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1815 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001816 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001817 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001818 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001819 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001820 ],
1821 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
1822 "//build/bazel/platforms/os:linux": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001823 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001824 "linux_glibc.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001825 ],
1826 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001827 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001828 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001829 ],
1830 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001831 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001832 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001833 ],
1834 "//build/bazel/platforms/os:windows": ["windows.cpp"],
1835 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001836 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001837 }),
1838 },
1839 )
Jingwen Chen97b85312021-10-08 10:41:31 +00001840}
1841
Yu Liu75be7b92022-02-01 09:54:09 -08001842func TestLibcryptoHashInjection(t *testing.T) {
1843 runCcLibraryTestCase(t, bp2buildTestCase{
1844 description: "cc_library - libcrypto hash injection",
1845 moduleTypeUnderTest: "cc_library",
1846 moduleTypeUnderTestFactory: cc.LibraryFactory,
1847 filesystem: map[string]string{},
1848 blueprint: soongCcLibraryPreamble + `
1849cc_library {
1850 name: "libcrypto",
1851 target: {
1852 android: {
1853 inject_bssl_hash: true,
1854 },
1855 },
1856 include_build_directory: false,
1857}
1858`,
1859 expectedBazelTargets: makeCcLibraryTargets("libcrypto", attrNameToString{
1860 "inject_bssl_hash": `select({
1861 "//build/bazel/platforms/os:android": True,
1862 "//conditions:default": None,
1863 })`,
1864 }),
1865 },
1866 )
1867}
1868
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001869func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00001870 type testCase struct {
1871 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001872 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001873 gnu_extensions string
1874 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001875 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001876 }
1877
1878 testCases := []testCase{
1879 // Existing usages of cpp_std in AOSP are:
1880 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
1881 //
1882 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
1883 // is set in the toolchain.)
1884 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001885 {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001886 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
1887 // experimental defaults to gnu++2a
1888 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "gnu++2a"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001889 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001890 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "gnu++2a"},
1891 // Explicitly setting a c++ std does not use replace gnu++ std even if
1892 // gnu_extensions is true.
1893 // "c++11",
1894 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001895 {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001896 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
1897 // "c++17",
1898 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001899 {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001900 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
1901 // "c++2a",
1902 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001903 {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001904 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
1905 // "c++98",
1906 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001907 {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001908 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
1909 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
1910 // "gnu++11",
1911 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001912 {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001913 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
1914 // "gnu++17",
1915 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001916 {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001917 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001918
1919 // some c_std test cases
Elliott Hughes6741d0e2022-05-12 09:53:11 -07001920 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "gnu17"},
1921 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c17"},
1922 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "gnu17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001923 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
1924 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
1925 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001926 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001927 for i, tc := range testCases {
1928 name_prefix := fmt.Sprintf("a_%v", i)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001929 cppStdProp := ""
Jingwen Chen97b85312021-10-08 10:41:31 +00001930 if tc.cpp_std != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001931 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
Jingwen Chen97b85312021-10-08 10:41:31 +00001932 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001933 cStdProp := ""
1934 if tc.c_std != "" {
1935 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
1936 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001937 gnuExtensionsProp := ""
Jingwen Chen97b85312021-10-08 10:41:31 +00001938 if tc.gnu_extensions != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001939 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
Jingwen Chen97b85312021-10-08 10:41:31 +00001940 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001941 attrs := attrNameToString{}
Jingwen Chen97b85312021-10-08 10:41:31 +00001942 if tc.bazel_cpp_std != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001943 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
Jingwen Chen97b85312021-10-08 10:41:31 +00001944 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001945 if tc.bazel_c_std != "" {
1946 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
1947 }
Jingwen Chen97b85312021-10-08 10:41:31 +00001948
1949 runCcLibraryTestCase(t, bp2buildTestCase{
1950 description: fmt.Sprintf(
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001951 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
1952 moduleTypeUnderTest: "cc_library",
1953 moduleTypeUnderTestFactory: cc.LibraryFactory,
Jingwen Chen97b85312021-10-08 10:41:31 +00001954 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1955cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001956 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00001957%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001958%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00001959%s // gnu_extensions: *bool
1960 include_build_directory: false,
1961}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001962`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001963 expectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Jingwen Chen97b85312021-10-08 10:41:31 +00001964 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001965
1966 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1967 description: fmt.Sprintf(
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001968 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
1969 moduleTypeUnderTest: "cc_library_static",
1970 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001971 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1972cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001973 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001974%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001975%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001976%s // gnu_extensions: *bool
1977 include_build_directory: false,
1978}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001979`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001980 expectedBazelTargets: []string{
Chris Parsons79bd2b72021-11-29 17:52:41 -05001981 makeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001982 },
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001983 })
1984
1985 runCcLibrarySharedTestCase(t, bp2buildTestCase{
1986 description: fmt.Sprintf(
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001987 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
1988 moduleTypeUnderTest: "cc_library_shared",
1989 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001990 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1991cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001992 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001993%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001994%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001995%s // gnu_extensions: *bool
1996 include_build_directory: false,
1997}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001998`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001999 expectedBazelTargets: []string{
Chris Parsons79bd2b72021-11-29 17:52:41 -05002000 makeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002001 },
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002002 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002003 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002004}
Liz Kammer12615db2021-09-28 09:19:17 -04002005
2006func TestCcLibraryProtoSimple(t *testing.T) {
2007 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002008 moduleTypeUnderTest: "cc_library",
2009 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002010 blueprint: soongCcProtoPreamble + `cc_library {
2011 name: "foo",
2012 srcs: ["foo.proto"],
2013 include_build_directory: false,
2014}`,
2015 expectedBazelTargets: []string{
2016 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002017 "srcs": `["foo.proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002018 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2019 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002020 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002021 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002022 "deps": `[":libprotobuf-cpp-lite"]`,
2023 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2024 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002025 }),
2026 },
2027 })
2028}
2029
2030func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
2031 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002032 moduleTypeUnderTest: "cc_library",
2033 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002034 blueprint: soongCcProtoPreamble + `cc_library {
2035 name: "foo",
2036 srcs: ["foo.proto"],
2037 proto: { canonical_path_from_root: false},
2038 include_build_directory: false,
2039}`,
2040 expectedBazelTargets: []string{
2041 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002042 "srcs": `["foo.proto"]`,
2043 "strip_import_prefix": `""`,
Liz Kammer12615db2021-09-28 09:19:17 -04002044 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2045 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002046 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002047 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002048 "deps": `[":libprotobuf-cpp-lite"]`,
2049 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2050 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002051 }),
2052 },
2053 })
2054}
2055
2056func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
2057 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002058 moduleTypeUnderTest: "cc_library",
2059 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002060 blueprint: soongCcProtoPreamble + `cc_library {
2061 name: "foo",
2062 srcs: ["foo.proto"],
2063 proto: { canonical_path_from_root: true},
2064 include_build_directory: false,
2065}`,
2066 expectedBazelTargets: []string{
2067 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002068 "srcs": `["foo.proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002069 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2070 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002071 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002072 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002073 "deps": `[":libprotobuf-cpp-lite"]`,
2074 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2075 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002076 }),
2077 },
2078 })
2079}
2080
2081func TestCcLibraryProtoFull(t *testing.T) {
2082 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002083 moduleTypeUnderTest: "cc_library",
2084 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002085 blueprint: soongCcProtoPreamble + `cc_library {
2086 name: "foo",
2087 srcs: ["foo.proto"],
2088 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002089 type: "full",
2090 },
2091 include_build_directory: false,
2092}`,
2093 expectedBazelTargets: []string{
2094 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2095 "srcs": `["foo.proto"]`,
2096 }), makeBazelTarget("cc_proto_library", "foo_cc_proto", attrNameToString{
2097 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002098 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002099 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002100 "deps": `[":libprotobuf-cpp-full"]`,
2101 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2102 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002103 }),
2104 },
2105 })
2106}
2107
2108func TestCcLibraryProtoLite(t *testing.T) {
2109 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002110 moduleTypeUnderTest: "cc_library",
2111 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002112 blueprint: soongCcProtoPreamble + `cc_library {
2113 name: "foo",
2114 srcs: ["foo.proto"],
2115 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002116 type: "lite",
2117 },
2118 include_build_directory: false,
2119}`,
2120 expectedBazelTargets: []string{
2121 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2122 "srcs": `["foo.proto"]`,
2123 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2124 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002125 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002126 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002127 "deps": `[":libprotobuf-cpp-lite"]`,
2128 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2129 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002130 }),
2131 },
2132 })
2133}
2134
2135func TestCcLibraryProtoExportHeaders(t *testing.T) {
2136 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04002137 moduleTypeUnderTest: "cc_library",
2138 moduleTypeUnderTestFactory: cc.LibraryFactory,
Liz Kammer12615db2021-09-28 09:19:17 -04002139 blueprint: soongCcProtoPreamble + `cc_library {
2140 name: "foo",
2141 srcs: ["foo.proto"],
2142 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002143 export_proto_headers: true,
2144 },
2145 include_build_directory: false,
2146}`,
2147 expectedBazelTargets: []string{
2148 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2149 "srcs": `["foo.proto"]`,
2150 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2151 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002152 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
2153 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002154 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002155 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2156 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2157 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002158 }),
2159 },
2160 })
2161}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002162
2163func TestCcLibraryProtoFilegroups(t *testing.T) {
2164 runCcLibraryTestCase(t, bp2buildTestCase{
2165 moduleTypeUnderTest: "cc_library",
2166 moduleTypeUnderTestFactory: cc.LibraryFactory,
2167 blueprint: soongCcProtoPreamble +
2168 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2169 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2170 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2171 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2172cc_library {
2173 name: "a",
2174 srcs: [":a_fg_proto"],
2175 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002176 export_proto_headers: true,
2177 },
2178 include_build_directory: false,
2179}
2180
2181cc_library {
2182 name: "b",
2183 srcs: [":b_protos"],
2184 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002185 export_proto_headers: true,
2186 },
2187 include_build_directory: false,
2188}
2189
2190cc_library {
2191 name: "c",
2192 srcs: [":c-proto-srcs"],
2193 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002194 export_proto_headers: true,
2195 },
2196 include_build_directory: false,
2197}
2198
2199cc_library {
2200 name: "d",
2201 srcs: [":proto-srcs-d"],
2202 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002203 export_proto_headers: true,
2204 },
2205 include_build_directory: false,
2206}`,
2207 expectedBazelTargets: []string{
2208 makeBazelTarget("proto_library", "a_proto", attrNameToString{
2209 "srcs": `[":a_fg_proto"]`,
2210 }), makeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", attrNameToString{
2211 "deps": `[":a_proto"]`,
2212 }), makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
2213 "deps": `[":libprotobuf-cpp-lite"]`,
2214 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2215 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2216 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2217 "srcs_c": `[":a_fg_proto_c_srcs"]`,
2218 }), makeBazelTarget("cc_library_shared", "a", attrNameToString{
2219 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2220 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2221 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2222 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2223 "srcs_c": `[":a_fg_proto_c_srcs"]`,
2224 }), makeBazelTarget("proto_library", "b_proto", attrNameToString{
2225 "srcs": `[":b_protos"]`,
2226 }), makeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", attrNameToString{
2227 "deps": `[":b_proto"]`,
2228 }), makeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", attrNameToString{
2229 "deps": `[":libprotobuf-cpp-lite"]`,
2230 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2231 "srcs": `[":b_protos_cpp_srcs"]`,
2232 "srcs_as": `[":b_protos_as_srcs"]`,
2233 "srcs_c": `[":b_protos_c_srcs"]`,
2234 }), makeBazelTarget("cc_library_shared", "b", attrNameToString{
2235 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2236 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2237 "srcs": `[":b_protos_cpp_srcs"]`,
2238 "srcs_as": `[":b_protos_as_srcs"]`,
2239 "srcs_c": `[":b_protos_c_srcs"]`,
2240 }), makeBazelTarget("proto_library", "c_proto", attrNameToString{
2241 "srcs": `[":c-proto-srcs"]`,
2242 }), makeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", attrNameToString{
2243 "deps": `[":c_proto"]`,
2244 }), makeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", attrNameToString{
2245 "deps": `[":libprotobuf-cpp-lite"]`,
2246 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2247 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2248 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2249 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
2250 }), makeBazelTarget("cc_library_shared", "c", attrNameToString{
2251 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2252 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2253 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2254 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2255 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
2256 }), makeBazelTarget("proto_library", "d_proto", attrNameToString{
2257 "srcs": `[":proto-srcs-d"]`,
2258 }), makeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", attrNameToString{
2259 "deps": `[":d_proto"]`,
2260 }), makeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", attrNameToString{
2261 "deps": `[":libprotobuf-cpp-lite"]`,
2262 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2263 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2264 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2265 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2266 }), makeBazelTarget("cc_library_shared", "d", attrNameToString{
2267 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2268 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2269 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2270 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2271 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2272 }),
2273 },
2274 })
2275}
Chris Parsons58852a02021-12-09 18:10:18 -05002276
2277func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
2278 runCcLibraryTestCase(t, bp2buildTestCase{
2279 moduleTypeUnderTest: "cc_library",
2280 moduleTypeUnderTestFactory: cc.LibraryFactory,
2281 blueprint: soongCcProtoPreamble + `cc_library {
2282 name: "foo",
2283 srcs: ["foo.cpp"],
2284 target: {
2285 darwin: {
2286 enabled: false,
2287 },
2288 windows: {
2289 enabled: false,
2290 },
2291 linux_glibc_x86: {
2292 enabled: false,
2293 },
2294 },
2295 include_build_directory: false,
2296}`,
2297 expectedBazelTargets: makeCcLibraryTargets("foo", attrNameToString{
2298 "srcs": `["foo.cpp"]`,
2299 "target_compatible_with": `select({
2300 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2301 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2302 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2303 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2304 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2305 "//conditions:default": [],
2306 })`,
2307 }),
2308 })
2309}
2310
2311func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
2312 runCcLibraryTestCase(t, bp2buildTestCase{
2313 moduleTypeUnderTest: "cc_library",
2314 moduleTypeUnderTestFactory: cc.LibraryFactory,
2315 blueprint: soongCcProtoPreamble + `cc_library {
2316 name: "foo",
2317 srcs: ["foo.cpp"],
2318 enabled: false,
2319 target: {
2320 darwin: {
2321 enabled: true,
2322 },
2323 windows: {
2324 enabled: false,
2325 },
2326 linux_glibc_x86: {
2327 enabled: false,
2328 },
2329 },
2330 include_build_directory: false,
2331}`,
2332 expectedBazelTargets: makeCcLibraryTargets("foo", attrNameToString{
2333 "srcs": `["foo.cpp"]`,
2334 "target_compatible_with": `select({
2335 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2336 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2337 "//conditions:default": ["@platforms//:incompatible"],
2338 })`,
2339 }),
2340 })
2341}
2342
2343func TestCcLibrarySharedDisabled(t *testing.T) {
2344 runCcLibraryTestCase(t, bp2buildTestCase{
2345 moduleTypeUnderTest: "cc_library",
2346 moduleTypeUnderTestFactory: cc.LibraryFactory,
2347 blueprint: soongCcProtoPreamble + `cc_library {
2348 name: "foo",
2349 srcs: ["foo.cpp"],
2350 enabled: false,
2351 shared: {
2352 enabled: true,
2353 },
2354 target: {
2355 android: {
2356 shared: {
2357 enabled: false,
2358 },
2359 }
2360 },
2361 include_build_directory: false,
2362}`,
2363 expectedBazelTargets: []string{makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
2364 "srcs": `["foo.cpp"]`,
2365 "target_compatible_with": `["@platforms//:incompatible"]`,
2366 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2367 "srcs": `["foo.cpp"]`,
2368 "target_compatible_with": `select({
2369 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2370 "//conditions:default": [],
2371 })`,
2372 }),
2373 },
2374 })
2375}
2376
2377func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
2378 runCcLibraryTestCase(t, bp2buildTestCase{
2379 moduleTypeUnderTest: "cc_library",
2380 moduleTypeUnderTestFactory: cc.LibraryFactory,
2381 blueprint: soongCcProtoPreamble + `cc_library {
2382 name: "foo",
2383 srcs: ["foo.cpp"],
2384 shared: {
2385 enabled: false
2386 },
2387 target: {
2388 darwin: {
2389 enabled: true,
2390 },
2391 windows: {
2392 enabled: false,
2393 },
2394 linux_glibc_x86: {
2395 shared: {
2396 enabled: true,
2397 },
2398 },
2399 },
2400 include_build_directory: false,
2401}`,
2402 expectedBazelTargets: []string{makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
2403 "srcs": `["foo.cpp"]`,
2404 "target_compatible_with": `select({
2405 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2406 "//conditions:default": [],
2407 })`,
2408 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2409 "srcs": `["foo.cpp"]`,
2410 "target_compatible_with": `select({
2411 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2412 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2413 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2414 "//conditions:default": ["@platforms//:incompatible"],
2415 })`,
2416 }),
2417 }})
2418}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002419
2420func TestCcLibraryStubs(t *testing.T) {
2421 runCcLibraryTestCase(t, bp2buildTestCase{
2422 description: "cc_library stubs",
2423 moduleTypeUnderTest: "cc_library",
2424 moduleTypeUnderTestFactory: cc.LibraryFactory,
2425 dir: "foo/bar",
2426 filesystem: map[string]string{
2427 "foo/bar/Android.bp": `
2428cc_library {
2429 name: "a",
2430 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2431 bazel_module: { bp2build_available: true },
2432 include_build_directory: false,
2433}
2434`,
2435 },
2436 blueprint: soongCcLibraryPreamble,
2437 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
2438 "stubs_symbol_file": `"a.map.txt"`,
2439 "stubs_versions": `[
2440 "28",
2441 "29",
2442 "current",
2443 ]`,
2444 }),
2445 },
2446 )
2447}
Liz Kammerf38a8372022-02-04 15:39:00 -05002448
2449func TestCcLibraryEscapeLdflags(t *testing.T) {
2450 runCcLibraryTestCase(t, bp2buildTestCase{
2451 moduleTypeUnderTest: "cc_library",
2452 moduleTypeUnderTestFactory: cc.LibraryFactory,
2453 blueprint: soongCcProtoPreamble + `cc_library {
2454 name: "foo",
2455 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2456 include_build_directory: false,
2457}`,
2458 expectedBazelTargets: makeCcLibraryTargets("foo", attrNameToString{
2459 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2460 }),
2461 })
2462}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002463
2464func TestCcLibraryConvertLex(t *testing.T) {
2465 runCcLibraryTestCase(t, bp2buildTestCase{
2466 moduleTypeUnderTest: "cc_library",
2467 moduleTypeUnderTestFactory: cc.LibraryFactory,
2468 filesystem: map[string]string{
2469 "foo.c": "",
2470 "bar.cc": "",
2471 "foo1.l": "",
2472 "bar1.ll": "",
2473 "foo2.l": "",
2474 "bar2.ll": "",
2475 },
2476 blueprint: `cc_library {
2477 name: "foo_lib",
2478 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2479 lex: { flags: ["--foo_flags"] },
2480 include_build_directory: false,
2481 bazel_module: { bp2build_available: true },
2482}`,
2483 expectedBazelTargets: append([]string{
2484 makeBazelTarget("genlex", "foo_lib_genlex_l", attrNameToString{
2485 "srcs": `[
2486 "foo1.l",
2487 "foo2.l",
2488 ]`,
2489 "lexopts": `["--foo_flags"]`,
2490 }),
2491 makeBazelTarget("genlex", "foo_lib_genlex_ll", attrNameToString{
2492 "srcs": `[
2493 "bar1.ll",
2494 "bar2.ll",
2495 ]`,
2496 "lexopts": `["--foo_flags"]`,
2497 }),
2498 },
2499 makeCcLibraryTargets("foo_lib", attrNameToString{
2500 "srcs": `[
2501 "bar.cc",
2502 ":foo_lib_genlex_ll",
2503 ]`,
2504 "srcs_c": `[
2505 "foo.c",
2506 ":foo_lib_genlex_l",
2507 ]`,
2508 })...),
2509 })
2510}