blob: dcbe326e9bc462acea751030a04f269fe743d01d [file] [log] [blame]
Jingwen Chen63930982021-03-24 10:04:33 -04001// 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 Thaureaux1c92aef2021-08-23 16:10:00 +000019 "testing"
20
Jingwen Chen63930982021-03-24 10:04:33 -040021 "android/soong/android"
22 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040023)
24
25const (
26 // See cc/testing.go for more context
27 soongCcLibraryPreamble = `
28cc_defaults {
Liz Kammer8337ea42021-09-10 10:06:32 -040029 name: "linux_bionic_supported",
Jingwen Chen63930982021-03-24 10:04:33 -040030}
31
32toolchain_library {
Liz Kammer8337ea42021-09-10 10:06:32 -040033 name: "libclang_rt.builtins-x86_64-android",
34 defaults: ["linux_bionic_supported"],
35 vendor_available: true,
36 vendor_ramdisk_available: true,
37 product_available: true,
38 recovery_available: true,
39 native_bridge_supported: true,
40 src: "",
Jingwen Chen63930982021-03-24 10:04:33 -040041}`
Liz Kammer12615db2021-09-28 09:19:17 -040042
43 soongCcProtoLibraries = `
44cc_library {
45 name: "libprotobuf-cpp-lite",
46 bazel_module: { bp2build_available: false },
47}
48
49cc_library {
50 name: "libprotobuf-cpp-full",
51 bazel_module: { bp2build_available: false },
52}`
53
54 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040055)
56
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040058 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059 runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
60}
61
62func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
63 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000064 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020065 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040066 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020067 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
68 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
69}
70
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020071func TestCcLibrarySimple(t *testing.T) {
72 runCcLibraryTestCase(t, bp2buildTestCase{
73 description: "cc_library - simple example",
74 moduleTypeUnderTest: "cc_library",
75 moduleTypeUnderTestFactory: cc.LibraryFactory,
76 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
77 filesystem: map[string]string{
78 "android.cpp": "",
Liz Kammer01a16e82021-07-16 16:33:47 -040079 "bionic.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020080 "darwin.cpp": "",
81 // Refer to cc.headerExts for the supported header extensions in Soong.
82 "header.h": "",
83 "header.hh": "",
84 "header.hpp": "",
85 "header.hxx": "",
86 "header.h++": "",
87 "header.inl": "",
88 "header.inc": "",
89 "header.ipp": "",
90 "header.h.generic": "",
91 "impl.cpp": "",
92 "linux.cpp": "",
93 "x86.cpp": "",
94 "x86_64.cpp": "",
95 "foo-dir/a.h": "",
96 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050097 blueprint: soongCcLibraryPreamble +
98 simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -040099cc_library {
100 name: "foo-lib",
101 srcs: ["impl.cpp"],
102 cflags: ["-Wall"],
103 header_libs: ["some-headers"],
104 export_include_dirs: ["foo-dir"],
105 ldflags: ["-Wl,--exclude-libs=bar.a"],
106 arch: {
107 x86: {
108 ldflags: ["-Wl,--exclude-libs=baz.a"],
109 srcs: ["x86.cpp"],
110 },
111 x86_64: {
112 ldflags: ["-Wl,--exclude-libs=qux.a"],
113 srcs: ["x86_64.cpp"],
114 },
115 },
116 target: {
117 android: {
118 srcs: ["android.cpp"],
119 },
120 linux_glibc: {
121 srcs: ["linux.cpp"],
122 },
123 darwin: {
124 srcs: ["darwin.cpp"],
125 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400126 bionic: {
127 srcs: ["bionic.cpp"]
128 },
Jingwen Chen63930982021-03-24 10:04:33 -0400129 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400130 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400131}
132`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500133 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
134 "copts": `["-Wall"]`,
135 "export_includes": `["foo-dir"]`,
136 "implementation_deps": `[":some-headers"]`,
137 "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000138 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
139 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
140 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500141 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500142 "srcs": `["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000143 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
144 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400145 "//conditions:default": [],
146 }) + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400147 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400148 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -0400149 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400150 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000151 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
152 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400153 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Liz Kammer01a16e82021-07-16 16:33:47 -0400154 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500155 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500156 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500157 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200158}
159
160func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
161 runCcLibraryTestCase(t, bp2buildTestCase{
162 description: "cc_library - trimmed example of //bionic/linker:ld-android",
163 moduleTypeUnderTest: "cc_library",
164 moduleTypeUnderTestFactory: cc.LibraryFactory,
165 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
166 filesystem: map[string]string{
167 "ld-android.cpp": "",
168 "linked_list.h": "",
169 "linker.h": "",
170 "linker_block_allocator.h": "",
171 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400172 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200173 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400174cc_library_headers { name: "libc_headers" }
175cc_library {
176 name: "fake-ld-android",
177 srcs: ["ld_android.cpp"],
178 cflags: [
179 "-Wall",
180 "-Wextra",
181 "-Wunused",
182 "-Werror",
183 ],
184 header_libs: ["libc_headers"],
185 ldflags: [
186 "-Wl,--exclude-libs=libgcc.a",
187 "-Wl,--exclude-libs=libgcc_stripped.a",
188 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
189 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
190 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
191 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
192 ],
193 arch: {
194 x86: {
195 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
196 },
197 x86_64: {
198 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
199 },
200 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400201 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400202}
203`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500204 expectedBazelTargets: makeCcLibraryTargets("fake-ld-android", attrNameToString{
205 "srcs": `["ld_android.cpp"]`,
206 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400207 "-Wall",
208 "-Wextra",
209 "-Wunused",
210 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500211 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500212 "implementation_deps": `[":libc_headers"]`,
213 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400214 "-Wl,--exclude-libs=libgcc.a",
215 "-Wl,--exclude-libs=libgcc_stripped.a",
216 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
217 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
218 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
219 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
220 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000221 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
222 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400223 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500224 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500225 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200226 })
227}
228
229func TestCcLibraryExcludeSrcs(t *testing.T) {
230 runCcLibraryTestCase(t, bp2buildTestCase{
231 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
232 moduleTypeUnderTest: "cc_library",
233 moduleTypeUnderTestFactory: cc.LibraryFactory,
234 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
235 dir: "external",
236 filesystem: map[string]string{
237 "external/math/cosf.c": "",
238 "external/math/erf.c": "",
239 "external/math/erf_data.c": "",
240 "external/math/erff.c": "",
241 "external/math/erff_data.c": "",
242 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000243cc_library {
244 name: "fake-libarm-optimized-routines-math",
245 exclude_srcs: [
246 // Provided by:
247 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
248 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
249 "math/erf.c",
250 "math/erf_data.c",
251 "math/erff.c",
252 "math/erff_data.c",
253 ],
254 srcs: [
255 "math/*.c",
256 ],
257 // arch-specific settings
258 arch: {
259 arm64: {
260 cflags: [
261 "-DHAVE_FAST_FMA=1",
262 ],
263 },
264 },
265 bazel_module: { bp2build_available: true },
266}
267`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200268 },
269 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500270 expectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", attrNameToString{
271 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000272 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
273 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500274 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500275 "local_includes": `["."]`,
276 "srcs_c": `["math/cosf.c"]`,
277 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200278 })
279}
280
281func TestCcLibrarySharedStaticProps(t *testing.T) {
282 runCcLibraryTestCase(t, bp2buildTestCase{
283 description: "cc_library shared/static props",
284 moduleTypeUnderTest: "cc_library",
285 moduleTypeUnderTestFactory: cc.LibraryFactory,
286 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200287 filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400288 "both.cpp": "",
289 "sharedonly.cpp": "",
290 "staticonly.cpp": "",
291 },
292 blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000293cc_library {
294 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400295 srcs: ["both.cpp"],
296 cflags: ["bothflag"],
297 shared_libs: ["shared_dep_for_both"],
298 static_libs: ["static_dep_for_both"],
299 whole_static_libs: ["whole_static_lib_for_both"],
300 static: {
301 srcs: ["staticonly.cpp"],
302 cflags: ["staticflag"],
303 shared_libs: ["shared_dep_for_static"],
304 static_libs: ["static_dep_for_static"],
305 whole_static_libs: ["whole_static_lib_for_static"],
306 },
307 shared: {
308 srcs: ["sharedonly.cpp"],
309 cflags: ["sharedflag"],
310 shared_libs: ["shared_dep_for_shared"],
311 static_libs: ["static_dep_for_shared"],
312 whole_static_libs: ["whole_static_lib_for_shared"],
313 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400314 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000315}
316
Liz Kammer8337ea42021-09-10 10:06:32 -0400317cc_library_static {
318 name: "static_dep_for_shared",
319 bazel_module: { bp2build_available: false },
320}
Chris Parsons08648312021-05-06 16:23:19 -0400321
Liz Kammer8337ea42021-09-10 10:06:32 -0400322cc_library_static {
323 name: "static_dep_for_static",
324 bazel_module: { bp2build_available: false },
325}
Chris Parsons08648312021-05-06 16:23:19 -0400326
Liz Kammer8337ea42021-09-10 10:06:32 -0400327cc_library_static {
328 name: "static_dep_for_both",
329 bazel_module: { bp2build_available: false },
330}
Chris Parsons08648312021-05-06 16:23:19 -0400331
Liz Kammer8337ea42021-09-10 10:06:32 -0400332cc_library_static {
333 name: "whole_static_lib_for_shared",
334 bazel_module: { bp2build_available: false },
335}
Chris Parsons08648312021-05-06 16:23:19 -0400336
Liz Kammer8337ea42021-09-10 10:06:32 -0400337cc_library_static {
338 name: "whole_static_lib_for_static",
339 bazel_module: { bp2build_available: false },
340}
Chris Parsons08648312021-05-06 16:23:19 -0400341
Liz Kammer8337ea42021-09-10 10:06:32 -0400342cc_library_static {
343 name: "whole_static_lib_for_both",
344 bazel_module: { bp2build_available: false },
345}
Chris Parsons08648312021-05-06 16:23:19 -0400346
Liz Kammer8337ea42021-09-10 10:06:32 -0400347cc_library {
348 name: "shared_dep_for_shared",
349 bazel_module: { bp2build_available: false },
350}
Chris Parsons08648312021-05-06 16:23:19 -0400351
Liz Kammer8337ea42021-09-10 10:06:32 -0400352cc_library {
353 name: "shared_dep_for_static",
354 bazel_module: { bp2build_available: false },
355}
Chris Parsons08648312021-05-06 16:23:19 -0400356
Liz Kammer8337ea42021-09-10 10:06:32 -0400357cc_library {
358 name: "shared_dep_for_both",
359 bazel_module: { bp2build_available: false },
360}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000361`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500362 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500363 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
364 "copts": `[
365 "bothflag",
366 "staticflag",
367 ]`,
368 "implementation_deps": `[
369 ":static_dep_for_both",
370 ":static_dep_for_static",
371 ]`,
372 "implementation_dynamic_deps": `[
373 ":shared_dep_for_both",
374 ":shared_dep_for_static",
375 ]`,
376 "srcs": `[
377 "both.cpp",
378 "staticonly.cpp",
379 ]`,
380 "whole_archive_deps": `[
381 ":whole_static_lib_for_both",
382 ":whole_static_lib_for_static",
383 ]`}),
384 makeBazelTarget("cc_library_shared", "a", attrNameToString{
385 "copts": `[
386 "bothflag",
387 "sharedflag",
388 ]`,
389 "implementation_deps": `[
390 ":static_dep_for_both",
391 ":static_dep_for_shared",
392 ]`,
393 "implementation_dynamic_deps": `[
394 ":shared_dep_for_both",
395 ":shared_dep_for_shared",
396 ]`,
397 "srcs": `[
398 "both.cpp",
399 "sharedonly.cpp",
400 ]`,
401 "whole_archive_deps": `[
402 ":whole_static_lib_for_both",
403 ":whole_static_lib_for_shared",
404 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500405 }),
406 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200407 })
408}
409
Liz Kammer7a210ac2021-09-22 15:52:58 -0400410func TestCcLibraryDeps(t *testing.T) {
411 runCcLibraryTestCase(t, bp2buildTestCase{
412 description: "cc_library shared/static props",
413 moduleTypeUnderTest: "cc_library",
414 moduleTypeUnderTestFactory: cc.LibraryFactory,
415 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
416 filesystem: map[string]string{
417 "both.cpp": "",
418 "sharedonly.cpp": "",
419 "staticonly.cpp": "",
420 },
421 blueprint: soongCcLibraryPreamble + `
422cc_library {
423 name: "a",
424 srcs: ["both.cpp"],
425 cflags: ["bothflag"],
426 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
427 export_shared_lib_headers: ["shared_dep_for_both"],
428 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
429 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
430 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
431 static: {
432 srcs: ["staticonly.cpp"],
433 cflags: ["staticflag"],
434 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
435 export_shared_lib_headers: ["shared_dep_for_static"],
436 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
437 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
438 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
439 },
440 shared: {
441 srcs: ["sharedonly.cpp"],
442 cflags: ["sharedflag"],
443 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
444 export_shared_lib_headers: ["shared_dep_for_shared"],
445 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
446 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
447 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
448 },
449 include_build_directory: false,
450}
451` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
452 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
453 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
454 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
455 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
456 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
457 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
458 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
459 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
460 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
461 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
462 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
463 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
464 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
465 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
466 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
467 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
468 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500469 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500470 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
471 "copts": `[
472 "bothflag",
473 "staticflag",
474 ]`,
475 "deps": `[
476 ":static_dep_for_both",
477 ":static_dep_for_static",
478 ]`,
479 "dynamic_deps": `[
480 ":shared_dep_for_both",
481 ":shared_dep_for_static",
482 ]`,
483 "implementation_deps": `[
484 ":implementation_static_dep_for_both",
485 ":implementation_static_dep_for_static",
486 ]`,
487 "implementation_dynamic_deps": `[
488 ":implementation_shared_dep_for_both",
489 ":implementation_shared_dep_for_static",
490 ]`,
491 "srcs": `[
492 "both.cpp",
493 "staticonly.cpp",
494 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500495 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400496 ":not_explicitly_exported_whole_static_dep_for_both",
497 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500498 ":not_explicitly_exported_whole_static_dep_for_static",
499 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500500 ]`,
501 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -0500502 makeBazelTarget("cc_library_shared", "a", attrNameToString{
503 "copts": `[
504 "bothflag",
505 "sharedflag",
506 ]`,
507 "deps": `[
508 ":static_dep_for_both",
509 ":static_dep_for_shared",
510 ]`,
511 "dynamic_deps": `[
512 ":shared_dep_for_both",
513 ":shared_dep_for_shared",
514 ]`,
515 "implementation_deps": `[
516 ":implementation_static_dep_for_both",
517 ":implementation_static_dep_for_shared",
518 ]`,
519 "implementation_dynamic_deps": `[
520 ":implementation_shared_dep_for_both",
521 ":implementation_shared_dep_for_shared",
522 ]`,
523 "srcs": `[
524 "both.cpp",
525 "sharedonly.cpp",
526 ]`,
527 "whole_archive_deps": `[
528 ":not_explicitly_exported_whole_static_dep_for_both",
529 ":whole_static_dep_for_both",
530 ":not_explicitly_exported_whole_static_dep_for_shared",
531 ":whole_static_dep_for_shared",
532 ]`,
533 })},
534 },
535 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400536}
537
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400538func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
539 runCcLibraryTestCase(t, bp2buildTestCase{
540 moduleTypeUnderTest: "cc_library",
541 moduleTypeUnderTestFactory: cc.LibraryFactory,
542 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400543 dir: "foo/bar",
544 filesystem: map[string]string{
545 "foo/bar/Android.bp": `
546cc_library {
547 name: "a",
548 whole_static_libs: ["whole_static_lib_for_both"],
549 static: {
550 whole_static_libs: ["whole_static_lib_for_static"],
551 },
552 shared: {
553 whole_static_libs: ["whole_static_lib_for_shared"],
554 },
555 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400556 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400557}
558
559cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
560
561cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
562
563cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
564`,
565 },
566 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500567 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500568 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
569 "whole_archive_deps": `[
570 ":whole_static_lib_for_both_alwayslink",
571 ":whole_static_lib_for_static_alwayslink",
572 ]`,
573 }),
574 makeBazelTarget("cc_library_shared", "a", attrNameToString{
575 "whole_archive_deps": `[
576 ":whole_static_lib_for_both_alwayslink",
577 ":whole_static_lib_for_shared_alwayslink",
578 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500579 }),
580 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500581 },
582 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400583}
584
Jingwen Chenbcf53042021-05-26 04:42:42 +0000585func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
586 runCcLibraryTestCase(t, bp2buildTestCase{
587 description: "cc_library shared/static props in arch",
588 moduleTypeUnderTest: "cc_library",
589 moduleTypeUnderTestFactory: cc.LibraryFactory,
590 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chenbcf53042021-05-26 04:42:42 +0000591 dir: "foo/bar",
592 filesystem: map[string]string{
593 "foo/bar/arm.cpp": "",
594 "foo/bar/x86.cpp": "",
595 "foo/bar/sharedonly.cpp": "",
596 "foo/bar/staticonly.cpp": "",
597 "foo/bar/Android.bp": `
598cc_library {
599 name: "a",
600 arch: {
601 arm: {
602 shared: {
603 srcs: ["arm_shared.cpp"],
604 cflags: ["-DARM_SHARED"],
605 static_libs: ["arm_static_dep_for_shared"],
606 whole_static_libs: ["arm_whole_static_dep_for_shared"],
607 shared_libs: ["arm_shared_dep_for_shared"],
608 },
609 },
610 x86: {
611 static: {
612 srcs: ["x86_static.cpp"],
613 cflags: ["-DX86_STATIC"],
614 static_libs: ["x86_dep_for_static"],
615 },
616 },
617 },
618 target: {
619 android: {
620 shared: {
621 srcs: ["android_shared.cpp"],
622 cflags: ["-DANDROID_SHARED"],
623 static_libs: ["android_dep_for_shared"],
624 },
625 },
626 android_arm: {
627 shared: {
628 cflags: ["-DANDROID_ARM_SHARED"],
629 },
630 },
631 },
632 srcs: ["both.cpp"],
633 cflags: ["bothflag"],
634 static_libs: ["static_dep_for_both"],
635 static: {
636 srcs: ["staticonly.cpp"],
637 cflags: ["staticflag"],
638 static_libs: ["static_dep_for_static"],
639 },
640 shared: {
641 srcs: ["sharedonly.cpp"],
642 cflags: ["sharedflag"],
643 static_libs: ["static_dep_for_shared"],
644 },
645 bazel_module: { bp2build_available: true },
646}
647
648cc_library_static { name: "static_dep_for_shared" }
649cc_library_static { name: "static_dep_for_static" }
650cc_library_static { name: "static_dep_for_both" }
651
652cc_library_static { name: "arm_static_dep_for_shared" }
653cc_library_static { name: "arm_whole_static_dep_for_shared" }
654cc_library_static { name: "arm_shared_dep_for_shared" }
655
656cc_library_static { name: "x86_dep_for_static" }
657
658cc_library_static { name: "android_dep_for_shared" }
659`,
660 },
661 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500662 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500663 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
664 "copts": `[
665 "bothflag",
666 "staticflag",
667 ] + select({
668 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
669 "//conditions:default": [],
670 })`,
671 "implementation_deps": `[
672 ":static_dep_for_both",
673 ":static_dep_for_static",
674 ] + select({
675 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
676 "//conditions:default": [],
677 })`,
678 "local_includes": `["."]`,
679 "srcs": `[
680 "both.cpp",
681 "staticonly.cpp",
682 ] + select({
683 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
684 "//conditions:default": [],
685 })`,
686 }),
687 makeBazelTarget("cc_library_shared", "a", attrNameToString{
688 "copts": `[
689 "bothflag",
690 "sharedflag",
691 ] + select({
692 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
693 "//conditions:default": [],
694 }) + select({
695 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
696 "//conditions:default": [],
697 }) + select({
698 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
699 "//conditions:default": [],
700 })`,
701 "implementation_deps": `[
702 ":static_dep_for_both",
703 ":static_dep_for_shared",
704 ] + select({
705 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
706 "//conditions:default": [],
707 }) + select({
708 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
709 "//conditions:default": [],
710 })`,
711 "implementation_dynamic_deps": `select({
712 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
713 "//conditions:default": [],
714 })`,
715 "local_includes": `["."]`,
716 "srcs": `[
717 "both.cpp",
718 "sharedonly.cpp",
719 ] + select({
720 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
721 "//conditions:default": [],
722 }) + select({
723 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
724 "//conditions:default": [],
725 })`,
726 "whole_archive_deps": `select({
727 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
728 "//conditions:default": [],
729 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500730 }),
731 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500732 },
733 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000734}
735
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000736func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
737 runCcLibraryTestCase(t, bp2buildTestCase{
738 description: "cc_library shared/static props with c/cpp/s mixed sources",
739 moduleTypeUnderTest: "cc_library",
740 moduleTypeUnderTestFactory: cc.LibraryFactory,
741 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000742 dir: "foo/bar",
743 filesystem: map[string]string{
744 "foo/bar/both_source.cpp": "",
745 "foo/bar/both_source.cc": "",
746 "foo/bar/both_source.c": "",
747 "foo/bar/both_source.s": "",
748 "foo/bar/both_source.S": "",
749 "foo/bar/shared_source.cpp": "",
750 "foo/bar/shared_source.cc": "",
751 "foo/bar/shared_source.c": "",
752 "foo/bar/shared_source.s": "",
753 "foo/bar/shared_source.S": "",
754 "foo/bar/static_source.cpp": "",
755 "foo/bar/static_source.cc": "",
756 "foo/bar/static_source.c": "",
757 "foo/bar/static_source.s": "",
758 "foo/bar/static_source.S": "",
759 "foo/bar/Android.bp": `
760cc_library {
761 name: "a",
762 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400763 "both_source.cpp",
764 "both_source.cc",
765 "both_source.c",
766 "both_source.s",
767 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400768 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400769 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000770 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400771 srcs: [
772 "static_source.cpp",
773 "static_source.cc",
774 "static_source.c",
775 "static_source.s",
776 "static_source.S",
777 ":static_filegroup",
778 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000779 },
780 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400781 srcs: [
782 "shared_source.cpp",
783 "shared_source.cc",
784 "shared_source.c",
785 "shared_source.s",
786 "shared_source.S",
787 ":shared_filegroup",
788 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000789 },
790 bazel_module: { bp2build_available: true },
791}
792
793filegroup {
794 name: "both_filegroup",
795 srcs: [
796 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400797 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000798}
799
800filegroup {
801 name: "shared_filegroup",
802 srcs: [
803 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400804 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000805}
806
807filegroup {
808 name: "static_filegroup",
809 srcs: [
810 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400811 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000812}
813`,
814 },
815 blueprint: soongCcLibraryPreamble,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500816 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500817 makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500818 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500819 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000820 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400821 "both_source.cc",
822 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500823 "static_source.cpp",
824 "static_source.cc",
825 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500826 ]`,
827 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000828 "both_source.s",
829 "both_source.S",
830 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500831 "static_source.s",
832 "static_source.S",
833 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500834 ]`,
835 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000836 "both_source.c",
837 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500838 "static_source.c",
839 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500840 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500841 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -0500842 makeBazelTarget("cc_library_shared", "a", attrNameToString{
843 "local_includes": `["."]`,
844 "srcs": `[
845 "both_source.cpp",
846 "both_source.cc",
847 ":both_filegroup_cpp_srcs",
848 "shared_source.cpp",
849 "shared_source.cc",
850 ":shared_filegroup_cpp_srcs",
851 ]`,
852 "srcs_as": `[
853 "both_source.s",
854 "both_source.S",
855 ":both_filegroup_as_srcs",
856 "shared_source.s",
857 "shared_source.S",
858 ":shared_filegroup_as_srcs",
859 ]`,
860 "srcs_c": `[
861 "both_source.c",
862 ":both_filegroup_c_srcs",
863 "shared_source.c",
864 ":shared_filegroup_c_srcs",
865 ]`,
866 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000867}
868
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200869func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
870 runCcLibraryTestCase(t, bp2buildTestCase{
871 description: "cc_library non-configured version script",
872 moduleTypeUnderTest: "cc_library",
873 moduleTypeUnderTestFactory: cc.LibraryFactory,
874 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200875 dir: "foo/bar",
876 filesystem: map[string]string{
877 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200878cc_library {
879 name: "a",
880 srcs: ["a.cpp"],
881 version_script: "v.map",
882 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400883 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200884}
885`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200886 },
887 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500888 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
889 "additional_linker_inputs": `["v.map"]`,
890 "linkopts": `["-Wl,--version-script,$(location v.map)"]`,
891 "srcs": `["a.cpp"]`,
892 }),
893 },
894 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200895}
896
897func TestCcLibraryConfiguredVersionScript(t *testing.T) {
898 runCcLibraryTestCase(t, bp2buildTestCase{
899 description: "cc_library configured version script",
900 moduleTypeUnderTest: "cc_library",
901 moduleTypeUnderTestFactory: cc.LibraryFactory,
902 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200903 dir: "foo/bar",
904 filesystem: map[string]string{
905 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400906cc_library {
907 name: "a",
908 srcs: ["a.cpp"],
909 arch: {
910 arm: {
911 version_script: "arm.map",
912 },
913 arm64: {
914 version_script: "arm64.map",
915 },
916 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200917
Liz Kammer8337ea42021-09-10 10:06:32 -0400918 bazel_module: { bp2build_available: true },
919 include_build_directory: false,
920}
Liz Kammerd366c902021-06-03 13:43:01 -0400921 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200922 },
923 blueprint: soongCcLibraryPreamble,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500924 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
925 "additional_linker_inputs": `select({
Liz Kammerd2871182021-10-04 13:54:37 -0400926 "//build/bazel/platforms/arch:arm": ["arm.map"],
927 "//build/bazel/platforms/arch:arm64": ["arm64.map"],
928 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500929 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500930 "linkopts": `select({
Liz Kammerd2871182021-10-04 13:54:37 -0400931 "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"],
932 "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"],
933 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500934 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500935 "srcs": `["a.cpp"]`,
936 }),
937 },
938 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200939}
940
941func TestCcLibrarySharedLibs(t *testing.T) {
942 runCcLibraryTestCase(t, bp2buildTestCase{
943 description: "cc_library shared_libs",
944 moduleTypeUnderTest: "cc_library",
945 moduleTypeUnderTestFactory: cc.LibraryFactory,
946 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -0400947 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400948cc_library {
949 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -0400950 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400951}
952
953cc_library {
954 name: "a",
955 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -0400956 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400957}
958`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500959 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
960 "implementation_dynamic_deps": `[":mylib"]`,
961 }),
962 },
963 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200964}
965
Liz Kammer0eae52e2021-10-06 10:32:26 -0400966func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -0500967 expected_targets := []string{}
968 expected_targets = append(expected_targets, makeCcLibraryTargets("a", attrNameToString{
969 "features": `[
970 "disable_pack_relocations",
971 "-no_undefined_symbols",
972 ]`,
973 "srcs": `["a.cpp"]`,
974 })...)
975 expected_targets = append(expected_targets, makeCcLibraryTargets("b", attrNameToString{
976 "features": `select({
977 "//build/bazel/platforms/arch:x86_64": [
978 "disable_pack_relocations",
979 "-no_undefined_symbols",
980 ],
981 "//conditions:default": [],
982 })`,
983 "srcs": `["b.cpp"]`,
984 })...)
985 expected_targets = append(expected_targets, makeCcLibraryTargets("c", attrNameToString{
986 "features": `select({
987 "//build/bazel/platforms/os:darwin": [
988 "disable_pack_relocations",
989 "-no_undefined_symbols",
990 ],
991 "//conditions:default": [],
992 })`,
993 "srcs": `["c.cpp"]`,
994 })...)
995
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200996 runCcLibraryTestCase(t, bp2buildTestCase{
997 description: "cc_library pack_relocations test",
998 moduleTypeUnderTest: "cc_library",
999 moduleTypeUnderTestFactory: cc.LibraryFactory,
1000 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001001 blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001002cc_library {
1003 name: "a",
1004 srcs: ["a.cpp"],
1005 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001006 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001007 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001008}
1009
1010cc_library {
1011 name: "b",
1012 srcs: ["b.cpp"],
1013 arch: {
1014 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001015 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001016 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001017 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001018 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001019 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001020}
1021
1022cc_library {
1023 name: "c",
1024 srcs: ["c.cpp"],
1025 target: {
1026 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001027 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001028 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001029 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001030 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001031 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001032}`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001033 expectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001034 })
1035}
1036
1037func TestCcLibrarySpacesInCopts(t *testing.T) {
1038 runCcLibraryTestCase(t, bp2buildTestCase{
1039 description: "cc_library spaces in copts",
1040 moduleTypeUnderTest: "cc_library",
1041 moduleTypeUnderTestFactory: cc.LibraryFactory,
1042 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001043 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001044cc_library {
1045 name: "a",
1046 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001047 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001048}
1049`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001050 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
1051 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001052 "-include",
1053 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001054 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001055 }),
1056 },
1057 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001058}
1059
1060func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
1061 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -04001062 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001063 moduleTypeUnderTest: "cc_library",
1064 moduleTypeUnderTestFactory: cc.LibraryFactory,
1065 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001066 blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001067 name: "a",
1068 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001069 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001070 cppflags: [
1071 "-fsigned-char",
1072 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001073 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001074 arch: {
1075 arm64: {
1076 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001077 },
Liz Kammerd366c902021-06-03 13:43:01 -04001078 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001079 target: {
1080 android: {
1081 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001082 },
Liz Kammerd366c902021-06-03 13:43:01 -04001083 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001084 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001085}
1086`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001087 expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{
1088 "copts": `["-Wall"]`,
1089 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001090 "-fsigned-char",
1091 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001092 ] + select({
1093 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1094 "//conditions:default": [],
1095 }) + select({
1096 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1097 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001098 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001099 "srcs": `["a.cpp"]`,
1100 }),
1101 },
1102 )
Jingwen Chen63930982021-03-24 10:04:33 -04001103}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001104
Liz Kammer47535c52021-06-02 16:02:22 -04001105func TestCcLibraryExcludeLibs(t *testing.T) {
1106 runCcLibraryTestCase(t, bp2buildTestCase{
1107 moduleTypeUnderTest: "cc_library",
1108 moduleTypeUnderTestFactory: cc.LibraryFactory,
1109 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer47535c52021-06-02 16:02:22 -04001110 filesystem: map[string]string{},
1111 blueprint: soongCcLibraryStaticPreamble + `
1112cc_library {
1113 name: "foo_static",
1114 srcs: ["common.c"],
1115 whole_static_libs: [
1116 "arm_whole_static_lib_excludes",
1117 "malloc_not_svelte_whole_static_lib_excludes"
1118 ],
1119 static_libs: [
1120 "arm_static_lib_excludes",
1121 "malloc_not_svelte_static_lib_excludes"
1122 ],
1123 shared_libs: [
1124 "arm_shared_lib_excludes",
1125 ],
1126 arch: {
1127 arm: {
1128 exclude_shared_libs: [
1129 "arm_shared_lib_excludes",
1130 ],
1131 exclude_static_libs: [
1132 "arm_static_lib_excludes",
1133 "arm_whole_static_lib_excludes",
1134 ],
1135 },
1136 },
1137 product_variables: {
1138 malloc_not_svelte: {
1139 shared_libs: ["malloc_not_svelte_shared_lib"],
1140 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1141 exclude_static_libs: [
1142 "malloc_not_svelte_static_lib_excludes",
1143 "malloc_not_svelte_whole_static_lib_excludes",
1144 ],
1145 },
1146 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001147 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001148}
1149
1150cc_library {
1151 name: "arm_whole_static_lib_excludes",
1152 bazel_module: { bp2build_available: false },
1153}
1154
1155cc_library {
1156 name: "malloc_not_svelte_whole_static_lib",
1157 bazel_module: { bp2build_available: false },
1158}
1159
1160cc_library {
1161 name: "malloc_not_svelte_whole_static_lib_excludes",
1162 bazel_module: { bp2build_available: false },
1163}
1164
1165cc_library {
1166 name: "arm_static_lib_excludes",
1167 bazel_module: { bp2build_available: false },
1168}
1169
1170cc_library {
1171 name: "malloc_not_svelte_static_lib_excludes",
1172 bazel_module: { bp2build_available: false },
1173}
1174
1175cc_library {
1176 name: "arm_shared_lib_excludes",
1177 bazel_module: { bp2build_available: false },
1178}
1179
1180cc_library {
1181 name: "malloc_not_svelte_shared_lib",
1182 bazel_module: { bp2build_available: false },
1183}
1184`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001185 expectedBazelTargets: makeCcLibraryTargets("foo_static", attrNameToString{
1186 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001187 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001188 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001189 }) + select({
1190 "//build/bazel/product_variables:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001191 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001192 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001193 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001194 "//build/bazel/platforms/arch:arm": [],
1195 "//conditions:default": [":arm_shared_lib_excludes"],
1196 }) + select({
1197 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1198 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001199 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001200 "srcs_c": `["common.c"]`,
1201 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001202 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001203 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001204 }) + select({
Chris Parsons953b3562021-09-20 15:14:39 -04001205 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
1206 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001207 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001208 }),
1209 },
1210 )
Liz Kammer47535c52021-06-02 16:02:22 -04001211}
Liz Kammerd366c902021-06-03 13:43:01 -04001212
1213func TestCCLibraryNoCrtTrue(t *testing.T) {
1214 runCcLibraryTestCase(t, bp2buildTestCase{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001215 description: "cc_library - nocrt: true emits attribute",
1216 moduleTypeUnderTest: "cc_library",
1217 moduleTypeUnderTestFactory: cc.LibraryFactory,
1218 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1219 filesystem: map[string]string{
1220 "impl.cpp": "",
1221 },
1222 blueprint: soongCcLibraryPreamble + `
1223cc_library {
1224 name: "foo-lib",
1225 srcs: ["impl.cpp"],
1226 nocrt: true,
1227 include_build_directory: false,
1228}
1229`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001230 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1231 "link_crt": `False`,
1232 "srcs": `["impl.cpp"]`,
1233 }),
1234 },
1235 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001236}
1237
1238func TestCCLibraryNoCrtFalse(t *testing.T) {
1239 runCcLibraryTestCase(t, bp2buildTestCase{
1240 description: "cc_library - nocrt: false - does not emit attribute",
1241 moduleTypeUnderTest: "cc_library",
1242 moduleTypeUnderTestFactory: cc.LibraryFactory,
1243 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1244 filesystem: map[string]string{
1245 "impl.cpp": "",
1246 },
1247 blueprint: soongCcLibraryPreamble + `
1248cc_library {
1249 name: "foo-lib",
1250 srcs: ["impl.cpp"],
1251 nocrt: false,
1252 include_build_directory: false,
1253}
1254`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001255 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1256 "srcs": `["impl.cpp"]`,
1257 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001258 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001259}
1260
1261func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1262 runCcLibraryTestCase(t, bp2buildTestCase{
1263 description: "cc_library - nocrt in select",
1264 moduleTypeUnderTest: "cc_library",
1265 moduleTypeUnderTestFactory: cc.LibraryFactory,
1266 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1267 filesystem: map[string]string{
1268 "impl.cpp": "",
1269 },
1270 blueprint: soongCcLibraryPreamble + `
1271cc_library {
1272 name: "foo-lib",
1273 srcs: ["impl.cpp"],
1274 arch: {
1275 arm: {
1276 nocrt: true,
1277 },
1278 x86: {
1279 nocrt: false,
1280 },
1281 },
1282 include_build_directory: false,
1283}
1284`,
1285 expectedErr: fmt.Errorf("Android.bp:16:1: module \"foo-lib\": nocrt is not supported for arch variants"),
1286 })
1287}
1288
1289func TestCCLibraryNoLibCrtTrue(t *testing.T) {
1290 runCcLibraryTestCase(t, bp2buildTestCase{
Liz Kammerd366c902021-06-03 13:43:01 -04001291 description: "cc_library - simple example",
1292 moduleTypeUnderTest: "cc_library",
1293 moduleTypeUnderTestFactory: cc.LibraryFactory,
1294 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1295 filesystem: map[string]string{
1296 "impl.cpp": "",
1297 },
1298 blueprint: soongCcLibraryPreamble + `
1299cc_library_headers { name: "some-headers" }
1300cc_library {
1301 name: "foo-lib",
1302 srcs: ["impl.cpp"],
1303 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001304 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001305}
1306`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001307 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1308 "srcs": `["impl.cpp"]`,
1309 "use_libcrt": `False`,
1310 }),
1311 })
1312}
1313
1314func makeCcLibraryTargets(name string, attrs attrNameToString) []string {
1315 STATIC_ONLY_ATTRS := map[string]bool{}
1316 SHARED_ONLY_ATTRS := map[string]bool{
1317 "link_crt": true,
1318 "additional_linker_inputs": true,
1319 "linkopts": true,
1320 "strip": true,
1321 }
1322 sharedAttrs := attrNameToString{}
1323 staticAttrs := attrNameToString{}
1324 for key, val := range attrs {
1325 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1326 sharedAttrs[key] = val
1327 }
1328 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1329 staticAttrs[key] = val
1330 }
1331 }
1332 sharedTarget := makeBazelTarget("cc_library_shared", name, sharedAttrs)
1333 staticTarget := makeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
1334
1335 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001336}
1337
Jingwen Chen6ada5892021-09-17 11:38:09 +00001338func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Liz Kammerd366c902021-06-03 13:43:01 -04001339 runCcLibraryTestCase(t, bp2buildTestCase{
1340 moduleTypeUnderTest: "cc_library",
1341 moduleTypeUnderTestFactory: cc.LibraryFactory,
1342 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1343 filesystem: map[string]string{
1344 "impl.cpp": "",
1345 },
1346 blueprint: soongCcLibraryPreamble + `
1347cc_library_headers { name: "some-headers" }
1348cc_library {
1349 name: "foo-lib",
1350 srcs: ["impl.cpp"],
1351 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001352 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001353}
1354`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001355 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1356 "srcs": `["impl.cpp"]`,
1357 "use_libcrt": `True`,
1358 }),
1359 })
Liz Kammerd366c902021-06-03 13:43:01 -04001360}
1361
Jingwen Chen6ada5892021-09-17 11:38:09 +00001362func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Liz Kammerd366c902021-06-03 13:43:01 -04001363 runCcLibraryTestCase(t, bp2buildTestCase{
1364 moduleTypeUnderTest: "cc_library",
1365 moduleTypeUnderTestFactory: cc.LibraryFactory,
1366 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1367 filesystem: map[string]string{
1368 "impl.cpp": "",
1369 },
1370 blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001371cc_library {
1372 name: "foo-lib",
1373 srcs: ["impl.cpp"],
1374 arch: {
1375 arm: {
1376 no_libcrt: true,
1377 },
1378 x86: {
1379 no_libcrt: true,
1380 },
1381 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001382 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001383}
1384`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001385 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1386 "srcs": `["impl.cpp"]`,
1387 "use_libcrt": `select({
Liz Kammerd366c902021-06-03 13:43:01 -04001388 "//build/bazel/platforms/arch:arm": False,
1389 "//build/bazel/platforms/arch:x86": False,
1390 "//conditions:default": None,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001391 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001392 }),
1393 })
Liz Kammerd366c902021-06-03 13:43:01 -04001394}
1395
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001396func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001397 expectedTargets := []string{}
1398 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", attrNameToString{
1399 "strip": `{
1400 "all": True,
1401 }`,
1402 })...)
1403 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", attrNameToString{
1404 "strip": `{
1405 "keep_symbols": True,
1406 }`,
1407 })...)
1408 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", attrNameToString{
1409 "strip": `{
1410 "keep_symbols_and_debug_frame": True,
1411 }`,
1412 })...)
1413 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", attrNameToString{
1414 "strip": `{
1415 "keep_symbols_list": ["symbol"],
1416 }`,
1417 })...)
1418 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", attrNameToString{
1419 "strip": `{
1420 "none": True,
1421 }`,
1422 })...)
1423 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", attrNameToString{})...)
1424
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001425 runCcLibraryTestCase(t, bp2buildTestCase{
1426 description: "cc_library strip args",
1427 moduleTypeUnderTest: "cc_library",
1428 moduleTypeUnderTestFactory: cc.LibraryFactory,
1429 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001430 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001431cc_library {
1432 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001433 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001434}
1435cc_library {
1436 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001437 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001438 keep_symbols: true,
1439 },
1440 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001441}
1442cc_library {
1443 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001444 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001445 keep_symbols_and_debug_frame: true,
1446 },
1447 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001448}
1449cc_library {
1450 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001451 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001452 none: true,
1453 },
1454 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001455}
1456cc_library {
1457 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001458 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001459 keep_symbols_list: ["symbol"],
1460 },
1461 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001462}
1463cc_library {
1464 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001465 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001466 all: true,
1467 },
1468 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001469}
1470`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001471 expectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001472 })
1473}
1474
1475func TestCcLibraryStripWithArch(t *testing.T) {
1476 runCcLibraryTestCase(t, bp2buildTestCase{
1477 description: "cc_library strip args",
1478 moduleTypeUnderTest: "cc_library",
1479 moduleTypeUnderTestFactory: cc.LibraryFactory,
1480 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer8337ea42021-09-10 10:06:32 -04001481 blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001482cc_library {
1483 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001484 target: {
1485 darwin: {
1486 strip: {
1487 keep_symbols_list: ["foo", "bar"]
1488 }
1489 },
1490 },
1491 arch: {
1492 arm: {
1493 strip: {
1494 keep_symbols_and_debug_frame: true,
1495 },
1496 },
1497 arm64: {
1498 strip: {
1499 keep_symbols: true,
1500 },
1501 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001502 },
1503 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001504}
1505`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001506 expectedBazelTargets: makeCcLibraryTargets("multi-arch", attrNameToString{
1507 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001508 "keep_symbols": select({
1509 "//build/bazel/platforms/arch:arm64": True,
1510 "//conditions:default": None,
1511 }),
1512 "keep_symbols_and_debug_frame": select({
1513 "//build/bazel/platforms/arch:arm": True,
1514 "//conditions:default": None,
1515 }),
1516 "keep_symbols_list": select({
1517 "//build/bazel/platforms/os:darwin": [
1518 "foo",
1519 "bar",
1520 ],
1521 "//conditions:default": [],
1522 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001523 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001524 }),
1525 },
1526 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001527}
Chris Parsons51f8c392021-08-03 21:01:05 -04001528
1529func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1530 runCcLibraryTestCase(t, bp2buildTestCase{
1531 description: "cc_library system_shared_libs empty at root",
1532 moduleTypeUnderTest: "cc_library",
1533 moduleTypeUnderTestFactory: cc.LibraryFactory,
1534 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1535 blueprint: soongCcLibraryPreamble + `
1536cc_library {
1537 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001538 system_shared_libs: [],
1539 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001540}
1541`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001542 expectedBazelTargets: makeCcLibraryTargets("root_empty", attrNameToString{
1543 "system_dynamic_deps": `[]`,
1544 }),
1545 },
1546 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001547}
1548
1549func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1550 runCcLibraryTestCase(t, bp2buildTestCase{
1551 description: "cc_library system_shared_libs empty for static variant",
1552 moduleTypeUnderTest: "cc_library",
1553 moduleTypeUnderTestFactory: cc.LibraryFactory,
1554 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1555 blueprint: soongCcLibraryPreamble + `
1556cc_library {
1557 name: "static_empty",
1558 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001559 system_shared_libs: [],
1560 },
1561 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001562}
1563`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001564 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001565 makeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", attrNameToString{
1566 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001567 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001568 makeBazelTarget("cc_library_shared", "static_empty", attrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001569 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001570 })
1571}
1572
1573func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
1574 runCcLibraryTestCase(t, bp2buildTestCase{
1575 description: "cc_library system_shared_libs empty for shared variant",
1576 moduleTypeUnderTest: "cc_library",
1577 moduleTypeUnderTestFactory: cc.LibraryFactory,
1578 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1579 blueprint: soongCcLibraryPreamble + `
1580cc_library {
1581 name: "shared_empty",
1582 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001583 system_shared_libs: [],
1584 },
1585 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001586}
1587`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001588 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001589 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}),
1590 makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{
1591 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001592 }),
1593 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001594 })
1595}
1596
1597func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
1598 runCcLibraryTestCase(t, bp2buildTestCase{
1599 description: "cc_library system_shared_libs empty for shared, bionic variant",
1600 moduleTypeUnderTest: "cc_library",
1601 moduleTypeUnderTestFactory: cc.LibraryFactory,
1602 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1603 blueprint: soongCcLibraryPreamble + `
1604cc_library {
1605 name: "shared_empty",
1606 target: {
1607 bionic: {
1608 shared: {
1609 system_shared_libs: [],
1610 }
1611 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001612 },
1613 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001614}
1615`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001616 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001617 makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}),
1618 makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{
1619 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001620 }),
1621 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001622 })
1623}
1624
1625func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1626 // Note that this behavior is technically incorrect (it's a simplification).
1627 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1628 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1629 // b/195791252 tracks the fix.
1630 runCcLibraryTestCase(t, bp2buildTestCase{
1631 description: "cc_library system_shared_libs empty for linux_bionic variant",
1632 moduleTypeUnderTest: "cc_library",
1633 moduleTypeUnderTestFactory: cc.LibraryFactory,
1634 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1635 blueprint: soongCcLibraryPreamble + `
1636cc_library {
1637 name: "target_linux_bionic_empty",
1638 target: {
1639 linux_bionic: {
1640 system_shared_libs: [],
1641 },
1642 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001643 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001644}
1645`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001646 expectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", attrNameToString{
1647 "system_dynamic_deps": `[]`,
1648 }),
1649 },
1650 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001651}
1652
1653func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1654 runCcLibraryTestCase(t, bp2buildTestCase{
1655 description: "cc_library system_shared_libs empty for bionic variant",
1656 moduleTypeUnderTest: "cc_library",
1657 moduleTypeUnderTestFactory: cc.LibraryFactory,
1658 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1659 blueprint: soongCcLibraryPreamble + `
1660cc_library {
1661 name: "target_bionic_empty",
1662 target: {
1663 bionic: {
1664 system_shared_libs: [],
1665 },
1666 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001667 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001668}
1669`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001670 expectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", attrNameToString{
1671 "system_dynamic_deps": `[]`,
1672 }),
1673 },
1674 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001675}
1676
1677func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
1678 runCcLibraryTestCase(t, bp2buildTestCase{
1679 description: "cc_library system_shared_libs set for shared and root",
1680 moduleTypeUnderTest: "cc_library",
1681 moduleTypeUnderTestFactory: cc.LibraryFactory,
1682 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1683 blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001684cc_library {
1685 name: "libc",
1686 bazel_module: { bp2build_available: false },
1687}
1688cc_library {
1689 name: "libm",
1690 bazel_module: { bp2build_available: false },
1691}
Chris Parsons51f8c392021-08-03 21:01:05 -04001692
1693cc_library {
1694 name: "foo",
1695 system_shared_libs: ["libc"],
1696 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001697 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001698 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001699 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001700}
1701`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001702 expectedBazelTargets: []string{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001703 makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001704 "system_dynamic_deps": `[":libc"]`,
1705 }),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001706 makeBazelTarget("cc_library_shared", "foo", attrNameToString{
1707 "system_dynamic_deps": `[
1708 ":libc",
1709 ":libm",
1710 ]`,
1711 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001712 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001713 })
1714}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001715
1716func TestCcLibraryOsSelects(t *testing.T) {
1717 runCcLibraryTestCase(t, bp2buildTestCase{
1718 description: "cc_library - selects for all os targets",
1719 moduleTypeUnderTest: "cc_library",
1720 moduleTypeUnderTestFactory: cc.LibraryFactory,
1721 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1722 filesystem: map[string]string{},
1723 blueprint: soongCcLibraryPreamble + `
1724cc_library_headers { name: "some-headers" }
1725cc_library {
1726 name: "foo-lib",
1727 srcs: ["base.cpp"],
1728 target: {
1729 android: {
1730 srcs: ["android.cpp"],
1731 },
1732 linux: {
1733 srcs: ["linux.cpp"],
1734 },
1735 linux_glibc: {
1736 srcs: ["linux_glibc.cpp"],
1737 },
1738 darwin: {
1739 srcs: ["darwin.cpp"],
1740 },
1741 bionic: {
1742 srcs: ["bionic.cpp"],
1743 },
1744 linux_musl: {
1745 srcs: ["linux_musl.cpp"],
1746 },
1747 windows: {
1748 srcs: ["windows.cpp"],
1749 },
1750 },
1751 include_build_directory: false,
1752}
1753`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001754 expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{
1755 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001756 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001757 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001758 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001759 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001760 ],
1761 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
1762 "//build/bazel/platforms/os:linux": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001763 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001764 "linux_glibc.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001765 ],
1766 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001767 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001768 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001769 ],
1770 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04001771 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001772 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001773 ],
1774 "//build/bazel/platforms/os:windows": ["windows.cpp"],
1775 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001776 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001777 }),
1778 },
1779 )
Jingwen Chen97b85312021-10-08 10:41:31 +00001780}
1781
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001782func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00001783 type testCase struct {
1784 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001785 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001786 gnu_extensions string
1787 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001788 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00001789 }
1790
1791 testCases := []testCase{
1792 // Existing usages of cpp_std in AOSP are:
1793 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
1794 //
1795 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
1796 // is set in the toolchain.)
1797 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001798 {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001799 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
1800 // experimental defaults to gnu++2a
1801 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "gnu++2a"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001802 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001803 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "gnu++2a"},
1804 // Explicitly setting a c++ std does not use replace gnu++ std even if
1805 // gnu_extensions is true.
1806 // "c++11",
1807 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001808 {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001809 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
1810 // "c++17",
1811 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001812 {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001813 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
1814 // "c++2a",
1815 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001816 {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001817 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
1818 // "c++98",
1819 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001820 {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001821 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
1822 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
1823 // "gnu++11",
1824 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001825 {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001826 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
1827 // "gnu++17",
1828 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001829 {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001830 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05001831
1832 // some c_std test cases
1833 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "gnu11"},
1834 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
1835 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "gnu11"},
1836 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
1837 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
1838 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
Jingwen Chen97b85312021-10-08 10:41:31 +00001839 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001840 for i, tc := range testCases {
1841 name_prefix := fmt.Sprintf("a_%v", i)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001842 cppStdProp := ""
Jingwen Chen97b85312021-10-08 10:41:31 +00001843 if tc.cpp_std != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001844 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
Jingwen Chen97b85312021-10-08 10:41:31 +00001845 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001846 cStdProp := ""
1847 if tc.c_std != "" {
1848 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
1849 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001850 gnuExtensionsProp := ""
Jingwen Chen97b85312021-10-08 10:41:31 +00001851 if tc.gnu_extensions != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001852 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
Jingwen Chen97b85312021-10-08 10:41:31 +00001853 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001854 attrs := attrNameToString{}
Jingwen Chen97b85312021-10-08 10:41:31 +00001855 if tc.bazel_cpp_std != "" {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001856 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
Jingwen Chen97b85312021-10-08 10:41:31 +00001857 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05001858 if tc.bazel_c_std != "" {
1859 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
1860 }
Jingwen Chen97b85312021-10-08 10:41:31 +00001861
1862 runCcLibraryTestCase(t, bp2buildTestCase{
1863 description: fmt.Sprintf(
Chris Parsons79bd2b72021-11-29 17:52:41 -05001864 "cc_library with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
Jingwen Chen97b85312021-10-08 10:41:31 +00001865 moduleTypeUnderTest: "cc_library",
1866 moduleTypeUnderTestFactory: cc.LibraryFactory,
1867 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1868 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1869cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001870 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00001871%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001872%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00001873%s // gnu_extensions: *bool
1874 include_build_directory: false,
1875}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001876`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Chris Parsons77acf2e2021-12-03 17:27:16 -05001877 expectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Jingwen Chen97b85312021-10-08 10:41:31 +00001878 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001879
1880 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1881 description: fmt.Sprintf(
Chris Parsons79bd2b72021-11-29 17:52:41 -05001882 "cc_library_static with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001883 moduleTypeUnderTest: "cc_library_static",
1884 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1885 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1886 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1887cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001888 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001889%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001890%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001891%s // gnu_extensions: *bool
1892 include_build_directory: false,
1893}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001894`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001895 expectedBazelTargets: []string{
Chris Parsons79bd2b72021-11-29 17:52:41 -05001896 makeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001897 },
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001898 })
1899
1900 runCcLibrarySharedTestCase(t, bp2buildTestCase{
1901 description: fmt.Sprintf(
Chris Parsons79bd2b72021-11-29 17:52:41 -05001902 "cc_library_shared with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001903 moduleTypeUnderTest: "cc_library_shared",
1904 moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
1905 moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
1906 blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
1907cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05001908 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001909%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05001910%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001911%s // gnu_extensions: *bool
1912 include_build_directory: false,
1913}
Chris Parsons79bd2b72021-11-29 17:52:41 -05001914`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001915 expectedBazelTargets: []string{
Chris Parsons79bd2b72021-11-29 17:52:41 -05001916 makeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001917 },
Jingwen Chen5b11ab12021-10-11 17:44:33 +00001918 })
Jingwen Chen97b85312021-10-08 10:41:31 +00001919 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001920}
Liz Kammer12615db2021-09-28 09:19:17 -04001921
1922func TestCcLibraryProtoSimple(t *testing.T) {
1923 runCcLibraryTestCase(t, bp2buildTestCase{
1924 moduleTypeUnderTest: "cc_library",
1925 moduleTypeUnderTestFactory: cc.LibraryFactory,
1926 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1927 blueprint: soongCcProtoPreamble + `cc_library {
1928 name: "foo",
1929 srcs: ["foo.proto"],
1930 include_build_directory: false,
1931}`,
1932 expectedBazelTargets: []string{
1933 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
1934 "srcs": `["foo.proto"]`,
1935 "strip_import_prefix": `""`,
1936 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
1937 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001938 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04001939 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001940 "deps": `[":libprotobuf-cpp-lite"]`,
1941 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
1942 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04001943 }),
1944 },
1945 })
1946}
1947
1948func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
1949 runCcLibraryTestCase(t, bp2buildTestCase{
1950 moduleTypeUnderTest: "cc_library",
1951 moduleTypeUnderTestFactory: cc.LibraryFactory,
1952 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1953 blueprint: soongCcProtoPreamble + `cc_library {
1954 name: "foo",
1955 srcs: ["foo.proto"],
1956 proto: { canonical_path_from_root: false},
1957 include_build_directory: false,
1958}`,
1959 expectedBazelTargets: []string{
1960 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
1961 "srcs": `["foo.proto"]`,
1962 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
1963 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001964 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04001965 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001966 "deps": `[":libprotobuf-cpp-lite"]`,
1967 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
1968 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04001969 }),
1970 },
1971 })
1972}
1973
1974func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
1975 runCcLibraryTestCase(t, bp2buildTestCase{
1976 moduleTypeUnderTest: "cc_library",
1977 moduleTypeUnderTestFactory: cc.LibraryFactory,
1978 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1979 blueprint: soongCcProtoPreamble + `cc_library {
1980 name: "foo",
1981 srcs: ["foo.proto"],
1982 proto: { canonical_path_from_root: true},
1983 include_build_directory: false,
1984}`,
1985 expectedBazelTargets: []string{
1986 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
1987 "srcs": `["foo.proto"]`,
1988 "strip_import_prefix": `""`,
1989 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
1990 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001991 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04001992 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001993 "deps": `[":libprotobuf-cpp-lite"]`,
1994 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
1995 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04001996 }),
1997 },
1998 })
1999}
2000
2001func TestCcLibraryProtoFull(t *testing.T) {
2002 runCcLibraryTestCase(t, bp2buildTestCase{
2003 moduleTypeUnderTest: "cc_library",
2004 moduleTypeUnderTestFactory: cc.LibraryFactory,
2005 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
2006 blueprint: soongCcProtoPreamble + `cc_library {
2007 name: "foo",
2008 srcs: ["foo.proto"],
2009 proto: {
2010 canonical_path_from_root: false,
2011 type: "full",
2012 },
2013 include_build_directory: false,
2014}`,
2015 expectedBazelTargets: []string{
2016 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2017 "srcs": `["foo.proto"]`,
2018 }), makeBazelTarget("cc_proto_library", "foo_cc_proto", attrNameToString{
2019 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002020 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002021 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002022 "deps": `[":libprotobuf-cpp-full"]`,
2023 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2024 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002025 }),
2026 },
2027 })
2028}
2029
2030func TestCcLibraryProtoLite(t *testing.T) {
2031 runCcLibraryTestCase(t, bp2buildTestCase{
2032 moduleTypeUnderTest: "cc_library",
2033 moduleTypeUnderTestFactory: cc.LibraryFactory,
2034 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
2035 blueprint: soongCcProtoPreamble + `cc_library {
2036 name: "foo",
2037 srcs: ["foo.proto"],
2038 proto: {
2039 canonical_path_from_root: false,
2040 type: "lite",
2041 },
2042 include_build_directory: false,
2043}`,
2044 expectedBazelTargets: []string{
2045 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2046 "srcs": `["foo.proto"]`,
2047 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2048 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002049 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002050 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002051 "deps": `[":libprotobuf-cpp-lite"]`,
2052 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2053 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002054 }),
2055 },
2056 })
2057}
2058
2059func TestCcLibraryProtoExportHeaders(t *testing.T) {
2060 runCcLibraryTestCase(t, bp2buildTestCase{
2061 moduleTypeUnderTest: "cc_library",
2062 moduleTypeUnderTestFactory: cc.LibraryFactory,
2063 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
2064 blueprint: soongCcProtoPreamble + `cc_library {
2065 name: "foo",
2066 srcs: ["foo.proto"],
2067 proto: {
2068 canonical_path_from_root: false,
2069 export_proto_headers: true,
2070 },
2071 include_build_directory: false,
2072}`,
2073 expectedBazelTargets: []string{
2074 makeBazelTarget("proto_library", "foo_proto", attrNameToString{
2075 "srcs": `["foo.proto"]`,
2076 }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
2077 "deps": `[":foo_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002078 }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{
2079 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002080 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002081 }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{
2082 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2083 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002084 }),
2085 },
2086 })
2087}