blob: 7741de34da1e14f6b1a6b13a15d4b152c01ee285 [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
38func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) {
39 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050040 (&tc).moduleTypeUnderTest = "cc_library_shared"
41 (&tc).moduleTypeUnderTestFactory = cc.LibrarySharedFactory
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000042 runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
43}
44
45func TestCcLibrarySharedSimple(t *testing.T) {
46 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050047 description: "cc_library_shared simple overall test",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000048 filesystem: map[string]string{
49 // 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 },
68 blueprint: soongCcLibrarySharedPreamble + `
69cc_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 ],
139
140 // TODO: Also support export_header_lib_headers
141}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500142 expectedBazelTargets: []string{
143 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
144 "absolute_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000145 "include_dir_1",
146 "include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500147 ]`,
148 "copts": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000149 "-Dflag1",
150 "-Dflag2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500151 ]`,
152 "export_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000153 "export_include_dir_1",
154 "export_include_dir_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500155 ]`,
156 "implementation_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000157 ":header_lib_1",
158 ":header_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 ]`,
160 "implementation_dynamic_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400161 ":shared_lib_1",
162 ":shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500163 ]`,
164 "local_includes": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000165 "local_include_dir_1",
166 "local_include_dir_2",
167 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500168 ]`,
169 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000170 "foo_shared1.cc",
171 "foo_shared2.cc",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500172 ]`,
173 "whole_archive_deps": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000174 ":whole_static_lib_1",
175 ":whole_static_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500176 ]`,
177 }),
178 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000179 })
180}
181
182func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
183 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500184 description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
185 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000186 blueprint: soongCcLibrarySharedPreamble + `
187cc_library_static {
188 name: "static_dep",
189 bazel_module: { bp2build_available: false },
190}
191cc_library_shared {
192 name: "shared_dep",
193 bazel_module: { bp2build_available: false },
194}
195cc_library_shared {
196 name: "foo_shared",
197 arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
198 include_build_directory: false,
199}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500200 expectedBazelTargets: []string{
201 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
202 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000203 "//build/bazel/platforms/arch:arm64": [":shared_dep"],
204 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500205 })`,
206 "whole_archive_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": [":static_dep"],
208 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500209 })`,
210 }),
211 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000212 })
213}
214
215func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500216 runCcLibrarySharedTestCase(t, bp2buildTestCase{
217 description: "cc_library_shared os-specific shared_libs",
218 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000219 blueprint: soongCcLibrarySharedPreamble + `
220cc_library_shared {
221 name: "shared_dep",
222 bazel_module: { bp2build_available: false },
223}
224cc_library_shared {
225 name: "foo_shared",
226 target: { android: { shared_libs: ["shared_dep"], } },
227 include_build_directory: false,
228}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500229 expectedBazelTargets: []string{
230 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
231 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000232 "//build/bazel/platforms/os:android": [":shared_dep"],
233 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500234 })`,
235 }),
236 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000237 })
238}
239
240func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
241 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500242 description: "cc_library_shared base, arch, and os-specific shared_libs",
243 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000244 blueprint: soongCcLibrarySharedPreamble + `
245cc_library_shared {
246 name: "shared_dep",
247 bazel_module: { bp2build_available: false },
248}
249cc_library_shared {
250 name: "shared_dep2",
251 bazel_module: { bp2build_available: false },
252}
253cc_library_shared {
254 name: "shared_dep3",
255 bazel_module: { bp2build_available: false },
256}
257cc_library_shared {
258 name: "foo_shared",
259 shared_libs: ["shared_dep"],
260 target: { android: { shared_libs: ["shared_dep2"] } },
261 arch: { arm64: { shared_libs: ["shared_dep3"] } },
262 include_build_directory: false,
263}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500264 expectedBazelTargets: []string{
265 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
266 "implementation_dynamic_deps": `[":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000267 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
268 "//conditions:default": [],
269 }) + select({
270 "//build/bazel/platforms/os:android": [":shared_dep2"],
271 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500272 })`,
273 }),
274 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000275 })
276}
277
278func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
279 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500280 description: "cc_library_shared simple exclude_srcs",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000281 filesystem: map[string]string{
282 "common.c": "",
283 "foo-a.c": "",
284 "foo-excluded.c": "",
285 },
286 blueprint: soongCcLibrarySharedPreamble + `
287cc_library_shared {
288 name: "foo_shared",
289 srcs: ["common.c", "foo-*.c"],
290 exclude_srcs: ["foo-excluded.c"],
291 include_build_directory: false,
292}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500293 expectedBazelTargets: []string{
294 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
295 "srcs_c": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000296 "common.c",
297 "foo-a.c",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500298 ]`,
299 }),
300 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000301 })
302}
303
304func TestCcLibrarySharedStrip(t *testing.T) {
305 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500306 description: "cc_library_shared stripping",
307 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000308 blueprint: soongCcLibrarySharedPreamble + `
309cc_library_shared {
310 name: "foo_shared",
311 strip: {
312 keep_symbols: false,
313 keep_symbols_and_debug_frame: true,
314 keep_symbols_list: ["sym", "sym2"],
315 all: true,
316 none: false,
317 },
318 include_build_directory: false,
319}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500320 expectedBazelTargets: []string{
321 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
322 "strip": `{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000323 "all": True,
324 "keep_symbols": False,
325 "keep_symbols_and_debug_frame": True,
326 "keep_symbols_list": [
327 "sym",
328 "sym2",
329 ],
330 "none": False,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500331 }`,
332 }),
333 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000334 })
335}
336
337func TestCcLibrarySharedVersionScript(t *testing.T) {
338 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500339 description: "cc_library_shared version script",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000340 filesystem: map[string]string{
341 "version_script": "",
342 },
343 blueprint: soongCcLibrarySharedPreamble + `
344cc_library_shared {
345 name: "foo_shared",
346 version_script: "version_script",
347 include_build_directory: false,
348}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 expectedBazelTargets: []string{
350 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
351 "additional_linker_inputs": `["version_script"]`,
352 "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
353 }),
354 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000355 })
356}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000357
358func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
359 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500360 description: "cc_library_shared - nocrt: true emits attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000361 filesystem: map[string]string{
362 "impl.cpp": "",
363 },
364 blueprint: soongCcLibraryPreamble + `
365cc_library_shared {
366 name: "foo_shared",
367 srcs: ["impl.cpp"],
368 nocrt: true,
369 include_build_directory: false,
370}
371`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500372 expectedBazelTargets: []string{
373 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
374 "link_crt": `False`,
375 "srcs": `["impl.cpp"]`,
376 }),
377 },
378 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000379}
380
381func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
382 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500383 description: "cc_library_shared - nocrt: false doesn't emit attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000384 filesystem: map[string]string{
385 "impl.cpp": "",
386 },
387 blueprint: soongCcLibraryPreamble + `
388cc_library_shared {
389 name: "foo_shared",
390 srcs: ["impl.cpp"],
391 nocrt: false,
392 include_build_directory: false,
393}
394`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500395 expectedBazelTargets: []string{
396 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
397 "srcs": `["impl.cpp"]`,
398 }),
399 },
400 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000401}
402
403func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
404 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500405 description: "cc_library_shared - nocrt in select",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000406 filesystem: map[string]string{
407 "impl.cpp": "",
408 },
409 blueprint: soongCcLibraryPreamble + `
410cc_library_shared {
411 name: "foo_shared",
412 srcs: ["impl.cpp"],
413 arch: {
414 arm: {
415 nocrt: true,
416 },
417 x86: {
418 nocrt: false,
419 },
420 },
421 include_build_directory: false,
422}
423`,
Liz Kammer718eb272022-01-07 10:53:37 -0500424 expectedErr: fmt.Errorf("module \"foo_shared\": nocrt is not supported for arch variants"),
Jingwen Chen6ada5892021-09-17 11:38:09 +0000425 })
426}
Liz Kammer12615db2021-09-28 09:19:17 -0400427
428func TestCcLibrarySharedProto(t *testing.T) {
429 runCcLibrarySharedTestCase(t, bp2buildTestCase{
430 blueprint: soongCcProtoPreamble + `cc_library_shared {
431 name: "foo",
432 srcs: ["foo.proto"],
433 proto: {
434 canonical_path_from_root: false,
435 export_proto_headers: true,
436 },
437 include_build_directory: false,
438}`,
439 expectedBazelTargets: []string{
440 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
441 "srcs": `["foo.proto"]`,
442 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
443 "deps": `[":foo_proto"]`,
444 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
445 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
446 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
447 }),
448 },
449 })
450}
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500451
452func TestCcLibrarySharedUseVersionLib(t *testing.T) {
453 runCcLibrarySharedTestCase(t, bp2buildTestCase{
454 blueprint: soongCcProtoPreamble + `cc_library_shared {
455 name: "foo",
456 use_version_lib: true,
457 include_build_directory: false,
458}`,
459 expectedBazelTargets: []string{
460 makeBazelTarget("cc_library_shared", "foo", attrNameToString{
461 "use_version_lib": "True",
462 }),
463 },
464 })
465}