blob: 2d1a7cea4dcc199d158a4513f8e7b46d452e8e33 [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
Sam Delmerico3177a6e2022-06-21 19:28:33 +000046func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040047 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000048 RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020049}
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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000060 runCcLibraryTestCase(t, Bp2buildTestCase{
61 Description: "cc_library - simple example",
62 ModuleTypeUnderTest: "cc_library",
63 ModuleTypeUnderTestFactory: cc.LibraryFactory,
64 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020065 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000084 Blueprint: soongCcLibraryPreamble +
Liz Kammer78cfdaa2021-11-08 12:56:31 -050085 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000123 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500124 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000154 runCcLibraryTestCase(t, Bp2buildTestCase{
155 Description: "cc_library - trimmed example of //bionic/linker:ld-android",
156 ModuleTypeUnderTest: "cc_library",
157 ModuleTypeUnderTestFactory: cc.LibraryFactory,
158 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200159 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000165 Blueprint: soongCcLibraryPreamble +
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400166 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000196 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500197 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000222 runCcLibraryTestCase(t, Bp2buildTestCase{
223 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",
227 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200228 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000260 Blueprint: soongCcLibraryPreamble,
261 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500262 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000273 runCcLibraryTestCase(t, Bp2buildTestCase{
274 Description: "cc_library shared/static props",
275 ModuleTypeUnderTest: "cc_library",
276 ModuleTypeUnderTestFactory: cc.LibraryFactory,
277 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400278 "both.cpp": "",
279 "sharedonly.cpp": "",
280 "staticonly.cpp": "",
281 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000282 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000357 ExpectedBazelTargets: []string{
358 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500359 "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 ]`}),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000380 makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500381 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000408 runCcLibraryTestCase(t, Bp2buildTestCase{
409 Description: "cc_library shared/static props",
410 ModuleTypeUnderTest: "cc_library",
411 ModuleTypeUnderTestFactory: cc.LibraryFactory,
412 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400413 "both.cpp": "",
414 "sharedonly.cpp": "",
415 "staticonly.cpp": "",
416 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000417 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400418cc_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"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000465 ExpectedBazelTargets: []string{
466 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500467 "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 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000498 makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500499 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000535 runCcLibraryTestCase(t, Bp2buildTestCase{
536 ModuleTypeUnderTest: "cc_library",
537 ModuleTypeUnderTestFactory: cc.LibraryFactory,
538 Dir: "foo/bar",
539 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400540 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000561 Blueprint: soongCcLibraryPreamble,
562 ExpectedBazelTargets: []string{
563 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500564 "whole_archive_deps": `[
565 ":whole_static_lib_for_both_alwayslink",
566 ":whole_static_lib_for_static_alwayslink",
567 ]`,
568 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000569 makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500570 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000581 runCcLibraryTestCase(t, Bp2buildTestCase{
582 Description: "cc_library shared/static props in arch",
583 ModuleTypeUnderTest: "cc_library",
584 ModuleTypeUnderTestFactory: cc.LibraryFactory,
585 Dir: "foo/bar",
586 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000587 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000655 Blueprint: soongCcLibraryPreamble,
656 ExpectedBazelTargets: []string{
657 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500658 "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 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000681 makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500682 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000731 runCcLibraryTestCase(t, Bp2buildTestCase{
732 Description: "cc_library shared/static props with c/cpp/s mixed sources",
733 ModuleTypeUnderTest: "cc_library",
734 ModuleTypeUnderTestFactory: cc.LibraryFactory,
735 Dir: "foo/bar",
736 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000737 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000808 Blueprint: soongCcLibraryPreamble,
809 ExpectedBazelTargets: []string{
810 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 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000835 makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500836 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000863 runCcLibraryTestCase(t, Bp2buildTestCase{
864 Description: "cc_library non-configured version script",
865 ModuleTypeUnderTest: "cc_library",
866 ModuleTypeUnderTestFactory: cc.LibraryFactory,
867 Dir: "foo/bar",
868 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200869 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000879 Blueprint: soongCcLibraryPreamble,
880 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500881 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000890 runCcLibraryTestCase(t, Bp2buildTestCase{
891 Description: "cc_library configured version script",
892 ModuleTypeUnderTest: "cc_library",
893 ModuleTypeUnderTestFactory: cc.LibraryFactory,
894 Dir: "foo/bar",
895 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200896 "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 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000914 Blueprint: soongCcLibraryPreamble,
915 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500916 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000933 runCcLibraryTestCase(t, Bp2buildTestCase{
934 Description: "cc_library shared_libs",
935 ModuleTypeUnderTest: "cc_library",
936 ModuleTypeUnderTestFactory: cc.LibraryFactory,
937 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000949 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500950 "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{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000958 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500959 "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 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000966 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 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000976 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500977 "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
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000987 runCcLibraryTestCase(t, Bp2buildTestCase{
988 Description: "cc_library pack_relocations test",
989 ModuleTypeUnderTest: "cc_library",
990 ModuleTypeUnderTestFactory: cc.LibraryFactory,
991 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001025 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001026 })
1027}
1028
1029func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001030 runCcLibraryTestCase(t, Bp2buildTestCase{
1031 Description: "cc_library spaces in copts",
1032 ModuleTypeUnderTest: "cc_library",
1033 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1034 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001042 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001052 runCcLibraryTestCase(t, Bp2buildTestCase{
1053 Description: "cc_library cppflags usage",
1054 ModuleTypeUnderTest: "cc_library",
1055 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1056 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001077 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001078 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001096 runCcLibraryTestCase(t, Bp2buildTestCase{
1097 ModuleTypeUnderTest: "cc_library",
1098 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1099 Filesystem: map[string]string{},
1100 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001101cc_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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001174 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001175 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001203 runCcLibraryTestCase(t, Bp2buildTestCase{
1204 Description: "cc_library - nocrt: true emits attribute",
1205 ModuleTypeUnderTest: "cc_library",
1206 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1207 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001208 "impl.cpp": "",
1209 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001210 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001211cc_library {
1212 name: "foo-lib",
1213 srcs: ["impl.cpp"],
1214 nocrt: true,
1215 include_build_directory: false,
1216}
1217`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001218 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001219 "link_crt": `False`,
1220 "srcs": `["impl.cpp"]`,
1221 }),
1222 },
1223 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001224}
1225
1226func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001227 runCcLibraryTestCase(t, Bp2buildTestCase{
1228 Description: "cc_library - nocrt: false - does not emit attribute",
1229 ModuleTypeUnderTest: "cc_library",
1230 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1231 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001232 "impl.cpp": "",
1233 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001234 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001235cc_library {
1236 name: "foo-lib",
1237 srcs: ["impl.cpp"],
1238 nocrt: false,
1239 include_build_directory: false,
1240}
1241`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001242 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001243 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001249 runCcLibraryTestCase(t, Bp2buildTestCase{
1250 Description: "cc_library - nocrt in select",
1251 ModuleTypeUnderTest: "cc_library",
1252 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1253 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001254 "impl.cpp": "",
1255 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001256 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001257cc_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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001271 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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001276 runCcLibraryTestCase(t, Bp2buildTestCase{
1277 ModuleTypeUnderTest: "cc_library",
1278 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1279 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001280 "impl.cpp": "",
1281 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001282 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`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001290 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001291 "srcs": `["impl.cpp"]`,
1292 "use_libcrt": `False`,
1293 }),
1294 })
1295}
1296
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001297func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001298 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,
Yu Liu75be7b92022-02-01 09:54:09 -08001304 "inject_bssl_hash": true,
Wei Li81852ca2022-07-27 00:22:06 -07001305 "has_stubs": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001306 }
Wei Li81852ca2022-07-27 00:22:06 -07001307
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001308 sharedAttrs := AttrNameToString{}
1309 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001310 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
Wei Li81852ca2022-07-27 00:22:06 -07001324func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
1325 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
1326 return ""
1327 }
1328 STUB_SUITE_ATTRS := map[string]string{
1329 "stubs_symbol_file": "symbol_file",
1330 "stubs_versions": "versions",
1331 "soname": "soname",
1332 "source_library": "source_library",
1333 }
1334
1335 stubSuiteAttrs := AttrNameToString{}
1336 for key, _ := range attrs {
1337 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
1338 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
1339 }
1340 }
1341 return makeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
1342}
1343
Jingwen Chen6ada5892021-09-17 11:38:09 +00001344func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001345 runCcLibraryTestCase(t, Bp2buildTestCase{
1346 ModuleTypeUnderTest: "cc_library",
1347 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1348 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001349 "impl.cpp": "",
1350 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001352cc_library {
1353 name: "foo-lib",
1354 srcs: ["impl.cpp"],
1355 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001356 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001357}
1358`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001359 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001360 "srcs": `["impl.cpp"]`,
1361 "use_libcrt": `True`,
1362 }),
1363 })
Liz Kammerd366c902021-06-03 13:43:01 -04001364}
1365
Jingwen Chen6ada5892021-09-17 11:38:09 +00001366func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001367 runCcLibraryTestCase(t, Bp2buildTestCase{
1368 ModuleTypeUnderTest: "cc_library",
1369 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1370 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001371 "impl.cpp": "",
1372 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001373 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001374cc_library {
1375 name: "foo-lib",
1376 srcs: ["impl.cpp"],
1377 arch: {
1378 arm: {
1379 no_libcrt: true,
1380 },
1381 x86: {
1382 no_libcrt: true,
1383 },
1384 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001385 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001386}
1387`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001388 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001389 "srcs": `["impl.cpp"]`,
1390 "use_libcrt": `select({
Liz Kammerd366c902021-06-03 13:43:01 -04001391 "//build/bazel/platforms/arch:arm": False,
1392 "//build/bazel/platforms/arch:x86": False,
1393 "//conditions:default": None,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001394 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001395 }),
1396 })
Liz Kammerd366c902021-06-03 13:43:01 -04001397}
1398
Chris Parsons58852a02021-12-09 18:10:18 -05001399func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001400 runCcLibraryTestCase(t, Bp2buildTestCase{
1401 ModuleTypeUnderTest: "cc_library",
1402 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1403 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001404 "impl.cpp": "",
1405 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001407cc_library {
1408 name: "foo-lib",
1409 srcs: ["impl.cpp"],
1410 arch: {
1411 arm: {
1412 no_libcrt: true,
1413 },
1414 x86: {
1415 no_libcrt: true,
1416 },
1417 },
1418 target: {
1419 darwin: {
1420 no_libcrt: true,
1421 }
1422 },
1423 include_build_directory: false,
1424}
1425`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001426 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001427 "srcs": `["impl.cpp"]`,
1428 "use_libcrt": `select({
1429 "//build/bazel/platforms/os_arch:android_arm": False,
1430 "//build/bazel/platforms/os_arch:android_x86": False,
1431 "//build/bazel/platforms/os_arch:darwin_arm64": False,
1432 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1433 "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
1434 "//build/bazel/platforms/os_arch:linux_musl_x86": False,
1435 "//build/bazel/platforms/os_arch:windows_x86": False,
1436 "//conditions:default": None,
1437 })`,
1438 }),
1439 })
1440}
1441
1442func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001443 runCcLibraryTestCase(t, Bp2buildTestCase{
1444 ModuleTypeUnderTest: "cc_library",
1445 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1446 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001447 "impl.cpp": "",
1448 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001449 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001450cc_library {
1451 name: "foo-lib",
1452 srcs: ["impl.cpp"],
1453 arch: {
1454 arm: {
1455 no_libcrt: true,
1456 },
1457 // This is expected to override the value for darwin_x86_64.
1458 x86_64: {
1459 no_libcrt: true,
1460 },
1461 },
1462 target: {
1463 darwin: {
1464 no_libcrt: false,
1465 }
1466 },
1467 include_build_directory: false,
1468}
1469`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001470 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001471 "srcs": `["impl.cpp"]`,
1472 "use_libcrt": `select({
1473 "//build/bazel/platforms/os_arch:android_arm": False,
1474 "//build/bazel/platforms/os_arch:android_x86_64": False,
1475 "//build/bazel/platforms/os_arch:darwin_arm64": True,
1476 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1477 "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
1478 "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
1479 "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
1480 "//build/bazel/platforms/os_arch:windows_x86_64": False,
1481 "//conditions:default": None,
1482 })`,
1483 }),
1484 })
1485}
1486
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001487func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001488 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001489 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001490 "strip": `{
1491 "all": True,
1492 }`,
1493 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001494 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001495 "strip": `{
1496 "keep_symbols": True,
1497 }`,
1498 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001499 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001500 "strip": `{
1501 "keep_symbols_and_debug_frame": True,
1502 }`,
1503 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001504 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001505 "strip": `{
1506 "keep_symbols_list": ["symbol"],
1507 }`,
1508 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001509 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001510 "strip": `{
1511 "none": True,
1512 }`,
1513 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001514 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001515
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001516 runCcLibraryTestCase(t, Bp2buildTestCase{
1517 Description: "cc_library strip args",
1518 ModuleTypeUnderTest: "cc_library",
1519 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1520 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001521cc_library {
1522 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001523 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001524}
1525cc_library {
1526 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001527 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001528 keep_symbols: true,
1529 },
1530 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001531}
1532cc_library {
1533 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001534 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001535 keep_symbols_and_debug_frame: true,
1536 },
1537 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001538}
1539cc_library {
1540 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001541 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001542 none: true,
1543 },
1544 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001545}
1546cc_library {
1547 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001548 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001549 keep_symbols_list: ["symbol"],
1550 },
1551 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001552}
1553cc_library {
1554 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001555 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001556 all: true,
1557 },
1558 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001559}
1560`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001561 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001562 })
1563}
1564
1565func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001566 runCcLibraryTestCase(t, Bp2buildTestCase{
1567 Description: "cc_library strip args",
1568 ModuleTypeUnderTest: "cc_library",
1569 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1570 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001571cc_library {
1572 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001573 target: {
1574 darwin: {
1575 strip: {
1576 keep_symbols_list: ["foo", "bar"]
1577 }
1578 },
1579 },
1580 arch: {
1581 arm: {
1582 strip: {
1583 keep_symbols_and_debug_frame: true,
1584 },
1585 },
1586 arm64: {
1587 strip: {
1588 keep_symbols: true,
1589 },
1590 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001591 },
1592 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001593}
1594`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001595 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001596 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001597 "keep_symbols": select({
1598 "//build/bazel/platforms/arch:arm64": True,
1599 "//conditions:default": None,
1600 }),
1601 "keep_symbols_and_debug_frame": select({
1602 "//build/bazel/platforms/arch:arm": True,
1603 "//conditions:default": None,
1604 }),
1605 "keep_symbols_list": select({
1606 "//build/bazel/platforms/os:darwin": [
1607 "foo",
1608 "bar",
1609 ],
1610 "//conditions:default": [],
1611 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001612 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001613 }),
1614 },
1615 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001616}
Chris Parsons51f8c392021-08-03 21:01:05 -04001617
1618func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001619 runCcLibraryTestCase(t, Bp2buildTestCase{
1620 Description: "cc_library system_shared_libs empty at root",
1621 ModuleTypeUnderTest: "cc_library",
1622 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1623 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001624cc_library {
1625 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001626 system_shared_libs: [],
1627 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001628}
1629`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001630 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001631 "system_dynamic_deps": `[]`,
1632 }),
1633 },
1634 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001635}
1636
1637func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001638 runCcLibraryTestCase(t, Bp2buildTestCase{
1639 Description: "cc_library system_shared_libs empty for static variant",
1640 ModuleTypeUnderTest: "cc_library",
1641 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1642 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001643cc_library {
1644 name: "static_empty",
1645 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001646 system_shared_libs: [],
1647 },
1648 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001649}
1650`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001651 ExpectedBazelTargets: []string{
1652 makeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001653 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001654 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001655 makeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001656 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001657 })
1658}
1659
1660func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001661 runCcLibraryTestCase(t, Bp2buildTestCase{
1662 Description: "cc_library system_shared_libs empty for shared variant",
1663 ModuleTypeUnderTest: "cc_library",
1664 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1665 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001666cc_library {
1667 name: "shared_empty",
1668 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001669 system_shared_libs: [],
1670 },
1671 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001672}
1673`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001674 ExpectedBazelTargets: []string{
1675 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1676 makeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001677 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001678 }),
1679 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001680 })
1681}
1682
1683func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001684 runCcLibraryTestCase(t, Bp2buildTestCase{
1685 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1686 ModuleTypeUnderTest: "cc_library",
1687 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1688 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001689cc_library {
1690 name: "shared_empty",
1691 target: {
1692 bionic: {
1693 shared: {
1694 system_shared_libs: [],
1695 }
1696 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001697 },
1698 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001699}
1700`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001701 ExpectedBazelTargets: []string{
1702 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1703 makeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001704 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001705 }),
1706 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001707 })
1708}
1709
1710func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1711 // Note that this behavior is technically incorrect (it's a simplification).
1712 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1713 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1714 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001715 runCcLibraryTestCase(t, Bp2buildTestCase{
1716 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1717 ModuleTypeUnderTest: "cc_library",
1718 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1719 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001720cc_library {
1721 name: "target_linux_bionic_empty",
1722 target: {
1723 linux_bionic: {
1724 system_shared_libs: [],
1725 },
1726 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001727 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001728}
1729`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001730 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001731 "system_dynamic_deps": `[]`,
1732 }),
1733 },
1734 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001735}
1736
1737func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001738 runCcLibraryTestCase(t, Bp2buildTestCase{
1739 Description: "cc_library system_shared_libs empty for bionic variant",
1740 ModuleTypeUnderTest: "cc_library",
1741 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1742 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001743cc_library {
1744 name: "target_bionic_empty",
1745 target: {
1746 bionic: {
1747 system_shared_libs: [],
1748 },
1749 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001750 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001751}
1752`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001753 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001754 "system_dynamic_deps": `[]`,
1755 }),
1756 },
1757 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001758}
1759
1760func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001761 runCcLibraryTestCase(t, Bp2buildTestCase{
1762 Description: "cc_library system_shared_libs set for shared and root",
1763 ModuleTypeUnderTest: "cc_library",
1764 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1765 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001766cc_library {
1767 name: "libc",
1768 bazel_module: { bp2build_available: false },
1769}
1770cc_library {
1771 name: "libm",
1772 bazel_module: { bp2build_available: false },
1773}
Chris Parsons51f8c392021-08-03 21:01:05 -04001774
1775cc_library {
1776 name: "foo",
1777 system_shared_libs: ["libc"],
1778 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001779 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001780 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001781 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001782}
1783`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001784 ExpectedBazelTargets: []string{
1785 makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001786 "system_dynamic_deps": `[":libc"]`,
1787 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001788 makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001789 "system_dynamic_deps": `[
1790 ":libc",
1791 ":libm",
1792 ]`,
1793 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001794 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001795 })
1796}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001797
1798func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001799 runCcLibraryTestCase(t, Bp2buildTestCase{
1800 Description: "cc_library - selects for all os targets",
1801 ModuleTypeUnderTest: "cc_library",
1802 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1803 Filesystem: map[string]string{},
1804 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001805cc_library {
1806 name: "foo-lib",
1807 srcs: ["base.cpp"],
1808 target: {
1809 android: {
1810 srcs: ["android.cpp"],
1811 },
1812 linux: {
1813 srcs: ["linux.cpp"],
1814 },
1815 linux_glibc: {
1816 srcs: ["linux_glibc.cpp"],
1817 },
1818 darwin: {
1819 srcs: ["darwin.cpp"],
1820 },
1821 bionic: {
1822 srcs: ["bionic.cpp"],
1823 },
1824 linux_musl: {
1825 srcs: ["linux_musl.cpp"],
1826 },
1827 windows: {
1828 srcs: ["windows.cpp"],
1829 },
1830 },
1831 include_build_directory: false,
1832}
1833`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001834 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001835 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001836 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001837 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001838 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001839 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001840 ],
1841 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
1842 "//build/bazel/platforms/os:linux": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001843 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001844 "linux_glibc.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001845 ],
1846 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001847 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001848 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001849 ],
1850 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001851 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001852 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001853 ],
1854 "//build/bazel/platforms/os:windows": ["windows.cpp"],
1855 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001856 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001857 }),
1858 },
1859 )
Jingwen Chen97b85312021-10-08 10:41:31 +00001860}
1861
Yu Liu75be7b92022-02-01 09:54:09 -08001862func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001863 runCcLibraryTestCase(t, Bp2buildTestCase{
1864 Description: "cc_library - libcrypto hash injection",
1865 ModuleTypeUnderTest: "cc_library",
1866 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1867 Filesystem: map[string]string{},
1868 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08001869cc_library {
1870 name: "libcrypto",
1871 target: {
1872 android: {
1873 inject_bssl_hash: true,
1874 },
1875 },
1876 include_build_directory: false,
1877}
1878`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001879 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08001880 "inject_bssl_hash": `select({
1881 "//build/bazel/platforms/os:android": True,
1882 "//conditions:default": None,
1883 })`,
1884 }),
1885 },
1886 )
1887}
1888
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001889func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00001890 type testCase struct {
1891 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001892 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001893 gnu_extensions string
1894 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001895 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001896 }
1897
1898 testCases := []testCase{
1899 // Existing usages of cpp_std in AOSP are:
1900 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
1901 //
1902 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
1903 // is set in the toolchain.)
1904 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04001905 {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001906 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
1907 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04001908 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
1909 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
1910 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001911 // Explicitly setting a c++ std does not use replace gnu++ std even if
1912 // gnu_extensions is true.
1913 // "c++11",
1914 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001915 {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001916 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
1917 // "c++17",
1918 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001919 {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001920 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
1921 // "c++2a",
1922 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001923 {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001924 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
1925 // "c++98",
1926 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001927 {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001928 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
1929 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
1930 // "gnu++11",
1931 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001932 {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001933 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
1934 // "gnu++17",
1935 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04001936 {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001937 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001938
1939 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04001940 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
1941 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
1942 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001943 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
1944 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
1945 {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 +00001946 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001947 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04001948 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
1949 t.Run(name, func(t *testing.T) {
1950 name_prefix := fmt.Sprintf("a_%v", i)
1951 cppStdProp := ""
1952 if tc.cpp_std != "" {
1953 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
1954 }
1955 cStdProp := ""
1956 if tc.c_std != "" {
1957 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
1958 }
1959 gnuExtensionsProp := ""
1960 if tc.gnu_extensions != "" {
1961 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
1962 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001963 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04001964 if tc.bazel_cpp_std != "" {
1965 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
1966 }
1967 if tc.bazel_c_std != "" {
1968 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
1969 }
Jingwen Chen97b85312021-10-08 10:41:31 +00001970
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001971 runCcLibraryTestCase(t, Bp2buildTestCase{
1972 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04001973 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001974 ModuleTypeUnderTest: "cc_library",
1975 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1976 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00001977cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001978 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00001979%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001980%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00001981%s // gnu_extensions: *bool
1982 include_build_directory: false,
1983}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001984`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001985 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04001986 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001987
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001988 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
1989 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04001990 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001991 ModuleTypeUnderTest: "cc_library_static",
1992 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
1993 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001994cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001995 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001996%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001997%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001998%s // gnu_extensions: *bool
1999 include_build_directory: false,
2000}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002001`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002002 ExpectedBazelTargets: []string{
Liz Kammera5a29de2022-05-25 23:19:37 -04002003 makeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
2004 },
2005 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002006
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002007 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2008 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002009 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002010 ModuleTypeUnderTest: "cc_library_shared",
2011 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2012 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002013cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002014 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002015%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002016%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002017%s // gnu_extensions: *bool
2018 include_build_directory: false,
2019}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002020`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002021 ExpectedBazelTargets: []string{
Liz Kammera5a29de2022-05-25 23:19:37 -04002022 makeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
2023 },
2024 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002025 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002026 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002027}
Liz Kammer12615db2021-09-28 09:19:17 -04002028
2029func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002030 runCcLibraryTestCase(t, Bp2buildTestCase{
2031 ModuleTypeUnderTest: "cc_library",
2032 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2033 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002034 name: "foo",
2035 srcs: ["foo.proto"],
2036 include_build_directory: false,
2037}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002038 ExpectedBazelTargets: []string{
2039 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002040 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002041 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002042 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002043 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002044 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002045 "deps": `[":libprotobuf-cpp-lite"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002046 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002047 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002048 }),
2049 },
2050 })
2051}
2052
2053func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002054 runCcLibraryTestCase(t, Bp2buildTestCase{
2055 ModuleTypeUnderTest: "cc_library",
2056 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2057 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002058 name: "foo",
2059 srcs: ["foo.proto"],
2060 proto: { canonical_path_from_root: false},
2061 include_build_directory: false,
2062}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002063 ExpectedBazelTargets: []string{
2064 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002065 "srcs": `["foo.proto"]`,
2066 "strip_import_prefix": `""`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002067 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002068 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002069 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002070 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002071 "deps": `[":libprotobuf-cpp-lite"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002072 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002073 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002074 }),
2075 },
2076 })
2077}
2078
2079func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002080 runCcLibraryTestCase(t, Bp2buildTestCase{
2081 ModuleTypeUnderTest: "cc_library",
2082 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2083 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002084 name: "foo",
2085 srcs: ["foo.proto"],
2086 proto: { canonical_path_from_root: true},
2087 include_build_directory: false,
2088}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002089 ExpectedBazelTargets: []string{
2090 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002091 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002092 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002093 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002094 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002095 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002096 "deps": `[":libprotobuf-cpp-lite"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002097 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002098 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002099 }),
2100 },
2101 })
2102}
2103
2104func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002105 runCcLibraryTestCase(t, Bp2buildTestCase{
2106 ModuleTypeUnderTest: "cc_library",
2107 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2108 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002109 name: "foo",
2110 srcs: ["foo.proto"],
2111 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002112 type: "full",
2113 },
2114 include_build_directory: false,
2115}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002116 ExpectedBazelTargets: []string{
2117 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002118 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002119 }), makeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002120 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002121 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002122 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002123 "deps": `[":libprotobuf-cpp-full"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002124 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002125 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002126 }),
2127 },
2128 })
2129}
2130
2131func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002132 runCcLibraryTestCase(t, Bp2buildTestCase{
2133 ModuleTypeUnderTest: "cc_library",
2134 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2135 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002136 name: "foo",
2137 srcs: ["foo.proto"],
2138 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002139 type: "lite",
2140 },
2141 include_build_directory: false,
2142}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002143 ExpectedBazelTargets: []string{
2144 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002145 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002146 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002147 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002148 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002149 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002150 "deps": `[":libprotobuf-cpp-lite"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002151 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002152 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002153 }),
2154 },
2155 })
2156}
2157
2158func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002159 runCcLibraryTestCase(t, Bp2buildTestCase{
2160 ModuleTypeUnderTest: "cc_library",
2161 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2162 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002163 name: "foo",
2164 srcs: ["foo.proto"],
2165 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002166 export_proto_headers: true,
2167 },
2168 include_build_directory: false,
2169}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002170 ExpectedBazelTargets: []string{
2171 makeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002172 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002173 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002174 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002175 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002176 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002177 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002178 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002179 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2180 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002181 }),
2182 },
2183 })
2184}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002185
2186func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002187 runCcLibraryTestCase(t, Bp2buildTestCase{
2188 ModuleTypeUnderTest: "cc_library",
2189 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2190 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002191 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2192 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2193 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2194 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2195cc_library {
2196 name: "a",
2197 srcs: [":a_fg_proto"],
2198 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002199 export_proto_headers: true,
2200 },
2201 include_build_directory: false,
2202}
2203
2204cc_library {
2205 name: "b",
2206 srcs: [":b_protos"],
2207 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002208 export_proto_headers: true,
2209 },
2210 include_build_directory: false,
2211}
2212
2213cc_library {
2214 name: "c",
2215 srcs: [":c-proto-srcs"],
2216 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002217 export_proto_headers: true,
2218 },
2219 include_build_directory: false,
2220}
2221
2222cc_library {
2223 name: "d",
2224 srcs: [":proto-srcs-d"],
2225 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002226 export_proto_headers: true,
2227 },
2228 include_build_directory: false,
2229}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002230 ExpectedBazelTargets: []string{
2231 makeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002232 "srcs": `[":a_fg_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002233 }), makeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002234 "deps": `[":a_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002235 }), makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002236 "deps": `[":libprotobuf-cpp-lite"]`,
2237 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2238 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2239 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2240 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002241 }), makeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002242 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2243 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2244 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2245 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2246 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002247 }), makeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002248 "srcs": `[":b_protos"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002249 }), makeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002250 "deps": `[":b_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002251 }), makeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002252 "deps": `[":libprotobuf-cpp-lite"]`,
2253 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2254 "srcs": `[":b_protos_cpp_srcs"]`,
2255 "srcs_as": `[":b_protos_as_srcs"]`,
2256 "srcs_c": `[":b_protos_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002257 }), makeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002258 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2259 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2260 "srcs": `[":b_protos_cpp_srcs"]`,
2261 "srcs_as": `[":b_protos_as_srcs"]`,
2262 "srcs_c": `[":b_protos_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002263 }), makeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002264 "srcs": `[":c-proto-srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002265 }), makeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002266 "deps": `[":c_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002267 }), makeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002268 "deps": `[":libprotobuf-cpp-lite"]`,
2269 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2270 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2271 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2272 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002273 }), makeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002274 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2275 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2276 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2277 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2278 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002279 }), makeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002280 "srcs": `[":proto-srcs-d"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002281 }), makeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002282 "deps": `[":d_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002283 }), makeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002284 "deps": `[":libprotobuf-cpp-lite"]`,
2285 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2286 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2287 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2288 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002289 }), makeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002290 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2291 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2292 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2293 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2294 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2295 }),
2296 },
2297 })
2298}
Chris Parsons58852a02021-12-09 18:10:18 -05002299
2300func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002301 runCcLibraryTestCase(t, Bp2buildTestCase{
2302 ModuleTypeUnderTest: "cc_library",
2303 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2304 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002305 name: "foo",
2306 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002307 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002308 target: {
2309 darwin: {
2310 enabled: false,
2311 },
2312 windows: {
2313 enabled: false,
2314 },
2315 linux_glibc_x86: {
2316 enabled: false,
2317 },
2318 },
2319 include_build_directory: false,
2320}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002321 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002322 "srcs": `["foo.cpp"]`,
2323 "target_compatible_with": `select({
2324 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2325 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2326 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2327 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2328 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2329 "//conditions:default": [],
2330 })`,
2331 }),
2332 })
2333}
2334
2335func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002336 runCcLibraryTestCase(t, Bp2buildTestCase{
2337 ModuleTypeUnderTest: "cc_library",
2338 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2339 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002340 name: "foo",
2341 srcs: ["foo.cpp"],
2342 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002343 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002344 target: {
2345 darwin: {
2346 enabled: true,
2347 },
2348 windows: {
2349 enabled: false,
2350 },
2351 linux_glibc_x86: {
2352 enabled: false,
2353 },
2354 },
2355 include_build_directory: false,
2356}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002357 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002358 "srcs": `["foo.cpp"]`,
2359 "target_compatible_with": `select({
2360 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2361 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2362 "//conditions:default": ["@platforms//:incompatible"],
2363 })`,
2364 }),
2365 })
2366}
2367
2368func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002369 runCcLibraryTestCase(t, Bp2buildTestCase{
2370 ModuleTypeUnderTest: "cc_library",
2371 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2372 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002373 name: "foo",
2374 srcs: ["foo.cpp"],
2375 enabled: false,
2376 shared: {
2377 enabled: true,
2378 },
2379 target: {
2380 android: {
2381 shared: {
2382 enabled: false,
2383 },
2384 }
2385 },
2386 include_build_directory: false,
2387}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002388 ExpectedBazelTargets: []string{makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002389 "srcs": `["foo.cpp"]`,
2390 "target_compatible_with": `["@platforms//:incompatible"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002391 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002392 "srcs": `["foo.cpp"]`,
2393 "target_compatible_with": `select({
2394 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2395 "//conditions:default": [],
2396 })`,
2397 }),
2398 },
2399 })
2400}
2401
2402func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002403 runCcLibraryTestCase(t, Bp2buildTestCase{
2404 ModuleTypeUnderTest: "cc_library",
2405 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2406 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002407 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002408 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002409 srcs: ["foo.cpp"],
2410 shared: {
2411 enabled: false
2412 },
2413 target: {
2414 darwin: {
2415 enabled: true,
2416 },
2417 windows: {
2418 enabled: false,
2419 },
2420 linux_glibc_x86: {
2421 shared: {
2422 enabled: true,
2423 },
2424 },
2425 },
2426 include_build_directory: false,
2427}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002428 ExpectedBazelTargets: []string{makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002429 "srcs": `["foo.cpp"]`,
2430 "target_compatible_with": `select({
2431 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2432 "//conditions:default": [],
2433 })`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002434 }), makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002435 "srcs": `["foo.cpp"]`,
2436 "target_compatible_with": `select({
2437 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2438 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2439 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2440 "//conditions:default": ["@platforms//:incompatible"],
2441 })`,
2442 }),
2443 }})
2444}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002445
2446func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002447 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
2448 "has_stubs": `True`,
2449 })
2450 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
2451 "soname": `"a.so"`,
2452 "source_library": `":a"`,
2453 "stubs_symbol_file": `"a.map.txt"`,
2454 "stubs_versions": `[
2455 "28",
2456 "29",
2457 "current",
2458 ]`,
2459 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002460 runCcLibraryTestCase(t, Bp2buildTestCase{
2461 Description: "cc_library stubs",
2462 ModuleTypeUnderTest: "cc_library",
2463 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2464 Dir: "foo/bar",
2465 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002466 "foo/bar/Android.bp": `
2467cc_library {
2468 name: "a",
2469 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2470 bazel_module: { bp2build_available: true },
2471 include_build_directory: false,
2472}
2473`,
2474 },
Wei Li81852ca2022-07-27 00:22:06 -07002475 Blueprint: soongCcLibraryPreamble,
2476 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002477 },
2478 )
2479}
Liz Kammerf38a8372022-02-04 15:39:00 -05002480
2481func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002482 runCcLibraryTestCase(t, Bp2buildTestCase{
2483 ModuleTypeUnderTest: "cc_library",
2484 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2485 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05002486 name: "foo",
2487 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2488 include_build_directory: false,
2489}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002490 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05002491 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2492 }),
2493 })
2494}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002495
2496func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002497 runCcLibraryTestCase(t, Bp2buildTestCase{
2498 ModuleTypeUnderTest: "cc_library",
2499 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2500 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002501 "foo.c": "",
2502 "bar.cc": "",
2503 "foo1.l": "",
2504 "bar1.ll": "",
2505 "foo2.l": "",
2506 "bar2.ll": "",
2507 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002508 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002509 name: "foo_lib",
2510 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2511 lex: { flags: ["--foo_flags"] },
2512 include_build_directory: false,
2513 bazel_module: { bp2build_available: true },
2514}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002515 ExpectedBazelTargets: append([]string{
2516 makeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002517 "srcs": `[
2518 "foo1.l",
2519 "foo2.l",
2520 ]`,
2521 "lexopts": `["--foo_flags"]`,
2522 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002523 makeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002524 "srcs": `[
2525 "bar1.ll",
2526 "bar2.ll",
2527 ]`,
2528 "lexopts": `["--foo_flags"]`,
2529 }),
2530 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002531 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002532 "srcs": `[
2533 "bar.cc",
2534 ":foo_lib_genlex_ll",
2535 ]`,
2536 "srcs_c": `[
2537 "foo.c",
2538 ":foo_lib_genlex_l",
2539 ]`,
2540 })...),
2541 })
2542}
Cole Faust6b29f592022-08-09 09:50:56 -07002543
2544func TestCCLibraryRuntimeDeps(t *testing.T) {
2545 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2546 Blueprint: `cc_library_shared {
2547 name: "bar",
2548}
2549
2550cc_library {
2551 name: "foo",
2552 runtime_libs: ["foo"],
2553}`,
2554 ExpectedBazelTargets: []string{
2555 makeBazelTarget("cc_library_shared", "bar", AttrNameToString{
2556 "local_includes": `["."]`,
2557 }),
2558 makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2559 "runtime_deps": `[":foo"]`,
2560 "local_includes": `["."]`,
2561 }),
2562 makeBazelTarget("cc_library_shared", "foo", AttrNameToString{
2563 "runtime_deps": `[":foo"]`,
2564 "local_includes": `["."]`,
2565 }),
2566 },
2567 })
2568}
Cole Faust5fa4e962022-08-22 14:31:04 -07002569
2570func TestCcLibraryWithInstructionSet(t *testing.T) {
2571 runCcLibraryTestCase(t, Bp2buildTestCase{
2572 ModuleTypeUnderTest: "cc_library",
2573 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2574 Blueprint: `cc_library {
2575 name: "foo",
2576 arch: {
2577 arm: {
2578 instruction_set: "arm",
2579 }
2580 }
2581}
2582`,
2583 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
2584 "features": `select({
2585 "//build/bazel/platforms/arch:arm": [
2586 "arm_isa_arm",
2587 "-arm_isa_thumb",
2588 ],
2589 "//conditions:default": [],
2590 })`,
2591 "local_includes": `["."]`,
2592 }),
2593 })
2594}