blob: 595ee637abe611c3ffb590c471b07bf807d09536 [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"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400152 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800153 "//build/bazel/platforms/os:linux_glibc": ["linux.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,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001412 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001413 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001414 }
Wei Li81852ca2022-07-27 00:22:06 -07001415
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001416 sharedAttrs := AttrNameToString{}
1417 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001418 for key, val := range attrs {
1419 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1420 sharedAttrs[key] = val
1421 }
1422 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1423 staticAttrs[key] = val
1424 }
1425 }
Alixe06d75b2022-08-31 18:28:19 +00001426 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1427 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001428
1429 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001430}
1431
Jingwen Chen6ada5892021-09-17 11:38:09 +00001432func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001433 runCcLibraryTestCase(t, Bp2buildTestCase{
1434 ModuleTypeUnderTest: "cc_library",
1435 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1436 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001437 "impl.cpp": "",
1438 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001439 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001440cc_library {
1441 name: "foo-lib",
1442 srcs: ["impl.cpp"],
1443 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001444 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001445}
1446`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001447 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001448 "srcs": `["impl.cpp"]`,
1449 "use_libcrt": `True`,
1450 }),
1451 })
Liz Kammerd366c902021-06-03 13:43:01 -04001452}
1453
Jingwen Chen6ada5892021-09-17 11:38:09 +00001454func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001455 runCcLibraryTestCase(t, Bp2buildTestCase{
1456 ModuleTypeUnderTest: "cc_library",
1457 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1458 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001459 "impl.cpp": "",
1460 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001461 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001462cc_library {
1463 name: "foo-lib",
1464 srcs: ["impl.cpp"],
1465 arch: {
1466 arm: {
1467 no_libcrt: true,
1468 },
1469 x86: {
1470 no_libcrt: true,
1471 },
1472 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001473 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001474}
1475`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001476 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001477 "srcs": `["impl.cpp"]`,
1478 "use_libcrt": `select({
Liz Kammerd366c902021-06-03 13:43:01 -04001479 "//build/bazel/platforms/arch:arm": False,
1480 "//build/bazel/platforms/arch:x86": False,
1481 "//conditions:default": None,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001482 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001483 }),
1484 })
Liz Kammerd366c902021-06-03 13:43:01 -04001485}
1486
Chris Parsons58852a02021-12-09 18:10:18 -05001487func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001488 runCcLibraryTestCase(t, Bp2buildTestCase{
1489 ModuleTypeUnderTest: "cc_library",
1490 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1491 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001492 "impl.cpp": "",
1493 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001494 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001495cc_library {
1496 name: "foo-lib",
1497 srcs: ["impl.cpp"],
1498 arch: {
1499 arm: {
1500 no_libcrt: true,
1501 },
1502 x86: {
1503 no_libcrt: true,
1504 },
1505 },
1506 target: {
1507 darwin: {
1508 no_libcrt: true,
1509 }
1510 },
1511 include_build_directory: false,
1512}
1513`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001514 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001515 "srcs": `["impl.cpp"]`,
1516 "use_libcrt": `select({
1517 "//build/bazel/platforms/os_arch:android_arm": False,
1518 "//build/bazel/platforms/os_arch:android_x86": False,
1519 "//build/bazel/platforms/os_arch:darwin_arm64": False,
1520 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1521 "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
1522 "//build/bazel/platforms/os_arch:linux_musl_x86": False,
1523 "//build/bazel/platforms/os_arch:windows_x86": False,
1524 "//conditions:default": None,
1525 })`,
1526 }),
1527 })
1528}
1529
1530func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001531 runCcLibraryTestCase(t, Bp2buildTestCase{
1532 ModuleTypeUnderTest: "cc_library",
1533 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1534 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001535 "impl.cpp": "",
1536 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001537 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001538cc_library {
1539 name: "foo-lib",
1540 srcs: ["impl.cpp"],
1541 arch: {
1542 arm: {
1543 no_libcrt: true,
1544 },
1545 // This is expected to override the value for darwin_x86_64.
1546 x86_64: {
1547 no_libcrt: true,
1548 },
1549 },
1550 target: {
1551 darwin: {
1552 no_libcrt: false,
1553 }
1554 },
1555 include_build_directory: false,
1556}
1557`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001558 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001559 "srcs": `["impl.cpp"]`,
1560 "use_libcrt": `select({
1561 "//build/bazel/platforms/os_arch:android_arm": False,
1562 "//build/bazel/platforms/os_arch:android_x86_64": False,
1563 "//build/bazel/platforms/os_arch:darwin_arm64": True,
1564 "//build/bazel/platforms/os_arch:darwin_x86_64": False,
1565 "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
1566 "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
1567 "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
1568 "//build/bazel/platforms/os_arch:windows_x86_64": False,
1569 "//conditions:default": None,
1570 })`,
1571 }),
1572 })
1573}
1574
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001575func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001576 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001577 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001578 "strip": `{
1579 "all": True,
1580 }`,
1581 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001582 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001583 "strip": `{
1584 "keep_symbols": True,
1585 }`,
1586 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001587 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001588 "strip": `{
1589 "keep_symbols_and_debug_frame": True,
1590 }`,
1591 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001592 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001593 "strip": `{
1594 "keep_symbols_list": ["symbol"],
1595 }`,
1596 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001597 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001598 "strip": `{
1599 "none": True,
1600 }`,
1601 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001602 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001603
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001604 runCcLibraryTestCase(t, Bp2buildTestCase{
1605 Description: "cc_library strip args",
1606 ModuleTypeUnderTest: "cc_library",
1607 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1608 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001609cc_library {
1610 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001611 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001612}
1613cc_library {
1614 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001615 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001616 keep_symbols: true,
1617 },
1618 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001619}
1620cc_library {
1621 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001622 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001623 keep_symbols_and_debug_frame: true,
1624 },
1625 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001626}
1627cc_library {
1628 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001629 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001630 none: true,
1631 },
1632 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001633}
1634cc_library {
1635 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001636 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001637 keep_symbols_list: ["symbol"],
1638 },
1639 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001640}
1641cc_library {
1642 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001643 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001644 all: true,
1645 },
1646 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001647}
1648`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001649 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001650 })
1651}
1652
1653func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001654 runCcLibraryTestCase(t, Bp2buildTestCase{
1655 Description: "cc_library strip args",
1656 ModuleTypeUnderTest: "cc_library",
1657 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1658 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001659cc_library {
1660 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001661 target: {
1662 darwin: {
1663 strip: {
1664 keep_symbols_list: ["foo", "bar"]
1665 }
1666 },
1667 },
1668 arch: {
1669 arm: {
1670 strip: {
1671 keep_symbols_and_debug_frame: true,
1672 },
1673 },
1674 arm64: {
1675 strip: {
1676 keep_symbols: true,
1677 },
1678 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001679 },
1680 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001681}
1682`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001683 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001684 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001685 "keep_symbols": select({
1686 "//build/bazel/platforms/arch:arm64": True,
1687 "//conditions:default": None,
1688 }),
1689 "keep_symbols_and_debug_frame": select({
1690 "//build/bazel/platforms/arch:arm": True,
1691 "//conditions:default": None,
1692 }),
1693 "keep_symbols_list": select({
1694 "//build/bazel/platforms/os:darwin": [
1695 "foo",
1696 "bar",
1697 ],
1698 "//conditions:default": [],
1699 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001700 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001701 }),
1702 },
1703 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001704}
Chris Parsons51f8c392021-08-03 21:01:05 -04001705
1706func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001707 runCcLibraryTestCase(t, Bp2buildTestCase{
1708 Description: "cc_library system_shared_libs empty at root",
1709 ModuleTypeUnderTest: "cc_library",
1710 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1711 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001712cc_library {
1713 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001714 system_shared_libs: [],
1715 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001716}
1717`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001718 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001719 "system_dynamic_deps": `[]`,
1720 }),
1721 },
1722 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001723}
1724
1725func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001726 runCcLibraryTestCase(t, Bp2buildTestCase{
1727 Description: "cc_library system_shared_libs empty for static variant",
1728 ModuleTypeUnderTest: "cc_library",
1729 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1730 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001731cc_library {
1732 name: "static_empty",
1733 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001734 system_shared_libs: [],
1735 },
1736 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001737}
1738`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001739 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001740 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001741 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001742 }),
Alixe06d75b2022-08-31 18:28:19 +00001743 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001744 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001745 })
1746}
1747
1748func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001749 runCcLibraryTestCase(t, Bp2buildTestCase{
1750 Description: "cc_library system_shared_libs empty for shared variant",
1751 ModuleTypeUnderTest: "cc_library",
1752 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1753 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001754cc_library {
1755 name: "shared_empty",
1756 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001757 system_shared_libs: [],
1758 },
1759 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001760}
1761`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001762 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001763 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1764 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001765 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001766 }),
1767 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001768 })
1769}
1770
1771func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001772 runCcLibraryTestCase(t, Bp2buildTestCase{
1773 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1774 ModuleTypeUnderTest: "cc_library",
1775 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1776 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001777cc_library {
1778 name: "shared_empty",
1779 target: {
1780 bionic: {
1781 shared: {
1782 system_shared_libs: [],
1783 }
1784 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001785 },
1786 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001787}
1788`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001789 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001790 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1791 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001792 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001793 }),
1794 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001795 })
1796}
1797
1798func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1799 // Note that this behavior is technically incorrect (it's a simplification).
1800 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1801 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1802 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001803 runCcLibraryTestCase(t, Bp2buildTestCase{
1804 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1805 ModuleTypeUnderTest: "cc_library",
1806 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1807 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001808cc_library {
1809 name: "target_linux_bionic_empty",
1810 target: {
1811 linux_bionic: {
1812 system_shared_libs: [],
1813 },
1814 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001815 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001816}
1817`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001818 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001819 "system_dynamic_deps": `[]`,
1820 }),
1821 },
1822 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001823}
1824
1825func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001826 runCcLibraryTestCase(t, Bp2buildTestCase{
1827 Description: "cc_library system_shared_libs empty for bionic variant",
1828 ModuleTypeUnderTest: "cc_library",
1829 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1830 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001831cc_library {
1832 name: "target_bionic_empty",
1833 target: {
1834 bionic: {
1835 system_shared_libs: [],
1836 },
1837 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001838 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001839}
1840`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001841 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001842 "system_dynamic_deps": `[]`,
1843 }),
1844 },
1845 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001846}
1847
1848func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001849 runCcLibraryTestCase(t, Bp2buildTestCase{
1850 Description: "cc_library system_shared_libs set for shared and root",
1851 ModuleTypeUnderTest: "cc_library",
1852 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1853 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001854cc_library {
1855 name: "libc",
1856 bazel_module: { bp2build_available: false },
1857}
1858cc_library {
1859 name: "libm",
1860 bazel_module: { bp2build_available: false },
1861}
Chris Parsons51f8c392021-08-03 21:01:05 -04001862
1863cc_library {
1864 name: "foo",
1865 system_shared_libs: ["libc"],
1866 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001867 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001868 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001869 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001870}
1871`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001872 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001873 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001874 "system_dynamic_deps": `[":libc"]`,
1875 }),
Alixe06d75b2022-08-31 18:28:19 +00001876 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001877 "system_dynamic_deps": `[
1878 ":libc",
1879 ":libm",
1880 ]`,
1881 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001882 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001883 })
1884}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001885
1886func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001887 runCcLibraryTestCase(t, Bp2buildTestCase{
1888 Description: "cc_library - selects for all os targets",
1889 ModuleTypeUnderTest: "cc_library",
1890 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1891 Filesystem: map[string]string{},
1892 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001893cc_library {
1894 name: "foo-lib",
1895 srcs: ["base.cpp"],
1896 target: {
1897 android: {
1898 srcs: ["android.cpp"],
1899 },
1900 linux: {
1901 srcs: ["linux.cpp"],
1902 },
1903 linux_glibc: {
1904 srcs: ["linux_glibc.cpp"],
1905 },
1906 darwin: {
1907 srcs: ["darwin.cpp"],
1908 },
1909 bionic: {
1910 srcs: ["bionic.cpp"],
1911 },
1912 linux_musl: {
1913 srcs: ["linux_musl.cpp"],
1914 },
1915 windows: {
1916 srcs: ["windows.cpp"],
1917 },
1918 },
1919 include_build_directory: false,
1920}
1921`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001922 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001923 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001924 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001925 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001926 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001927 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001928 ],
1929 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001930 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001931 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001932 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001933 ],
Colin Cross133782e2022-12-20 15:29:31 -08001934 "//build/bazel/platforms/os:linux_glibc": [
1935 "linux.cpp",
1936 "linux_glibc.cpp",
1937 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001938 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001939 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001940 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001941 ],
1942 "//build/bazel/platforms/os:windows": ["windows.cpp"],
1943 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001944 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001945 }),
1946 },
1947 )
Jingwen Chen97b85312021-10-08 10:41:31 +00001948}
1949
Yu Liu75be7b92022-02-01 09:54:09 -08001950func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001951 runCcLibraryTestCase(t, Bp2buildTestCase{
1952 Description: "cc_library - libcrypto hash injection",
1953 ModuleTypeUnderTest: "cc_library",
1954 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1955 Filesystem: map[string]string{},
1956 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08001957cc_library {
1958 name: "libcrypto",
1959 target: {
1960 android: {
1961 inject_bssl_hash: true,
1962 },
1963 },
1964 include_build_directory: false,
1965}
1966`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001967 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08001968 "inject_bssl_hash": `select({
1969 "//build/bazel/platforms/os:android": True,
1970 "//conditions:default": None,
1971 })`,
1972 }),
1973 },
1974 )
1975}
1976
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001977func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00001978 type testCase struct {
1979 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001980 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001981 gnu_extensions string
1982 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001983 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001984 }
1985
1986 testCases := []testCase{
1987 // Existing usages of cpp_std in AOSP are:
1988 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
1989 //
1990 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
1991 // is set in the toolchain.)
1992 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04001993 {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 +00001994 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
1995 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04001996 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
1997 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
1998 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001999 // Explicitly setting a c++ std does not use replace gnu++ std even if
2000 // gnu_extensions is true.
2001 // "c++11",
2002 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002003 {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 +00002004 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2005 // "c++17",
2006 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002007 {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 +00002008 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2009 // "c++2a",
2010 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002011 {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 +00002012 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2013 // "c++98",
2014 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002015 {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 +00002016 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2017 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2018 // "gnu++11",
2019 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002020 {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 +00002021 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2022 // "gnu++17",
2023 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002024 {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 +00002025 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002026
2027 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002028 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2029 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2030 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002031 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2032 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2033 {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 +00002034 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002035 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002036 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2037 t.Run(name, func(t *testing.T) {
2038 name_prefix := fmt.Sprintf("a_%v", i)
2039 cppStdProp := ""
2040 if tc.cpp_std != "" {
2041 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2042 }
2043 cStdProp := ""
2044 if tc.c_std != "" {
2045 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2046 }
2047 gnuExtensionsProp := ""
2048 if tc.gnu_extensions != "" {
2049 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2050 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002051 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002052 if tc.bazel_cpp_std != "" {
2053 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2054 }
2055 if tc.bazel_c_std != "" {
2056 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2057 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002058
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002059 runCcLibraryTestCase(t, Bp2buildTestCase{
2060 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002061 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002062 ModuleTypeUnderTest: "cc_library",
2063 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2064 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002065cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002066 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002067%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002068%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002069%s // gnu_extensions: *bool
2070 include_build_directory: false,
2071}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002072`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002073 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002074 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002075
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002076 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2077 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002078 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002079 ModuleTypeUnderTest: "cc_library_static",
2080 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2081 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002082cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002083 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002084%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002085%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002086%s // gnu_extensions: *bool
2087 include_build_directory: false,
2088}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002089`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002090 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002091 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002092 },
2093 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002094
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002095 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2096 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002097 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002098 ModuleTypeUnderTest: "cc_library_shared",
2099 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2100 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002101cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002102 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002103%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002104%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002105%s // gnu_extensions: *bool
2106 include_build_directory: false,
2107}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002108`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002109 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002110 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002111 },
2112 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002113 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002114 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002115}
Liz Kammer12615db2021-09-28 09:19:17 -04002116
2117func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002118 runCcLibraryTestCase(t, Bp2buildTestCase{
2119 ModuleTypeUnderTest: "cc_library",
2120 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2121 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002122 name: "foo",
2123 srcs: ["foo.proto"],
2124 include_build_directory: false,
2125}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002126 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002127 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002128 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002129 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002130 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002131 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002132 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002133 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002134 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002135 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2136 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002137 }),
2138 },
2139 })
2140}
2141
2142func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002143 runCcLibraryTestCase(t, Bp2buildTestCase{
2144 ModuleTypeUnderTest: "cc_library",
2145 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2146 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002147 name: "foo",
2148 srcs: ["foo.proto"],
2149 proto: { canonical_path_from_root: false},
2150 include_build_directory: false,
2151}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002152 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002153 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002154 "srcs": `["foo.proto"]`,
2155 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002156 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002157 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002158 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002159 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002160 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002161 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002162 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2163 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002164 }),
2165 },
2166 })
2167}
2168
2169func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002170 runCcLibraryTestCase(t, Bp2buildTestCase{
2171 ModuleTypeUnderTest: "cc_library",
2172 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2173 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002174 name: "foo",
2175 srcs: ["foo.proto"],
2176 proto: { canonical_path_from_root: true},
2177 include_build_directory: false,
2178}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002179 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002180 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002181 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002182 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002183 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002184 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002185 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002186 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002187 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002188 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2189 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002190 }),
2191 },
2192 })
2193}
2194
2195func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002196 runCcLibraryTestCase(t, Bp2buildTestCase{
2197 ModuleTypeUnderTest: "cc_library",
2198 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2199 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002200 name: "foo",
2201 srcs: ["foo.proto"],
2202 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002203 type: "full",
2204 },
2205 include_build_directory: false,
2206}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002207 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002208 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002209 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002210 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002211 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002212 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002213 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002214 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002215 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002216 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2217 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002218 }),
2219 },
2220 })
2221}
2222
2223func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002224 runCcLibraryTestCase(t, Bp2buildTestCase{
2225 ModuleTypeUnderTest: "cc_library",
2226 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2227 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002228 name: "foo",
2229 srcs: ["foo.proto"],
2230 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002231 type: "lite",
2232 },
2233 include_build_directory: false,
2234}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002235 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002236 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002237 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002238 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002239 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002240 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002241 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002242 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002243 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002244 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2245 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002246 }),
2247 },
2248 })
2249}
2250
2251func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002252 runCcLibraryTestCase(t, Bp2buildTestCase{
2253 ModuleTypeUnderTest: "cc_library",
2254 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2255 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002256 name: "foo",
2257 srcs: ["foo.proto"],
2258 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002259 export_proto_headers: true,
2260 },
2261 include_build_directory: false,
2262}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002263 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002264 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002265 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002266 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002267 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002268 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002269 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002270 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002271 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002272 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2273 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002274 }),
2275 },
2276 })
2277}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002278
Yu Liu2d136142022-08-18 14:46:13 -07002279func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2280 runCcLibraryTestCase(t, Bp2buildTestCase{
2281 ModuleTypeUnderTest: "cc_library",
2282 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2283 Blueprint: soongCcProtoPreamble + `cc_library {
2284 name: "foo",
2285 srcs: ["foo.proto"],
2286 proto: {
2287 include_dirs: ["external/protobuf/src"],
2288 },
2289 include_build_directory: false,
2290}`,
2291 ExpectedBazelTargets: []string{
2292 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2293 "srcs": `["foo.proto"]`,
2294 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2295 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2296 "deps": `[":foo_proto"]`,
2297 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2298 "deps": `[":libprotobuf-cpp-lite"]`,
2299 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2300 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002301 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2302 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002303 }),
2304 },
2305 })
2306}
2307
2308func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2309 runCcLibraryTestCase(t, Bp2buildTestCase{
2310 ModuleTypeUnderTest: "cc_library",
2311 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2312 Blueprint: soongCcProtoPreamble + `cc_library {
2313 name: "foo",
2314 srcs: ["foo.proto"],
2315 proto: {
2316 include_dirs: ["external/protobuf/abc"],
2317 },
2318 include_build_directory: false,
2319}`,
2320 ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"),
2321 })
2322}
2323
Yu Liu2aa806b2022-09-01 11:54:47 -07002324func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2325 runCcLibraryTestCase(t, Bp2buildTestCase{
2326 ModuleTypeUnderTest: "cc_library",
2327 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2328 Blueprint: soongCcProtoPreamble + `
2329filegroup {
2330 name: "a_fg_proto",
2331 srcs: ["a_fg.proto"],
2332}
2333
2334cc_library {
2335 name: "a",
2336 srcs: [
2337 ":a_fg_proto",
2338 "a.proto",
2339 ],
2340 proto: {
2341 export_proto_headers: true,
2342 },
2343 include_build_directory: false,
2344}`,
2345 ExpectedBazelTargets: []string{
2346 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002347 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002348 "srcs": `["a.proto"]`,
2349 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2350 "deps": `[
2351 ":a_fg_proto_bp2build_converted",
2352 ":a_proto",
2353 ]`,
2354 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2355 "deps": `[":libprotobuf-cpp-lite"]`,
2356 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2357 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2358 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2359 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2360 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2361 "srcs": `["a_fg.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -07002362 "tags": `["manual"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002363 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2364 "srcs": `["a_fg.proto"]`,
2365 }),
2366 },
2367 })
2368}
2369
2370func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2371 runCcLibraryTestCase(t, Bp2buildTestCase{
2372 ModuleTypeUnderTest: "cc_library",
2373 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2374 Blueprint: soongCcProtoPreamble + `
2375filegroup {
2376 name: "a_fg_proto",
2377 srcs: ["a_fg.proto"],
2378}
2379
2380cc_library {
2381 name: "a",
2382 srcs: [
2383 ":a_fg_proto",
2384 ],
2385 proto: {
2386 export_proto_headers: true,
2387 },
2388 include_build_directory: false,
2389}`,
2390 ExpectedBazelTargets: []string{
2391 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2392 "deps": `[":a_fg_proto_bp2build_converted"]`,
2393 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2394 "deps": `[":libprotobuf-cpp-lite"]`,
2395 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2396 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2397 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2398 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2399 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2400 "srcs": `["a_fg.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -07002401 "tags": `["manual"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002402 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2403 "srcs": `["a_fg.proto"]`,
2404 }),
2405 },
2406 })
2407}
2408
2409func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2410 runCcLibraryTestCase(t, Bp2buildTestCase{
2411 ModuleTypeUnderTest: "cc_library",
2412 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2413 Filesystem: map[string]string{
2414 "path/to/A/Android.bp": `
2415filegroup {
2416 name: "a_fg_proto",
2417 srcs: ["a_fg.proto"],
2418}`,
2419 },
2420 Blueprint: soongCcProtoPreamble + `
2421cc_library {
2422 name: "a",
2423 srcs: [
2424 ":a_fg_proto",
2425 "a.proto",
2426 ],
2427 proto: {
2428 export_proto_headers: true,
2429 },
2430 include_build_directory: false,
2431}`,
2432 ExpectedBazelTargets: []string{
2433 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002434 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002435 "srcs": `["a.proto"]`,
2436 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2437 "deps": `[
2438 "//path/to/A:a_fg_proto_bp2build_converted",
2439 ":a_proto",
2440 ]`,
2441 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2442 "deps": `[":libprotobuf-cpp-lite"]`,
2443 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2444 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2445 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2446 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2447 }),
2448 },
2449 })
2450}
2451
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002452func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002453 runCcLibraryTestCase(t, Bp2buildTestCase{
2454 ModuleTypeUnderTest: "cc_library",
2455 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2456 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002457 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2458 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2459 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2460 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2461cc_library {
2462 name: "a",
2463 srcs: [":a_fg_proto"],
2464 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002465 export_proto_headers: true,
2466 },
2467 include_build_directory: false,
2468}
2469
2470cc_library {
2471 name: "b",
2472 srcs: [":b_protos"],
2473 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002474 export_proto_headers: true,
2475 },
2476 include_build_directory: false,
2477}
2478
2479cc_library {
2480 name: "c",
2481 srcs: [":c-proto-srcs"],
2482 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002483 export_proto_headers: true,
2484 },
2485 include_build_directory: false,
2486}
2487
2488cc_library {
2489 name: "d",
2490 srcs: [":proto-srcs-d"],
2491 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002492 export_proto_headers: true,
2493 },
2494 include_build_directory: false,
2495}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002496 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002497 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002498 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002499 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002500 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002501 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002502 "deps": `[":libprotobuf-cpp-lite"]`,
2503 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2504 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2505 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2506 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002507 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002508 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2509 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2510 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2511 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2512 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002513 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002514 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002515 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002516 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002517 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002518 "deps": `[":libprotobuf-cpp-lite"]`,
2519 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2520 "srcs": `[":b_protos_cpp_srcs"]`,
2521 "srcs_as": `[":b_protos_as_srcs"]`,
2522 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002523 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002524 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2525 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2526 "srcs": `[":b_protos_cpp_srcs"]`,
2527 "srcs_as": `[":b_protos_as_srcs"]`,
2528 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002529 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002530 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002531 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002532 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002533 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002534 "deps": `[":libprotobuf-cpp-lite"]`,
2535 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2536 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2537 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2538 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002539 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002540 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2541 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2542 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2543 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2544 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002545 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002546 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002547 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002548 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002549 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002550 "deps": `[":libprotobuf-cpp-lite"]`,
2551 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2552 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2553 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2554 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002555 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002556 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2557 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2558 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2559 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2560 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2561 }),
2562 },
2563 })
2564}
Chris Parsons58852a02021-12-09 18:10:18 -05002565
2566func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002567 runCcLibraryTestCase(t, Bp2buildTestCase{
2568 ModuleTypeUnderTest: "cc_library",
2569 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2570 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002571 name: "foo",
2572 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002573 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002574 target: {
2575 darwin: {
2576 enabled: false,
2577 },
2578 windows: {
2579 enabled: false,
2580 },
2581 linux_glibc_x86: {
2582 enabled: false,
2583 },
2584 },
2585 include_build_directory: false,
2586}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002587 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002588 "srcs": `["foo.cpp"]`,
2589 "target_compatible_with": `select({
2590 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2591 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2592 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2593 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2594 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2595 "//conditions:default": [],
2596 })`,
2597 }),
2598 })
2599}
2600
2601func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002602 runCcLibraryTestCase(t, Bp2buildTestCase{
2603 ModuleTypeUnderTest: "cc_library",
2604 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2605 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002606 name: "foo",
2607 srcs: ["foo.cpp"],
2608 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002609 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002610 target: {
2611 darwin: {
2612 enabled: true,
2613 },
2614 windows: {
2615 enabled: false,
2616 },
2617 linux_glibc_x86: {
2618 enabled: false,
2619 },
2620 },
2621 include_build_directory: false,
2622}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002623 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002624 "srcs": `["foo.cpp"]`,
2625 "target_compatible_with": `select({
2626 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2627 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2628 "//conditions:default": ["@platforms//:incompatible"],
2629 })`,
2630 }),
2631 })
2632}
2633
2634func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002635 runCcLibraryTestCase(t, Bp2buildTestCase{
2636 ModuleTypeUnderTest: "cc_library",
2637 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2638 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002639 name: "foo",
2640 srcs: ["foo.cpp"],
2641 enabled: false,
2642 shared: {
2643 enabled: true,
2644 },
2645 target: {
2646 android: {
2647 shared: {
2648 enabled: false,
2649 },
2650 }
2651 },
2652 include_build_directory: false,
2653}`,
Alixe06d75b2022-08-31 18:28:19 +00002654 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002655 "srcs": `["foo.cpp"]`,
2656 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002657 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002658 "srcs": `["foo.cpp"]`,
2659 "target_compatible_with": `select({
2660 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2661 "//conditions:default": [],
2662 })`,
2663 }),
2664 },
2665 })
2666}
2667
2668func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002669 runCcLibraryTestCase(t, Bp2buildTestCase{
2670 ModuleTypeUnderTest: "cc_library",
2671 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2672 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002673 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002674 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002675 srcs: ["foo.cpp"],
2676 shared: {
2677 enabled: false
2678 },
2679 target: {
2680 darwin: {
2681 enabled: true,
2682 },
2683 windows: {
2684 enabled: false,
2685 },
2686 linux_glibc_x86: {
2687 shared: {
2688 enabled: true,
2689 },
2690 },
2691 },
2692 include_build_directory: false,
2693}`,
Alixe06d75b2022-08-31 18:28:19 +00002694 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002695 "srcs": `["foo.cpp"]`,
2696 "target_compatible_with": `select({
2697 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2698 "//conditions:default": [],
2699 })`,
Alixe06d75b2022-08-31 18:28:19 +00002700 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002701 "srcs": `["foo.cpp"]`,
2702 "target_compatible_with": `select({
2703 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2704 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2705 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2706 "//conditions:default": ["@platforms//:incompatible"],
2707 })`,
2708 }),
2709 }})
2710}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002711
2712func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002713 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002714 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002715 })
2716 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
2717 "soname": `"a.so"`,
2718 "source_library": `":a"`,
2719 "stubs_symbol_file": `"a.map.txt"`,
2720 "stubs_versions": `[
2721 "28",
2722 "29",
2723 "current",
2724 ]`,
2725 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002726 runCcLibraryTestCase(t, Bp2buildTestCase{
2727 Description: "cc_library stubs",
2728 ModuleTypeUnderTest: "cc_library",
2729 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2730 Dir: "foo/bar",
2731 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002732 "foo/bar/Android.bp": `
2733cc_library {
2734 name: "a",
2735 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2736 bazel_module: { bp2build_available: true },
2737 include_build_directory: false,
2738}
2739`,
2740 },
Wei Li81852ca2022-07-27 00:22:06 -07002741 Blueprint: soongCcLibraryPreamble,
2742 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002743 },
2744 )
2745}
Liz Kammerf38a8372022-02-04 15:39:00 -05002746
Spandan Das4238c652022-09-09 01:38:47 +00002747func TestCcApiContributionsWithHdrs(t *testing.T) {
2748 bp := `
2749 cc_library {
2750 name: "libfoo",
2751 stubs: { symbol_file: "libfoo.map.txt", versions: ["28", "29", "current"] },
2752 llndk: { symbol_file: "libfoo.map.txt", override_export_include_dirs: ["dir2"]},
2753 export_include_dirs: ["dir1"],
2754 }
2755 `
2756 expectedBazelTargets := []string{
2757 MakeBazelTarget(
2758 "cc_api_library_headers",
2759 "libfoo.systemapi.headers",
2760 AttrNameToString{
2761 "export_includes": `["dir1"]`,
2762 }),
2763 MakeBazelTarget(
2764 "cc_api_library_headers",
2765 "libfoo.vendorapi.headers",
2766 AttrNameToString{
2767 "export_includes": `["dir2"]`,
2768 }),
2769 MakeBazelTarget(
2770 "cc_api_contribution",
2771 "libfoo.contribution",
2772 AttrNameToString{
2773 "api": `"libfoo.map.txt"`,
2774 "library_name": `"libfoo"`,
2775 "api_surfaces": `[
2776 "systemapi",
2777 "vendorapi",
2778 ]`,
2779 "hdrs": `[
2780 ":libfoo.systemapi.headers",
2781 ":libfoo.vendorapi.headers",
2782 ]`,
2783 }),
2784 }
2785 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2786 Blueprint: bp,
2787 Description: "cc API contributions to systemapi and vendorapi",
2788 ExpectedBazelTargets: expectedBazelTargets,
2789 })
2790}
2791
2792func TestCcApiSurfaceCombinations(t *testing.T) {
2793 testCases := []struct {
2794 bp string
2795 expectedApi string
2796 expectedApiSurfaces string
2797 description string
2798 }{
2799 {
2800 bp: `
2801 cc_library {
2802 name: "a",
2803 stubs: {symbol_file: "a.map.txt"},
2804 }`,
2805 expectedApi: `"a.map.txt"`,
2806 expectedApiSurfaces: `["systemapi"]`,
2807 description: "Library that contributes to systemapi",
2808 },
2809 {
2810 bp: `
2811 cc_library {
2812 name: "a",
2813 llndk: {symbol_file: "a.map.txt"},
2814 }`,
2815 expectedApi: `"a.map.txt"`,
2816 expectedApiSurfaces: `["vendorapi"]`,
2817 description: "Library that contributes to vendorapi",
2818 },
2819 {
2820 bp: `
2821 cc_library {
2822 name: "a",
2823 llndk: {symbol_file: "a.map.txt"},
2824 stubs: {symbol_file: "a.map.txt"},
2825 }`,
2826 expectedApi: `"a.map.txt"`,
2827 expectedApiSurfaces: `[
2828 "systemapi",
2829 "vendorapi",
2830 ]`,
2831 description: "Library that contributes to systemapi and vendorapi",
2832 },
2833 }
2834 for _, testCase := range testCases {
2835 expectedBazelTargets := []string{
2836 MakeBazelTarget(
2837 "cc_api_contribution",
2838 "a.contribution",
2839 AttrNameToString{
2840 "library_name": `"a"`,
2841 "hdrs": `[]`,
2842 "api": testCase.expectedApi,
2843 "api_surfaces": testCase.expectedApiSurfaces,
2844 },
2845 ),
2846 }
2847 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2848 Blueprint: testCase.bp,
2849 Description: testCase.description,
2850 ExpectedBazelTargets: expectedBazelTargets,
2851 })
2852 }
2853}
2854
2855// llndk struct property in Soong provides users with several options to configure the exported include dirs
2856// Test the generated bazel targets for the different configurations
2857func TestCcVendorApiHeaders(t *testing.T) {
2858 testCases := []struct {
2859 bp string
2860 expectedIncludes string
2861 expectedSystemIncludes string
2862 description string
2863 }{
2864 {
2865 bp: `
2866 cc_library {
2867 name: "a",
2868 export_include_dirs: ["include"],
2869 export_system_include_dirs: ["base_system_include"],
2870 llndk: {
2871 symbol_file: "a.map.txt",
2872 export_headers_as_system: true,
2873 },
2874 }
2875 `,
2876 expectedIncludes: "",
2877 expectedSystemIncludes: `[
2878 "base_system_include",
2879 "include",
2880 ]`,
2881 description: "Headers are exported as system to API surface",
2882 },
2883 {
2884 bp: `
2885 cc_library {
2886 name: "a",
2887 export_include_dirs: ["include"],
2888 export_system_include_dirs: ["base_system_include"],
2889 llndk: {
2890 symbol_file: "a.map.txt",
2891 override_export_include_dirs: ["llndk_include"],
2892 },
2893 }
2894 `,
2895 expectedIncludes: `["llndk_include"]`,
2896 expectedSystemIncludes: `["base_system_include"]`,
2897 description: "Non-system Headers are ovverriden before export to API surface",
2898 },
2899 {
2900 bp: `
2901 cc_library {
2902 name: "a",
2903 export_include_dirs: ["include"],
2904 export_system_include_dirs: ["base_system_include"],
2905 llndk: {
2906 symbol_file: "a.map.txt",
2907 override_export_include_dirs: ["llndk_include"],
2908 export_headers_as_system: true,
2909 },
2910 }
2911 `,
2912 expectedIncludes: "", // includes are set to nil
2913 expectedSystemIncludes: `[
2914 "base_system_include",
2915 "llndk_include",
2916 ]`,
2917 description: "System Headers are extended before export to API surface",
2918 },
2919 }
2920 for _, testCase := range testCases {
2921 attrs := AttrNameToString{}
2922 if testCase.expectedIncludes != "" {
2923 attrs["export_includes"] = testCase.expectedIncludes
2924 }
2925 if testCase.expectedSystemIncludes != "" {
2926 attrs["export_system_includes"] = testCase.expectedSystemIncludes
2927 }
2928
2929 expectedBazelTargets := []string{
2930 MakeBazelTarget("cc_api_library_headers", "a.vendorapi.headers", attrs),
2931 // Create a target for cc_api_contribution target
2932 MakeBazelTarget("cc_api_contribution", "a.contribution", AttrNameToString{
2933 "api": `"a.map.txt"`,
2934 "api_surfaces": `["vendorapi"]`,
2935 "hdrs": `[":a.vendorapi.headers"]`,
2936 "library_name": `"a"`,
2937 }),
2938 }
2939 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2940 Blueprint: testCase.bp,
2941 ExpectedBazelTargets: expectedBazelTargets,
2942 })
2943 }
2944}
2945
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002946func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2947 runCcLibraryTestCase(t, Bp2buildTestCase{
2948 Description: "stub target generation of the same lib across configs should not result in duplicates",
2949 ModuleTypeUnderTest: "cc_library",
2950 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2951 Filesystem: map[string]string{
2952 "bar.map.txt": "",
2953 },
2954 Blueprint: `
2955cc_library {
2956 name: "barlib",
2957 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2958 bazel_module: { bp2build_available: false },
2959}
2960cc_library {
2961 name: "foolib",
2962 shared_libs: ["barlib"],
2963 target: {
2964 android: {
2965 shared_libs: ["barlib"],
2966 },
2967 },
2968 bazel_module: { bp2build_available: true },
2969}`,
2970 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2971 "implementation_dynamic_deps": `select({
2972 "//build/bazel/rules/apex:android-in_apex": [":barlib_stub_libs_current"],
2973 "//conditions:default": [":barlib"],
2974 })`,
2975 "local_includes": `["."]`,
2976 }),
2977 })
2978}
2979
Liz Kammerffc17e42022-11-23 09:42:05 -05002980func TestCcLibraryExcludesLibsHost(t *testing.T) {
2981 runCcLibraryTestCase(t, Bp2buildTestCase{
2982 ModuleTypeUnderTest: "cc_library",
2983 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2984 Filesystem: map[string]string{
2985 "bar.map.txt": "",
2986 },
2987 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
2988cc_library {
2989 name: "quxlib",
2990 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
2991 bazel_module: { bp2build_available: false },
2992}
2993cc_library {
2994 name: "barlib",
2995 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
2996 bazel_module: { bp2build_available: false },
2997}
2998cc_library {
2999 name: "foolib",
3000 shared_libs: ["barlib", "quxlib"],
3001 target: {
3002 host: {
3003 shared_libs: ["bazlib"],
3004 exclude_shared_libs: ["barlib"],
3005 },
3006 },
3007 include_build_directory: false,
3008 bazel_module: { bp2build_available: true },
3009}`,
3010 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3011 "implementation_dynamic_deps": `select({
3012 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003013 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003014 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003015 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
3016 "//build/bazel/platforms/os:windows": [":bazlib"],
3017 "//conditions:default": [],
3018 }) + select({
3019 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003020 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003021 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003022 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
3023 "//build/bazel/platforms/os:windows": [":quxlib"],
3024 "//build/bazel/rules/apex:android-in_apex": [
3025 ":barlib_stub_libs_current",
3026 ":quxlib_stub_libs_current",
3027 ],
3028 "//conditions:default": [
3029 ":barlib",
3030 ":quxlib",
3031 ],
3032 })`,
3033 }),
3034 })
3035}
3036
Liz Kammerf38a8372022-02-04 15:39:00 -05003037func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003038 runCcLibraryTestCase(t, Bp2buildTestCase{
3039 ModuleTypeUnderTest: "cc_library",
3040 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3041 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05003042 name: "foo",
3043 ldflags: ["-Wl,--rpath,${ORIGIN}"],
3044 include_build_directory: false,
3045}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003046 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05003047 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
3048 }),
3049 })
3050}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003051
3052func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003053 runCcLibraryTestCase(t, Bp2buildTestCase{
3054 ModuleTypeUnderTest: "cc_library",
3055 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3056 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003057 "foo.c": "",
3058 "bar.cc": "",
3059 "foo1.l": "",
3060 "bar1.ll": "",
3061 "foo2.l": "",
3062 "bar2.ll": "",
3063 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003064 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003065 name: "foo_lib",
3066 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
3067 lex: { flags: ["--foo_flags"] },
3068 include_build_directory: false,
3069 bazel_module: { bp2build_available: true },
3070}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003071 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00003072 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003073 "srcs": `[
3074 "foo1.l",
3075 "foo2.l",
3076 ]`,
3077 "lexopts": `["--foo_flags"]`,
3078 }),
Alixe06d75b2022-08-31 18:28:19 +00003079 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003080 "srcs": `[
3081 "bar1.ll",
3082 "bar2.ll",
3083 ]`,
3084 "lexopts": `["--foo_flags"]`,
3085 }),
3086 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003087 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003088 "srcs": `[
3089 "bar.cc",
3090 ":foo_lib_genlex_ll",
3091 ]`,
3092 "srcs_c": `[
3093 "foo.c",
3094 ":foo_lib_genlex_l",
3095 ]`,
3096 })...),
3097 })
3098}
Cole Faust6b29f592022-08-09 09:50:56 -07003099
3100func TestCCLibraryRuntimeDeps(t *testing.T) {
3101 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3102 Blueprint: `cc_library_shared {
3103 name: "bar",
3104}
3105
3106cc_library {
3107 name: "foo",
3108 runtime_libs: ["foo"],
3109}`,
3110 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003111 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003112 "local_includes": `["."]`,
3113 }),
Alixe06d75b2022-08-31 18:28:19 +00003114 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003115 "runtime_deps": `[":foo"]`,
3116 "local_includes": `["."]`,
3117 }),
Alixe06d75b2022-08-31 18:28:19 +00003118 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003119 "runtime_deps": `[":foo"]`,
3120 "local_includes": `["."]`,
3121 }),
3122 },
3123 })
3124}
Cole Faust5fa4e962022-08-22 14:31:04 -07003125
3126func TestCcLibraryWithInstructionSet(t *testing.T) {
3127 runCcLibraryTestCase(t, Bp2buildTestCase{
3128 ModuleTypeUnderTest: "cc_library",
3129 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3130 Blueprint: `cc_library {
3131 name: "foo",
3132 arch: {
3133 arm: {
3134 instruction_set: "arm",
3135 }
3136 }
3137}
3138`,
3139 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3140 "features": `select({
3141 "//build/bazel/platforms/arch:arm": [
3142 "arm_isa_arm",
3143 "-arm_isa_thumb",
3144 ],
3145 "//conditions:default": [],
3146 })`,
3147 "local_includes": `["."]`,
3148 }),
3149 })
3150}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003151
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003152func TestCcLibraryEmptySuffix(t *testing.T) {
3153 runCcLibraryTestCase(t, Bp2buildTestCase{
3154 Description: "cc_library with empty suffix",
3155 ModuleTypeUnderTest: "cc_library",
3156 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3157 Filesystem: map[string]string{
3158 "foo.c": "",
3159 },
3160 Blueprint: `cc_library {
3161 name: "foo",
3162 suffix: "",
3163 srcs: ["foo.c"],
3164 include_build_directory: false,
3165}`,
3166 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003167 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 -05003168 "srcs_c": `["foo.c"]`,
3169 }),
Alixe06d75b2022-08-31 18:28:19 +00003170 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003171 "srcs_c": `["foo.c"]`,
3172 "suffix": `""`,
3173 }),
3174 },
3175 })
3176}
3177
3178func TestCcLibrarySuffix(t *testing.T) {
3179 runCcLibraryTestCase(t, Bp2buildTestCase{
3180 Description: "cc_library with suffix",
3181 ModuleTypeUnderTest: "cc_library",
3182 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3183 Filesystem: map[string]string{
3184 "foo.c": "",
3185 },
3186 Blueprint: `cc_library {
3187 name: "foo",
3188 suffix: "-suf",
3189 srcs: ["foo.c"],
3190 include_build_directory: false,
3191}`,
3192 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003193 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 -05003194 "srcs_c": `["foo.c"]`,
3195 }),
Alixe06d75b2022-08-31 18:28:19 +00003196 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003197 "srcs_c": `["foo.c"]`,
3198 "suffix": `"-suf"`,
3199 }),
3200 },
3201 })
3202}
3203
3204func TestCcLibraryArchVariantSuffix(t *testing.T) {
3205 runCcLibraryTestCase(t, Bp2buildTestCase{
3206 Description: "cc_library with arch-variant suffix",
3207 ModuleTypeUnderTest: "cc_library",
3208 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3209 Filesystem: map[string]string{
3210 "foo.c": "",
3211 },
3212 Blueprint: `cc_library {
3213 name: "foo",
3214 arch: {
3215 arm64: { suffix: "-64" },
3216 arm: { suffix: "-32" },
3217 },
3218 srcs: ["foo.c"],
3219 include_build_directory: false,
3220}`,
3221 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003222 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 -05003223 "srcs_c": `["foo.c"]`,
3224 }),
Alixe06d75b2022-08-31 18:28:19 +00003225 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003226 "srcs_c": `["foo.c"]`,
3227 "suffix": `select({
3228 "//build/bazel/platforms/arch:arm": "-32",
3229 "//build/bazel/platforms/arch:arm64": "-64",
3230 "//conditions:default": None,
3231 })`,
3232 }),
3233 },
3234 })
3235}
3236
Vinh Tran9f6796a2022-08-16 13:10:31 -04003237func TestCcLibraryWithAidlSrcs(t *testing.T) {
3238 runCcLibraryTestCase(t, Bp2buildTestCase{
3239 Description: "cc_library with aidl srcs",
3240 ModuleTypeUnderTest: "cc_library",
3241 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3242 Blueprint: `
3243filegroup {
3244 name: "A_aidl",
3245 srcs: ["aidl/A.aidl"],
3246 path: "aidl",
3247}
3248cc_library {
3249 name: "foo",
3250 srcs: [
3251 ":A_aidl",
3252 "B.aidl",
3253 ],
3254}`,
3255 ExpectedBazelTargets: []string{
3256 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3257 "srcs": `["aidl/A.aidl"]`,
3258 "strip_import_prefix": `"aidl"`,
3259 }),
Alixe06d75b2022-08-31 18:28:19 +00003260 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003261 "srcs": `["B.aidl"]`,
3262 }),
Alixe06d75b2022-08-31 18:28:19 +00003263 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003264 "deps": `[
3265 ":A_aidl",
3266 ":foo_aidl_library",
3267 ]`,
3268 }),
Alixe06d75b2022-08-31 18:28:19 +00003269 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003270 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3271 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003272 }),
Alixe06d75b2022-08-31 18:28:19 +00003273 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003274 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3275 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003276 }),
3277 },
3278 })
3279}
3280
3281func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3282 runCcLibraryTestCase(t, Bp2buildTestCase{
3283 Description: "cc_library with non aidl filegroup",
3284 ModuleTypeUnderTest: "cc_library",
3285 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3286 Filesystem: map[string]string{
3287 "path/to/A/Android.bp": `
3288filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003289 name: "A_aidl",
3290 srcs: ["aidl/A.aidl"],
3291 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003292}`,
3293 },
3294 Blueprint: `
3295cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003296 name: "foo",
3297 srcs: [
3298 ":A_aidl",
3299 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003300}`,
3301 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003302 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003303 "deps": `["//path/to/A:A_aidl"]`,
3304 }),
Alixe06d75b2022-08-31 18:28:19 +00003305 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003306 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3307 "local_includes": `["."]`,
3308 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003309 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003310 "local_includes": `["."]`,
3311 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003312 }),
3313 },
3314 })
3315}
3316
3317func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
3318 runCcLibraryTestCase(t, Bp2buildTestCase{
3319 Description: "cc_library with export aidl headers",
3320 ModuleTypeUnderTest: "cc_library",
3321 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3322 Blueprint: `
3323cc_library {
3324 name: "foo",
3325 srcs: [
3326 "Foo.aidl",
3327 ],
3328 aidl: {
3329 export_aidl_headers: true,
3330 }
3331}`,
3332 ExpectedBazelTargets: []string{
3333 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3334 "srcs": `["Foo.aidl"]`,
3335 }),
3336 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3337 "deps": `[":foo_aidl_library"]`,
3338 }),
3339 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003340 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3341 "local_includes": `["."]`,
3342 }),
Alixe06d75b2022-08-31 18:28:19 +00003343 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003344 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3345 "local_includes": `["."]`,
3346 }),
3347 },
3348 })
3349}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003350
3351func TestCcLibraryWithTargetApex(t *testing.T) {
3352 runCcLibraryTestCase(t, Bp2buildTestCase{
3353 Description: "cc_library with target.apex",
3354 ModuleTypeUnderTest: "cc_library",
3355 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3356 Blueprint: `
3357cc_library {
3358 name: "foo",
3359 shared_libs: ["bar", "baz"],
3360 static_libs: ["baz", "buh"],
3361 target: {
3362 apex: {
3363 exclude_shared_libs: ["bar"],
3364 exclude_static_libs: ["buh"],
3365 }
3366 }
3367}`,
3368 ExpectedBazelTargets: []string{
3369 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3370 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3371 "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
3372 "//conditions:default": [],
3373 })`,
3374 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3375 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3376 "//conditions:default": [],
3377 })`,
3378 "local_includes": `["."]`,
3379 }),
3380 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3381 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3382 "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
3383 "//conditions:default": [],
3384 })`,
3385 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
3386 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3387 "//conditions:default": [],
3388 })`,
3389 "local_includes": `["."]`,
3390 }),
3391 },
3392 })
3393}
3394
3395func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3396 runCcLibraryTestCase(t, Bp2buildTestCase{
3397 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3398 ModuleTypeUnderTest: "cc_library",
3399 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3400 Blueprint: `
3401cc_library_static {
3402 name: "foo",
3403 shared_libs: ["bar", "baz"],
3404 static_libs: ["abc"],
3405 export_shared_lib_headers: ["baz"],
3406 export_static_lib_headers: ["abc"],
3407 target: {
3408 apex: {
3409 exclude_shared_libs: ["baz", "bar"],
3410 exclude_static_libs: ["abc"],
3411 }
3412 }
3413}`,
3414 ExpectedBazelTargets: []string{
3415 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3416 "implementation_dynamic_deps": `select({
3417 "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
3418 "//conditions:default": [],
3419 })`,
3420 "dynamic_deps": `select({
3421 "//build/bazel/rules/apex:non_apex": [":baz__BP2BUILD__MISSING__DEP"],
3422 "//conditions:default": [],
3423 })`,
3424 "deps": `select({
3425 "//build/bazel/rules/apex:non_apex": [":abc__BP2BUILD__MISSING__DEP"],
3426 "//conditions:default": [],
3427 })`,
3428 "local_includes": `["."]`,
3429 }),
3430 },
3431 })
3432}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003433
3434func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3435 runCcLibraryTestCase(t, Bp2buildTestCase{
3436 Description: "cc_library with sysprop sources",
3437 ModuleTypeUnderTest: "cc_library",
3438 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3439 Blueprint: `
3440cc_library {
3441 name: "foo",
3442 srcs: [
3443 "bar.sysprop",
3444 "baz.sysprop",
3445 "blah.cpp",
3446 ],
3447 min_sdk_version: "5",
3448}`,
3449 ExpectedBazelTargets: []string{
3450 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3451 "srcs": `[
3452 "bar.sysprop",
3453 "baz.sysprop",
3454 ]`,
3455 }),
3456 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3457 "dep": `":foo_sysprop_library"`,
3458 "min_sdk_version": `"5"`,
3459 }),
3460 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3461 "srcs": `["blah.cpp"]`,
3462 "local_includes": `["."]`,
3463 "min_sdk_version": `"5"`,
3464 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3465 }),
3466 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3467 "srcs": `["blah.cpp"]`,
3468 "local_includes": `["."]`,
3469 "min_sdk_version": `"5"`,
3470 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3471 }),
3472 },
3473 })
3474}
3475
3476func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3477 runCcLibraryTestCase(t, Bp2buildTestCase{
3478 Description: "cc_library with sysprop sources in some configs but not others",
3479 ModuleTypeUnderTest: "cc_library",
3480 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3481 Blueprint: `
3482cc_library {
3483 name: "foo",
3484 host_supported: true,
3485 srcs: [
3486 "blah.cpp",
3487 ],
3488 target: {
3489 android: {
3490 srcs: ["bar.sysprop"],
3491 },
3492 },
3493 min_sdk_version: "5",
3494}`,
3495 ExpectedBazelTargets: []string{
3496 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3497 "srcs": `select({
3498 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3499 "//conditions:default": [],
3500 })`,
3501 }),
3502 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3503 "dep": `":foo_sysprop_library"`,
3504 "min_sdk_version": `"5"`,
3505 }),
3506 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3507 "srcs": `["blah.cpp"]`,
3508 "local_includes": `["."]`,
3509 "min_sdk_version": `"5"`,
3510 "whole_archive_deps": `select({
3511 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3512 "//conditions:default": [],
3513 })`,
3514 }),
3515 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3516 "srcs": `["blah.cpp"]`,
3517 "local_includes": `["."]`,
3518 "min_sdk_version": `"5"`,
3519 "whole_archive_deps": `select({
3520 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3521 "//conditions:default": [],
3522 })`,
3523 }),
3524 },
3525 })
3526}
Vinh Tran395a1e92022-09-16 18:27:29 -04003527
3528func TestCcLibraryWithAidlAndSharedLibs(t *testing.T) {
3529 runCcLibraryTestCase(t, Bp2buildTestCase{
3530 Description: "cc_aidl_library depends on shared libs from parent cc_library_static",
3531 ModuleTypeUnderTest: "cc_library",
3532 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3533 Blueprint: `
3534cc_library_static {
3535 name: "foo",
3536 srcs: [
3537 "Foo.aidl",
3538 ],
3539 shared_libs: [
3540 "bar",
3541 "baz",
3542 ],
3543 export_shared_lib_headers: [
3544 "baz",
3545 ],
3546}` +
3547 simpleModuleDoNotConvertBp2build("cc_library", "bar") +
3548 simpleModuleDoNotConvertBp2build("cc_library", "baz"),
3549 ExpectedBazelTargets: []string{
3550 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3551 "srcs": `["Foo.aidl"]`,
3552 }),
3553 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3554 "deps": `[":foo_aidl_library"]`,
3555 "implementation_dynamic_deps": `[
3556 ":baz",
3557 ":bar",
3558 ]`,
3559 }),
3560 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3561 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3562 "dynamic_deps": `[":baz"]`,
3563 "implementation_dynamic_deps": `[":bar"]`,
3564 "local_includes": `["."]`,
3565 }),
3566 },
3567 })
3568}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003569
3570func TestCcLibraryWithTidy(t *testing.T) {
3571 runCcLibraryTestCase(t, Bp2buildTestCase{
3572 Description: "cc_library uses tidy properties",
3573 ModuleTypeUnderTest: "cc_library",
3574 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3575 Blueprint: `
3576cc_library_static {
3577 name: "foo",
3578 srcs: ["foo.cpp"],
3579 tidy: true,
3580 tidy_checks: ["check1", "check2"],
3581 tidy_checks_as_errors: ["check1error", "check2error"],
3582 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003583 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003584}`,
3585 ExpectedBazelTargets: []string{
3586 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3587 "local_includes": `["."]`,
3588 "srcs": `["foo.cpp"]`,
3589 "tidy": `True`,
3590 "tidy_checks": `[
3591 "check1",
3592 "check2",
3593 ]`,
3594 "tidy_checks_as_errors": `[
3595 "check1error",
3596 "check2error",
3597 ]`,
3598 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003599 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003600 }),
3601 },
3602 })
3603}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003604
Vinh Tran99270ea2022-11-28 11:15:23 -05003605func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3606 bp := `
3607cc_library {
3608 name: "foo",
3609 afdo: true,
3610 include_build_directory: false,
3611}`
3612
3613 // TODO(b/260714900): Add test case for arch-specific afdo profile
3614 testCases := []struct {
3615 description string
3616 filesystem map[string]string
3617 expectedBazelTargets []string
3618 }{
3619 {
3620 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003621 filesystem: map[string]string{
3622 "vendor/google_data/pgo_profile/sampling/BUILD": "",
3623 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3624 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003625 expectedBazelTargets: []string{
3626 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3627 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3628 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3629 }),
3630 },
3631 },
3632 {
3633 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003634 filesystem: map[string]string{
3635 "toolchain/pgo-profiles/sampling/BUILD": "",
3636 "toolchain/pgo-profiles/sampling/foo.afdo": "",
3637 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003638 expectedBazelTargets: []string{
3639 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3640 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3641 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3642 }),
3643 },
3644 },
3645 {
3646 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003647 filesystem: map[string]string{
3648 "toolchain/pgo-profiles/sampling/BUILD": "",
3649 "toolchain/pgo-profiles/sampling/bar.afdo": "",
3650 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003651 expectedBazelTargets: []string{
3652 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3653 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3654 },
3655 },
3656 {
3657 description: "cc_library with afdo enabled but profile doesn't exist",
3658 expectedBazelTargets: []string{
3659 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3660 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3661 },
3662 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003663 {
3664 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3665 filesystem: map[string]string{
3666 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3667 },
3668 expectedBazelTargets: []string{
3669 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3670 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3671 },
3672 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003673 }
3674 for _, testCase := range testCases {
3675 t.Run(testCase.description, func(t *testing.T) {
3676 runCcLibraryTestCase(t, Bp2buildTestCase{
3677 ExpectedBazelTargets: testCase.expectedBazelTargets,
3678 ModuleTypeUnderTest: "cc_library",
3679 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3680 Description: testCase.description,
3681 Blueprint: binaryReplacer.Replace(bp),
3682 Filesystem: testCase.filesystem,
3683 })
3684 })
3685 }
3686}
3687
Yu Liu56ccb1a2022-11-12 10:47:07 -08003688func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3689 runCcLibraryTestCase(t, Bp2buildTestCase{
3690 Description: "cc_library with header abi checker",
3691 ModuleTypeUnderTest: "cc_library",
3692 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3693 Blueprint: `cc_library {
3694 name: "foo",
3695 header_abi_checker: {
3696 enabled: true,
3697 symbol_file: "a.map.txt",
3698 exclude_symbol_versions: [
3699 "29",
3700 "30",
3701 ],
3702 exclude_symbol_tags: [
3703 "tag1",
3704 "tag2",
3705 ],
3706 check_all_apis: true,
3707 diff_flags: ["-allow-adding-removing-weak-symbols"],
3708 },
3709 include_build_directory: false,
3710}`,
3711 ExpectedBazelTargets: []string{
3712 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3713 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3714 "abi_checker_enabled": `True`,
3715 "abi_checker_symbol_file": `"a.map.txt"`,
3716 "abi_checker_exclude_symbol_versions": `[
3717 "29",
3718 "30",
3719 ]`,
3720 "abi_checker_exclude_symbol_tags": `[
3721 "tag1",
3722 "tag2",
3723 ]`,
3724 "abi_checker_check_all_apis": `True`,
3725 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3726 }),
3727 },
3728 })
3729}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003730
3731func TestCcLibraryApexAvailable(t *testing.T) {
3732 runCcLibraryTestCase(t, Bp2buildTestCase{
3733 Description: "cc_library apex_available converted to tags",
3734 ModuleTypeUnderTest: "cc_library",
3735 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3736 Blueprint: soongCcLibraryPreamble + `
3737cc_library {
3738 name: "a",
3739 srcs: ["a.cpp"],
3740 apex_available: ["com.android.foo"],
3741}
3742`,
3743 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3744 "tags": `["apex_available=com.android.foo"]`,
3745 "srcs": `["a.cpp"]`,
3746 "local_includes": `["."]`,
3747 }),
3748 },
3749 )
3750}
3751
3752func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3753 runCcLibraryTestCase(t, Bp2buildTestCase{
3754 Description: "cc_library apex_available converted to multiple tags",
3755 ModuleTypeUnderTest: "cc_library",
3756 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3757 Blueprint: soongCcLibraryPreamble + `
3758cc_library {
3759 name: "a",
3760 srcs: ["a.cpp"],
3761 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3762}
3763`,
3764 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3765 "tags": `[
3766 "apex_available=com.android.foo",
3767 "apex_available=//apex_available:platform",
3768 "apex_available=com.android.bar",
3769 ]`,
3770 "srcs": `["a.cpp"]`,
3771 "local_includes": `["."]`,
3772 }),
3773 },
3774 )
3775}
Zi Wang0f828442022-12-28 11:18:11 -08003776
3777// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3778// In bp2build output, variant info(select) should go before general info.
3779// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3780func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3781 runCcLibraryTestCase(t, Bp2buildTestCase{
3782 Description: "cc_library variant prepend properties order",
3783 ModuleTypeUnderTest: "cc_library",
3784 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3785 Blueprint: soongCcLibraryPreamble + `
3786cc_library {
3787 name: "a",
3788 srcs: ["a.cpp"],
3789 export_include_dirs: ["eid1", "eid2"],
3790 export_system_include_dirs: ["esid1", "esid2"],
3791 target: {
3792 android: {
3793 export_include_dirs: ["android_eid1", "android_eid2"],
3794 export_system_include_dirs: ["android_esid1", "android_esid2"],
3795 },
3796 android_arm: {
3797 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3798 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3799 },
3800 linux: {
3801 export_include_dirs: ["linux_eid1", "linux_eid2"],
3802 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3803 },
3804 },
3805 multilib: {
3806 lib32: {
3807 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3808 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3809 },
3810 },
3811 arch: {
3812 arm: {
3813 export_include_dirs: ["arm_eid1", "arm_eid2"],
3814 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3815 },
3816 }
3817}
3818`,
3819 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3820 "export_includes": `select({
3821 "//build/bazel/platforms/os_arch:android_arm": [
3822 "android_arm_eid1",
3823 "android_arm_eid2",
3824 ],
3825 "//conditions:default": [],
3826 }) + select({
3827 "//build/bazel/platforms/os:android": [
3828 "android_eid1",
3829 "android_eid2",
3830 "linux_eid1",
3831 "linux_eid2",
3832 ],
3833 "//build/bazel/platforms/os:linux_bionic": [
3834 "linux_eid1",
3835 "linux_eid2",
3836 ],
3837 "//build/bazel/platforms/os:linux_glibc": [
3838 "linux_eid1",
3839 "linux_eid2",
3840 ],
3841 "//build/bazel/platforms/os:linux_musl": [
3842 "linux_eid1",
3843 "linux_eid2",
3844 ],
3845 "//conditions:default": [],
3846 }) + select({
3847 "//build/bazel/platforms/arch:arm": [
3848 "lib32_eid1",
3849 "lib32_eid2",
3850 "arm_eid1",
3851 "arm_eid2",
3852 ],
3853 "//build/bazel/platforms/arch:x86": [
3854 "lib32_eid1",
3855 "lib32_eid2",
3856 ],
3857 "//conditions:default": [],
3858 }) + [
3859 "eid1",
3860 "eid2",
3861 ]`,
3862 "export_system_includes": `select({
3863 "//build/bazel/platforms/os_arch:android_arm": [
3864 "android_arm_esid1",
3865 "android_arm_esid2",
3866 ],
3867 "//conditions:default": [],
3868 }) + select({
3869 "//build/bazel/platforms/os:android": [
3870 "android_esid1",
3871 "android_esid2",
3872 "linux_esid1",
3873 "linux_esid2",
3874 ],
3875 "//build/bazel/platforms/os:linux_bionic": [
3876 "linux_esid1",
3877 "linux_esid2",
3878 ],
3879 "//build/bazel/platforms/os:linux_glibc": [
3880 "linux_esid1",
3881 "linux_esid2",
3882 ],
3883 "//build/bazel/platforms/os:linux_musl": [
3884 "linux_esid1",
3885 "linux_esid2",
3886 ],
3887 "//conditions:default": [],
3888 }) + select({
3889 "//build/bazel/platforms/arch:arm": [
3890 "lib32_esid1",
3891 "lib32_esid2",
3892 "arm_esid1",
3893 "arm_esid2",
3894 ],
3895 "//build/bazel/platforms/arch:x86": [
3896 "lib32_esid1",
3897 "lib32_esid2",
3898 ],
3899 "//conditions:default": [],
3900 }) + [
3901 "esid1",
3902 "esid2",
3903 ]`,
3904 "srcs": `["a.cpp"]`,
3905 "local_includes": `["."]`,
3906 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
3907 }),
3908 },
3909 )
3910}