blob: 3dcfbd7db4defc448790e8bb7d661409ecf63f8b [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 (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/cc"
22)
23
24const (
25 // See cc/testing.go for more context
26 // TODO(alexmarquez): Split out the preamble into common code?
27 soongCcLibrarySharedPreamble = soongCcLibraryStaticPreamble
28)
29
30func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) {
31 cc.RegisterCCBuildComponents(ctx)
32 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
33 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
34 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
35}
36
37func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) {
38 t.Helper()
39 runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
40}
41
42func TestCcLibrarySharedSimple(t *testing.T) {
43 runCcLibrarySharedTestCase(t, bp2buildTestCase{
44 description: "cc_library_shared simple overall test",
45 moduleTypeUnderTest: "cc_library_shared",
46 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
47 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
48 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}`,
142 expectedBazelTargets: []string{`cc_library_shared(
143 name = "foo_shared",
144 absolute_includes = [
145 "include_dir_1",
146 "include_dir_2",
147 ],
148 copts = [
149 "-Dflag1",
150 "-Dflag2",
151 ],
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000152 export_includes = [
153 "export_include_dir_1",
154 "export_include_dir_2",
155 ],
156 implementation_deps = [
157 ":header_lib_1",
158 ":header_lib_2",
159 ],
Liz Kammer7a210ac2021-09-22 15:52:58 -0400160 implementation_dynamic_deps = [
161 ":shared_lib_1",
162 ":shared_lib_2",
163 ],
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000164 local_includes = [
165 "local_include_dir_1",
166 "local_include_dir_2",
167 ".",
168 ],
169 srcs = [
170 "foo_shared1.cc",
171 "foo_shared2.cc",
172 ],
173 whole_archive_deps = [
174 ":whole_static_lib_1",
175 ":whole_static_lib_2",
176 ],
177)`},
178 })
179}
180
181func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
182 runCcLibrarySharedTestCase(t, bp2buildTestCase{
183 description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
184 moduleTypeUnderTest: "cc_library_shared",
185 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
186 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
187 filesystem: map[string]string{},
188 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}`,
202 expectedBazelTargets: []string{`cc_library_shared(
203 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400204 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": [],
207 }),
208 whole_archive_deps = select({
209 "//build/bazel/platforms/arch:arm64": [":static_dep"],
210 "//conditions:default": [],
211 }),
212)`},
213 })
214}
215
216func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
217 runCcLibraryStaticTestCase(t, bp2buildTestCase{
218 description: "cc_library_shared os-specific shared_libs",
219 moduleTypeUnderTest: "cc_library_shared",
220 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
221 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
222 filesystem: map[string]string{},
223 blueprint: soongCcLibrarySharedPreamble + `
224cc_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}`,
233 expectedBazelTargets: []string{`cc_library_shared(
234 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400235 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": [],
238 }),
239)`},
240 })
241}
242
243func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
244 runCcLibrarySharedTestCase(t, bp2buildTestCase{
245 description: "cc_library_shared base, arch, and os-specific shared_libs",
246 moduleTypeUnderTest: "cc_library_shared",
247 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
248 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
249 filesystem: map[string]string{},
250 blueprint: soongCcLibrarySharedPreamble + `
251cc_library_shared {
252 name: "shared_dep",
253 bazel_module: { bp2build_available: false },
254}
255cc_library_shared {
256 name: "shared_dep2",
257 bazel_module: { bp2build_available: false },
258}
259cc_library_shared {
260 name: "shared_dep3",
261 bazel_module: { bp2build_available: false },
262}
263cc_library_shared {
264 name: "foo_shared",
265 shared_libs: ["shared_dep"],
266 target: { android: { shared_libs: ["shared_dep2"] } },
267 arch: { arm64: { shared_libs: ["shared_dep3"] } },
268 include_build_directory: false,
269}`,
270 expectedBazelTargets: []string{`cc_library_shared(
271 name = "foo_shared",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400272 implementation_dynamic_deps = [":shared_dep"] + select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000273 "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
274 "//conditions:default": [],
275 }) + select({
276 "//build/bazel/platforms/os:android": [":shared_dep2"],
277 "//conditions:default": [],
278 }),
279)`},
280 })
281}
282
283func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
284 runCcLibrarySharedTestCase(t, bp2buildTestCase{
285 description: "cc_library_shared simple exclude_srcs",
286 moduleTypeUnderTest: "cc_library_shared",
287 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
288 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
289 filesystem: map[string]string{
290 "common.c": "",
291 "foo-a.c": "",
292 "foo-excluded.c": "",
293 },
294 blueprint: soongCcLibrarySharedPreamble + `
295cc_library_shared {
296 name: "foo_shared",
297 srcs: ["common.c", "foo-*.c"],
298 exclude_srcs: ["foo-excluded.c"],
299 include_build_directory: false,
300}`,
301 expectedBazelTargets: []string{`cc_library_shared(
302 name = "foo_shared",
303 srcs_c = [
304 "common.c",
305 "foo-a.c",
306 ],
307)`},
308 })
309}
310
311func TestCcLibrarySharedStrip(t *testing.T) {
312 runCcLibrarySharedTestCase(t, bp2buildTestCase{
313 description: "cc_library_shared stripping",
314 moduleTypeUnderTest: "cc_library_shared",
315 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
316 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
317 filesystem: map[string]string{},
318 blueprint: soongCcLibrarySharedPreamble + `
319cc_library_shared {
320 name: "foo_shared",
321 strip: {
322 keep_symbols: false,
323 keep_symbols_and_debug_frame: true,
324 keep_symbols_list: ["sym", "sym2"],
325 all: true,
326 none: false,
327 },
328 include_build_directory: false,
329}`,
330 expectedBazelTargets: []string{`cc_library_shared(
331 name = "foo_shared",
332 strip = {
333 "all": True,
334 "keep_symbols": False,
335 "keep_symbols_and_debug_frame": True,
336 "keep_symbols_list": [
337 "sym",
338 "sym2",
339 ],
340 "none": False,
341 },
342)`},
343 })
344}
345
346func TestCcLibrarySharedVersionScript(t *testing.T) {
347 runCcLibrarySharedTestCase(t, bp2buildTestCase{
348 description: "cc_library_shared version script",
349 moduleTypeUnderTest: "cc_library_shared",
350 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
351 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
352 filesystem: map[string]string{
353 "version_script": "",
354 },
355 blueprint: soongCcLibrarySharedPreamble + `
356cc_library_shared {
357 name: "foo_shared",
358 version_script: "version_script",
359 include_build_directory: false,
360}`,
361 expectedBazelTargets: []string{`cc_library_shared(
362 name = "foo_shared",
363 version_script = "version_script",
364)`},
365 })
366}