blob: 6c9f9a18f071ed387390898961a95e3ff2f84533 [file] [log] [blame]
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +00001// 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 (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/cc"
22)
23
24const (
25 // See cc/testing.go for more context
26 // TODO(alexmarquez): Split out the preamble into common code?
27 soongCcLibrarySharedPreamble = soongCcLibraryStaticPreamble
28)
29
30func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) {
31 cc.RegisterCCBuildComponents(ctx)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000032 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
33 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer12615db2021-09-28 09:19:17 -040034 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000035}
36
Sam Delmerico3177a6e2022-06-21 19:28:33 +000037func runCcLibrarySharedTestCase(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000038 t.Helper()
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc641cc42023-03-16 17:03:43 +000039 t.Parallel()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000040 (&tc).ModuleTypeUnderTest = "cc_library_shared"
41 (&tc).ModuleTypeUnderTestFactory = cc.LibrarySharedFactory
42 RunBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000043}
44
45func TestCcLibrarySharedSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000046 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
47 Description: "cc_library_shared simple overall test",
48 Filesystem: map[string]string{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000049 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
50 "include_dir_1/include_dir_1_a.h": "",
51 "include_dir_1/include_dir_1_b.h": "",
52 "include_dir_2/include_dir_2_a.h": "",
53 "include_dir_2/include_dir_2_b.h": "",
54 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
55 "local_include_dir_1/local_include_dir_1_a.h": "",
56 "local_include_dir_1/local_include_dir_1_b.h": "",
57 "local_include_dir_2/local_include_dir_2_a.h": "",
58 "local_include_dir_2/local_include_dir_2_b.h": "",
59 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
60 "export_include_dir_1/export_include_dir_1_a.h": "",
61 "export_include_dir_1/export_include_dir_1_b.h": "",
62 "export_include_dir_2/export_include_dir_2_a.h": "",
63 "export_include_dir_2/export_include_dir_2_b.h": "",
64 // NOTE: Soong implicitly includes headers in the current directory
65 "implicit_include_1.h": "",
66 "implicit_include_2.h": "",
67 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000068 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000069cc_library_headers {
70 name: "header_lib_1",
71 export_include_dirs: ["header_lib_1"],
72 bazel_module: { bp2build_available: false },
73}
74
75cc_library_headers {
76 name: "header_lib_2",
77 export_include_dirs: ["header_lib_2"],
78 bazel_module: { bp2build_available: false },
79}
80
81cc_library_shared {
82 name: "shared_lib_1",
83 srcs: ["shared_lib_1.cc"],
84 bazel_module: { bp2build_available: false },
85}
86
87cc_library_shared {
88 name: "shared_lib_2",
89 srcs: ["shared_lib_2.cc"],
90 bazel_module: { bp2build_available: false },
91}
92
93cc_library_static {
94 name: "whole_static_lib_1",
95 srcs: ["whole_static_lib_1.cc"],
96 bazel_module: { bp2build_available: false },
97}
98
99cc_library_static {
100 name: "whole_static_lib_2",
101 srcs: ["whole_static_lib_2.cc"],
102 bazel_module: { bp2build_available: false },
103}
104
105cc_library_shared {
106 name: "foo_shared",
107 srcs: [
108 "foo_shared1.cc",
109 "foo_shared2.cc",
110 ],
111 cflags: [
112 "-Dflag1",
113 "-Dflag2"
114 ],
115 shared_libs: [
116 "shared_lib_1",
117 "shared_lib_2"
118 ],
119 whole_static_libs: [
120 "whole_static_lib_1",
121 "whole_static_lib_2"
122 ],
123 include_dirs: [
124 "include_dir_1",
125 "include_dir_2",
126 ],
127 local_include_dirs: [
128 "local_include_dir_1",
129 "local_include_dir_2",
130 ],
131 export_include_dirs: [
132 "export_include_dir_1",
133 "export_include_dir_2"
134 ],
135 header_libs: [
136 "header_lib_1",
137 "header_lib_2"
138 ],
Yu Liufc603162022-03-01 15:44:08 -0800139 sdk_version: "current",
140 min_sdk_version: "29",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000141
142 // TODO: Also support export_header_lib_headers
143}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000144 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000145 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500146 "absolute_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000147 "include_dir_1",
148 "include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 ]`,
150 "copts": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000151 "-Dflag1",
152 "-Dflag2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500153 ]`,
154 "export_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000155 "export_include_dir_1",
156 "export_include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500157 ]`,
158 "implementation_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000159 ":header_lib_1",
160 ":header_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500161 ]`,
162 "implementation_dynamic_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400163 ":shared_lib_1",
164 ":shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500165 ]`,
166 "local_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000167 "local_include_dir_1",
168 "local_include_dir_2",
169 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500170 ]`,
171 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000172 "foo_shared1.cc",
173 "foo_shared2.cc",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500174 ]`,
175 "whole_archive_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000176 ":whole_static_lib_1",
177 ":whole_static_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500178 ]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000179 "sdk_version": `"current"`,
180 "min_sdk_version": `"29"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500181 }),
182 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000183 })
184}
185
186func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000187 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
188 Description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
189 Filesystem: map[string]string{},
190 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000191cc_library_static {
192 name: "static_dep",
193 bazel_module: { bp2build_available: false },
194}
195cc_library_shared {
196 name: "shared_dep",
197 bazel_module: { bp2build_available: false },
198}
199cc_library_shared {
200 name: "foo_shared",
201 arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
202 include_build_directory: false,
203}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000204 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000205 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500206 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000207 "//build/bazel/platforms/arch:arm64": [":shared_dep"],
208 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500209 })`,
210 "whole_archive_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000211 "//build/bazel/platforms/arch:arm64": [":static_dep"],
212 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500213 })`,
214 }),
215 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000216 })
217}
218
219func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000220 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
221 Description: "cc_library_shared os-specific shared_libs",
222 Filesystem: map[string]string{},
223 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000224cc_library_shared {
225 name: "shared_dep",
226 bazel_module: { bp2build_available: false },
227}
228cc_library_shared {
229 name: "foo_shared",
230 target: { android: { shared_libs: ["shared_dep"], } },
231 include_build_directory: false,
232}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000233 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000234 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500235 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000236 "//build/bazel/platforms/os:android": [":shared_dep"],
237 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500238 })`,
239 }),
240 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000241 })
242}
243
244func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000245 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
246 Description: "cc_library_shared base, arch, and os-specific shared_libs",
247 Filesystem: map[string]string{},
248 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000249cc_library_shared {
250 name: "shared_dep",
251 bazel_module: { bp2build_available: false },
252}
253cc_library_shared {
254 name: "shared_dep2",
255 bazel_module: { bp2build_available: false },
256}
257cc_library_shared {
258 name: "shared_dep3",
259 bazel_module: { bp2build_available: false },
260}
261cc_library_shared {
262 name: "foo_shared",
263 shared_libs: ["shared_dep"],
264 target: { android: { shared_libs: ["shared_dep2"] } },
265 arch: { arm64: { shared_libs: ["shared_dep3"] } },
266 include_build_directory: false,
267}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000268 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000269 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500270 "implementation_dynamic_deps": `[":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000271 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
272 "//conditions:default": [],
273 }) + select({
274 "//build/bazel/platforms/os:android": [":shared_dep2"],
275 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500276 })`,
277 }),
278 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000279 })
280}
281
282func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000283 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
284 Description: "cc_library_shared simple exclude_srcs",
285 Filesystem: map[string]string{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000286 "common.c": "",
287 "foo-a.c": "",
288 "foo-excluded.c": "",
289 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000290 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000291cc_library_shared {
292 name: "foo_shared",
293 srcs: ["common.c", "foo-*.c"],
294 exclude_srcs: ["foo-excluded.c"],
295 include_build_directory: false,
296}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000297 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000298 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500299 "srcs_c": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000300 "common.c",
301 "foo-a.c",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500302 ]`,
303 }),
304 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000305 })
306}
307
308func TestCcLibrarySharedStrip(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000309 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
310 Description: "cc_library_shared stripping",
311 Filesystem: map[string]string{},
312 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000313cc_library_shared {
314 name: "foo_shared",
315 strip: {
316 keep_symbols: false,
317 keep_symbols_and_debug_frame: true,
318 keep_symbols_list: ["sym", "sym2"],
319 all: true,
320 none: false,
321 },
322 include_build_directory: false,
323}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000324 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000325 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500326 "strip": `{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000327 "all": True,
328 "keep_symbols": False,
329 "keep_symbols_and_debug_frame": True,
330 "keep_symbols_list": [
331 "sym",
332 "sym2",
333 ],
334 "none": False,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500335 }`,
336 }),
337 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000338 })
339}
340
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +0000341func TestCcLibrarySharedVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000342 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +0000343 Description: "cc_library_shared version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000344 Filesystem: map[string]string{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000345 "version_script": "",
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +0000346 "dynamic.list": "",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000347 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000348 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000349cc_library_shared {
350 name: "foo_shared",
351 version_script: "version_script",
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +0000352 dynamic_list: "dynamic.list",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000353 include_build_directory: false,
354}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000355 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000356 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +0000357 "additional_linker_inputs": `[
358 "version_script",
359 "dynamic.list",
360 ]`,
361 "linkopts": `[
362 "-Wl,--version-script,$(location version_script)",
363 "-Wl,--dynamic-list,$(location dynamic.list)",
364 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000365 "features": `["android_cfi_exports_map"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500366 }),
367 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000368 })
369}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000370
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000371func TestCcLibraryLdflagsSplitBySpaceSoongAdded(t *testing.T) {
372 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
373 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
374 Filesystem: map[string]string{
375 "version_script": "",
376 "dynamic.list": "",
377 },
378 Blueprint: `
379cc_library_shared {
380 name: "foo",
381 ldflags: [
382 "--nospace_flag",
383 "-z spaceflag",
384 ],
385 version_script: "version_script",
386 dynamic_list: "dynamic.list",
387 include_build_directory: false,
388}`,
389 ExpectedBazelTargets: []string{
390 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
391 "additional_linker_inputs": `[
392 "version_script",
393 "dynamic.list",
394 ]`,
395 "linkopts": `[
396 "--nospace_flag",
397 "-z",
398 "spaceflag",
399 "-Wl,--version-script,$(location version_script)",
400 "-Wl,--dynamic-list,$(location dynamic.list)",
401 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000402 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000403 }),
404 },
405 })
406}
407
Jingwen Chen6ada5892021-09-17 11:38:09 +0000408func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000409 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000410 Description: "cc_library_shared - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000411 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000412 "impl.cpp": "",
413 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000414 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000415cc_library_shared {
416 name: "foo_shared",
417 srcs: ["impl.cpp"],
418 nocrt: true,
419 include_build_directory: false,
420}
421`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000422 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000423 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000424 "features": `["-link_crt"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500425 "srcs": `["impl.cpp"]`,
426 }),
427 },
428 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000429}
430
431func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000432 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000433 Description: "cc_library_shared - nocrt: false doesn't disable feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000434 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000435 "impl.cpp": "",
436 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000437 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000438cc_library_shared {
439 name: "foo_shared",
440 srcs: ["impl.cpp"],
441 nocrt: false,
442 include_build_directory: false,
443}
444`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000445 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000446 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500447 "srcs": `["impl.cpp"]`,
448 }),
449 },
450 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000451}
452
453func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000454 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
455 Description: "cc_library_shared - nocrt in select",
456 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000457 "impl.cpp": "",
458 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000459 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000460cc_library_shared {
461 name: "foo_shared",
462 srcs: ["impl.cpp"],
463 arch: {
464 arm: {
465 nocrt: true,
466 },
467 x86: {
468 nocrt: false,
469 },
470 },
471 include_build_directory: false,
472}
473`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000474 ExpectedBazelTargets: []string{
475 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
476 "features": `select({
477 "//build/bazel/platforms/arch:arm": ["-link_crt"],
478 "//conditions:default": [],
479 })`,
480 "srcs": `["impl.cpp"]`,
481 }),
482 },
Jingwen Chen6ada5892021-09-17 11:38:09 +0000483 })
484}
Liz Kammer12615db2021-09-28 09:19:17 -0400485
486func TestCcLibrarySharedProto(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000487 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
488 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Liz Kammer12615db2021-09-28 09:19:17 -0400489 name: "foo",
490 srcs: ["foo.proto"],
491 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400492 export_proto_headers: true,
493 },
494 include_build_directory: false,
495}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000496 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000497 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400498 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +0000499 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400500 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +0000501 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400502 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
503 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
504 }),
505 },
506 })
507}
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500508
509func TestCcLibrarySharedUseVersionLib(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000510 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Liz Kammerbaced712022-09-16 09:01:29 -0400511 Filesystem: map[string]string{
512 soongCcVersionLibBpPath: soongCcVersionLibBp,
513 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000514 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500515 name: "foo",
516 use_version_lib: true,
517 include_build_directory: false,
518}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000520 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Yu Liufe978fd2023-04-24 16:37:18 -0700521 "use_version_lib": "True",
522 "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500523 }),
524 },
525 })
526}
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000527
528func TestCcLibrarySharedStubs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000529 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
530 Description: "cc_library_shared stubs",
531 ModuleTypeUnderTest: "cc_library_shared",
532 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
533 Dir: "foo/bar",
534 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000535 "foo/bar/Android.bp": `
536cc_library_shared {
537 name: "a",
538 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
539 bazel_module: { bp2build_available: true },
540 include_build_directory: false,
541}
542`,
543 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000544 Blueprint: soongCcLibraryPreamble,
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000545 ExpectedBazelTargets: []string{makeCcStubSuiteTargets("a", AttrNameToString{
Sam Delmerico5f906492023-03-15 18:06:18 -0400546 "soname": `"a.so"`,
547 "source_library_label": `"//foo/bar:a"`,
548 "stubs_symbol_file": `"a.map.txt"`,
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000549 "stubs_versions": `[
Trevor Radcliffe087af542022-09-16 15:36:10 +0000550 "28",
551 "29",
552 "current",
553 ]`,
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000554 }),
555 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -0800556 "stubs_symbol_file": `"a.map.txt"`,
Trevor Radcliffe087af542022-09-16 15:36:10 +0000557 }),
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000558 },
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000559 })
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000560}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000561
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400562func TestCcLibrarySharedStubs_UseImplementationInSameApex(t *testing.T) {
563 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
564 Description: "cc_library_shared stubs",
565 ModuleTypeUnderTest: "cc_library_shared",
566 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
567 Blueprint: soongCcLibrarySharedPreamble + `
568cc_library_shared {
569 name: "a",
570 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
571 bazel_module: { bp2build_available: false },
572 include_build_directory: false,
573 apex_available: ["made_up_apex"],
574}
575cc_library_shared {
576 name: "b",
577 shared_libs: [":a"],
578 include_build_directory: false,
579 apex_available: ["made_up_apex"],
580}
581`,
582 ExpectedBazelTargets: []string{
583 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
584 "implementation_dynamic_deps": `[":a"]`,
585 "tags": `["apex_available=made_up_apex"]`,
586 }),
587 },
588 })
589}
590
591func TestCcLibrarySharedStubs_UseStubsInDifferentApex(t *testing.T) {
592 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
593 Description: "cc_library_shared stubs",
594 ModuleTypeUnderTest: "cc_library_shared",
595 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
596 Blueprint: soongCcLibrarySharedPreamble + `
597cc_library_shared {
598 name: "a",
599 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
600 bazel_module: { bp2build_available: false },
601 include_build_directory: false,
602 apex_available: ["apex_a"],
603}
604cc_library_shared {
605 name: "b",
606 shared_libs: [":a"],
607 include_build_directory: false,
608 apex_available: ["apex_b"],
609}
610`,
611 ExpectedBazelTargets: []string{
612 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
613 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +0000614 "//build/bazel/rules/apex:apex_b": ["@api_surfaces//module-libapi/current:a"],
615 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"],
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400616 "//conditions:default": [":a"],
617 })`,
618 "tags": `["apex_available=apex_b"]`,
619 }),
620 },
621 })
622}
623
Spandan Das6d4d9da2023-04-18 06:20:40 +0000624// Tests that library in apexfoo links against stubs of platform_lib and otherapex_lib
625func TestCcLibrarySharedStubs_UseStubsFromMultipleApiDomains(t *testing.T) {
626 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
627 Description: "cc_library_shared stubs",
628 ModuleTypeUnderTest: "cc_library_shared",
629 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
630 Blueprint: soongCcLibrarySharedPreamble + `
631cc_library_shared {
632 name: "libplatform_stable",
633 stubs: { symbol_file: "libplatform_stable.map.txt", versions: ["28", "29", "current"] },
634 apex_available: ["//apex_available:platform"],
635 bazel_module: { bp2build_available: false },
636 include_build_directory: false,
637}
638cc_library_shared {
639 name: "libapexfoo_stable",
640 stubs: { symbol_file: "libapexfoo_stable.map.txt", versions: ["28", "29", "current"] },
641 apex_available: ["apexfoo"],
642 bazel_module: { bp2build_available: false },
643 include_build_directory: false,
644}
645cc_library_shared {
646 name: "libutils",
647 shared_libs: ["libplatform_stable", "libapexfoo_stable",],
648 apex_available: ["//apex_available:platform", "apexfoo", "apexbar"],
649 include_build_directory: false,
650}
651`,
652 ExpectedBazelTargets: []string{
653 MakeBazelTarget("cc_library_shared", "libutils", AttrNameToString{
654 "implementation_dynamic_deps": `select({
655 "//build/bazel/rules/apex:apexbar": [
656 "@api_surfaces//module-libapi/current:libplatform_stable",
657 "@api_surfaces//module-libapi/current:libapexfoo_stable",
658 ],
659 "//build/bazel/rules/apex:apexfoo": [
660 "@api_surfaces//module-libapi/current:libplatform_stable",
661 ":libapexfoo_stable",
662 ],
663 "//build/bazel/rules/apex:system": [
664 "@api_surfaces//module-libapi/current:libplatform_stable",
665 "@api_surfaces//module-libapi/current:libapexfoo_stable",
666 ],
667 "//conditions:default": [
668 ":libplatform_stable",
669 ":libapexfoo_stable",
670 ],
671 })`,
672 "tags": `[
673 "apex_available=//apex_available:platform",
674 "apex_available=apexfoo",
675 "apex_available=apexbar",
676 ]`,
677 }),
678 },
679 })
680}
681
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400682func TestCcLibrarySharedStubs_IgnorePlatformAvailable(t *testing.T) {
683 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
684 Description: "cc_library_shared stubs",
685 ModuleTypeUnderTest: "cc_library_shared",
686 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
687 Blueprint: soongCcLibrarySharedPreamble + `
688cc_library_shared {
689 name: "a",
690 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
691 bazel_module: { bp2build_available: false },
692 include_build_directory: false,
693 apex_available: ["//apex_available:platform", "apex_a"],
694}
695cc_library_shared {
696 name: "b",
697 shared_libs: [":a"],
698 include_build_directory: false,
699 apex_available: ["//apex_available:platform", "apex_b"],
700}
701`,
702 ExpectedBazelTargets: []string{
703 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
704 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +0000705 "//build/bazel/rules/apex:apex_b": ["@api_surfaces//module-libapi/current:a"],
706 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"],
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400707 "//conditions:default": [":a"],
708 })`,
709 "tags": `[
710 "apex_available=//apex_available:platform",
711 "apex_available=apex_b",
712 ]`,
713 }),
714 },
715 })
716}
717
Spandan Das6d4d9da2023-04-18 06:20:40 +0000718func TestCcLibraryDoesNotDropStubDepIfNoVariationAcrossAxis(t *testing.T) {
719 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
720 Description: "cc_library depeends on impl for all configurations",
721 ModuleTypeUnderTest: "cc_library_shared",
722 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
723 Blueprint: soongCcLibrarySharedPreamble + `
724cc_library_shared {
725 name: "a",
726 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
727 bazel_module: { bp2build_available: false },
728 apex_available: ["//apex_available:platform"],
729}
730cc_library_shared {
731 name: "b",
732 shared_libs: [":a"],
733 include_build_directory: false,
734 apex_available: ["//apex_available:platform"],
735}
736`,
737 ExpectedBazelTargets: []string{
738 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
739 "implementation_dynamic_deps": `[":a"]`,
740 "tags": `["apex_available=//apex_available:platform"]`,
741 }),
742 },
743 })
744}
745
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400746func TestCcLibrarySharedStubs_MultipleApexAvailable(t *testing.T) {
747 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
748 ModuleTypeUnderTest: "cc_library_shared",
749 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
750 Blueprint: soongCcLibrarySharedPreamble + `
751cc_library_shared {
752 name: "a",
753 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
754 bazel_module: { bp2build_available: false },
755 include_build_directory: false,
756 apex_available: ["//apex_available:platform", "apex_a", "apex_b"],
757}
758cc_library_shared {
759 name: "b",
760 shared_libs: [":a"],
761 include_build_directory: false,
762 apex_available: ["//apex_available:platform", "apex_b"],
763}
764
765cc_library_shared {
766 name: "c",
767 shared_libs: [":a"],
768 include_build_directory: false,
769 apex_available: ["//apex_available:platform", "apex_a", "apex_b"],
770}
771`,
772 ExpectedBazelTargets: []string{
773 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
774 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +0000775 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"],
Liz Kammer48cdbeb2023-03-17 10:17:50 -0400776 "//conditions:default": [":a"],
777 })`,
778 "tags": `[
779 "apex_available=//apex_available:platform",
780 "apex_available=apex_b",
781 ]`,
782 }),
783 MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
784 "implementation_dynamic_deps": `[":a"]`,
785 "tags": `[
786 "apex_available=//apex_available:platform",
787 "apex_available=apex_a",
788 "apex_available=apex_b",
789 ]`,
790 }),
791 },
792 })
793}
794
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000795func TestCcLibrarySharedSystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000796 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
797 Description: "cc_library_shared system_shared_libs empty shared default",
798 ModuleTypeUnderTest: "cc_library_shared",
799 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
800 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000801cc_defaults {
802 name: "empty_defaults",
803 shared: {
804 system_shared_libs: [],
805 },
806 include_build_directory: false,
807}
808cc_library_shared {
809 name: "empty",
810 defaults: ["empty_defaults"],
811}
812`,
Alixe06d75b2022-08-31 18:28:19 +0000813 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_shared", "empty", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000814 "system_dynamic_deps": "[]",
815 })},
816 })
817}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000818
819func TestCcLibrarySharedConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000820 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
821 Description: "cc_library_shared with lex files",
822 ModuleTypeUnderTest: "cc_library_shared",
823 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
824 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000825 "foo.c": "",
826 "bar.cc": "",
827 "foo1.l": "",
828 "bar1.ll": "",
829 "foo2.l": "",
830 "bar2.ll": "",
831 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000832 Blueprint: `cc_library_shared {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000833 name: "foo_lib",
834 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
835 lex: { flags: ["--foo_flags"] },
836 include_build_directory: false,
837 bazel_module: { bp2build_available: true },
838}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000839 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000840 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000841 "srcs": `[
842 "foo1.l",
843 "foo2.l",
844 ]`,
845 "lexopts": `["--foo_flags"]`,
846 }),
Alixe06d75b2022-08-31 18:28:19 +0000847 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000848 "srcs": `[
849 "bar1.ll",
850 "bar2.ll",
851 ]`,
852 "lexopts": `["--foo_flags"]`,
853 }),
Alixe06d75b2022-08-31 18:28:19 +0000854 MakeBazelTarget("cc_library_shared", "foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000855 "srcs": `[
856 "bar.cc",
857 ":foo_lib_genlex_ll",
858 ]`,
859 "srcs_c": `[
860 "foo.c",
861 ":foo_lib_genlex_l",
862 ]`,
863 }),
864 },
865 })
866}
Alix1be00d42022-05-16 22:56:04 +0000867
868func TestCcLibrarySharedClangUnknownFlags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000869 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
870 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Alix1be00d42022-05-16 22:56:04 +0000871 name: "foo",
872 conlyflags: ["-a", "-finline-functions"],
873 cflags: ["-b","-finline-functions"],
874 cppflags: ["-c", "-finline-functions"],
875 ldflags: ["-d","-finline-functions", "-e"],
876 include_build_directory: false,
877}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000878 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000879 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alix1be00d42022-05-16 22:56:04 +0000880 "conlyflags": `["-a"]`,
881 "copts": `["-b"]`,
882 "cppflags": `["-c"]`,
883 "linkopts": `[
884 "-d",
885 "-e",
886 ]`,
887 }),
888 },
889 })
890}
891
892func TestCCLibraryFlagSpaceSplitting(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000893 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
894 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Alix1be00d42022-05-16 22:56:04 +0000895 name: "foo",
896 conlyflags: [ "-include header.h"],
897 cflags: ["-include header.h"],
898 cppflags: ["-include header.h"],
899 version_script: "version_script",
900 include_build_directory: false,
901}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000902 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000903 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alix1be00d42022-05-16 22:56:04 +0000904 "additional_linker_inputs": `["version_script"]`,
905 "conlyflags": `[
906 "-include",
907 "header.h",
908 ]`,
909 "copts": `[
910 "-include",
911 "header.h",
912 ]`,
913 "cppflags": `[
914 "-include",
915 "header.h",
916 ]`,
917 "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000918 "features": `["android_cfi_exports_map"]`,
Alix1be00d42022-05-16 22:56:04 +0000919 }),
920 },
921 })
922}
Cole Faust6b29f592022-08-09 09:50:56 -0700923
924func TestCCLibrarySharedRuntimeDeps(t *testing.T) {
925 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
926 Blueprint: `cc_library_shared {
927 name: "bar",
928}
929
930cc_library_shared {
931 name: "foo",
932 runtime_libs: ["foo"],
933}`,
934 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000935 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -0700936 "local_includes": `["."]`,
937 }),
Alixe06d75b2022-08-31 18:28:19 +0000938 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -0700939 "runtime_deps": `[":foo"]`,
940 "local_includes": `["."]`,
941 }),
942 },
943 })
944}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500945
946func TestCcLibrarySharedEmptySuffix(t *testing.T) {
947 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
948 Description: "cc_library_shared with empty suffix",
949 Filesystem: map[string]string{
950 "foo.c": "",
951 },
952 Blueprint: soongCcLibrarySharedPreamble + `
953cc_library_shared {
954 name: "foo_shared",
955 suffix: "",
956 srcs: ["foo.c"],
957 include_build_directory: false,
958}`,
959 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000960 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500961 "srcs_c": `["foo.c"]`,
962 "suffix": `""`,
963 }),
964 },
965 })
966}
967
968func TestCcLibrarySharedSuffix(t *testing.T) {
969 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
970 Description: "cc_library_shared with suffix",
971 Filesystem: map[string]string{
972 "foo.c": "",
973 },
974 Blueprint: soongCcLibrarySharedPreamble + `
975cc_library_shared {
976 name: "foo_shared",
977 suffix: "-suf",
978 srcs: ["foo.c"],
979 include_build_directory: false,
980}`,
981 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000982 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500983 "srcs_c": `["foo.c"]`,
984 "suffix": `"-suf"`,
985 }),
986 },
987 })
988}
989
990func TestCcLibrarySharedArchVariantSuffix(t *testing.T) {
991 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
992 Description: "cc_library_shared with arch-variant suffix",
993 Filesystem: map[string]string{
994 "foo.c": "",
995 },
996 Blueprint: soongCcLibrarySharedPreamble + `
997cc_library_shared {
998 name: "foo_shared",
999 arch: {
1000 arm64: { suffix: "-64" },
1001 arm: { suffix: "-32" },
1002 },
1003 srcs: ["foo.c"],
1004 include_build_directory: false,
1005}`,
1006 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001007 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05001008 "srcs_c": `["foo.c"]`,
1009 "suffix": `select({
1010 "//build/bazel/platforms/arch:arm": "-32",
1011 "//build/bazel/platforms/arch:arm64": "-64",
1012 "//conditions:default": None,
1013 })`,
1014 }),
1015 },
1016 })
1017}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00001018
1019func TestCcLibrarySharedWithSyspropSrcs(t *testing.T) {
1020 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1021 Description: "cc_library_shared with sysprop sources",
1022 Blueprint: `
1023cc_library_shared {
1024 name: "foo",
1025 srcs: [
1026 "bar.sysprop",
1027 "baz.sysprop",
1028 "blah.cpp",
1029 ],
1030 min_sdk_version: "5",
1031}`,
1032 ExpectedBazelTargets: []string{
1033 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
1034 "srcs": `[
1035 "bar.sysprop",
1036 "baz.sysprop",
1037 ]`,
1038 }),
1039 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
1040 "dep": `":foo_sysprop_library"`,
1041 "min_sdk_version": `"5"`,
1042 }),
1043 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1044 "srcs": `["blah.cpp"]`,
1045 "local_includes": `["."]`,
1046 "min_sdk_version": `"5"`,
1047 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
1048 }),
1049 },
1050 })
1051}
1052
1053func TestCcLibrarySharedWithSyspropSrcsSomeConfigs(t *testing.T) {
1054 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1055 Description: "cc_library_shared with sysprop sources in some configs but not others",
1056 Blueprint: `
1057cc_library_shared {
1058 name: "foo",
1059 srcs: [
1060 "blah.cpp",
1061 ],
1062 target: {
1063 android: {
1064 srcs: ["bar.sysprop"],
1065 },
1066 },
1067 min_sdk_version: "5",
1068}`,
1069 ExpectedBazelTargets: []string{
1070 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
1071 "srcs": `select({
1072 "//build/bazel/platforms/os:android": ["bar.sysprop"],
1073 "//conditions:default": [],
1074 })`,
1075 }),
1076 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
1077 "dep": `":foo_sysprop_library"`,
1078 "min_sdk_version": `"5"`,
1079 }),
1080 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1081 "srcs": `["blah.cpp"]`,
1082 "local_includes": `["."]`,
1083 "min_sdk_version": `"5"`,
1084 "whole_archive_deps": `select({
1085 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
1086 "//conditions:default": [],
1087 })`,
1088 }),
1089 },
1090 })
1091}
Yu Liu56ccb1a2022-11-12 10:47:07 -08001092
1093func TestCcLibrarySharedHeaderAbiChecker(t *testing.T) {
1094 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1095 Description: "cc_library_shared with header abi checker",
1096 Blueprint: `cc_library_shared {
1097 name: "foo",
1098 header_abi_checker: {
1099 enabled: true,
1100 symbol_file: "a.map.txt",
1101 exclude_symbol_versions: [
1102 "29",
1103 "30",
1104 ],
1105 exclude_symbol_tags: [
1106 "tag1",
1107 "tag2",
1108 ],
1109 check_all_apis: true,
1110 diff_flags: ["-allow-adding-removing-weak-symbols"],
1111 },
1112 include_build_directory: false,
1113}`,
1114 ExpectedBazelTargets: []string{
1115 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1116 "abi_checker_enabled": `True`,
1117 "abi_checker_symbol_file": `"a.map.txt"`,
1118 "abi_checker_exclude_symbol_versions": `[
1119 "29",
1120 "30",
1121 ]`,
1122 "abi_checker_exclude_symbol_tags": `[
1123 "tag1",
1124 "tag2",
1125 ]`,
1126 "abi_checker_check_all_apis": `True`,
1127 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
1128 }),
1129 },
1130 })
1131}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00001132
1133func TestCcLibrarySharedWithIntegerOverflowProperty(t *testing.T) {
1134 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1135 Description: "cc_library_shared has correct features when integer_overflow property is provided",
1136 Blueprint: `
1137cc_library_shared {
1138 name: "foo",
1139 sanitize: {
1140 integer_overflow: true,
1141 },
1142}
1143`,
1144 ExpectedBazelTargets: []string{
1145 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1146 "features": `["ubsan_integer_overflow"]`,
1147 "local_includes": `["."]`,
1148 }),
1149 },
1150 })
1151}
1152
1153func TestCcLibrarySharedWithMiscUndefinedProperty(t *testing.T) {
1154 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1155 Description: "cc_library_shared has correct features when misc_undefined property is provided",
1156 Blueprint: `
1157cc_library_shared {
1158 name: "foo",
1159 sanitize: {
1160 misc_undefined: ["undefined", "nullability"],
1161 },
1162}
1163`,
1164 ExpectedBazelTargets: []string{
1165 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1166 "features": `[
1167 "ubsan_undefined",
1168 "ubsan_nullability",
1169 ]`,
1170 "local_includes": `["."]`,
1171 }),
1172 },
1173 })
1174}
1175
1176func TestCcLibrarySharedWithUBSanPropertiesArchSpecific(t *testing.T) {
1177 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1178 Description: "cc_library_shared has correct feature select when UBSan props are specified in arch specific blocks",
1179 Blueprint: `
1180cc_library_shared {
1181 name: "foo",
1182 sanitize: {
1183 misc_undefined: ["undefined", "nullability"],
1184 },
1185 target: {
1186 android: {
1187 sanitize: {
1188 misc_undefined: ["alignment"],
1189 },
1190 },
1191 linux_glibc: {
1192 sanitize: {
1193 integer_overflow: true,
1194 },
1195 },
1196 },
1197}
1198`,
1199 ExpectedBazelTargets: []string{
1200 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1201 "features": `[
1202 "ubsan_undefined",
1203 "ubsan_nullability",
1204 ] + select({
1205 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
1206 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
1207 "//conditions:default": [],
1208 })`,
1209 "local_includes": `["."]`,
1210 }),
1211 },
1212 })
1213}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00001214
1215func TestCcLibrarySharedWithThinLto(t *testing.T) {
1216 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1217 Description: "cc_library_shared has correct features when thin lto is enabled",
1218 Blueprint: `
1219cc_library_shared {
1220 name: "foo",
1221 lto: {
1222 thin: true,
1223 },
1224}
1225`,
1226 ExpectedBazelTargets: []string{
1227 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1228 "features": `["android_thin_lto"]`,
1229 "local_includes": `["."]`,
1230 }),
1231 },
1232 })
1233}
1234
1235func TestCcLibrarySharedWithLtoNever(t *testing.T) {
1236 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1237 Description: "cc_library_shared has correct features when thin lto is enabled",
1238 Blueprint: `
1239cc_library_shared {
1240 name: "foo",
1241 lto: {
1242 never: true,
1243 },
1244}
1245`,
1246 ExpectedBazelTargets: []string{
1247 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1248 "features": `["-android_thin_lto"]`,
1249 "local_includes": `["."]`,
1250 }),
1251 },
1252 })
1253}
1254
1255func TestCcLibrarySharedWithThinLtoArchSpecific(t *testing.T) {
1256 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1257 Description: "cc_library_shared has correct features when LTO differs across arch and os variants",
1258 Blueprint: `
1259cc_library_shared {
1260 name: "foo",
1261 target: {
1262 android: {
1263 lto: {
1264 thin: true,
1265 },
1266 },
1267 },
1268 arch: {
1269 riscv64: {
1270 lto: {
1271 thin: false,
1272 },
1273 },
1274 },
1275}`,
1276 ExpectedBazelTargets: []string{
1277 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1278 "local_includes": `["."]`,
1279 "features": `select({
1280 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
1281 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
1282 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
1283 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
1284 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
1285 "//conditions:default": [],
1286 })`}),
1287 },
1288 })
1289}
1290
1291func TestCcLibrarySharedWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
1292 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1293 Description: "cc_library_shared with thin lto disabled by default but enabled on a particular variant",
1294 Blueprint: `
1295cc_library_shared {
1296 name: "foo",
1297 lto: {
1298 never: true,
1299 },
1300 target: {
1301 android: {
1302 lto: {
1303 thin: true,
1304 never: false,
1305 },
1306 },
1307 },
1308}`,
1309 ExpectedBazelTargets: []string{
1310 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1311 "local_includes": `["."]`,
1312 "features": `select({
1313 "//build/bazel/platforms/os:android": ["android_thin_lto"],
1314 "//conditions:default": ["-android_thin_lto"],
1315 })`,
1316 }),
1317 },
1318 })
1319}
1320
1321func TestCcLibrarySharedWithThinLtoAndWholeProgramVtables(t *testing.T) {
1322 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1323 Description: "cc_library_shared has correct features when thin LTO is enabled with whole_program_vtables",
1324 Blueprint: `
1325cc_library_shared {
1326 name: "foo",
1327 lto: {
1328 thin: true,
1329 },
1330 whole_program_vtables: true,
1331}
1332`,
1333 ExpectedBazelTargets: []string{
1334 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1335 "features": `[
1336 "android_thin_lto",
1337 "android_thin_lto_whole_program_vtables",
1338 ]`,
1339 "local_includes": `["."]`,
1340 }),
1341 },
1342 })
1343}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00001344
1345func TestCcLibrarySharedHiddenVisibilityConvertedToFeature(t *testing.T) {
1346 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1347 Description: "cc_library_shared changes hidden visibility flag to feature",
1348 Blueprint: `
1349cc_library_shared{
1350 name: "foo",
1351 cflags: ["-fvisibility=hidden"],
1352}`,
1353 ExpectedBazelTargets: []string{
1354 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1355 "features": `["visibility_hidden"]`,
1356 "local_includes": `["."]`,
1357 }),
1358 },
1359 })
1360}
1361
1362func TestCcLibrarySharedHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
1363 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1364 Description: "cc_library_shared changes hidden visibility flag to feature for specific os",
1365 Blueprint: `
1366cc_library_shared{
1367 name: "foo",
1368 target: {
1369 android: {
1370 cflags: ["-fvisibility=hidden"],
1371 },
1372 },
1373}`,
1374 ExpectedBazelTargets: []string{
1375 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1376 "features": `select({
1377 "//build/bazel/platforms/os:android": ["visibility_hidden"],
1378 "//conditions:default": [],
1379 })`,
1380 "local_includes": `["."]`,
1381 }),
1382 },
1383 })
1384}
Sam Delmerico75dbca22023-04-20 13:13:25 +00001385
1386func TestCcLibrarySharedStubsDessertVersionConversion(t *testing.T) {
1387 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
1388 Description: "cc_library_shared converts dessert codename versions to numerical versions",
1389 Blueprint: `
1390cc_library_shared {
1391 name: "a",
1392 include_build_directory: false,
1393 stubs: {
1394 symbol_file: "a.map.txt",
1395 versions: [
1396 "Q",
1397 "R",
1398 "31",
1399 ],
1400 },
1401}
1402cc_library_shared {
1403 name: "b",
1404 include_build_directory: false,
1405 stubs: {
1406 symbol_file: "b.map.txt",
1407 versions: [
1408 "Q",
1409 "R",
1410 "31",
1411 "current",
1412 ],
1413 },
1414}
1415`,
1416 ExpectedBazelTargets: []string{
1417 makeCcStubSuiteTargets("a", AttrNameToString{
1418 "soname": `"a.so"`,
1419 "source_library_label": `"//:a"`,
1420 "stubs_symbol_file": `"a.map.txt"`,
1421 "stubs_versions": `[
1422 "29",
1423 "30",
1424 "31",
1425 "current",
1426 ]`,
1427 }),
1428 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
1429 "stubs_symbol_file": `"a.map.txt"`,
1430 }),
1431 makeCcStubSuiteTargets("b", AttrNameToString{
1432 "soname": `"b.so"`,
1433 "source_library_label": `"//:b"`,
1434 "stubs_symbol_file": `"b.map.txt"`,
1435 "stubs_versions": `[
1436 "29",
1437 "30",
1438 "31",
1439 "current",
1440 ]`,
1441 }),
1442 MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
1443 "stubs_symbol_file": `"b.map.txt"`,
1444 }),
1445 },
1446 })
1447}