blob: 728225ae3a5e75c221cb004d008a13898bbccf7a [file] [log] [blame]
Jingwen Chen63930982021-03-24 10:04:33 -04001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
Jingwen Chen6ada5892021-09-17 11:38:09 +000018 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000019 "testing"
20
Jingwen Chen63930982021-03-24 10:04:33 -040021 "android/soong/android"
22 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040023)
24
25const (
26 // See cc/testing.go for more context
27 soongCcLibraryPreamble = `
28cc_defaults {
Liz Kammer8337ea42021-09-10 10:06:32 -040029 name: "linux_bionic_supported",
Liz Kammerbaced712022-09-16 09:01:29 -040030}
31`
32
33 soongCcVersionLibBpPath = "build/soong/cc/libbuildversion/Android.bp"
34 soongCcVersionLibBp = `
35cc_library_static {
36 name: "libbuildversion",
37 bazel_module: { bp2build_available: false },
38}
39`
Liz Kammer12615db2021-09-28 09:19:17 -040040
41 soongCcProtoLibraries = `
42cc_library {
43 name: "libprotobuf-cpp-lite",
44 bazel_module: { bp2build_available: false },
45}
46
47cc_library {
48 name: "libprotobuf-cpp-full",
49 bazel_module: { bp2build_available: false },
50}`
51
52 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040053)
54
Sam Delmerico3177a6e2022-06-21 19:28:33 +000055func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040056 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000057 RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020058}
59
60func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
61 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000062 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020063 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040064 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020065 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
66}
67
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020068func TestCcLibrarySimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000069 runCcLibraryTestCase(t, Bp2buildTestCase{
70 Description: "cc_library - simple example",
71 ModuleTypeUnderTest: "cc_library",
72 ModuleTypeUnderTestFactory: cc.LibraryFactory,
73 Filesystem: map[string]string{
Liz Kammerbaced712022-09-16 09:01:29 -040074 soongCcVersionLibBpPath: soongCcVersionLibBp,
75 "android.cpp": "",
76 "bionic.cpp": "",
77 "darwin.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020078 // Refer to cc.headerExts for the supported header extensions in Soong.
79 "header.h": "",
80 "header.hh": "",
81 "header.hpp": "",
82 "header.hxx": "",
83 "header.h++": "",
84 "header.inl": "",
85 "header.inc": "",
86 "header.ipp": "",
87 "header.h.generic": "",
88 "impl.cpp": "",
89 "linux.cpp": "",
90 "x86.cpp": "",
91 "x86_64.cpp": "",
92 "foo-dir/a.h": "",
93 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000094 Blueprint: soongCcLibraryPreamble +
Liz Kammer78cfdaa2021-11-08 12:56:31 -050095 simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -040096cc_library {
97 name: "foo-lib",
98 srcs: ["impl.cpp"],
99 cflags: ["-Wall"],
100 header_libs: ["some-headers"],
101 export_include_dirs: ["foo-dir"],
102 ldflags: ["-Wl,--exclude-libs=bar.a"],
103 arch: {
104 x86: {
105 ldflags: ["-Wl,--exclude-libs=baz.a"],
106 srcs: ["x86.cpp"],
107 },
108 x86_64: {
109 ldflags: ["-Wl,--exclude-libs=qux.a"],
110 srcs: ["x86_64.cpp"],
111 },
112 },
113 target: {
114 android: {
115 srcs: ["android.cpp"],
116 },
117 linux_glibc: {
118 srcs: ["linux.cpp"],
119 },
120 darwin: {
121 srcs: ["darwin.cpp"],
122 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400123 bionic: {
124 srcs: ["bionic.cpp"]
125 },
Jingwen Chen63930982021-03-24 10:04:33 -0400126 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400127 include_build_directory: false,
Yu Liufc603162022-03-01 15:44:08 -0800128 sdk_version: "current",
129 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700130 use_version_lib: true,
Jingwen Chen63930982021-03-24 10:04:33 -0400131}
132`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000133 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500134 "copts": `["-Wall"]`,
135 "export_includes": `["foo-dir"]`,
136 "implementation_deps": `[":some-headers"]`,
137 "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000138 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
139 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
140 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500141 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500142 "srcs": `["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000143 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
144 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400145 "//conditions:default": [],
146 }) + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400147 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400148 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -0400149 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400150 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000151 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
152 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400153 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Liz Kammer01a16e82021-07-16 16:33:47 -0400154 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500155 })`,
Liz Kammerbaced712022-09-16 09:01:29 -0400156 "sdk_version": `"current"`,
157 "min_sdk_version": `"29"`,
158 "use_version_lib": `True`,
159 "implementation_whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500160 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500161 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200162}
163
164func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000165 runCcLibraryTestCase(t, Bp2buildTestCase{
166 Description: "cc_library - trimmed example of //bionic/linker:ld-android",
167 ModuleTypeUnderTest: "cc_library",
168 ModuleTypeUnderTestFactory: cc.LibraryFactory,
169 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200170 "ld-android.cpp": "",
171 "linked_list.h": "",
172 "linker.h": "",
173 "linker_block_allocator.h": "",
174 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400175 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000176 Blueprint: soongCcLibraryPreamble +
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400177 simpleModuleDoNotConvertBp2build("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400178cc_library {
179 name: "fake-ld-android",
180 srcs: ["ld_android.cpp"],
181 cflags: [
182 "-Wall",
183 "-Wextra",
184 "-Wunused",
185 "-Werror",
186 ],
187 header_libs: ["libc_headers"],
188 ldflags: [
189 "-Wl,--exclude-libs=libgcc.a",
190 "-Wl,--exclude-libs=libgcc_stripped.a",
191 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
192 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
193 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
194 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
195 ],
196 arch: {
197 x86: {
198 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
199 },
200 x86_64: {
201 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
202 },
203 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400204 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400205}
206`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000207 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500208 "srcs": `["ld_android.cpp"]`,
209 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400210 "-Wall",
211 "-Wextra",
212 "-Wunused",
213 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500214 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500215 "implementation_deps": `[":libc_headers"]`,
216 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400217 "-Wl,--exclude-libs=libgcc.a",
218 "-Wl,--exclude-libs=libgcc_stripped.a",
219 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
220 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
221 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
222 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
223 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000224 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
225 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400226 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500227 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500228 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200229 })
230}
231
232func TestCcLibraryExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000233 runCcLibraryTestCase(t, Bp2buildTestCase{
234 Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
235 ModuleTypeUnderTest: "cc_library",
236 ModuleTypeUnderTestFactory: cc.LibraryFactory,
237 Dir: "external",
238 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200239 "external/math/cosf.c": "",
240 "external/math/erf.c": "",
241 "external/math/erf_data.c": "",
242 "external/math/erff.c": "",
243 "external/math/erff_data.c": "",
244 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000245cc_library {
246 name: "fake-libarm-optimized-routines-math",
247 exclude_srcs: [
248 // Provided by:
249 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
250 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
251 "math/erf.c",
252 "math/erf_data.c",
253 "math/erff.c",
254 "math/erff_data.c",
255 ],
256 srcs: [
257 "math/*.c",
258 ],
259 // arch-specific settings
260 arch: {
261 arm64: {
262 cflags: [
263 "-DHAVE_FAST_FMA=1",
264 ],
265 },
266 },
267 bazel_module: { bp2build_available: true },
268}
269`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200270 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000271 Blueprint: soongCcLibraryPreamble,
272 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500273 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000274 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
275 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500276 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500277 "local_includes": `["."]`,
278 "srcs_c": `["math/cosf.c"]`,
279 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200280 })
281}
282
283func TestCcLibrarySharedStaticProps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000284 runCcLibraryTestCase(t, Bp2buildTestCase{
285 Description: "cc_library shared/static props",
286 ModuleTypeUnderTest: "cc_library",
287 ModuleTypeUnderTestFactory: cc.LibraryFactory,
288 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400289 "both.cpp": "",
290 "sharedonly.cpp": "",
291 "staticonly.cpp": "",
292 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000293 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000294cc_library {
295 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400296 srcs: ["both.cpp"],
297 cflags: ["bothflag"],
298 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400299 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
300 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400301 static: {
302 srcs: ["staticonly.cpp"],
303 cflags: ["staticflag"],
304 shared_libs: ["shared_dep_for_static"],
305 static_libs: ["static_dep_for_static"],
306 whole_static_libs: ["whole_static_lib_for_static"],
307 },
308 shared: {
309 srcs: ["sharedonly.cpp"],
310 cflags: ["sharedflag"],
311 shared_libs: ["shared_dep_for_shared"],
312 static_libs: ["static_dep_for_shared"],
313 whole_static_libs: ["whole_static_lib_for_shared"],
314 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400315 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000316}
317
Liz Kammer8337ea42021-09-10 10:06:32 -0400318cc_library_static {
319 name: "static_dep_for_shared",
320 bazel_module: { bp2build_available: false },
321}
Chris Parsons08648312021-05-06 16:23:19 -0400322
Liz Kammer8337ea42021-09-10 10:06:32 -0400323cc_library_static {
324 name: "static_dep_for_static",
325 bazel_module: { bp2build_available: false },
326}
Chris Parsons08648312021-05-06 16:23:19 -0400327
Liz Kammer8337ea42021-09-10 10:06:32 -0400328cc_library_static {
329 name: "static_dep_for_both",
330 bazel_module: { bp2build_available: false },
331}
Chris Parsons08648312021-05-06 16:23:19 -0400332
Liz Kammer8337ea42021-09-10 10:06:32 -0400333cc_library_static {
334 name: "whole_static_lib_for_shared",
335 bazel_module: { bp2build_available: false },
336}
Chris Parsons08648312021-05-06 16:23:19 -0400337
Liz Kammer8337ea42021-09-10 10:06:32 -0400338cc_library_static {
339 name: "whole_static_lib_for_static",
340 bazel_module: { bp2build_available: false },
341}
Chris Parsons08648312021-05-06 16:23:19 -0400342
Liz Kammer8337ea42021-09-10 10:06:32 -0400343cc_library_static {
344 name: "whole_static_lib_for_both",
345 bazel_module: { bp2build_available: false },
346}
Chris Parsons08648312021-05-06 16:23:19 -0400347
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400348cc_library_static {
349 name: "whole_and_static_lib_for_both",
350 bazel_module: { bp2build_available: false },
351}
352
Liz Kammer8337ea42021-09-10 10:06:32 -0400353cc_library {
354 name: "shared_dep_for_shared",
355 bazel_module: { bp2build_available: false },
356}
Chris Parsons08648312021-05-06 16:23:19 -0400357
Liz Kammer8337ea42021-09-10 10:06:32 -0400358cc_library {
359 name: "shared_dep_for_static",
360 bazel_module: { bp2build_available: false },
361}
Chris Parsons08648312021-05-06 16:23:19 -0400362
Liz Kammer8337ea42021-09-10 10:06:32 -0400363cc_library {
364 name: "shared_dep_for_both",
365 bazel_module: { bp2build_available: false },
366}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000367`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000368 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000369 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500370 "copts": `[
371 "bothflag",
372 "staticflag",
373 ]`,
374 "implementation_deps": `[
375 ":static_dep_for_both",
376 ":static_dep_for_static",
377 ]`,
378 "implementation_dynamic_deps": `[
379 ":shared_dep_for_both",
380 ":shared_dep_for_static",
381 ]`,
382 "srcs": `[
383 "both.cpp",
384 "staticonly.cpp",
385 ]`,
386 "whole_archive_deps": `[
387 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400388 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500389 ":whole_static_lib_for_static",
390 ]`}),
Alixe06d75b2022-08-31 18:28:19 +0000391 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500392 "copts": `[
393 "bothflag",
394 "sharedflag",
395 ]`,
396 "implementation_deps": `[
397 ":static_dep_for_both",
398 ":static_dep_for_shared",
399 ]`,
400 "implementation_dynamic_deps": `[
401 ":shared_dep_for_both",
402 ":shared_dep_for_shared",
403 ]`,
404 "srcs": `[
405 "both.cpp",
406 "sharedonly.cpp",
407 ]`,
408 "whole_archive_deps": `[
409 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400410 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500411 ":whole_static_lib_for_shared",
412 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500413 }),
414 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200415 })
416}
417
Liz Kammer7a210ac2021-09-22 15:52:58 -0400418func TestCcLibraryDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000419 runCcLibraryTestCase(t, Bp2buildTestCase{
420 Description: "cc_library shared/static props",
421 ModuleTypeUnderTest: "cc_library",
422 ModuleTypeUnderTestFactory: cc.LibraryFactory,
423 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400424 "both.cpp": "",
425 "sharedonly.cpp": "",
426 "staticonly.cpp": "",
427 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000428 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400429cc_library {
430 name: "a",
431 srcs: ["both.cpp"],
432 cflags: ["bothflag"],
433 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
434 export_shared_lib_headers: ["shared_dep_for_both"],
435 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
436 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
437 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
438 static: {
439 srcs: ["staticonly.cpp"],
440 cflags: ["staticflag"],
441 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
442 export_shared_lib_headers: ["shared_dep_for_static"],
443 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
444 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
445 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
446 },
447 shared: {
448 srcs: ["sharedonly.cpp"],
449 cflags: ["sharedflag"],
450 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
451 export_shared_lib_headers: ["shared_dep_for_shared"],
452 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
453 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
454 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
455 },
456 include_build_directory: false,
457}
458` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
459 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
460 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
461 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
462 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
463 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
464 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
465 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
466 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
467 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
468 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
469 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
470 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
471 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
472 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
473 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
474 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
475 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000476 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000477 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500478 "copts": `[
479 "bothflag",
480 "staticflag",
481 ]`,
482 "deps": `[
483 ":static_dep_for_both",
484 ":static_dep_for_static",
485 ]`,
486 "dynamic_deps": `[
487 ":shared_dep_for_both",
488 ":shared_dep_for_static",
489 ]`,
490 "implementation_deps": `[
491 ":implementation_static_dep_for_both",
492 ":implementation_static_dep_for_static",
493 ]`,
494 "implementation_dynamic_deps": `[
495 ":implementation_shared_dep_for_both",
496 ":implementation_shared_dep_for_static",
497 ]`,
498 "srcs": `[
499 "both.cpp",
500 "staticonly.cpp",
501 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500502 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400503 ":not_explicitly_exported_whole_static_dep_for_both",
504 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500505 ":not_explicitly_exported_whole_static_dep_for_static",
506 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500507 ]`,
508 }),
Alixe06d75b2022-08-31 18:28:19 +0000509 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500510 "copts": `[
511 "bothflag",
512 "sharedflag",
513 ]`,
514 "deps": `[
515 ":static_dep_for_both",
516 ":static_dep_for_shared",
517 ]`,
518 "dynamic_deps": `[
519 ":shared_dep_for_both",
520 ":shared_dep_for_shared",
521 ]`,
522 "implementation_deps": `[
523 ":implementation_static_dep_for_both",
524 ":implementation_static_dep_for_shared",
525 ]`,
526 "implementation_dynamic_deps": `[
527 ":implementation_shared_dep_for_both",
528 ":implementation_shared_dep_for_shared",
529 ]`,
530 "srcs": `[
531 "both.cpp",
532 "sharedonly.cpp",
533 ]`,
534 "whole_archive_deps": `[
535 ":not_explicitly_exported_whole_static_dep_for_both",
536 ":whole_static_dep_for_both",
537 ":not_explicitly_exported_whole_static_dep_for_shared",
538 ":whole_static_dep_for_shared",
539 ]`,
540 })},
541 },
542 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400543}
544
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400545func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000546 runCcLibraryTestCase(t, Bp2buildTestCase{
547 ModuleTypeUnderTest: "cc_library",
548 ModuleTypeUnderTestFactory: cc.LibraryFactory,
549 Dir: "foo/bar",
550 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400551 "foo/bar/Android.bp": `
552cc_library {
553 name: "a",
554 whole_static_libs: ["whole_static_lib_for_both"],
555 static: {
556 whole_static_libs: ["whole_static_lib_for_static"],
557 },
558 shared: {
559 whole_static_libs: ["whole_static_lib_for_shared"],
560 },
561 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400562 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400563}
564
565cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
566
567cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
568
569cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
570`,
571 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000572 Blueprint: soongCcLibraryPreamble,
573 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000574 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500575 "whole_archive_deps": `[
576 ":whole_static_lib_for_both_alwayslink",
577 ":whole_static_lib_for_static_alwayslink",
578 ]`,
579 }),
Alixe06d75b2022-08-31 18:28:19 +0000580 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500581 "whole_archive_deps": `[
582 ":whole_static_lib_for_both_alwayslink",
583 ":whole_static_lib_for_shared_alwayslink",
584 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500585 }),
586 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500587 },
588 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400589}
590
Jingwen Chenbcf53042021-05-26 04:42:42 +0000591func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000592 runCcLibraryTestCase(t, Bp2buildTestCase{
593 Description: "cc_library shared/static props in arch",
594 ModuleTypeUnderTest: "cc_library",
595 ModuleTypeUnderTestFactory: cc.LibraryFactory,
596 Dir: "foo/bar",
597 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000598 "foo/bar/arm.cpp": "",
599 "foo/bar/x86.cpp": "",
600 "foo/bar/sharedonly.cpp": "",
601 "foo/bar/staticonly.cpp": "",
602 "foo/bar/Android.bp": `
603cc_library {
604 name: "a",
605 arch: {
606 arm: {
607 shared: {
608 srcs: ["arm_shared.cpp"],
609 cflags: ["-DARM_SHARED"],
610 static_libs: ["arm_static_dep_for_shared"],
611 whole_static_libs: ["arm_whole_static_dep_for_shared"],
612 shared_libs: ["arm_shared_dep_for_shared"],
613 },
614 },
615 x86: {
616 static: {
617 srcs: ["x86_static.cpp"],
618 cflags: ["-DX86_STATIC"],
619 static_libs: ["x86_dep_for_static"],
620 },
621 },
622 },
623 target: {
624 android: {
625 shared: {
626 srcs: ["android_shared.cpp"],
627 cflags: ["-DANDROID_SHARED"],
628 static_libs: ["android_dep_for_shared"],
629 },
630 },
631 android_arm: {
632 shared: {
633 cflags: ["-DANDROID_ARM_SHARED"],
634 },
635 },
636 },
637 srcs: ["both.cpp"],
638 cflags: ["bothflag"],
639 static_libs: ["static_dep_for_both"],
640 static: {
641 srcs: ["staticonly.cpp"],
642 cflags: ["staticflag"],
643 static_libs: ["static_dep_for_static"],
644 },
645 shared: {
646 srcs: ["sharedonly.cpp"],
647 cflags: ["sharedflag"],
648 static_libs: ["static_dep_for_shared"],
649 },
650 bazel_module: { bp2build_available: true },
651}
652
653cc_library_static { name: "static_dep_for_shared" }
654cc_library_static { name: "static_dep_for_static" }
655cc_library_static { name: "static_dep_for_both" }
656
657cc_library_static { name: "arm_static_dep_for_shared" }
658cc_library_static { name: "arm_whole_static_dep_for_shared" }
659cc_library_static { name: "arm_shared_dep_for_shared" }
660
661cc_library_static { name: "x86_dep_for_static" }
662
663cc_library_static { name: "android_dep_for_shared" }
664`,
665 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000666 Blueprint: soongCcLibraryPreamble,
667 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000668 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500669 "copts": `[
670 "bothflag",
671 "staticflag",
672 ] + select({
673 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
674 "//conditions:default": [],
675 })`,
676 "implementation_deps": `[
677 ":static_dep_for_both",
678 ":static_dep_for_static",
679 ] + select({
680 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
681 "//conditions:default": [],
682 })`,
683 "local_includes": `["."]`,
684 "srcs": `[
685 "both.cpp",
686 "staticonly.cpp",
687 ] + select({
688 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
689 "//conditions:default": [],
690 })`,
691 }),
Alixe06d75b2022-08-31 18:28:19 +0000692 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500693 "copts": `[
694 "bothflag",
695 "sharedflag",
696 ] + select({
697 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
698 "//conditions:default": [],
699 }) + select({
700 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
701 "//conditions:default": [],
702 }) + select({
703 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
704 "//conditions:default": [],
705 })`,
706 "implementation_deps": `[
707 ":static_dep_for_both",
708 ":static_dep_for_shared",
709 ] + select({
710 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
711 "//conditions:default": [],
712 }) + select({
713 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
714 "//conditions:default": [],
715 })`,
716 "implementation_dynamic_deps": `select({
717 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
718 "//conditions:default": [],
719 })`,
720 "local_includes": `["."]`,
721 "srcs": `[
722 "both.cpp",
723 "sharedonly.cpp",
724 ] + select({
725 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
726 "//conditions:default": [],
727 }) + select({
728 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
729 "//conditions:default": [],
730 })`,
731 "whole_archive_deps": `select({
732 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
733 "//conditions:default": [],
734 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500735 }),
736 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500737 },
738 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000739}
740
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000741func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000742 runCcLibraryTestCase(t, Bp2buildTestCase{
743 Description: "cc_library shared/static props with c/cpp/s mixed sources",
744 ModuleTypeUnderTest: "cc_library",
745 ModuleTypeUnderTestFactory: cc.LibraryFactory,
746 Dir: "foo/bar",
747 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000748 "foo/bar/both_source.cpp": "",
749 "foo/bar/both_source.cc": "",
750 "foo/bar/both_source.c": "",
751 "foo/bar/both_source.s": "",
752 "foo/bar/both_source.S": "",
753 "foo/bar/shared_source.cpp": "",
754 "foo/bar/shared_source.cc": "",
755 "foo/bar/shared_source.c": "",
756 "foo/bar/shared_source.s": "",
757 "foo/bar/shared_source.S": "",
758 "foo/bar/static_source.cpp": "",
759 "foo/bar/static_source.cc": "",
760 "foo/bar/static_source.c": "",
761 "foo/bar/static_source.s": "",
762 "foo/bar/static_source.S": "",
763 "foo/bar/Android.bp": `
764cc_library {
765 name: "a",
766 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400767 "both_source.cpp",
768 "both_source.cc",
769 "both_source.c",
770 "both_source.s",
771 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400772 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400773 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000774 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400775 srcs: [
776 "static_source.cpp",
777 "static_source.cc",
778 "static_source.c",
779 "static_source.s",
780 "static_source.S",
781 ":static_filegroup",
782 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000783 },
784 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400785 srcs: [
786 "shared_source.cpp",
787 "shared_source.cc",
788 "shared_source.c",
789 "shared_source.s",
790 "shared_source.S",
791 ":shared_filegroup",
792 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000793 },
794 bazel_module: { bp2build_available: true },
795}
796
797filegroup {
798 name: "both_filegroup",
799 srcs: [
800 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400801 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000802}
803
804filegroup {
805 name: "shared_filegroup",
806 srcs: [
807 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400808 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000809}
810
811filegroup {
812 name: "static_filegroup",
813 srcs: [
814 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400815 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000816}
817`,
818 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000819 Blueprint: soongCcLibraryPreamble,
820 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000821 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500822 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500823 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000824 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400825 "both_source.cc",
826 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500827 "static_source.cpp",
828 "static_source.cc",
829 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500830 ]`,
831 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000832 "both_source.s",
833 "both_source.S",
834 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500835 "static_source.s",
836 "static_source.S",
837 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500838 ]`,
839 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000840 "both_source.c",
841 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500842 "static_source.c",
843 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500844 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500845 }),
Alixe06d75b2022-08-31 18:28:19 +0000846 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500847 "local_includes": `["."]`,
848 "srcs": `[
849 "both_source.cpp",
850 "both_source.cc",
851 ":both_filegroup_cpp_srcs",
852 "shared_source.cpp",
853 "shared_source.cc",
854 ":shared_filegroup_cpp_srcs",
855 ]`,
856 "srcs_as": `[
857 "both_source.s",
858 "both_source.S",
859 ":both_filegroup_as_srcs",
860 "shared_source.s",
861 "shared_source.S",
862 ":shared_filegroup_as_srcs",
863 ]`,
864 "srcs_c": `[
865 "both_source.c",
866 ":both_filegroup_c_srcs",
867 "shared_source.c",
868 ":shared_filegroup_c_srcs",
869 ]`,
870 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000871}
872
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000873func TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000874 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000875 Description: "cc_library non-configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000876 ModuleTypeUnderTest: "cc_library",
877 ModuleTypeUnderTestFactory: cc.LibraryFactory,
878 Dir: "foo/bar",
879 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200880 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200881cc_library {
882 name: "a",
883 srcs: ["a.cpp"],
884 version_script: "v.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000885 dynamic_list: "dynamic.list",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200886 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400887 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200888}
889`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200890 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000891 Blueprint: soongCcLibraryPreamble,
892 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000893 "additional_linker_inputs": `[
894 "v.map",
895 "dynamic.list",
896 ]`,
897 "linkopts": `[
898 "-Wl,--version-script,$(location v.map)",
899 "-Wl,--dynamic-list,$(location dynamic.list)",
900 ]`,
901 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500902 }),
903 },
904 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200905}
906
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000907func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000908 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000909 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000910 ModuleTypeUnderTest: "cc_library",
911 ModuleTypeUnderTestFactory: cc.LibraryFactory,
912 Dir: "foo/bar",
913 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200914 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400915cc_library {
916 name: "a",
917 srcs: ["a.cpp"],
918 arch: {
919 arm: {
920 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000921 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400922 },
923 arm64: {
924 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000925 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400926 },
927 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200928
Liz Kammer8337ea42021-09-10 10:06:32 -0400929 bazel_module: { bp2build_available: true },
930 include_build_directory: false,
931}
Liz Kammerd366c902021-06-03 13:43:01 -0400932 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200933 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000934 Blueprint: soongCcLibraryPreamble,
935 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500936 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000937 "//build/bazel/platforms/arch:arm": [
938 "arm.map",
939 "dynamic_arm.list",
940 ],
941 "//build/bazel/platforms/arch:arm64": [
942 "arm64.map",
943 "dynamic_arm64.list",
944 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400945 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500946 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500947 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000948 "//build/bazel/platforms/arch:arm": [
949 "-Wl,--version-script,$(location arm.map)",
950 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
951 ],
952 "//build/bazel/platforms/arch:arm64": [
953 "-Wl,--version-script,$(location arm64.map)",
954 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
955 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400956 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500957 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500958 "srcs": `["a.cpp"]`,
959 }),
960 },
961 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200962}
963
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000964func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
965 runCcLibraryTestCase(t, Bp2buildTestCase{
966 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
967 ModuleTypeUnderTest: "cc_library",
968 ModuleTypeUnderTestFactory: cc.LibraryFactory,
969 Filesystem: map[string]string{
970 "version_script": "",
971 "dynamic.list": "",
972 },
973 Blueprint: `
974cc_library {
975 name: "foo",
976 ldflags: [
977 "--nospace_flag",
978 "-z spaceflag",
979 ],
980 version_script: "version_script",
981 dynamic_list: "dynamic.list",
982 include_build_directory: false,
983}
984`,
985 ExpectedBazelTargets: []string{
986 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
987 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
988 "additional_linker_inputs": `[
989 "version_script",
990 "dynamic.list",
991 ]`,
992 "linkopts": `[
993 "--nospace_flag",
994 "-z",
995 "spaceflag",
996 "-Wl,--version-script,$(location version_script)",
997 "-Wl,--dynamic-list,$(location dynamic.list)",
998 ]`,
999 }),
1000 },
1001 })
1002}
1003
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001004func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001005 runCcLibraryTestCase(t, Bp2buildTestCase{
1006 Description: "cc_library shared_libs",
1007 ModuleTypeUnderTest: "cc_library",
1008 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1009 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001010cc_library {
1011 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -04001012 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001013}
1014
1015cc_library {
1016 name: "a",
1017 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001018 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001019}
1020`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001021 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001022 "implementation_dynamic_deps": `[":mylib"]`,
1023 }),
1024 },
1025 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001026}
1027
Liz Kammer0eae52e2021-10-06 10:32:26 -04001028func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001029 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001030 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001031 "features": `[
1032 "disable_pack_relocations",
1033 "-no_undefined_symbols",
1034 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001035 "native_coverage": `False`,
1036 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001037 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001038 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001039 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001040 "//build/bazel/platforms/arch:x86_64": [
1041 "disable_pack_relocations",
1042 "-no_undefined_symbols",
1043 ],
1044 "//conditions:default": [],
1045 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001046 "native_coverage": `False`,
1047 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001048 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001049 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001050 "features": `select({
1051 "//build/bazel/platforms/os:darwin": [
1052 "disable_pack_relocations",
1053 "-no_undefined_symbols",
1054 ],
1055 "//conditions:default": [],
1056 })`,
1057 "srcs": `["c.cpp"]`,
1058 })...)
1059
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001060 runCcLibraryTestCase(t, Bp2buildTestCase{
1061 Description: "cc_library pack_relocations test",
1062 ModuleTypeUnderTest: "cc_library",
1063 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1064 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001065cc_library {
1066 name: "a",
1067 srcs: ["a.cpp"],
1068 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001069 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001070 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001071 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001072}
1073
1074cc_library {
1075 name: "b",
1076 srcs: ["b.cpp"],
1077 arch: {
1078 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001079 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001080 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001081 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001082 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001083 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001084 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001085}
1086
1087cc_library {
1088 name: "c",
1089 srcs: ["c.cpp"],
1090 target: {
1091 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001092 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001093 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001094 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001095 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001096 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001097}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001098 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001099 })
1100}
1101
1102func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001103 runCcLibraryTestCase(t, Bp2buildTestCase{
1104 Description: "cc_library spaces in copts",
1105 ModuleTypeUnderTest: "cc_library",
1106 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1107 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001108cc_library {
1109 name: "a",
1110 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001111 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001112}
1113`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001114 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001115 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001116 "-include",
1117 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001118 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001119 }),
1120 },
1121 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001122}
1123
1124func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001125 runCcLibraryTestCase(t, Bp2buildTestCase{
1126 Description: "cc_library cppflags usage",
1127 ModuleTypeUnderTest: "cc_library",
1128 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1129 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001130 name: "a",
1131 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001132 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001133 cppflags: [
1134 "-fsigned-char",
1135 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001136 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001137 arch: {
1138 arm64: {
1139 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001140 },
Liz Kammerd366c902021-06-03 13:43:01 -04001141 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001142 target: {
1143 android: {
1144 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001145 },
Liz Kammerd366c902021-06-03 13:43:01 -04001146 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001147 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001148}
1149`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001150 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001151 "copts": `["-Wall"]`,
1152 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001153 "-fsigned-char",
1154 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001155 ] + select({
1156 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1157 "//conditions:default": [],
1158 }) + select({
1159 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1160 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001161 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001162 "srcs": `["a.cpp"]`,
1163 }),
1164 },
1165 )
Jingwen Chen63930982021-03-24 10:04:33 -04001166}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001167
Liz Kammer47535c52021-06-02 16:02:22 -04001168func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001169 runCcLibraryTestCase(t, Bp2buildTestCase{
1170 ModuleTypeUnderTest: "cc_library",
1171 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1172 Filesystem: map[string]string{},
1173 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001174cc_library {
1175 name: "foo_static",
1176 srcs: ["common.c"],
1177 whole_static_libs: [
1178 "arm_whole_static_lib_excludes",
1179 "malloc_not_svelte_whole_static_lib_excludes"
1180 ],
1181 static_libs: [
1182 "arm_static_lib_excludes",
1183 "malloc_not_svelte_static_lib_excludes"
1184 ],
1185 shared_libs: [
1186 "arm_shared_lib_excludes",
1187 ],
1188 arch: {
1189 arm: {
1190 exclude_shared_libs: [
1191 "arm_shared_lib_excludes",
1192 ],
1193 exclude_static_libs: [
1194 "arm_static_lib_excludes",
1195 "arm_whole_static_lib_excludes",
1196 ],
1197 },
1198 },
1199 product_variables: {
1200 malloc_not_svelte: {
1201 shared_libs: ["malloc_not_svelte_shared_lib"],
1202 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1203 exclude_static_libs: [
1204 "malloc_not_svelte_static_lib_excludes",
1205 "malloc_not_svelte_whole_static_lib_excludes",
1206 ],
1207 },
1208 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001209 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001210}
1211
1212cc_library {
1213 name: "arm_whole_static_lib_excludes",
1214 bazel_module: { bp2build_available: false },
1215}
1216
1217cc_library {
1218 name: "malloc_not_svelte_whole_static_lib",
1219 bazel_module: { bp2build_available: false },
1220}
1221
1222cc_library {
1223 name: "malloc_not_svelte_whole_static_lib_excludes",
1224 bazel_module: { bp2build_available: false },
1225}
1226
1227cc_library {
1228 name: "arm_static_lib_excludes",
1229 bazel_module: { bp2build_available: false },
1230}
1231
1232cc_library {
1233 name: "malloc_not_svelte_static_lib_excludes",
1234 bazel_module: { bp2build_available: false },
1235}
1236
1237cc_library {
1238 name: "arm_shared_lib_excludes",
1239 bazel_module: { bp2build_available: false },
1240}
1241
1242cc_library {
1243 name: "malloc_not_svelte_shared_lib",
1244 bazel_module: { bp2build_available: false },
1245}
1246`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001247 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001248 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001249 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001250 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001251 }) + select({
1252 "//build/bazel/product_variables:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001253 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001254 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001255 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001256 "//build/bazel/platforms/arch:arm": [],
1257 "//conditions:default": [":arm_shared_lib_excludes"],
1258 }) + select({
1259 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1260 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001261 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001262 "srcs_c": `["common.c"]`,
1263 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001264 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001265 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001266 }) + select({
Chris Parsons953b3562021-09-20 15:14:39 -04001267 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
1268 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001269 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001270 }),
1271 },
1272 )
Liz Kammer47535c52021-06-02 16:02:22 -04001273}
Liz Kammerd366c902021-06-03 13:43:01 -04001274
Zi Wang0a8a1292022-08-30 06:27:01 +00001275func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1276 runCcLibraryTestCase(t, Bp2buildTestCase{
1277 ModuleTypeUnderTest: "cc_library",
1278 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1279 Filesystem: map[string]string{},
1280 Blueprint: soongCcLibraryStaticPreamble + `
1281cc_library {
1282 name: "foo_static",
1283 srcs: ["common.c"],
1284 product_variables: {
1285 malloc_not_svelte: {
1286 header_libs: ["malloc_not_svelte_header_lib"],
1287 },
1288 },
1289 include_build_directory: false,
1290}
1291
1292cc_library {
1293 name: "malloc_not_svelte_header_lib",
1294 bazel_module: { bp2build_available: false },
1295}
1296`,
1297 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1298 "implementation_deps": `select({
1299 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
1300 "//conditions:default": [],
1301 })`,
1302 "srcs_c": `["common.c"]`,
1303 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1304 }),
1305 },
1306 )
1307}
1308
Liz Kammerd366c902021-06-03 13:43:01 -04001309func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001310 runCcLibraryTestCase(t, Bp2buildTestCase{
1311 Description: "cc_library - nocrt: true emits attribute",
1312 ModuleTypeUnderTest: "cc_library",
1313 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1314 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001315 "impl.cpp": "",
1316 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001317 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001318cc_library {
1319 name: "foo-lib",
1320 srcs: ["impl.cpp"],
1321 nocrt: true,
1322 include_build_directory: false,
1323}
1324`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001325 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001326 "link_crt": `False`,
1327 "srcs": `["impl.cpp"]`,
1328 }),
1329 },
1330 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001331}
1332
1333func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001334 runCcLibraryTestCase(t, Bp2buildTestCase{
1335 Description: "cc_library - nocrt: false - does not emit attribute",
1336 ModuleTypeUnderTest: "cc_library",
1337 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1338 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001339 "impl.cpp": "",
1340 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001341 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001342cc_library {
1343 name: "foo-lib",
1344 srcs: ["impl.cpp"],
1345 nocrt: false,
1346 include_build_directory: false,
1347}
1348`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001349 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001350 "srcs": `["impl.cpp"]`,
1351 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001352 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001353}
1354
1355func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001356 runCcLibraryTestCase(t, Bp2buildTestCase{
1357 Description: "cc_library - nocrt in select",
1358 ModuleTypeUnderTest: "cc_library",
1359 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1360 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001361 "impl.cpp": "",
1362 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001363 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001364cc_library {
1365 name: "foo-lib",
1366 srcs: ["impl.cpp"],
1367 arch: {
1368 arm: {
1369 nocrt: true,
1370 },
1371 x86: {
1372 nocrt: false,
1373 },
1374 },
1375 include_build_directory: false,
1376}
1377`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001378 ExpectedErr: fmt.Errorf("module \"foo-lib\": nocrt is not supported for arch variants"),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001379 })
1380}
1381
1382func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001383 runCcLibraryTestCase(t, Bp2buildTestCase{
1384 ModuleTypeUnderTest: "cc_library",
1385 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1386 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001387 "impl.cpp": "",
1388 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001389 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001390cc_library {
1391 name: "foo-lib",
1392 srcs: ["impl.cpp"],
1393 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001394 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001395}
1396`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001397 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001398 "srcs": `["impl.cpp"]`,
1399 "use_libcrt": `False`,
1400 }),
1401 })
1402}
1403
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001404func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001405 STATIC_ONLY_ATTRS := map[string]bool{}
1406 SHARED_ONLY_ATTRS := map[string]bool{
1407 "link_crt": true,
1408 "additional_linker_inputs": true,
1409 "linkopts": true,
1410 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001411 "inject_bssl_hash": true,
Wei Li81852ca2022-07-27 00:22:06 -07001412 "has_stubs": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001413 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001414 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001415 }
Wei Li81852ca2022-07-27 00:22:06 -07001416
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001417 sharedAttrs := AttrNameToString{}
1418 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001419 for key, val := range attrs {
1420 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1421 sharedAttrs[key] = val
1422 }
1423 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1424 staticAttrs[key] = val
1425 }
1426 }
Alixe06d75b2022-08-31 18:28:19 +00001427 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1428 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001429
1430 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001431}
1432
Jingwen Chen6ada5892021-09-17 11:38:09 +00001433func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001434 runCcLibraryTestCase(t, Bp2buildTestCase{
1435 ModuleTypeUnderTest: "cc_library",
1436 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1437 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001438 "impl.cpp": "",
1439 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001440 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001441cc_library {
1442 name: "foo-lib",
1443 srcs: ["impl.cpp"],
1444 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001445 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001446}
1447`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001448 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001449 "srcs": `["impl.cpp"]`,
1450 "use_libcrt": `True`,
1451 }),
1452 })
Liz Kammerd366c902021-06-03 13:43:01 -04001453}
1454
Jingwen Chen6ada5892021-09-17 11:38:09 +00001455func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001456 runCcLibraryTestCase(t, Bp2buildTestCase{
1457 ModuleTypeUnderTest: "cc_library",
1458 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1459 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001460 "impl.cpp": "",
1461 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001462 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001463cc_library {
1464 name: "foo-lib",
1465 srcs: ["impl.cpp"],
1466 arch: {
1467 arm: {
1468 no_libcrt: true,
1469 },
1470 x86: {
1471 no_libcrt: true,
1472 },
1473 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001474 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001475}
1476`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001477 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001478 "srcs": `["impl.cpp"]`,
1479 "use_libcrt": `select({
Liz Kammerd366c902021-06-03 13:43:01 -04001480 "//build/bazel/platforms/arch:arm": False,
1481 "//build/bazel/platforms/arch:x86": False,
1482 "//conditions:default": None,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001483 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001484 }),
1485 })
Liz Kammerd366c902021-06-03 13:43:01 -04001486}
1487
Chris Parsons58852a02021-12-09 18:10:18 -05001488func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001489 runCcLibraryTestCase(t, Bp2buildTestCase{
1490 ModuleTypeUnderTest: "cc_library",
1491 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1492 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001493 "impl.cpp": "",
1494 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001495 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001496cc_library {
1497 name: "foo-lib",
1498 srcs: ["impl.cpp"],
1499 arch: {
1500 arm: {
1501 no_libcrt: true,
1502 },
1503 x86: {
1504 no_libcrt: true,
1505 },
1506 },
1507 target: {
1508 darwin: {
1509 no_libcrt: true,
1510 }
1511 },
1512 include_build_directory: false,
1513}
1514`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001515 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001516 "srcs": `["impl.cpp"]`,
1517 "use_libcrt": `select({
1518 "//build/bazel/platforms/os_arch:android_arm": False,
1519 "//build/bazel/platforms/os_arch:android_x86": False,
1520 "//build/bazel/platforms/os_arch:darwin_arm64": False,
1521 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1522 "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
1523 "//build/bazel/platforms/os_arch:linux_musl_x86": False,
1524 "//build/bazel/platforms/os_arch:windows_x86": False,
1525 "//conditions:default": None,
1526 })`,
1527 }),
1528 })
1529}
1530
1531func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001532 runCcLibraryTestCase(t, Bp2buildTestCase{
1533 ModuleTypeUnderTest: "cc_library",
1534 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1535 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001536 "impl.cpp": "",
1537 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001538 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001539cc_library {
1540 name: "foo-lib",
1541 srcs: ["impl.cpp"],
1542 arch: {
1543 arm: {
1544 no_libcrt: true,
1545 },
1546 // This is expected to override the value for darwin_x86_64.
1547 x86_64: {
1548 no_libcrt: true,
1549 },
1550 },
1551 target: {
1552 darwin: {
1553 no_libcrt: false,
1554 }
1555 },
1556 include_build_directory: false,
1557}
1558`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001559 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001560 "srcs": `["impl.cpp"]`,
1561 "use_libcrt": `select({
1562 "//build/bazel/platforms/os_arch:android_arm": False,
1563 "//build/bazel/platforms/os_arch:android_x86_64": False,
1564 "//build/bazel/platforms/os_arch:darwin_arm64": True,
1565 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1566 "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
1567 "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
1568 "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
1569 "//build/bazel/platforms/os_arch:windows_x86_64": False,
1570 "//conditions:default": None,
1571 })`,
1572 }),
1573 })
1574}
1575
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001576func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001577 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001578 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001579 "strip": `{
1580 "all": True,
1581 }`,
1582 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001583 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001584 "strip": `{
1585 "keep_symbols": True,
1586 }`,
1587 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001588 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001589 "strip": `{
1590 "keep_symbols_and_debug_frame": True,
1591 }`,
1592 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001593 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001594 "strip": `{
1595 "keep_symbols_list": ["symbol"],
1596 }`,
1597 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001598 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001599 "strip": `{
1600 "none": True,
1601 }`,
1602 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001603 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001604
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001605 runCcLibraryTestCase(t, Bp2buildTestCase{
1606 Description: "cc_library strip args",
1607 ModuleTypeUnderTest: "cc_library",
1608 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1609 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001610cc_library {
1611 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001612 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001613}
1614cc_library {
1615 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001616 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001617 keep_symbols: true,
1618 },
1619 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001620}
1621cc_library {
1622 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001623 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001624 keep_symbols_and_debug_frame: true,
1625 },
1626 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001627}
1628cc_library {
1629 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001630 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001631 none: true,
1632 },
1633 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001634}
1635cc_library {
1636 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001637 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001638 keep_symbols_list: ["symbol"],
1639 },
1640 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001641}
1642cc_library {
1643 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001644 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001645 all: true,
1646 },
1647 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001648}
1649`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001650 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001651 })
1652}
1653
1654func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001655 runCcLibraryTestCase(t, Bp2buildTestCase{
1656 Description: "cc_library strip args",
1657 ModuleTypeUnderTest: "cc_library",
1658 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1659 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001660cc_library {
1661 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001662 target: {
1663 darwin: {
1664 strip: {
1665 keep_symbols_list: ["foo", "bar"]
1666 }
1667 },
1668 },
1669 arch: {
1670 arm: {
1671 strip: {
1672 keep_symbols_and_debug_frame: true,
1673 },
1674 },
1675 arm64: {
1676 strip: {
1677 keep_symbols: true,
1678 },
1679 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001680 },
1681 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001682}
1683`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001684 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001685 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001686 "keep_symbols": select({
1687 "//build/bazel/platforms/arch:arm64": True,
1688 "//conditions:default": None,
1689 }),
1690 "keep_symbols_and_debug_frame": select({
1691 "//build/bazel/platforms/arch:arm": True,
1692 "//conditions:default": None,
1693 }),
1694 "keep_symbols_list": select({
1695 "//build/bazel/platforms/os:darwin": [
1696 "foo",
1697 "bar",
1698 ],
1699 "//conditions:default": [],
1700 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001701 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001702 }),
1703 },
1704 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001705}
Chris Parsons51f8c392021-08-03 21:01:05 -04001706
1707func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001708 runCcLibraryTestCase(t, Bp2buildTestCase{
1709 Description: "cc_library system_shared_libs empty at root",
1710 ModuleTypeUnderTest: "cc_library",
1711 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1712 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001713cc_library {
1714 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001715 system_shared_libs: [],
1716 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001717}
1718`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001719 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001720 "system_dynamic_deps": `[]`,
1721 }),
1722 },
1723 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001724}
1725
1726func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001727 runCcLibraryTestCase(t, Bp2buildTestCase{
1728 Description: "cc_library system_shared_libs empty for static variant",
1729 ModuleTypeUnderTest: "cc_library",
1730 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1731 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001732cc_library {
1733 name: "static_empty",
1734 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001735 system_shared_libs: [],
1736 },
1737 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001738}
1739`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001740 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001741 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001742 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001743 }),
Alixe06d75b2022-08-31 18:28:19 +00001744 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001745 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001746 })
1747}
1748
1749func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001750 runCcLibraryTestCase(t, Bp2buildTestCase{
1751 Description: "cc_library system_shared_libs empty for shared variant",
1752 ModuleTypeUnderTest: "cc_library",
1753 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1754 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001755cc_library {
1756 name: "shared_empty",
1757 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001758 system_shared_libs: [],
1759 },
1760 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001761}
1762`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001763 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001764 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1765 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001766 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001767 }),
1768 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001769 })
1770}
1771
1772func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001773 runCcLibraryTestCase(t, Bp2buildTestCase{
1774 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1775 ModuleTypeUnderTest: "cc_library",
1776 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1777 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001778cc_library {
1779 name: "shared_empty",
1780 target: {
1781 bionic: {
1782 shared: {
1783 system_shared_libs: [],
1784 }
1785 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001786 },
1787 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001788}
1789`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001790 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001791 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1792 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001793 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001794 }),
1795 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001796 })
1797}
1798
1799func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1800 // Note that this behavior is technically incorrect (it's a simplification).
1801 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1802 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1803 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001804 runCcLibraryTestCase(t, Bp2buildTestCase{
1805 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1806 ModuleTypeUnderTest: "cc_library",
1807 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1808 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001809cc_library {
1810 name: "target_linux_bionic_empty",
1811 target: {
1812 linux_bionic: {
1813 system_shared_libs: [],
1814 },
1815 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001816 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001817}
1818`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001819 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001820 "system_dynamic_deps": `[]`,
1821 }),
1822 },
1823 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001824}
1825
1826func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001827 runCcLibraryTestCase(t, Bp2buildTestCase{
1828 Description: "cc_library system_shared_libs empty for bionic variant",
1829 ModuleTypeUnderTest: "cc_library",
1830 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1831 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001832cc_library {
1833 name: "target_bionic_empty",
1834 target: {
1835 bionic: {
1836 system_shared_libs: [],
1837 },
1838 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001839 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001840}
1841`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001842 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001843 "system_dynamic_deps": `[]`,
1844 }),
1845 },
1846 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001847}
1848
1849func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001850 runCcLibraryTestCase(t, Bp2buildTestCase{
1851 Description: "cc_library system_shared_libs set for shared and root",
1852 ModuleTypeUnderTest: "cc_library",
1853 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1854 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001855cc_library {
1856 name: "libc",
1857 bazel_module: { bp2build_available: false },
1858}
1859cc_library {
1860 name: "libm",
1861 bazel_module: { bp2build_available: false },
1862}
Chris Parsons51f8c392021-08-03 21:01:05 -04001863
1864cc_library {
1865 name: "foo",
1866 system_shared_libs: ["libc"],
1867 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001868 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001869 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001870 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001871}
1872`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001873 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001874 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001875 "system_dynamic_deps": `[":libc"]`,
1876 }),
Alixe06d75b2022-08-31 18:28:19 +00001877 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001878 "system_dynamic_deps": `[
1879 ":libc",
1880 ":libm",
1881 ]`,
1882 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001883 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001884 })
1885}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001886
1887func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001888 runCcLibraryTestCase(t, Bp2buildTestCase{
1889 Description: "cc_library - selects for all os targets",
1890 ModuleTypeUnderTest: "cc_library",
1891 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1892 Filesystem: map[string]string{},
1893 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001894cc_library {
1895 name: "foo-lib",
1896 srcs: ["base.cpp"],
1897 target: {
1898 android: {
1899 srcs: ["android.cpp"],
1900 },
1901 linux: {
1902 srcs: ["linux.cpp"],
1903 },
1904 linux_glibc: {
1905 srcs: ["linux_glibc.cpp"],
1906 },
1907 darwin: {
1908 srcs: ["darwin.cpp"],
1909 },
1910 bionic: {
1911 srcs: ["bionic.cpp"],
1912 },
1913 linux_musl: {
1914 srcs: ["linux_musl.cpp"],
1915 },
1916 windows: {
1917 srcs: ["windows.cpp"],
1918 },
1919 },
1920 include_build_directory: false,
1921}
1922`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001923 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001924 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001925 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001926 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001927 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001928 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001929 ],
1930 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
1931 "//build/bazel/platforms/os:linux": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001932 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001933 "linux_glibc.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001934 ],
1935 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001936 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001937 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001938 ],
1939 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001940 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001941 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001942 ],
1943 "//build/bazel/platforms/os:windows": ["windows.cpp"],
1944 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001945 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001946 }),
1947 },
1948 )
Jingwen Chen97b85312021-10-08 10:41:31 +00001949}
1950
Yu Liu75be7b92022-02-01 09:54:09 -08001951func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001952 runCcLibraryTestCase(t, Bp2buildTestCase{
1953 Description: "cc_library - libcrypto hash injection",
1954 ModuleTypeUnderTest: "cc_library",
1955 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1956 Filesystem: map[string]string{},
1957 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08001958cc_library {
1959 name: "libcrypto",
1960 target: {
1961 android: {
1962 inject_bssl_hash: true,
1963 },
1964 },
1965 include_build_directory: false,
1966}
1967`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001968 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08001969 "inject_bssl_hash": `select({
1970 "//build/bazel/platforms/os:android": True,
1971 "//conditions:default": None,
1972 })`,
1973 }),
1974 },
1975 )
1976}
1977
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001978func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00001979 type testCase struct {
1980 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001981 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001982 gnu_extensions string
1983 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001984 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001985 }
1986
1987 testCases := []testCase{
1988 // Existing usages of cpp_std in AOSP are:
1989 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
1990 //
1991 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
1992 // is set in the toolchain.)
1993 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04001994 {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 +00001995 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
1996 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04001997 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
1998 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
1999 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002000 // Explicitly setting a c++ std does not use replace gnu++ std even if
2001 // gnu_extensions is true.
2002 // "c++11",
2003 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002004 {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 +00002005 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2006 // "c++17",
2007 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002008 {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 +00002009 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2010 // "c++2a",
2011 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002012 {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 +00002013 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2014 // "c++98",
2015 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002016 {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 +00002017 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2018 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2019 // "gnu++11",
2020 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002021 {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 +00002022 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2023 // "gnu++17",
2024 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002025 {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 +00002026 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002027
2028 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002029 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2030 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2031 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002032 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2033 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2034 {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 +00002035 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002036 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002037 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2038 t.Run(name, func(t *testing.T) {
2039 name_prefix := fmt.Sprintf("a_%v", i)
2040 cppStdProp := ""
2041 if tc.cpp_std != "" {
2042 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2043 }
2044 cStdProp := ""
2045 if tc.c_std != "" {
2046 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2047 }
2048 gnuExtensionsProp := ""
2049 if tc.gnu_extensions != "" {
2050 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2051 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002052 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002053 if tc.bazel_cpp_std != "" {
2054 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2055 }
2056 if tc.bazel_c_std != "" {
2057 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2058 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002059
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002060 runCcLibraryTestCase(t, Bp2buildTestCase{
2061 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002062 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002063 ModuleTypeUnderTest: "cc_library",
2064 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2065 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002066cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002067 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002068%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002069%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002070%s // gnu_extensions: *bool
2071 include_build_directory: false,
2072}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002073`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002074 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002075 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002076
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002077 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2078 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002079 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002080 ModuleTypeUnderTest: "cc_library_static",
2081 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2082 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002083cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002084 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002085%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002086%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002087%s // gnu_extensions: *bool
2088 include_build_directory: false,
2089}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002090`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002091 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002092 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002093 },
2094 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002095
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002096 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2097 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002098 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002099 ModuleTypeUnderTest: "cc_library_shared",
2100 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2101 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002102cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002103 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002104%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002105%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002106%s // gnu_extensions: *bool
2107 include_build_directory: false,
2108}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002109`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002110 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002111 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002112 },
2113 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002114 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002115 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002116}
Liz Kammer12615db2021-09-28 09:19:17 -04002117
2118func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002119 runCcLibraryTestCase(t, Bp2buildTestCase{
2120 ModuleTypeUnderTest: "cc_library",
2121 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2122 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002123 name: "foo",
2124 srcs: ["foo.proto"],
2125 include_build_directory: false,
2126}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002127 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002128 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002129 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002130 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002131 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002132 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002133 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002134 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002135 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002136 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2137 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002138 }),
2139 },
2140 })
2141}
2142
2143func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002144 runCcLibraryTestCase(t, Bp2buildTestCase{
2145 ModuleTypeUnderTest: "cc_library",
2146 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2147 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002148 name: "foo",
2149 srcs: ["foo.proto"],
2150 proto: { canonical_path_from_root: false},
2151 include_build_directory: false,
2152}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002153 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002154 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002155 "srcs": `["foo.proto"]`,
2156 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002157 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002158 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002159 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002160 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002161 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002162 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002163 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2164 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002165 }),
2166 },
2167 })
2168}
2169
2170func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002171 runCcLibraryTestCase(t, Bp2buildTestCase{
2172 ModuleTypeUnderTest: "cc_library",
2173 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2174 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002175 name: "foo",
2176 srcs: ["foo.proto"],
2177 proto: { canonical_path_from_root: true},
2178 include_build_directory: false,
2179}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002180 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002181 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002182 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002183 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002184 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002185 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002186 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002187 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002188 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002189 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2190 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002191 }),
2192 },
2193 })
2194}
2195
2196func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002197 runCcLibraryTestCase(t, Bp2buildTestCase{
2198 ModuleTypeUnderTest: "cc_library",
2199 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2200 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002201 name: "foo",
2202 srcs: ["foo.proto"],
2203 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002204 type: "full",
2205 },
2206 include_build_directory: false,
2207}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002208 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002209 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002210 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002211 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002212 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002213 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002214 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002215 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002216 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002217 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2218 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002219 }),
2220 },
2221 })
2222}
2223
2224func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002225 runCcLibraryTestCase(t, Bp2buildTestCase{
2226 ModuleTypeUnderTest: "cc_library",
2227 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2228 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002229 name: "foo",
2230 srcs: ["foo.proto"],
2231 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002232 type: "lite",
2233 },
2234 include_build_directory: false,
2235}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002236 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002237 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002238 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002239 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002240 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002241 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002242 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002243 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002244 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002245 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2246 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002247 }),
2248 },
2249 })
2250}
2251
2252func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002253 runCcLibraryTestCase(t, Bp2buildTestCase{
2254 ModuleTypeUnderTest: "cc_library",
2255 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2256 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002257 name: "foo",
2258 srcs: ["foo.proto"],
2259 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002260 export_proto_headers: true,
2261 },
2262 include_build_directory: false,
2263}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002264 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002265 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002266 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002267 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002268 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002269 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002270 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002271 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002272 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002273 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2274 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002275 }),
2276 },
2277 })
2278}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002279
Yu Liu2d136142022-08-18 14:46:13 -07002280func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2281 runCcLibraryTestCase(t, Bp2buildTestCase{
2282 ModuleTypeUnderTest: "cc_library",
2283 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2284 Blueprint: soongCcProtoPreamble + `cc_library {
2285 name: "foo",
2286 srcs: ["foo.proto"],
2287 proto: {
2288 include_dirs: ["external/protobuf/src"],
2289 },
2290 include_build_directory: false,
2291}`,
2292 ExpectedBazelTargets: []string{
2293 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2294 "srcs": `["foo.proto"]`,
2295 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2296 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2297 "deps": `[":foo_proto"]`,
2298 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2299 "deps": `[":libprotobuf-cpp-lite"]`,
2300 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2301 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002302 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2303 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002304 }),
2305 },
2306 })
2307}
2308
2309func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2310 runCcLibraryTestCase(t, Bp2buildTestCase{
2311 ModuleTypeUnderTest: "cc_library",
2312 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2313 Blueprint: soongCcProtoPreamble + `cc_library {
2314 name: "foo",
2315 srcs: ["foo.proto"],
2316 proto: {
2317 include_dirs: ["external/protobuf/abc"],
2318 },
2319 include_build_directory: false,
2320}`,
2321 ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"),
2322 })
2323}
2324
Yu Liu2aa806b2022-09-01 11:54:47 -07002325func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2326 runCcLibraryTestCase(t, Bp2buildTestCase{
2327 ModuleTypeUnderTest: "cc_library",
2328 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2329 Blueprint: soongCcProtoPreamble + `
2330filegroup {
2331 name: "a_fg_proto",
2332 srcs: ["a_fg.proto"],
2333}
2334
2335cc_library {
2336 name: "a",
2337 srcs: [
2338 ":a_fg_proto",
2339 "a.proto",
2340 ],
2341 proto: {
2342 export_proto_headers: true,
2343 },
2344 include_build_directory: false,
2345}`,
2346 ExpectedBazelTargets: []string{
2347 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002348 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002349 "srcs": `["a.proto"]`,
2350 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2351 "deps": `[
2352 ":a_fg_proto_bp2build_converted",
2353 ":a_proto",
2354 ]`,
2355 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2356 "deps": `[":libprotobuf-cpp-lite"]`,
2357 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2358 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2359 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2360 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2361 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2362 "srcs": `["a_fg.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -07002363 "tags": `["manual"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002364 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2365 "srcs": `["a_fg.proto"]`,
2366 }),
2367 },
2368 })
2369}
2370
2371func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2372 runCcLibraryTestCase(t, Bp2buildTestCase{
2373 ModuleTypeUnderTest: "cc_library",
2374 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2375 Blueprint: soongCcProtoPreamble + `
2376filegroup {
2377 name: "a_fg_proto",
2378 srcs: ["a_fg.proto"],
2379}
2380
2381cc_library {
2382 name: "a",
2383 srcs: [
2384 ":a_fg_proto",
2385 ],
2386 proto: {
2387 export_proto_headers: true,
2388 },
2389 include_build_directory: false,
2390}`,
2391 ExpectedBazelTargets: []string{
2392 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2393 "deps": `[":a_fg_proto_bp2build_converted"]`,
2394 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2395 "deps": `[":libprotobuf-cpp-lite"]`,
2396 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2397 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2398 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2399 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2400 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2401 "srcs": `["a_fg.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -07002402 "tags": `["manual"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002403 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2404 "srcs": `["a_fg.proto"]`,
2405 }),
2406 },
2407 })
2408}
2409
2410func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2411 runCcLibraryTestCase(t, Bp2buildTestCase{
2412 ModuleTypeUnderTest: "cc_library",
2413 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2414 Filesystem: map[string]string{
2415 "path/to/A/Android.bp": `
2416filegroup {
2417 name: "a_fg_proto",
2418 srcs: ["a_fg.proto"],
2419}`,
2420 },
2421 Blueprint: soongCcProtoPreamble + `
2422cc_library {
2423 name: "a",
2424 srcs: [
2425 ":a_fg_proto",
2426 "a.proto",
2427 ],
2428 proto: {
2429 export_proto_headers: true,
2430 },
2431 include_build_directory: false,
2432}`,
2433 ExpectedBazelTargets: []string{
2434 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002435 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002436 "srcs": `["a.proto"]`,
2437 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2438 "deps": `[
2439 "//path/to/A:a_fg_proto_bp2build_converted",
2440 ":a_proto",
2441 ]`,
2442 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2443 "deps": `[":libprotobuf-cpp-lite"]`,
2444 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2445 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2446 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2447 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2448 }),
2449 },
2450 })
2451}
2452
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002453func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002454 runCcLibraryTestCase(t, Bp2buildTestCase{
2455 ModuleTypeUnderTest: "cc_library",
2456 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2457 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002458 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2459 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2460 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2461 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2462cc_library {
2463 name: "a",
2464 srcs: [":a_fg_proto"],
2465 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002466 export_proto_headers: true,
2467 },
2468 include_build_directory: false,
2469}
2470
2471cc_library {
2472 name: "b",
2473 srcs: [":b_protos"],
2474 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002475 export_proto_headers: true,
2476 },
2477 include_build_directory: false,
2478}
2479
2480cc_library {
2481 name: "c",
2482 srcs: [":c-proto-srcs"],
2483 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002484 export_proto_headers: true,
2485 },
2486 include_build_directory: false,
2487}
2488
2489cc_library {
2490 name: "d",
2491 srcs: [":proto-srcs-d"],
2492 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002493 export_proto_headers: true,
2494 },
2495 include_build_directory: false,
2496}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002497 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002498 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002499 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002500 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002501 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002502 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002503 "deps": `[":libprotobuf-cpp-lite"]`,
2504 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2505 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2506 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2507 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002508 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002509 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2510 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2511 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2512 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2513 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002514 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002515 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002516 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002517 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002518 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002519 "deps": `[":libprotobuf-cpp-lite"]`,
2520 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2521 "srcs": `[":b_protos_cpp_srcs"]`,
2522 "srcs_as": `[":b_protos_as_srcs"]`,
2523 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002524 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002525 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2526 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2527 "srcs": `[":b_protos_cpp_srcs"]`,
2528 "srcs_as": `[":b_protos_as_srcs"]`,
2529 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002530 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002531 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002532 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002533 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002534 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002535 "deps": `[":libprotobuf-cpp-lite"]`,
2536 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2537 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2538 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2539 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002540 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002541 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2542 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2543 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2544 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2545 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002546 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002547 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002548 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002549 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002550 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002551 "deps": `[":libprotobuf-cpp-lite"]`,
2552 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2553 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2554 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2555 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002556 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002557 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2558 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2559 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2560 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2561 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2562 }),
2563 },
2564 })
2565}
Chris Parsons58852a02021-12-09 18:10:18 -05002566
2567func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002568 runCcLibraryTestCase(t, Bp2buildTestCase{
2569 ModuleTypeUnderTest: "cc_library",
2570 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2571 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002572 name: "foo",
2573 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002574 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002575 target: {
2576 darwin: {
2577 enabled: false,
2578 },
2579 windows: {
2580 enabled: false,
2581 },
2582 linux_glibc_x86: {
2583 enabled: false,
2584 },
2585 },
2586 include_build_directory: false,
2587}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002588 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002589 "srcs": `["foo.cpp"]`,
2590 "target_compatible_with": `select({
2591 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2592 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2593 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2594 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2595 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2596 "//conditions:default": [],
2597 })`,
2598 }),
2599 })
2600}
2601
2602func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002603 runCcLibraryTestCase(t, Bp2buildTestCase{
2604 ModuleTypeUnderTest: "cc_library",
2605 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2606 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002607 name: "foo",
2608 srcs: ["foo.cpp"],
2609 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002610 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002611 target: {
2612 darwin: {
2613 enabled: true,
2614 },
2615 windows: {
2616 enabled: false,
2617 },
2618 linux_glibc_x86: {
2619 enabled: false,
2620 },
2621 },
2622 include_build_directory: false,
2623}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002624 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002625 "srcs": `["foo.cpp"]`,
2626 "target_compatible_with": `select({
2627 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2628 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2629 "//conditions:default": ["@platforms//:incompatible"],
2630 })`,
2631 }),
2632 })
2633}
2634
2635func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002636 runCcLibraryTestCase(t, Bp2buildTestCase{
2637 ModuleTypeUnderTest: "cc_library",
2638 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2639 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002640 name: "foo",
2641 srcs: ["foo.cpp"],
2642 enabled: false,
2643 shared: {
2644 enabled: true,
2645 },
2646 target: {
2647 android: {
2648 shared: {
2649 enabled: false,
2650 },
2651 }
2652 },
2653 include_build_directory: false,
2654}`,
Alixe06d75b2022-08-31 18:28:19 +00002655 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002656 "srcs": `["foo.cpp"]`,
2657 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002658 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002659 "srcs": `["foo.cpp"]`,
2660 "target_compatible_with": `select({
2661 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2662 "//conditions:default": [],
2663 })`,
2664 }),
2665 },
2666 })
2667}
2668
2669func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002670 runCcLibraryTestCase(t, Bp2buildTestCase{
2671 ModuleTypeUnderTest: "cc_library",
2672 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2673 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002674 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002675 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002676 srcs: ["foo.cpp"],
2677 shared: {
2678 enabled: false
2679 },
2680 target: {
2681 darwin: {
2682 enabled: true,
2683 },
2684 windows: {
2685 enabled: false,
2686 },
2687 linux_glibc_x86: {
2688 shared: {
2689 enabled: true,
2690 },
2691 },
2692 },
2693 include_build_directory: false,
2694}`,
Alixe06d75b2022-08-31 18:28:19 +00002695 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002696 "srcs": `["foo.cpp"]`,
2697 "target_compatible_with": `select({
2698 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2699 "//conditions:default": [],
2700 })`,
Alixe06d75b2022-08-31 18:28:19 +00002701 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002702 "srcs": `["foo.cpp"]`,
2703 "target_compatible_with": `select({
2704 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2705 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2706 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2707 "//conditions:default": ["@platforms//:incompatible"],
2708 })`,
2709 }),
2710 }})
2711}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002712
2713func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002714 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002715 "has_stubs": `True`,
2716 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002717 })
2718 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
2719 "soname": `"a.so"`,
2720 "source_library": `":a"`,
2721 "stubs_symbol_file": `"a.map.txt"`,
2722 "stubs_versions": `[
2723 "28",
2724 "29",
2725 "current",
2726 ]`,
2727 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002728 runCcLibraryTestCase(t, Bp2buildTestCase{
2729 Description: "cc_library stubs",
2730 ModuleTypeUnderTest: "cc_library",
2731 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2732 Dir: "foo/bar",
2733 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002734 "foo/bar/Android.bp": `
2735cc_library {
2736 name: "a",
2737 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2738 bazel_module: { bp2build_available: true },
2739 include_build_directory: false,
2740}
2741`,
2742 },
Wei Li81852ca2022-07-27 00:22:06 -07002743 Blueprint: soongCcLibraryPreamble,
2744 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002745 },
2746 )
2747}
Liz Kammerf38a8372022-02-04 15:39:00 -05002748
Spandan Das4238c652022-09-09 01:38:47 +00002749func TestCcApiContributionsWithHdrs(t *testing.T) {
2750 bp := `
2751 cc_library {
2752 name: "libfoo",
2753 stubs: { symbol_file: "libfoo.map.txt", versions: ["28", "29", "current"] },
2754 llndk: { symbol_file: "libfoo.map.txt", override_export_include_dirs: ["dir2"]},
2755 export_include_dirs: ["dir1"],
2756 }
2757 `
2758 expectedBazelTargets := []string{
2759 MakeBazelTarget(
2760 "cc_api_library_headers",
2761 "libfoo.systemapi.headers",
2762 AttrNameToString{
2763 "export_includes": `["dir1"]`,
2764 }),
2765 MakeBazelTarget(
2766 "cc_api_library_headers",
2767 "libfoo.vendorapi.headers",
2768 AttrNameToString{
2769 "export_includes": `["dir2"]`,
2770 }),
2771 MakeBazelTarget(
2772 "cc_api_contribution",
2773 "libfoo.contribution",
2774 AttrNameToString{
2775 "api": `"libfoo.map.txt"`,
2776 "library_name": `"libfoo"`,
2777 "api_surfaces": `[
2778 "systemapi",
2779 "vendorapi",
2780 ]`,
2781 "hdrs": `[
2782 ":libfoo.systemapi.headers",
2783 ":libfoo.vendorapi.headers",
2784 ]`,
2785 }),
2786 }
2787 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2788 Blueprint: bp,
2789 Description: "cc API contributions to systemapi and vendorapi",
2790 ExpectedBazelTargets: expectedBazelTargets,
2791 })
2792}
2793
2794func TestCcApiSurfaceCombinations(t *testing.T) {
2795 testCases := []struct {
2796 bp string
2797 expectedApi string
2798 expectedApiSurfaces string
2799 description string
2800 }{
2801 {
2802 bp: `
2803 cc_library {
2804 name: "a",
2805 stubs: {symbol_file: "a.map.txt"},
2806 }`,
2807 expectedApi: `"a.map.txt"`,
2808 expectedApiSurfaces: `["systemapi"]`,
2809 description: "Library that contributes to systemapi",
2810 },
2811 {
2812 bp: `
2813 cc_library {
2814 name: "a",
2815 llndk: {symbol_file: "a.map.txt"},
2816 }`,
2817 expectedApi: `"a.map.txt"`,
2818 expectedApiSurfaces: `["vendorapi"]`,
2819 description: "Library that contributes to vendorapi",
2820 },
2821 {
2822 bp: `
2823 cc_library {
2824 name: "a",
2825 llndk: {symbol_file: "a.map.txt"},
2826 stubs: {symbol_file: "a.map.txt"},
2827 }`,
2828 expectedApi: `"a.map.txt"`,
2829 expectedApiSurfaces: `[
2830 "systemapi",
2831 "vendorapi",
2832 ]`,
2833 description: "Library that contributes to systemapi and vendorapi",
2834 },
2835 }
2836 for _, testCase := range testCases {
2837 expectedBazelTargets := []string{
2838 MakeBazelTarget(
2839 "cc_api_contribution",
2840 "a.contribution",
2841 AttrNameToString{
2842 "library_name": `"a"`,
2843 "hdrs": `[]`,
2844 "api": testCase.expectedApi,
2845 "api_surfaces": testCase.expectedApiSurfaces,
2846 },
2847 ),
2848 }
2849 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2850 Blueprint: testCase.bp,
2851 Description: testCase.description,
2852 ExpectedBazelTargets: expectedBazelTargets,
2853 })
2854 }
2855}
2856
2857// llndk struct property in Soong provides users with several options to configure the exported include dirs
2858// Test the generated bazel targets for the different configurations
2859func TestCcVendorApiHeaders(t *testing.T) {
2860 testCases := []struct {
2861 bp string
2862 expectedIncludes string
2863 expectedSystemIncludes string
2864 description string
2865 }{
2866 {
2867 bp: `
2868 cc_library {
2869 name: "a",
2870 export_include_dirs: ["include"],
2871 export_system_include_dirs: ["base_system_include"],
2872 llndk: {
2873 symbol_file: "a.map.txt",
2874 export_headers_as_system: true,
2875 },
2876 }
2877 `,
2878 expectedIncludes: "",
2879 expectedSystemIncludes: `[
2880 "base_system_include",
2881 "include",
2882 ]`,
2883 description: "Headers are exported as system to API surface",
2884 },
2885 {
2886 bp: `
2887 cc_library {
2888 name: "a",
2889 export_include_dirs: ["include"],
2890 export_system_include_dirs: ["base_system_include"],
2891 llndk: {
2892 symbol_file: "a.map.txt",
2893 override_export_include_dirs: ["llndk_include"],
2894 },
2895 }
2896 `,
2897 expectedIncludes: `["llndk_include"]`,
2898 expectedSystemIncludes: `["base_system_include"]`,
2899 description: "Non-system Headers are ovverriden before export to API surface",
2900 },
2901 {
2902 bp: `
2903 cc_library {
2904 name: "a",
2905 export_include_dirs: ["include"],
2906 export_system_include_dirs: ["base_system_include"],
2907 llndk: {
2908 symbol_file: "a.map.txt",
2909 override_export_include_dirs: ["llndk_include"],
2910 export_headers_as_system: true,
2911 },
2912 }
2913 `,
2914 expectedIncludes: "", // includes are set to nil
2915 expectedSystemIncludes: `[
2916 "base_system_include",
2917 "llndk_include",
2918 ]`,
2919 description: "System Headers are extended before export to API surface",
2920 },
2921 }
2922 for _, testCase := range testCases {
2923 attrs := AttrNameToString{}
2924 if testCase.expectedIncludes != "" {
2925 attrs["export_includes"] = testCase.expectedIncludes
2926 }
2927 if testCase.expectedSystemIncludes != "" {
2928 attrs["export_system_includes"] = testCase.expectedSystemIncludes
2929 }
2930
2931 expectedBazelTargets := []string{
2932 MakeBazelTarget("cc_api_library_headers", "a.vendorapi.headers", attrs),
2933 // Create a target for cc_api_contribution target
2934 MakeBazelTarget("cc_api_contribution", "a.contribution", AttrNameToString{
2935 "api": `"a.map.txt"`,
2936 "api_surfaces": `["vendorapi"]`,
2937 "hdrs": `[":a.vendorapi.headers"]`,
2938 "library_name": `"a"`,
2939 }),
2940 }
2941 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2942 Blueprint: testCase.bp,
2943 ExpectedBazelTargets: expectedBazelTargets,
2944 })
2945 }
2946}
2947
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002948func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2949 runCcLibraryTestCase(t, Bp2buildTestCase{
2950 Description: "stub target generation of the same lib across configs should not result in duplicates",
2951 ModuleTypeUnderTest: "cc_library",
2952 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2953 Filesystem: map[string]string{
2954 "bar.map.txt": "",
2955 },
2956 Blueprint: `
2957cc_library {
2958 name: "barlib",
2959 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2960 bazel_module: { bp2build_available: false },
2961}
2962cc_library {
2963 name: "foolib",
2964 shared_libs: ["barlib"],
2965 target: {
2966 android: {
2967 shared_libs: ["barlib"],
2968 },
2969 },
2970 bazel_module: { bp2build_available: true },
2971}`,
2972 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2973 "implementation_dynamic_deps": `select({
2974 "//build/bazel/rules/apex:android-in_apex": [":barlib_stub_libs_current"],
2975 "//conditions:default": [":barlib"],
2976 })`,
2977 "local_includes": `["."]`,
2978 }),
2979 })
2980}
2981
Liz Kammerffc17e42022-11-23 09:42:05 -05002982func TestCcLibraryExcludesLibsHost(t *testing.T) {
2983 runCcLibraryTestCase(t, Bp2buildTestCase{
2984 ModuleTypeUnderTest: "cc_library",
2985 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2986 Filesystem: map[string]string{
2987 "bar.map.txt": "",
2988 },
2989 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
2990cc_library {
2991 name: "quxlib",
2992 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
2993 bazel_module: { bp2build_available: false },
2994}
2995cc_library {
2996 name: "barlib",
2997 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2998 bazel_module: { bp2build_available: false },
2999}
3000cc_library {
3001 name: "foolib",
3002 shared_libs: ["barlib", "quxlib"],
3003 target: {
3004 host: {
3005 shared_libs: ["bazlib"],
3006 exclude_shared_libs: ["barlib"],
3007 },
3008 },
3009 include_build_directory: false,
3010 bazel_module: { bp2build_available: true },
3011}`,
3012 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3013 "implementation_dynamic_deps": `select({
3014 "//build/bazel/platforms/os:darwin": [":bazlib"],
3015 "//build/bazel/platforms/os:linux": [":bazlib"],
3016 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
3017 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
3018 "//build/bazel/platforms/os:windows": [":bazlib"],
3019 "//conditions:default": [],
3020 }) + select({
3021 "//build/bazel/platforms/os:darwin": [":quxlib"],
3022 "//build/bazel/platforms/os:linux": [":quxlib"],
3023 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
3024 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
3025 "//build/bazel/platforms/os:windows": [":quxlib"],
3026 "//build/bazel/rules/apex:android-in_apex": [
3027 ":barlib_stub_libs_current",
3028 ":quxlib_stub_libs_current",
3029 ],
3030 "//conditions:default": [
3031 ":barlib",
3032 ":quxlib",
3033 ],
3034 })`,
3035 }),
3036 })
3037}
3038
Liz Kammerf38a8372022-02-04 15:39:00 -05003039func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003040 runCcLibraryTestCase(t, Bp2buildTestCase{
3041 ModuleTypeUnderTest: "cc_library",
3042 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3043 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05003044 name: "foo",
3045 ldflags: ["-Wl,--rpath,${ORIGIN}"],
3046 include_build_directory: false,
3047}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003048 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05003049 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
3050 }),
3051 })
3052}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003053
3054func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003055 runCcLibraryTestCase(t, Bp2buildTestCase{
3056 ModuleTypeUnderTest: "cc_library",
3057 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3058 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003059 "foo.c": "",
3060 "bar.cc": "",
3061 "foo1.l": "",
3062 "bar1.ll": "",
3063 "foo2.l": "",
3064 "bar2.ll": "",
3065 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003066 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003067 name: "foo_lib",
3068 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
3069 lex: { flags: ["--foo_flags"] },
3070 include_build_directory: false,
3071 bazel_module: { bp2build_available: true },
3072}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003073 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00003074 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003075 "srcs": `[
3076 "foo1.l",
3077 "foo2.l",
3078 ]`,
3079 "lexopts": `["--foo_flags"]`,
3080 }),
Alixe06d75b2022-08-31 18:28:19 +00003081 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003082 "srcs": `[
3083 "bar1.ll",
3084 "bar2.ll",
3085 ]`,
3086 "lexopts": `["--foo_flags"]`,
3087 }),
3088 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003089 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003090 "srcs": `[
3091 "bar.cc",
3092 ":foo_lib_genlex_ll",
3093 ]`,
3094 "srcs_c": `[
3095 "foo.c",
3096 ":foo_lib_genlex_l",
3097 ]`,
3098 })...),
3099 })
3100}
Cole Faust6b29f592022-08-09 09:50:56 -07003101
3102func TestCCLibraryRuntimeDeps(t *testing.T) {
3103 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3104 Blueprint: `cc_library_shared {
3105 name: "bar",
3106}
3107
3108cc_library {
3109 name: "foo",
3110 runtime_libs: ["foo"],
3111}`,
3112 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003113 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003114 "local_includes": `["."]`,
3115 }),
Alixe06d75b2022-08-31 18:28:19 +00003116 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003117 "runtime_deps": `[":foo"]`,
3118 "local_includes": `["."]`,
3119 }),
Alixe06d75b2022-08-31 18:28:19 +00003120 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003121 "runtime_deps": `[":foo"]`,
3122 "local_includes": `["."]`,
3123 }),
3124 },
3125 })
3126}
Cole Faust5fa4e962022-08-22 14:31:04 -07003127
3128func TestCcLibraryWithInstructionSet(t *testing.T) {
3129 runCcLibraryTestCase(t, Bp2buildTestCase{
3130 ModuleTypeUnderTest: "cc_library",
3131 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3132 Blueprint: `cc_library {
3133 name: "foo",
3134 arch: {
3135 arm: {
3136 instruction_set: "arm",
3137 }
3138 }
3139}
3140`,
3141 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3142 "features": `select({
3143 "//build/bazel/platforms/arch:arm": [
3144 "arm_isa_arm",
3145 "-arm_isa_thumb",
3146 ],
3147 "//conditions:default": [],
3148 })`,
3149 "local_includes": `["."]`,
3150 }),
3151 })
3152}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003153
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003154func TestCcLibraryEmptySuffix(t *testing.T) {
3155 runCcLibraryTestCase(t, Bp2buildTestCase{
3156 Description: "cc_library with empty suffix",
3157 ModuleTypeUnderTest: "cc_library",
3158 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3159 Filesystem: map[string]string{
3160 "foo.c": "",
3161 },
3162 Blueprint: `cc_library {
3163 name: "foo",
3164 suffix: "",
3165 srcs: ["foo.c"],
3166 include_build_directory: false,
3167}`,
3168 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003169 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 -05003170 "srcs_c": `["foo.c"]`,
3171 }),
Alixe06d75b2022-08-31 18:28:19 +00003172 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003173 "srcs_c": `["foo.c"]`,
3174 "suffix": `""`,
3175 }),
3176 },
3177 })
3178}
3179
3180func TestCcLibrarySuffix(t *testing.T) {
3181 runCcLibraryTestCase(t, Bp2buildTestCase{
3182 Description: "cc_library with suffix",
3183 ModuleTypeUnderTest: "cc_library",
3184 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3185 Filesystem: map[string]string{
3186 "foo.c": "",
3187 },
3188 Blueprint: `cc_library {
3189 name: "foo",
3190 suffix: "-suf",
3191 srcs: ["foo.c"],
3192 include_build_directory: false,
3193}`,
3194 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003195 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 -05003196 "srcs_c": `["foo.c"]`,
3197 }),
Alixe06d75b2022-08-31 18:28:19 +00003198 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003199 "srcs_c": `["foo.c"]`,
3200 "suffix": `"-suf"`,
3201 }),
3202 },
3203 })
3204}
3205
3206func TestCcLibraryArchVariantSuffix(t *testing.T) {
3207 runCcLibraryTestCase(t, Bp2buildTestCase{
3208 Description: "cc_library with arch-variant suffix",
3209 ModuleTypeUnderTest: "cc_library",
3210 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3211 Filesystem: map[string]string{
3212 "foo.c": "",
3213 },
3214 Blueprint: `cc_library {
3215 name: "foo",
3216 arch: {
3217 arm64: { suffix: "-64" },
3218 arm: { suffix: "-32" },
3219 },
3220 srcs: ["foo.c"],
3221 include_build_directory: false,
3222}`,
3223 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003224 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 -05003225 "srcs_c": `["foo.c"]`,
3226 }),
Alixe06d75b2022-08-31 18:28:19 +00003227 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003228 "srcs_c": `["foo.c"]`,
3229 "suffix": `select({
3230 "//build/bazel/platforms/arch:arm": "-32",
3231 "//build/bazel/platforms/arch:arm64": "-64",
3232 "//conditions:default": None,
3233 })`,
3234 }),
3235 },
3236 })
3237}
3238
Vinh Tran9f6796a2022-08-16 13:10:31 -04003239func TestCcLibraryWithAidlSrcs(t *testing.T) {
3240 runCcLibraryTestCase(t, Bp2buildTestCase{
3241 Description: "cc_library with aidl srcs",
3242 ModuleTypeUnderTest: "cc_library",
3243 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3244 Blueprint: `
3245filegroup {
3246 name: "A_aidl",
3247 srcs: ["aidl/A.aidl"],
3248 path: "aidl",
3249}
3250cc_library {
3251 name: "foo",
3252 srcs: [
3253 ":A_aidl",
3254 "B.aidl",
3255 ],
3256}`,
3257 ExpectedBazelTargets: []string{
3258 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3259 "srcs": `["aidl/A.aidl"]`,
3260 "strip_import_prefix": `"aidl"`,
3261 }),
Alixe06d75b2022-08-31 18:28:19 +00003262 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003263 "srcs": `["B.aidl"]`,
3264 }),
Alixe06d75b2022-08-31 18:28:19 +00003265 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003266 "deps": `[
3267 ":A_aidl",
3268 ":foo_aidl_library",
3269 ]`,
3270 }),
Alixe06d75b2022-08-31 18:28:19 +00003271 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003272 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3273 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003274 }),
Alixe06d75b2022-08-31 18:28:19 +00003275 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003276 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3277 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003278 }),
3279 },
3280 })
3281}
3282
3283func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3284 runCcLibraryTestCase(t, Bp2buildTestCase{
3285 Description: "cc_library with non aidl filegroup",
3286 ModuleTypeUnderTest: "cc_library",
3287 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3288 Filesystem: map[string]string{
3289 "path/to/A/Android.bp": `
3290filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003291 name: "A_aidl",
3292 srcs: ["aidl/A.aidl"],
3293 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003294}`,
3295 },
3296 Blueprint: `
3297cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003298 name: "foo",
3299 srcs: [
3300 ":A_aidl",
3301 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003302}`,
3303 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003304 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003305 "deps": `["//path/to/A:A_aidl"]`,
3306 }),
Alixe06d75b2022-08-31 18:28:19 +00003307 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003308 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3309 "local_includes": `["."]`,
3310 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003311 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003312 "local_includes": `["."]`,
3313 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003314 }),
3315 },
3316 })
3317}
3318
3319func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
3320 runCcLibraryTestCase(t, Bp2buildTestCase{
3321 Description: "cc_library with export aidl headers",
3322 ModuleTypeUnderTest: "cc_library",
3323 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3324 Blueprint: `
3325cc_library {
3326 name: "foo",
3327 srcs: [
3328 "Foo.aidl",
3329 ],
3330 aidl: {
3331 export_aidl_headers: true,
3332 }
3333}`,
3334 ExpectedBazelTargets: []string{
3335 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3336 "srcs": `["Foo.aidl"]`,
3337 }),
3338 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3339 "deps": `[":foo_aidl_library"]`,
3340 }),
3341 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003342 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3343 "local_includes": `["."]`,
3344 }),
Alixe06d75b2022-08-31 18:28:19 +00003345 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003346 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3347 "local_includes": `["."]`,
3348 }),
3349 },
3350 })
3351}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003352
3353func TestCcLibraryWithTargetApex(t *testing.T) {
3354 runCcLibraryTestCase(t, Bp2buildTestCase{
3355 Description: "cc_library with target.apex",
3356 ModuleTypeUnderTest: "cc_library",
3357 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3358 Blueprint: `
3359cc_library {
3360 name: "foo",
3361 shared_libs: ["bar", "baz"],
3362 static_libs: ["baz", "buh"],
3363 target: {
3364 apex: {
3365 exclude_shared_libs: ["bar"],
3366 exclude_static_libs: ["buh"],
3367 }
3368 }
3369}`,
3370 ExpectedBazelTargets: []string{
3371 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3372 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3373 "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
3374 "//conditions:default": [],
3375 })`,
3376 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3377 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3378 "//conditions:default": [],
3379 })`,
3380 "local_includes": `["."]`,
3381 }),
3382 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3383 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3384 "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
3385 "//conditions:default": [],
3386 })`,
3387 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3388 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3389 "//conditions:default": [],
3390 })`,
3391 "local_includes": `["."]`,
3392 }),
3393 },
3394 })
3395}
3396
3397func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3398 runCcLibraryTestCase(t, Bp2buildTestCase{
3399 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3400 ModuleTypeUnderTest: "cc_library",
3401 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3402 Blueprint: `
3403cc_library_static {
3404 name: "foo",
3405 shared_libs: ["bar", "baz"],
3406 static_libs: ["abc"],
3407 export_shared_lib_headers: ["baz"],
3408 export_static_lib_headers: ["abc"],
3409 target: {
3410 apex: {
3411 exclude_shared_libs: ["baz", "bar"],
3412 exclude_static_libs: ["abc"],
3413 }
3414 }
3415}`,
3416 ExpectedBazelTargets: []string{
3417 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3418 "implementation_dynamic_deps": `select({
3419 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3420 "//conditions:default": [],
3421 })`,
3422 "dynamic_deps": `select({
3423 "//build/bazel/rules/apex:non_apex": [":baz__BP2BUILD__MISSING__DEP"],
3424 "//conditions:default": [],
3425 })`,
3426 "deps": `select({
3427 "//build/bazel/rules/apex:non_apex": [":abc__BP2BUILD__MISSING__DEP"],
3428 "//conditions:default": [],
3429 })`,
3430 "local_includes": `["."]`,
3431 }),
3432 },
3433 })
3434}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003435
3436func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3437 runCcLibraryTestCase(t, Bp2buildTestCase{
3438 Description: "cc_library with sysprop sources",
3439 ModuleTypeUnderTest: "cc_library",
3440 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3441 Blueprint: `
3442cc_library {
3443 name: "foo",
3444 srcs: [
3445 "bar.sysprop",
3446 "baz.sysprop",
3447 "blah.cpp",
3448 ],
3449 min_sdk_version: "5",
3450}`,
3451 ExpectedBazelTargets: []string{
3452 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3453 "srcs": `[
3454 "bar.sysprop",
3455 "baz.sysprop",
3456 ]`,
3457 }),
3458 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3459 "dep": `":foo_sysprop_library"`,
3460 "min_sdk_version": `"5"`,
3461 }),
3462 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3463 "srcs": `["blah.cpp"]`,
3464 "local_includes": `["."]`,
3465 "min_sdk_version": `"5"`,
3466 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3467 }),
3468 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3469 "srcs": `["blah.cpp"]`,
3470 "local_includes": `["."]`,
3471 "min_sdk_version": `"5"`,
3472 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3473 }),
3474 },
3475 })
3476}
3477
3478func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3479 runCcLibraryTestCase(t, Bp2buildTestCase{
3480 Description: "cc_library with sysprop sources in some configs but not others",
3481 ModuleTypeUnderTest: "cc_library",
3482 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3483 Blueprint: `
3484cc_library {
3485 name: "foo",
3486 host_supported: true,
3487 srcs: [
3488 "blah.cpp",
3489 ],
3490 target: {
3491 android: {
3492 srcs: ["bar.sysprop"],
3493 },
3494 },
3495 min_sdk_version: "5",
3496}`,
3497 ExpectedBazelTargets: []string{
3498 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3499 "srcs": `select({
3500 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3501 "//conditions:default": [],
3502 })`,
3503 }),
3504 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3505 "dep": `":foo_sysprop_library"`,
3506 "min_sdk_version": `"5"`,
3507 }),
3508 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3509 "srcs": `["blah.cpp"]`,
3510 "local_includes": `["."]`,
3511 "min_sdk_version": `"5"`,
3512 "whole_archive_deps": `select({
3513 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3514 "//conditions:default": [],
3515 })`,
3516 }),
3517 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3518 "srcs": `["blah.cpp"]`,
3519 "local_includes": `["."]`,
3520 "min_sdk_version": `"5"`,
3521 "whole_archive_deps": `select({
3522 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3523 "//conditions:default": [],
3524 })`,
3525 }),
3526 },
3527 })
3528}
Vinh Tran395a1e92022-09-16 18:27:29 -04003529
3530func TestCcLibraryWithAidlAndSharedLibs(t *testing.T) {
3531 runCcLibraryTestCase(t, Bp2buildTestCase{
3532 Description: "cc_aidl_library depends on shared libs from parent cc_library_static",
3533 ModuleTypeUnderTest: "cc_library",
3534 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3535 Blueprint: `
3536cc_library_static {
3537 name: "foo",
3538 srcs: [
3539 "Foo.aidl",
3540 ],
3541 shared_libs: [
3542 "bar",
3543 "baz",
3544 ],
3545 export_shared_lib_headers: [
3546 "baz",
3547 ],
3548}` +
3549 simpleModuleDoNotConvertBp2build("cc_library", "bar") +
3550 simpleModuleDoNotConvertBp2build("cc_library", "baz"),
3551 ExpectedBazelTargets: []string{
3552 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3553 "srcs": `["Foo.aidl"]`,
3554 }),
3555 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3556 "deps": `[":foo_aidl_library"]`,
3557 "implementation_dynamic_deps": `[
3558 ":baz",
3559 ":bar",
3560 ]`,
3561 }),
3562 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3563 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3564 "dynamic_deps": `[":baz"]`,
3565 "implementation_dynamic_deps": `[":bar"]`,
3566 "local_includes": `["."]`,
3567 }),
3568 },
3569 })
3570}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003571
3572func TestCcLibraryWithTidy(t *testing.T) {
3573 runCcLibraryTestCase(t, Bp2buildTestCase{
3574 Description: "cc_library uses tidy properties",
3575 ModuleTypeUnderTest: "cc_library",
3576 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3577 Blueprint: `
3578cc_library_static {
3579 name: "foo",
3580 srcs: ["foo.cpp"],
3581 tidy: true,
3582 tidy_checks: ["check1", "check2"],
3583 tidy_checks_as_errors: ["check1error", "check2error"],
3584 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003585 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003586}`,
3587 ExpectedBazelTargets: []string{
3588 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3589 "local_includes": `["."]`,
3590 "srcs": `["foo.cpp"]`,
3591 "tidy": `True`,
3592 "tidy_checks": `[
3593 "check1",
3594 "check2",
3595 ]`,
3596 "tidy_checks_as_errors": `[
3597 "check1error",
3598 "check2error",
3599 ]`,
3600 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003601 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003602 }),
3603 },
3604 })
3605}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003606
Vinh Tran99270ea2022-11-28 11:15:23 -05003607func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3608 bp := `
3609cc_library {
3610 name: "foo",
3611 afdo: true,
3612 include_build_directory: false,
3613}`
3614
3615 // TODO(b/260714900): Add test case for arch-specific afdo profile
3616 testCases := []struct {
3617 description string
3618 filesystem map[string]string
3619 expectedBazelTargets []string
3620 }{
3621 {
3622 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003623 filesystem: map[string]string{
3624 "vendor/google_data/pgo_profile/sampling/BUILD": "",
3625 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3626 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003627 expectedBazelTargets: []string{
3628 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3629 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3630 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3631 }),
3632 },
3633 },
3634 {
3635 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003636 filesystem: map[string]string{
3637 "toolchain/pgo-profiles/sampling/BUILD": "",
3638 "toolchain/pgo-profiles/sampling/foo.afdo": "",
3639 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003640 expectedBazelTargets: []string{
3641 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3642 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3643 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3644 }),
3645 },
3646 },
3647 {
3648 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003649 filesystem: map[string]string{
3650 "toolchain/pgo-profiles/sampling/BUILD": "",
3651 "toolchain/pgo-profiles/sampling/bar.afdo": "",
3652 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003653 expectedBazelTargets: []string{
3654 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3655 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3656 },
3657 },
3658 {
3659 description: "cc_library with afdo enabled but profile doesn't exist",
3660 expectedBazelTargets: []string{
3661 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3662 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3663 },
3664 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003665 {
3666 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3667 filesystem: map[string]string{
3668 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3669 },
3670 expectedBazelTargets: []string{
3671 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3672 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3673 },
3674 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003675 }
3676 for _, testCase := range testCases {
3677 t.Run(testCase.description, func(t *testing.T) {
3678 runCcLibraryTestCase(t, Bp2buildTestCase{
3679 ExpectedBazelTargets: testCase.expectedBazelTargets,
3680 ModuleTypeUnderTest: "cc_library",
3681 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3682 Description: testCase.description,
3683 Blueprint: binaryReplacer.Replace(bp),
3684 Filesystem: testCase.filesystem,
3685 })
3686 })
3687 }
3688}
3689
Yu Liu56ccb1a2022-11-12 10:47:07 -08003690func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3691 runCcLibraryTestCase(t, Bp2buildTestCase{
3692 Description: "cc_library with header abi checker",
3693 ModuleTypeUnderTest: "cc_library",
3694 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3695 Blueprint: `cc_library {
3696 name: "foo",
3697 header_abi_checker: {
3698 enabled: true,
3699 symbol_file: "a.map.txt",
3700 exclude_symbol_versions: [
3701 "29",
3702 "30",
3703 ],
3704 exclude_symbol_tags: [
3705 "tag1",
3706 "tag2",
3707 ],
3708 check_all_apis: true,
3709 diff_flags: ["-allow-adding-removing-weak-symbols"],
3710 },
3711 include_build_directory: false,
3712}`,
3713 ExpectedBazelTargets: []string{
3714 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3715 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3716 "abi_checker_enabled": `True`,
3717 "abi_checker_symbol_file": `"a.map.txt"`,
3718 "abi_checker_exclude_symbol_versions": `[
3719 "29",
3720 "30",
3721 ]`,
3722 "abi_checker_exclude_symbol_tags": `[
3723 "tag1",
3724 "tag2",
3725 ]`,
3726 "abi_checker_check_all_apis": `True`,
3727 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3728 }),
3729 },
3730 })
3731}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003732
3733func TestCcLibraryApexAvailable(t *testing.T) {
3734 runCcLibraryTestCase(t, Bp2buildTestCase{
3735 Description: "cc_library apex_available converted to tags",
3736 ModuleTypeUnderTest: "cc_library",
3737 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3738 Blueprint: soongCcLibraryPreamble + `
3739cc_library {
3740 name: "a",
3741 srcs: ["a.cpp"],
3742 apex_available: ["com.android.foo"],
3743}
3744`,
3745 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3746 "tags": `["apex_available=com.android.foo"]`,
3747 "srcs": `["a.cpp"]`,
3748 "local_includes": `["."]`,
3749 }),
3750 },
3751 )
3752}
3753
3754func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3755 runCcLibraryTestCase(t, Bp2buildTestCase{
3756 Description: "cc_library apex_available converted to multiple tags",
3757 ModuleTypeUnderTest: "cc_library",
3758 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3759 Blueprint: soongCcLibraryPreamble + `
3760cc_library {
3761 name: "a",
3762 srcs: ["a.cpp"],
3763 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3764}
3765`,
3766 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3767 "tags": `[
3768 "apex_available=com.android.foo",
3769 "apex_available=//apex_available:platform",
3770 "apex_available=com.android.bar",
3771 ]`,
3772 "srcs": `["a.cpp"]`,
3773 "local_includes": `["."]`,
3774 }),
3775 },
3776 )
3777}