blob: 7e1d1117d3a3fcfd355112d377339787a08b1e4a [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 (
Jingwen Chen6ada5892021-09-17 11:38:09 +000018 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000019 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
23)
24
25const (
26 // See cc/testing.go for more context
27 // TODO(alexmarquez): Split out the preamble into common code?
28 soongCcLibrarySharedPreamble = soongCcLibraryStaticPreamble
29)
30
31func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) {
32 cc.RegisterCCBuildComponents(ctx)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000033 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
34 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer12615db2021-09-28 09:19:17 -040035 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000036}
37
Sam Delmerico3177a6e2022-06-21 19:28:33 +000038func runCcLibrarySharedTestCase(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000039 t.Helper()
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 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500365 }),
366 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000367 })
368}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000369
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000370func TestCcLibraryLdflagsSplitBySpaceSoongAdded(t *testing.T) {
371 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
372 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
373 Filesystem: map[string]string{
374 "version_script": "",
375 "dynamic.list": "",
376 },
377 Blueprint: `
378cc_library_shared {
379 name: "foo",
380 ldflags: [
381 "--nospace_flag",
382 "-z spaceflag",
383 ],
384 version_script: "version_script",
385 dynamic_list: "dynamic.list",
386 include_build_directory: false,
387}`,
388 ExpectedBazelTargets: []string{
389 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
390 "additional_linker_inputs": `[
391 "version_script",
392 "dynamic.list",
393 ]`,
394 "linkopts": `[
395 "--nospace_flag",
396 "-z",
397 "spaceflag",
398 "-Wl,--version-script,$(location version_script)",
399 "-Wl,--dynamic-list,$(location dynamic.list)",
400 ]`,
401 }),
402 },
403 })
404}
405
Jingwen Chen6ada5892021-09-17 11:38:09 +0000406func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000407 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
408 Description: "cc_library_shared - nocrt: true emits attribute",
409 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000410 "impl.cpp": "",
411 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000412 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000413cc_library_shared {
414 name: "foo_shared",
415 srcs: ["impl.cpp"],
416 nocrt: true,
417 include_build_directory: false,
418}
419`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000420 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000421 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500422 "link_crt": `False`,
423 "srcs": `["impl.cpp"]`,
424 }),
425 },
426 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000427}
428
429func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000430 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
431 Description: "cc_library_shared - nocrt: false doesn't emit attribute",
432 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000433 "impl.cpp": "",
434 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000435 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000436cc_library_shared {
437 name: "foo_shared",
438 srcs: ["impl.cpp"],
439 nocrt: false,
440 include_build_directory: false,
441}
442`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000443 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000444 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 "srcs": `["impl.cpp"]`,
446 }),
447 },
448 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000449}
450
451func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000452 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
453 Description: "cc_library_shared - nocrt in select",
454 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +0000455 "impl.cpp": "",
456 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000457 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +0000458cc_library_shared {
459 name: "foo_shared",
460 srcs: ["impl.cpp"],
461 arch: {
462 arm: {
463 nocrt: true,
464 },
465 x86: {
466 nocrt: false,
467 },
468 },
469 include_build_directory: false,
470}
471`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000472 ExpectedErr: fmt.Errorf("module \"foo_shared\": nocrt is not supported for arch variants"),
Jingwen Chen6ada5892021-09-17 11:38:09 +0000473 })
474}
Liz Kammer12615db2021-09-28 09:19:17 -0400475
476func TestCcLibrarySharedProto(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000477 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
478 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Liz Kammer12615db2021-09-28 09:19:17 -0400479 name: "foo",
480 srcs: ["foo.proto"],
481 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400482 export_proto_headers: true,
483 },
484 include_build_directory: false,
485}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000486 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000487 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400488 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +0000489 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400490 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +0000491 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400492 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
493 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
494 }),
495 },
496 })
497}
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500498
499func TestCcLibrarySharedUseVersionLib(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000500 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Liz Kammerbaced712022-09-16 09:01:29 -0400501 Filesystem: map[string]string{
502 soongCcVersionLibBpPath: soongCcVersionLibBp,
503 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000504 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500505 name: "foo",
506 use_version_lib: true,
507 include_build_directory: false,
508}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000509 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000510 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammerbaced712022-09-16 09:01:29 -0400511 "use_version_lib": "True",
512 "implementation_whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500513 }),
514 },
515 })
516}
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000517
518func TestCcLibrarySharedStubs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
520 Description: "cc_library_shared stubs",
521 ModuleTypeUnderTest: "cc_library_shared",
522 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
523 Dir: "foo/bar",
524 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000525 "foo/bar/Android.bp": `
526cc_library_shared {
527 name: "a",
528 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
529 bazel_module: { bp2build_available: true },
530 include_build_directory: false,
531}
532`,
533 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000534 Blueprint: soongCcLibraryPreamble,
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000535 ExpectedBazelTargets: []string{makeCcStubSuiteTargets("a", AttrNameToString{
536 "soname": `"a.so"`,
537 "source_library": `":a"`,
538 "stubs_symbol_file": `"a.map.txt"`,
539 "stubs_versions": `[
Trevor Radcliffe087af542022-09-16 15:36:10 +0000540 "28",
541 "29",
542 "current",
543 ]`,
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000544 }),
545 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -0800546 "has_stubs": `True`,
547 "stubs_symbol_file": `"a.map.txt"`,
Trevor Radcliffe087af542022-09-16 15:36:10 +0000548 }),
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000549 },
Trevor Radcliffef19d8a72022-09-20 23:04:39 +0000550 })
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000551}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000552
553func TestCcLibrarySharedSystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000554 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
555 Description: "cc_library_shared system_shared_libs empty shared default",
556 ModuleTypeUnderTest: "cc_library_shared",
557 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
558 Blueprint: soongCcLibrarySharedPreamble + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux126bd582022-04-21 15:19:27 +0000559cc_defaults {
560 name: "empty_defaults",
561 shared: {
562 system_shared_libs: [],
563 },
564 include_build_directory: false,
565}
566cc_library_shared {
567 name: "empty",
568 defaults: ["empty_defaults"],
569}
570`,
Alixe06d75b2022-08-31 18:28:19 +0000571 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 +0000572 "system_dynamic_deps": "[]",
573 })},
574 })
575}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000576
577func TestCcLibrarySharedConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
579 Description: "cc_library_shared with lex files",
580 ModuleTypeUnderTest: "cc_library_shared",
581 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
582 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000583 "foo.c": "",
584 "bar.cc": "",
585 "foo1.l": "",
586 "bar1.ll": "",
587 "foo2.l": "",
588 "bar2.ll": "",
589 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000590 Blueprint: `cc_library_shared {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000591 name: "foo_lib",
592 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
593 lex: { flags: ["--foo_flags"] },
594 include_build_directory: false,
595 bazel_module: { bp2build_available: true },
596}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000597 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000598 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000599 "srcs": `[
600 "foo1.l",
601 "foo2.l",
602 ]`,
603 "lexopts": `["--foo_flags"]`,
604 }),
Alixe06d75b2022-08-31 18:28:19 +0000605 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000606 "srcs": `[
607 "bar1.ll",
608 "bar2.ll",
609 ]`,
610 "lexopts": `["--foo_flags"]`,
611 }),
Alixe06d75b2022-08-31 18:28:19 +0000612 MakeBazelTarget("cc_library_shared", "foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000613 "srcs": `[
614 "bar.cc",
615 ":foo_lib_genlex_ll",
616 ]`,
617 "srcs_c": `[
618 "foo.c",
619 ":foo_lib_genlex_l",
620 ]`,
621 }),
622 },
623 })
624}
Alix1be00d42022-05-16 22:56:04 +0000625
626func TestCcLibrarySharedClangUnknownFlags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000627 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
628 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Alix1be00d42022-05-16 22:56:04 +0000629 name: "foo",
630 conlyflags: ["-a", "-finline-functions"],
631 cflags: ["-b","-finline-functions"],
632 cppflags: ["-c", "-finline-functions"],
633 ldflags: ["-d","-finline-functions", "-e"],
634 include_build_directory: false,
635}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000636 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000637 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alix1be00d42022-05-16 22:56:04 +0000638 "conlyflags": `["-a"]`,
639 "copts": `["-b"]`,
640 "cppflags": `["-c"]`,
641 "linkopts": `[
642 "-d",
643 "-e",
644 ]`,
645 }),
646 },
647 })
648}
649
650func TestCCLibraryFlagSpaceSplitting(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000651 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
652 Blueprint: soongCcProtoPreamble + `cc_library_shared {
Alix1be00d42022-05-16 22:56:04 +0000653 name: "foo",
654 conlyflags: [ "-include header.h"],
655 cflags: ["-include header.h"],
656 cppflags: ["-include header.h"],
657 version_script: "version_script",
658 include_build_directory: false,
659}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000660 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000661 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alix1be00d42022-05-16 22:56:04 +0000662 "additional_linker_inputs": `["version_script"]`,
663 "conlyflags": `[
664 "-include",
665 "header.h",
666 ]`,
667 "copts": `[
668 "-include",
669 "header.h",
670 ]`,
671 "cppflags": `[
672 "-include",
673 "header.h",
674 ]`,
675 "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
676 }),
677 },
678 })
679}
Cole Faust6b29f592022-08-09 09:50:56 -0700680
681func TestCCLibrarySharedRuntimeDeps(t *testing.T) {
682 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
683 Blueprint: `cc_library_shared {
684 name: "bar",
685}
686
687cc_library_shared {
688 name: "foo",
689 runtime_libs: ["foo"],
690}`,
691 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000692 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -0700693 "local_includes": `["."]`,
694 }),
Alixe06d75b2022-08-31 18:28:19 +0000695 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -0700696 "runtime_deps": `[":foo"]`,
697 "local_includes": `["."]`,
698 }),
699 },
700 })
701}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500702
703func TestCcLibrarySharedEmptySuffix(t *testing.T) {
704 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
705 Description: "cc_library_shared with empty suffix",
706 Filesystem: map[string]string{
707 "foo.c": "",
708 },
709 Blueprint: soongCcLibrarySharedPreamble + `
710cc_library_shared {
711 name: "foo_shared",
712 suffix: "",
713 srcs: ["foo.c"],
714 include_build_directory: false,
715}`,
716 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000717 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500718 "srcs_c": `["foo.c"]`,
719 "suffix": `""`,
720 }),
721 },
722 })
723}
724
725func TestCcLibrarySharedSuffix(t *testing.T) {
726 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
727 Description: "cc_library_shared with suffix",
728 Filesystem: map[string]string{
729 "foo.c": "",
730 },
731 Blueprint: soongCcLibrarySharedPreamble + `
732cc_library_shared {
733 name: "foo_shared",
734 suffix: "-suf",
735 srcs: ["foo.c"],
736 include_build_directory: false,
737}`,
738 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000739 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500740 "srcs_c": `["foo.c"]`,
741 "suffix": `"-suf"`,
742 }),
743 },
744 })
745}
746
747func TestCcLibrarySharedArchVariantSuffix(t *testing.T) {
748 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
749 Description: "cc_library_shared with arch-variant suffix",
750 Filesystem: map[string]string{
751 "foo.c": "",
752 },
753 Blueprint: soongCcLibrarySharedPreamble + `
754cc_library_shared {
755 name: "foo_shared",
756 arch: {
757 arm64: { suffix: "-64" },
758 arm: { suffix: "-32" },
759 },
760 srcs: ["foo.c"],
761 include_build_directory: false,
762}`,
763 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000764 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500765 "srcs_c": `["foo.c"]`,
766 "suffix": `select({
767 "//build/bazel/platforms/arch:arm": "-32",
768 "//build/bazel/platforms/arch:arm64": "-64",
769 "//conditions:default": None,
770 })`,
771 }),
772 },
773 })
774}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000775
776func TestCcLibrarySharedWithSyspropSrcs(t *testing.T) {
777 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
778 Description: "cc_library_shared with sysprop sources",
779 Blueprint: `
780cc_library_shared {
781 name: "foo",
782 srcs: [
783 "bar.sysprop",
784 "baz.sysprop",
785 "blah.cpp",
786 ],
787 min_sdk_version: "5",
788}`,
789 ExpectedBazelTargets: []string{
790 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
791 "srcs": `[
792 "bar.sysprop",
793 "baz.sysprop",
794 ]`,
795 }),
796 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
797 "dep": `":foo_sysprop_library"`,
798 "min_sdk_version": `"5"`,
799 }),
800 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
801 "srcs": `["blah.cpp"]`,
802 "local_includes": `["."]`,
803 "min_sdk_version": `"5"`,
804 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
805 }),
806 },
807 })
808}
809
810func TestCcLibrarySharedWithSyspropSrcsSomeConfigs(t *testing.T) {
811 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
812 Description: "cc_library_shared with sysprop sources in some configs but not others",
813 Blueprint: `
814cc_library_shared {
815 name: "foo",
816 srcs: [
817 "blah.cpp",
818 ],
819 target: {
820 android: {
821 srcs: ["bar.sysprop"],
822 },
823 },
824 min_sdk_version: "5",
825}`,
826 ExpectedBazelTargets: []string{
827 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
828 "srcs": `select({
829 "//build/bazel/platforms/os:android": ["bar.sysprop"],
830 "//conditions:default": [],
831 })`,
832 }),
833 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
834 "dep": `":foo_sysprop_library"`,
835 "min_sdk_version": `"5"`,
836 }),
837 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
838 "srcs": `["blah.cpp"]`,
839 "local_includes": `["."]`,
840 "min_sdk_version": `"5"`,
841 "whole_archive_deps": `select({
842 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
843 "//conditions:default": [],
844 })`,
845 }),
846 },
847 })
848}
Yu Liu56ccb1a2022-11-12 10:47:07 -0800849
850func TestCcLibrarySharedHeaderAbiChecker(t *testing.T) {
851 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
852 Description: "cc_library_shared with header abi checker",
853 Blueprint: `cc_library_shared {
854 name: "foo",
855 header_abi_checker: {
856 enabled: true,
857 symbol_file: "a.map.txt",
858 exclude_symbol_versions: [
859 "29",
860 "30",
861 ],
862 exclude_symbol_tags: [
863 "tag1",
864 "tag2",
865 ],
866 check_all_apis: true,
867 diff_flags: ["-allow-adding-removing-weak-symbols"],
868 },
869 include_build_directory: false,
870}`,
871 ExpectedBazelTargets: []string{
872 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
873 "abi_checker_enabled": `True`,
874 "abi_checker_symbol_file": `"a.map.txt"`,
875 "abi_checker_exclude_symbol_versions": `[
876 "29",
877 "30",
878 ]`,
879 "abi_checker_exclude_symbol_tags": `[
880 "tag1",
881 "tag2",
882 ]`,
883 "abi_checker_check_all_apis": `True`,
884 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
885 }),
886 },
887 })
888}