blob: 64f368eccda3ff62dc421721539d1b37ae09836b [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
43 (&tc).moduleTypeUnderTestBp2BuildMutator = cc.CcLibrarySharedBp2Build
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000044 runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
45}
46
47func TestCcLibrarySharedSimple(t *testing.T) {
48 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050049 description: "cc_library_shared simple overall test",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000050 filesystem: map[string]string{
51 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
52 "include_dir_1/include_dir_1_a.h": "",
53 "include_dir_1/include_dir_1_b.h": "",
54 "include_dir_2/include_dir_2_a.h": "",
55 "include_dir_2/include_dir_2_b.h": "",
56 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
57 "local_include_dir_1/local_include_dir_1_a.h": "",
58 "local_include_dir_1/local_include_dir_1_b.h": "",
59 "local_include_dir_2/local_include_dir_2_a.h": "",
60 "local_include_dir_2/local_include_dir_2_b.h": "",
61 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
62 "export_include_dir_1/export_include_dir_1_a.h": "",
63 "export_include_dir_1/export_include_dir_1_b.h": "",
64 "export_include_dir_2/export_include_dir_2_a.h": "",
65 "export_include_dir_2/export_include_dir_2_b.h": "",
66 // NOTE: Soong implicitly includes headers in the current directory
67 "implicit_include_1.h": "",
68 "implicit_include_2.h": "",
69 },
70 blueprint: soongCcLibrarySharedPreamble + `
71cc_library_headers {
72 name: "header_lib_1",
73 export_include_dirs: ["header_lib_1"],
74 bazel_module: { bp2build_available: false },
75}
76
77cc_library_headers {
78 name: "header_lib_2",
79 export_include_dirs: ["header_lib_2"],
80 bazel_module: { bp2build_available: false },
81}
82
83cc_library_shared {
84 name: "shared_lib_1",
85 srcs: ["shared_lib_1.cc"],
86 bazel_module: { bp2build_available: false },
87}
88
89cc_library_shared {
90 name: "shared_lib_2",
91 srcs: ["shared_lib_2.cc"],
92 bazel_module: { bp2build_available: false },
93}
94
95cc_library_static {
96 name: "whole_static_lib_1",
97 srcs: ["whole_static_lib_1.cc"],
98 bazel_module: { bp2build_available: false },
99}
100
101cc_library_static {
102 name: "whole_static_lib_2",
103 srcs: ["whole_static_lib_2.cc"],
104 bazel_module: { bp2build_available: false },
105}
106
107cc_library_shared {
108 name: "foo_shared",
109 srcs: [
110 "foo_shared1.cc",
111 "foo_shared2.cc",
112 ],
113 cflags: [
114 "-Dflag1",
115 "-Dflag2"
116 ],
117 shared_libs: [
118 "shared_lib_1",
119 "shared_lib_2"
120 ],
121 whole_static_libs: [
122 "whole_static_lib_1",
123 "whole_static_lib_2"
124 ],
125 include_dirs: [
126 "include_dir_1",
127 "include_dir_2",
128 ],
129 local_include_dirs: [
130 "local_include_dir_1",
131 "local_include_dir_2",
132 ],
133 export_include_dirs: [
134 "export_include_dir_1",
135 "export_include_dir_2"
136 ],
137 header_libs: [
138 "header_lib_1",
139 "header_lib_2"
140 ],
141
142 // TODO: Also support export_header_lib_headers
143}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500144 expectedBazelTargets: []string{
145 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
146 "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 ]`,
179 }),
180 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000181 })
182}
183
184func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
185 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500186 description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
187 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000188 blueprint: soongCcLibrarySharedPreamble + `
189cc_library_static {
190 name: "static_dep",
191 bazel_module: { bp2build_available: false },
192}
193cc_library_shared {
194 name: "shared_dep",
195 bazel_module: { bp2build_available: false },
196}
197cc_library_shared {
198 name: "foo_shared",
199 arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
200 include_build_directory: false,
201}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500202 expectedBazelTargets: []string{
203 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
204 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000205 "//build/bazel/platforms/arch:arm64": [":shared_dep"],
206 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500207 })`,
208 "whole_archive_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000209 "//build/bazel/platforms/arch:arm64": [":static_dep"],
210 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500211 })`,
212 }),
213 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000214 })
215}
216
217func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500218 runCcLibrarySharedTestCase(t, bp2buildTestCase{
219 description: "cc_library_shared os-specific shared_libs",
220 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000221 blueprint: soongCcLibrarySharedPreamble + `
222cc_library_shared {
223 name: "shared_dep",
224 bazel_module: { bp2build_available: false },
225}
226cc_library_shared {
227 name: "foo_shared",
228 target: { android: { shared_libs: ["shared_dep"], } },
229 include_build_directory: false,
230}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500231 expectedBazelTargets: []string{
232 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
233 "implementation_dynamic_deps": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000234 "//build/bazel/platforms/os:android": [":shared_dep"],
235 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500236 })`,
237 }),
238 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000239 })
240}
241
242func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
243 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500244 description: "cc_library_shared base, arch, and os-specific shared_libs",
245 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000246 blueprint: soongCcLibrarySharedPreamble + `
247cc_library_shared {
248 name: "shared_dep",
249 bazel_module: { bp2build_available: false },
250}
251cc_library_shared {
252 name: "shared_dep2",
253 bazel_module: { bp2build_available: false },
254}
255cc_library_shared {
256 name: "shared_dep3",
257 bazel_module: { bp2build_available: false },
258}
259cc_library_shared {
260 name: "foo_shared",
261 shared_libs: ["shared_dep"],
262 target: { android: { shared_libs: ["shared_dep2"] } },
263 arch: { arm64: { shared_libs: ["shared_dep3"] } },
264 include_build_directory: false,
265}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500266 expectedBazelTargets: []string{
267 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
268 "implementation_dynamic_deps": `[":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000269 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
270 "//conditions:default": [],
271 }) + select({
272 "//build/bazel/platforms/os:android": [":shared_dep2"],
273 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500274 })`,
275 }),
276 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000277 })
278}
279
280func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
281 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500282 description: "cc_library_shared simple exclude_srcs",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000283 filesystem: map[string]string{
284 "common.c": "",
285 "foo-a.c": "",
286 "foo-excluded.c": "",
287 },
288 blueprint: soongCcLibrarySharedPreamble + `
289cc_library_shared {
290 name: "foo_shared",
291 srcs: ["common.c", "foo-*.c"],
292 exclude_srcs: ["foo-excluded.c"],
293 include_build_directory: false,
294}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500295 expectedBazelTargets: []string{
296 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
297 "srcs_c": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000298 "common.c",
299 "foo-a.c",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500300 ]`,
301 }),
302 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000303 })
304}
305
306func TestCcLibrarySharedStrip(t *testing.T) {
307 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500308 description: "cc_library_shared stripping",
309 filesystem: map[string]string{},
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000310 blueprint: soongCcLibrarySharedPreamble + `
311cc_library_shared {
312 name: "foo_shared",
313 strip: {
314 keep_symbols: false,
315 keep_symbols_and_debug_frame: true,
316 keep_symbols_list: ["sym", "sym2"],
317 all: true,
318 none: false,
319 },
320 include_build_directory: false,
321}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500322 expectedBazelTargets: []string{
323 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
324 "strip": `{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000325 "all": True,
326 "keep_symbols": False,
327 "keep_symbols_and_debug_frame": True,
328 "keep_symbols_list": [
329 "sym",
330 "sym2",
331 ],
332 "none": False,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500333 }`,
334 }),
335 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000336 })
337}
338
339func TestCcLibrarySharedVersionScript(t *testing.T) {
340 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500341 description: "cc_library_shared version script",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000342 filesystem: map[string]string{
343 "version_script": "",
344 },
345 blueprint: soongCcLibrarySharedPreamble + `
346cc_library_shared {
347 name: "foo_shared",
348 version_script: "version_script",
349 include_build_directory: false,
350}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500351 expectedBazelTargets: []string{
352 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
353 "additional_linker_inputs": `["version_script"]`,
354 "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
355 }),
356 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000357 })
358}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000359
360func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
361 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500362 description: "cc_library_shared - nocrt: true emits attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000363 filesystem: map[string]string{
364 "impl.cpp": "",
365 },
366 blueprint: soongCcLibraryPreamble + `
367cc_library_shared {
368 name: "foo_shared",
369 srcs: ["impl.cpp"],
370 nocrt: true,
371 include_build_directory: false,
372}
373`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500374 expectedBazelTargets: []string{
375 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
376 "link_crt": `False`,
377 "srcs": `["impl.cpp"]`,
378 }),
379 },
380 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000381}
382
383func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
384 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500385 description: "cc_library_shared - nocrt: false doesn't emit attribute",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000386 filesystem: map[string]string{
387 "impl.cpp": "",
388 },
389 blueprint: soongCcLibraryPreamble + `
390cc_library_shared {
391 name: "foo_shared",
392 srcs: ["impl.cpp"],
393 nocrt: false,
394 include_build_directory: false,
395}
396`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500397 expectedBazelTargets: []string{
398 makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
399 "srcs": `["impl.cpp"]`,
400 }),
401 },
402 })
Jingwen Chen6ada5892021-09-17 11:38:09 +0000403}
404
405func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
406 runCcLibrarySharedTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500407 description: "cc_library_shared - nocrt in select",
Jingwen Chen6ada5892021-09-17 11:38:09 +0000408 filesystem: map[string]string{
409 "impl.cpp": "",
410 },
411 blueprint: soongCcLibraryPreamble + `
412cc_library_shared {
413 name: "foo_shared",
414 srcs: ["impl.cpp"],
415 arch: {
416 arm: {
417 nocrt: true,
418 },
419 x86: {
420 nocrt: false,
421 },
422 },
423 include_build_directory: false,
424}
425`,
426 expectedErr: fmt.Errorf("Android.bp:16:1: module \"foo_shared\": nocrt is not supported for arch variants"),
427 })
428}
Liz Kammer12615db2021-09-28 09:19:17 -0400429
430func TestCcLibrarySharedProto(t *testing.T) {
431 runCcLibrarySharedTestCase(t, bp2buildTestCase{
432 blueprint: soongCcProtoPreamble + `cc_library_shared {
433 name: "foo",
434 srcs: ["foo.proto"],
435 proto: {
436 canonical_path_from_root: false,
437 export_proto_headers: true,
438 },
439 include_build_directory: false,
440}`,
441 expectedBazelTargets: []string{
442 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
443 "srcs": `["foo.proto"]`,
444 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
445 "deps": `[":foo_proto"]`,
446 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
447 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
448 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
449 }),
450 },
451 })
452}
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500453
454func TestCcLibrarySharedUseVersionLib(t *testing.T) {
455 runCcLibrarySharedTestCase(t, bp2buildTestCase{
456 blueprint: soongCcProtoPreamble + `cc_library_shared {
457 name: "foo",
458 use_version_lib: true,
459 include_build_directory: false,
460}`,
461 expectedBazelTargets: []string{
462 makeBazelTarget("cc_library_shared", "foo", attrNameToString{
463 "use_version_lib": "True",
464 }),
465 },
466 })
467}