blob: 41cd451b30cc1769fc21f813b4bcb5b1fe7278e5 [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 })`,
Yu Liufe978fd2023-04-24 16:37:18 -0700156 "sdk_version": `"current"`,
157 "min_sdk_version": `"29"`,
158 "use_version_lib": `True`,
159 "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{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001311 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001312 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{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001326 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001327 "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`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001378 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1379 "features": `select({
1380 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1381 "//conditions:default": [],
1382 })`,
1383 "srcs": `["impl.cpp"]`,
1384 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001385 })
1386}
1387
1388func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001389 runCcLibraryTestCase(t, Bp2buildTestCase{
1390 ModuleTypeUnderTest: "cc_library",
1391 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1392 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001393 "impl.cpp": "",
1394 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001395 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001396cc_library {
1397 name: "foo-lib",
1398 srcs: ["impl.cpp"],
1399 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001400 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001401}
1402`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001403 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001404 "features": `["-use_libcrt"]`,
1405 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001406 }),
1407 })
1408}
1409
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001410func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001411 STATIC_ONLY_ATTRS := map[string]bool{}
1412 SHARED_ONLY_ATTRS := map[string]bool{
1413 "link_crt": true,
1414 "additional_linker_inputs": true,
1415 "linkopts": true,
1416 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001417 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001418 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001419 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001420 }
Wei Li81852ca2022-07-27 00:22:06 -07001421
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001422 sharedAttrs := AttrNameToString{}
1423 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001424 for key, val := range attrs {
1425 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1426 sharedAttrs[key] = val
1427 }
1428 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1429 staticAttrs[key] = val
1430 }
1431 }
Alixe06d75b2022-08-31 18:28:19 +00001432 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1433 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001434
1435 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001436}
1437
Jingwen Chen6ada5892021-09-17 11:38:09 +00001438func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001439 runCcLibraryTestCase(t, Bp2buildTestCase{
1440 ModuleTypeUnderTest: "cc_library",
1441 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1442 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001443 "impl.cpp": "",
1444 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001445 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001446cc_library {
1447 name: "foo-lib",
1448 srcs: ["impl.cpp"],
1449 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001450 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001451}
1452`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001453 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001454 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001455 }),
1456 })
Liz Kammerd366c902021-06-03 13:43:01 -04001457}
1458
Jingwen Chen6ada5892021-09-17 11:38:09 +00001459func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001460 runCcLibraryTestCase(t, Bp2buildTestCase{
1461 ModuleTypeUnderTest: "cc_library",
1462 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1463 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001464 "impl.cpp": "",
1465 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001466 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001467cc_library {
1468 name: "foo-lib",
1469 srcs: ["impl.cpp"],
1470 arch: {
1471 arm: {
1472 no_libcrt: true,
1473 },
1474 x86: {
1475 no_libcrt: true,
1476 },
1477 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001478 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001479}
1480`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001481 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001482 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001483 "features": `select({
1484 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1485 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1486 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001487 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001488 }),
1489 })
Liz Kammerd366c902021-06-03 13:43:01 -04001490}
1491
Chris Parsons58852a02021-12-09 18:10:18 -05001492func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001493 runCcLibraryTestCase(t, Bp2buildTestCase{
1494 ModuleTypeUnderTest: "cc_library",
1495 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1496 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001497 "impl.cpp": "",
1498 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001499 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001500cc_library {
1501 name: "foo-lib",
1502 srcs: ["impl.cpp"],
1503 arch: {
1504 arm: {
1505 no_libcrt: true,
1506 },
1507 x86: {
1508 no_libcrt: true,
1509 },
1510 },
1511 target: {
1512 darwin: {
1513 no_libcrt: true,
1514 }
1515 },
1516 include_build_directory: false,
1517}
1518`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001519 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001520 "features": `select({
1521 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1522 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1523 "//conditions:default": [],
1524 }) + select({
1525 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1526 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001527 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001528 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001529 }),
1530 })
1531}
1532
1533func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001534 runCcLibraryTestCase(t, Bp2buildTestCase{
1535 ModuleTypeUnderTest: "cc_library",
1536 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1537 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001538 "impl.cpp": "",
1539 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001540 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001541cc_library {
1542 name: "foo-lib",
1543 srcs: ["impl.cpp"],
1544 arch: {
1545 arm: {
1546 no_libcrt: true,
1547 },
1548 // This is expected to override the value for darwin_x86_64.
1549 x86_64: {
1550 no_libcrt: true,
1551 },
1552 },
1553 target: {
1554 darwin: {
1555 no_libcrt: false,
1556 }
1557 },
1558 include_build_directory: false,
1559}
1560`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001561 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001562 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001563 "features": `select({
1564 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1565 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1566 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001567 })`,
1568 }),
1569 })
1570}
1571
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001572func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001573 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001574 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001575 "strip": `{
1576 "all": True,
1577 }`,
1578 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001579 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001580 "strip": `{
1581 "keep_symbols": True,
1582 }`,
1583 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001584 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001585 "strip": `{
1586 "keep_symbols_and_debug_frame": True,
1587 }`,
1588 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001589 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001590 "strip": `{
1591 "keep_symbols_list": ["symbol"],
1592 }`,
1593 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001594 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001595 "strip": `{
1596 "none": True,
1597 }`,
1598 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001599 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001600
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001601 runCcLibraryTestCase(t, Bp2buildTestCase{
1602 Description: "cc_library strip args",
1603 ModuleTypeUnderTest: "cc_library",
1604 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1605 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001606cc_library {
1607 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001608 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001609}
1610cc_library {
1611 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001612 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001613 keep_symbols: true,
1614 },
1615 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001616}
1617cc_library {
1618 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001619 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001620 keep_symbols_and_debug_frame: true,
1621 },
1622 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001623}
1624cc_library {
1625 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001626 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001627 none: true,
1628 },
1629 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001630}
1631cc_library {
1632 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001633 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001634 keep_symbols_list: ["symbol"],
1635 },
1636 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001637}
1638cc_library {
1639 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001640 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001641 all: true,
1642 },
1643 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001644}
1645`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001646 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001647 })
1648}
1649
1650func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001651 runCcLibraryTestCase(t, Bp2buildTestCase{
1652 Description: "cc_library strip args",
1653 ModuleTypeUnderTest: "cc_library",
1654 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1655 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001656cc_library {
1657 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001658 target: {
1659 darwin: {
1660 strip: {
1661 keep_symbols_list: ["foo", "bar"]
1662 }
1663 },
1664 },
1665 arch: {
1666 arm: {
1667 strip: {
1668 keep_symbols_and_debug_frame: true,
1669 },
1670 },
1671 arm64: {
1672 strip: {
1673 keep_symbols: true,
1674 },
1675 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001676 },
1677 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001678}
1679`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001680 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001681 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001682 "keep_symbols": select({
1683 "//build/bazel/platforms/arch:arm64": True,
1684 "//conditions:default": None,
1685 }),
1686 "keep_symbols_and_debug_frame": select({
1687 "//build/bazel/platforms/arch:arm": True,
1688 "//conditions:default": None,
1689 }),
1690 "keep_symbols_list": select({
1691 "//build/bazel/platforms/os:darwin": [
1692 "foo",
1693 "bar",
1694 ],
1695 "//conditions:default": [],
1696 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001697 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001698 }),
1699 },
1700 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001701}
Chris Parsons51f8c392021-08-03 21:01:05 -04001702
1703func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001704 runCcLibraryTestCase(t, Bp2buildTestCase{
1705 Description: "cc_library system_shared_libs empty at root",
1706 ModuleTypeUnderTest: "cc_library",
1707 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1708 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001709cc_library {
1710 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001711 system_shared_libs: [],
1712 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001713}
1714`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001715 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001716 "system_dynamic_deps": `[]`,
1717 }),
1718 },
1719 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001720}
1721
1722func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001723 runCcLibraryTestCase(t, Bp2buildTestCase{
1724 Description: "cc_library system_shared_libs empty for static variant",
1725 ModuleTypeUnderTest: "cc_library",
1726 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1727 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001728cc_library {
1729 name: "static_empty",
1730 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001731 system_shared_libs: [],
1732 },
1733 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001734}
1735`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001736 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001737 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001738 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001739 }),
Alixe06d75b2022-08-31 18:28:19 +00001740 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001741 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001742 })
1743}
1744
1745func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001746 runCcLibraryTestCase(t, Bp2buildTestCase{
1747 Description: "cc_library system_shared_libs empty for shared variant",
1748 ModuleTypeUnderTest: "cc_library",
1749 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1750 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001751cc_library {
1752 name: "shared_empty",
1753 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001754 system_shared_libs: [],
1755 },
1756 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001757}
1758`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001759 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001760 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1761 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001762 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001763 }),
1764 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001765 })
1766}
1767
1768func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001769 runCcLibraryTestCase(t, Bp2buildTestCase{
1770 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1771 ModuleTypeUnderTest: "cc_library",
1772 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1773 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001774cc_library {
1775 name: "shared_empty",
1776 target: {
1777 bionic: {
1778 shared: {
1779 system_shared_libs: [],
1780 }
1781 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001782 },
1783 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001784}
1785`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001786 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001787 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1788 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001789 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001790 }),
1791 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001792 })
1793}
1794
1795func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1796 // Note that this behavior is technically incorrect (it's a simplification).
1797 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1798 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1799 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001800 runCcLibraryTestCase(t, Bp2buildTestCase{
1801 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1802 ModuleTypeUnderTest: "cc_library",
1803 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1804 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001805cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001806 name: "libc_musl",
1807 bazel_module: { bp2build_available: false },
1808}
1809
1810cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001811 name: "target_linux_bionic_empty",
1812 target: {
1813 linux_bionic: {
1814 system_shared_libs: [],
1815 },
1816 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001817 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001818}
1819`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001820 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001821 "system_dynamic_deps": `select({
1822 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1823 "//conditions:default": [],
1824 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001825 }),
1826 },
1827 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001828}
1829
1830func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001831 runCcLibraryTestCase(t, Bp2buildTestCase{
1832 Description: "cc_library system_shared_libs empty for bionic variant",
1833 ModuleTypeUnderTest: "cc_library",
1834 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1835 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001836cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001837 name: "libc_musl",
1838 bazel_module: { bp2build_available: false },
1839}
1840
1841cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001842 name: "target_bionic_empty",
1843 target: {
1844 bionic: {
1845 system_shared_libs: [],
1846 },
1847 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001848 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001849}
1850`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001851 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001852 "system_dynamic_deps": `select({
1853 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1854 "//conditions:default": [],
1855 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001856 }),
1857 },
1858 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001859}
1860
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001861func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1862 runCcLibraryTestCase(t, Bp2buildTestCase{
1863 Description: "cc_library system_shared_lib empty for musl variant",
1864 ModuleTypeUnderTest: "cc_library",
1865 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1866 Blueprint: soongCcLibraryPreamble + `
1867cc_library {
1868 name: "libc_musl",
1869 bazel_module: { bp2build_available: false },
1870}
1871
1872cc_library {
1873 name: "target_musl_empty",
1874 target: {
1875 musl: {
1876 system_shared_libs: [],
1877 },
1878 },
1879 include_build_directory: false,
1880}
1881`,
1882 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1883 "system_dynamic_deps": `[]`,
1884 }),
1885 })
1886}
1887
1888func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1889 runCcLibraryTestCase(t, Bp2buildTestCase{
1890 Description: "cc_library system_shared_lib empty for linux_musl variant",
1891 ModuleTypeUnderTest: "cc_library",
1892 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1893 Blueprint: soongCcLibraryPreamble + `
1894cc_library {
1895 name: "libc_musl",
1896 bazel_module: { bp2build_available: false },
1897}
1898
1899cc_library {
1900 name: "target_linux_musl_empty",
1901 target: {
1902 linux_musl: {
1903 system_shared_libs: [],
1904 },
1905 },
1906 include_build_directory: false,
1907}
1908`,
1909 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1910 "system_dynamic_deps": `[]`,
1911 }),
1912 })
1913}
Chris Parsons51f8c392021-08-03 21:01:05 -04001914func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001915 runCcLibraryTestCase(t, Bp2buildTestCase{
1916 Description: "cc_library system_shared_libs set for shared and root",
1917 ModuleTypeUnderTest: "cc_library",
1918 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1919 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001920cc_library {
1921 name: "libc",
1922 bazel_module: { bp2build_available: false },
1923}
1924cc_library {
1925 name: "libm",
1926 bazel_module: { bp2build_available: false },
1927}
Chris Parsons51f8c392021-08-03 21:01:05 -04001928
1929cc_library {
1930 name: "foo",
1931 system_shared_libs: ["libc"],
1932 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001933 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001934 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001935 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001936}
1937`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001938 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001939 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001940 "system_dynamic_deps": `[":libc"]`,
1941 }),
Alixe06d75b2022-08-31 18:28:19 +00001942 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001943 "system_dynamic_deps": `[
1944 ":libc",
1945 ":libm",
1946 ]`,
1947 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001948 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001949 })
1950}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001951
1952func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001953 runCcLibraryTestCase(t, Bp2buildTestCase{
1954 Description: "cc_library - selects for all os targets",
1955 ModuleTypeUnderTest: "cc_library",
1956 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1957 Filesystem: map[string]string{},
1958 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001959cc_library {
1960 name: "foo-lib",
1961 srcs: ["base.cpp"],
1962 target: {
1963 android: {
1964 srcs: ["android.cpp"],
1965 },
1966 linux: {
1967 srcs: ["linux.cpp"],
1968 },
1969 linux_glibc: {
1970 srcs: ["linux_glibc.cpp"],
1971 },
1972 darwin: {
1973 srcs: ["darwin.cpp"],
1974 },
1975 bionic: {
1976 srcs: ["bionic.cpp"],
1977 },
1978 linux_musl: {
1979 srcs: ["linux_musl.cpp"],
1980 },
1981 windows: {
1982 srcs: ["windows.cpp"],
1983 },
1984 },
1985 include_build_directory: false,
1986}
1987`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001988 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001989 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001990 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001991 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001992 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001993 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001994 ],
1995 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001996 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001997 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001998 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001999 ],
Colin Cross133782e2022-12-20 15:29:31 -08002000 "//build/bazel/platforms/os:linux_glibc": [
2001 "linux.cpp",
2002 "linux_glibc.cpp",
2003 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002004 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002005 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002006 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002007 ],
2008 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2009 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002010 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002011 }),
2012 },
2013 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002014}
2015
Yu Liu75be7b92022-02-01 09:54:09 -08002016func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002017 runCcLibraryTestCase(t, Bp2buildTestCase{
2018 Description: "cc_library - libcrypto hash injection",
2019 ModuleTypeUnderTest: "cc_library",
2020 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2021 Filesystem: map[string]string{},
2022 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002023cc_library {
2024 name: "libcrypto",
2025 target: {
2026 android: {
2027 inject_bssl_hash: true,
2028 },
2029 },
2030 include_build_directory: false,
2031}
2032`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002033 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002034 "inject_bssl_hash": `select({
2035 "//build/bazel/platforms/os:android": True,
2036 "//conditions:default": None,
2037 })`,
2038 }),
2039 },
2040 )
2041}
2042
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002043func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002044 type testCase struct {
2045 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002046 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002047 gnu_extensions string
2048 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002049 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002050 }
2051
2052 testCases := []testCase{
2053 // Existing usages of cpp_std in AOSP are:
2054 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2055 //
2056 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2057 // is set in the toolchain.)
2058 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002059 {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 +00002060 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2061 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002062 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2063 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2064 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002065 // Explicitly setting a c++ std does not use replace gnu++ std even if
2066 // gnu_extensions is true.
2067 // "c++11",
2068 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002069 {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 +00002070 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2071 // "c++17",
2072 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002073 {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 +00002074 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2075 // "c++2a",
2076 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002077 {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 +00002078 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2079 // "c++98",
2080 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002081 {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 +00002082 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2083 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2084 // "gnu++11",
2085 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002086 {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 +00002087 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2088 // "gnu++17",
2089 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002090 {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 +00002091 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002092
2093 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002094 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2095 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2096 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002097 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2098 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2099 {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 +00002100 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002101 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002102 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2103 t.Run(name, func(t *testing.T) {
2104 name_prefix := fmt.Sprintf("a_%v", i)
2105 cppStdProp := ""
2106 if tc.cpp_std != "" {
2107 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2108 }
2109 cStdProp := ""
2110 if tc.c_std != "" {
2111 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2112 }
2113 gnuExtensionsProp := ""
2114 if tc.gnu_extensions != "" {
2115 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2116 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002117 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002118 if tc.bazel_cpp_std != "" {
2119 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2120 }
2121 if tc.bazel_c_std != "" {
2122 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2123 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002124
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002125 runCcLibraryTestCase(t, Bp2buildTestCase{
2126 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002127 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002128 ModuleTypeUnderTest: "cc_library",
2129 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2130 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002131cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002132 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002133%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002134%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002135%s // gnu_extensions: *bool
2136 include_build_directory: false,
2137}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002138`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002139 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002140 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002141
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002142 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2143 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002144 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002145 ModuleTypeUnderTest: "cc_library_static",
2146 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2147 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002148cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002149 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002150%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002151%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002152%s // gnu_extensions: *bool
2153 include_build_directory: false,
2154}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002155`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002156 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002157 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002158 },
2159 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002160
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002161 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2162 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002163 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002164 ModuleTypeUnderTest: "cc_library_shared",
2165 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2166 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002167cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002168 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002169%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002170%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002171%s // gnu_extensions: *bool
2172 include_build_directory: false,
2173}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002174`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002175 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002176 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002177 },
2178 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002179 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002180 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002181}
Liz Kammer12615db2021-09-28 09:19:17 -04002182
2183func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002184 runCcLibraryTestCase(t, Bp2buildTestCase{
2185 ModuleTypeUnderTest: "cc_library",
2186 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2187 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002188 name: "foo",
2189 srcs: ["foo.proto"],
2190 include_build_directory: false,
2191}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002192 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002193 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002194 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002195 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002196 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002197 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002198 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002199 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002200 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002201 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2202 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002203 }),
2204 },
2205 })
2206}
2207
2208func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002209 runCcLibraryTestCase(t, Bp2buildTestCase{
2210 ModuleTypeUnderTest: "cc_library",
2211 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2212 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002213 name: "foo",
2214 srcs: ["foo.proto"],
2215 proto: { canonical_path_from_root: false},
2216 include_build_directory: false,
2217}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002218 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002219 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002220 "srcs": `["foo.proto"]`,
2221 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002222 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002223 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002224 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002225 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002226 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002227 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002228 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2229 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002230 }),
2231 },
2232 })
2233}
2234
2235func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002236 runCcLibraryTestCase(t, Bp2buildTestCase{
2237 ModuleTypeUnderTest: "cc_library",
2238 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2239 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002240 name: "foo",
2241 srcs: ["foo.proto"],
2242 proto: { canonical_path_from_root: true},
2243 include_build_directory: false,
2244}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002245 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002246 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002247 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002248 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002249 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002250 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002251 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002252 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002253 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002254 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2255 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002256 }),
2257 },
2258 })
2259}
2260
2261func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002262 runCcLibraryTestCase(t, Bp2buildTestCase{
2263 ModuleTypeUnderTest: "cc_library",
2264 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2265 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002266 name: "foo",
2267 srcs: ["foo.proto"],
2268 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002269 type: "full",
2270 },
2271 include_build_directory: false,
2272}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002273 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002274 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002275 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002276 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002277 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002278 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002279 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002280 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002281 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002282 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2283 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002284 }),
2285 },
2286 })
2287}
2288
2289func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002290 runCcLibraryTestCase(t, Bp2buildTestCase{
2291 ModuleTypeUnderTest: "cc_library",
2292 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2293 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002294 name: "foo",
2295 srcs: ["foo.proto"],
2296 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002297 type: "lite",
2298 },
2299 include_build_directory: false,
2300}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002301 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002302 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002303 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002304 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002305 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002306 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002307 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002308 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002309 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002310 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2311 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002312 }),
2313 },
2314 })
2315}
2316
2317func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002318 runCcLibraryTestCase(t, Bp2buildTestCase{
2319 ModuleTypeUnderTest: "cc_library",
2320 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2321 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002322 name: "foo",
2323 srcs: ["foo.proto"],
2324 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002325 export_proto_headers: true,
2326 },
2327 include_build_directory: false,
2328}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002329 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002330 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002331 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002332 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002333 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002334 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002335 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002336 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002337 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002338 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2339 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002340 }),
2341 },
2342 })
2343}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002344
Yu Liu2d136142022-08-18 14:46:13 -07002345func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2346 runCcLibraryTestCase(t, Bp2buildTestCase{
2347 ModuleTypeUnderTest: "cc_library",
2348 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2349 Blueprint: soongCcProtoPreamble + `cc_library {
2350 name: "foo",
2351 srcs: ["foo.proto"],
2352 proto: {
2353 include_dirs: ["external/protobuf/src"],
2354 },
2355 include_build_directory: false,
2356}`,
2357 ExpectedBazelTargets: []string{
2358 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2359 "srcs": `["foo.proto"]`,
2360 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2361 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2362 "deps": `[":foo_proto"]`,
2363 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2364 "deps": `[":libprotobuf-cpp-lite"]`,
2365 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2366 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002367 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2368 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002369 }),
2370 },
2371 })
2372}
2373
2374func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2375 runCcLibraryTestCase(t, Bp2buildTestCase{
2376 ModuleTypeUnderTest: "cc_library",
2377 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2378 Blueprint: soongCcProtoPreamble + `cc_library {
2379 name: "foo",
2380 srcs: ["foo.proto"],
2381 proto: {
2382 include_dirs: ["external/protobuf/abc"],
2383 },
2384 include_build_directory: false,
2385}`,
2386 ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"),
2387 })
2388}
2389
Yu Liu2aa806b2022-09-01 11:54:47 -07002390func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2391 runCcLibraryTestCase(t, Bp2buildTestCase{
2392 ModuleTypeUnderTest: "cc_library",
2393 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2394 Blueprint: soongCcProtoPreamble + `
2395filegroup {
2396 name: "a_fg_proto",
2397 srcs: ["a_fg.proto"],
2398}
2399
2400cc_library {
2401 name: "a",
2402 srcs: [
2403 ":a_fg_proto",
2404 "a.proto",
2405 ],
2406 proto: {
2407 export_proto_headers: true,
2408 },
2409 include_build_directory: false,
2410}`,
2411 ExpectedBazelTargets: []string{
2412 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002413 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002414 "srcs": `["a.proto"]`,
2415 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2416 "deps": `[
2417 ":a_fg_proto_bp2build_converted",
2418 ":a_proto",
2419 ]`,
2420 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2421 "deps": `[":libprotobuf-cpp-lite"]`,
2422 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2423 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2424 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2425 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2426 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2427 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002428 "tags": `[
2429 "apex_available=//apex_available:anyapex",
2430 "manual",
2431 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002432 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2433 "srcs": `["a_fg.proto"]`,
2434 }),
2435 },
2436 })
2437}
2438
2439func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2440 runCcLibraryTestCase(t, Bp2buildTestCase{
2441 ModuleTypeUnderTest: "cc_library",
2442 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2443 Blueprint: soongCcProtoPreamble + `
2444filegroup {
2445 name: "a_fg_proto",
2446 srcs: ["a_fg.proto"],
2447}
2448
2449cc_library {
2450 name: "a",
2451 srcs: [
2452 ":a_fg_proto",
2453 ],
2454 proto: {
2455 export_proto_headers: true,
2456 },
2457 include_build_directory: false,
2458}`,
2459 ExpectedBazelTargets: []string{
2460 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2461 "deps": `[":a_fg_proto_bp2build_converted"]`,
2462 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2463 "deps": `[":libprotobuf-cpp-lite"]`,
2464 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2465 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2466 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2467 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2468 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2469 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002470 "tags": `[
2471 "apex_available=//apex_available:anyapex",
2472 "manual",
2473 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002474 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2475 "srcs": `["a_fg.proto"]`,
2476 }),
2477 },
2478 })
2479}
2480
2481func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2482 runCcLibraryTestCase(t, Bp2buildTestCase{
2483 ModuleTypeUnderTest: "cc_library",
2484 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2485 Filesystem: map[string]string{
2486 "path/to/A/Android.bp": `
2487filegroup {
2488 name: "a_fg_proto",
2489 srcs: ["a_fg.proto"],
2490}`,
2491 },
2492 Blueprint: soongCcProtoPreamble + `
2493cc_library {
2494 name: "a",
2495 srcs: [
2496 ":a_fg_proto",
2497 "a.proto",
2498 ],
2499 proto: {
2500 export_proto_headers: true,
2501 },
2502 include_build_directory: false,
2503}`,
2504 ExpectedBazelTargets: []string{
2505 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002506 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002507 "srcs": `["a.proto"]`,
2508 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2509 "deps": `[
2510 "//path/to/A:a_fg_proto_bp2build_converted",
2511 ":a_proto",
2512 ]`,
2513 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2514 "deps": `[":libprotobuf-cpp-lite"]`,
2515 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2516 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2517 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2518 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2519 }),
2520 },
2521 })
2522}
2523
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002524func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002525 runCcLibraryTestCase(t, Bp2buildTestCase{
2526 ModuleTypeUnderTest: "cc_library",
2527 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2528 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002529 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2530 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2531 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2532 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2533cc_library {
2534 name: "a",
2535 srcs: [":a_fg_proto"],
2536 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002537 export_proto_headers: true,
2538 },
2539 include_build_directory: false,
2540}
2541
2542cc_library {
2543 name: "b",
2544 srcs: [":b_protos"],
2545 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002546 export_proto_headers: true,
2547 },
2548 include_build_directory: false,
2549}
2550
2551cc_library {
2552 name: "c",
2553 srcs: [":c-proto-srcs"],
2554 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002555 export_proto_headers: true,
2556 },
2557 include_build_directory: false,
2558}
2559
2560cc_library {
2561 name: "d",
2562 srcs: [":proto-srcs-d"],
2563 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002564 export_proto_headers: true,
2565 },
2566 include_build_directory: false,
2567}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002568 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002569 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002570 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002571 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002572 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002573 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002574 "deps": `[":libprotobuf-cpp-lite"]`,
2575 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2576 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2577 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2578 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002579 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002580 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2581 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2582 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2583 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2584 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002585 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002586 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002587 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002588 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002589 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002590 "deps": `[":libprotobuf-cpp-lite"]`,
2591 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2592 "srcs": `[":b_protos_cpp_srcs"]`,
2593 "srcs_as": `[":b_protos_as_srcs"]`,
2594 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002595 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002596 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2597 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2598 "srcs": `[":b_protos_cpp_srcs"]`,
2599 "srcs_as": `[":b_protos_as_srcs"]`,
2600 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002601 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002602 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002603 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002604 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002605 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002606 "deps": `[":libprotobuf-cpp-lite"]`,
2607 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2608 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2609 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2610 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002611 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002612 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2613 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2614 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2615 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2616 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002617 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002618 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002619 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002620 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002621 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002622 "deps": `[":libprotobuf-cpp-lite"]`,
2623 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2624 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2625 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2626 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002627 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002628 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2629 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2630 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2631 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2632 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2633 }),
2634 },
2635 })
2636}
Chris Parsons58852a02021-12-09 18:10:18 -05002637
2638func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002639 runCcLibraryTestCase(t, Bp2buildTestCase{
2640 ModuleTypeUnderTest: "cc_library",
2641 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2642 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002643 name: "foo",
2644 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002645 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002646 target: {
2647 darwin: {
2648 enabled: false,
2649 },
2650 windows: {
2651 enabled: false,
2652 },
2653 linux_glibc_x86: {
2654 enabled: false,
2655 },
2656 },
2657 include_build_directory: false,
2658}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002659 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002660 "srcs": `["foo.cpp"]`,
2661 "target_compatible_with": `select({
2662 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2663 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2664 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2665 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2666 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2667 "//conditions:default": [],
2668 })`,
2669 }),
2670 })
2671}
2672
2673func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002674 runCcLibraryTestCase(t, Bp2buildTestCase{
2675 ModuleTypeUnderTest: "cc_library",
2676 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2677 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002678 name: "foo",
2679 srcs: ["foo.cpp"],
2680 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002681 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002682 target: {
2683 darwin: {
2684 enabled: true,
2685 },
2686 windows: {
2687 enabled: false,
2688 },
2689 linux_glibc_x86: {
2690 enabled: false,
2691 },
2692 },
2693 include_build_directory: false,
2694}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002695 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002696 "srcs": `["foo.cpp"]`,
2697 "target_compatible_with": `select({
2698 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2699 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2700 "//conditions:default": ["@platforms//:incompatible"],
2701 })`,
2702 }),
2703 })
2704}
2705
2706func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002707 runCcLibraryTestCase(t, Bp2buildTestCase{
2708 ModuleTypeUnderTest: "cc_library",
2709 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2710 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002711 name: "foo",
2712 srcs: ["foo.cpp"],
2713 enabled: false,
2714 shared: {
2715 enabled: true,
2716 },
2717 target: {
2718 android: {
2719 shared: {
2720 enabled: false,
2721 },
2722 }
2723 },
2724 include_build_directory: false,
2725}`,
Alixe06d75b2022-08-31 18:28:19 +00002726 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002727 "srcs": `["foo.cpp"]`,
2728 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002729 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002730 "srcs": `["foo.cpp"]`,
2731 "target_compatible_with": `select({
2732 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2733 "//conditions:default": [],
2734 })`,
2735 }),
2736 },
2737 })
2738}
2739
2740func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002741 runCcLibraryTestCase(t, Bp2buildTestCase{
2742 ModuleTypeUnderTest: "cc_library",
2743 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2744 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002745 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002746 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002747 srcs: ["foo.cpp"],
2748 shared: {
2749 enabled: false
2750 },
2751 target: {
2752 darwin: {
2753 enabled: true,
2754 },
2755 windows: {
2756 enabled: false,
2757 },
2758 linux_glibc_x86: {
2759 shared: {
2760 enabled: true,
2761 },
2762 },
2763 },
2764 include_build_directory: false,
2765}`,
Alixe06d75b2022-08-31 18:28:19 +00002766 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002767 "srcs": `["foo.cpp"]`,
2768 "target_compatible_with": `select({
2769 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2770 "//conditions:default": [],
2771 })`,
Alixe06d75b2022-08-31 18:28:19 +00002772 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002773 "srcs": `["foo.cpp"]`,
2774 "target_compatible_with": `select({
2775 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2776 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2777 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2778 "//conditions:default": ["@platforms//:incompatible"],
2779 })`,
2780 }),
2781 }})
2782}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002783
2784func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002785 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002786 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002787 })
2788 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Sam Delmerico5f906492023-03-15 18:06:18 -04002789 "soname": `"a.so"`,
2790 "source_library_label": `"//foo/bar:a"`,
2791 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002792 "stubs_versions": `[
2793 "28",
2794 "29",
2795 "current",
2796 ]`,
2797 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002798 runCcLibraryTestCase(t, Bp2buildTestCase{
2799 Description: "cc_library stubs",
2800 ModuleTypeUnderTest: "cc_library",
2801 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2802 Dir: "foo/bar",
2803 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002804 "foo/bar/Android.bp": `
2805cc_library {
2806 name: "a",
2807 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2808 bazel_module: { bp2build_available: true },
2809 include_build_directory: false,
2810}
2811`,
2812 },
Wei Li81852ca2022-07-27 00:22:06 -07002813 Blueprint: soongCcLibraryPreamble,
2814 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002815 },
2816 )
2817}
Liz Kammerf38a8372022-02-04 15:39:00 -05002818
Spandan Das4238c652022-09-09 01:38:47 +00002819func TestCcApiContributionsWithHdrs(t *testing.T) {
2820 bp := `
2821 cc_library {
2822 name: "libfoo",
2823 stubs: { symbol_file: "libfoo.map.txt", versions: ["28", "29", "current"] },
2824 llndk: { symbol_file: "libfoo.map.txt", override_export_include_dirs: ["dir2"]},
2825 export_include_dirs: ["dir1"],
2826 }
2827 `
2828 expectedBazelTargets := []string{
2829 MakeBazelTarget(
2830 "cc_api_library_headers",
Spandan Das627fc3e2023-01-26 23:02:00 +00002831 "libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002832 AttrNameToString{
2833 "export_includes": `["dir1"]`,
2834 }),
2835 MakeBazelTarget(
2836 "cc_api_library_headers",
2837 "libfoo.vendorapi.headers",
2838 AttrNameToString{
2839 "export_includes": `["dir2"]`,
2840 }),
2841 MakeBazelTarget(
2842 "cc_api_contribution",
2843 "libfoo.contribution",
2844 AttrNameToString{
2845 "api": `"libfoo.map.txt"`,
2846 "library_name": `"libfoo"`,
2847 "api_surfaces": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002848 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002849 "vendorapi",
2850 ]`,
2851 "hdrs": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002852 ":libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002853 ":libfoo.vendorapi.headers",
2854 ]`,
2855 }),
2856 }
2857 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2858 Blueprint: bp,
Spandan Das627fc3e2023-01-26 23:02:00 +00002859 Description: "cc API contributions to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002860 ExpectedBazelTargets: expectedBazelTargets,
2861 })
2862}
2863
2864func TestCcApiSurfaceCombinations(t *testing.T) {
2865 testCases := []struct {
2866 bp string
2867 expectedApi string
2868 expectedApiSurfaces string
2869 description string
2870 }{
2871 {
2872 bp: `
2873 cc_library {
2874 name: "a",
2875 stubs: {symbol_file: "a.map.txt"},
2876 }`,
2877 expectedApi: `"a.map.txt"`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002878 expectedApiSurfaces: `["module-libapi"]`,
2879 description: "Library that contributes to module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002880 },
2881 {
2882 bp: `
2883 cc_library {
2884 name: "a",
2885 llndk: {symbol_file: "a.map.txt"},
2886 }`,
2887 expectedApi: `"a.map.txt"`,
2888 expectedApiSurfaces: `["vendorapi"]`,
2889 description: "Library that contributes to vendorapi",
2890 },
2891 {
2892 bp: `
2893 cc_library {
2894 name: "a",
2895 llndk: {symbol_file: "a.map.txt"},
2896 stubs: {symbol_file: "a.map.txt"},
2897 }`,
2898 expectedApi: `"a.map.txt"`,
2899 expectedApiSurfaces: `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002900 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002901 "vendorapi",
2902 ]`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002903 description: "Library that contributes to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002904 },
2905 }
2906 for _, testCase := range testCases {
2907 expectedBazelTargets := []string{
2908 MakeBazelTarget(
2909 "cc_api_contribution",
2910 "a.contribution",
2911 AttrNameToString{
2912 "library_name": `"a"`,
2913 "hdrs": `[]`,
2914 "api": testCase.expectedApi,
2915 "api_surfaces": testCase.expectedApiSurfaces,
2916 },
2917 ),
2918 }
2919 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2920 Blueprint: testCase.bp,
2921 Description: testCase.description,
2922 ExpectedBazelTargets: expectedBazelTargets,
2923 })
2924 }
2925}
2926
2927// llndk struct property in Soong provides users with several options to configure the exported include dirs
2928// Test the generated bazel targets for the different configurations
2929func TestCcVendorApiHeaders(t *testing.T) {
2930 testCases := []struct {
2931 bp string
2932 expectedIncludes string
2933 expectedSystemIncludes string
2934 description string
2935 }{
2936 {
2937 bp: `
2938 cc_library {
2939 name: "a",
2940 export_include_dirs: ["include"],
2941 export_system_include_dirs: ["base_system_include"],
2942 llndk: {
2943 symbol_file: "a.map.txt",
2944 export_headers_as_system: true,
2945 },
2946 }
2947 `,
2948 expectedIncludes: "",
2949 expectedSystemIncludes: `[
2950 "base_system_include",
2951 "include",
2952 ]`,
2953 description: "Headers are exported as system to API surface",
2954 },
2955 {
2956 bp: `
2957 cc_library {
2958 name: "a",
2959 export_include_dirs: ["include"],
2960 export_system_include_dirs: ["base_system_include"],
2961 llndk: {
2962 symbol_file: "a.map.txt",
2963 override_export_include_dirs: ["llndk_include"],
2964 },
2965 }
2966 `,
2967 expectedIncludes: `["llndk_include"]`,
2968 expectedSystemIncludes: `["base_system_include"]`,
2969 description: "Non-system Headers are ovverriden before export to API surface",
2970 },
2971 {
2972 bp: `
2973 cc_library {
2974 name: "a",
2975 export_include_dirs: ["include"],
2976 export_system_include_dirs: ["base_system_include"],
2977 llndk: {
2978 symbol_file: "a.map.txt",
2979 override_export_include_dirs: ["llndk_include"],
2980 export_headers_as_system: true,
2981 },
2982 }
2983 `,
2984 expectedIncludes: "", // includes are set to nil
2985 expectedSystemIncludes: `[
2986 "base_system_include",
2987 "llndk_include",
2988 ]`,
2989 description: "System Headers are extended before export to API surface",
2990 },
2991 }
2992 for _, testCase := range testCases {
2993 attrs := AttrNameToString{}
2994 if testCase.expectedIncludes != "" {
2995 attrs["export_includes"] = testCase.expectedIncludes
2996 }
2997 if testCase.expectedSystemIncludes != "" {
2998 attrs["export_system_includes"] = testCase.expectedSystemIncludes
2999 }
3000
3001 expectedBazelTargets := []string{
3002 MakeBazelTarget("cc_api_library_headers", "a.vendorapi.headers", attrs),
3003 // Create a target for cc_api_contribution target
3004 MakeBazelTarget("cc_api_contribution", "a.contribution", AttrNameToString{
3005 "api": `"a.map.txt"`,
3006 "api_surfaces": `["vendorapi"]`,
3007 "hdrs": `[":a.vendorapi.headers"]`,
3008 "library_name": `"a"`,
3009 }),
3010 }
3011 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
3012 Blueprint: testCase.bp,
3013 ExpectedBazelTargets: expectedBazelTargets,
3014 })
3015 }
3016}
3017
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003018func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
3019 runCcLibraryTestCase(t, Bp2buildTestCase{
3020 Description: "stub target generation of the same lib across configs should not result in duplicates",
3021 ModuleTypeUnderTest: "cc_library",
3022 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3023 Filesystem: map[string]string{
3024 "bar.map.txt": "",
3025 },
3026 Blueprint: `
3027cc_library {
3028 name: "barlib",
3029 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3030 bazel_module: { bp2build_available: false },
3031}
3032cc_library {
3033 name: "foolib",
3034 shared_libs: ["barlib"],
3035 target: {
3036 android: {
3037 shared_libs: ["barlib"],
3038 },
3039 },
3040 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003041 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003042}`,
3043 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3044 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00003045 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
3046 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003047 "//conditions:default": [":barlib"],
3048 })`,
3049 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003050 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003051 }),
3052 })
3053}
3054
Liz Kammerffc17e42022-11-23 09:42:05 -05003055func TestCcLibraryExcludesLibsHost(t *testing.T) {
3056 runCcLibraryTestCase(t, Bp2buildTestCase{
3057 ModuleTypeUnderTest: "cc_library",
3058 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3059 Filesystem: map[string]string{
3060 "bar.map.txt": "",
3061 },
3062 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
3063cc_library {
3064 name: "quxlib",
3065 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
3066 bazel_module: { bp2build_available: false },
3067}
3068cc_library {
3069 name: "barlib",
3070 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3071 bazel_module: { bp2build_available: false },
3072}
3073cc_library {
3074 name: "foolib",
3075 shared_libs: ["barlib", "quxlib"],
3076 target: {
3077 host: {
3078 shared_libs: ["bazlib"],
3079 exclude_shared_libs: ["barlib"],
3080 },
3081 },
3082 include_build_directory: false,
3083 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003084 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003085}`,
3086 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3087 "implementation_dynamic_deps": `select({
3088 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003089 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003090 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003091 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
3092 "//build/bazel/platforms/os:windows": [":bazlib"],
3093 "//conditions:default": [],
3094 }) + select({
3095 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003096 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003097 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003098 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
3099 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00003100 "//build/bazel/rules/apex:foo": [
3101 "@api_surfaces//module-libapi/current:barlib",
3102 "@api_surfaces//module-libapi/current:quxlib",
3103 ],
3104 "//build/bazel/rules/apex:system": [
Spandan Das2518c022023-03-17 03:02:32 +00003105 "@api_surfaces//module-libapi/current:barlib",
3106 "@api_surfaces//module-libapi/current:quxlib",
Liz Kammerffc17e42022-11-23 09:42:05 -05003107 ],
3108 "//conditions:default": [
3109 ":barlib",
3110 ":quxlib",
3111 ],
3112 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003113 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05003114 }),
3115 })
3116}
3117
Liz Kammerf38a8372022-02-04 15:39:00 -05003118func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003119 runCcLibraryTestCase(t, Bp2buildTestCase{
3120 ModuleTypeUnderTest: "cc_library",
3121 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3122 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05003123 name: "foo",
3124 ldflags: ["-Wl,--rpath,${ORIGIN}"],
3125 include_build_directory: false,
3126}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003127 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05003128 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
3129 }),
3130 })
3131}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003132
3133func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003134 runCcLibraryTestCase(t, Bp2buildTestCase{
3135 ModuleTypeUnderTest: "cc_library",
3136 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3137 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003138 "foo.c": "",
3139 "bar.cc": "",
3140 "foo1.l": "",
3141 "bar1.ll": "",
3142 "foo2.l": "",
3143 "bar2.ll": "",
3144 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003145 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003146 name: "foo_lib",
3147 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
3148 lex: { flags: ["--foo_flags"] },
3149 include_build_directory: false,
3150 bazel_module: { bp2build_available: true },
3151}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003152 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00003153 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003154 "srcs": `[
3155 "foo1.l",
3156 "foo2.l",
3157 ]`,
3158 "lexopts": `["--foo_flags"]`,
3159 }),
Alixe06d75b2022-08-31 18:28:19 +00003160 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003161 "srcs": `[
3162 "bar1.ll",
3163 "bar2.ll",
3164 ]`,
3165 "lexopts": `["--foo_flags"]`,
3166 }),
3167 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003168 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003169 "srcs": `[
3170 "bar.cc",
3171 ":foo_lib_genlex_ll",
3172 ]`,
3173 "srcs_c": `[
3174 "foo.c",
3175 ":foo_lib_genlex_l",
3176 ]`,
3177 })...),
3178 })
3179}
Cole Faust6b29f592022-08-09 09:50:56 -07003180
3181func TestCCLibraryRuntimeDeps(t *testing.T) {
3182 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3183 Blueprint: `cc_library_shared {
3184 name: "bar",
3185}
3186
3187cc_library {
3188 name: "foo",
3189 runtime_libs: ["foo"],
3190}`,
3191 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003192 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003193 "local_includes": `["."]`,
3194 }),
Alixe06d75b2022-08-31 18:28:19 +00003195 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003196 "runtime_deps": `[":foo"]`,
3197 "local_includes": `["."]`,
3198 }),
Alixe06d75b2022-08-31 18:28:19 +00003199 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003200 "runtime_deps": `[":foo"]`,
3201 "local_includes": `["."]`,
3202 }),
3203 },
3204 })
3205}
Cole Faust5fa4e962022-08-22 14:31:04 -07003206
3207func TestCcLibraryWithInstructionSet(t *testing.T) {
3208 runCcLibraryTestCase(t, Bp2buildTestCase{
3209 ModuleTypeUnderTest: "cc_library",
3210 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3211 Blueprint: `cc_library {
3212 name: "foo",
3213 arch: {
3214 arm: {
3215 instruction_set: "arm",
3216 }
3217 }
3218}
3219`,
3220 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3221 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003222 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003223 "//conditions:default": [],
3224 })`,
3225 "local_includes": `["."]`,
3226 }),
3227 })
3228}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003229
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003230func TestCcLibraryEmptySuffix(t *testing.T) {
3231 runCcLibraryTestCase(t, Bp2buildTestCase{
3232 Description: "cc_library with empty suffix",
3233 ModuleTypeUnderTest: "cc_library",
3234 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3235 Filesystem: map[string]string{
3236 "foo.c": "",
3237 },
3238 Blueprint: `cc_library {
3239 name: "foo",
3240 suffix: "",
3241 srcs: ["foo.c"],
3242 include_build_directory: false,
3243}`,
3244 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003245 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 -05003246 "srcs_c": `["foo.c"]`,
3247 }),
Alixe06d75b2022-08-31 18:28:19 +00003248 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003249 "srcs_c": `["foo.c"]`,
3250 "suffix": `""`,
3251 }),
3252 },
3253 })
3254}
3255
3256func TestCcLibrarySuffix(t *testing.T) {
3257 runCcLibraryTestCase(t, Bp2buildTestCase{
3258 Description: "cc_library with suffix",
3259 ModuleTypeUnderTest: "cc_library",
3260 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3261 Filesystem: map[string]string{
3262 "foo.c": "",
3263 },
3264 Blueprint: `cc_library {
3265 name: "foo",
3266 suffix: "-suf",
3267 srcs: ["foo.c"],
3268 include_build_directory: false,
3269}`,
3270 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003271 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 -05003272 "srcs_c": `["foo.c"]`,
3273 }),
Alixe06d75b2022-08-31 18:28:19 +00003274 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003275 "srcs_c": `["foo.c"]`,
3276 "suffix": `"-suf"`,
3277 }),
3278 },
3279 })
3280}
3281
3282func TestCcLibraryArchVariantSuffix(t *testing.T) {
3283 runCcLibraryTestCase(t, Bp2buildTestCase{
3284 Description: "cc_library with arch-variant suffix",
3285 ModuleTypeUnderTest: "cc_library",
3286 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3287 Filesystem: map[string]string{
3288 "foo.c": "",
3289 },
3290 Blueprint: `cc_library {
3291 name: "foo",
3292 arch: {
3293 arm64: { suffix: "-64" },
3294 arm: { suffix: "-32" },
3295 },
3296 srcs: ["foo.c"],
3297 include_build_directory: false,
3298}`,
3299 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003300 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 -05003301 "srcs_c": `["foo.c"]`,
3302 }),
Alixe06d75b2022-08-31 18:28:19 +00003303 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003304 "srcs_c": `["foo.c"]`,
3305 "suffix": `select({
3306 "//build/bazel/platforms/arch:arm": "-32",
3307 "//build/bazel/platforms/arch:arm64": "-64",
3308 "//conditions:default": None,
3309 })`,
3310 }),
3311 },
3312 })
3313}
3314
Vinh Tran9f6796a2022-08-16 13:10:31 -04003315func TestCcLibraryWithAidlSrcs(t *testing.T) {
3316 runCcLibraryTestCase(t, Bp2buildTestCase{
3317 Description: "cc_library with aidl srcs",
3318 ModuleTypeUnderTest: "cc_library",
3319 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3320 Blueprint: `
3321filegroup {
3322 name: "A_aidl",
3323 srcs: ["aidl/A.aidl"],
3324 path: "aidl",
3325}
3326cc_library {
3327 name: "foo",
3328 srcs: [
3329 ":A_aidl",
3330 "B.aidl",
3331 ],
3332}`,
3333 ExpectedBazelTargets: []string{
3334 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3335 "srcs": `["aidl/A.aidl"]`,
3336 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003337 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003338 }),
Alixe06d75b2022-08-31 18:28:19 +00003339 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003340 "srcs": `["B.aidl"]`,
3341 }),
Alixe06d75b2022-08-31 18:28:19 +00003342 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003343 "deps": `[
3344 ":A_aidl",
3345 ":foo_aidl_library",
3346 ]`,
3347 }),
Alixe06d75b2022-08-31 18:28:19 +00003348 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003349 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3350 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003351 }),
Alixe06d75b2022-08-31 18:28:19 +00003352 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003353 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3354 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003355 }),
3356 },
3357 })
3358}
3359
3360func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3361 runCcLibraryTestCase(t, Bp2buildTestCase{
3362 Description: "cc_library with non aidl filegroup",
3363 ModuleTypeUnderTest: "cc_library",
3364 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3365 Filesystem: map[string]string{
3366 "path/to/A/Android.bp": `
3367filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003368 name: "A_aidl",
3369 srcs: ["aidl/A.aidl"],
3370 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003371}`,
3372 },
3373 Blueprint: `
3374cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003375 name: "foo",
3376 srcs: [
3377 ":A_aidl",
3378 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003379}`,
3380 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003381 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003382 "deps": `["//path/to/A:A_aidl"]`,
3383 }),
Alixe06d75b2022-08-31 18:28:19 +00003384 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003385 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3386 "local_includes": `["."]`,
3387 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003388 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003389 "local_includes": `["."]`,
3390 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003391 }),
3392 },
3393 })
3394}
3395
3396func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
3397 runCcLibraryTestCase(t, Bp2buildTestCase{
3398 Description: "cc_library with export aidl headers",
3399 ModuleTypeUnderTest: "cc_library",
3400 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3401 Blueprint: `
3402cc_library {
3403 name: "foo",
3404 srcs: [
3405 "Foo.aidl",
3406 ],
3407 aidl: {
3408 export_aidl_headers: true,
3409 }
3410}`,
3411 ExpectedBazelTargets: []string{
3412 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3413 "srcs": `["Foo.aidl"]`,
3414 }),
3415 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3416 "deps": `[":foo_aidl_library"]`,
3417 }),
3418 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003419 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3420 "local_includes": `["."]`,
3421 }),
Alixe06d75b2022-08-31 18:28:19 +00003422 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003423 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3424 "local_includes": `["."]`,
3425 }),
3426 },
3427 })
3428}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003429
3430func TestCcLibraryWithTargetApex(t *testing.T) {
3431 runCcLibraryTestCase(t, Bp2buildTestCase{
3432 Description: "cc_library with target.apex",
3433 ModuleTypeUnderTest: "cc_library",
3434 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3435 Blueprint: `
3436cc_library {
3437 name: "foo",
3438 shared_libs: ["bar", "baz"],
3439 static_libs: ["baz", "buh"],
3440 target: {
3441 apex: {
3442 exclude_shared_libs: ["bar"],
3443 exclude_static_libs: ["buh"],
3444 }
3445 }
3446}`,
3447 ExpectedBazelTargets: []string{
3448 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3449 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003450 "//build/bazel/rules/apex:in_apex": [],
3451 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003452 })`,
3453 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003454 "//build/bazel/rules/apex:in_apex": [],
3455 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003456 })`,
3457 "local_includes": `["."]`,
3458 }),
3459 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3460 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003461 "//build/bazel/rules/apex:in_apex": [],
3462 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003463 })`,
3464 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003465 "//build/bazel/rules/apex:in_apex": [],
3466 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003467 })`,
3468 "local_includes": `["."]`,
3469 }),
3470 },
3471 })
3472}
3473
3474func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3475 runCcLibraryTestCase(t, Bp2buildTestCase{
3476 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3477 ModuleTypeUnderTest: "cc_library",
3478 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3479 Blueprint: `
3480cc_library_static {
3481 name: "foo",
3482 shared_libs: ["bar", "baz"],
3483 static_libs: ["abc"],
3484 export_shared_lib_headers: ["baz"],
3485 export_static_lib_headers: ["abc"],
3486 target: {
3487 apex: {
3488 exclude_shared_libs: ["baz", "bar"],
3489 exclude_static_libs: ["abc"],
3490 }
3491 }
3492}`,
3493 ExpectedBazelTargets: []string{
3494 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3495 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003496 "//build/bazel/rules/apex:in_apex": [],
3497 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003498 })`,
3499 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003500 "//build/bazel/rules/apex:in_apex": [],
3501 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003502 })`,
3503 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003504 "//build/bazel/rules/apex:in_apex": [],
3505 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003506 })`,
3507 "local_includes": `["."]`,
3508 }),
3509 },
3510 })
3511}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003512
3513func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3514 runCcLibraryTestCase(t, Bp2buildTestCase{
3515 Description: "cc_library with sysprop sources",
3516 ModuleTypeUnderTest: "cc_library",
3517 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3518 Blueprint: `
3519cc_library {
3520 name: "foo",
3521 srcs: [
3522 "bar.sysprop",
3523 "baz.sysprop",
3524 "blah.cpp",
3525 ],
3526 min_sdk_version: "5",
3527}`,
3528 ExpectedBazelTargets: []string{
3529 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3530 "srcs": `[
3531 "bar.sysprop",
3532 "baz.sysprop",
3533 ]`,
3534 }),
3535 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3536 "dep": `":foo_sysprop_library"`,
3537 "min_sdk_version": `"5"`,
3538 }),
3539 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3540 "srcs": `["blah.cpp"]`,
3541 "local_includes": `["."]`,
3542 "min_sdk_version": `"5"`,
3543 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3544 }),
3545 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3546 "srcs": `["blah.cpp"]`,
3547 "local_includes": `["."]`,
3548 "min_sdk_version": `"5"`,
3549 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3550 }),
3551 },
3552 })
3553}
3554
3555func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3556 runCcLibraryTestCase(t, Bp2buildTestCase{
3557 Description: "cc_library with sysprop sources in some configs but not others",
3558 ModuleTypeUnderTest: "cc_library",
3559 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3560 Blueprint: `
3561cc_library {
3562 name: "foo",
3563 host_supported: true,
3564 srcs: [
3565 "blah.cpp",
3566 ],
3567 target: {
3568 android: {
3569 srcs: ["bar.sysprop"],
3570 },
3571 },
3572 min_sdk_version: "5",
3573}`,
3574 ExpectedBazelTargets: []string{
3575 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3576 "srcs": `select({
3577 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3578 "//conditions:default": [],
3579 })`,
3580 }),
3581 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3582 "dep": `":foo_sysprop_library"`,
3583 "min_sdk_version": `"5"`,
3584 }),
3585 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3586 "srcs": `["blah.cpp"]`,
3587 "local_includes": `["."]`,
3588 "min_sdk_version": `"5"`,
3589 "whole_archive_deps": `select({
3590 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3591 "//conditions:default": [],
3592 })`,
3593 }),
3594 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3595 "srcs": `["blah.cpp"]`,
3596 "local_includes": `["."]`,
3597 "min_sdk_version": `"5"`,
3598 "whole_archive_deps": `select({
3599 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3600 "//conditions:default": [],
3601 })`,
3602 }),
3603 },
3604 })
3605}
Vinh Tran395a1e92022-09-16 18:27:29 -04003606
Sam Delmerico512437b2023-03-17 11:34:15 -04003607func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003608 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003609 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003610 ModuleTypeUnderTest: "cc_library",
3611 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3612 Blueprint: `
3613cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003614 name: "foo",
3615 srcs: [
3616 "Foo.aidl",
3617 ],
3618 static_libs: [
3619 "bar-static",
3620 "baz-static",
3621 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003622 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003623 "bar-shared",
3624 "baz-shared",
3625 ],
3626 export_static_lib_headers: [
3627 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003628 ],
3629 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003630 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003631 ],
3632}` +
Sam Delmerico512437b2023-03-17 11:34:15 -04003633 simpleModuleDoNotConvertBp2build("cc_library_static", "bar-static") +
3634 simpleModuleDoNotConvertBp2build("cc_library_static", "baz-static") +
3635 simpleModuleDoNotConvertBp2build("cc_library", "bar-shared") +
3636 simpleModuleDoNotConvertBp2build("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003637 ExpectedBazelTargets: []string{
3638 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3639 "srcs": `["Foo.aidl"]`,
3640 }),
3641 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
3642 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003643 "implementation_deps": `[
3644 ":baz-static",
3645 ":bar-static",
3646 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003647 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003648 ":baz-shared",
3649 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003650 ]`,
3651 }),
3652 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3653 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003654 "deps": `[":baz-static"]`,
3655 "implementation_deps": `[":bar-static"]`,
3656 "dynamic_deps": `[":baz-shared"]`,
3657 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003658 "local_includes": `["."]`,
3659 }),
3660 },
3661 })
3662}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003663
3664func TestCcLibraryWithTidy(t *testing.T) {
3665 runCcLibraryTestCase(t, Bp2buildTestCase{
3666 Description: "cc_library uses tidy properties",
3667 ModuleTypeUnderTest: "cc_library",
3668 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3669 Blueprint: `
3670cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003671 name: "foo",
3672 srcs: ["foo.cpp"],
3673}
3674cc_library_static {
3675 name: "foo-no-tidy",
3676 srcs: ["foo.cpp"],
3677 tidy: false,
3678}
3679cc_library_static {
3680 name: "foo-tidy",
3681 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003682 tidy: true,
3683 tidy_checks: ["check1", "check2"],
3684 tidy_checks_as_errors: ["check1error", "check2error"],
3685 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003686 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003687}`,
3688 ExpectedBazelTargets: []string{
3689 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3690 "local_includes": `["."]`,
3691 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003692 }),
3693 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3694 "local_includes": `["."]`,
3695 "srcs": `["foo.cpp"]`,
3696 "tidy": `"never"`,
3697 }),
3698 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3699 "local_includes": `["."]`,
3700 "srcs": `["foo.cpp"]`,
3701 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003702 "tidy_checks": `[
3703 "check1",
3704 "check2",
3705 ]`,
3706 "tidy_checks_as_errors": `[
3707 "check1error",
3708 "check2error",
3709 ]`,
3710 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003711 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003712 }),
3713 },
3714 })
3715}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003716
Vinh Tran99270ea2022-11-28 11:15:23 -05003717func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3718 bp := `
3719cc_library {
3720 name: "foo",
3721 afdo: true,
3722 include_build_directory: false,
3723}`
3724
3725 // TODO(b/260714900): Add test case for arch-specific afdo profile
3726 testCases := []struct {
3727 description string
3728 filesystem map[string]string
3729 expectedBazelTargets []string
3730 }{
3731 {
3732 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003733 filesystem: map[string]string{
3734 "vendor/google_data/pgo_profile/sampling/BUILD": "",
3735 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3736 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003737 expectedBazelTargets: []string{
3738 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3739 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3740 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3741 }),
3742 },
3743 },
3744 {
3745 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003746 filesystem: map[string]string{
3747 "toolchain/pgo-profiles/sampling/BUILD": "",
3748 "toolchain/pgo-profiles/sampling/foo.afdo": "",
3749 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003750 expectedBazelTargets: []string{
3751 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3752 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3753 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3754 }),
3755 },
3756 },
3757 {
3758 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003759 filesystem: map[string]string{
3760 "toolchain/pgo-profiles/sampling/BUILD": "",
3761 "toolchain/pgo-profiles/sampling/bar.afdo": "",
3762 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003763 expectedBazelTargets: []string{
3764 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3765 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3766 },
3767 },
3768 {
3769 description: "cc_library with afdo enabled but profile doesn't exist",
3770 expectedBazelTargets: []string{
3771 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3772 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3773 },
3774 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003775 {
3776 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3777 filesystem: map[string]string{
3778 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3779 },
3780 expectedBazelTargets: []string{
3781 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3782 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3783 },
3784 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003785 }
3786 for _, testCase := range testCases {
3787 t.Run(testCase.description, func(t *testing.T) {
3788 runCcLibraryTestCase(t, Bp2buildTestCase{
3789 ExpectedBazelTargets: testCase.expectedBazelTargets,
3790 ModuleTypeUnderTest: "cc_library",
3791 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3792 Description: testCase.description,
3793 Blueprint: binaryReplacer.Replace(bp),
3794 Filesystem: testCase.filesystem,
3795 })
3796 })
3797 }
3798}
3799
Yu Liu56ccb1a2022-11-12 10:47:07 -08003800func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3801 runCcLibraryTestCase(t, Bp2buildTestCase{
3802 Description: "cc_library with header abi checker",
3803 ModuleTypeUnderTest: "cc_library",
3804 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3805 Blueprint: `cc_library {
3806 name: "foo",
3807 header_abi_checker: {
3808 enabled: true,
3809 symbol_file: "a.map.txt",
3810 exclude_symbol_versions: [
3811 "29",
3812 "30",
3813 ],
3814 exclude_symbol_tags: [
3815 "tag1",
3816 "tag2",
3817 ],
3818 check_all_apis: true,
3819 diff_flags: ["-allow-adding-removing-weak-symbols"],
3820 },
3821 include_build_directory: false,
3822}`,
3823 ExpectedBazelTargets: []string{
3824 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3825 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3826 "abi_checker_enabled": `True`,
3827 "abi_checker_symbol_file": `"a.map.txt"`,
3828 "abi_checker_exclude_symbol_versions": `[
3829 "29",
3830 "30",
3831 ]`,
3832 "abi_checker_exclude_symbol_tags": `[
3833 "tag1",
3834 "tag2",
3835 ]`,
3836 "abi_checker_check_all_apis": `True`,
3837 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3838 }),
3839 },
3840 })
3841}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003842
3843func TestCcLibraryApexAvailable(t *testing.T) {
3844 runCcLibraryTestCase(t, Bp2buildTestCase{
3845 Description: "cc_library apex_available converted to tags",
3846 ModuleTypeUnderTest: "cc_library",
3847 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3848 Blueprint: soongCcLibraryPreamble + `
3849cc_library {
3850 name: "a",
3851 srcs: ["a.cpp"],
3852 apex_available: ["com.android.foo"],
3853}
3854`,
3855 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3856 "tags": `["apex_available=com.android.foo"]`,
3857 "srcs": `["a.cpp"]`,
3858 "local_includes": `["."]`,
3859 }),
3860 },
3861 )
3862}
3863
3864func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3865 runCcLibraryTestCase(t, Bp2buildTestCase{
3866 Description: "cc_library apex_available converted to multiple tags",
3867 ModuleTypeUnderTest: "cc_library",
3868 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3869 Blueprint: soongCcLibraryPreamble + `
3870cc_library {
3871 name: "a",
3872 srcs: ["a.cpp"],
3873 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3874}
3875`,
3876 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3877 "tags": `[
3878 "apex_available=com.android.foo",
3879 "apex_available=//apex_available:platform",
3880 "apex_available=com.android.bar",
3881 ]`,
3882 "srcs": `["a.cpp"]`,
3883 "local_includes": `["."]`,
3884 }),
3885 },
3886 )
3887}
Zi Wang0f828442022-12-28 11:18:11 -08003888
3889// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3890// In bp2build output, variant info(select) should go before general info.
3891// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3892func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3893 runCcLibraryTestCase(t, Bp2buildTestCase{
3894 Description: "cc_library variant prepend properties order",
3895 ModuleTypeUnderTest: "cc_library",
3896 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3897 Blueprint: soongCcLibraryPreamble + `
3898cc_library {
3899 name: "a",
3900 srcs: ["a.cpp"],
3901 export_include_dirs: ["eid1", "eid2"],
3902 export_system_include_dirs: ["esid1", "esid2"],
3903 target: {
3904 android: {
3905 export_include_dirs: ["android_eid1", "android_eid2"],
3906 export_system_include_dirs: ["android_esid1", "android_esid2"],
3907 },
3908 android_arm: {
3909 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3910 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3911 },
3912 linux: {
3913 export_include_dirs: ["linux_eid1", "linux_eid2"],
3914 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3915 },
3916 },
3917 multilib: {
3918 lib32: {
3919 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3920 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3921 },
3922 },
3923 arch: {
3924 arm: {
3925 export_include_dirs: ["arm_eid1", "arm_eid2"],
3926 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3927 },
3928 }
3929}
3930`,
3931 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3932 "export_includes": `select({
3933 "//build/bazel/platforms/os_arch:android_arm": [
3934 "android_arm_eid1",
3935 "android_arm_eid2",
3936 ],
3937 "//conditions:default": [],
3938 }) + select({
3939 "//build/bazel/platforms/os:android": [
3940 "android_eid1",
3941 "android_eid2",
3942 "linux_eid1",
3943 "linux_eid2",
3944 ],
3945 "//build/bazel/platforms/os:linux_bionic": [
3946 "linux_eid1",
3947 "linux_eid2",
3948 ],
3949 "//build/bazel/platforms/os:linux_glibc": [
3950 "linux_eid1",
3951 "linux_eid2",
3952 ],
3953 "//build/bazel/platforms/os:linux_musl": [
3954 "linux_eid1",
3955 "linux_eid2",
3956 ],
3957 "//conditions:default": [],
3958 }) + select({
3959 "//build/bazel/platforms/arch:arm": [
3960 "lib32_eid1",
3961 "lib32_eid2",
3962 "arm_eid1",
3963 "arm_eid2",
3964 ],
3965 "//build/bazel/platforms/arch:x86": [
3966 "lib32_eid1",
3967 "lib32_eid2",
3968 ],
3969 "//conditions:default": [],
3970 }) + [
3971 "eid1",
3972 "eid2",
3973 ]`,
3974 "export_system_includes": `select({
3975 "//build/bazel/platforms/os_arch:android_arm": [
3976 "android_arm_esid1",
3977 "android_arm_esid2",
3978 ],
3979 "//conditions:default": [],
3980 }) + select({
3981 "//build/bazel/platforms/os:android": [
3982 "android_esid1",
3983 "android_esid2",
3984 "linux_esid1",
3985 "linux_esid2",
3986 ],
3987 "//build/bazel/platforms/os:linux_bionic": [
3988 "linux_esid1",
3989 "linux_esid2",
3990 ],
3991 "//build/bazel/platforms/os:linux_glibc": [
3992 "linux_esid1",
3993 "linux_esid2",
3994 ],
3995 "//build/bazel/platforms/os:linux_musl": [
3996 "linux_esid1",
3997 "linux_esid2",
3998 ],
3999 "//conditions:default": [],
4000 }) + select({
4001 "//build/bazel/platforms/arch:arm": [
4002 "lib32_esid1",
4003 "lib32_esid2",
4004 "arm_esid1",
4005 "arm_esid2",
4006 ],
4007 "//build/bazel/platforms/arch:x86": [
4008 "lib32_esid1",
4009 "lib32_esid2",
4010 ],
4011 "//conditions:default": [],
4012 }) + [
4013 "esid1",
4014 "esid2",
4015 ]`,
4016 "srcs": `["a.cpp"]`,
4017 "local_includes": `["."]`,
4018 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
4019 }),
4020 },
4021 )
4022}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004023
4024func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
4025 runCcLibraryTestCase(t, Bp2buildTestCase{
4026 Description: "cc_library has correct features when integer_overflow property is provided",
4027 ModuleTypeUnderTest: "cc_library",
4028 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4029 Blueprint: `
4030cc_library {
4031 name: "foo",
4032 sanitize: {
4033 integer_overflow: true,
4034 },
4035}
4036`,
4037 ExpectedBazelTargets: []string{
4038 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4039 "features": `["ubsan_integer_overflow"]`,
4040 "local_includes": `["."]`,
4041 }),
4042 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4043 "features": `["ubsan_integer_overflow"]`,
4044 "local_includes": `["."]`,
4045 }),
4046 },
4047 })
4048}
4049
4050func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
4051 runCcLibraryTestCase(t, Bp2buildTestCase{
4052 Description: "cc_library has correct features when misc_undefined property is provided",
4053 ModuleTypeUnderTest: "cc_library",
4054 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4055 Blueprint: `
4056cc_library {
4057 name: "foo",
4058 sanitize: {
4059 misc_undefined: ["undefined", "nullability"],
4060 },
4061}
4062`,
4063 ExpectedBazelTargets: []string{
4064 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4065 "features": `[
4066 "ubsan_undefined",
4067 "ubsan_nullability",
4068 ]`,
4069 "local_includes": `["."]`,
4070 }),
4071 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4072 "features": `[
4073 "ubsan_undefined",
4074 "ubsan_nullability",
4075 ]`,
4076 "local_includes": `["."]`,
4077 }),
4078 },
4079 })
4080}
4081
4082func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4083 runCcLibraryTestCase(t, Bp2buildTestCase{
4084 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4085 ModuleTypeUnderTest: "cc_library",
4086 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4087 Blueprint: `
4088cc_library {
4089 name: "foo",
4090 sanitize: {
4091 misc_undefined: ["undefined", "nullability"],
4092 },
4093 target: {
4094 android: {
4095 sanitize: {
4096 misc_undefined: ["alignment"],
4097 },
4098 },
4099 linux_glibc: {
4100 sanitize: {
4101 integer_overflow: true,
4102 },
4103 },
4104 },
4105}
4106`,
4107 ExpectedBazelTargets: []string{
4108 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4109 "features": `[
4110 "ubsan_undefined",
4111 "ubsan_nullability",
4112 ] + select({
4113 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4114 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4115 "//conditions:default": [],
4116 })`,
4117 "local_includes": `["."]`,
4118 }),
4119 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4120 "features": `[
4121 "ubsan_undefined",
4122 "ubsan_nullability",
4123 ] + select({
4124 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4125 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4126 "//conditions:default": [],
4127 })`,
4128 "local_includes": `["."]`,
4129 }),
4130 },
4131 })
4132}
Yu Liu10174ff2023-02-21 12:05:26 -08004133
4134func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4135 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4136 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4137 ModuleTypeUnderTest: "cc_library",
4138 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4139 Blueprint: `
4140cc_library {
4141 name: "barlib",
4142 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4143 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004144 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004145}
4146cc_library {
4147 name: "bazlib",
4148 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4149 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004150 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004151}
4152cc_library {
4153 name: "foo",
4154 shared_libs: ["barlib", "bazlib"],
4155 export_shared_lib_headers: ["bazlib"],
4156 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004157 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004158 ],
4159}`,
4160 ExpectedBazelTargets: []string{
4161 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004162 "implementation_dynamic_deps": `[":barlib"]`,
4163 "dynamic_deps": `[":bazlib"]`,
4164 "local_includes": `["."]`,
4165 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004166 }),
4167 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004168 "implementation_dynamic_deps": `[":barlib"]`,
4169 "dynamic_deps": `[":bazlib"]`,
4170 "local_includes": `["."]`,
4171 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004172 }),
4173 },
4174 })
4175}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004176
4177func TestCcLibraryWithThinLto(t *testing.T) {
4178 runCcLibraryTestCase(t, Bp2buildTestCase{
4179 Description: "cc_library has correct features when thin LTO is enabled",
4180 ModuleTypeUnderTest: "cc_library",
4181 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4182 Blueprint: `
4183cc_library {
4184 name: "foo",
4185 lto: {
4186 thin: true,
4187 },
4188}`,
4189 ExpectedBazelTargets: []string{
4190 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4191 "features": `["android_thin_lto"]`,
4192 "local_includes": `["."]`,
4193 }),
4194 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4195 "features": `["android_thin_lto"]`,
4196 "local_includes": `["."]`,
4197 }),
4198 },
4199 })
4200}
4201
4202func TestCcLibraryWithLtoNever(t *testing.T) {
4203 runCcLibraryTestCase(t, Bp2buildTestCase{
4204 Description: "cc_library has correct features when LTO is explicitly disabled",
4205 ModuleTypeUnderTest: "cc_library",
4206 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4207 Blueprint: `
4208cc_library {
4209 name: "foo",
4210 lto: {
4211 never: true,
4212 },
4213}`,
4214 ExpectedBazelTargets: []string{
4215 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4216 "features": `["-android_thin_lto"]`,
4217 "local_includes": `["."]`,
4218 }),
4219 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4220 "features": `["-android_thin_lto"]`,
4221 "local_includes": `["."]`,
4222 }),
4223 },
4224 })
4225}
4226
4227func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4228 runCcLibraryTestCase(t, Bp2buildTestCase{
4229 Description: "cc_library has correct features when LTO differs across arch and os variants",
4230 ModuleTypeUnderTest: "cc_library",
4231 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4232 Blueprint: `
4233cc_library {
4234 name: "foo",
4235 target: {
4236 android: {
4237 lto: {
4238 thin: true,
4239 },
4240 },
4241 },
4242 arch: {
4243 riscv64: {
4244 lto: {
4245 thin: false,
4246 },
4247 },
4248 },
4249}`,
4250 ExpectedBazelTargets: []string{
4251 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4252 "local_includes": `["."]`,
4253 "features": `select({
4254 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4255 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4256 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4257 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4258 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4259 "//conditions:default": [],
4260 })`}),
4261 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4262 "local_includes": `["."]`,
4263 "features": `select({
4264 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4265 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4266 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4267 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4268 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4269 "//conditions:default": [],
4270 })`}),
4271 },
4272 })
4273}
4274
4275func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4276 runCcLibraryTestCase(t, Bp2buildTestCase{
4277 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4278 ModuleTypeUnderTest: "cc_library",
4279 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4280 Blueprint: `
4281cc_library {
4282 name: "foo",
4283 lto: {
4284 never: true,
4285 },
4286 target: {
4287 android: {
4288 lto: {
4289 thin: true,
4290 never: false,
4291 },
4292 },
4293 },
4294}`,
4295 ExpectedBazelTargets: []string{
4296 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4297 "local_includes": `["."]`,
4298 "features": `select({
4299 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4300 "//conditions:default": ["-android_thin_lto"],
4301 })`,
4302 }),
4303 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4304 "local_includes": `["."]`,
4305 "features": `select({
4306 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4307 "//conditions:default": ["-android_thin_lto"],
4308 })`,
4309 }),
4310 },
4311 })
4312}
4313
4314func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4315 runCcLibraryTestCase(t, Bp2buildTestCase{
4316 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4317 ModuleTypeUnderTest: "cc_library",
4318 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4319 Blueprint: `
4320cc_library {
4321 name: "foo",
4322 lto: {
4323 thin: true,
4324 },
4325 whole_program_vtables: true,
4326}`,
4327 ExpectedBazelTargets: []string{
4328 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4329 "features": `[
4330 "android_thin_lto",
4331 "android_thin_lto_whole_program_vtables",
4332 ]`,
4333 "local_includes": `["."]`,
4334 }),
4335 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4336 "features": `[
4337 "android_thin_lto",
4338 "android_thin_lto_whole_program_vtables",
4339 ]`,
4340 "local_includes": `["."]`,
4341 }),
4342 },
4343 })
4344}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004345
4346func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4347 runCcLibraryTestCase(t, Bp2buildTestCase{
4348 Description: "cc_library changes hidden visibility flag to feature",
4349 ModuleTypeUnderTest: "cc_library",
4350 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4351 Blueprint: `
4352cc_library {
4353 name: "foo",
4354 cflags: ["-fvisibility=hidden"],
4355}`,
4356 ExpectedBazelTargets: []string{
4357 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4358 "features": `["visibility_hidden"]`,
4359 "local_includes": `["."]`,
4360 }),
4361 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4362 "features": `["visibility_hidden"]`,
4363 "local_includes": `["."]`,
4364 }),
4365 },
4366 })
4367}
4368
4369func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4370 runCcLibraryTestCase(t, Bp2buildTestCase{
4371 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4372 ModuleTypeUnderTest: "cc_library",
4373 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4374 Blueprint: `
4375cc_library {
4376 name: "foo",
4377 shared: {
4378 cflags: ["-fvisibility=hidden"],
4379 },
4380}`,
4381 ExpectedBazelTargets: []string{
4382 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4383 "local_includes": `["."]`,
4384 }),
4385 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4386 "features": `["visibility_hidden"]`,
4387 "local_includes": `["."]`,
4388 }),
4389 },
4390 })
4391}
4392
4393func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4394 runCcLibraryTestCase(t, Bp2buildTestCase{
4395 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4396 ModuleTypeUnderTest: "cc_library",
4397 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4398 Blueprint: `
4399cc_library {
4400 name: "foo",
4401 static: {
4402 cflags: ["-fvisibility=hidden"],
4403 },
4404}`,
4405 ExpectedBazelTargets: []string{
4406 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4407 "features": `["visibility_hidden"]`,
4408 "local_includes": `["."]`,
4409 }),
4410 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4411 "local_includes": `["."]`,
4412 }),
4413 },
4414 })
4415}
4416
4417func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4418 runCcLibraryTestCase(t, Bp2buildTestCase{
4419 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4420 ModuleTypeUnderTest: "cc_library",
4421 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4422 Blueprint: `
4423cc_library {
4424 name: "foo",
4425 target: {
4426 android: {
4427 cflags: ["-fvisibility=hidden"],
4428 },
4429 },
4430}`,
4431 ExpectedBazelTargets: []string{
4432 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4433 "features": `select({
4434 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4435 "//conditions:default": [],
4436 })`,
4437 "local_includes": `["."]`,
4438 }),
4439 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4440 "features": `select({
4441 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4442 "//conditions:default": [],
4443 })`,
4444 "local_includes": `["."]`,
4445 }),
4446 },
4447 })
4448}
Spandan Das4242f102023-04-19 22:31:54 +00004449
4450// Test that a config_setting specific to an apex is created by cc_library.
4451func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4452 runCcLibraryTestCase(t, Bp2buildTestCase{
4453 Description: "cc_library creates a config_setting for each apex in apex_available",
4454 ModuleTypeUnderTest: "cc_library",
4455 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4456 Dir: "build/bazel/rules/apex",
4457 Blueprint: `
4458cc_library {
4459 name: "foo",
4460 apex_available: [
4461 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4462 "myapex"
4463 ],
4464}`,
4465 ExpectedBazelTargets: []string{
4466 MakeBazelTargetNoRestrictions(
4467 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004468 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004469 AttrNameToString{
4470 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004471 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004472 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004473 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004474 },
4475 ),
4476 },
4477 })
4478}
Yu Liu93893ba2023-05-01 13:49:52 -07004479
4480func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4481 runCcLibraryTestCase(t, Bp2buildTestCase{
4482 Description: "cc_library cppflags in product variables",
4483 ModuleTypeUnderTest: "cc_library",
4484 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4485 Blueprint: soongCcLibraryPreamble + `cc_library {
4486 name: "a",
4487 srcs: ["a.cpp"],
4488 cppflags: [
4489 "-Wextra",
4490 "-DDEBUG_ONLY_CODE=0",
4491 ],
4492 product_variables: {
4493 eng: {
4494 cppflags: [
4495 "-UDEBUG_ONLY_CODE",
4496 "-DDEBUG_ONLY_CODE=1",
4497 ],
4498 },
4499 },
4500 include_build_directory: false,
4501}
4502`,
4503 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4504 "cppflags": `[
4505 "-Wextra",
4506 "-DDEBUG_ONLY_CODE=0",
4507 ] + select({
4508 "//build/bazel/product_variables:eng": [
4509 "-UDEBUG_ONLY_CODE",
4510 "-DDEBUG_ONLY_CODE=1",
4511 ],
4512 "//conditions:default": [],
4513 })`,
4514 "srcs": `["a.cpp"]`,
4515 }),
4516 },
4517 )
4518}