blob: 76dc5904bd45a87655a45e530b1593fe377560f9 [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
Vinh Tran367d89d2023-04-28 11:21:25 -040021 "android/soong/aidl_library"
Jingwen Chen63930982021-03-24 10:04:33 -040022 "android/soong/android"
23 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040024)
25
26const (
27 // See cc/testing.go for more context
28 soongCcLibraryPreamble = `
29cc_defaults {
Liz Kammer8337ea42021-09-10 10:06:32 -040030 name: "linux_bionic_supported",
Liz Kammerbaced712022-09-16 09:01:29 -040031}
32`
33
34 soongCcVersionLibBpPath = "build/soong/cc/libbuildversion/Android.bp"
35 soongCcVersionLibBp = `
36cc_library_static {
37 name: "libbuildversion",
38 bazel_module: { bp2build_available: false },
39}
40`
Liz Kammer12615db2021-09-28 09:19:17 -040041
42 soongCcProtoLibraries = `
43cc_library {
44 name: "libprotobuf-cpp-lite",
45 bazel_module: { bp2build_available: false },
46}
47
48cc_library {
49 name: "libprotobuf-cpp-full",
50 bazel_module: { bp2build_available: false },
51}`
52
53 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040054)
55
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040057 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059}
60
61func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
62 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000063 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040065 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020066 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Vinh Tran367d89d2023-04-28 11:21:25 -040067 ctx.RegisterModuleType("aidl_library", aidl_library.AidlLibraryFactory)
Spandan Das63acae92023-09-14 22:34:34 +000068 ctx.RegisterModuleType("ndk_library", cc.NdkLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020069}
70
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020071func TestCcLibrarySimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000072 runCcLibraryTestCase(t, Bp2buildTestCase{
73 Description: "cc_library - simple example",
74 ModuleTypeUnderTest: "cc_library",
75 ModuleTypeUnderTestFactory: cc.LibraryFactory,
76 Filesystem: map[string]string{
Liz Kammerbaced712022-09-16 09:01:29 -040077 soongCcVersionLibBpPath: soongCcVersionLibBp,
78 "android.cpp": "",
79 "bionic.cpp": "",
80 "darwin.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020081 // Refer to cc.headerExts for the supported header extensions in Soong.
82 "header.h": "",
83 "header.hh": "",
84 "header.hpp": "",
85 "header.hxx": "",
86 "header.h++": "",
87 "header.inl": "",
88 "header.inc": "",
89 "header.ipp": "",
90 "header.h.generic": "",
91 "impl.cpp": "",
92 "linux.cpp": "",
93 "x86.cpp": "",
94 "x86_64.cpp": "",
95 "foo-dir/a.h": "",
96 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000097 Blueprint: soongCcLibraryPreamble +
Sam Delmerico130d75b2023-08-31 00:51:44 +000098 SimpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -040099cc_library {
100 name: "foo-lib",
101 srcs: ["impl.cpp"],
102 cflags: ["-Wall"],
103 header_libs: ["some-headers"],
104 export_include_dirs: ["foo-dir"],
105 ldflags: ["-Wl,--exclude-libs=bar.a"],
106 arch: {
107 x86: {
108 ldflags: ["-Wl,--exclude-libs=baz.a"],
109 srcs: ["x86.cpp"],
110 },
111 x86_64: {
112 ldflags: ["-Wl,--exclude-libs=qux.a"],
113 srcs: ["x86_64.cpp"],
114 },
115 },
116 target: {
117 android: {
118 srcs: ["android.cpp"],
119 },
120 linux_glibc: {
121 srcs: ["linux.cpp"],
122 },
123 darwin: {
124 srcs: ["darwin.cpp"],
125 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400126 bionic: {
127 srcs: ["bionic.cpp"]
128 },
Jingwen Chen63930982021-03-24 10:04:33 -0400129 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400130 include_build_directory: false,
Yu Liufc603162022-03-01 15:44:08 -0800131 sdk_version: "current",
132 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700133 use_version_lib: true,
Jingwen Chen63930982021-03-24 10:04:33 -0400134}
135`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000136 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500137 "copts": `["-Wall"]`,
138 "export_includes": `["foo-dir"]`,
139 "implementation_deps": `[":some-headers"]`,
140 "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000141 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
142 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
143 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500144 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500145 "srcs": `["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000146 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
147 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400148 "//conditions:default": [],
149 }) + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400150 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400151 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -0400152 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400153 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000154 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400155 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800156 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Liz Kammer01a16e82021-07-16 16:33:47 -0400157 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158 })`,
Yu Liufe978fd2023-04-24 16:37:18 -0700159 "sdk_version": `"current"`,
160 "min_sdk_version": `"29"`,
161 "use_version_lib": `True`,
162 "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500163 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500164 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200165}
166
167func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000168 runCcLibraryTestCase(t, Bp2buildTestCase{
169 Description: "cc_library - trimmed example of //bionic/linker:ld-android",
170 ModuleTypeUnderTest: "cc_library",
171 ModuleTypeUnderTestFactory: cc.LibraryFactory,
172 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200173 "ld-android.cpp": "",
174 "linked_list.h": "",
175 "linker.h": "",
176 "linker_block_allocator.h": "",
177 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400178 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000179 Blueprint: soongCcLibraryPreamble +
Sam Delmerico130d75b2023-08-31 00:51:44 +0000180 SimpleModuleDoNotConvertBp2build("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400181cc_library {
182 name: "fake-ld-android",
183 srcs: ["ld_android.cpp"],
184 cflags: [
185 "-Wall",
186 "-Wextra",
187 "-Wunused",
188 "-Werror",
189 ],
190 header_libs: ["libc_headers"],
191 ldflags: [
192 "-Wl,--exclude-libs=libgcc.a",
193 "-Wl,--exclude-libs=libgcc_stripped.a",
194 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
195 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
196 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
197 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
198 ],
199 arch: {
200 x86: {
201 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
202 },
203 x86_64: {
204 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
205 },
206 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400207 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400208}
209`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000210 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500211 "srcs": `["ld_android.cpp"]`,
212 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400213 "-Wall",
214 "-Wextra",
215 "-Wunused",
216 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500217 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500218 "implementation_deps": `[":libc_headers"]`,
219 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400220 "-Wl,--exclude-libs=libgcc.a",
221 "-Wl,--exclude-libs=libgcc_stripped.a",
222 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
226 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000227 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
228 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400229 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500231 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200232 })
233}
234
235func TestCcLibraryExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000236 runCcLibraryTestCase(t, Bp2buildTestCase{
237 Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
238 ModuleTypeUnderTest: "cc_library",
239 ModuleTypeUnderTestFactory: cc.LibraryFactory,
240 Dir: "external",
241 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200242 "external/math/cosf.c": "",
243 "external/math/erf.c": "",
244 "external/math/erf_data.c": "",
245 "external/math/erff.c": "",
246 "external/math/erff_data.c": "",
247 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000248cc_library {
249 name: "fake-libarm-optimized-routines-math",
250 exclude_srcs: [
251 // Provided by:
252 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
253 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
254 "math/erf.c",
255 "math/erf_data.c",
256 "math/erff.c",
257 "math/erff_data.c",
258 ],
259 srcs: [
260 "math/*.c",
261 ],
262 // arch-specific settings
263 arch: {
264 arm64: {
265 cflags: [
266 "-DHAVE_FAST_FMA=1",
267 ],
268 },
269 },
270 bazel_module: { bp2build_available: true },
271}
272`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200273 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000274 Blueprint: soongCcLibraryPreamble,
275 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500276 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000277 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
278 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500279 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500280 "local_includes": `["."]`,
281 "srcs_c": `["math/cosf.c"]`,
282 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200283 })
284}
285
286func TestCcLibrarySharedStaticProps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000287 runCcLibraryTestCase(t, Bp2buildTestCase{
288 Description: "cc_library shared/static props",
289 ModuleTypeUnderTest: "cc_library",
290 ModuleTypeUnderTestFactory: cc.LibraryFactory,
291 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400292 "both.cpp": "",
293 "sharedonly.cpp": "",
294 "staticonly.cpp": "",
295 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000296 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000297cc_library {
298 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400299 srcs: ["both.cpp"],
300 cflags: ["bothflag"],
301 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400302 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
303 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400304 static: {
305 srcs: ["staticonly.cpp"],
306 cflags: ["staticflag"],
307 shared_libs: ["shared_dep_for_static"],
308 static_libs: ["static_dep_for_static"],
309 whole_static_libs: ["whole_static_lib_for_static"],
310 },
311 shared: {
312 srcs: ["sharedonly.cpp"],
313 cflags: ["sharedflag"],
314 shared_libs: ["shared_dep_for_shared"],
315 static_libs: ["static_dep_for_shared"],
316 whole_static_libs: ["whole_static_lib_for_shared"],
317 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400318 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000319}
320
Liz Kammer8337ea42021-09-10 10:06:32 -0400321cc_library_static {
322 name: "static_dep_for_shared",
323 bazel_module: { bp2build_available: false },
324}
Chris Parsons08648312021-05-06 16:23:19 -0400325
Liz Kammer8337ea42021-09-10 10:06:32 -0400326cc_library_static {
327 name: "static_dep_for_static",
328 bazel_module: { bp2build_available: false },
329}
Chris Parsons08648312021-05-06 16:23:19 -0400330
Liz Kammer8337ea42021-09-10 10:06:32 -0400331cc_library_static {
332 name: "static_dep_for_both",
333 bazel_module: { bp2build_available: false },
334}
Chris Parsons08648312021-05-06 16:23:19 -0400335
Liz Kammer8337ea42021-09-10 10:06:32 -0400336cc_library_static {
337 name: "whole_static_lib_for_shared",
338 bazel_module: { bp2build_available: false },
339}
Chris Parsons08648312021-05-06 16:23:19 -0400340
Liz Kammer8337ea42021-09-10 10:06:32 -0400341cc_library_static {
342 name: "whole_static_lib_for_static",
343 bazel_module: { bp2build_available: false },
344}
Chris Parsons08648312021-05-06 16:23:19 -0400345
Liz Kammer8337ea42021-09-10 10:06:32 -0400346cc_library_static {
347 name: "whole_static_lib_for_both",
348 bazel_module: { bp2build_available: false },
349}
Chris Parsons08648312021-05-06 16:23:19 -0400350
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400351cc_library_static {
352 name: "whole_and_static_lib_for_both",
353 bazel_module: { bp2build_available: false },
354}
355
Liz Kammer8337ea42021-09-10 10:06:32 -0400356cc_library {
357 name: "shared_dep_for_shared",
358 bazel_module: { bp2build_available: false },
359}
Chris Parsons08648312021-05-06 16:23:19 -0400360
Liz Kammer8337ea42021-09-10 10:06:32 -0400361cc_library {
362 name: "shared_dep_for_static",
363 bazel_module: { bp2build_available: false },
364}
Chris Parsons08648312021-05-06 16:23:19 -0400365
Liz Kammer8337ea42021-09-10 10:06:32 -0400366cc_library {
367 name: "shared_dep_for_both",
368 bazel_module: { bp2build_available: false },
369}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000370`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000371 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000372 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500373 "copts": `[
374 "bothflag",
375 "staticflag",
376 ]`,
377 "implementation_deps": `[
378 ":static_dep_for_both",
379 ":static_dep_for_static",
380 ]`,
381 "implementation_dynamic_deps": `[
382 ":shared_dep_for_both",
383 ":shared_dep_for_static",
384 ]`,
385 "srcs": `[
386 "both.cpp",
387 "staticonly.cpp",
388 ]`,
389 "whole_archive_deps": `[
390 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400391 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500392 ":whole_static_lib_for_static",
393 ]`}),
Alixe06d75b2022-08-31 18:28:19 +0000394 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500395 "copts": `[
396 "bothflag",
397 "sharedflag",
398 ]`,
399 "implementation_deps": `[
400 ":static_dep_for_both",
401 ":static_dep_for_shared",
402 ]`,
403 "implementation_dynamic_deps": `[
404 ":shared_dep_for_both",
405 ":shared_dep_for_shared",
406 ]`,
407 "srcs": `[
408 "both.cpp",
409 "sharedonly.cpp",
410 ]`,
411 "whole_archive_deps": `[
412 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400413 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500414 ":whole_static_lib_for_shared",
415 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500416 }),
417 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200418 })
419}
420
Liz Kammer7a210ac2021-09-22 15:52:58 -0400421func TestCcLibraryDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000422 runCcLibraryTestCase(t, Bp2buildTestCase{
423 Description: "cc_library shared/static props",
424 ModuleTypeUnderTest: "cc_library",
425 ModuleTypeUnderTestFactory: cc.LibraryFactory,
426 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400427 "both.cpp": "",
428 "sharedonly.cpp": "",
429 "staticonly.cpp": "",
430 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000431 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400432cc_library {
433 name: "a",
434 srcs: ["both.cpp"],
435 cflags: ["bothflag"],
436 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
437 export_shared_lib_headers: ["shared_dep_for_both"],
438 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
439 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
440 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
441 static: {
442 srcs: ["staticonly.cpp"],
443 cflags: ["staticflag"],
444 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
445 export_shared_lib_headers: ["shared_dep_for_static"],
446 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
447 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
448 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
449 },
450 shared: {
451 srcs: ["sharedonly.cpp"],
452 cflags: ["sharedflag"],
453 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
454 export_shared_lib_headers: ["shared_dep_for_shared"],
455 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
456 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
457 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
458 },
459 include_build_directory: false,
460}
Sam Delmerico130d75b2023-08-31 00:51:44 +0000461` + SimpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
462 SimpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
463 SimpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
464 SimpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
465 SimpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
466 SimpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
467 SimpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
468 SimpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
469 SimpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
470 SimpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
471 SimpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
472 SimpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
473 SimpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
474 SimpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
475 SimpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
476 SimpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
477 SimpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
478 SimpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000479 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000480 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500481 "copts": `[
482 "bothflag",
483 "staticflag",
484 ]`,
485 "deps": `[
486 ":static_dep_for_both",
487 ":static_dep_for_static",
488 ]`,
489 "dynamic_deps": `[
490 ":shared_dep_for_both",
491 ":shared_dep_for_static",
492 ]`,
493 "implementation_deps": `[
494 ":implementation_static_dep_for_both",
495 ":implementation_static_dep_for_static",
496 ]`,
497 "implementation_dynamic_deps": `[
498 ":implementation_shared_dep_for_both",
499 ":implementation_shared_dep_for_static",
500 ]`,
501 "srcs": `[
502 "both.cpp",
503 "staticonly.cpp",
504 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500505 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400506 ":not_explicitly_exported_whole_static_dep_for_both",
507 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500508 ":not_explicitly_exported_whole_static_dep_for_static",
509 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500510 ]`,
511 }),
Alixe06d75b2022-08-31 18:28:19 +0000512 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500513 "copts": `[
514 "bothflag",
515 "sharedflag",
516 ]`,
517 "deps": `[
518 ":static_dep_for_both",
519 ":static_dep_for_shared",
520 ]`,
521 "dynamic_deps": `[
522 ":shared_dep_for_both",
523 ":shared_dep_for_shared",
524 ]`,
525 "implementation_deps": `[
526 ":implementation_static_dep_for_both",
527 ":implementation_static_dep_for_shared",
528 ]`,
529 "implementation_dynamic_deps": `[
530 ":implementation_shared_dep_for_both",
531 ":implementation_shared_dep_for_shared",
532 ]`,
533 "srcs": `[
534 "both.cpp",
535 "sharedonly.cpp",
536 ]`,
537 "whole_archive_deps": `[
538 ":not_explicitly_exported_whole_static_dep_for_both",
539 ":whole_static_dep_for_both",
540 ":not_explicitly_exported_whole_static_dep_for_shared",
541 ":whole_static_dep_for_shared",
542 ]`,
543 })},
544 },
545 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400546}
547
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400548func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000549 runCcLibraryTestCase(t, Bp2buildTestCase{
550 ModuleTypeUnderTest: "cc_library",
551 ModuleTypeUnderTestFactory: cc.LibraryFactory,
552 Dir: "foo/bar",
553 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400554 "foo/bar/Android.bp": `
555cc_library {
556 name: "a",
557 whole_static_libs: ["whole_static_lib_for_both"],
558 static: {
559 whole_static_libs: ["whole_static_lib_for_static"],
560 },
561 shared: {
562 whole_static_libs: ["whole_static_lib_for_shared"],
563 },
564 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400565 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400566}
567
568cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
569
570cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
571
572cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
573`,
574 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000575 Blueprint: soongCcLibraryPreamble,
576 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000577 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500578 "whole_archive_deps": `[
579 ":whole_static_lib_for_both_alwayslink",
580 ":whole_static_lib_for_static_alwayslink",
581 ]`,
582 }),
Alixe06d75b2022-08-31 18:28:19 +0000583 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500584 "whole_archive_deps": `[
585 ":whole_static_lib_for_both_alwayslink",
586 ":whole_static_lib_for_shared_alwayslink",
587 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500588 }),
589 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500590 },
591 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400592}
593
Jingwen Chenbcf53042021-05-26 04:42:42 +0000594func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000595 runCcLibraryTestCase(t, Bp2buildTestCase{
596 Description: "cc_library shared/static props in arch",
597 ModuleTypeUnderTest: "cc_library",
598 ModuleTypeUnderTestFactory: cc.LibraryFactory,
599 Dir: "foo/bar",
600 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000601 "foo/bar/arm.cpp": "",
602 "foo/bar/x86.cpp": "",
603 "foo/bar/sharedonly.cpp": "",
604 "foo/bar/staticonly.cpp": "",
605 "foo/bar/Android.bp": `
606cc_library {
607 name: "a",
608 arch: {
609 arm: {
610 shared: {
611 srcs: ["arm_shared.cpp"],
612 cflags: ["-DARM_SHARED"],
613 static_libs: ["arm_static_dep_for_shared"],
614 whole_static_libs: ["arm_whole_static_dep_for_shared"],
615 shared_libs: ["arm_shared_dep_for_shared"],
616 },
617 },
618 x86: {
619 static: {
620 srcs: ["x86_static.cpp"],
621 cflags: ["-DX86_STATIC"],
622 static_libs: ["x86_dep_for_static"],
623 },
624 },
625 },
626 target: {
627 android: {
628 shared: {
629 srcs: ["android_shared.cpp"],
630 cflags: ["-DANDROID_SHARED"],
631 static_libs: ["android_dep_for_shared"],
632 },
633 },
634 android_arm: {
635 shared: {
636 cflags: ["-DANDROID_ARM_SHARED"],
637 },
638 },
639 },
640 srcs: ["both.cpp"],
641 cflags: ["bothflag"],
642 static_libs: ["static_dep_for_both"],
643 static: {
644 srcs: ["staticonly.cpp"],
645 cflags: ["staticflag"],
646 static_libs: ["static_dep_for_static"],
647 },
648 shared: {
649 srcs: ["sharedonly.cpp"],
650 cflags: ["sharedflag"],
651 static_libs: ["static_dep_for_shared"],
652 },
653 bazel_module: { bp2build_available: true },
654}
655
656cc_library_static { name: "static_dep_for_shared" }
657cc_library_static { name: "static_dep_for_static" }
658cc_library_static { name: "static_dep_for_both" }
659
660cc_library_static { name: "arm_static_dep_for_shared" }
661cc_library_static { name: "arm_whole_static_dep_for_shared" }
662cc_library_static { name: "arm_shared_dep_for_shared" }
663
664cc_library_static { name: "x86_dep_for_static" }
665
666cc_library_static { name: "android_dep_for_shared" }
667`,
668 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000669 Blueprint: soongCcLibraryPreamble,
670 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000671 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500672 "copts": `[
673 "bothflag",
674 "staticflag",
675 ] + select({
676 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
677 "//conditions:default": [],
678 })`,
679 "implementation_deps": `[
680 ":static_dep_for_both",
681 ":static_dep_for_static",
682 ] + select({
683 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
684 "//conditions:default": [],
685 })`,
686 "local_includes": `["."]`,
687 "srcs": `[
688 "both.cpp",
689 "staticonly.cpp",
690 ] + select({
691 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
692 "//conditions:default": [],
693 })`,
694 }),
Alixe06d75b2022-08-31 18:28:19 +0000695 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500696 "copts": `[
697 "bothflag",
698 "sharedflag",
699 ] + select({
700 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
701 "//conditions:default": [],
702 }) + select({
703 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
704 "//conditions:default": [],
705 }) + select({
706 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
707 "//conditions:default": [],
708 })`,
709 "implementation_deps": `[
710 ":static_dep_for_both",
711 ":static_dep_for_shared",
712 ] + select({
713 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
714 "//conditions:default": [],
715 }) + select({
716 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
717 "//conditions:default": [],
718 })`,
719 "implementation_dynamic_deps": `select({
720 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
721 "//conditions:default": [],
722 })`,
723 "local_includes": `["."]`,
724 "srcs": `[
725 "both.cpp",
726 "sharedonly.cpp",
727 ] + select({
728 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
729 "//conditions:default": [],
730 }) + select({
731 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
732 "//conditions:default": [],
733 })`,
734 "whole_archive_deps": `select({
735 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
736 "//conditions:default": [],
737 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500738 }),
739 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500740 },
741 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000742}
743
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000744func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000745 runCcLibraryTestCase(t, Bp2buildTestCase{
746 Description: "cc_library shared/static props with c/cpp/s mixed sources",
747 ModuleTypeUnderTest: "cc_library",
748 ModuleTypeUnderTestFactory: cc.LibraryFactory,
749 Dir: "foo/bar",
750 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000751 "foo/bar/both_source.cpp": "",
752 "foo/bar/both_source.cc": "",
753 "foo/bar/both_source.c": "",
754 "foo/bar/both_source.s": "",
755 "foo/bar/both_source.S": "",
756 "foo/bar/shared_source.cpp": "",
757 "foo/bar/shared_source.cc": "",
758 "foo/bar/shared_source.c": "",
759 "foo/bar/shared_source.s": "",
760 "foo/bar/shared_source.S": "",
761 "foo/bar/static_source.cpp": "",
762 "foo/bar/static_source.cc": "",
763 "foo/bar/static_source.c": "",
764 "foo/bar/static_source.s": "",
765 "foo/bar/static_source.S": "",
766 "foo/bar/Android.bp": `
767cc_library {
768 name: "a",
769 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400770 "both_source.cpp",
771 "both_source.cc",
772 "both_source.c",
773 "both_source.s",
774 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400775 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400776 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000777 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400778 srcs: [
779 "static_source.cpp",
780 "static_source.cc",
781 "static_source.c",
782 "static_source.s",
783 "static_source.S",
784 ":static_filegroup",
785 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000786 },
787 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400788 srcs: [
789 "shared_source.cpp",
790 "shared_source.cc",
791 "shared_source.c",
792 "shared_source.s",
793 "shared_source.S",
794 ":shared_filegroup",
795 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000796 },
797 bazel_module: { bp2build_available: true },
798}
799
800filegroup {
801 name: "both_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
807filegroup {
808 name: "shared_filegroup",
809 srcs: [
810 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400811 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000812}
813
814filegroup {
815 name: "static_filegroup",
816 srcs: [
817 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400818 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000819}
820`,
821 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000822 Blueprint: soongCcLibraryPreamble,
823 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000824 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500825 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500826 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000827 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400828 "both_source.cc",
829 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500830 "static_source.cpp",
831 "static_source.cc",
832 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500833 ]`,
834 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000835 "both_source.s",
836 "both_source.S",
837 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500838 "static_source.s",
839 "static_source.S",
840 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500841 ]`,
842 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000843 "both_source.c",
844 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500845 "static_source.c",
846 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500847 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500848 }),
Alixe06d75b2022-08-31 18:28:19 +0000849 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500850 "local_includes": `["."]`,
851 "srcs": `[
852 "both_source.cpp",
853 "both_source.cc",
854 ":both_filegroup_cpp_srcs",
855 "shared_source.cpp",
856 "shared_source.cc",
857 ":shared_filegroup_cpp_srcs",
858 ]`,
859 "srcs_as": `[
860 "both_source.s",
861 "both_source.S",
862 ":both_filegroup_as_srcs",
863 "shared_source.s",
864 "shared_source.S",
865 ":shared_filegroup_as_srcs",
866 ]`,
867 "srcs_c": `[
868 "both_source.c",
869 ":both_filegroup_c_srcs",
870 "shared_source.c",
871 ":shared_filegroup_c_srcs",
872 ]`,
873 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000874}
875
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000876func TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000877 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000878 Description: "cc_library non-configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000879 ModuleTypeUnderTest: "cc_library",
880 ModuleTypeUnderTestFactory: cc.LibraryFactory,
881 Dir: "foo/bar",
882 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200883 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200884cc_library {
885 name: "a",
886 srcs: ["a.cpp"],
887 version_script: "v.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000888 dynamic_list: "dynamic.list",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200889 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400890 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200891}
892`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200893 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000894 Blueprint: soongCcLibraryPreamble,
895 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000896 "additional_linker_inputs": `[
897 "v.map",
898 "dynamic.list",
899 ]`,
900 "linkopts": `[
901 "-Wl,--version-script,$(location v.map)",
902 "-Wl,--dynamic-list,$(location dynamic.list)",
903 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000904 "srcs": `["a.cpp"]`,
905 "features": `["android_cfi_exports_map"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500906 }),
907 },
908 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200909}
910
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000911func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000912 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000913 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000914 ModuleTypeUnderTest: "cc_library",
915 ModuleTypeUnderTestFactory: cc.LibraryFactory,
916 Dir: "foo/bar",
917 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200918 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400919cc_library {
920 name: "a",
921 srcs: ["a.cpp"],
922 arch: {
923 arm: {
924 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000925 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400926 },
927 arm64: {
928 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000929 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400930 },
931 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200932
Liz Kammer8337ea42021-09-10 10:06:32 -0400933 bazel_module: { bp2build_available: true },
934 include_build_directory: false,
935}
Liz Kammerd366c902021-06-03 13:43:01 -0400936 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200937 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000938 Blueprint: soongCcLibraryPreamble,
939 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500940 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000941 "//build/bazel/platforms/arch:arm": [
942 "arm.map",
943 "dynamic_arm.list",
944 ],
945 "//build/bazel/platforms/arch:arm64": [
946 "arm64.map",
947 "dynamic_arm64.list",
948 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400949 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500950 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500951 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000952 "//build/bazel/platforms/arch:arm": [
953 "-Wl,--version-script,$(location arm.map)",
954 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
955 ],
956 "//build/bazel/platforms/arch:arm64": [
957 "-Wl,--version-script,$(location arm64.map)",
958 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
959 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400960 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500961 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500962 "srcs": `["a.cpp"]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000963 "features": `select({
964 "//build/bazel/platforms/arch:arm": ["android_cfi_exports_map"],
965 "//build/bazel/platforms/arch:arm64": ["android_cfi_exports_map"],
966 "//conditions:default": [],
967 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500968 }),
969 },
970 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200971}
972
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000973func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
974 runCcLibraryTestCase(t, Bp2buildTestCase{
975 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
976 ModuleTypeUnderTest: "cc_library",
977 ModuleTypeUnderTestFactory: cc.LibraryFactory,
978 Filesystem: map[string]string{
979 "version_script": "",
980 "dynamic.list": "",
981 },
982 Blueprint: `
983cc_library {
984 name: "foo",
985 ldflags: [
986 "--nospace_flag",
987 "-z spaceflag",
988 ],
989 version_script: "version_script",
990 dynamic_list: "dynamic.list",
991 include_build_directory: false,
992}
993`,
994 ExpectedBazelTargets: []string{
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000995 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
996 "features": `["android_cfi_exports_map"]`,
997 }),
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000998 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
999 "additional_linker_inputs": `[
1000 "version_script",
1001 "dynamic.list",
1002 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001003 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001004 "linkopts": `[
1005 "--nospace_flag",
1006 "-z",
1007 "spaceflag",
1008 "-Wl,--version-script,$(location version_script)",
1009 "-Wl,--dynamic-list,$(location dynamic.list)",
1010 ]`,
1011 }),
1012 },
1013 })
1014}
1015
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001016func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001017 runCcLibraryTestCase(t, Bp2buildTestCase{
1018 Description: "cc_library shared_libs",
1019 ModuleTypeUnderTest: "cc_library",
1020 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1021 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001022cc_library {
1023 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -04001024 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001025}
1026
1027cc_library {
1028 name: "a",
1029 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001030 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001031}
1032`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001033 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001034 "implementation_dynamic_deps": `[":mylib"]`,
1035 }),
1036 },
1037 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001038}
1039
Liz Kammer0eae52e2021-10-06 10:32:26 -04001040func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001041 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001042 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001043 "features": `[
1044 "disable_pack_relocations",
1045 "-no_undefined_symbols",
1046 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001047 "native_coverage": `False`,
1048 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001049 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001050 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001051 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001052 "//build/bazel/platforms/arch:x86_64": [
1053 "disable_pack_relocations",
1054 "-no_undefined_symbols",
1055 ],
1056 "//conditions:default": [],
1057 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001058 "native_coverage": `False`,
1059 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001060 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001061 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001062 "features": `select({
1063 "//build/bazel/platforms/os:darwin": [
1064 "disable_pack_relocations",
1065 "-no_undefined_symbols",
1066 ],
1067 "//conditions:default": [],
1068 })`,
1069 "srcs": `["c.cpp"]`,
1070 })...)
1071
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001072 runCcLibraryTestCase(t, Bp2buildTestCase{
1073 Description: "cc_library pack_relocations test",
1074 ModuleTypeUnderTest: "cc_library",
1075 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1076 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001077cc_library {
1078 name: "a",
1079 srcs: ["a.cpp"],
1080 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001081 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001082 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001083 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001084}
1085
1086cc_library {
1087 name: "b",
1088 srcs: ["b.cpp"],
1089 arch: {
1090 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001091 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001092 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001093 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001094 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001095 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001096 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001097}
1098
1099cc_library {
1100 name: "c",
1101 srcs: ["c.cpp"],
1102 target: {
1103 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001104 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001105 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001106 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001107 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001108 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001109}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001110 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001111 })
1112}
1113
1114func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001115 runCcLibraryTestCase(t, Bp2buildTestCase{
1116 Description: "cc_library spaces in copts",
1117 ModuleTypeUnderTest: "cc_library",
1118 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1119 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001120cc_library {
1121 name: "a",
1122 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001123 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001124}
1125`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001126 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001127 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001128 "-include",
1129 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001130 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001131 }),
1132 },
1133 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001134}
1135
1136func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001137 runCcLibraryTestCase(t, Bp2buildTestCase{
1138 Description: "cc_library cppflags usage",
1139 ModuleTypeUnderTest: "cc_library",
1140 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1141 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001142 name: "a",
1143 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001144 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001145 cppflags: [
1146 "-fsigned-char",
1147 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001148 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001149 arch: {
1150 arm64: {
1151 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001152 },
Liz Kammerd366c902021-06-03 13:43:01 -04001153 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001154 target: {
1155 android: {
1156 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001157 },
Liz Kammerd366c902021-06-03 13:43:01 -04001158 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001159 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001160}
1161`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001162 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001163 "copts": `["-Wall"]`,
1164 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001165 "-fsigned-char",
1166 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001167 ] + select({
1168 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1169 "//conditions:default": [],
1170 }) + select({
1171 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1172 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001173 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001174 "srcs": `["a.cpp"]`,
1175 }),
1176 },
1177 )
Jingwen Chen63930982021-03-24 10:04:33 -04001178}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001179
Liz Kammer47535c52021-06-02 16:02:22 -04001180func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001181 runCcLibraryTestCase(t, Bp2buildTestCase{
1182 ModuleTypeUnderTest: "cc_library",
1183 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1184 Filesystem: map[string]string{},
1185 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001186cc_library {
1187 name: "foo_static",
1188 srcs: ["common.c"],
1189 whole_static_libs: [
1190 "arm_whole_static_lib_excludes",
1191 "malloc_not_svelte_whole_static_lib_excludes"
1192 ],
1193 static_libs: [
1194 "arm_static_lib_excludes",
1195 "malloc_not_svelte_static_lib_excludes"
1196 ],
1197 shared_libs: [
1198 "arm_shared_lib_excludes",
1199 ],
1200 arch: {
1201 arm: {
1202 exclude_shared_libs: [
1203 "arm_shared_lib_excludes",
1204 ],
1205 exclude_static_libs: [
1206 "arm_static_lib_excludes",
1207 "arm_whole_static_lib_excludes",
1208 ],
1209 },
1210 },
1211 product_variables: {
1212 malloc_not_svelte: {
1213 shared_libs: ["malloc_not_svelte_shared_lib"],
1214 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1215 exclude_static_libs: [
1216 "malloc_not_svelte_static_lib_excludes",
1217 "malloc_not_svelte_whole_static_lib_excludes",
1218 ],
1219 },
1220 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001221 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001222}
1223
1224cc_library {
1225 name: "arm_whole_static_lib_excludes",
1226 bazel_module: { bp2build_available: false },
1227}
1228
1229cc_library {
1230 name: "malloc_not_svelte_whole_static_lib",
1231 bazel_module: { bp2build_available: false },
1232}
1233
1234cc_library {
1235 name: "malloc_not_svelte_whole_static_lib_excludes",
1236 bazel_module: { bp2build_available: false },
1237}
1238
1239cc_library {
1240 name: "arm_static_lib_excludes",
1241 bazel_module: { bp2build_available: false },
1242}
1243
1244cc_library {
1245 name: "malloc_not_svelte_static_lib_excludes",
1246 bazel_module: { bp2build_available: false },
1247}
1248
1249cc_library {
1250 name: "arm_shared_lib_excludes",
1251 bazel_module: { bp2build_available: false },
1252}
1253
1254cc_library {
1255 name: "malloc_not_svelte_shared_lib",
1256 bazel_module: { bp2build_available: false },
1257}
1258`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001259 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001260 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001261 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001262 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001263 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001264 "//build/bazel/product_config/config_settings:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001265 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001266 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001267 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001268 "//build/bazel/platforms/arch:arm": [],
1269 "//conditions:default": [":arm_shared_lib_excludes"],
1270 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001271 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
Liz Kammer7a210ac2021-09-22 15:52:58 -04001272 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001273 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001274 "srcs_c": `["common.c"]`,
1275 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001276 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001277 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001278 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001279 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
Chris Parsons953b3562021-09-20 15:14:39 -04001280 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001281 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001282 }),
1283 },
1284 )
Liz Kammer47535c52021-06-02 16:02:22 -04001285}
Liz Kammerd366c902021-06-03 13:43:01 -04001286
Zi Wang0a8a1292022-08-30 06:27:01 +00001287func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1288 runCcLibraryTestCase(t, Bp2buildTestCase{
1289 ModuleTypeUnderTest: "cc_library",
1290 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1291 Filesystem: map[string]string{},
1292 Blueprint: soongCcLibraryStaticPreamble + `
1293cc_library {
1294 name: "foo_static",
1295 srcs: ["common.c"],
1296 product_variables: {
1297 malloc_not_svelte: {
1298 header_libs: ["malloc_not_svelte_header_lib"],
1299 },
1300 },
1301 include_build_directory: false,
1302}
1303
1304cc_library {
1305 name: "malloc_not_svelte_header_lib",
1306 bazel_module: { bp2build_available: false },
1307}
1308`,
1309 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1310 "implementation_deps": `select({
Cole Faust87c0c332023-07-31 12:10:12 -07001311 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
Zi Wang0a8a1292022-08-30 06:27:01 +00001312 "//conditions:default": [],
1313 })`,
1314 "srcs_c": `["common.c"]`,
1315 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1316 }),
1317 },
1318 )
1319}
1320
Liz Kammerd366c902021-06-03 13:43:01 -04001321func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001322 runCcLibraryTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001323 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001324 ModuleTypeUnderTest: "cc_library",
1325 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1326 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001327 "impl.cpp": "",
1328 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001329 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001330cc_library {
1331 name: "foo-lib",
1332 srcs: ["impl.cpp"],
1333 nocrt: true,
1334 include_build_directory: false,
1335}
1336`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001337 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001338 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001339 "srcs": `["impl.cpp"]`,
1340 }),
1341 },
1342 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001343}
1344
1345func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001346 runCcLibraryTestCase(t, Bp2buildTestCase{
1347 Description: "cc_library - nocrt: false - does not emit attribute",
1348 ModuleTypeUnderTest: "cc_library",
1349 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1350 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001351 "impl.cpp": "",
1352 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001353 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001354cc_library {
1355 name: "foo-lib",
1356 srcs: ["impl.cpp"],
1357 nocrt: false,
1358 include_build_directory: false,
1359}
1360`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001361 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001362 "srcs": `["impl.cpp"]`,
1363 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001364 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001365}
1366
1367func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001368 runCcLibraryTestCase(t, Bp2buildTestCase{
1369 Description: "cc_library - nocrt in select",
1370 ModuleTypeUnderTest: "cc_library",
1371 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1372 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001373 "impl.cpp": "",
1374 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001375 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001376cc_library {
1377 name: "foo-lib",
1378 srcs: ["impl.cpp"],
1379 arch: {
1380 arm: {
1381 nocrt: true,
1382 },
1383 x86: {
1384 nocrt: false,
1385 },
1386 },
1387 include_build_directory: false,
1388}
1389`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001390 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1391 "features": `select({
1392 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1393 "//conditions:default": [],
1394 })`,
1395 "srcs": `["impl.cpp"]`,
1396 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001397 })
1398}
1399
1400func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001401 runCcLibraryTestCase(t, Bp2buildTestCase{
1402 ModuleTypeUnderTest: "cc_library",
1403 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1404 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001405 "impl.cpp": "",
1406 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001407 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001408cc_library {
1409 name: "foo-lib",
1410 srcs: ["impl.cpp"],
1411 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001412 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001413}
1414`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001415 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001416 "features": `["-use_libcrt"]`,
1417 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001418 }),
1419 })
1420}
1421
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001422func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001423 STATIC_ONLY_ATTRS := map[string]bool{}
1424 SHARED_ONLY_ATTRS := map[string]bool{
1425 "link_crt": true,
1426 "additional_linker_inputs": true,
1427 "linkopts": true,
1428 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001429 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001430 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001431 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001432 }
Wei Li81852ca2022-07-27 00:22:06 -07001433
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001434 sharedAttrs := AttrNameToString{}
1435 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001436 for key, val := range attrs {
1437 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1438 sharedAttrs[key] = val
1439 }
1440 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1441 staticAttrs[key] = val
1442 }
1443 }
Alixe06d75b2022-08-31 18:28:19 +00001444 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1445 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001446
1447 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001448}
1449
Jingwen Chen6ada5892021-09-17 11:38:09 +00001450func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001451 runCcLibraryTestCase(t, Bp2buildTestCase{
1452 ModuleTypeUnderTest: "cc_library",
1453 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1454 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001455 "impl.cpp": "",
1456 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001457 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001458cc_library {
1459 name: "foo-lib",
1460 srcs: ["impl.cpp"],
1461 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001462 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001463}
1464`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001465 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001466 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001467 }),
1468 })
Liz Kammerd366c902021-06-03 13:43:01 -04001469}
1470
Jingwen Chen6ada5892021-09-17 11:38:09 +00001471func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001472 runCcLibraryTestCase(t, Bp2buildTestCase{
1473 ModuleTypeUnderTest: "cc_library",
1474 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1475 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001476 "impl.cpp": "",
1477 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001478 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001479cc_library {
1480 name: "foo-lib",
1481 srcs: ["impl.cpp"],
1482 arch: {
1483 arm: {
1484 no_libcrt: true,
1485 },
1486 x86: {
1487 no_libcrt: true,
1488 },
1489 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001490 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001491}
1492`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001493 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001494 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001495 "features": `select({
1496 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1497 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1498 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001499 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001500 }),
1501 })
Liz Kammerd366c902021-06-03 13:43:01 -04001502}
1503
Chris Parsons58852a02021-12-09 18:10:18 -05001504func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001505 runCcLibraryTestCase(t, Bp2buildTestCase{
1506 ModuleTypeUnderTest: "cc_library",
1507 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1508 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001509 "impl.cpp": "",
1510 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001511 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001512cc_library {
1513 name: "foo-lib",
1514 srcs: ["impl.cpp"],
1515 arch: {
1516 arm: {
1517 no_libcrt: true,
1518 },
1519 x86: {
1520 no_libcrt: true,
1521 },
1522 },
1523 target: {
1524 darwin: {
1525 no_libcrt: true,
1526 }
1527 },
1528 include_build_directory: false,
1529}
1530`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001531 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001532 "features": `select({
1533 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1534 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1535 "//conditions:default": [],
1536 }) + select({
1537 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1538 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001539 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001540 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001541 }),
1542 })
1543}
1544
1545func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001546 runCcLibraryTestCase(t, Bp2buildTestCase{
1547 ModuleTypeUnderTest: "cc_library",
1548 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1549 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001550 "impl.cpp": "",
1551 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001552 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001553cc_library {
1554 name: "foo-lib",
1555 srcs: ["impl.cpp"],
1556 arch: {
1557 arm: {
1558 no_libcrt: true,
1559 },
1560 // This is expected to override the value for darwin_x86_64.
1561 x86_64: {
1562 no_libcrt: true,
1563 },
1564 },
1565 target: {
1566 darwin: {
1567 no_libcrt: false,
1568 }
1569 },
1570 include_build_directory: false,
1571}
1572`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001573 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001574 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001575 "features": `select({
1576 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1577 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1578 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001579 })`,
1580 }),
1581 })
1582}
1583
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001584func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001585 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001586 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001587 "strip": `{
1588 "all": True,
1589 }`,
1590 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001591 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001592 "strip": `{
1593 "keep_symbols": True,
1594 }`,
1595 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001596 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001597 "strip": `{
1598 "keep_symbols_and_debug_frame": True,
1599 }`,
1600 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001601 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001602 "strip": `{
1603 "keep_symbols_list": ["symbol"],
1604 }`,
1605 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001606 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001607 "strip": `{
1608 "none": True,
1609 }`,
1610 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001611 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001612
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001613 runCcLibraryTestCase(t, Bp2buildTestCase{
1614 Description: "cc_library strip args",
1615 ModuleTypeUnderTest: "cc_library",
1616 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1617 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001618cc_library {
1619 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001620 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001621}
1622cc_library {
1623 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001624 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001625 keep_symbols: true,
1626 },
1627 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001628}
1629cc_library {
1630 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001631 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001632 keep_symbols_and_debug_frame: true,
1633 },
1634 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001635}
1636cc_library {
1637 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001638 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001639 none: true,
1640 },
1641 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001642}
1643cc_library {
1644 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001645 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001646 keep_symbols_list: ["symbol"],
1647 },
1648 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001649}
1650cc_library {
1651 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001652 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001653 all: true,
1654 },
1655 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001656}
1657`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001658 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001659 })
1660}
1661
1662func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001663 runCcLibraryTestCase(t, Bp2buildTestCase{
1664 Description: "cc_library strip args",
1665 ModuleTypeUnderTest: "cc_library",
1666 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1667 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001668cc_library {
1669 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001670 target: {
1671 darwin: {
1672 strip: {
1673 keep_symbols_list: ["foo", "bar"]
1674 }
1675 },
1676 },
1677 arch: {
1678 arm: {
1679 strip: {
1680 keep_symbols_and_debug_frame: true,
1681 },
1682 },
1683 arm64: {
1684 strip: {
1685 keep_symbols: true,
1686 },
1687 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001688 },
1689 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001690}
1691`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001692 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001693 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001694 "keep_symbols": select({
1695 "//build/bazel/platforms/arch:arm64": True,
1696 "//conditions:default": None,
1697 }),
1698 "keep_symbols_and_debug_frame": select({
1699 "//build/bazel/platforms/arch:arm": True,
1700 "//conditions:default": None,
1701 }),
1702 "keep_symbols_list": select({
1703 "//build/bazel/platforms/os:darwin": [
1704 "foo",
1705 "bar",
1706 ],
1707 "//conditions:default": [],
1708 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001709 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001710 }),
1711 },
1712 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001713}
Chris Parsons51f8c392021-08-03 21:01:05 -04001714
1715func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001716 runCcLibraryTestCase(t, Bp2buildTestCase{
1717 Description: "cc_library system_shared_libs empty at root",
1718 ModuleTypeUnderTest: "cc_library",
1719 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1720 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001721cc_library {
1722 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001723 system_shared_libs: [],
1724 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001725}
1726`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001727 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001728 "system_dynamic_deps": `[]`,
1729 }),
1730 },
1731 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001732}
1733
1734func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001735 runCcLibraryTestCase(t, Bp2buildTestCase{
1736 Description: "cc_library system_shared_libs empty for static variant",
1737 ModuleTypeUnderTest: "cc_library",
1738 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1739 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001740cc_library {
1741 name: "static_empty",
1742 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001743 system_shared_libs: [],
1744 },
1745 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001746}
1747`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001748 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001749 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001750 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001751 }),
Alixe06d75b2022-08-31 18:28:19 +00001752 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001753 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001754 })
1755}
1756
1757func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001758 runCcLibraryTestCase(t, Bp2buildTestCase{
1759 Description: "cc_library system_shared_libs empty for shared variant",
1760 ModuleTypeUnderTest: "cc_library",
1761 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1762 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001763cc_library {
1764 name: "shared_empty",
1765 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001766 system_shared_libs: [],
1767 },
1768 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001769}
1770`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001772 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1773 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001774 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001775 }),
1776 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001777 })
1778}
1779
1780func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001781 runCcLibraryTestCase(t, Bp2buildTestCase{
1782 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1783 ModuleTypeUnderTest: "cc_library",
1784 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1785 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001786cc_library {
1787 name: "shared_empty",
1788 target: {
1789 bionic: {
1790 shared: {
1791 system_shared_libs: [],
1792 }
1793 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001794 },
1795 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001796}
1797`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001798 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001799 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1800 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001801 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001802 }),
1803 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001804 })
1805}
1806
1807func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1808 // Note that this behavior is technically incorrect (it's a simplification).
1809 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1810 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1811 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001812 runCcLibraryTestCase(t, Bp2buildTestCase{
1813 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1814 ModuleTypeUnderTest: "cc_library",
1815 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1816 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001817cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001818 name: "libc_musl",
1819 bazel_module: { bp2build_available: false },
1820}
1821
1822cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001823 name: "target_linux_bionic_empty",
1824 target: {
1825 linux_bionic: {
1826 system_shared_libs: [],
1827 },
1828 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001829 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001830}
1831`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001832 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001833 "system_dynamic_deps": `select({
1834 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1835 "//conditions:default": [],
1836 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001837 }),
1838 },
1839 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001840}
1841
1842func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001843 runCcLibraryTestCase(t, Bp2buildTestCase{
1844 Description: "cc_library system_shared_libs empty for bionic variant",
1845 ModuleTypeUnderTest: "cc_library",
1846 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1847 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001848cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001849 name: "libc_musl",
1850 bazel_module: { bp2build_available: false },
1851}
1852
1853cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001854 name: "target_bionic_empty",
1855 target: {
1856 bionic: {
1857 system_shared_libs: [],
1858 },
1859 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001860 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001861}
1862`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001863 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001864 "system_dynamic_deps": `select({
1865 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1866 "//conditions:default": [],
1867 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001868 }),
1869 },
1870 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001871}
1872
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001873func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1874 runCcLibraryTestCase(t, Bp2buildTestCase{
1875 Description: "cc_library system_shared_lib empty for musl variant",
1876 ModuleTypeUnderTest: "cc_library",
1877 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1878 Blueprint: soongCcLibraryPreamble + `
1879cc_library {
1880 name: "libc_musl",
1881 bazel_module: { bp2build_available: false },
1882}
1883
1884cc_library {
1885 name: "target_musl_empty",
1886 target: {
1887 musl: {
1888 system_shared_libs: [],
1889 },
1890 },
1891 include_build_directory: false,
1892}
1893`,
1894 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1895 "system_dynamic_deps": `[]`,
1896 }),
1897 })
1898}
1899
1900func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1901 runCcLibraryTestCase(t, Bp2buildTestCase{
1902 Description: "cc_library system_shared_lib empty for linux_musl variant",
1903 ModuleTypeUnderTest: "cc_library",
1904 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1905 Blueprint: soongCcLibraryPreamble + `
1906cc_library {
1907 name: "libc_musl",
1908 bazel_module: { bp2build_available: false },
1909}
1910
1911cc_library {
1912 name: "target_linux_musl_empty",
1913 target: {
1914 linux_musl: {
1915 system_shared_libs: [],
1916 },
1917 },
1918 include_build_directory: false,
1919}
1920`,
1921 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1922 "system_dynamic_deps": `[]`,
1923 }),
1924 })
1925}
Chris Parsons51f8c392021-08-03 21:01:05 -04001926func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001927 runCcLibraryTestCase(t, Bp2buildTestCase{
1928 Description: "cc_library system_shared_libs set for shared and root",
1929 ModuleTypeUnderTest: "cc_library",
1930 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1931 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001932cc_library {
1933 name: "libc",
1934 bazel_module: { bp2build_available: false },
1935}
1936cc_library {
1937 name: "libm",
1938 bazel_module: { bp2build_available: false },
1939}
Chris Parsons51f8c392021-08-03 21:01:05 -04001940
1941cc_library {
1942 name: "foo",
1943 system_shared_libs: ["libc"],
1944 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001945 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001946 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001947 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001948}
1949`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001950 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001951 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001952 "system_dynamic_deps": `[":libc"]`,
1953 }),
Alixe06d75b2022-08-31 18:28:19 +00001954 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001955 "system_dynamic_deps": `[
1956 ":libc",
1957 ":libm",
1958 ]`,
1959 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001960 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001961 })
1962}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001963
1964func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001965 runCcLibraryTestCase(t, Bp2buildTestCase{
1966 Description: "cc_library - selects for all os targets",
1967 ModuleTypeUnderTest: "cc_library",
1968 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1969 Filesystem: map[string]string{},
1970 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001971cc_library {
1972 name: "foo-lib",
1973 srcs: ["base.cpp"],
1974 target: {
1975 android: {
1976 srcs: ["android.cpp"],
1977 },
1978 linux: {
1979 srcs: ["linux.cpp"],
1980 },
1981 linux_glibc: {
1982 srcs: ["linux_glibc.cpp"],
1983 },
1984 darwin: {
1985 srcs: ["darwin.cpp"],
1986 },
1987 bionic: {
1988 srcs: ["bionic.cpp"],
1989 },
1990 linux_musl: {
1991 srcs: ["linux_musl.cpp"],
1992 },
1993 windows: {
1994 srcs: ["windows.cpp"],
1995 },
1996 },
1997 include_build_directory: false,
1998}
1999`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002000 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002001 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002002 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002003 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002004 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002005 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002006 ],
2007 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002008 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002009 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002010 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002011 ],
Colin Cross133782e2022-12-20 15:29:31 -08002012 "//build/bazel/platforms/os:linux_glibc": [
2013 "linux.cpp",
2014 "linux_glibc.cpp",
2015 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002016 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002017 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002018 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002019 ],
2020 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2021 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002022 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002023 }),
2024 },
2025 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002026}
2027
Yu Liu75be7b92022-02-01 09:54:09 -08002028func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002029 runCcLibraryTestCase(t, Bp2buildTestCase{
2030 Description: "cc_library - libcrypto hash injection",
2031 ModuleTypeUnderTest: "cc_library",
2032 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2033 Filesystem: map[string]string{},
2034 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002035cc_library {
2036 name: "libcrypto",
2037 target: {
2038 android: {
2039 inject_bssl_hash: true,
2040 },
2041 },
2042 include_build_directory: false,
2043}
2044`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002045 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002046 "inject_bssl_hash": `select({
2047 "//build/bazel/platforms/os:android": True,
2048 "//conditions:default": None,
2049 })`,
2050 }),
2051 },
2052 )
2053}
2054
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002055func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002056 type testCase struct {
2057 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002058 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002059 gnu_extensions string
2060 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002061 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002062 }
2063
2064 testCases := []testCase{
2065 // Existing usages of cpp_std in AOSP are:
2066 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2067 //
2068 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2069 // is set in the toolchain.)
2070 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002071 {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 +00002072 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2073 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002074 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2075 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2076 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002077 // Explicitly setting a c++ std does not use replace gnu++ std even if
2078 // gnu_extensions is true.
2079 // "c++11",
2080 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002081 {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 +00002082 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2083 // "c++17",
2084 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002085 {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 +00002086 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2087 // "c++2a",
2088 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002089 {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 +00002090 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2091 // "c++98",
2092 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002093 {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 +00002094 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2095 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2096 // "gnu++11",
2097 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002098 {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 +00002099 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2100 // "gnu++17",
2101 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002102 {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 +00002103 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002104
2105 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002106 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2107 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2108 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002109 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2110 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2111 {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 +00002112 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002113 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002114 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2115 t.Run(name, func(t *testing.T) {
2116 name_prefix := fmt.Sprintf("a_%v", i)
2117 cppStdProp := ""
2118 if tc.cpp_std != "" {
2119 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2120 }
2121 cStdProp := ""
2122 if tc.c_std != "" {
2123 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2124 }
2125 gnuExtensionsProp := ""
2126 if tc.gnu_extensions != "" {
2127 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2128 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002129 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002130 if tc.bazel_cpp_std != "" {
2131 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2132 }
2133 if tc.bazel_c_std != "" {
2134 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2135 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002136
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002137 runCcLibraryTestCase(t, Bp2buildTestCase{
2138 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002139 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002140 ModuleTypeUnderTest: "cc_library",
2141 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2142 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002143cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002144 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002145%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002146%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002147%s // gnu_extensions: *bool
2148 include_build_directory: false,
2149}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002150`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002151 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002152 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002153
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002154 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2155 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002156 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002157 ModuleTypeUnderTest: "cc_library_static",
2158 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2159 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002160cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002161 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002162%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002163%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002164%s // gnu_extensions: *bool
2165 include_build_directory: false,
2166}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002167`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002168 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002169 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002170 },
2171 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002172
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002173 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2174 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002175 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002176 ModuleTypeUnderTest: "cc_library_shared",
2177 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2178 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002179cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002180 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002181%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002182%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002183%s // gnu_extensions: *bool
2184 include_build_directory: false,
2185}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002186`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002187 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002188 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002189 },
2190 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002191 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002192 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002193}
Liz Kammer12615db2021-09-28 09:19:17 -04002194
2195func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002196 runCcLibraryTestCase(t, Bp2buildTestCase{
2197 ModuleTypeUnderTest: "cc_library",
2198 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2199 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002200 name: "foo",
2201 srcs: ["foo.proto"],
2202 include_build_directory: false,
2203}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002204 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002205 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002206 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002207 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002208 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002209 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002210 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002211 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002212 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002213 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2214 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002215 }),
2216 },
2217 })
2218}
2219
2220func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002221 runCcLibraryTestCase(t, Bp2buildTestCase{
2222 ModuleTypeUnderTest: "cc_library",
2223 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2224 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002225 name: "foo",
2226 srcs: ["foo.proto"],
2227 proto: { canonical_path_from_root: false},
2228 include_build_directory: false,
2229}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002230 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002231 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002232 "srcs": `["foo.proto"]`,
2233 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002234 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002235 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002236 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002237 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002238 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002239 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002240 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2241 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002242 }),
2243 },
2244 })
2245}
2246
2247func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002248 runCcLibraryTestCase(t, Bp2buildTestCase{
2249 ModuleTypeUnderTest: "cc_library",
2250 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2251 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002252 name: "foo",
2253 srcs: ["foo.proto"],
2254 proto: { canonical_path_from_root: true},
2255 include_build_directory: false,
2256}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002257 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002258 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002259 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002260 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002261 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002262 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002263 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002264 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002265 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002266 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2267 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002268 }),
2269 },
2270 })
2271}
2272
2273func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002274 runCcLibraryTestCase(t, Bp2buildTestCase{
2275 ModuleTypeUnderTest: "cc_library",
2276 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2277 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002278 name: "foo",
2279 srcs: ["foo.proto"],
2280 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002281 type: "full",
2282 },
2283 include_build_directory: false,
2284}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002285 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002286 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002287 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002288 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002289 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002290 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002291 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002292 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002293 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002294 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2295 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002296 }),
2297 },
2298 })
2299}
2300
2301func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002302 runCcLibraryTestCase(t, Bp2buildTestCase{
2303 ModuleTypeUnderTest: "cc_library",
2304 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2305 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002306 name: "foo",
2307 srcs: ["foo.proto"],
2308 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002309 type: "lite",
2310 },
2311 include_build_directory: false,
2312}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002313 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002314 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002315 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002316 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002317 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002318 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002319 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002320 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002321 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002322 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2323 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002324 }),
2325 },
2326 })
2327}
2328
2329func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002330 runCcLibraryTestCase(t, Bp2buildTestCase{
2331 ModuleTypeUnderTest: "cc_library",
2332 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2333 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002334 name: "foo",
2335 srcs: ["foo.proto"],
2336 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002337 export_proto_headers: true,
2338 },
2339 include_build_directory: false,
2340}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002341 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002342 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002343 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002344 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002345 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002346 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002347 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002348 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002349 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002350 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2351 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002352 }),
2353 },
2354 })
2355}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002356
Yu Liu2d136142022-08-18 14:46:13 -07002357func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2358 runCcLibraryTestCase(t, Bp2buildTestCase{
2359 ModuleTypeUnderTest: "cc_library",
2360 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2361 Blueprint: soongCcProtoPreamble + `cc_library {
2362 name: "foo",
2363 srcs: ["foo.proto"],
2364 proto: {
2365 include_dirs: ["external/protobuf/src"],
2366 },
2367 include_build_directory: false,
2368}`,
2369 ExpectedBazelTargets: []string{
2370 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2371 "srcs": `["foo.proto"]`,
2372 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2373 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2374 "deps": `[":foo_proto"]`,
2375 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2376 "deps": `[":libprotobuf-cpp-lite"]`,
2377 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2378 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002379 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2380 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002381 }),
2382 },
2383 })
2384}
2385
2386func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2387 runCcLibraryTestCase(t, Bp2buildTestCase{
2388 ModuleTypeUnderTest: "cc_library",
2389 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2390 Blueprint: soongCcProtoPreamble + `cc_library {
2391 name: "foo",
2392 srcs: ["foo.proto"],
2393 proto: {
2394 include_dirs: ["external/protobuf/abc"],
2395 },
2396 include_build_directory: false,
2397}`,
Spandan Dasec39d512023-08-15 22:08:18 +00002398 ExpectedErr: fmt.Errorf("module \"foo\": TODO: Add support for proto.include_dir: external/protobuf/abc. This directory does not contain an Android.bp file"),
Yu Liu2d136142022-08-18 14:46:13 -07002399 })
2400}
2401
Yu Liu2aa806b2022-09-01 11:54:47 -07002402func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2403 runCcLibraryTestCase(t, Bp2buildTestCase{
2404 ModuleTypeUnderTest: "cc_library",
2405 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2406 Blueprint: soongCcProtoPreamble + `
2407filegroup {
2408 name: "a_fg_proto",
2409 srcs: ["a_fg.proto"],
2410}
2411
2412cc_library {
2413 name: "a",
2414 srcs: [
2415 ":a_fg_proto",
2416 "a.proto",
2417 ],
2418 proto: {
2419 export_proto_headers: true,
2420 },
2421 include_build_directory: false,
2422}`,
2423 ExpectedBazelTargets: []string{
2424 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002425 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002426 "srcs": `["a.proto"]`,
2427 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2428 "deps": `[
2429 ":a_fg_proto_bp2build_converted",
2430 ":a_proto",
2431 ]`,
2432 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2433 "deps": `[":libprotobuf-cpp-lite"]`,
2434 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2435 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2436 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2437 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002438 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002439 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002440 "tags": `[
2441 "apex_available=//apex_available:anyapex",
2442 "manual",
2443 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002444 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2445 "actual": `"//.:a_fg_proto_proto"`,
2446 "tags": `[
2447 "apex_available=//apex_available:anyapex",
2448 "manual",
2449 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002450 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2451 "srcs": `["a_fg.proto"]`,
2452 }),
2453 },
2454 })
2455}
2456
2457func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2458 runCcLibraryTestCase(t, Bp2buildTestCase{
2459 ModuleTypeUnderTest: "cc_library",
2460 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2461 Blueprint: soongCcProtoPreamble + `
2462filegroup {
2463 name: "a_fg_proto",
2464 srcs: ["a_fg.proto"],
2465}
2466
2467cc_library {
2468 name: "a",
2469 srcs: [
2470 ":a_fg_proto",
2471 ],
2472 proto: {
2473 export_proto_headers: true,
2474 },
2475 include_build_directory: false,
2476}`,
2477 ExpectedBazelTargets: []string{
2478 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2479 "deps": `[":a_fg_proto_bp2build_converted"]`,
2480 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2481 "deps": `[":libprotobuf-cpp-lite"]`,
2482 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2483 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2484 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2485 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002486 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002487 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002488 "tags": `[
2489 "apex_available=//apex_available:anyapex",
2490 "manual",
2491 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002492 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2493 "actual": `"//.:a_fg_proto_proto"`,
2494 "tags": `[
2495 "apex_available=//apex_available:anyapex",
2496 "manual",
2497 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002498 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2499 "srcs": `["a_fg.proto"]`,
2500 }),
2501 },
2502 })
2503}
2504
2505func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2506 runCcLibraryTestCase(t, Bp2buildTestCase{
2507 ModuleTypeUnderTest: "cc_library",
2508 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2509 Filesystem: map[string]string{
2510 "path/to/A/Android.bp": `
2511filegroup {
2512 name: "a_fg_proto",
2513 srcs: ["a_fg.proto"],
2514}`,
2515 },
2516 Blueprint: soongCcProtoPreamble + `
2517cc_library {
2518 name: "a",
2519 srcs: [
2520 ":a_fg_proto",
2521 "a.proto",
2522 ],
2523 proto: {
2524 export_proto_headers: true,
2525 },
2526 include_build_directory: false,
2527}`,
2528 ExpectedBazelTargets: []string{
2529 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002530 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002531 "srcs": `["a.proto"]`,
2532 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2533 "deps": `[
2534 "//path/to/A:a_fg_proto_bp2build_converted",
2535 ":a_proto",
2536 ]`,
2537 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2538 "deps": `[":libprotobuf-cpp-lite"]`,
2539 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2540 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2541 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2542 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2543 }),
2544 },
2545 })
2546}
2547
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002548func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002549 runCcLibraryTestCase(t, Bp2buildTestCase{
2550 ModuleTypeUnderTest: "cc_library",
2551 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2552 Blueprint: soongCcProtoPreamble +
Sam Delmerico130d75b2023-08-31 00:51:44 +00002553 SimpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2554 SimpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2555 SimpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2556 SimpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002557cc_library {
2558 name: "a",
2559 srcs: [":a_fg_proto"],
2560 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002561 export_proto_headers: true,
2562 },
2563 include_build_directory: false,
2564}
2565
2566cc_library {
2567 name: "b",
2568 srcs: [":b_protos"],
2569 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002570 export_proto_headers: true,
2571 },
2572 include_build_directory: false,
2573}
2574
2575cc_library {
2576 name: "c",
2577 srcs: [":c-proto-srcs"],
2578 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002579 export_proto_headers: true,
2580 },
2581 include_build_directory: false,
2582}
2583
2584cc_library {
2585 name: "d",
2586 srcs: [":proto-srcs-d"],
2587 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002588 export_proto_headers: true,
2589 },
2590 include_build_directory: false,
2591}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002592 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002593 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002594 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002595 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002596 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002597 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002598 "deps": `[":libprotobuf-cpp-lite"]`,
2599 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2600 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2601 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2602 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002603 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002604 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2605 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2606 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2607 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2608 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002609 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002610 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002611 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002612 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002613 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002614 "deps": `[":libprotobuf-cpp-lite"]`,
2615 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2616 "srcs": `[":b_protos_cpp_srcs"]`,
2617 "srcs_as": `[":b_protos_as_srcs"]`,
2618 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002619 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002620 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2621 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2622 "srcs": `[":b_protos_cpp_srcs"]`,
2623 "srcs_as": `[":b_protos_as_srcs"]`,
2624 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002625 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002626 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002627 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002628 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002629 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002630 "deps": `[":libprotobuf-cpp-lite"]`,
2631 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2632 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2633 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2634 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002635 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002636 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2637 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2638 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2639 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2640 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002641 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002642 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002643 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002644 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002645 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002646 "deps": `[":libprotobuf-cpp-lite"]`,
2647 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2648 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2649 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2650 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002651 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002652 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2653 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2654 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2655 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2656 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2657 }),
2658 },
2659 })
2660}
Chris Parsons58852a02021-12-09 18:10:18 -05002661
2662func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002663 runCcLibraryTestCase(t, Bp2buildTestCase{
2664 ModuleTypeUnderTest: "cc_library",
2665 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2666 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002667 name: "foo",
2668 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002669 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002670 target: {
2671 darwin: {
2672 enabled: false,
2673 },
2674 windows: {
2675 enabled: false,
2676 },
2677 linux_glibc_x86: {
2678 enabled: false,
2679 },
2680 },
2681 include_build_directory: false,
2682}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002683 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002684 "srcs": `["foo.cpp"]`,
2685 "target_compatible_with": `select({
2686 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2687 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2688 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2689 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2690 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2691 "//conditions:default": [],
2692 })`,
2693 }),
2694 })
2695}
2696
2697func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002698 runCcLibraryTestCase(t, Bp2buildTestCase{
2699 ModuleTypeUnderTest: "cc_library",
2700 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2701 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002702 name: "foo",
2703 srcs: ["foo.cpp"],
2704 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002705 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002706 target: {
2707 darwin: {
2708 enabled: true,
2709 },
2710 windows: {
2711 enabled: false,
2712 },
2713 linux_glibc_x86: {
2714 enabled: false,
2715 },
2716 },
2717 include_build_directory: false,
2718}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002719 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002720 "srcs": `["foo.cpp"]`,
2721 "target_compatible_with": `select({
2722 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2723 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2724 "//conditions:default": ["@platforms//:incompatible"],
2725 })`,
2726 }),
2727 })
2728}
2729
2730func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002731 runCcLibraryTestCase(t, Bp2buildTestCase{
2732 ModuleTypeUnderTest: "cc_library",
2733 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2734 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002735 name: "foo",
2736 srcs: ["foo.cpp"],
2737 enabled: false,
2738 shared: {
2739 enabled: true,
2740 },
2741 target: {
2742 android: {
2743 shared: {
2744 enabled: false,
2745 },
2746 }
2747 },
2748 include_build_directory: false,
2749}`,
Alixe06d75b2022-08-31 18:28:19 +00002750 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002751 "srcs": `["foo.cpp"]`,
2752 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002753 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002754 "srcs": `["foo.cpp"]`,
2755 "target_compatible_with": `select({
2756 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2757 "//conditions:default": [],
2758 })`,
2759 }),
2760 },
2761 })
2762}
2763
2764func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002765 runCcLibraryTestCase(t, Bp2buildTestCase{
2766 ModuleTypeUnderTest: "cc_library",
2767 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2768 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002769 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002770 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002771 srcs: ["foo.cpp"],
2772 shared: {
2773 enabled: false
2774 },
2775 target: {
2776 darwin: {
2777 enabled: true,
2778 },
2779 windows: {
2780 enabled: false,
2781 },
2782 linux_glibc_x86: {
2783 shared: {
2784 enabled: true,
2785 },
2786 },
2787 },
2788 include_build_directory: false,
2789}`,
Alixe06d75b2022-08-31 18:28:19 +00002790 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002791 "srcs": `["foo.cpp"]`,
2792 "target_compatible_with": `select({
2793 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2794 "//conditions:default": [],
2795 })`,
Alixe06d75b2022-08-31 18:28:19 +00002796 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002797 "srcs": `["foo.cpp"]`,
2798 "target_compatible_with": `select({
2799 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2800 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2801 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2802 "//conditions:default": ["@platforms//:incompatible"],
2803 })`,
2804 }),
2805 }})
2806}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002807
2808func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002809 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002810 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002811 })
2812 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Spandan Das04f9f4c2023-09-13 23:59:05 +00002813 "api_surface": `"module-libapi"`,
Sam Delmerico5f906492023-03-15 18:06:18 -04002814 "soname": `"a.so"`,
2815 "source_library_label": `"//foo/bar:a"`,
2816 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002817 "stubs_versions": `[
2818 "28",
2819 "29",
2820 "current",
2821 ]`,
2822 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002823 runCcLibraryTestCase(t, Bp2buildTestCase{
2824 Description: "cc_library stubs",
2825 ModuleTypeUnderTest: "cc_library",
2826 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2827 Dir: "foo/bar",
2828 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002829 "foo/bar/Android.bp": `
2830cc_library {
2831 name: "a",
2832 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2833 bazel_module: { bp2build_available: true },
2834 include_build_directory: false,
2835}
2836`,
2837 },
Wei Li81852ca2022-07-27 00:22:06 -07002838 Blueprint: soongCcLibraryPreamble,
2839 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002840 },
2841 )
2842}
Liz Kammerf38a8372022-02-04 15:39:00 -05002843
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002844func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2845 runCcLibraryTestCase(t, Bp2buildTestCase{
2846 Description: "stub target generation of the same lib across configs should not result in duplicates",
2847 ModuleTypeUnderTest: "cc_library",
2848 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2849 Filesystem: map[string]string{
2850 "bar.map.txt": "",
2851 },
2852 Blueprint: `
2853cc_library {
2854 name: "barlib",
2855 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2856 bazel_module: { bp2build_available: false },
2857}
2858cc_library {
2859 name: "foolib",
2860 shared_libs: ["barlib"],
2861 target: {
2862 android: {
2863 shared_libs: ["barlib"],
2864 },
2865 },
2866 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002867 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002868}`,
2869 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2870 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00002871 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002872 "//conditions:default": [":barlib"],
2873 })`,
2874 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002875 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002876 }),
2877 })
2878}
2879
Liz Kammerffc17e42022-11-23 09:42:05 -05002880func TestCcLibraryExcludesLibsHost(t *testing.T) {
2881 runCcLibraryTestCase(t, Bp2buildTestCase{
2882 ModuleTypeUnderTest: "cc_library",
2883 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2884 Filesystem: map[string]string{
2885 "bar.map.txt": "",
2886 },
Sam Delmerico130d75b2023-08-31 00:51:44 +00002887 Blueprint: SimpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
Liz Kammerffc17e42022-11-23 09:42:05 -05002888cc_library {
2889 name: "quxlib",
2890 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
2891 bazel_module: { bp2build_available: false },
2892}
2893cc_library {
2894 name: "barlib",
2895 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2896 bazel_module: { bp2build_available: false },
2897}
2898cc_library {
2899 name: "foolib",
2900 shared_libs: ["barlib", "quxlib"],
2901 target: {
2902 host: {
2903 shared_libs: ["bazlib"],
2904 exclude_shared_libs: ["barlib"],
2905 },
2906 },
2907 include_build_directory: false,
2908 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002909 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002910}`,
2911 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2912 "implementation_dynamic_deps": `select({
2913 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002914 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002915 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002916 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
2917 "//build/bazel/platforms/os:windows": [":bazlib"],
2918 "//conditions:default": [],
2919 }) + select({
2920 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002921 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002922 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002923 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
2924 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00002925 "//build/bazel/rules/apex:foo": [
2926 "@api_surfaces//module-libapi/current:barlib",
2927 "@api_surfaces//module-libapi/current:quxlib",
2928 ],
Liz Kammerffc17e42022-11-23 09:42:05 -05002929 "//conditions:default": [
2930 ":barlib",
2931 ":quxlib",
2932 ],
2933 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002934 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05002935 }),
2936 })
2937}
2938
Liz Kammerf38a8372022-02-04 15:39:00 -05002939func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002940 runCcLibraryTestCase(t, Bp2buildTestCase{
2941 ModuleTypeUnderTest: "cc_library",
2942 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2943 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05002944 name: "foo",
2945 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2946 include_build_directory: false,
2947}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002948 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05002949 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2950 }),
2951 })
2952}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002953
2954func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002955 runCcLibraryTestCase(t, Bp2buildTestCase{
2956 ModuleTypeUnderTest: "cc_library",
2957 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2958 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002959 "foo.c": "",
2960 "bar.cc": "",
2961 "foo1.l": "",
2962 "bar1.ll": "",
2963 "foo2.l": "",
2964 "bar2.ll": "",
2965 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002966 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002967 name: "foo_lib",
2968 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2969 lex: { flags: ["--foo_flags"] },
2970 include_build_directory: false,
2971 bazel_module: { bp2build_available: true },
2972}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002973 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00002974 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002975 "srcs": `[
2976 "foo1.l",
2977 "foo2.l",
2978 ]`,
2979 "lexopts": `["--foo_flags"]`,
2980 }),
Alixe06d75b2022-08-31 18:28:19 +00002981 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002982 "srcs": `[
2983 "bar1.ll",
2984 "bar2.ll",
2985 ]`,
2986 "lexopts": `["--foo_flags"]`,
2987 }),
2988 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002989 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002990 "srcs": `[
2991 "bar.cc",
2992 ":foo_lib_genlex_ll",
2993 ]`,
2994 "srcs_c": `[
2995 "foo.c",
2996 ":foo_lib_genlex_l",
2997 ]`,
2998 })...),
2999 })
3000}
Cole Faust6b29f592022-08-09 09:50:56 -07003001
3002func TestCCLibraryRuntimeDeps(t *testing.T) {
3003 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3004 Blueprint: `cc_library_shared {
3005 name: "bar",
3006}
3007
3008cc_library {
3009 name: "foo",
3010 runtime_libs: ["foo"],
3011}`,
3012 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003013 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003014 "local_includes": `["."]`,
3015 }),
Alixe06d75b2022-08-31 18:28:19 +00003016 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003017 "runtime_deps": `[":foo"]`,
3018 "local_includes": `["."]`,
3019 }),
Alixe06d75b2022-08-31 18:28:19 +00003020 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003021 "runtime_deps": `[":foo"]`,
3022 "local_includes": `["."]`,
3023 }),
3024 },
3025 })
3026}
Cole Faust5fa4e962022-08-22 14:31:04 -07003027
3028func TestCcLibraryWithInstructionSet(t *testing.T) {
3029 runCcLibraryTestCase(t, Bp2buildTestCase{
3030 ModuleTypeUnderTest: "cc_library",
3031 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3032 Blueprint: `cc_library {
3033 name: "foo",
3034 arch: {
3035 arm: {
3036 instruction_set: "arm",
3037 }
3038 }
3039}
3040`,
3041 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3042 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003043 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003044 "//conditions:default": [],
3045 })`,
3046 "local_includes": `["."]`,
3047 }),
3048 })
3049}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003050
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003051func TestCcLibraryEmptySuffix(t *testing.T) {
3052 runCcLibraryTestCase(t, Bp2buildTestCase{
3053 Description: "cc_library with empty suffix",
3054 ModuleTypeUnderTest: "cc_library",
3055 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3056 Filesystem: map[string]string{
3057 "foo.c": "",
3058 },
3059 Blueprint: `cc_library {
3060 name: "foo",
3061 suffix: "",
3062 srcs: ["foo.c"],
3063 include_build_directory: false,
3064}`,
3065 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003066 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003067 "srcs_c": `["foo.c"]`,
3068 }),
Alixe06d75b2022-08-31 18:28:19 +00003069 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003070 "srcs_c": `["foo.c"]`,
3071 "suffix": `""`,
3072 }),
3073 },
3074 })
3075}
3076
3077func TestCcLibrarySuffix(t *testing.T) {
3078 runCcLibraryTestCase(t, Bp2buildTestCase{
3079 Description: "cc_library with suffix",
3080 ModuleTypeUnderTest: "cc_library",
3081 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3082 Filesystem: map[string]string{
3083 "foo.c": "",
3084 },
3085 Blueprint: `cc_library {
3086 name: "foo",
3087 suffix: "-suf",
3088 srcs: ["foo.c"],
3089 include_build_directory: false,
3090}`,
3091 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003092 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003093 "srcs_c": `["foo.c"]`,
3094 }),
Alixe06d75b2022-08-31 18:28:19 +00003095 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003096 "srcs_c": `["foo.c"]`,
3097 "suffix": `"-suf"`,
3098 }),
3099 },
3100 })
3101}
3102
3103func TestCcLibraryArchVariantSuffix(t *testing.T) {
3104 runCcLibraryTestCase(t, Bp2buildTestCase{
3105 Description: "cc_library with arch-variant suffix",
3106 ModuleTypeUnderTest: "cc_library",
3107 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3108 Filesystem: map[string]string{
3109 "foo.c": "",
3110 },
3111 Blueprint: `cc_library {
3112 name: "foo",
3113 arch: {
3114 arm64: { suffix: "-64" },
3115 arm: { suffix: "-32" },
3116 },
3117 srcs: ["foo.c"],
3118 include_build_directory: false,
3119}`,
3120 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003121 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003122 "srcs_c": `["foo.c"]`,
3123 }),
Alixe06d75b2022-08-31 18:28:19 +00003124 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003125 "srcs_c": `["foo.c"]`,
3126 "suffix": `select({
3127 "//build/bazel/platforms/arch:arm": "-32",
3128 "//build/bazel/platforms/arch:arm64": "-64",
3129 "//conditions:default": None,
3130 })`,
3131 }),
3132 },
3133 })
3134}
3135
Vinh Tran367d89d2023-04-28 11:21:25 -04003136func TestCcLibraryWithAidlLibrary(t *testing.T) {
3137 runCcLibraryTestCase(t, Bp2buildTestCase{
3138 Description: "cc_library with aidl_library",
3139 ModuleTypeUnderTest: "cc_library",
3140 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3141 Blueprint: `
3142aidl_library {
3143 name: "A_aidl",
3144 srcs: ["aidl/A.aidl"],
3145 hdrs: ["aidl/Header.aidl"],
3146 strip_import_prefix: "aidl",
3147}
3148cc_library {
3149 name: "foo",
3150 aidl: {
3151 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003152 },
3153 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003154}`,
3155 ExpectedBazelTargets: []string{
3156 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3157 "srcs": `["aidl/A.aidl"]`,
3158 "hdrs": `["aidl/Header.aidl"]`,
3159 "strip_import_prefix": `"aidl"`,
3160 "tags": `["apex_available=//apex_available:anyapex"]`,
3161 }),
3162 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003163 "deps": `[":A_aidl"]`,
3164 "local_includes": `["."]`,
3165 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003166 }),
3167 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3168 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3169 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003170 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003171 }),
3172 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3173 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3174 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003175 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003176 }),
3177 },
3178 })
3179}
3180
Vinh Tran9f6796a2022-08-16 13:10:31 -04003181func TestCcLibraryWithAidlSrcs(t *testing.T) {
3182 runCcLibraryTestCase(t, Bp2buildTestCase{
3183 Description: "cc_library with aidl srcs",
3184 ModuleTypeUnderTest: "cc_library",
3185 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3186 Blueprint: `
3187filegroup {
3188 name: "A_aidl",
3189 srcs: ["aidl/A.aidl"],
3190 path: "aidl",
3191}
3192cc_library {
3193 name: "foo",
3194 srcs: [
3195 ":A_aidl",
3196 "B.aidl",
3197 ],
3198}`,
3199 ExpectedBazelTargets: []string{
3200 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3201 "srcs": `["aidl/A.aidl"]`,
3202 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003203 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003204 }),
Alixe06d75b2022-08-31 18:28:19 +00003205 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003206 "srcs": `["B.aidl"]`,
3207 }),
Alixe06d75b2022-08-31 18:28:19 +00003208 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003209 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003210 "deps": `[
3211 ":A_aidl",
3212 ":foo_aidl_library",
3213 ]`,
3214 }),
Alixe06d75b2022-08-31 18:28:19 +00003215 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003216 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3217 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003218 }),
Alixe06d75b2022-08-31 18:28:19 +00003219 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003220 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3221 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003222 }),
3223 },
3224 })
3225}
3226
3227func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3228 runCcLibraryTestCase(t, Bp2buildTestCase{
3229 Description: "cc_library with non aidl filegroup",
3230 ModuleTypeUnderTest: "cc_library",
3231 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3232 Filesystem: map[string]string{
3233 "path/to/A/Android.bp": `
3234filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003235 name: "A_aidl",
3236 srcs: ["aidl/A.aidl"],
3237 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003238}`,
3239 },
3240 Blueprint: `
3241cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003242 name: "foo",
3243 srcs: [
3244 ":A_aidl",
3245 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003246}`,
3247 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003248 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003249 "local_includes": `["."]`,
3250 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003251 }),
Alixe06d75b2022-08-31 18:28:19 +00003252 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003253 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3254 "local_includes": `["."]`,
3255 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003256 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003257 "local_includes": `["."]`,
3258 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003259 }),
3260 },
3261 })
3262}
3263
3264func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003265 t.Parallel()
3266
3267 expectedBazelTargets := []string{
3268 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003269 "local_includes": `["."]`,
3270 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003271 }),
3272 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3273 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3274 "local_includes": `["."]`,
3275 }),
3276 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3277 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3278 "local_includes": `["."]`,
3279 }),
3280 }
3281 testCases := []struct {
3282 description string
3283 bp string
3284 expectedBazelTargets []string
3285 }{
3286 {
3287 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3288 bp: `
3289 cc_library {
3290 name: "foo",
3291 srcs: [
3292 "Foo.aidl",
3293 ],
3294 aidl: {
3295 export_aidl_headers: true,
3296 }
3297 }`,
3298 expectedBazelTargets: append(
3299 expectedBazelTargets,
3300 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3301 "srcs": `["Foo.aidl"]`,
3302 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003303 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003304 {
3305 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3306 bp: `
3307 aidl_library {
3308 name: "foo_aidl_library",
3309 srcs: ["Foo.aidl"],
3310 }
3311 cc_library {
3312 name: "foo",
3313 aidl: {
3314 libs: ["foo_aidl_library"],
3315 export_aidl_headers: true,
3316 }
3317 }`,
3318 expectedBazelTargets: append(
3319 expectedBazelTargets,
3320 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3321 "srcs": `["Foo.aidl"]`,
3322 "tags": `["apex_available=//apex_available:anyapex"]`,
3323 }),
3324 ),
3325 },
3326 }
3327
3328 for _, testCase := range testCases {
3329 runCcLibraryTestCase(t, Bp2buildTestCase{
3330 Description: "cc_library with export aidl headers",
3331 ModuleTypeUnderTest: "cc_library",
3332 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3333 Blueprint: testCase.bp,
3334 ExpectedBazelTargets: testCase.expectedBazelTargets,
3335 })
3336 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003337}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003338
3339func TestCcLibraryWithTargetApex(t *testing.T) {
3340 runCcLibraryTestCase(t, Bp2buildTestCase{
3341 Description: "cc_library with target.apex",
3342 ModuleTypeUnderTest: "cc_library",
3343 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3344 Blueprint: `
3345cc_library {
3346 name: "foo",
3347 shared_libs: ["bar", "baz"],
3348 static_libs: ["baz", "buh"],
3349 target: {
3350 apex: {
3351 exclude_shared_libs: ["bar"],
3352 exclude_static_libs: ["buh"],
3353 }
3354 }
3355}`,
3356 ExpectedBazelTargets: []string{
3357 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3358 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003359 "//build/bazel/rules/apex:in_apex": [],
3360 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003361 })`,
3362 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003363 "//build/bazel/rules/apex:in_apex": [],
3364 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003365 })`,
3366 "local_includes": `["."]`,
3367 }),
3368 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3369 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003370 "//build/bazel/rules/apex:in_apex": [],
3371 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003372 })`,
3373 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003374 "//build/bazel/rules/apex:in_apex": [],
3375 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003376 })`,
3377 "local_includes": `["."]`,
3378 }),
3379 },
3380 })
3381}
3382
3383func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3384 runCcLibraryTestCase(t, Bp2buildTestCase{
3385 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3386 ModuleTypeUnderTest: "cc_library",
3387 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3388 Blueprint: `
3389cc_library_static {
3390 name: "foo",
3391 shared_libs: ["bar", "baz"],
3392 static_libs: ["abc"],
3393 export_shared_lib_headers: ["baz"],
3394 export_static_lib_headers: ["abc"],
3395 target: {
3396 apex: {
3397 exclude_shared_libs: ["baz", "bar"],
3398 exclude_static_libs: ["abc"],
3399 }
3400 }
3401}`,
3402 ExpectedBazelTargets: []string{
3403 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3404 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003405 "//build/bazel/rules/apex:in_apex": [],
3406 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003407 })`,
3408 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003409 "//build/bazel/rules/apex:in_apex": [],
3410 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003411 })`,
3412 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003413 "//build/bazel/rules/apex:in_apex": [],
3414 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003415 })`,
3416 "local_includes": `["."]`,
3417 }),
3418 },
3419 })
3420}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003421
3422func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3423 runCcLibraryTestCase(t, Bp2buildTestCase{
3424 Description: "cc_library with sysprop sources",
3425 ModuleTypeUnderTest: "cc_library",
3426 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3427 Blueprint: `
3428cc_library {
3429 name: "foo",
3430 srcs: [
3431 "bar.sysprop",
3432 "baz.sysprop",
3433 "blah.cpp",
3434 ],
3435 min_sdk_version: "5",
3436}`,
3437 ExpectedBazelTargets: []string{
3438 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3439 "srcs": `[
3440 "bar.sysprop",
3441 "baz.sysprop",
3442 ]`,
3443 }),
3444 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3445 "dep": `":foo_sysprop_library"`,
3446 "min_sdk_version": `"5"`,
3447 }),
3448 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3449 "srcs": `["blah.cpp"]`,
3450 "local_includes": `["."]`,
3451 "min_sdk_version": `"5"`,
3452 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3453 }),
3454 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3455 "srcs": `["blah.cpp"]`,
3456 "local_includes": `["."]`,
3457 "min_sdk_version": `"5"`,
3458 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3459 }),
3460 },
3461 })
3462}
3463
3464func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3465 runCcLibraryTestCase(t, Bp2buildTestCase{
3466 Description: "cc_library with sysprop sources in some configs but not others",
3467 ModuleTypeUnderTest: "cc_library",
3468 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3469 Blueprint: `
3470cc_library {
3471 name: "foo",
3472 host_supported: true,
3473 srcs: [
3474 "blah.cpp",
3475 ],
3476 target: {
3477 android: {
3478 srcs: ["bar.sysprop"],
3479 },
3480 },
3481 min_sdk_version: "5",
3482}`,
3483 ExpectedBazelTargets: []string{
3484 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3485 "srcs": `select({
3486 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3487 "//conditions:default": [],
3488 })`,
3489 }),
3490 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3491 "dep": `":foo_sysprop_library"`,
3492 "min_sdk_version": `"5"`,
3493 }),
3494 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3495 "srcs": `["blah.cpp"]`,
3496 "local_includes": `["."]`,
3497 "min_sdk_version": `"5"`,
3498 "whole_archive_deps": `select({
3499 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3500 "//conditions:default": [],
3501 })`,
3502 }),
3503 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3504 "srcs": `["blah.cpp"]`,
3505 "local_includes": `["."]`,
3506 "min_sdk_version": `"5"`,
3507 "whole_archive_deps": `select({
3508 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3509 "//conditions:default": [],
3510 })`,
3511 }),
3512 },
3513 })
3514}
Vinh Tran395a1e92022-09-16 18:27:29 -04003515
Sam Delmerico512437b2023-03-17 11:34:15 -04003516func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003517 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003518 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003519 ModuleTypeUnderTest: "cc_library",
3520 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3521 Blueprint: `
3522cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003523 name: "foo",
3524 srcs: [
3525 "Foo.aidl",
3526 ],
3527 static_libs: [
3528 "bar-static",
3529 "baz-static",
3530 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003531 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003532 "bar-shared",
3533 "baz-shared",
3534 ],
3535 export_static_lib_headers: [
3536 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003537 ],
3538 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003539 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003540 ],
3541}` +
Sam Delmerico130d75b2023-08-31 00:51:44 +00003542 SimpleModuleDoNotConvertBp2build("cc_library_static", "bar-static") +
3543 SimpleModuleDoNotConvertBp2build("cc_library_static", "baz-static") +
3544 SimpleModuleDoNotConvertBp2build("cc_library", "bar-shared") +
3545 SimpleModuleDoNotConvertBp2build("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003546 ExpectedBazelTargets: []string{
3547 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3548 "srcs": `["Foo.aidl"]`,
3549 }),
3550 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003551 "local_includes": `["."]`,
3552 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003553 "implementation_deps": `[
3554 ":baz-static",
3555 ":bar-static",
3556 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003557 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003558 ":baz-shared",
3559 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003560 ]`,
3561 }),
3562 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3563 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003564 "deps": `[":baz-static"]`,
3565 "implementation_deps": `[":bar-static"]`,
3566 "dynamic_deps": `[":baz-shared"]`,
3567 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003568 "local_includes": `["."]`,
3569 }),
3570 },
3571 })
3572}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003573
3574func TestCcLibraryWithTidy(t *testing.T) {
3575 runCcLibraryTestCase(t, Bp2buildTestCase{
3576 Description: "cc_library uses tidy properties",
3577 ModuleTypeUnderTest: "cc_library",
3578 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3579 Blueprint: `
3580cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003581 name: "foo",
3582 srcs: ["foo.cpp"],
3583}
3584cc_library_static {
3585 name: "foo-no-tidy",
3586 srcs: ["foo.cpp"],
3587 tidy: false,
3588}
3589cc_library_static {
3590 name: "foo-tidy",
3591 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003592 tidy: true,
3593 tidy_checks: ["check1", "check2"],
3594 tidy_checks_as_errors: ["check1error", "check2error"],
3595 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003596 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003597}`,
3598 ExpectedBazelTargets: []string{
3599 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3600 "local_includes": `["."]`,
3601 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003602 }),
3603 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3604 "local_includes": `["."]`,
3605 "srcs": `["foo.cpp"]`,
3606 "tidy": `"never"`,
3607 }),
3608 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3609 "local_includes": `["."]`,
3610 "srcs": `["foo.cpp"]`,
3611 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003612 "tidy_checks": `[
3613 "check1",
3614 "check2",
3615 ]`,
3616 "tidy_checks_as_errors": `[
3617 "check1error",
3618 "check2error",
3619 ]`,
3620 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003621 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003622 }),
3623 },
3624 })
3625}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003626
Vinh Tran99270ea2022-11-28 11:15:23 -05003627func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3628 bp := `
3629cc_library {
3630 name: "foo",
3631 afdo: true,
3632 include_build_directory: false,
3633}`
3634
3635 // TODO(b/260714900): Add test case for arch-specific afdo profile
3636 testCases := []struct {
3637 description string
3638 filesystem map[string]string
3639 expectedBazelTargets []string
3640 }{
3641 {
3642 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003643 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003644 "vendor/google_data/pgo_profile/sampling/Android.bp": "",
3645 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003646 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003647 expectedBazelTargets: []string{
3648 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3649 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3650 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3651 }),
3652 },
3653 },
3654 {
3655 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003656 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003657 "toolchain/pgo-profiles/sampling/Android.bp": "",
3658 "toolchain/pgo-profiles/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003659 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003660 expectedBazelTargets: []string{
3661 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3662 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3663 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3664 }),
3665 },
3666 },
3667 {
3668 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003669 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003670 "toolchain/pgo-profiles/sampling/Android.bp": "",
3671 "toolchain/pgo-profiles/sampling/bar.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003672 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003673 expectedBazelTargets: []string{
3674 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3675 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3676 },
3677 },
3678 {
3679 description: "cc_library with afdo enabled but profile doesn't exist",
3680 expectedBazelTargets: []string{
3681 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3682 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3683 },
3684 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003685 {
3686 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3687 filesystem: map[string]string{
3688 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3689 },
3690 expectedBazelTargets: []string{
3691 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3692 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3693 },
3694 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003695 }
3696 for _, testCase := range testCases {
3697 t.Run(testCase.description, func(t *testing.T) {
3698 runCcLibraryTestCase(t, Bp2buildTestCase{
3699 ExpectedBazelTargets: testCase.expectedBazelTargets,
3700 ModuleTypeUnderTest: "cc_library",
3701 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3702 Description: testCase.description,
3703 Blueprint: binaryReplacer.Replace(bp),
3704 Filesystem: testCase.filesystem,
3705 })
3706 })
3707 }
3708}
3709
Yu Liu56ccb1a2022-11-12 10:47:07 -08003710func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3711 runCcLibraryTestCase(t, Bp2buildTestCase{
3712 Description: "cc_library with header abi checker",
3713 ModuleTypeUnderTest: "cc_library",
3714 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3715 Blueprint: `cc_library {
3716 name: "foo",
3717 header_abi_checker: {
3718 enabled: true,
3719 symbol_file: "a.map.txt",
3720 exclude_symbol_versions: [
3721 "29",
3722 "30",
3723 ],
3724 exclude_symbol_tags: [
3725 "tag1",
3726 "tag2",
3727 ],
3728 check_all_apis: true,
3729 diff_flags: ["-allow-adding-removing-weak-symbols"],
3730 },
3731 include_build_directory: false,
3732}`,
3733 ExpectedBazelTargets: []string{
3734 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3735 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3736 "abi_checker_enabled": `True`,
3737 "abi_checker_symbol_file": `"a.map.txt"`,
3738 "abi_checker_exclude_symbol_versions": `[
3739 "29",
3740 "30",
3741 ]`,
3742 "abi_checker_exclude_symbol_tags": `[
3743 "tag1",
3744 "tag2",
3745 ]`,
3746 "abi_checker_check_all_apis": `True`,
3747 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3748 }),
3749 },
3750 })
3751}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003752
3753func TestCcLibraryApexAvailable(t *testing.T) {
3754 runCcLibraryTestCase(t, Bp2buildTestCase{
3755 Description: "cc_library apex_available converted to tags",
3756 ModuleTypeUnderTest: "cc_library",
3757 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3758 Blueprint: soongCcLibraryPreamble + `
3759cc_library {
3760 name: "a",
3761 srcs: ["a.cpp"],
3762 apex_available: ["com.android.foo"],
3763}
3764`,
3765 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3766 "tags": `["apex_available=com.android.foo"]`,
3767 "srcs": `["a.cpp"]`,
3768 "local_includes": `["."]`,
3769 }),
3770 },
3771 )
3772}
3773
3774func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3775 runCcLibraryTestCase(t, Bp2buildTestCase{
3776 Description: "cc_library apex_available converted to multiple tags",
3777 ModuleTypeUnderTest: "cc_library",
3778 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3779 Blueprint: soongCcLibraryPreamble + `
3780cc_library {
3781 name: "a",
3782 srcs: ["a.cpp"],
3783 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3784}
3785`,
3786 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3787 "tags": `[
3788 "apex_available=com.android.foo",
3789 "apex_available=//apex_available:platform",
3790 "apex_available=com.android.bar",
3791 ]`,
3792 "srcs": `["a.cpp"]`,
3793 "local_includes": `["."]`,
3794 }),
3795 },
3796 )
3797}
Zi Wang0f828442022-12-28 11:18:11 -08003798
3799// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3800// In bp2build output, variant info(select) should go before general info.
3801// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3802func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3803 runCcLibraryTestCase(t, Bp2buildTestCase{
3804 Description: "cc_library variant prepend properties order",
3805 ModuleTypeUnderTest: "cc_library",
3806 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3807 Blueprint: soongCcLibraryPreamble + `
3808cc_library {
3809 name: "a",
3810 srcs: ["a.cpp"],
3811 export_include_dirs: ["eid1", "eid2"],
3812 export_system_include_dirs: ["esid1", "esid2"],
3813 target: {
3814 android: {
3815 export_include_dirs: ["android_eid1", "android_eid2"],
3816 export_system_include_dirs: ["android_esid1", "android_esid2"],
3817 },
3818 android_arm: {
3819 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3820 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3821 },
3822 linux: {
3823 export_include_dirs: ["linux_eid1", "linux_eid2"],
3824 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3825 },
3826 },
3827 multilib: {
3828 lib32: {
3829 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3830 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3831 },
3832 },
3833 arch: {
3834 arm: {
3835 export_include_dirs: ["arm_eid1", "arm_eid2"],
3836 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3837 },
3838 }
3839}
3840`,
3841 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3842 "export_includes": `select({
3843 "//build/bazel/platforms/os_arch:android_arm": [
3844 "android_arm_eid1",
3845 "android_arm_eid2",
3846 ],
3847 "//conditions:default": [],
3848 }) + select({
3849 "//build/bazel/platforms/os:android": [
3850 "android_eid1",
3851 "android_eid2",
3852 "linux_eid1",
3853 "linux_eid2",
3854 ],
3855 "//build/bazel/platforms/os:linux_bionic": [
3856 "linux_eid1",
3857 "linux_eid2",
3858 ],
3859 "//build/bazel/platforms/os:linux_glibc": [
3860 "linux_eid1",
3861 "linux_eid2",
3862 ],
3863 "//build/bazel/platforms/os:linux_musl": [
3864 "linux_eid1",
3865 "linux_eid2",
3866 ],
3867 "//conditions:default": [],
3868 }) + select({
3869 "//build/bazel/platforms/arch:arm": [
3870 "lib32_eid1",
3871 "lib32_eid2",
3872 "arm_eid1",
3873 "arm_eid2",
3874 ],
3875 "//build/bazel/platforms/arch:x86": [
3876 "lib32_eid1",
3877 "lib32_eid2",
3878 ],
3879 "//conditions:default": [],
3880 }) + [
3881 "eid1",
3882 "eid2",
3883 ]`,
3884 "export_system_includes": `select({
3885 "//build/bazel/platforms/os_arch:android_arm": [
3886 "android_arm_esid1",
3887 "android_arm_esid2",
3888 ],
3889 "//conditions:default": [],
3890 }) + select({
3891 "//build/bazel/platforms/os:android": [
3892 "android_esid1",
3893 "android_esid2",
3894 "linux_esid1",
3895 "linux_esid2",
3896 ],
3897 "//build/bazel/platforms/os:linux_bionic": [
3898 "linux_esid1",
3899 "linux_esid2",
3900 ],
3901 "//build/bazel/platforms/os:linux_glibc": [
3902 "linux_esid1",
3903 "linux_esid2",
3904 ],
3905 "//build/bazel/platforms/os:linux_musl": [
3906 "linux_esid1",
3907 "linux_esid2",
3908 ],
3909 "//conditions:default": [],
3910 }) + select({
3911 "//build/bazel/platforms/arch:arm": [
3912 "lib32_esid1",
3913 "lib32_esid2",
3914 "arm_esid1",
3915 "arm_esid2",
3916 ],
3917 "//build/bazel/platforms/arch:x86": [
3918 "lib32_esid1",
3919 "lib32_esid2",
3920 ],
3921 "//conditions:default": [],
3922 }) + [
3923 "esid1",
3924 "esid2",
3925 ]`,
3926 "srcs": `["a.cpp"]`,
3927 "local_includes": `["."]`,
3928 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
3929 }),
3930 },
3931 )
3932}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00003933
3934func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
3935 runCcLibraryTestCase(t, Bp2buildTestCase{
3936 Description: "cc_library has correct features when integer_overflow property is provided",
3937 ModuleTypeUnderTest: "cc_library",
3938 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3939 Blueprint: `
3940cc_library {
3941 name: "foo",
3942 sanitize: {
3943 integer_overflow: true,
3944 },
3945}
3946`,
3947 ExpectedBazelTargets: []string{
3948 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3949 "features": `["ubsan_integer_overflow"]`,
3950 "local_includes": `["."]`,
3951 }),
3952 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3953 "features": `["ubsan_integer_overflow"]`,
3954 "local_includes": `["."]`,
3955 }),
3956 },
3957 })
3958}
3959
3960func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
3961 runCcLibraryTestCase(t, Bp2buildTestCase{
3962 Description: "cc_library has correct features when misc_undefined property is provided",
3963 ModuleTypeUnderTest: "cc_library",
3964 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3965 Blueprint: `
3966cc_library {
3967 name: "foo",
3968 sanitize: {
3969 misc_undefined: ["undefined", "nullability"],
3970 },
3971}
3972`,
3973 ExpectedBazelTargets: []string{
3974 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3975 "features": `[
3976 "ubsan_undefined",
3977 "ubsan_nullability",
3978 ]`,
3979 "local_includes": `["."]`,
3980 }),
3981 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3982 "features": `[
3983 "ubsan_undefined",
3984 "ubsan_nullability",
3985 ]`,
3986 "local_includes": `["."]`,
3987 }),
3988 },
3989 })
3990}
3991
Trevor Radcliffeded095c2023-06-12 19:18:28 +00003992func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
3993 runCcLibraryTestCase(t, Bp2buildTestCase{
3994 Description: "cc_library has correct feature when sanitize.blocklist is provided",
3995 ModuleTypeUnderTest: "cc_library",
3996 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3997 Blueprint: `
3998cc_library {
3999 name: "foo",
4000 sanitize: {
4001 blocklist: "foo_blocklist.txt",
4002 },
4003}
4004`,
4005 ExpectedBazelTargets: []string{
4006 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004007 "copts": `select({
4008 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4009 "//conditions:default": [],
4010 })`,
4011 "additional_compiler_inputs": `select({
4012 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4013 "//conditions:default": [],
4014 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004015 "local_includes": `["."]`,
4016 }),
4017 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004018 "copts": `select({
4019 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4020 "//conditions:default": [],
4021 })`,
4022 "additional_compiler_inputs": `select({
4023 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4024 "//conditions:default": [],
4025 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004026 "local_includes": `["."]`,
4027 }),
4028 },
4029 })
4030}
4031
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004032func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4033 runCcLibraryTestCase(t, Bp2buildTestCase{
4034 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4035 ModuleTypeUnderTest: "cc_library",
4036 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4037 Blueprint: `
4038cc_library {
4039 name: "foo",
4040 sanitize: {
4041 misc_undefined: ["undefined", "nullability"],
4042 },
4043 target: {
4044 android: {
4045 sanitize: {
4046 misc_undefined: ["alignment"],
4047 },
4048 },
4049 linux_glibc: {
4050 sanitize: {
4051 integer_overflow: true,
4052 },
4053 },
4054 },
4055}
4056`,
4057 ExpectedBazelTargets: []string{
4058 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4059 "features": `[
4060 "ubsan_undefined",
4061 "ubsan_nullability",
4062 ] + select({
4063 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4064 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4065 "//conditions:default": [],
4066 })`,
4067 "local_includes": `["."]`,
4068 }),
4069 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4070 "features": `[
4071 "ubsan_undefined",
4072 "ubsan_nullability",
4073 ] + select({
4074 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4075 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4076 "//conditions:default": [],
4077 })`,
4078 "local_includes": `["."]`,
4079 }),
4080 },
4081 })
4082}
Yu Liu10174ff2023-02-21 12:05:26 -08004083
4084func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4085 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4086 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4087 ModuleTypeUnderTest: "cc_library",
4088 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4089 Blueprint: `
4090cc_library {
4091 name: "barlib",
4092 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4093 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004094 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004095}
4096cc_library {
4097 name: "bazlib",
4098 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4099 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004100 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004101}
4102cc_library {
4103 name: "foo",
4104 shared_libs: ["barlib", "bazlib"],
4105 export_shared_lib_headers: ["bazlib"],
4106 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004107 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004108 ],
4109}`,
4110 ExpectedBazelTargets: []string{
4111 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004112 "implementation_dynamic_deps": `[":barlib"]`,
4113 "dynamic_deps": `[":bazlib"]`,
4114 "local_includes": `["."]`,
4115 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004116 }),
4117 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004118 "implementation_dynamic_deps": `[":barlib"]`,
4119 "dynamic_deps": `[":bazlib"]`,
4120 "local_includes": `["."]`,
4121 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004122 }),
4123 },
4124 })
4125}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004126
4127func TestCcLibraryWithThinLto(t *testing.T) {
4128 runCcLibraryTestCase(t, Bp2buildTestCase{
4129 Description: "cc_library has correct features when thin LTO is enabled",
4130 ModuleTypeUnderTest: "cc_library",
4131 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4132 Blueprint: `
4133cc_library {
4134 name: "foo",
4135 lto: {
4136 thin: true,
4137 },
4138}`,
4139 ExpectedBazelTargets: []string{
4140 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4141 "features": `["android_thin_lto"]`,
4142 "local_includes": `["."]`,
4143 }),
4144 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4145 "features": `["android_thin_lto"]`,
4146 "local_includes": `["."]`,
4147 }),
4148 },
4149 })
4150}
4151
4152func TestCcLibraryWithLtoNever(t *testing.T) {
4153 runCcLibraryTestCase(t, Bp2buildTestCase{
4154 Description: "cc_library has correct features when LTO is explicitly disabled",
4155 ModuleTypeUnderTest: "cc_library",
4156 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4157 Blueprint: `
4158cc_library {
4159 name: "foo",
4160 lto: {
4161 never: true,
4162 },
4163}`,
4164 ExpectedBazelTargets: []string{
4165 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4166 "features": `["-android_thin_lto"]`,
4167 "local_includes": `["."]`,
4168 }),
4169 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4170 "features": `["-android_thin_lto"]`,
4171 "local_includes": `["."]`,
4172 }),
4173 },
4174 })
4175}
4176
4177func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4178 runCcLibraryTestCase(t, Bp2buildTestCase{
4179 Description: "cc_library has correct features when LTO differs across arch and os variants",
4180 ModuleTypeUnderTest: "cc_library",
4181 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4182 Blueprint: `
4183cc_library {
4184 name: "foo",
4185 target: {
4186 android: {
4187 lto: {
4188 thin: true,
4189 },
4190 },
4191 },
4192 arch: {
4193 riscv64: {
4194 lto: {
4195 thin: false,
4196 },
4197 },
4198 },
4199}`,
4200 ExpectedBazelTargets: []string{
4201 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4202 "local_includes": `["."]`,
4203 "features": `select({
4204 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4205 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4206 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4207 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4208 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4209 "//conditions:default": [],
4210 })`}),
4211 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4212 "local_includes": `["."]`,
4213 "features": `select({
4214 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4215 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4216 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4217 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4218 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4219 "//conditions:default": [],
4220 })`}),
4221 },
4222 })
4223}
4224
4225func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4226 runCcLibraryTestCase(t, Bp2buildTestCase{
4227 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4228 ModuleTypeUnderTest: "cc_library",
4229 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4230 Blueprint: `
4231cc_library {
4232 name: "foo",
4233 lto: {
4234 never: true,
4235 },
4236 target: {
4237 android: {
4238 lto: {
4239 thin: true,
4240 never: false,
4241 },
4242 },
4243 },
4244}`,
4245 ExpectedBazelTargets: []string{
4246 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4247 "local_includes": `["."]`,
4248 "features": `select({
4249 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4250 "//conditions:default": ["-android_thin_lto"],
4251 })`,
4252 }),
4253 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4254 "local_includes": `["."]`,
4255 "features": `select({
4256 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4257 "//conditions:default": ["-android_thin_lto"],
4258 })`,
4259 }),
4260 },
4261 })
4262}
4263
4264func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4265 runCcLibraryTestCase(t, Bp2buildTestCase{
4266 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4267 ModuleTypeUnderTest: "cc_library",
4268 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4269 Blueprint: `
4270cc_library {
4271 name: "foo",
4272 lto: {
4273 thin: true,
4274 },
4275 whole_program_vtables: true,
4276}`,
4277 ExpectedBazelTargets: []string{
4278 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4279 "features": `[
4280 "android_thin_lto",
4281 "android_thin_lto_whole_program_vtables",
4282 ]`,
4283 "local_includes": `["."]`,
4284 }),
4285 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4286 "features": `[
4287 "android_thin_lto",
4288 "android_thin_lto_whole_program_vtables",
4289 ]`,
4290 "local_includes": `["."]`,
4291 }),
4292 },
4293 })
4294}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004295
4296func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4297 runCcLibraryTestCase(t, Bp2buildTestCase{
4298 Description: "cc_library changes hidden visibility flag to feature",
4299 ModuleTypeUnderTest: "cc_library",
4300 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4301 Blueprint: `
4302cc_library {
4303 name: "foo",
4304 cflags: ["-fvisibility=hidden"],
4305}`,
4306 ExpectedBazelTargets: []string{
4307 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4308 "features": `["visibility_hidden"]`,
4309 "local_includes": `["."]`,
4310 }),
4311 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4312 "features": `["visibility_hidden"]`,
4313 "local_includes": `["."]`,
4314 }),
4315 },
4316 })
4317}
4318
4319func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4320 runCcLibraryTestCase(t, Bp2buildTestCase{
4321 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4322 ModuleTypeUnderTest: "cc_library",
4323 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4324 Blueprint: `
4325cc_library {
4326 name: "foo",
4327 shared: {
4328 cflags: ["-fvisibility=hidden"],
4329 },
4330}`,
4331 ExpectedBazelTargets: []string{
4332 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4333 "local_includes": `["."]`,
4334 }),
4335 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4336 "features": `["visibility_hidden"]`,
4337 "local_includes": `["."]`,
4338 }),
4339 },
4340 })
4341}
4342
4343func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4344 runCcLibraryTestCase(t, Bp2buildTestCase{
4345 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4346 ModuleTypeUnderTest: "cc_library",
4347 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4348 Blueprint: `
4349cc_library {
4350 name: "foo",
4351 static: {
4352 cflags: ["-fvisibility=hidden"],
4353 },
4354}`,
4355 ExpectedBazelTargets: []string{
4356 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4357 "features": `["visibility_hidden"]`,
4358 "local_includes": `["."]`,
4359 }),
4360 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4361 "local_includes": `["."]`,
4362 }),
4363 },
4364 })
4365}
4366
4367func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4368 runCcLibraryTestCase(t, Bp2buildTestCase{
4369 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4370 ModuleTypeUnderTest: "cc_library",
4371 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4372 Blueprint: `
4373cc_library {
4374 name: "foo",
4375 target: {
4376 android: {
4377 cflags: ["-fvisibility=hidden"],
4378 },
4379 },
4380}`,
4381 ExpectedBazelTargets: []string{
4382 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4383 "features": `select({
4384 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4385 "//conditions:default": [],
4386 })`,
4387 "local_includes": `["."]`,
4388 }),
4389 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4390 "features": `select({
4391 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4392 "//conditions:default": [],
4393 })`,
4394 "local_includes": `["."]`,
4395 }),
4396 },
4397 })
4398}
Spandan Das4242f102023-04-19 22:31:54 +00004399
4400// Test that a config_setting specific to an apex is created by cc_library.
4401func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4402 runCcLibraryTestCase(t, Bp2buildTestCase{
4403 Description: "cc_library creates a config_setting for each apex in apex_available",
4404 ModuleTypeUnderTest: "cc_library",
4405 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4406 Dir: "build/bazel/rules/apex",
4407 Blueprint: `
4408cc_library {
4409 name: "foo",
4410 apex_available: [
4411 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4412 "myapex"
4413 ],
4414}`,
4415 ExpectedBazelTargets: []string{
4416 MakeBazelTargetNoRestrictions(
4417 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004418 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004419 AttrNameToString{
4420 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004421 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004422 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004423 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004424 },
4425 ),
4426 },
4427 })
4428}
Yu Liu93893ba2023-05-01 13:49:52 -07004429
4430func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4431 runCcLibraryTestCase(t, Bp2buildTestCase{
4432 Description: "cc_library cppflags in product variables",
4433 ModuleTypeUnderTest: "cc_library",
4434 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4435 Blueprint: soongCcLibraryPreamble + `cc_library {
4436 name: "a",
4437 srcs: ["a.cpp"],
4438 cppflags: [
4439 "-Wextra",
4440 "-DDEBUG_ONLY_CODE=0",
4441 ],
4442 product_variables: {
4443 eng: {
4444 cppflags: [
4445 "-UDEBUG_ONLY_CODE",
4446 "-DDEBUG_ONLY_CODE=1",
4447 ],
4448 },
4449 },
4450 include_build_directory: false,
4451}
4452`,
4453 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4454 "cppflags": `[
4455 "-Wextra",
4456 "-DDEBUG_ONLY_CODE=0",
4457 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004458 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004459 "-UDEBUG_ONLY_CODE",
4460 "-DDEBUG_ONLY_CODE=1",
4461 ],
4462 "//conditions:default": [],
4463 })`,
4464 "srcs": `["a.cpp"]`,
4465 }),
4466 },
4467 )
4468}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004469
4470func TestCcLibraryYaccConversion(t *testing.T) {
4471 runCcLibraryTestCase(t, Bp2buildTestCase{
4472 Description: "cc_library is built from .y/.yy files",
4473 ModuleTypeUnderTest: "cc_library",
4474 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4475 Blueprint: soongCcLibraryPreamble + `cc_library {
4476 name: "a",
4477 srcs: [
4478 "a.cpp",
4479 "a.yy",
4480 ],
4481 shared_libs: ["sharedlib"],
4482 static_libs: ["staticlib"],
4483 yacc: {
4484 flags: ["someYaccFlag"],
4485 gen_location_hh: true,
4486 gen_position_hh: true,
4487 },
4488}
4489cc_library_static {
4490 name: "staticlib",
4491 bazel_module: { bp2build_available: false },
4492}
4493cc_library {
4494 name: "sharedlib",
4495 bazel_module: { bp2build_available: false },
4496}
4497`,
4498 ExpectedBazelTargets: []string{
4499 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4500 "src": `"a.yy"`,
4501 "implementation_deps": `[":staticlib"]`,
4502 "implementation_dynamic_deps": `[":sharedlib"]`,
4503 "flags": `["someYaccFlag"]`,
4504 "gen_location_hh": "True",
4505 "gen_position_hh": "True",
4506 "local_includes": `["."]`,
4507 }),
4508 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4509 "srcs": `["a.cpp"]`,
4510 "implementation_deps": `[":staticlib"]`,
4511 "implementation_dynamic_deps": `[":sharedlib"]`,
4512 "implementation_whole_archive_deps": `[":a_yacc"]`,
4513 "local_includes": `["."]`,
4514 }),
4515 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4516 "srcs": `["a.cpp"]`,
4517 "implementation_deps": `[":staticlib"]`,
4518 "implementation_dynamic_deps": `[":sharedlib"]`,
4519 "implementation_whole_archive_deps": `[":a_yacc"]`,
4520 "local_includes": `["."]`,
4521 }),
4522 },
4523 })
4524}
Spandan Dasfb04c412023-05-15 18:35:36 +00004525
4526func TestCcLibraryHostLdLibs(t *testing.T) {
4527 runCcLibraryTestCase(t, Bp2buildTestCase{
4528 Description: "cc_binary linker flags for host_ldlibs",
4529 ModuleTypeUnderTest: "cc_binary",
4530 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4531 Blueprint: soongCcLibraryPreamble + `cc_binary {
4532 name: "a",
4533 host_supported: true,
4534 ldflags: ["-lcommon"],
4535 target: {
4536 linux: {
4537 host_ldlibs: [
4538 "-llinux",
4539 ],
4540 },
4541 darwin: {
4542 ldflags: ["-ldarwinadditional"],
4543 host_ldlibs: [
4544 "-ldarwin",
4545 ],
4546 },
4547 windows: {
4548 host_ldlibs: [
4549 "-lwindows",
4550 ],
4551 },
4552 },
4553}
4554`,
4555 ExpectedBazelTargets: []string{
4556 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4557 "linkopts": `["-lcommon"] + select({
4558 "//build/bazel/platforms/os:darwin": [
4559 "-ldarwinadditional",
4560 "-ldarwin",
4561 ],
4562 "//build/bazel/platforms/os:linux_glibc": ["-llinux"],
4563 "//build/bazel/platforms/os:windows": ["-lwindows"],
4564 "//conditions:default": [],
4565 })`,
4566 "local_includes": `["."]`,
4567 }),
4568 },
4569 })
4570}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004571
4572func TestCcLibraryWithCfi(t *testing.T) {
4573 runCcLibraryTestCase(t, Bp2buildTestCase{
4574 Description: "cc_library has correct features when cfi is enabled",
4575 ModuleTypeUnderTest: "cc_library",
4576 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4577 Blueprint: `
4578cc_library {
4579 name: "foo",
4580 sanitize: {
4581 cfi: true,
4582 },
4583}`,
4584 ExpectedBazelTargets: []string{
4585 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4586 "features": `["android_cfi"]`,
4587 "local_includes": `["."]`,
4588 }),
4589 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4590 "features": `["android_cfi"]`,
4591 "local_includes": `["."]`,
4592 }),
4593 },
4594 })
4595}
4596
4597func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4598 runCcLibraryTestCase(t, Bp2buildTestCase{
4599 Description: "cc_library has correct features when cfi is enabled for specific variants",
4600 ModuleTypeUnderTest: "cc_library",
4601 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4602 Blueprint: `
4603cc_library {
4604 name: "foo",
4605 target: {
4606 android: {
4607 sanitize: {
4608 cfi: true,
4609 },
4610 },
4611 },
4612}`,
4613 ExpectedBazelTargets: []string{
4614 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4615 "features": `select({
4616 "//build/bazel/platforms/os:android": ["android_cfi"],
4617 "//conditions:default": [],
4618 })`,
4619 "local_includes": `["."]`,
4620 }),
4621 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4622 "features": `select({
4623 "//build/bazel/platforms/os:android": ["android_cfi"],
4624 "//conditions:default": [],
4625 })`,
4626 "local_includes": `["."]`,
4627 }),
4628 },
4629 })
4630}
4631
4632func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4633 runCcLibraryTestCase(t, Bp2buildTestCase{
4634 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4635 ModuleTypeUnderTest: "cc_library",
4636 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4637 Blueprint: `
4638cc_library {
4639 name: "foo",
4640 sanitize: {
4641 cfi: true,
4642 config: {
4643 cfi_assembly_support: true,
4644 },
4645 },
4646}`,
4647 ExpectedBazelTargets: []string{
4648 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4649 "features": `[
4650 "android_cfi",
4651 "android_cfi_assembly_support",
4652 ]`,
4653 "local_includes": `["."]`,
4654 }),
4655 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4656 "features": `[
4657 "android_cfi",
4658 "android_cfi_assembly_support",
4659 ]`,
4660 "local_includes": `["."]`,
4661 }),
4662 },
4663 })
4664}
Spandan Das39ccf932023-05-26 18:03:39 +00004665
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004666func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4667 runCcLibraryTestCase(t, Bp2buildTestCase{
4668 Description: "cc_library disables cfi when explciitly set to false in the bp",
4669 ModuleTypeUnderTest: "cc_library",
4670 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4671 Blueprint: `
4672cc_library {
4673 name: "foo",
4674 sanitize: {
4675 cfi: false,
4676 },
4677}
4678`,
4679 ExpectedBazelTargets: []string{
4680 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4681 "features": `["-android_cfi"]`,
4682 "local_includes": `["."]`,
4683 }),
4684 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4685 "features": `["-android_cfi"]`,
4686 "local_includes": `["."]`,
4687 }),
4688 },
4689 })
4690}
4691
Spandan Das39ccf932023-05-26 18:03:39 +00004692func TestCcLibraryWithStem(t *testing.T) {
4693 runCcLibraryTestCase(t, Bp2buildTestCase{
4694 Description: "cc_library with stem property",
4695 ModuleTypeUnderTest: "cc_library_shared",
4696 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4697 Blueprint: soongCcLibraryPreamble + `
4698cc_library_shared {
4699 name: "foo_with_stem_simple",
4700 stem: "foo",
4701}
4702cc_library_shared {
4703 name: "foo_with_arch_variant_stem",
4704 arch: {
4705 arm: {
4706 stem: "foo-arm",
4707 },
4708 arm64: {
4709 stem: "foo-arm64",
4710 },
4711 },
4712}
4713`,
4714 ExpectedBazelTargets: []string{
4715 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4716 "stem": `"foo"`,
4717 "local_includes": `["."]`,
4718 }),
4719 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4720 "stem": `select({
4721 "//build/bazel/platforms/arch:arm": "foo-arm",
4722 "//build/bazel/platforms/arch:arm64": "foo-arm64",
4723 "//conditions:default": None,
4724 })`,
4725 "local_includes": `["."]`,
4726 }),
4727 },
4728 })
4729}
Spandan Dasc53767e2023-08-03 23:02:26 +00004730
4731// Bazel enforces that proto_library and the .proto file are in the same bazel package
4732func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4733 tc := Bp2buildTestCase{
4734 Description: "cc_library depends on .proto files from multiple packages",
4735 ModuleTypeUnderTest: "cc_library",
4736 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4737 Blueprint: `
4738cc_library_static {
4739 name: "foo",
4740 srcs: [
4741 "foo.proto",
4742 "bar/bar.proto", // Different package because there is a bar/Android.bp
4743 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4744 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004745 proto: {
4746 canonical_path_from_root: true,
4747 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004748}
Sam Delmerico130d75b2023-08-31 00:51:44 +00004749` + SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasc53767e2023-08-03 23:02:26 +00004750 Filesystem: map[string]string{
4751 "bar/Android.bp": "",
4752 "baz/subbaz/Android.bp": "",
4753 },
4754 }
4755
4756 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4757 // Root dir
4758 tc.ExpectedBazelTargets = []string{
4759 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4760 "local_includes": `["."]`,
4761 "deps": `[":libprotobuf-cpp-lite"]`,
4762 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4763 }),
4764 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4765 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004766 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004767 }),
4768 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4769 "deps": `[
4770 ":foo_proto",
4771 "//bar:foo_proto",
4772 "//baz/subbaz:foo_proto",
4773 ]`,
4774 }),
4775 }
4776 runCcLibraryTestCase(t, tc)
4777
4778 // bar dir
4779 tc.Dir = "bar"
4780 tc.ExpectedBazelTargets = []string{
4781 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004782 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004783 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004784 }),
4785 }
4786 runCcLibraryTestCase(t, tc)
4787
4788 // baz/subbaz dir
4789 tc.Dir = "baz/subbaz"
4790 tc.ExpectedBazelTargets = []string{
4791 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004792 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004793 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004794 }),
4795 }
4796 runCcLibraryTestCase(t, tc)
4797}
4798
4799// Bazel enforces that proto_library and the .proto file are in the same bazel package
4800func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
4801 tc := Bp2buildTestCase{
4802 Description: "cc_library depends on .proto files from multiple packages",
4803 ModuleTypeUnderTest: "cc_library",
4804 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4805 Blueprint: `
4806cc_library_static {
4807 name: "foo",
4808 srcs: [
4809 "foo.proto",
4810 "bar/bar.proto", // Different package because there is a bar/Android.bp
4811 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4812 ],
4813 proto: {
4814 canonical_path_from_root: false,
4815 }
4816}
Sam Delmerico130d75b2023-08-31 00:51:44 +00004817` + SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004818 Filesystem: map[string]string{
4819 "bar/Android.bp": "",
4820 "baz/subbaz/Android.bp": "",
4821 },
4822 }
4823
4824 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4825 // Root dir
4826 tc.ExpectedBazelTargets = []string{
4827 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4828 "local_includes": `["."]`,
4829 "deps": `[":libprotobuf-cpp-lite"]`,
4830 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4831 }),
4832 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4833 "srcs": `["foo.proto"]`,
4834 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004835 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004836 }),
4837 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4838 "deps": `[
4839 ":foo_proto",
4840 "//bar:foo_proto",
4841 "//baz/subbaz:foo_proto",
4842 ]`,
4843 }),
4844 }
4845 runCcLibraryTestCase(t, tc)
4846
4847 // bar dir
4848 tc.Dir = "bar"
4849 tc.ExpectedBazelTargets = []string{
4850 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4851 "srcs": `["//bar:bar.proto"]`,
4852 "strip_import_prefix": `""`,
4853 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00004854 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004855 }),
4856 }
4857 runCcLibraryTestCase(t, tc)
4858
4859 // baz/subbaz dir
4860 tc.Dir = "baz/subbaz"
4861 tc.ExpectedBazelTargets = []string{
4862 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4863 "srcs": `["//baz/subbaz:baz.proto"]`,
4864 "strip_import_prefix": `""`,
4865 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00004866 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004867 }),
4868 }
4869 runCcLibraryTestCase(t, tc)
4870}
Spandan Dasec39d512023-08-15 22:08:18 +00004871
4872func TestProtoIncludeDirs(t *testing.T) {
4873 tc := Bp2buildTestCase{
4874 Description: "cc_library depends on .proto files using proto.include_dirs",
4875 ModuleTypeUnderTest: "cc_library",
4876 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4877 Blueprint: `
4878cc_library_static {
4879 name: "foo",
4880 srcs: [
4881 "foo.proto",
4882 ],
4883 proto: {
4884 include_dirs: ["bar"],
4885 }
4886}
Sam Delmerico130d75b2023-08-31 00:51:44 +00004887` + SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasec39d512023-08-15 22:08:18 +00004888 Filesystem: map[string]string{
4889 "bar/Android.bp": "",
4890 "bar/bar.proto": "",
4891 "bar/baz/Android.bp": "",
4892 "bar/baz/baz.proto": "",
4893 },
4894 }
4895
4896 // We will run the test 3 times and check in the root, bar and bar/baz directories
4897 // Root dir
4898 tc.ExpectedBazelTargets = []string{
4899 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4900 "local_includes": `["."]`,
4901 "deps": `[":libprotobuf-cpp-lite"]`,
4902 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4903 }),
4904 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4905 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004906 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00004907 }),
4908 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4909 "deps": `[":foo_proto"]`,
4910 "transitive_deps": `[
4911 "//bar:bar.include_dir_bp2build_generated_proto",
4912 "//bar/baz:bar.include_dir_bp2build_generated_proto",
4913 ]`,
4914 }),
4915 }
4916 runCcLibraryTestCase(t, tc)
4917
4918 // bar dir
4919 tc.Dir = "bar"
4920 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004921 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004922 "srcs": `["bar.proto"]`,
4923 "strip_import_prefix": `""`,
4924 "tags": `["manual"]`,
4925 }),
4926 }
4927 runCcLibraryTestCase(t, tc)
4928
4929 // bar/baz dir
4930 tc.Dir = "bar/baz"
4931 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004932 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004933 "srcs": `["//bar/baz:baz.proto"]`,
4934 "strip_import_prefix": `""`,
4935 "import_prefix": `"baz"`,
4936 "tags": `["manual"]`,
4937 }),
4938 }
4939 runCcLibraryTestCase(t, tc)
4940}
Spandan Das4e5a1942023-08-22 19:20:39 +00004941
Spandan Dasf26ee152023-08-24 23:21:04 +00004942func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
4943 tc := Bp2buildTestCase{
4944 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
4945 ModuleTypeUnderTest: "cc_library",
4946 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4947 Blueprint: `
4948cc_library_static {
4949 name: "foo",
4950 srcs: [
4951 "foo.proto",
4952 "bar/bar.proto",
4953 ],
4954 proto: {
4955 include_dirs: ["baz"],
4956 }
4957}
Sam Delmerico130d75b2023-08-31 00:51:44 +00004958` + SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasf26ee152023-08-24 23:21:04 +00004959 Filesystem: map[string]string{
4960 "bar/Android.bp": "", // package boundary
4961 "baz/Android.bp": "",
4962 "baz/baz.proto": "",
4963 },
4964 }
4965
4966 tc.ExpectedBazelTargets = []string{
4967 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4968 "local_includes": `["."]`,
4969 "deps": `[":libprotobuf-cpp-lite"]`,
4970 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4971 }),
4972 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4973 "srcs": `["foo.proto"]`,
4974 "tags": `["manual"]`,
4975 }),
4976 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4977 "deps": `[
4978 ":foo_proto",
4979 "//bar:foo_proto",
4980 ]`,
4981 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
4982 }),
4983 }
4984 runCcLibraryTestCase(t, tc)
4985
4986}
4987
Spandan Das4e5a1942023-08-22 19:20:39 +00004988func TestProtoLocalIncludeDirs(t *testing.T) {
4989 tc := Bp2buildTestCase{
4990 Description: "cc_library depends on .proto files using proto.local_include_dirs",
4991 ModuleTypeUnderTest: "cc_library",
4992 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Sam Delmerico130d75b2023-08-31 00:51:44 +00004993 Blueprint: SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Spandan Das4e5a1942023-08-22 19:20:39 +00004994 Filesystem: map[string]string{
4995 "foo/Android.bp": `cc_library_static {
4996 name: "foo",
4997 srcs: [
4998 "foo.proto",
4999 ],
5000 proto: {
5001 local_include_dirs: ["foo_subdir"],
5002 },
5003 bazel_module: { bp2build_available: true },
5004}`,
5005 "foo/foo.proto": "",
5006 "foo/foo_subdir/Android.bp": "",
5007 "foo/foo_subdir/foo_subdir.proto": "",
5008 },
5009 }
5010
5011 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5012 // foo dir
5013 tc.Dir = "foo"
5014 tc.ExpectedBazelTargets = []string{
5015 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5016 "local_includes": `["."]`,
5017 "deps": `["//:libprotobuf-cpp-lite"]`,
5018 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5019 }),
5020 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5021 "srcs": `["foo.proto"]`,
5022 "tags": `["manual"]`,
5023 }),
5024 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5025 "deps": `[":foo_proto"]`,
5026 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5027 }),
5028 }
5029 runCcLibraryTestCase(t, tc)
5030
5031 // foo/foo_subdir
5032 tc.Dir = "foo/foo_subdir"
5033 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005034 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005035 "srcs": `["foo_subdir.proto"]`,
5036 "strip_import_prefix": `""`,
5037 "tags": `["manual"]`,
5038 }),
5039 }
5040 runCcLibraryTestCase(t, tc)
5041}
Spandan Dasab29f572023-09-01 19:43:56 +00005042
5043// `foo_device` and `bar_host` can depend on .proto files of a specific dir,
5044// the dynamically generated proto_library should not have any target_compatible_with
5045func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) {
5046 tc := Bp2buildTestCase{
5047 Description: "proto_library generated for proto.include_dirs is compatible for all axes",
5048 ModuleTypeUnderTest: "cc_library",
5049 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Sam Delmerico130d75b2023-08-31 00:51:44 +00005050 Blueprint: SimpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite") + `
Spandan Dasab29f572023-09-01 19:43:56 +00005051cc_library {
5052 name: "foo_device",
5053 device_supported: true, // this is the default behavior, but added explicitly here for illustration
5054 host_supported: false,
5055 proto: {include_dirs: ["dir"]},
5056}
5057cc_library {
5058 name: "bar_host",
5059 device_supported: false,
5060 host_supported: true,
5061 srcs: ["bar.proto"],
5062 proto: {include_dirs: ["dir"]},
5063}
5064`,
5065 Filesystem: map[string]string{
5066 "dir/Android.bp": "",
5067 "dir/dir.proto": "",
5068 },
5069 Dir: "dir", // check for the generated proto_library
5070 ExpectedBazelTargets: []string{
5071 MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{
5072 "srcs": `["dir.proto"]`,
5073 "strip_import_prefix": `""`,
5074 "tags": `["manual"]`,
5075 }),
5076 },
5077 }
5078 runCcLibraryTestCase(t, tc)
5079}
Spandan Dase1cb14b2023-09-05 19:31:12 +00005080
5081func TestCcCompileMultilibConversion(t *testing.T) {
5082 tc := Bp2buildTestCase{
5083 Description: "cc_library with compile_multilib",
5084 ModuleTypeUnderTest: "cc_library",
5085 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5086 Blueprint: `
5087cc_library {
5088 name: "lib32",
5089 compile_multilib: "32",
5090}
5091cc_library {
5092 name: "lib64",
5093 compile_multilib: "64",
5094}
5095`,
5096 ExpectedBazelTargets: []string{
5097 MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{
5098 "local_includes": `["."]`,
5099 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5100 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5101 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5102 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5103 "//conditions:default": [],
5104 })`,
5105 }),
5106 MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{
5107 "local_includes": `["."]`,
5108 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5109 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5110 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5111 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5112 "//conditions:default": [],
5113 })`,
5114 }),
5115 MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{
5116 "local_includes": `["."]`,
5117 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5118 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5119 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5120 "//conditions:default": [],
5121 })`,
5122 }),
5123 MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{
5124 "local_includes": `["."]`,
5125 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5126 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5127 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5128 "//conditions:default": [],
5129 })`,
5130 }),
5131 },
5132 }
5133 runCcLibraryTestCase(t, tc)
5134}
Spandan Das63acae92023-09-14 22:34:34 +00005135
5136func TestNdkLibraryConversion(t *testing.T) {
5137 tc := Bp2buildTestCase{
5138 Description: "ndk_library conversion",
5139 ModuleTypeUnderTest: "cc_library",
5140 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5141 Blueprint: `
5142cc_library {
5143 name: "libfoo",
5144 bazel_module: { bp2build_available: false },
5145}
5146ndk_library {
5147 name: "libfoo",
5148 first_version: "29",
5149 symbol_file: "libfoo.map.txt",
5150}
5151`,
5152 ExpectedBazelTargets: []string{
5153 MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{
5154 "api_surface": `"publicapi"`,
5155 "soname": `"libfoo.so"`,
5156 "source_library_label": `"//:libfoo"`,
5157 "symbol_file": `"libfoo.map.txt"`,
5158 "versions": `[
5159 "29",
5160 "30",
5161 "S",
5162 "Tiramisu",
5163 "current",
5164 ]`,
5165 }),
5166 },
5167 }
5168 runCcLibraryTestCase(t, tc)
5169}