blob: 0f6765320fca5e3ecf93d0bf0915672932c59014 [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)
33 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
34 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
35 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer12615db2021-09-28 09:19:17 -040036 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000037}
38
39func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) {
40 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050041 (&tc).moduleTypeUnderTest = "cc_library_shared"
42 (&tc).moduleTypeUnderTestFactory = cc.LibrarySharedFactory
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000043 runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
44}
45
46func TestCcLibrarySharedSimple(t *testing.T) {
47 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050048 description: "cc_library_shared simple overall test",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000049 filesystem: map[string]string{
50 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
51 "include_dir_1/include_dir_1_a.h": "",
52 "include_dir_1/include_dir_1_b.h": "",
53 "include_dir_2/include_dir_2_a.h": "",
54 "include_dir_2/include_dir_2_b.h": "",
55 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
56 "local_include_dir_1/local_include_dir_1_a.h": "",
57 "local_include_dir_1/local_include_dir_1_b.h": "",
58 "local_include_dir_2/local_include_dir_2_a.h": "",
59 "local_include_dir_2/local_include_dir_2_b.h": "",
60 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
61 "export_include_dir_1/export_include_dir_1_a.h": "",
62 "export_include_dir_1/export_include_dir_1_b.h": "",
63 "export_include_dir_2/export_include_dir_2_a.h": "",
64 "export_include_dir_2/export_include_dir_2_b.h": "",
65 // NOTE: Soong implicitly includes headers in the current directory
66 "implicit_include_1.h": "",
67 "implicit_include_2.h": "",
68 },
69 blueprint: soongCcLibrarySharedPreamble + `
70cc_library_headers {
71 name: "header_lib_1",
72 export_include_dirs: ["header_lib_1"],
73 bazel_module: { bp2build_available: false },
74}
75
76cc_library_headers {
77 name: "header_lib_2",
78 export_include_dirs: ["header_lib_2"],
79 bazel_module: { bp2build_available: false },
80}
81
82cc_library_shared {
83 name: "shared_lib_1",
84 srcs: ["shared_lib_1.cc"],
85 bazel_module: { bp2build_available: false },
86}
87
88cc_library_shared {
89 name: "shared_lib_2",
90 srcs: ["shared_lib_2.cc"],
91 bazel_module: { bp2build_available: false },
92}
93
94cc_library_static {
95 name: "whole_static_lib_1",
96 srcs: ["whole_static_lib_1.cc"],
97 bazel_module: { bp2build_available: false },
98}
99
100cc_library_static {
101 name: "whole_static_lib_2",
102 srcs: ["whole_static_lib_2.cc"],
103 bazel_module: { bp2build_available: false },
104}
105
106cc_library_shared {
107 name: "foo_shared",
108 srcs: [
109 "foo_shared1.cc",
110 "foo_shared2.cc",
111 ],
112 cflags: [
113 "-Dflag1",
114 "-Dflag2"
115 ],
116 shared_libs: [
117 "shared_lib_1",
118 "shared_lib_2"
119 ],
120 whole_static_libs: [
121 "whole_static_lib_1",
122 "whole_static_lib_2"
123 ],
124 include_dirs: [
125 "include_dir_1",
126 "include_dir_2",
127 ],
128 local_include_dirs: [
129 "local_include_dir_1",
130 "local_include_dir_2",
131 ],
132 export_include_dirs: [
133 "export_include_dir_1",
134 "export_include_dir_2"
135 ],
136 header_libs: [
137 "header_lib_1",
138 "header_lib_2"
139 ],
140
141 // TODO: Also support export_header_lib_headers
142}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143 expectedBazelTargets: []string{
144 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
145 "absolute_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000146 "include_dir_1",
147 "include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500148 ]`,
149 "copts": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000150 "-Dflag1",
151 "-Dflag2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500152 ]`,
153 "export_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000154 "export_include_dir_1",
155 "export_include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500156 ]`,
157 "implementation_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000158 ":header_lib_1",
159 ":header_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500160 ]`,
161 "implementation_dynamic_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400162 ":shared_lib_1",
163 ":shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500164 ]`,
165 "local_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000166 "local_include_dir_1",
167 "local_include_dir_2",
168 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500169 ]`,
170 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000171 "foo_shared1.cc",
172 "foo_shared2.cc",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500173 ]`,
174 "whole_archive_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000175 ":whole_static_lib_1",
176 ":whole_static_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 ]`,
178 }),
179 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000180 })
181}
182
183func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
184 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500185 description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
186 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000187 blueprint: soongCcLibrarySharedPreamble + `
188cc_library_static {
189 name: "static_dep",
190 bazel_module: { bp2build_available: false },
191}
192cc_library_shared {
193 name: "shared_dep",
194 bazel_module: { bp2build_available: false },
195}
196cc_library_shared {
197 name: "foo_shared",
198 arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
199 include_build_directory: false,
200}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500201 expectedBazelTargets: []string{
202 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
203 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000204 "//build/bazel/platforms/arch:arm64": [":shared_dep"],
205 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500206 })`,
207 "whole_archive_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000208 "//build/bazel/platforms/arch:arm64": [":static_dep"],
209 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500210 })`,
211 }),
212 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000213 })
214}
215
216func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500217 runCcLibrarySharedTestCase(t, bp2buildTestCase{
218 description: "cc_library_shared os-specific shared_libs",
219 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000220 blueprint: soongCcLibrarySharedPreamble + `
221cc_library_shared {
222 name: "shared_dep",
223 bazel_module: { bp2build_available: false },
224}
225cc_library_shared {
226 name: "foo_shared",
227 target: { android: { shared_libs: ["shared_dep"], } },
228 include_build_directory: false,
229}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 expectedBazelTargets: []string{
231 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
232 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000233 "//build/bazel/platforms/os:android": [":shared_dep"],
234 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500235 })`,
236 }),
237 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000238 })
239}
240
241func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
242 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500243 description: "cc_library_shared base, arch, and os-specific shared_libs",
244 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000245 blueprint: soongCcLibrarySharedPreamble + `
246cc_library_shared {
247 name: "shared_dep",
248 bazel_module: { bp2build_available: false },
249}
250cc_library_shared {
251 name: "shared_dep2",
252 bazel_module: { bp2build_available: false },
253}
254cc_library_shared {
255 name: "shared_dep3",
256 bazel_module: { bp2build_available: false },
257}
258cc_library_shared {
259 name: "foo_shared",
260 shared_libs: ["shared_dep"],
261 target: { android: { shared_libs: ["shared_dep2"] } },
262 arch: { arm64: { shared_libs: ["shared_dep3"] } },
263 include_build_directory: false,
264}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500265 expectedBazelTargets: []string{
266 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
267 "implementation_dynamic_deps": `[":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000268 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
269 "//conditions:default": [],
270 }) + select({
271 "//build/bazel/platforms/os:android": [":shared_dep2"],
272 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500273 })`,
274 }),
275 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000276 })
277}
278
279func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
280 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500281 description: "cc_library_shared simple exclude_srcs",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000282 filesystem: map[string]string{
283 "common.c": "",
284 "foo-a.c": "",
285 "foo-excluded.c": "",
286 },
287 blueprint: soongCcLibrarySharedPreamble + `
288cc_library_shared {
289 name: "foo_shared",
290 srcs: ["common.c", "foo-*.c"],
291 exclude_srcs: ["foo-excluded.c"],
292 include_build_directory: false,
293}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 expectedBazelTargets: []string{
295 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
296 "srcs_c": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000297 "common.c",
298 "foo-a.c",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500299 ]`,
300 }),
301 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000302 })
303}
304
305func TestCcLibrarySharedStrip(t *testing.T) {
306 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500307 description: "cc_library_shared stripping",
308 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000309 blueprint: soongCcLibrarySharedPreamble + `
310cc_library_shared {
311 name: "foo_shared",
312 strip: {
313 keep_symbols: false,
314 keep_symbols_and_debug_frame: true,
315 keep_symbols_list: ["sym", "sym2"],
316 all: true,
317 none: false,
318 },
319 include_build_directory: false,
320}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500321 expectedBazelTargets: []string{
322 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
323 "strip": `{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000324 "all": True,
325 "keep_symbols": False,
326 "keep_symbols_and_debug_frame": True,
327 "keep_symbols_list": [
328 "sym",
329 "sym2",
330 ],
331 "none": False,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500332 }`,
333 }),
334 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000335 })
336}
337
338func TestCcLibrarySharedVersionScript(t *testing.T) {
339 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500340 description: "cc_library_shared version script",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000341 filesystem: map[string]string{
342 "version_script": "",
343 },
344 blueprint: soongCcLibrarySharedPreamble + `
345cc_library_shared {
346 name: "foo_shared",
347 version_script: "version_script",
348 include_build_directory: false,
349}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500350 expectedBazelTargets: []string{
351 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
352 "additional_linker_inputs": `["version_script"]`,
353 "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
354 }),
355 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000356 })
357}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000358
359func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
360 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500361 description: "cc_library_shared - nocrt: true emits attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000362 filesystem: map[string]string{
363 "impl.cpp": "",
364 },
365 blueprint: soongCcLibraryPreamble + `
366cc_library_shared {
367 name: "foo_shared",
368 srcs: ["impl.cpp"],
369 nocrt: true,
370 include_build_directory: false,
371}
372`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500373 expectedBazelTargets: []string{
374 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
375 "link_crt": `False`,
376 "srcs": `["impl.cpp"]`,
377 }),
378 },
379 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000380}
381
382func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
383 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500384 description: "cc_library_shared - nocrt: false doesn't emit attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000385 filesystem: map[string]string{
386 "impl.cpp": "",
387 },
388 blueprint: soongCcLibraryPreamble + `
389cc_library_shared {
390 name: "foo_shared",
391 srcs: ["impl.cpp"],
392 nocrt: false,
393 include_build_directory: false,
394}
395`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500396 expectedBazelTargets: []string{
397 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
398 "srcs": `["impl.cpp"]`,
399 }),
400 },
401 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000402}
403
404func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
405 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500406 description: "cc_library_shared - nocrt in select",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000407 filesystem: map[string]string{
408 "impl.cpp": "",
409 },
410 blueprint: soongCcLibraryPreamble + `
411cc_library_shared {
412 name: "foo_shared",
413 srcs: ["impl.cpp"],
414 arch: {
415 arm: {
416 nocrt: true,
417 },
418 x86: {
419 nocrt: false,
420 },
421 },
422 include_build_directory: false,
423}
424`,
425 expectedErr: fmt.Errorf("Android.bp:16:1: module \"foo_shared\": nocrt is not supported for arch variants"),
426 })
427}
Liz Kammer12615db2021-09-28 09:19:17 -0400428
429func TestCcLibrarySharedProto(t *testing.T) {
430 runCcLibrarySharedTestCase(t, bp2buildTestCase{
431 blueprint: soongCcProtoPreamble + `cc_library_shared {
432 name: "foo",
433 srcs: ["foo.proto"],
434 proto: {
435 canonical_path_from_root: false,
436 export_proto_headers: true,
437 },
438 include_build_directory: false,
439}`,
440 expectedBazelTargets: []string{
441 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
442 "srcs": `["foo.proto"]`,
443 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
444 "deps": `[":foo_proto"]`,
445 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
446 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
447 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
448 }),
449 },
450 })
451}
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500452
453func TestCcLibrarySharedUseVersionLib(t *testing.T) {
454 runCcLibrarySharedTestCase(t, bp2buildTestCase{
455 blueprint: soongCcProtoPreamble + `cc_library_shared {
456 name: "foo",
457 use_version_lib: true,
458 include_build_directory: false,
459}`,
460 expectedBazelTargets: []string{
461 makeBazelTarget("cc_library_shared", "foo", attrNameToString{
462 "use_version_lib": "True",
463 }),
464 },
465 })
466}
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000467
468func TestCcLibrarySharedStubs(t *testing.T) {
469 runCcLibrarySharedTestCase(t, bp2buildTestCase{
470 description: "cc_library_shared stubs",
471 moduleTypeUnderTest: "cc_library_shared",
472 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
473 dir: "foo/bar",
474 filesystem: map[string]string{
475 "foo/bar/Android.bp": `
476cc_library_shared {
477 name: "a",
478 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
479 bazel_module: { bp2build_available: true },
480 include_build_directory: false,
481}
482`,
483 },
484 blueprint: soongCcLibraryPreamble,
485 expectedBazelTargets: []string{makeBazelTarget("cc_library_shared", "a", attrNameToString{
486 "stubs_symbol_file": `"a.map.txt"`,
487 "stubs_versions": `[
488 "28",
489 "29",
490 "current",
491 ]`,
492 }),
493 },
494 },
495 )
496}