blob: bb15776eb5e0d9a483945eedc36864f4d756f8b0 [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)
36}
37
38func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) {
39 t.Helper()
40 runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
41}
42
43func TestCcLibrarySharedSimple(t *testing.T) {
44 runCcLibrarySharedTestCase(t, bp2buildTestCase{
45 description: "cc_library_shared simple overall test",
46 moduleTypeUnderTest: "cc_library_shared",
47 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
48 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
49 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}`,
143 expectedBazelTargets: []string{`cc_library_shared(
144 name = "foo_shared",
145 absolute_includes = [
146 "include_dir_1",
147 "include_dir_2",
148 ],
149 copts = [
150 "-Dflag1",
151 "-Dflag2",
152 ],
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000153 export_includes = [
154 "export_include_dir_1",
155 "export_include_dir_2",
156 ],
157 implementation_deps = [
158 ":header_lib_1",
159 ":header_lib_2",
160 ],
Liz Kammer7a210ac2021-09-22 15:52:58 -0400161 implementation_dynamic_deps = [
162 ":shared_lib_1",
163 ":shared_lib_2",
164 ],
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000165 local_includes = [
166 "local_include_dir_1",
167 "local_include_dir_2",
168 ".",
169 ],
170 srcs = [
171 "foo_shared1.cc",
172 "foo_shared2.cc",
173 ],
174 whole_archive_deps = [
175 ":whole_static_lib_1",
176 ":whole_static_lib_2",
177 ],
178)`},
179 })
180}
181
182func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
183 runCcLibrarySharedTestCase(t, bp2buildTestCase{
184 description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
185 moduleTypeUnderTest: "cc_library_shared",
186 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
187 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
188 filesystem: map[string]string{},
189 blueprint: soongCcLibrarySharedPreamble + `
190cc_library_static {
191 name: "static_dep",
192 bazel_module: { bp2build_available: false },
193}
194cc_library_shared {
195 name: "shared_dep",
196 bazel_module: { bp2build_available: false },
197}
198cc_library_shared {
199 name: "foo_shared",
200 arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
201 include_build_directory: false,
202}`,
203 expectedBazelTargets: []string{`cc_library_shared(
204 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400205 implementation_dynamic_deps = select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000206 "//build/bazel/platforms/arch:arm64": [":shared_dep"],
207 "//conditions:default": [],
208 }),
209 whole_archive_deps = select({
210 "//build/bazel/platforms/arch:arm64": [":static_dep"],
211 "//conditions:default": [],
212 }),
213)`},
214 })
215}
216
217func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
218 runCcLibraryStaticTestCase(t, bp2buildTestCase{
219 description: "cc_library_shared os-specific shared_libs",
220 moduleTypeUnderTest: "cc_library_shared",
221 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
222 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
223 filesystem: map[string]string{},
224 blueprint: soongCcLibrarySharedPreamble + `
225cc_library_shared {
226 name: "shared_dep",
227 bazel_module: { bp2build_available: false },
228}
229cc_library_shared {
230 name: "foo_shared",
231 target: { android: { shared_libs: ["shared_dep"], } },
232 include_build_directory: false,
233}`,
234 expectedBazelTargets: []string{`cc_library_shared(
235 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400236 implementation_dynamic_deps = select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000237 "//build/bazel/platforms/os:android": [":shared_dep"],
238 "//conditions:default": [],
239 }),
240)`},
241 })
242}
243
244func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
245 runCcLibrarySharedTestCase(t, bp2buildTestCase{
246 description: "cc_library_shared base, arch, and os-specific shared_libs",
247 moduleTypeUnderTest: "cc_library_shared",
248 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
249 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
250 filesystem: map[string]string{},
251 blueprint: soongCcLibrarySharedPreamble + `
252cc_library_shared {
253 name: "shared_dep",
254 bazel_module: { bp2build_available: false },
255}
256cc_library_shared {
257 name: "shared_dep2",
258 bazel_module: { bp2build_available: false },
259}
260cc_library_shared {
261 name: "shared_dep3",
262 bazel_module: { bp2build_available: false },
263}
264cc_library_shared {
265 name: "foo_shared",
266 shared_libs: ["shared_dep"],
267 target: { android: { shared_libs: ["shared_dep2"] } },
268 arch: { arm64: { shared_libs: ["shared_dep3"] } },
269 include_build_directory: false,
270}`,
271 expectedBazelTargets: []string{`cc_library_shared(
272 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400273 implementation_dynamic_deps = [":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000274 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
275 "//conditions:default": [],
276 }) + select({
277 "//build/bazel/platforms/os:android": [":shared_dep2"],
278 "//conditions:default": [],
279 }),
280)`},
281 })
282}
283
284func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
285 runCcLibrarySharedTestCase(t, bp2buildTestCase{
286 description: "cc_library_shared simple exclude_srcs",
287 moduleTypeUnderTest: "cc_library_shared",
288 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
289 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
290 filesystem: map[string]string{
291 "common.c": "",
292 "foo-a.c": "",
293 "foo-excluded.c": "",
294 },
295 blueprint: soongCcLibrarySharedPreamble + `
296cc_library_shared {
297 name: "foo_shared",
298 srcs: ["common.c", "foo-*.c"],
299 exclude_srcs: ["foo-excluded.c"],
300 include_build_directory: false,
301}`,
302 expectedBazelTargets: []string{`cc_library_shared(
303 name = "foo_shared",
304 srcs_c = [
305 "common.c",
306 "foo-a.c",
307 ],
308)`},
309 })
310}
311
312func TestCcLibrarySharedStrip(t *testing.T) {
313 runCcLibrarySharedTestCase(t, bp2buildTestCase{
314 description: "cc_library_shared stripping",
315 moduleTypeUnderTest: "cc_library_shared",
316 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
317 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
318 filesystem: map[string]string{},
319 blueprint: soongCcLibrarySharedPreamble + `
320cc_library_shared {
321 name: "foo_shared",
322 strip: {
323 keep_symbols: false,
324 keep_symbols_and_debug_frame: true,
325 keep_symbols_list: ["sym", "sym2"],
326 all: true,
327 none: false,
328 },
329 include_build_directory: false,
330}`,
331 expectedBazelTargets: []string{`cc_library_shared(
332 name = "foo_shared",
333 strip = {
334 "all": True,
335 "keep_symbols": False,
336 "keep_symbols_and_debug_frame": True,
337 "keep_symbols_list": [
338 "sym",
339 "sym2",
340 ],
341 "none": False,
342 },
343)`},
344 })
345}
346
347func TestCcLibrarySharedVersionScript(t *testing.T) {
348 runCcLibrarySharedTestCase(t, bp2buildTestCase{
349 description: "cc_library_shared version script",
350 moduleTypeUnderTest: "cc_library_shared",
351 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
352 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
353 filesystem: map[string]string{
354 "version_script": "",
355 },
356 blueprint: soongCcLibrarySharedPreamble + `
357cc_library_shared {
358 name: "foo_shared",
359 version_script: "version_script",
360 include_build_directory: false,
361}`,
362 expectedBazelTargets: []string{`cc_library_shared(
363 name = "foo_shared",
Liz Kammerd2871182021-10-04 13:54:37 -0400364 additional_linker_inputs = ["version_script"],
365 linkopts = ["-Wl,--version-script,$(location version_script)"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000366)`},
367 })
368}
Jingwen Chen6ada5892021-09-17 11:38:09 +0000369
370func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
371 runCcLibrarySharedTestCase(t, bp2buildTestCase{
372 description: "cc_library_shared - nocrt: true emits attribute",
373 moduleTypeUnderTest: "cc_library_shared",
374 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
375 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
376 filesystem: map[string]string{
377 "impl.cpp": "",
378 },
379 blueprint: soongCcLibraryPreamble + `
380cc_library_shared {
381 name: "foo_shared",
382 srcs: ["impl.cpp"],
383 nocrt: true,
384 include_build_directory: false,
385}
386`,
387 expectedBazelTargets: []string{`cc_library_shared(
388 name = "foo_shared",
389 link_crt = False,
390 srcs = ["impl.cpp"],
391)`}})
392}
393
394func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
395 runCcLibrarySharedTestCase(t, bp2buildTestCase{
396 description: "cc_library_shared - nocrt: false doesn't emit attribute",
397 moduleTypeUnderTest: "cc_library_shared",
398 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
399 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
400 filesystem: map[string]string{
401 "impl.cpp": "",
402 },
403 blueprint: soongCcLibraryPreamble + `
404cc_library_shared {
405 name: "foo_shared",
406 srcs: ["impl.cpp"],
407 nocrt: false,
408 include_build_directory: false,
409}
410`,
411 expectedBazelTargets: []string{`cc_library_shared(
412 name = "foo_shared",
413 srcs = ["impl.cpp"],
414)`}})
415}
416
417func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
418 runCcLibrarySharedTestCase(t, bp2buildTestCase{
419 description: "cc_library_shared - nocrt in select",
420 moduleTypeUnderTest: "cc_library_shared",
421 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
422 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
423 filesystem: map[string]string{
424 "impl.cpp": "",
425 },
426 blueprint: soongCcLibraryPreamble + `
427cc_library_shared {
428 name: "foo_shared",
429 srcs: ["impl.cpp"],
430 arch: {
431 arm: {
432 nocrt: true,
433 },
434 x86: {
435 nocrt: false,
436 },
437 },
438 include_build_directory: false,
439}
440`,
441 expectedErr: fmt.Errorf("Android.bp:16:1: module \"foo_shared\": nocrt is not supported for arch variants"),
442 })
443}