blob: 5f3ff6217d589464689880d97e046fba7643fb19 [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
Vinh Tran367d89d2023-04-28 11:21:25 -040021 "android/soong/aidl_library"
Jingwen Chen63930982021-03-24 10:04:33 -040022 "android/soong/android"
23 "android/soong/cc"
Jingwen Chen63930982021-03-24 10:04:33 -040024)
25
26const (
27 // See cc/testing.go for more context
28 soongCcLibraryPreamble = `
29cc_defaults {
Liz Kammer8337ea42021-09-10 10:06:32 -040030 name: "linux_bionic_supported",
Liz Kammerbaced712022-09-16 09:01:29 -040031}
32`
33
34 soongCcVersionLibBpPath = "build/soong/cc/libbuildversion/Android.bp"
35 soongCcVersionLibBp = `
36cc_library_static {
37 name: "libbuildversion",
38 bazel_module: { bp2build_available: false },
39}
40`
Liz Kammer12615db2021-09-28 09:19:17 -040041
42 soongCcProtoLibraries = `
43cc_library {
44 name: "libprotobuf-cpp-lite",
45 bazel_module: { bp2build_available: false },
46}
47
48cc_library {
49 name: "libprotobuf-cpp-full",
50 bazel_module: { bp2build_available: false },
51}`
52
53 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040054)
55
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040057 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059}
60
61func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
62 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000063 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040065 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020066 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Vinh Tran367d89d2023-04-28 11:21:25 -040067 ctx.RegisterModuleType("aidl_library", aidl_library.AidlLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020068}
69
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020070func TestCcLibrarySimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000071 runCcLibraryTestCase(t, Bp2buildTestCase{
72 Description: "cc_library - simple example",
73 ModuleTypeUnderTest: "cc_library",
74 ModuleTypeUnderTestFactory: cc.LibraryFactory,
75 Filesystem: map[string]string{
Liz Kammerbaced712022-09-16 09:01:29 -040076 soongCcVersionLibBpPath: soongCcVersionLibBp,
77 "android.cpp": "",
78 "bionic.cpp": "",
79 "darwin.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020080 // Refer to cc.headerExts for the supported header extensions in Soong.
81 "header.h": "",
82 "header.hh": "",
83 "header.hpp": "",
84 "header.hxx": "",
85 "header.h++": "",
86 "header.inl": "",
87 "header.inc": "",
88 "header.ipp": "",
89 "header.h.generic": "",
90 "impl.cpp": "",
91 "linux.cpp": "",
92 "x86.cpp": "",
93 "x86_64.cpp": "",
94 "foo-dir/a.h": "",
95 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000096 Blueprint: soongCcLibraryPreamble +
Liz Kammer78cfdaa2021-11-08 12:56:31 -050097 simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -040098cc_library {
99 name: "foo-lib",
100 srcs: ["impl.cpp"],
101 cflags: ["-Wall"],
102 header_libs: ["some-headers"],
103 export_include_dirs: ["foo-dir"],
104 ldflags: ["-Wl,--exclude-libs=bar.a"],
105 arch: {
106 x86: {
107 ldflags: ["-Wl,--exclude-libs=baz.a"],
108 srcs: ["x86.cpp"],
109 },
110 x86_64: {
111 ldflags: ["-Wl,--exclude-libs=qux.a"],
112 srcs: ["x86_64.cpp"],
113 },
114 },
115 target: {
116 android: {
117 srcs: ["android.cpp"],
118 },
119 linux_glibc: {
120 srcs: ["linux.cpp"],
121 },
122 darwin: {
123 srcs: ["darwin.cpp"],
124 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400125 bionic: {
126 srcs: ["bionic.cpp"]
127 },
Jingwen Chen63930982021-03-24 10:04:33 -0400128 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400129 include_build_directory: false,
Yu Liufc603162022-03-01 15:44:08 -0800130 sdk_version: "current",
131 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700132 use_version_lib: true,
Jingwen Chen63930982021-03-24 10:04:33 -0400133}
134`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000135 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500136 "copts": `["-Wall"]`,
137 "export_includes": `["foo-dir"]`,
138 "implementation_deps": `[":some-headers"]`,
139 "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000140 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
141 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
142 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500144 "srcs": `["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000145 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
146 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400147 "//conditions:default": [],
148 }) + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400149 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400150 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -0400151 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400152 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000153 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400154 "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800155 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Liz Kammer01a16e82021-07-16 16:33:47 -0400156 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500157 })`,
Yu Liufe978fd2023-04-24 16:37:18 -0700158 "sdk_version": `"current"`,
159 "min_sdk_version": `"29"`,
160 "use_version_lib": `True`,
161 "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500162 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500163 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200164}
165
166func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000167 runCcLibraryTestCase(t, Bp2buildTestCase{
168 Description: "cc_library - trimmed example of //bionic/linker:ld-android",
169 ModuleTypeUnderTest: "cc_library",
170 ModuleTypeUnderTestFactory: cc.LibraryFactory,
171 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200172 "ld-android.cpp": "",
173 "linked_list.h": "",
174 "linker.h": "",
175 "linker_block_allocator.h": "",
176 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400177 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000178 Blueprint: soongCcLibraryPreamble +
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400179 simpleModuleDoNotConvertBp2build("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400180cc_library {
181 name: "fake-ld-android",
182 srcs: ["ld_android.cpp"],
183 cflags: [
184 "-Wall",
185 "-Wextra",
186 "-Wunused",
187 "-Werror",
188 ],
189 header_libs: ["libc_headers"],
190 ldflags: [
191 "-Wl,--exclude-libs=libgcc.a",
192 "-Wl,--exclude-libs=libgcc_stripped.a",
193 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
194 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
195 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
196 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
197 ],
198 arch: {
199 x86: {
200 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
201 },
202 x86_64: {
203 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
204 },
205 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400206 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400207}
208`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000209 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500210 "srcs": `["ld_android.cpp"]`,
211 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400212 "-Wall",
213 "-Wextra",
214 "-Wunused",
215 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500216 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500217 "implementation_deps": `[":libc_headers"]`,
218 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400219 "-Wl,--exclude-libs=libgcc.a",
220 "-Wl,--exclude-libs=libgcc_stripped.a",
221 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
222 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
225 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000226 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
227 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400228 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500229 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500230 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200231 })
232}
233
234func TestCcLibraryExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000235 runCcLibraryTestCase(t, Bp2buildTestCase{
236 Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
237 ModuleTypeUnderTest: "cc_library",
238 ModuleTypeUnderTestFactory: cc.LibraryFactory,
239 Dir: "external",
240 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200241 "external/math/cosf.c": "",
242 "external/math/erf.c": "",
243 "external/math/erf_data.c": "",
244 "external/math/erff.c": "",
245 "external/math/erff_data.c": "",
246 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000247cc_library {
248 name: "fake-libarm-optimized-routines-math",
249 exclude_srcs: [
250 // Provided by:
251 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
252 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
253 "math/erf.c",
254 "math/erf_data.c",
255 "math/erff.c",
256 "math/erff_data.c",
257 ],
258 srcs: [
259 "math/*.c",
260 ],
261 // arch-specific settings
262 arch: {
263 arm64: {
264 cflags: [
265 "-DHAVE_FAST_FMA=1",
266 ],
267 },
268 },
269 bazel_module: { bp2build_available: true },
270}
271`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200272 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000273 Blueprint: soongCcLibraryPreamble,
274 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500275 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000276 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
277 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500278 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500279 "local_includes": `["."]`,
280 "srcs_c": `["math/cosf.c"]`,
281 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200282 })
283}
284
285func TestCcLibrarySharedStaticProps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000286 runCcLibraryTestCase(t, Bp2buildTestCase{
287 Description: "cc_library shared/static props",
288 ModuleTypeUnderTest: "cc_library",
289 ModuleTypeUnderTestFactory: cc.LibraryFactory,
290 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400291 "both.cpp": "",
292 "sharedonly.cpp": "",
293 "staticonly.cpp": "",
294 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000295 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000296cc_library {
297 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400298 srcs: ["both.cpp"],
299 cflags: ["bothflag"],
300 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400301 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
302 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400303 static: {
304 srcs: ["staticonly.cpp"],
305 cflags: ["staticflag"],
306 shared_libs: ["shared_dep_for_static"],
307 static_libs: ["static_dep_for_static"],
308 whole_static_libs: ["whole_static_lib_for_static"],
309 },
310 shared: {
311 srcs: ["sharedonly.cpp"],
312 cflags: ["sharedflag"],
313 shared_libs: ["shared_dep_for_shared"],
314 static_libs: ["static_dep_for_shared"],
315 whole_static_libs: ["whole_static_lib_for_shared"],
316 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400317 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000318}
319
Liz Kammer8337ea42021-09-10 10:06:32 -0400320cc_library_static {
321 name: "static_dep_for_shared",
322 bazel_module: { bp2build_available: false },
323}
Chris Parsons08648312021-05-06 16:23:19 -0400324
Liz Kammer8337ea42021-09-10 10:06:32 -0400325cc_library_static {
326 name: "static_dep_for_static",
327 bazel_module: { bp2build_available: false },
328}
Chris Parsons08648312021-05-06 16:23:19 -0400329
Liz Kammer8337ea42021-09-10 10:06:32 -0400330cc_library_static {
331 name: "static_dep_for_both",
332 bazel_module: { bp2build_available: false },
333}
Chris Parsons08648312021-05-06 16:23:19 -0400334
Liz Kammer8337ea42021-09-10 10:06:32 -0400335cc_library_static {
336 name: "whole_static_lib_for_shared",
337 bazel_module: { bp2build_available: false },
338}
Chris Parsons08648312021-05-06 16:23:19 -0400339
Liz Kammer8337ea42021-09-10 10:06:32 -0400340cc_library_static {
341 name: "whole_static_lib_for_static",
342 bazel_module: { bp2build_available: false },
343}
Chris Parsons08648312021-05-06 16:23:19 -0400344
Liz Kammer8337ea42021-09-10 10:06:32 -0400345cc_library_static {
346 name: "whole_static_lib_for_both",
347 bazel_module: { bp2build_available: false },
348}
Chris Parsons08648312021-05-06 16:23:19 -0400349
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400350cc_library_static {
351 name: "whole_and_static_lib_for_both",
352 bazel_module: { bp2build_available: false },
353}
354
Liz Kammer8337ea42021-09-10 10:06:32 -0400355cc_library {
356 name: "shared_dep_for_shared",
357 bazel_module: { bp2build_available: false },
358}
Chris Parsons08648312021-05-06 16:23:19 -0400359
Liz Kammer8337ea42021-09-10 10:06:32 -0400360cc_library {
361 name: "shared_dep_for_static",
362 bazel_module: { bp2build_available: false },
363}
Chris Parsons08648312021-05-06 16:23:19 -0400364
Liz Kammer8337ea42021-09-10 10:06:32 -0400365cc_library {
366 name: "shared_dep_for_both",
367 bazel_module: { bp2build_available: false },
368}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000369`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000370 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000371 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500372 "copts": `[
373 "bothflag",
374 "staticflag",
375 ]`,
376 "implementation_deps": `[
377 ":static_dep_for_both",
378 ":static_dep_for_static",
379 ]`,
380 "implementation_dynamic_deps": `[
381 ":shared_dep_for_both",
382 ":shared_dep_for_static",
383 ]`,
384 "srcs": `[
385 "both.cpp",
386 "staticonly.cpp",
387 ]`,
388 "whole_archive_deps": `[
389 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400390 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500391 ":whole_static_lib_for_static",
392 ]`}),
Alixe06d75b2022-08-31 18:28:19 +0000393 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500394 "copts": `[
395 "bothflag",
396 "sharedflag",
397 ]`,
398 "implementation_deps": `[
399 ":static_dep_for_both",
400 ":static_dep_for_shared",
401 ]`,
402 "implementation_dynamic_deps": `[
403 ":shared_dep_for_both",
404 ":shared_dep_for_shared",
405 ]`,
406 "srcs": `[
407 "both.cpp",
408 "sharedonly.cpp",
409 ]`,
410 "whole_archive_deps": `[
411 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400412 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500413 ":whole_static_lib_for_shared",
414 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500415 }),
416 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200417 })
418}
419
Liz Kammer7a210ac2021-09-22 15:52:58 -0400420func TestCcLibraryDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000421 runCcLibraryTestCase(t, Bp2buildTestCase{
422 Description: "cc_library shared/static props",
423 ModuleTypeUnderTest: "cc_library",
424 ModuleTypeUnderTestFactory: cc.LibraryFactory,
425 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400426 "both.cpp": "",
427 "sharedonly.cpp": "",
428 "staticonly.cpp": "",
429 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000430 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400431cc_library {
432 name: "a",
433 srcs: ["both.cpp"],
434 cflags: ["bothflag"],
435 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
436 export_shared_lib_headers: ["shared_dep_for_both"],
437 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
438 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
439 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
440 static: {
441 srcs: ["staticonly.cpp"],
442 cflags: ["staticflag"],
443 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
444 export_shared_lib_headers: ["shared_dep_for_static"],
445 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
446 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
447 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
448 },
449 shared: {
450 srcs: ["sharedonly.cpp"],
451 cflags: ["sharedflag"],
452 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
453 export_shared_lib_headers: ["shared_dep_for_shared"],
454 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
455 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
456 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
457 },
458 include_build_directory: false,
459}
460` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
461 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
462 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
463 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
464 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
465 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
466 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
467 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
468 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
469 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
470 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
471 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
472 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
473 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
474 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
475 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
476 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
477 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000478 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000479 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500480 "copts": `[
481 "bothflag",
482 "staticflag",
483 ]`,
484 "deps": `[
485 ":static_dep_for_both",
486 ":static_dep_for_static",
487 ]`,
488 "dynamic_deps": `[
489 ":shared_dep_for_both",
490 ":shared_dep_for_static",
491 ]`,
492 "implementation_deps": `[
493 ":implementation_static_dep_for_both",
494 ":implementation_static_dep_for_static",
495 ]`,
496 "implementation_dynamic_deps": `[
497 ":implementation_shared_dep_for_both",
498 ":implementation_shared_dep_for_static",
499 ]`,
500 "srcs": `[
501 "both.cpp",
502 "staticonly.cpp",
503 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500504 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400505 ":not_explicitly_exported_whole_static_dep_for_both",
506 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500507 ":not_explicitly_exported_whole_static_dep_for_static",
508 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500509 ]`,
510 }),
Alixe06d75b2022-08-31 18:28:19 +0000511 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500512 "copts": `[
513 "bothflag",
514 "sharedflag",
515 ]`,
516 "deps": `[
517 ":static_dep_for_both",
518 ":static_dep_for_shared",
519 ]`,
520 "dynamic_deps": `[
521 ":shared_dep_for_both",
522 ":shared_dep_for_shared",
523 ]`,
524 "implementation_deps": `[
525 ":implementation_static_dep_for_both",
526 ":implementation_static_dep_for_shared",
527 ]`,
528 "implementation_dynamic_deps": `[
529 ":implementation_shared_dep_for_both",
530 ":implementation_shared_dep_for_shared",
531 ]`,
532 "srcs": `[
533 "both.cpp",
534 "sharedonly.cpp",
535 ]`,
536 "whole_archive_deps": `[
537 ":not_explicitly_exported_whole_static_dep_for_both",
538 ":whole_static_dep_for_both",
539 ":not_explicitly_exported_whole_static_dep_for_shared",
540 ":whole_static_dep_for_shared",
541 ]`,
542 })},
543 },
544 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400545}
546
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400547func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000548 runCcLibraryTestCase(t, Bp2buildTestCase{
549 ModuleTypeUnderTest: "cc_library",
550 ModuleTypeUnderTestFactory: cc.LibraryFactory,
551 Dir: "foo/bar",
552 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400553 "foo/bar/Android.bp": `
554cc_library {
555 name: "a",
556 whole_static_libs: ["whole_static_lib_for_both"],
557 static: {
558 whole_static_libs: ["whole_static_lib_for_static"],
559 },
560 shared: {
561 whole_static_libs: ["whole_static_lib_for_shared"],
562 },
563 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400564 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400565}
566
567cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
568
569cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
570
571cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
572`,
573 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000574 Blueprint: soongCcLibraryPreamble,
575 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000576 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500577 "whole_archive_deps": `[
578 ":whole_static_lib_for_both_alwayslink",
579 ":whole_static_lib_for_static_alwayslink",
580 ]`,
581 }),
Alixe06d75b2022-08-31 18:28:19 +0000582 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500583 "whole_archive_deps": `[
584 ":whole_static_lib_for_both_alwayslink",
585 ":whole_static_lib_for_shared_alwayslink",
586 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500587 }),
588 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500589 },
590 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400591}
592
Jingwen Chenbcf53042021-05-26 04:42:42 +0000593func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000594 runCcLibraryTestCase(t, Bp2buildTestCase{
595 Description: "cc_library shared/static props in arch",
596 ModuleTypeUnderTest: "cc_library",
597 ModuleTypeUnderTestFactory: cc.LibraryFactory,
598 Dir: "foo/bar",
599 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000600 "foo/bar/arm.cpp": "",
601 "foo/bar/x86.cpp": "",
602 "foo/bar/sharedonly.cpp": "",
603 "foo/bar/staticonly.cpp": "",
604 "foo/bar/Android.bp": `
605cc_library {
606 name: "a",
607 arch: {
608 arm: {
609 shared: {
610 srcs: ["arm_shared.cpp"],
611 cflags: ["-DARM_SHARED"],
612 static_libs: ["arm_static_dep_for_shared"],
613 whole_static_libs: ["arm_whole_static_dep_for_shared"],
614 shared_libs: ["arm_shared_dep_for_shared"],
615 },
616 },
617 x86: {
618 static: {
619 srcs: ["x86_static.cpp"],
620 cflags: ["-DX86_STATIC"],
621 static_libs: ["x86_dep_for_static"],
622 },
623 },
624 },
625 target: {
626 android: {
627 shared: {
628 srcs: ["android_shared.cpp"],
629 cflags: ["-DANDROID_SHARED"],
630 static_libs: ["android_dep_for_shared"],
631 },
632 },
633 android_arm: {
634 shared: {
635 cflags: ["-DANDROID_ARM_SHARED"],
636 },
637 },
638 },
639 srcs: ["both.cpp"],
640 cflags: ["bothflag"],
641 static_libs: ["static_dep_for_both"],
642 static: {
643 srcs: ["staticonly.cpp"],
644 cflags: ["staticflag"],
645 static_libs: ["static_dep_for_static"],
646 },
647 shared: {
648 srcs: ["sharedonly.cpp"],
649 cflags: ["sharedflag"],
650 static_libs: ["static_dep_for_shared"],
651 },
652 bazel_module: { bp2build_available: true },
653}
654
655cc_library_static { name: "static_dep_for_shared" }
656cc_library_static { name: "static_dep_for_static" }
657cc_library_static { name: "static_dep_for_both" }
658
659cc_library_static { name: "arm_static_dep_for_shared" }
660cc_library_static { name: "arm_whole_static_dep_for_shared" }
661cc_library_static { name: "arm_shared_dep_for_shared" }
662
663cc_library_static { name: "x86_dep_for_static" }
664
665cc_library_static { name: "android_dep_for_shared" }
666`,
667 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000668 Blueprint: soongCcLibraryPreamble,
669 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000670 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500671 "copts": `[
672 "bothflag",
673 "staticflag",
674 ] + select({
675 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
676 "//conditions:default": [],
677 })`,
678 "implementation_deps": `[
679 ":static_dep_for_both",
680 ":static_dep_for_static",
681 ] + select({
682 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
683 "//conditions:default": [],
684 })`,
685 "local_includes": `["."]`,
686 "srcs": `[
687 "both.cpp",
688 "staticonly.cpp",
689 ] + select({
690 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
691 "//conditions:default": [],
692 })`,
693 }),
Alixe06d75b2022-08-31 18:28:19 +0000694 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500695 "copts": `[
696 "bothflag",
697 "sharedflag",
698 ] + select({
699 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
700 "//conditions:default": [],
701 }) + select({
702 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
703 "//conditions:default": [],
704 }) + select({
705 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
706 "//conditions:default": [],
707 })`,
708 "implementation_deps": `[
709 ":static_dep_for_both",
710 ":static_dep_for_shared",
711 ] + select({
712 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
713 "//conditions:default": [],
714 }) + select({
715 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
716 "//conditions:default": [],
717 })`,
718 "implementation_dynamic_deps": `select({
719 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
720 "//conditions:default": [],
721 })`,
722 "local_includes": `["."]`,
723 "srcs": `[
724 "both.cpp",
725 "sharedonly.cpp",
726 ] + select({
727 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
728 "//conditions:default": [],
729 }) + select({
730 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
731 "//conditions:default": [],
732 })`,
733 "whole_archive_deps": `select({
734 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
735 "//conditions:default": [],
736 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500737 }),
738 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500739 },
740 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000741}
742
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000743func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000744 runCcLibraryTestCase(t, Bp2buildTestCase{
745 Description: "cc_library shared/static props with c/cpp/s mixed sources",
746 ModuleTypeUnderTest: "cc_library",
747 ModuleTypeUnderTestFactory: cc.LibraryFactory,
748 Dir: "foo/bar",
749 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000750 "foo/bar/both_source.cpp": "",
751 "foo/bar/both_source.cc": "",
752 "foo/bar/both_source.c": "",
753 "foo/bar/both_source.s": "",
754 "foo/bar/both_source.S": "",
755 "foo/bar/shared_source.cpp": "",
756 "foo/bar/shared_source.cc": "",
757 "foo/bar/shared_source.c": "",
758 "foo/bar/shared_source.s": "",
759 "foo/bar/shared_source.S": "",
760 "foo/bar/static_source.cpp": "",
761 "foo/bar/static_source.cc": "",
762 "foo/bar/static_source.c": "",
763 "foo/bar/static_source.s": "",
764 "foo/bar/static_source.S": "",
765 "foo/bar/Android.bp": `
766cc_library {
767 name: "a",
768 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400769 "both_source.cpp",
770 "both_source.cc",
771 "both_source.c",
772 "both_source.s",
773 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400774 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400775 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000776 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400777 srcs: [
778 "static_source.cpp",
779 "static_source.cc",
780 "static_source.c",
781 "static_source.s",
782 "static_source.S",
783 ":static_filegroup",
784 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000785 },
786 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400787 srcs: [
788 "shared_source.cpp",
789 "shared_source.cc",
790 "shared_source.c",
791 "shared_source.s",
792 "shared_source.S",
793 ":shared_filegroup",
794 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000795 },
796 bazel_module: { bp2build_available: true },
797}
798
799filegroup {
800 name: "both_filegroup",
801 srcs: [
802 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400803 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000804}
805
806filegroup {
807 name: "shared_filegroup",
808 srcs: [
809 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400810 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000811}
812
813filegroup {
814 name: "static_filegroup",
815 srcs: [
816 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400817 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000818}
819`,
820 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000821 Blueprint: soongCcLibraryPreamble,
822 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000823 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500824 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500825 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000826 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400827 "both_source.cc",
828 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500829 "static_source.cpp",
830 "static_source.cc",
831 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500832 ]`,
833 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000834 "both_source.s",
835 "both_source.S",
836 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500837 "static_source.s",
838 "static_source.S",
839 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500840 ]`,
841 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000842 "both_source.c",
843 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500844 "static_source.c",
845 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500846 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500847 }),
Alixe06d75b2022-08-31 18:28:19 +0000848 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500849 "local_includes": `["."]`,
850 "srcs": `[
851 "both_source.cpp",
852 "both_source.cc",
853 ":both_filegroup_cpp_srcs",
854 "shared_source.cpp",
855 "shared_source.cc",
856 ":shared_filegroup_cpp_srcs",
857 ]`,
858 "srcs_as": `[
859 "both_source.s",
860 "both_source.S",
861 ":both_filegroup_as_srcs",
862 "shared_source.s",
863 "shared_source.S",
864 ":shared_filegroup_as_srcs",
865 ]`,
866 "srcs_c": `[
867 "both_source.c",
868 ":both_filegroup_c_srcs",
869 "shared_source.c",
870 ":shared_filegroup_c_srcs",
871 ]`,
872 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000873}
874
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000875func TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000876 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000877 Description: "cc_library non-configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000878 ModuleTypeUnderTest: "cc_library",
879 ModuleTypeUnderTestFactory: cc.LibraryFactory,
880 Dir: "foo/bar",
881 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200882 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200883cc_library {
884 name: "a",
885 srcs: ["a.cpp"],
886 version_script: "v.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000887 dynamic_list: "dynamic.list",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200888 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400889 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200890}
891`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200892 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000893 Blueprint: soongCcLibraryPreamble,
894 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000895 "additional_linker_inputs": `[
896 "v.map",
897 "dynamic.list",
898 ]`,
899 "linkopts": `[
900 "-Wl,--version-script,$(location v.map)",
901 "-Wl,--dynamic-list,$(location dynamic.list)",
902 ]`,
903 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500904 }),
905 },
906 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200907}
908
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000909func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000910 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000911 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000912 ModuleTypeUnderTest: "cc_library",
913 ModuleTypeUnderTestFactory: cc.LibraryFactory,
914 Dir: "foo/bar",
915 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200916 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400917cc_library {
918 name: "a",
919 srcs: ["a.cpp"],
920 arch: {
921 arm: {
922 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000923 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400924 },
925 arm64: {
926 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000927 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400928 },
929 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200930
Liz Kammer8337ea42021-09-10 10:06:32 -0400931 bazel_module: { bp2build_available: true },
932 include_build_directory: false,
933}
Liz Kammerd366c902021-06-03 13:43:01 -0400934 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200935 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000936 Blueprint: soongCcLibraryPreamble,
937 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500938 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000939 "//build/bazel/platforms/arch:arm": [
940 "arm.map",
941 "dynamic_arm.list",
942 ],
943 "//build/bazel/platforms/arch:arm64": [
944 "arm64.map",
945 "dynamic_arm64.list",
946 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400947 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500948 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500949 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000950 "//build/bazel/platforms/arch:arm": [
951 "-Wl,--version-script,$(location arm.map)",
952 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
953 ],
954 "//build/bazel/platforms/arch:arm64": [
955 "-Wl,--version-script,$(location arm64.map)",
956 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
957 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400958 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500959 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500960 "srcs": `["a.cpp"]`,
961 }),
962 },
963 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200964}
965
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000966func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
967 runCcLibraryTestCase(t, Bp2buildTestCase{
968 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
969 ModuleTypeUnderTest: "cc_library",
970 ModuleTypeUnderTestFactory: cc.LibraryFactory,
971 Filesystem: map[string]string{
972 "version_script": "",
973 "dynamic.list": "",
974 },
975 Blueprint: `
976cc_library {
977 name: "foo",
978 ldflags: [
979 "--nospace_flag",
980 "-z spaceflag",
981 ],
982 version_script: "version_script",
983 dynamic_list: "dynamic.list",
984 include_build_directory: false,
985}
986`,
987 ExpectedBazelTargets: []string{
988 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
989 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
990 "additional_linker_inputs": `[
991 "version_script",
992 "dynamic.list",
993 ]`,
994 "linkopts": `[
995 "--nospace_flag",
996 "-z",
997 "spaceflag",
998 "-Wl,--version-script,$(location version_script)",
999 "-Wl,--dynamic-list,$(location dynamic.list)",
1000 ]`,
1001 }),
1002 },
1003 })
1004}
1005
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001006func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001007 runCcLibraryTestCase(t, Bp2buildTestCase{
1008 Description: "cc_library shared_libs",
1009 ModuleTypeUnderTest: "cc_library",
1010 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1011 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001012cc_library {
1013 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -04001014 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001015}
1016
1017cc_library {
1018 name: "a",
1019 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001020 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001021}
1022`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001023 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001024 "implementation_dynamic_deps": `[":mylib"]`,
1025 }),
1026 },
1027 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001028}
1029
Liz Kammer0eae52e2021-10-06 10:32:26 -04001030func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001031 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001032 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001033 "features": `[
1034 "disable_pack_relocations",
1035 "-no_undefined_symbols",
1036 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001037 "native_coverage": `False`,
1038 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001039 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001040 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001041 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001042 "//build/bazel/platforms/arch:x86_64": [
1043 "disable_pack_relocations",
1044 "-no_undefined_symbols",
1045 ],
1046 "//conditions:default": [],
1047 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001048 "native_coverage": `False`,
1049 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001050 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001051 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001052 "features": `select({
1053 "//build/bazel/platforms/os:darwin": [
1054 "disable_pack_relocations",
1055 "-no_undefined_symbols",
1056 ],
1057 "//conditions:default": [],
1058 })`,
1059 "srcs": `["c.cpp"]`,
1060 })...)
1061
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001062 runCcLibraryTestCase(t, Bp2buildTestCase{
1063 Description: "cc_library pack_relocations test",
1064 ModuleTypeUnderTest: "cc_library",
1065 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1066 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001067cc_library {
1068 name: "a",
1069 srcs: ["a.cpp"],
1070 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001071 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001072 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001073 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001074}
1075
1076cc_library {
1077 name: "b",
1078 srcs: ["b.cpp"],
1079 arch: {
1080 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001081 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001082 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001083 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001084 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001085 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001086 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001087}
1088
1089cc_library {
1090 name: "c",
1091 srcs: ["c.cpp"],
1092 target: {
1093 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001094 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001095 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001096 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001097 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001098 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001099}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001100 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001101 })
1102}
1103
1104func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001105 runCcLibraryTestCase(t, Bp2buildTestCase{
1106 Description: "cc_library spaces in copts",
1107 ModuleTypeUnderTest: "cc_library",
1108 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1109 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001110cc_library {
1111 name: "a",
1112 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001113 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001114}
1115`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001116 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001117 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001118 "-include",
1119 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001120 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001121 }),
1122 },
1123 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001124}
1125
1126func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001127 runCcLibraryTestCase(t, Bp2buildTestCase{
1128 Description: "cc_library cppflags usage",
1129 ModuleTypeUnderTest: "cc_library",
1130 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1131 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001132 name: "a",
1133 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001134 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001135 cppflags: [
1136 "-fsigned-char",
1137 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001138 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001139 arch: {
1140 arm64: {
1141 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001142 },
Liz Kammerd366c902021-06-03 13:43:01 -04001143 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001144 target: {
1145 android: {
1146 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001147 },
Liz Kammerd366c902021-06-03 13:43:01 -04001148 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001149 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001150}
1151`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001152 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001153 "copts": `["-Wall"]`,
1154 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001155 "-fsigned-char",
1156 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001157 ] + select({
1158 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1159 "//conditions:default": [],
1160 }) + select({
1161 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1162 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001163 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001164 "srcs": `["a.cpp"]`,
1165 }),
1166 },
1167 )
Jingwen Chen63930982021-03-24 10:04:33 -04001168}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001169
Liz Kammer47535c52021-06-02 16:02:22 -04001170func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001171 runCcLibraryTestCase(t, Bp2buildTestCase{
1172 ModuleTypeUnderTest: "cc_library",
1173 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1174 Filesystem: map[string]string{},
1175 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001176cc_library {
1177 name: "foo_static",
1178 srcs: ["common.c"],
1179 whole_static_libs: [
1180 "arm_whole_static_lib_excludes",
1181 "malloc_not_svelte_whole_static_lib_excludes"
1182 ],
1183 static_libs: [
1184 "arm_static_lib_excludes",
1185 "malloc_not_svelte_static_lib_excludes"
1186 ],
1187 shared_libs: [
1188 "arm_shared_lib_excludes",
1189 ],
1190 arch: {
1191 arm: {
1192 exclude_shared_libs: [
1193 "arm_shared_lib_excludes",
1194 ],
1195 exclude_static_libs: [
1196 "arm_static_lib_excludes",
1197 "arm_whole_static_lib_excludes",
1198 ],
1199 },
1200 },
1201 product_variables: {
1202 malloc_not_svelte: {
1203 shared_libs: ["malloc_not_svelte_shared_lib"],
1204 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1205 exclude_static_libs: [
1206 "malloc_not_svelte_static_lib_excludes",
1207 "malloc_not_svelte_whole_static_lib_excludes",
1208 ],
1209 },
1210 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001211 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001212}
1213
1214cc_library {
1215 name: "arm_whole_static_lib_excludes",
1216 bazel_module: { bp2build_available: false },
1217}
1218
1219cc_library {
1220 name: "malloc_not_svelte_whole_static_lib",
1221 bazel_module: { bp2build_available: false },
1222}
1223
1224cc_library {
1225 name: "malloc_not_svelte_whole_static_lib_excludes",
1226 bazel_module: { bp2build_available: false },
1227}
1228
1229cc_library {
1230 name: "arm_static_lib_excludes",
1231 bazel_module: { bp2build_available: false },
1232}
1233
1234cc_library {
1235 name: "malloc_not_svelte_static_lib_excludes",
1236 bazel_module: { bp2build_available: false },
1237}
1238
1239cc_library {
1240 name: "arm_shared_lib_excludes",
1241 bazel_module: { bp2build_available: false },
1242}
1243
1244cc_library {
1245 name: "malloc_not_svelte_shared_lib",
1246 bazel_module: { bp2build_available: false },
1247}
1248`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001249 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001250 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001251 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001252 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001253 }) + select({
1254 "//build/bazel/product_variables:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001255 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001256 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001257 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001258 "//build/bazel/platforms/arch:arm": [],
1259 "//conditions:default": [":arm_shared_lib_excludes"],
1260 }) + select({
1261 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1262 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001263 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001264 "srcs_c": `["common.c"]`,
1265 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001266 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001267 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001268 }) + select({
Chris Parsons953b3562021-09-20 15:14:39 -04001269 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
1270 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001271 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001272 }),
1273 },
1274 )
Liz Kammer47535c52021-06-02 16:02:22 -04001275}
Liz Kammerd366c902021-06-03 13:43:01 -04001276
Zi Wang0a8a1292022-08-30 06:27:01 +00001277func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1278 runCcLibraryTestCase(t, Bp2buildTestCase{
1279 ModuleTypeUnderTest: "cc_library",
1280 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1281 Filesystem: map[string]string{},
1282 Blueprint: soongCcLibraryStaticPreamble + `
1283cc_library {
1284 name: "foo_static",
1285 srcs: ["common.c"],
1286 product_variables: {
1287 malloc_not_svelte: {
1288 header_libs: ["malloc_not_svelte_header_lib"],
1289 },
1290 },
1291 include_build_directory: false,
1292}
1293
1294cc_library {
1295 name: "malloc_not_svelte_header_lib",
1296 bazel_module: { bp2build_available: false },
1297}
1298`,
1299 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1300 "implementation_deps": `select({
1301 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
1302 "//conditions:default": [],
1303 })`,
1304 "srcs_c": `["common.c"]`,
1305 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1306 }),
1307 },
1308 )
1309}
1310
Liz Kammerd366c902021-06-03 13:43:01 -04001311func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001312 runCcLibraryTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001313 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001314 ModuleTypeUnderTest: "cc_library",
1315 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1316 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001317 "impl.cpp": "",
1318 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001319 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001320cc_library {
1321 name: "foo-lib",
1322 srcs: ["impl.cpp"],
1323 nocrt: true,
1324 include_build_directory: false,
1325}
1326`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001327 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001328 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001329 "srcs": `["impl.cpp"]`,
1330 }),
1331 },
1332 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001333}
1334
1335func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001336 runCcLibraryTestCase(t, Bp2buildTestCase{
1337 Description: "cc_library - nocrt: false - does not emit attribute",
1338 ModuleTypeUnderTest: "cc_library",
1339 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1340 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001341 "impl.cpp": "",
1342 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001343 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001344cc_library {
1345 name: "foo-lib",
1346 srcs: ["impl.cpp"],
1347 nocrt: false,
1348 include_build_directory: false,
1349}
1350`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001352 "srcs": `["impl.cpp"]`,
1353 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001354 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001355}
1356
1357func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001358 runCcLibraryTestCase(t, Bp2buildTestCase{
1359 Description: "cc_library - nocrt in select",
1360 ModuleTypeUnderTest: "cc_library",
1361 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1362 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001363 "impl.cpp": "",
1364 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001365 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001366cc_library {
1367 name: "foo-lib",
1368 srcs: ["impl.cpp"],
1369 arch: {
1370 arm: {
1371 nocrt: true,
1372 },
1373 x86: {
1374 nocrt: false,
1375 },
1376 },
1377 include_build_directory: false,
1378}
1379`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001380 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1381 "features": `select({
1382 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1383 "//conditions:default": [],
1384 })`,
1385 "srcs": `["impl.cpp"]`,
1386 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001387 })
1388}
1389
1390func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001391 runCcLibraryTestCase(t, Bp2buildTestCase{
1392 ModuleTypeUnderTest: "cc_library",
1393 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1394 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001395 "impl.cpp": "",
1396 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001397 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001398cc_library {
1399 name: "foo-lib",
1400 srcs: ["impl.cpp"],
1401 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001402 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001403}
1404`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001405 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001406 "features": `["-use_libcrt"]`,
1407 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001408 }),
1409 })
1410}
1411
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001412func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001413 STATIC_ONLY_ATTRS := map[string]bool{}
1414 SHARED_ONLY_ATTRS := map[string]bool{
1415 "link_crt": true,
1416 "additional_linker_inputs": true,
1417 "linkopts": true,
1418 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001419 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001420 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001421 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001422 }
Wei Li81852ca2022-07-27 00:22:06 -07001423
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001424 sharedAttrs := AttrNameToString{}
1425 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001426 for key, val := range attrs {
1427 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1428 sharedAttrs[key] = val
1429 }
1430 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1431 staticAttrs[key] = val
1432 }
1433 }
Alixe06d75b2022-08-31 18:28:19 +00001434 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1435 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001436
1437 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001438}
1439
Jingwen Chen6ada5892021-09-17 11:38:09 +00001440func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001441 runCcLibraryTestCase(t, Bp2buildTestCase{
1442 ModuleTypeUnderTest: "cc_library",
1443 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1444 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001445 "impl.cpp": "",
1446 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001447 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001448cc_library {
1449 name: "foo-lib",
1450 srcs: ["impl.cpp"],
1451 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001452 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001453}
1454`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001455 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001456 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001457 }),
1458 })
Liz Kammerd366c902021-06-03 13:43:01 -04001459}
1460
Jingwen Chen6ada5892021-09-17 11:38:09 +00001461func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001462 runCcLibraryTestCase(t, Bp2buildTestCase{
1463 ModuleTypeUnderTest: "cc_library",
1464 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1465 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001466 "impl.cpp": "",
1467 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001468 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001469cc_library {
1470 name: "foo-lib",
1471 srcs: ["impl.cpp"],
1472 arch: {
1473 arm: {
1474 no_libcrt: true,
1475 },
1476 x86: {
1477 no_libcrt: true,
1478 },
1479 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001480 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001481}
1482`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001483 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001484 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001485 "features": `select({
1486 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1487 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1488 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001489 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001490 }),
1491 })
Liz Kammerd366c902021-06-03 13:43:01 -04001492}
1493
Chris Parsons58852a02021-12-09 18:10:18 -05001494func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001495 runCcLibraryTestCase(t, Bp2buildTestCase{
1496 ModuleTypeUnderTest: "cc_library",
1497 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1498 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001499 "impl.cpp": "",
1500 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001501 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001502cc_library {
1503 name: "foo-lib",
1504 srcs: ["impl.cpp"],
1505 arch: {
1506 arm: {
1507 no_libcrt: true,
1508 },
1509 x86: {
1510 no_libcrt: true,
1511 },
1512 },
1513 target: {
1514 darwin: {
1515 no_libcrt: true,
1516 }
1517 },
1518 include_build_directory: false,
1519}
1520`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001521 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001522 "features": `select({
1523 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1524 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1525 "//conditions:default": [],
1526 }) + select({
1527 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1528 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001529 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001530 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001531 }),
1532 })
1533}
1534
1535func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001536 runCcLibraryTestCase(t, Bp2buildTestCase{
1537 ModuleTypeUnderTest: "cc_library",
1538 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1539 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001540 "impl.cpp": "",
1541 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001542 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001543cc_library {
1544 name: "foo-lib",
1545 srcs: ["impl.cpp"],
1546 arch: {
1547 arm: {
1548 no_libcrt: true,
1549 },
1550 // This is expected to override the value for darwin_x86_64.
1551 x86_64: {
1552 no_libcrt: true,
1553 },
1554 },
1555 target: {
1556 darwin: {
1557 no_libcrt: false,
1558 }
1559 },
1560 include_build_directory: false,
1561}
1562`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001563 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001564 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001565 "features": `select({
1566 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1567 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1568 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001569 })`,
1570 }),
1571 })
1572}
1573
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001574func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001575 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001576 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001577 "strip": `{
1578 "all": True,
1579 }`,
1580 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001581 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001582 "strip": `{
1583 "keep_symbols": True,
1584 }`,
1585 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001586 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001587 "strip": `{
1588 "keep_symbols_and_debug_frame": True,
1589 }`,
1590 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001591 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001592 "strip": `{
1593 "keep_symbols_list": ["symbol"],
1594 }`,
1595 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001596 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001597 "strip": `{
1598 "none": True,
1599 }`,
1600 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001601 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001602
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001603 runCcLibraryTestCase(t, Bp2buildTestCase{
1604 Description: "cc_library strip args",
1605 ModuleTypeUnderTest: "cc_library",
1606 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1607 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001608cc_library {
1609 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001610 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001611}
1612cc_library {
1613 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001614 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001615 keep_symbols: true,
1616 },
1617 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001618}
1619cc_library {
1620 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001621 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001622 keep_symbols_and_debug_frame: true,
1623 },
1624 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001625}
1626cc_library {
1627 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001628 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001629 none: true,
1630 },
1631 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001632}
1633cc_library {
1634 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001635 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001636 keep_symbols_list: ["symbol"],
1637 },
1638 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001639}
1640cc_library {
1641 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001642 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001643 all: true,
1644 },
1645 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001646}
1647`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001648 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001649 })
1650}
1651
1652func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001653 runCcLibraryTestCase(t, Bp2buildTestCase{
1654 Description: "cc_library strip args",
1655 ModuleTypeUnderTest: "cc_library",
1656 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1657 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001658cc_library {
1659 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001660 target: {
1661 darwin: {
1662 strip: {
1663 keep_symbols_list: ["foo", "bar"]
1664 }
1665 },
1666 },
1667 arch: {
1668 arm: {
1669 strip: {
1670 keep_symbols_and_debug_frame: true,
1671 },
1672 },
1673 arm64: {
1674 strip: {
1675 keep_symbols: true,
1676 },
1677 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001678 },
1679 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001680}
1681`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001682 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001683 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001684 "keep_symbols": select({
1685 "//build/bazel/platforms/arch:arm64": True,
1686 "//conditions:default": None,
1687 }),
1688 "keep_symbols_and_debug_frame": select({
1689 "//build/bazel/platforms/arch:arm": True,
1690 "//conditions:default": None,
1691 }),
1692 "keep_symbols_list": select({
1693 "//build/bazel/platforms/os:darwin": [
1694 "foo",
1695 "bar",
1696 ],
1697 "//conditions:default": [],
1698 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001699 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001700 }),
1701 },
1702 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001703}
Chris Parsons51f8c392021-08-03 21:01:05 -04001704
1705func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001706 runCcLibraryTestCase(t, Bp2buildTestCase{
1707 Description: "cc_library system_shared_libs empty at root",
1708 ModuleTypeUnderTest: "cc_library",
1709 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1710 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001711cc_library {
1712 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001713 system_shared_libs: [],
1714 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001715}
1716`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001717 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001718 "system_dynamic_deps": `[]`,
1719 }),
1720 },
1721 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001722}
1723
1724func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001725 runCcLibraryTestCase(t, Bp2buildTestCase{
1726 Description: "cc_library system_shared_libs empty for static variant",
1727 ModuleTypeUnderTest: "cc_library",
1728 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1729 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001730cc_library {
1731 name: "static_empty",
1732 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001733 system_shared_libs: [],
1734 },
1735 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001736}
1737`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001738 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001739 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001740 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001741 }),
Alixe06d75b2022-08-31 18:28:19 +00001742 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001743 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001744 })
1745}
1746
1747func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001748 runCcLibraryTestCase(t, Bp2buildTestCase{
1749 Description: "cc_library system_shared_libs empty for shared variant",
1750 ModuleTypeUnderTest: "cc_library",
1751 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1752 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001753cc_library {
1754 name: "shared_empty",
1755 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001756 system_shared_libs: [],
1757 },
1758 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001759}
1760`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001761 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001762 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1763 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001764 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001765 }),
1766 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001767 })
1768}
1769
1770func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 runCcLibraryTestCase(t, Bp2buildTestCase{
1772 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1773 ModuleTypeUnderTest: "cc_library",
1774 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1775 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001776cc_library {
1777 name: "shared_empty",
1778 target: {
1779 bionic: {
1780 shared: {
1781 system_shared_libs: [],
1782 }
1783 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001784 },
1785 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001786}
1787`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001788 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001789 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1790 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001791 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001792 }),
1793 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001794 })
1795}
1796
1797func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1798 // Note that this behavior is technically incorrect (it's a simplification).
1799 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1800 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1801 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001802 runCcLibraryTestCase(t, Bp2buildTestCase{
1803 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1804 ModuleTypeUnderTest: "cc_library",
1805 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1806 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001807cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001808 name: "libc_musl",
1809 bazel_module: { bp2build_available: false },
1810}
1811
1812cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001813 name: "target_linux_bionic_empty",
1814 target: {
1815 linux_bionic: {
1816 system_shared_libs: [],
1817 },
1818 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001819 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001820}
1821`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001822 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001823 "system_dynamic_deps": `select({
1824 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1825 "//conditions:default": [],
1826 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001827 }),
1828 },
1829 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001830}
1831
1832func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001833 runCcLibraryTestCase(t, Bp2buildTestCase{
1834 Description: "cc_library system_shared_libs empty for bionic variant",
1835 ModuleTypeUnderTest: "cc_library",
1836 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1837 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001838cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001839 name: "libc_musl",
1840 bazel_module: { bp2build_available: false },
1841}
1842
1843cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001844 name: "target_bionic_empty",
1845 target: {
1846 bionic: {
1847 system_shared_libs: [],
1848 },
1849 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001850 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001851}
1852`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001853 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001854 "system_dynamic_deps": `select({
1855 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1856 "//conditions:default": [],
1857 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001858 }),
1859 },
1860 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001861}
1862
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001863func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1864 runCcLibraryTestCase(t, Bp2buildTestCase{
1865 Description: "cc_library system_shared_lib empty for musl variant",
1866 ModuleTypeUnderTest: "cc_library",
1867 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1868 Blueprint: soongCcLibraryPreamble + `
1869cc_library {
1870 name: "libc_musl",
1871 bazel_module: { bp2build_available: false },
1872}
1873
1874cc_library {
1875 name: "target_musl_empty",
1876 target: {
1877 musl: {
1878 system_shared_libs: [],
1879 },
1880 },
1881 include_build_directory: false,
1882}
1883`,
1884 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1885 "system_dynamic_deps": `[]`,
1886 }),
1887 })
1888}
1889
1890func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1891 runCcLibraryTestCase(t, Bp2buildTestCase{
1892 Description: "cc_library system_shared_lib empty for linux_musl variant",
1893 ModuleTypeUnderTest: "cc_library",
1894 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1895 Blueprint: soongCcLibraryPreamble + `
1896cc_library {
1897 name: "libc_musl",
1898 bazel_module: { bp2build_available: false },
1899}
1900
1901cc_library {
1902 name: "target_linux_musl_empty",
1903 target: {
1904 linux_musl: {
1905 system_shared_libs: [],
1906 },
1907 },
1908 include_build_directory: false,
1909}
1910`,
1911 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1912 "system_dynamic_deps": `[]`,
1913 }),
1914 })
1915}
Chris Parsons51f8c392021-08-03 21:01:05 -04001916func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001917 runCcLibraryTestCase(t, Bp2buildTestCase{
1918 Description: "cc_library system_shared_libs set for shared and root",
1919 ModuleTypeUnderTest: "cc_library",
1920 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1921 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001922cc_library {
1923 name: "libc",
1924 bazel_module: { bp2build_available: false },
1925}
1926cc_library {
1927 name: "libm",
1928 bazel_module: { bp2build_available: false },
1929}
Chris Parsons51f8c392021-08-03 21:01:05 -04001930
1931cc_library {
1932 name: "foo",
1933 system_shared_libs: ["libc"],
1934 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001935 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001936 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001937 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001938}
1939`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001940 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001941 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001942 "system_dynamic_deps": `[":libc"]`,
1943 }),
Alixe06d75b2022-08-31 18:28:19 +00001944 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001945 "system_dynamic_deps": `[
1946 ":libc",
1947 ":libm",
1948 ]`,
1949 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001950 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001951 })
1952}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001953
1954func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001955 runCcLibraryTestCase(t, Bp2buildTestCase{
1956 Description: "cc_library - selects for all os targets",
1957 ModuleTypeUnderTest: "cc_library",
1958 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1959 Filesystem: map[string]string{},
1960 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001961cc_library {
1962 name: "foo-lib",
1963 srcs: ["base.cpp"],
1964 target: {
1965 android: {
1966 srcs: ["android.cpp"],
1967 },
1968 linux: {
1969 srcs: ["linux.cpp"],
1970 },
1971 linux_glibc: {
1972 srcs: ["linux_glibc.cpp"],
1973 },
1974 darwin: {
1975 srcs: ["darwin.cpp"],
1976 },
1977 bionic: {
1978 srcs: ["bionic.cpp"],
1979 },
1980 linux_musl: {
1981 srcs: ["linux_musl.cpp"],
1982 },
1983 windows: {
1984 srcs: ["windows.cpp"],
1985 },
1986 },
1987 include_build_directory: false,
1988}
1989`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001990 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001991 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001992 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001993 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04001994 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04001995 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001996 ],
1997 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001998 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001999 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002000 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002001 ],
Colin Cross133782e2022-12-20 15:29:31 -08002002 "//build/bazel/platforms/os:linux_glibc": [
2003 "linux.cpp",
2004 "linux_glibc.cpp",
2005 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002006 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002007 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002008 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002009 ],
2010 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2011 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002012 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002013 }),
2014 },
2015 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002016}
2017
Yu Liu75be7b92022-02-01 09:54:09 -08002018func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002019 runCcLibraryTestCase(t, Bp2buildTestCase{
2020 Description: "cc_library - libcrypto hash injection",
2021 ModuleTypeUnderTest: "cc_library",
2022 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2023 Filesystem: map[string]string{},
2024 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002025cc_library {
2026 name: "libcrypto",
2027 target: {
2028 android: {
2029 inject_bssl_hash: true,
2030 },
2031 },
2032 include_build_directory: false,
2033}
2034`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002035 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002036 "inject_bssl_hash": `select({
2037 "//build/bazel/platforms/os:android": True,
2038 "//conditions:default": None,
2039 })`,
2040 }),
2041 },
2042 )
2043}
2044
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002045func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002046 type testCase struct {
2047 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002048 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002049 gnu_extensions string
2050 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002051 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002052 }
2053
2054 testCases := []testCase{
2055 // Existing usages of cpp_std in AOSP are:
2056 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2057 //
2058 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2059 // is set in the toolchain.)
2060 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002061 {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002062 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2063 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002064 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2065 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2066 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002067 // Explicitly setting a c++ std does not use replace gnu++ std even if
2068 // gnu_extensions is true.
2069 // "c++11",
2070 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002071 {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002072 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2073 // "c++17",
2074 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002075 {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002076 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2077 // "c++2a",
2078 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002079 {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002080 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2081 // "c++98",
2082 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002083 {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002084 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2085 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2086 // "gnu++11",
2087 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002088 {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002089 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2090 // "gnu++17",
2091 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002092 {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002093 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002094
2095 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002096 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2097 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2098 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002099 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2100 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2101 {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 +00002102 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002103 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002104 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2105 t.Run(name, func(t *testing.T) {
2106 name_prefix := fmt.Sprintf("a_%v", i)
2107 cppStdProp := ""
2108 if tc.cpp_std != "" {
2109 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2110 }
2111 cStdProp := ""
2112 if tc.c_std != "" {
2113 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2114 }
2115 gnuExtensionsProp := ""
2116 if tc.gnu_extensions != "" {
2117 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2118 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002119 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002120 if tc.bazel_cpp_std != "" {
2121 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2122 }
2123 if tc.bazel_c_std != "" {
2124 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2125 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002126
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002127 runCcLibraryTestCase(t, Bp2buildTestCase{
2128 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002129 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002130 ModuleTypeUnderTest: "cc_library",
2131 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2132 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002133cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002134 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002135%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002136%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002137%s // gnu_extensions: *bool
2138 include_build_directory: false,
2139}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002140`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002141 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002142 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002143
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002144 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2145 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002146 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002147 ModuleTypeUnderTest: "cc_library_static",
2148 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2149 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002150cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002151 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002152%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002153%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002154%s // gnu_extensions: *bool
2155 include_build_directory: false,
2156}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002157`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002158 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002159 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002160 },
2161 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002162
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002163 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2164 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002165 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002166 ModuleTypeUnderTest: "cc_library_shared",
2167 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2168 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002169cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002170 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002171%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002172%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002173%s // gnu_extensions: *bool
2174 include_build_directory: false,
2175}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002176`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002177 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002178 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002179 },
2180 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002181 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002182 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002183}
Liz Kammer12615db2021-09-28 09:19:17 -04002184
2185func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002186 runCcLibraryTestCase(t, Bp2buildTestCase{
2187 ModuleTypeUnderTest: "cc_library",
2188 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2189 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002190 name: "foo",
2191 srcs: ["foo.proto"],
2192 include_build_directory: false,
2193}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002194 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002195 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002196 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002197 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002198 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002199 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002200 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002201 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002202 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002203 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2204 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002205 }),
2206 },
2207 })
2208}
2209
2210func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002211 runCcLibraryTestCase(t, Bp2buildTestCase{
2212 ModuleTypeUnderTest: "cc_library",
2213 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2214 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002215 name: "foo",
2216 srcs: ["foo.proto"],
2217 proto: { canonical_path_from_root: false},
2218 include_build_directory: false,
2219}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002220 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002221 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002222 "srcs": `["foo.proto"]`,
2223 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002224 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002225 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002226 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002227 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002228 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002229 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002230 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2231 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002232 }),
2233 },
2234 })
2235}
2236
2237func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002238 runCcLibraryTestCase(t, Bp2buildTestCase{
2239 ModuleTypeUnderTest: "cc_library",
2240 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2241 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002242 name: "foo",
2243 srcs: ["foo.proto"],
2244 proto: { canonical_path_from_root: true},
2245 include_build_directory: false,
2246}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002247 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002248 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002249 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002250 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002251 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002252 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002253 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002254 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002255 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002256 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2257 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002258 }),
2259 },
2260 })
2261}
2262
2263func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002264 runCcLibraryTestCase(t, Bp2buildTestCase{
2265 ModuleTypeUnderTest: "cc_library",
2266 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2267 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002268 name: "foo",
2269 srcs: ["foo.proto"],
2270 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002271 type: "full",
2272 },
2273 include_build_directory: false,
2274}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002275 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002276 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002277 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002278 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002279 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002280 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002281 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002282 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002283 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002284 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2285 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002286 }),
2287 },
2288 })
2289}
2290
2291func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002292 runCcLibraryTestCase(t, Bp2buildTestCase{
2293 ModuleTypeUnderTest: "cc_library",
2294 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2295 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002296 name: "foo",
2297 srcs: ["foo.proto"],
2298 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002299 type: "lite",
2300 },
2301 include_build_directory: false,
2302}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002303 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002304 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002305 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002306 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002307 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002308 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002309 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002310 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002311 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002312 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2313 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002314 }),
2315 },
2316 })
2317}
2318
2319func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002320 runCcLibraryTestCase(t, Bp2buildTestCase{
2321 ModuleTypeUnderTest: "cc_library",
2322 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2323 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002324 name: "foo",
2325 srcs: ["foo.proto"],
2326 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002327 export_proto_headers: true,
2328 },
2329 include_build_directory: false,
2330}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002331 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002332 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002333 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002334 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002335 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002336 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002337 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002338 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002339 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002340 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2341 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002342 }),
2343 },
2344 })
2345}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002346
Yu Liu2d136142022-08-18 14:46:13 -07002347func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2348 runCcLibraryTestCase(t, Bp2buildTestCase{
2349 ModuleTypeUnderTest: "cc_library",
2350 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2351 Blueprint: soongCcProtoPreamble + `cc_library {
2352 name: "foo",
2353 srcs: ["foo.proto"],
2354 proto: {
2355 include_dirs: ["external/protobuf/src"],
2356 },
2357 include_build_directory: false,
2358}`,
2359 ExpectedBazelTargets: []string{
2360 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2361 "srcs": `["foo.proto"]`,
2362 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2363 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2364 "deps": `[":foo_proto"]`,
2365 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2366 "deps": `[":libprotobuf-cpp-lite"]`,
2367 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2368 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002369 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2370 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002371 }),
2372 },
2373 })
2374}
2375
2376func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2377 runCcLibraryTestCase(t, Bp2buildTestCase{
2378 ModuleTypeUnderTest: "cc_library",
2379 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2380 Blueprint: soongCcProtoPreamble + `cc_library {
2381 name: "foo",
2382 srcs: ["foo.proto"],
2383 proto: {
2384 include_dirs: ["external/protobuf/abc"],
2385 },
2386 include_build_directory: false,
2387}`,
2388 ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"),
2389 })
2390}
2391
Yu Liu2aa806b2022-09-01 11:54:47 -07002392func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2393 runCcLibraryTestCase(t, Bp2buildTestCase{
2394 ModuleTypeUnderTest: "cc_library",
2395 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2396 Blueprint: soongCcProtoPreamble + `
2397filegroup {
2398 name: "a_fg_proto",
2399 srcs: ["a_fg.proto"],
2400}
2401
2402cc_library {
2403 name: "a",
2404 srcs: [
2405 ":a_fg_proto",
2406 "a.proto",
2407 ],
2408 proto: {
2409 export_proto_headers: true,
2410 },
2411 include_build_directory: false,
2412}`,
2413 ExpectedBazelTargets: []string{
2414 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002415 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002416 "srcs": `["a.proto"]`,
2417 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2418 "deps": `[
2419 ":a_fg_proto_bp2build_converted",
2420 ":a_proto",
2421 ]`,
2422 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2423 "deps": `[":libprotobuf-cpp-lite"]`,
2424 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2425 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2426 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2427 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2428 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2429 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002430 "tags": `[
2431 "apex_available=//apex_available:anyapex",
2432 "manual",
2433 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002434 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2435 "srcs": `["a_fg.proto"]`,
2436 }),
2437 },
2438 })
2439}
2440
2441func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2442 runCcLibraryTestCase(t, Bp2buildTestCase{
2443 ModuleTypeUnderTest: "cc_library",
2444 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2445 Blueprint: soongCcProtoPreamble + `
2446filegroup {
2447 name: "a_fg_proto",
2448 srcs: ["a_fg.proto"],
2449}
2450
2451cc_library {
2452 name: "a",
2453 srcs: [
2454 ":a_fg_proto",
2455 ],
2456 proto: {
2457 export_proto_headers: true,
2458 },
2459 include_build_directory: false,
2460}`,
2461 ExpectedBazelTargets: []string{
2462 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2463 "deps": `[":a_fg_proto_bp2build_converted"]`,
2464 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2465 "deps": `[":libprotobuf-cpp-lite"]`,
2466 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2467 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2468 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2469 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2470 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
2471 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002472 "tags": `[
2473 "apex_available=//apex_available:anyapex",
2474 "manual",
2475 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002476 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2477 "srcs": `["a_fg.proto"]`,
2478 }),
2479 },
2480 })
2481}
2482
2483func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2484 runCcLibraryTestCase(t, Bp2buildTestCase{
2485 ModuleTypeUnderTest: "cc_library",
2486 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2487 Filesystem: map[string]string{
2488 "path/to/A/Android.bp": `
2489filegroup {
2490 name: "a_fg_proto",
2491 srcs: ["a_fg.proto"],
2492}`,
2493 },
2494 Blueprint: soongCcProtoPreamble + `
2495cc_library {
2496 name: "a",
2497 srcs: [
2498 ":a_fg_proto",
2499 "a.proto",
2500 ],
2501 proto: {
2502 export_proto_headers: true,
2503 },
2504 include_build_directory: false,
2505}`,
2506 ExpectedBazelTargets: []string{
2507 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002508 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002509 "srcs": `["a.proto"]`,
2510 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2511 "deps": `[
2512 "//path/to/A:a_fg_proto_bp2build_converted",
2513 ":a_proto",
2514 ]`,
2515 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2516 "deps": `[":libprotobuf-cpp-lite"]`,
2517 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2518 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2519 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2520 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2521 }),
2522 },
2523 })
2524}
2525
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002526func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002527 runCcLibraryTestCase(t, Bp2buildTestCase{
2528 ModuleTypeUnderTest: "cc_library",
2529 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2530 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002531 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2532 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2533 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2534 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2535cc_library {
2536 name: "a",
2537 srcs: [":a_fg_proto"],
2538 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002539 export_proto_headers: true,
2540 },
2541 include_build_directory: false,
2542}
2543
2544cc_library {
2545 name: "b",
2546 srcs: [":b_protos"],
2547 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002548 export_proto_headers: true,
2549 },
2550 include_build_directory: false,
2551}
2552
2553cc_library {
2554 name: "c",
2555 srcs: [":c-proto-srcs"],
2556 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002557 export_proto_headers: true,
2558 },
2559 include_build_directory: false,
2560}
2561
2562cc_library {
2563 name: "d",
2564 srcs: [":proto-srcs-d"],
2565 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002566 export_proto_headers: true,
2567 },
2568 include_build_directory: false,
2569}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002570 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002571 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002572 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002573 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002574 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002575 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002576 "deps": `[":libprotobuf-cpp-lite"]`,
2577 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2578 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2579 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2580 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002581 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002582 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2583 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2584 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2585 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2586 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002587 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002588 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002589 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002590 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002591 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002592 "deps": `[":libprotobuf-cpp-lite"]`,
2593 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2594 "srcs": `[":b_protos_cpp_srcs"]`,
2595 "srcs_as": `[":b_protos_as_srcs"]`,
2596 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002597 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002598 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2599 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2600 "srcs": `[":b_protos_cpp_srcs"]`,
2601 "srcs_as": `[":b_protos_as_srcs"]`,
2602 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002603 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002604 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002605 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002606 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002607 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002608 "deps": `[":libprotobuf-cpp-lite"]`,
2609 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2610 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2611 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2612 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002613 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002614 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2615 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2616 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2617 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2618 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002619 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002620 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002621 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002622 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002623 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002624 "deps": `[":libprotobuf-cpp-lite"]`,
2625 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2626 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2627 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2628 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002629 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002630 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2631 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2632 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2633 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2634 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2635 }),
2636 },
2637 })
2638}
Chris Parsons58852a02021-12-09 18:10:18 -05002639
2640func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002641 runCcLibraryTestCase(t, Bp2buildTestCase{
2642 ModuleTypeUnderTest: "cc_library",
2643 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2644 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002645 name: "foo",
2646 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002647 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002648 target: {
2649 darwin: {
2650 enabled: false,
2651 },
2652 windows: {
2653 enabled: false,
2654 },
2655 linux_glibc_x86: {
2656 enabled: false,
2657 },
2658 },
2659 include_build_directory: false,
2660}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002661 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002662 "srcs": `["foo.cpp"]`,
2663 "target_compatible_with": `select({
2664 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2665 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2666 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2667 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2668 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2669 "//conditions:default": [],
2670 })`,
2671 }),
2672 })
2673}
2674
2675func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002676 runCcLibraryTestCase(t, Bp2buildTestCase{
2677 ModuleTypeUnderTest: "cc_library",
2678 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2679 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002680 name: "foo",
2681 srcs: ["foo.cpp"],
2682 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002683 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002684 target: {
2685 darwin: {
2686 enabled: true,
2687 },
2688 windows: {
2689 enabled: false,
2690 },
2691 linux_glibc_x86: {
2692 enabled: false,
2693 },
2694 },
2695 include_build_directory: false,
2696}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002697 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002698 "srcs": `["foo.cpp"]`,
2699 "target_compatible_with": `select({
2700 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2701 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2702 "//conditions:default": ["@platforms//:incompatible"],
2703 })`,
2704 }),
2705 })
2706}
2707
2708func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002709 runCcLibraryTestCase(t, Bp2buildTestCase{
2710 ModuleTypeUnderTest: "cc_library",
2711 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2712 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002713 name: "foo",
2714 srcs: ["foo.cpp"],
2715 enabled: false,
2716 shared: {
2717 enabled: true,
2718 },
2719 target: {
2720 android: {
2721 shared: {
2722 enabled: false,
2723 },
2724 }
2725 },
2726 include_build_directory: false,
2727}`,
Alixe06d75b2022-08-31 18:28:19 +00002728 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002729 "srcs": `["foo.cpp"]`,
2730 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002731 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002732 "srcs": `["foo.cpp"]`,
2733 "target_compatible_with": `select({
2734 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2735 "//conditions:default": [],
2736 })`,
2737 }),
2738 },
2739 })
2740}
2741
2742func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002743 runCcLibraryTestCase(t, Bp2buildTestCase{
2744 ModuleTypeUnderTest: "cc_library",
2745 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2746 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002747 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002748 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002749 srcs: ["foo.cpp"],
2750 shared: {
2751 enabled: false
2752 },
2753 target: {
2754 darwin: {
2755 enabled: true,
2756 },
2757 windows: {
2758 enabled: false,
2759 },
2760 linux_glibc_x86: {
2761 shared: {
2762 enabled: true,
2763 },
2764 },
2765 },
2766 include_build_directory: false,
2767}`,
Alixe06d75b2022-08-31 18:28:19 +00002768 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002769 "srcs": `["foo.cpp"]`,
2770 "target_compatible_with": `select({
2771 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2772 "//conditions:default": [],
2773 })`,
Alixe06d75b2022-08-31 18:28:19 +00002774 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002775 "srcs": `["foo.cpp"]`,
2776 "target_compatible_with": `select({
2777 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2778 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2779 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2780 "//conditions:default": ["@platforms//:incompatible"],
2781 })`,
2782 }),
2783 }})
2784}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002785
2786func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002787 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002788 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002789 })
2790 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Sam Delmerico5f906492023-03-15 18:06:18 -04002791 "soname": `"a.so"`,
2792 "source_library_label": `"//foo/bar:a"`,
2793 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002794 "stubs_versions": `[
2795 "28",
2796 "29",
2797 "current",
2798 ]`,
2799 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002800 runCcLibraryTestCase(t, Bp2buildTestCase{
2801 Description: "cc_library stubs",
2802 ModuleTypeUnderTest: "cc_library",
2803 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2804 Dir: "foo/bar",
2805 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002806 "foo/bar/Android.bp": `
2807cc_library {
2808 name: "a",
2809 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2810 bazel_module: { bp2build_available: true },
2811 include_build_directory: false,
2812}
2813`,
2814 },
Wei Li81852ca2022-07-27 00:22:06 -07002815 Blueprint: soongCcLibraryPreamble,
2816 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002817 },
2818 )
2819}
Liz Kammerf38a8372022-02-04 15:39:00 -05002820
Spandan Das4238c652022-09-09 01:38:47 +00002821func TestCcApiContributionsWithHdrs(t *testing.T) {
2822 bp := `
2823 cc_library {
2824 name: "libfoo",
2825 stubs: { symbol_file: "libfoo.map.txt", versions: ["28", "29", "current"] },
2826 llndk: { symbol_file: "libfoo.map.txt", override_export_include_dirs: ["dir2"]},
2827 export_include_dirs: ["dir1"],
2828 }
2829 `
2830 expectedBazelTargets := []string{
2831 MakeBazelTarget(
2832 "cc_api_library_headers",
Spandan Das627fc3e2023-01-26 23:02:00 +00002833 "libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002834 AttrNameToString{
2835 "export_includes": `["dir1"]`,
2836 }),
2837 MakeBazelTarget(
2838 "cc_api_library_headers",
2839 "libfoo.vendorapi.headers",
2840 AttrNameToString{
2841 "export_includes": `["dir2"]`,
2842 }),
2843 MakeBazelTarget(
2844 "cc_api_contribution",
2845 "libfoo.contribution",
2846 AttrNameToString{
2847 "api": `"libfoo.map.txt"`,
2848 "library_name": `"libfoo"`,
2849 "api_surfaces": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002850 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002851 "vendorapi",
2852 ]`,
2853 "hdrs": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002854 ":libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002855 ":libfoo.vendorapi.headers",
2856 ]`,
2857 }),
2858 }
2859 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2860 Blueprint: bp,
Spandan Das627fc3e2023-01-26 23:02:00 +00002861 Description: "cc API contributions to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002862 ExpectedBazelTargets: expectedBazelTargets,
2863 })
2864}
2865
2866func TestCcApiSurfaceCombinations(t *testing.T) {
2867 testCases := []struct {
2868 bp string
2869 expectedApi string
2870 expectedApiSurfaces string
2871 description string
2872 }{
2873 {
2874 bp: `
2875 cc_library {
2876 name: "a",
2877 stubs: {symbol_file: "a.map.txt"},
2878 }`,
2879 expectedApi: `"a.map.txt"`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002880 expectedApiSurfaces: `["module-libapi"]`,
2881 description: "Library that contributes to module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002882 },
2883 {
2884 bp: `
2885 cc_library {
2886 name: "a",
2887 llndk: {symbol_file: "a.map.txt"},
2888 }`,
2889 expectedApi: `"a.map.txt"`,
2890 expectedApiSurfaces: `["vendorapi"]`,
2891 description: "Library that contributes to vendorapi",
2892 },
2893 {
2894 bp: `
2895 cc_library {
2896 name: "a",
2897 llndk: {symbol_file: "a.map.txt"},
2898 stubs: {symbol_file: "a.map.txt"},
2899 }`,
2900 expectedApi: `"a.map.txt"`,
2901 expectedApiSurfaces: `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002902 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002903 "vendorapi",
2904 ]`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002905 description: "Library that contributes to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002906 },
2907 }
2908 for _, testCase := range testCases {
2909 expectedBazelTargets := []string{
2910 MakeBazelTarget(
2911 "cc_api_contribution",
2912 "a.contribution",
2913 AttrNameToString{
2914 "library_name": `"a"`,
2915 "hdrs": `[]`,
2916 "api": testCase.expectedApi,
2917 "api_surfaces": testCase.expectedApiSurfaces,
2918 },
2919 ),
2920 }
2921 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2922 Blueprint: testCase.bp,
2923 Description: testCase.description,
2924 ExpectedBazelTargets: expectedBazelTargets,
2925 })
2926 }
2927}
2928
2929// llndk struct property in Soong provides users with several options to configure the exported include dirs
2930// Test the generated bazel targets for the different configurations
2931func TestCcVendorApiHeaders(t *testing.T) {
2932 testCases := []struct {
2933 bp string
2934 expectedIncludes string
2935 expectedSystemIncludes string
2936 description string
2937 }{
2938 {
2939 bp: `
2940 cc_library {
2941 name: "a",
2942 export_include_dirs: ["include"],
2943 export_system_include_dirs: ["base_system_include"],
2944 llndk: {
2945 symbol_file: "a.map.txt",
2946 export_headers_as_system: true,
2947 },
2948 }
2949 `,
2950 expectedIncludes: "",
2951 expectedSystemIncludes: `[
2952 "base_system_include",
2953 "include",
2954 ]`,
2955 description: "Headers are exported as system to API surface",
2956 },
2957 {
2958 bp: `
2959 cc_library {
2960 name: "a",
2961 export_include_dirs: ["include"],
2962 export_system_include_dirs: ["base_system_include"],
2963 llndk: {
2964 symbol_file: "a.map.txt",
2965 override_export_include_dirs: ["llndk_include"],
2966 },
2967 }
2968 `,
2969 expectedIncludes: `["llndk_include"]`,
2970 expectedSystemIncludes: `["base_system_include"]`,
2971 description: "Non-system Headers are ovverriden before export to API surface",
2972 },
2973 {
2974 bp: `
2975 cc_library {
2976 name: "a",
2977 export_include_dirs: ["include"],
2978 export_system_include_dirs: ["base_system_include"],
2979 llndk: {
2980 symbol_file: "a.map.txt",
2981 override_export_include_dirs: ["llndk_include"],
2982 export_headers_as_system: true,
2983 },
2984 }
2985 `,
2986 expectedIncludes: "", // includes are set to nil
2987 expectedSystemIncludes: `[
2988 "base_system_include",
2989 "llndk_include",
2990 ]`,
2991 description: "System Headers are extended before export to API surface",
2992 },
2993 }
2994 for _, testCase := range testCases {
2995 attrs := AttrNameToString{}
2996 if testCase.expectedIncludes != "" {
2997 attrs["export_includes"] = testCase.expectedIncludes
2998 }
2999 if testCase.expectedSystemIncludes != "" {
3000 attrs["export_system_includes"] = testCase.expectedSystemIncludes
3001 }
3002
3003 expectedBazelTargets := []string{
3004 MakeBazelTarget("cc_api_library_headers", "a.vendorapi.headers", attrs),
3005 // Create a target for cc_api_contribution target
3006 MakeBazelTarget("cc_api_contribution", "a.contribution", AttrNameToString{
3007 "api": `"a.map.txt"`,
3008 "api_surfaces": `["vendorapi"]`,
3009 "hdrs": `[":a.vendorapi.headers"]`,
3010 "library_name": `"a"`,
3011 }),
3012 }
3013 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
3014 Blueprint: testCase.bp,
3015 ExpectedBazelTargets: expectedBazelTargets,
3016 })
3017 }
3018}
3019
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003020func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
3021 runCcLibraryTestCase(t, Bp2buildTestCase{
3022 Description: "stub target generation of the same lib across configs should not result in duplicates",
3023 ModuleTypeUnderTest: "cc_library",
3024 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3025 Filesystem: map[string]string{
3026 "bar.map.txt": "",
3027 },
3028 Blueprint: `
3029cc_library {
3030 name: "barlib",
3031 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3032 bazel_module: { bp2build_available: false },
3033}
3034cc_library {
3035 name: "foolib",
3036 shared_libs: ["barlib"],
3037 target: {
3038 android: {
3039 shared_libs: ["barlib"],
3040 },
3041 },
3042 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003043 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003044}`,
3045 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3046 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00003047 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
3048 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003049 "//conditions:default": [":barlib"],
3050 })`,
3051 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003052 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003053 }),
3054 })
3055}
3056
Liz Kammerffc17e42022-11-23 09:42:05 -05003057func TestCcLibraryExcludesLibsHost(t *testing.T) {
3058 runCcLibraryTestCase(t, Bp2buildTestCase{
3059 ModuleTypeUnderTest: "cc_library",
3060 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3061 Filesystem: map[string]string{
3062 "bar.map.txt": "",
3063 },
3064 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
3065cc_library {
3066 name: "quxlib",
3067 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
3068 bazel_module: { bp2build_available: false },
3069}
3070cc_library {
3071 name: "barlib",
3072 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3073 bazel_module: { bp2build_available: false },
3074}
3075cc_library {
3076 name: "foolib",
3077 shared_libs: ["barlib", "quxlib"],
3078 target: {
3079 host: {
3080 shared_libs: ["bazlib"],
3081 exclude_shared_libs: ["barlib"],
3082 },
3083 },
3084 include_build_directory: false,
3085 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003086 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003087}`,
3088 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3089 "implementation_dynamic_deps": `select({
3090 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003091 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003092 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003093 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
3094 "//build/bazel/platforms/os:windows": [":bazlib"],
3095 "//conditions:default": [],
3096 }) + select({
3097 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003098 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003099 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003100 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
3101 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00003102 "//build/bazel/rules/apex:foo": [
3103 "@api_surfaces//module-libapi/current:barlib",
3104 "@api_surfaces//module-libapi/current:quxlib",
3105 ],
3106 "//build/bazel/rules/apex:system": [
Spandan Das2518c022023-03-17 03:02:32 +00003107 "@api_surfaces//module-libapi/current:barlib",
3108 "@api_surfaces//module-libapi/current:quxlib",
Liz Kammerffc17e42022-11-23 09:42:05 -05003109 ],
3110 "//conditions:default": [
3111 ":barlib",
3112 ":quxlib",
3113 ],
3114 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003115 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05003116 }),
3117 })
3118}
3119
Liz Kammerf38a8372022-02-04 15:39:00 -05003120func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003121 runCcLibraryTestCase(t, Bp2buildTestCase{
3122 ModuleTypeUnderTest: "cc_library",
3123 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3124 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05003125 name: "foo",
3126 ldflags: ["-Wl,--rpath,${ORIGIN}"],
3127 include_build_directory: false,
3128}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003129 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05003130 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
3131 }),
3132 })
3133}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003134
3135func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003136 runCcLibraryTestCase(t, Bp2buildTestCase{
3137 ModuleTypeUnderTest: "cc_library",
3138 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3139 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003140 "foo.c": "",
3141 "bar.cc": "",
3142 "foo1.l": "",
3143 "bar1.ll": "",
3144 "foo2.l": "",
3145 "bar2.ll": "",
3146 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003147 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003148 name: "foo_lib",
3149 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
3150 lex: { flags: ["--foo_flags"] },
3151 include_build_directory: false,
3152 bazel_module: { bp2build_available: true },
3153}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003154 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00003155 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003156 "srcs": `[
3157 "foo1.l",
3158 "foo2.l",
3159 ]`,
3160 "lexopts": `["--foo_flags"]`,
3161 }),
Alixe06d75b2022-08-31 18:28:19 +00003162 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003163 "srcs": `[
3164 "bar1.ll",
3165 "bar2.ll",
3166 ]`,
3167 "lexopts": `["--foo_flags"]`,
3168 }),
3169 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003170 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003171 "srcs": `[
3172 "bar.cc",
3173 ":foo_lib_genlex_ll",
3174 ]`,
3175 "srcs_c": `[
3176 "foo.c",
3177 ":foo_lib_genlex_l",
3178 ]`,
3179 })...),
3180 })
3181}
Cole Faust6b29f592022-08-09 09:50:56 -07003182
3183func TestCCLibraryRuntimeDeps(t *testing.T) {
3184 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3185 Blueprint: `cc_library_shared {
3186 name: "bar",
3187}
3188
3189cc_library {
3190 name: "foo",
3191 runtime_libs: ["foo"],
3192}`,
3193 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003194 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003195 "local_includes": `["."]`,
3196 }),
Alixe06d75b2022-08-31 18:28:19 +00003197 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003198 "runtime_deps": `[":foo"]`,
3199 "local_includes": `["."]`,
3200 }),
Alixe06d75b2022-08-31 18:28:19 +00003201 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003202 "runtime_deps": `[":foo"]`,
3203 "local_includes": `["."]`,
3204 }),
3205 },
3206 })
3207}
Cole Faust5fa4e962022-08-22 14:31:04 -07003208
3209func TestCcLibraryWithInstructionSet(t *testing.T) {
3210 runCcLibraryTestCase(t, Bp2buildTestCase{
3211 ModuleTypeUnderTest: "cc_library",
3212 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3213 Blueprint: `cc_library {
3214 name: "foo",
3215 arch: {
3216 arm: {
3217 instruction_set: "arm",
3218 }
3219 }
3220}
3221`,
3222 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3223 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003224 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003225 "//conditions:default": [],
3226 })`,
3227 "local_includes": `["."]`,
3228 }),
3229 })
3230}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003231
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003232func TestCcLibraryEmptySuffix(t *testing.T) {
3233 runCcLibraryTestCase(t, Bp2buildTestCase{
3234 Description: "cc_library with empty suffix",
3235 ModuleTypeUnderTest: "cc_library",
3236 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3237 Filesystem: map[string]string{
3238 "foo.c": "",
3239 },
3240 Blueprint: `cc_library {
3241 name: "foo",
3242 suffix: "",
3243 srcs: ["foo.c"],
3244 include_build_directory: false,
3245}`,
3246 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003247 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003248 "srcs_c": `["foo.c"]`,
3249 }),
Alixe06d75b2022-08-31 18:28:19 +00003250 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003251 "srcs_c": `["foo.c"]`,
3252 "suffix": `""`,
3253 }),
3254 },
3255 })
3256}
3257
3258func TestCcLibrarySuffix(t *testing.T) {
3259 runCcLibraryTestCase(t, Bp2buildTestCase{
3260 Description: "cc_library with suffix",
3261 ModuleTypeUnderTest: "cc_library",
3262 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3263 Filesystem: map[string]string{
3264 "foo.c": "",
3265 },
3266 Blueprint: `cc_library {
3267 name: "foo",
3268 suffix: "-suf",
3269 srcs: ["foo.c"],
3270 include_build_directory: false,
3271}`,
3272 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003273 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003274 "srcs_c": `["foo.c"]`,
3275 }),
Alixe06d75b2022-08-31 18:28:19 +00003276 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003277 "srcs_c": `["foo.c"]`,
3278 "suffix": `"-suf"`,
3279 }),
3280 },
3281 })
3282}
3283
3284func TestCcLibraryArchVariantSuffix(t *testing.T) {
3285 runCcLibraryTestCase(t, Bp2buildTestCase{
3286 Description: "cc_library with arch-variant suffix",
3287 ModuleTypeUnderTest: "cc_library",
3288 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3289 Filesystem: map[string]string{
3290 "foo.c": "",
3291 },
3292 Blueprint: `cc_library {
3293 name: "foo",
3294 arch: {
3295 arm64: { suffix: "-64" },
3296 arm: { suffix: "-32" },
3297 },
3298 srcs: ["foo.c"],
3299 include_build_directory: false,
3300}`,
3301 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003302 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003303 "srcs_c": `["foo.c"]`,
3304 }),
Alixe06d75b2022-08-31 18:28:19 +00003305 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003306 "srcs_c": `["foo.c"]`,
3307 "suffix": `select({
3308 "//build/bazel/platforms/arch:arm": "-32",
3309 "//build/bazel/platforms/arch:arm64": "-64",
3310 "//conditions:default": None,
3311 })`,
3312 }),
3313 },
3314 })
3315}
3316
Vinh Tran367d89d2023-04-28 11:21:25 -04003317func TestCcLibraryWithAidlLibrary(t *testing.T) {
3318 runCcLibraryTestCase(t, Bp2buildTestCase{
3319 Description: "cc_library with aidl_library",
3320 ModuleTypeUnderTest: "cc_library",
3321 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3322 Blueprint: `
3323aidl_library {
3324 name: "A_aidl",
3325 srcs: ["aidl/A.aidl"],
3326 hdrs: ["aidl/Header.aidl"],
3327 strip_import_prefix: "aidl",
3328}
3329cc_library {
3330 name: "foo",
3331 aidl: {
3332 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003333 },
3334 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003335}`,
3336 ExpectedBazelTargets: []string{
3337 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3338 "srcs": `["aidl/A.aidl"]`,
3339 "hdrs": `["aidl/Header.aidl"]`,
3340 "strip_import_prefix": `"aidl"`,
3341 "tags": `["apex_available=//apex_available:anyapex"]`,
3342 }),
3343 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003344 "deps": `[":A_aidl"]`,
3345 "local_includes": `["."]`,
3346 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003347 }),
3348 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3349 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3350 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003351 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003352 }),
3353 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3354 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3355 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003356 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003357 }),
3358 },
3359 })
3360}
3361
Vinh Tran9f6796a2022-08-16 13:10:31 -04003362func TestCcLibraryWithAidlSrcs(t *testing.T) {
3363 runCcLibraryTestCase(t, Bp2buildTestCase{
3364 Description: "cc_library with aidl srcs",
3365 ModuleTypeUnderTest: "cc_library",
3366 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3367 Blueprint: `
3368filegroup {
3369 name: "A_aidl",
3370 srcs: ["aidl/A.aidl"],
3371 path: "aidl",
3372}
3373cc_library {
3374 name: "foo",
3375 srcs: [
3376 ":A_aidl",
3377 "B.aidl",
3378 ],
3379}`,
3380 ExpectedBazelTargets: []string{
3381 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3382 "srcs": `["aidl/A.aidl"]`,
3383 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003384 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003385 }),
Alixe06d75b2022-08-31 18:28:19 +00003386 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003387 "srcs": `["B.aidl"]`,
3388 }),
Alixe06d75b2022-08-31 18:28:19 +00003389 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003390 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003391 "deps": `[
3392 ":A_aidl",
3393 ":foo_aidl_library",
3394 ]`,
3395 }),
Alixe06d75b2022-08-31 18:28:19 +00003396 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003397 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3398 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003399 }),
Alixe06d75b2022-08-31 18:28:19 +00003400 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003401 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3402 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003403 }),
3404 },
3405 })
3406}
3407
3408func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3409 runCcLibraryTestCase(t, Bp2buildTestCase{
3410 Description: "cc_library with non aidl filegroup",
3411 ModuleTypeUnderTest: "cc_library",
3412 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3413 Filesystem: map[string]string{
3414 "path/to/A/Android.bp": `
3415filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003416 name: "A_aidl",
3417 srcs: ["aidl/A.aidl"],
3418 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003419}`,
3420 },
3421 Blueprint: `
3422cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003423 name: "foo",
3424 srcs: [
3425 ":A_aidl",
3426 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003427}`,
3428 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003429 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003430 "local_includes": `["."]`,
3431 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003432 }),
Alixe06d75b2022-08-31 18:28:19 +00003433 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003434 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3435 "local_includes": `["."]`,
3436 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003437 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003438 "local_includes": `["."]`,
3439 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003440 }),
3441 },
3442 })
3443}
3444
3445func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003446 t.Parallel()
3447
3448 expectedBazelTargets := []string{
3449 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003450 "local_includes": `["."]`,
3451 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003452 }),
3453 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3454 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3455 "local_includes": `["."]`,
3456 }),
3457 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3458 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3459 "local_includes": `["."]`,
3460 }),
3461 }
3462 testCases := []struct {
3463 description string
3464 bp string
3465 expectedBazelTargets []string
3466 }{
3467 {
3468 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3469 bp: `
3470 cc_library {
3471 name: "foo",
3472 srcs: [
3473 "Foo.aidl",
3474 ],
3475 aidl: {
3476 export_aidl_headers: true,
3477 }
3478 }`,
3479 expectedBazelTargets: append(
3480 expectedBazelTargets,
3481 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3482 "srcs": `["Foo.aidl"]`,
3483 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003484 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003485 {
3486 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3487 bp: `
3488 aidl_library {
3489 name: "foo_aidl_library",
3490 srcs: ["Foo.aidl"],
3491 }
3492 cc_library {
3493 name: "foo",
3494 aidl: {
3495 libs: ["foo_aidl_library"],
3496 export_aidl_headers: true,
3497 }
3498 }`,
3499 expectedBazelTargets: append(
3500 expectedBazelTargets,
3501 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3502 "srcs": `["Foo.aidl"]`,
3503 "tags": `["apex_available=//apex_available:anyapex"]`,
3504 }),
3505 ),
3506 },
3507 }
3508
3509 for _, testCase := range testCases {
3510 runCcLibraryTestCase(t, Bp2buildTestCase{
3511 Description: "cc_library with export aidl headers",
3512 ModuleTypeUnderTest: "cc_library",
3513 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3514 Blueprint: testCase.bp,
3515 ExpectedBazelTargets: testCase.expectedBazelTargets,
3516 })
3517 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003518}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003519
3520func TestCcLibraryWithTargetApex(t *testing.T) {
3521 runCcLibraryTestCase(t, Bp2buildTestCase{
3522 Description: "cc_library with target.apex",
3523 ModuleTypeUnderTest: "cc_library",
3524 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3525 Blueprint: `
3526cc_library {
3527 name: "foo",
3528 shared_libs: ["bar", "baz"],
3529 static_libs: ["baz", "buh"],
3530 target: {
3531 apex: {
3532 exclude_shared_libs: ["bar"],
3533 exclude_static_libs: ["buh"],
3534 }
3535 }
3536}`,
3537 ExpectedBazelTargets: []string{
3538 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3539 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003540 "//build/bazel/rules/apex:in_apex": [],
3541 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003542 })`,
3543 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003544 "//build/bazel/rules/apex:in_apex": [],
3545 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003546 })`,
3547 "local_includes": `["."]`,
3548 }),
3549 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3550 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003551 "//build/bazel/rules/apex:in_apex": [],
3552 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003553 })`,
3554 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003555 "//build/bazel/rules/apex:in_apex": [],
3556 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003557 })`,
3558 "local_includes": `["."]`,
3559 }),
3560 },
3561 })
3562}
3563
3564func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3565 runCcLibraryTestCase(t, Bp2buildTestCase{
3566 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3567 ModuleTypeUnderTest: "cc_library",
3568 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3569 Blueprint: `
3570cc_library_static {
3571 name: "foo",
3572 shared_libs: ["bar", "baz"],
3573 static_libs: ["abc"],
3574 export_shared_lib_headers: ["baz"],
3575 export_static_lib_headers: ["abc"],
3576 target: {
3577 apex: {
3578 exclude_shared_libs: ["baz", "bar"],
3579 exclude_static_libs: ["abc"],
3580 }
3581 }
3582}`,
3583 ExpectedBazelTargets: []string{
3584 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3585 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003586 "//build/bazel/rules/apex:in_apex": [],
3587 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003588 })`,
3589 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003590 "//build/bazel/rules/apex:in_apex": [],
3591 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003592 })`,
3593 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003594 "//build/bazel/rules/apex:in_apex": [],
3595 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003596 })`,
3597 "local_includes": `["."]`,
3598 }),
3599 },
3600 })
3601}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003602
3603func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3604 runCcLibraryTestCase(t, Bp2buildTestCase{
3605 Description: "cc_library with sysprop sources",
3606 ModuleTypeUnderTest: "cc_library",
3607 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3608 Blueprint: `
3609cc_library {
3610 name: "foo",
3611 srcs: [
3612 "bar.sysprop",
3613 "baz.sysprop",
3614 "blah.cpp",
3615 ],
3616 min_sdk_version: "5",
3617}`,
3618 ExpectedBazelTargets: []string{
3619 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3620 "srcs": `[
3621 "bar.sysprop",
3622 "baz.sysprop",
3623 ]`,
3624 }),
3625 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3626 "dep": `":foo_sysprop_library"`,
3627 "min_sdk_version": `"5"`,
3628 }),
3629 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3630 "srcs": `["blah.cpp"]`,
3631 "local_includes": `["."]`,
3632 "min_sdk_version": `"5"`,
3633 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3634 }),
3635 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3636 "srcs": `["blah.cpp"]`,
3637 "local_includes": `["."]`,
3638 "min_sdk_version": `"5"`,
3639 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3640 }),
3641 },
3642 })
3643}
3644
3645func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3646 runCcLibraryTestCase(t, Bp2buildTestCase{
3647 Description: "cc_library with sysprop sources in some configs but not others",
3648 ModuleTypeUnderTest: "cc_library",
3649 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3650 Blueprint: `
3651cc_library {
3652 name: "foo",
3653 host_supported: true,
3654 srcs: [
3655 "blah.cpp",
3656 ],
3657 target: {
3658 android: {
3659 srcs: ["bar.sysprop"],
3660 },
3661 },
3662 min_sdk_version: "5",
3663}`,
3664 ExpectedBazelTargets: []string{
3665 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3666 "srcs": `select({
3667 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3668 "//conditions:default": [],
3669 })`,
3670 }),
3671 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3672 "dep": `":foo_sysprop_library"`,
3673 "min_sdk_version": `"5"`,
3674 }),
3675 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3676 "srcs": `["blah.cpp"]`,
3677 "local_includes": `["."]`,
3678 "min_sdk_version": `"5"`,
3679 "whole_archive_deps": `select({
3680 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3681 "//conditions:default": [],
3682 })`,
3683 }),
3684 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3685 "srcs": `["blah.cpp"]`,
3686 "local_includes": `["."]`,
3687 "min_sdk_version": `"5"`,
3688 "whole_archive_deps": `select({
3689 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3690 "//conditions:default": [],
3691 })`,
3692 }),
3693 },
3694 })
3695}
Vinh Tran395a1e92022-09-16 18:27:29 -04003696
Sam Delmerico512437b2023-03-17 11:34:15 -04003697func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003698 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003699 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003700 ModuleTypeUnderTest: "cc_library",
3701 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3702 Blueprint: `
3703cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003704 name: "foo",
3705 srcs: [
3706 "Foo.aidl",
3707 ],
3708 static_libs: [
3709 "bar-static",
3710 "baz-static",
3711 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003712 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003713 "bar-shared",
3714 "baz-shared",
3715 ],
3716 export_static_lib_headers: [
3717 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003718 ],
3719 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003720 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003721 ],
3722}` +
Sam Delmerico512437b2023-03-17 11:34:15 -04003723 simpleModuleDoNotConvertBp2build("cc_library_static", "bar-static") +
3724 simpleModuleDoNotConvertBp2build("cc_library_static", "baz-static") +
3725 simpleModuleDoNotConvertBp2build("cc_library", "bar-shared") +
3726 simpleModuleDoNotConvertBp2build("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003727 ExpectedBazelTargets: []string{
3728 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3729 "srcs": `["Foo.aidl"]`,
3730 }),
3731 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003732 "local_includes": `["."]`,
3733 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003734 "implementation_deps": `[
3735 ":baz-static",
3736 ":bar-static",
3737 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003738 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003739 ":baz-shared",
3740 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003741 ]`,
3742 }),
3743 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3744 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003745 "deps": `[":baz-static"]`,
3746 "implementation_deps": `[":bar-static"]`,
3747 "dynamic_deps": `[":baz-shared"]`,
3748 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003749 "local_includes": `["."]`,
3750 }),
3751 },
3752 })
3753}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003754
3755func TestCcLibraryWithTidy(t *testing.T) {
3756 runCcLibraryTestCase(t, Bp2buildTestCase{
3757 Description: "cc_library uses tidy properties",
3758 ModuleTypeUnderTest: "cc_library",
3759 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3760 Blueprint: `
3761cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003762 name: "foo",
3763 srcs: ["foo.cpp"],
3764}
3765cc_library_static {
3766 name: "foo-no-tidy",
3767 srcs: ["foo.cpp"],
3768 tidy: false,
3769}
3770cc_library_static {
3771 name: "foo-tidy",
3772 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003773 tidy: true,
3774 tidy_checks: ["check1", "check2"],
3775 tidy_checks_as_errors: ["check1error", "check2error"],
3776 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003777 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003778}`,
3779 ExpectedBazelTargets: []string{
3780 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3781 "local_includes": `["."]`,
3782 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003783 }),
3784 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3785 "local_includes": `["."]`,
3786 "srcs": `["foo.cpp"]`,
3787 "tidy": `"never"`,
3788 }),
3789 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3790 "local_includes": `["."]`,
3791 "srcs": `["foo.cpp"]`,
3792 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003793 "tidy_checks": `[
3794 "check1",
3795 "check2",
3796 ]`,
3797 "tidy_checks_as_errors": `[
3798 "check1error",
3799 "check2error",
3800 ]`,
3801 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003802 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003803 }),
3804 },
3805 })
3806}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003807
Vinh Tran99270ea2022-11-28 11:15:23 -05003808func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3809 bp := `
3810cc_library {
3811 name: "foo",
3812 afdo: true,
3813 include_build_directory: false,
3814}`
3815
3816 // TODO(b/260714900): Add test case for arch-specific afdo profile
3817 testCases := []struct {
3818 description string
3819 filesystem map[string]string
3820 expectedBazelTargets []string
3821 }{
3822 {
3823 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003824 filesystem: map[string]string{
3825 "vendor/google_data/pgo_profile/sampling/BUILD": "",
3826 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3827 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003828 expectedBazelTargets: []string{
3829 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3830 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3831 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3832 }),
3833 },
3834 },
3835 {
3836 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003837 filesystem: map[string]string{
3838 "toolchain/pgo-profiles/sampling/BUILD": "",
3839 "toolchain/pgo-profiles/sampling/foo.afdo": "",
3840 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003841 expectedBazelTargets: []string{
3842 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3843 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3844 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3845 }),
3846 },
3847 },
3848 {
3849 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003850 filesystem: map[string]string{
3851 "toolchain/pgo-profiles/sampling/BUILD": "",
3852 "toolchain/pgo-profiles/sampling/bar.afdo": "",
3853 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003854 expectedBazelTargets: []string{
3855 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3856 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3857 },
3858 },
3859 {
3860 description: "cc_library with afdo enabled but profile doesn't exist",
3861 expectedBazelTargets: []string{
3862 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3863 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3864 },
3865 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003866 {
3867 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3868 filesystem: map[string]string{
3869 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3870 },
3871 expectedBazelTargets: []string{
3872 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3873 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3874 },
3875 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003876 }
3877 for _, testCase := range testCases {
3878 t.Run(testCase.description, func(t *testing.T) {
3879 runCcLibraryTestCase(t, Bp2buildTestCase{
3880 ExpectedBazelTargets: testCase.expectedBazelTargets,
3881 ModuleTypeUnderTest: "cc_library",
3882 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3883 Description: testCase.description,
3884 Blueprint: binaryReplacer.Replace(bp),
3885 Filesystem: testCase.filesystem,
3886 })
3887 })
3888 }
3889}
3890
Yu Liu56ccb1a2022-11-12 10:47:07 -08003891func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3892 runCcLibraryTestCase(t, Bp2buildTestCase{
3893 Description: "cc_library with header abi checker",
3894 ModuleTypeUnderTest: "cc_library",
3895 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3896 Blueprint: `cc_library {
3897 name: "foo",
3898 header_abi_checker: {
3899 enabled: true,
3900 symbol_file: "a.map.txt",
3901 exclude_symbol_versions: [
3902 "29",
3903 "30",
3904 ],
3905 exclude_symbol_tags: [
3906 "tag1",
3907 "tag2",
3908 ],
3909 check_all_apis: true,
3910 diff_flags: ["-allow-adding-removing-weak-symbols"],
3911 },
3912 include_build_directory: false,
3913}`,
3914 ExpectedBazelTargets: []string{
3915 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3916 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3917 "abi_checker_enabled": `True`,
3918 "abi_checker_symbol_file": `"a.map.txt"`,
3919 "abi_checker_exclude_symbol_versions": `[
3920 "29",
3921 "30",
3922 ]`,
3923 "abi_checker_exclude_symbol_tags": `[
3924 "tag1",
3925 "tag2",
3926 ]`,
3927 "abi_checker_check_all_apis": `True`,
3928 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3929 }),
3930 },
3931 })
3932}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003933
3934func TestCcLibraryApexAvailable(t *testing.T) {
3935 runCcLibraryTestCase(t, Bp2buildTestCase{
3936 Description: "cc_library apex_available converted to tags",
3937 ModuleTypeUnderTest: "cc_library",
3938 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3939 Blueprint: soongCcLibraryPreamble + `
3940cc_library {
3941 name: "a",
3942 srcs: ["a.cpp"],
3943 apex_available: ["com.android.foo"],
3944}
3945`,
3946 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3947 "tags": `["apex_available=com.android.foo"]`,
3948 "srcs": `["a.cpp"]`,
3949 "local_includes": `["."]`,
3950 }),
3951 },
3952 )
3953}
3954
3955func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3956 runCcLibraryTestCase(t, Bp2buildTestCase{
3957 Description: "cc_library apex_available converted to multiple tags",
3958 ModuleTypeUnderTest: "cc_library",
3959 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3960 Blueprint: soongCcLibraryPreamble + `
3961cc_library {
3962 name: "a",
3963 srcs: ["a.cpp"],
3964 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3965}
3966`,
3967 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3968 "tags": `[
3969 "apex_available=com.android.foo",
3970 "apex_available=//apex_available:platform",
3971 "apex_available=com.android.bar",
3972 ]`,
3973 "srcs": `["a.cpp"]`,
3974 "local_includes": `["."]`,
3975 }),
3976 },
3977 )
3978}
Zi Wang0f828442022-12-28 11:18:11 -08003979
3980// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3981// In bp2build output, variant info(select) should go before general info.
3982// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3983func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3984 runCcLibraryTestCase(t, Bp2buildTestCase{
3985 Description: "cc_library variant prepend properties order",
3986 ModuleTypeUnderTest: "cc_library",
3987 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3988 Blueprint: soongCcLibraryPreamble + `
3989cc_library {
3990 name: "a",
3991 srcs: ["a.cpp"],
3992 export_include_dirs: ["eid1", "eid2"],
3993 export_system_include_dirs: ["esid1", "esid2"],
3994 target: {
3995 android: {
3996 export_include_dirs: ["android_eid1", "android_eid2"],
3997 export_system_include_dirs: ["android_esid1", "android_esid2"],
3998 },
3999 android_arm: {
4000 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
4001 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
4002 },
4003 linux: {
4004 export_include_dirs: ["linux_eid1", "linux_eid2"],
4005 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
4006 },
4007 },
4008 multilib: {
4009 lib32: {
4010 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
4011 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
4012 },
4013 },
4014 arch: {
4015 arm: {
4016 export_include_dirs: ["arm_eid1", "arm_eid2"],
4017 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
4018 },
4019 }
4020}
4021`,
4022 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4023 "export_includes": `select({
4024 "//build/bazel/platforms/os_arch:android_arm": [
4025 "android_arm_eid1",
4026 "android_arm_eid2",
4027 ],
4028 "//conditions:default": [],
4029 }) + select({
4030 "//build/bazel/platforms/os:android": [
4031 "android_eid1",
4032 "android_eid2",
4033 "linux_eid1",
4034 "linux_eid2",
4035 ],
4036 "//build/bazel/platforms/os:linux_bionic": [
4037 "linux_eid1",
4038 "linux_eid2",
4039 ],
4040 "//build/bazel/platforms/os:linux_glibc": [
4041 "linux_eid1",
4042 "linux_eid2",
4043 ],
4044 "//build/bazel/platforms/os:linux_musl": [
4045 "linux_eid1",
4046 "linux_eid2",
4047 ],
4048 "//conditions:default": [],
4049 }) + select({
4050 "//build/bazel/platforms/arch:arm": [
4051 "lib32_eid1",
4052 "lib32_eid2",
4053 "arm_eid1",
4054 "arm_eid2",
4055 ],
4056 "//build/bazel/platforms/arch:x86": [
4057 "lib32_eid1",
4058 "lib32_eid2",
4059 ],
4060 "//conditions:default": [],
4061 }) + [
4062 "eid1",
4063 "eid2",
4064 ]`,
4065 "export_system_includes": `select({
4066 "//build/bazel/platforms/os_arch:android_arm": [
4067 "android_arm_esid1",
4068 "android_arm_esid2",
4069 ],
4070 "//conditions:default": [],
4071 }) + select({
4072 "//build/bazel/platforms/os:android": [
4073 "android_esid1",
4074 "android_esid2",
4075 "linux_esid1",
4076 "linux_esid2",
4077 ],
4078 "//build/bazel/platforms/os:linux_bionic": [
4079 "linux_esid1",
4080 "linux_esid2",
4081 ],
4082 "//build/bazel/platforms/os:linux_glibc": [
4083 "linux_esid1",
4084 "linux_esid2",
4085 ],
4086 "//build/bazel/platforms/os:linux_musl": [
4087 "linux_esid1",
4088 "linux_esid2",
4089 ],
4090 "//conditions:default": [],
4091 }) + select({
4092 "//build/bazel/platforms/arch:arm": [
4093 "lib32_esid1",
4094 "lib32_esid2",
4095 "arm_esid1",
4096 "arm_esid2",
4097 ],
4098 "//build/bazel/platforms/arch:x86": [
4099 "lib32_esid1",
4100 "lib32_esid2",
4101 ],
4102 "//conditions:default": [],
4103 }) + [
4104 "esid1",
4105 "esid2",
4106 ]`,
4107 "srcs": `["a.cpp"]`,
4108 "local_includes": `["."]`,
4109 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
4110 }),
4111 },
4112 )
4113}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004114
4115func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
4116 runCcLibraryTestCase(t, Bp2buildTestCase{
4117 Description: "cc_library has correct features when integer_overflow property is provided",
4118 ModuleTypeUnderTest: "cc_library",
4119 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4120 Blueprint: `
4121cc_library {
4122 name: "foo",
4123 sanitize: {
4124 integer_overflow: true,
4125 },
4126}
4127`,
4128 ExpectedBazelTargets: []string{
4129 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4130 "features": `["ubsan_integer_overflow"]`,
4131 "local_includes": `["."]`,
4132 }),
4133 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4134 "features": `["ubsan_integer_overflow"]`,
4135 "local_includes": `["."]`,
4136 }),
4137 },
4138 })
4139}
4140
4141func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
4142 runCcLibraryTestCase(t, Bp2buildTestCase{
4143 Description: "cc_library has correct features when misc_undefined property is provided",
4144 ModuleTypeUnderTest: "cc_library",
4145 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4146 Blueprint: `
4147cc_library {
4148 name: "foo",
4149 sanitize: {
4150 misc_undefined: ["undefined", "nullability"],
4151 },
4152}
4153`,
4154 ExpectedBazelTargets: []string{
4155 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4156 "features": `[
4157 "ubsan_undefined",
4158 "ubsan_nullability",
4159 ]`,
4160 "local_includes": `["."]`,
4161 }),
4162 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4163 "features": `[
4164 "ubsan_undefined",
4165 "ubsan_nullability",
4166 ]`,
4167 "local_includes": `["."]`,
4168 }),
4169 },
4170 })
4171}
4172
4173func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4174 runCcLibraryTestCase(t, Bp2buildTestCase{
4175 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4176 ModuleTypeUnderTest: "cc_library",
4177 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4178 Blueprint: `
4179cc_library {
4180 name: "foo",
4181 sanitize: {
4182 misc_undefined: ["undefined", "nullability"],
4183 },
4184 target: {
4185 android: {
4186 sanitize: {
4187 misc_undefined: ["alignment"],
4188 },
4189 },
4190 linux_glibc: {
4191 sanitize: {
4192 integer_overflow: true,
4193 },
4194 },
4195 },
4196}
4197`,
4198 ExpectedBazelTargets: []string{
4199 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4200 "features": `[
4201 "ubsan_undefined",
4202 "ubsan_nullability",
4203 ] + select({
4204 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4205 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4206 "//conditions:default": [],
4207 })`,
4208 "local_includes": `["."]`,
4209 }),
4210 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4211 "features": `[
4212 "ubsan_undefined",
4213 "ubsan_nullability",
4214 ] + select({
4215 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4216 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4217 "//conditions:default": [],
4218 })`,
4219 "local_includes": `["."]`,
4220 }),
4221 },
4222 })
4223}
Yu Liu10174ff2023-02-21 12:05:26 -08004224
4225func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4226 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4227 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4228 ModuleTypeUnderTest: "cc_library",
4229 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4230 Blueprint: `
4231cc_library {
4232 name: "barlib",
4233 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4234 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004235 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004236}
4237cc_library {
4238 name: "bazlib",
4239 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4240 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004241 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004242}
4243cc_library {
4244 name: "foo",
4245 shared_libs: ["barlib", "bazlib"],
4246 export_shared_lib_headers: ["bazlib"],
4247 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004248 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004249 ],
4250}`,
4251 ExpectedBazelTargets: []string{
4252 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004253 "implementation_dynamic_deps": `[":barlib"]`,
4254 "dynamic_deps": `[":bazlib"]`,
4255 "local_includes": `["."]`,
4256 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004257 }),
4258 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004259 "implementation_dynamic_deps": `[":barlib"]`,
4260 "dynamic_deps": `[":bazlib"]`,
4261 "local_includes": `["."]`,
4262 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004263 }),
4264 },
4265 })
4266}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004267
4268func TestCcLibraryWithThinLto(t *testing.T) {
4269 runCcLibraryTestCase(t, Bp2buildTestCase{
4270 Description: "cc_library has correct features when thin LTO is enabled",
4271 ModuleTypeUnderTest: "cc_library",
4272 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4273 Blueprint: `
4274cc_library {
4275 name: "foo",
4276 lto: {
4277 thin: true,
4278 },
4279}`,
4280 ExpectedBazelTargets: []string{
4281 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4282 "features": `["android_thin_lto"]`,
4283 "local_includes": `["."]`,
4284 }),
4285 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4286 "features": `["android_thin_lto"]`,
4287 "local_includes": `["."]`,
4288 }),
4289 },
4290 })
4291}
4292
4293func TestCcLibraryWithLtoNever(t *testing.T) {
4294 runCcLibraryTestCase(t, Bp2buildTestCase{
4295 Description: "cc_library has correct features when LTO is explicitly disabled",
4296 ModuleTypeUnderTest: "cc_library",
4297 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4298 Blueprint: `
4299cc_library {
4300 name: "foo",
4301 lto: {
4302 never: true,
4303 },
4304}`,
4305 ExpectedBazelTargets: []string{
4306 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4307 "features": `["-android_thin_lto"]`,
4308 "local_includes": `["."]`,
4309 }),
4310 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4311 "features": `["-android_thin_lto"]`,
4312 "local_includes": `["."]`,
4313 }),
4314 },
4315 })
4316}
4317
4318func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4319 runCcLibraryTestCase(t, Bp2buildTestCase{
4320 Description: "cc_library has correct features when LTO differs across arch and os variants",
4321 ModuleTypeUnderTest: "cc_library",
4322 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4323 Blueprint: `
4324cc_library {
4325 name: "foo",
4326 target: {
4327 android: {
4328 lto: {
4329 thin: true,
4330 },
4331 },
4332 },
4333 arch: {
4334 riscv64: {
4335 lto: {
4336 thin: false,
4337 },
4338 },
4339 },
4340}`,
4341 ExpectedBazelTargets: []string{
4342 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4343 "local_includes": `["."]`,
4344 "features": `select({
4345 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4346 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4347 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4348 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4349 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4350 "//conditions:default": [],
4351 })`}),
4352 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4353 "local_includes": `["."]`,
4354 "features": `select({
4355 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4356 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4357 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4358 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4359 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4360 "//conditions:default": [],
4361 })`}),
4362 },
4363 })
4364}
4365
4366func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4367 runCcLibraryTestCase(t, Bp2buildTestCase{
4368 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4369 ModuleTypeUnderTest: "cc_library",
4370 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4371 Blueprint: `
4372cc_library {
4373 name: "foo",
4374 lto: {
4375 never: true,
4376 },
4377 target: {
4378 android: {
4379 lto: {
4380 thin: true,
4381 never: false,
4382 },
4383 },
4384 },
4385}`,
4386 ExpectedBazelTargets: []string{
4387 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4388 "local_includes": `["."]`,
4389 "features": `select({
4390 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4391 "//conditions:default": ["-android_thin_lto"],
4392 })`,
4393 }),
4394 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4395 "local_includes": `["."]`,
4396 "features": `select({
4397 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4398 "//conditions:default": ["-android_thin_lto"],
4399 })`,
4400 }),
4401 },
4402 })
4403}
4404
4405func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4406 runCcLibraryTestCase(t, Bp2buildTestCase{
4407 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4408 ModuleTypeUnderTest: "cc_library",
4409 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4410 Blueprint: `
4411cc_library {
4412 name: "foo",
4413 lto: {
4414 thin: true,
4415 },
4416 whole_program_vtables: true,
4417}`,
4418 ExpectedBazelTargets: []string{
4419 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4420 "features": `[
4421 "android_thin_lto",
4422 "android_thin_lto_whole_program_vtables",
4423 ]`,
4424 "local_includes": `["."]`,
4425 }),
4426 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4427 "features": `[
4428 "android_thin_lto",
4429 "android_thin_lto_whole_program_vtables",
4430 ]`,
4431 "local_includes": `["."]`,
4432 }),
4433 },
4434 })
4435}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004436
4437func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4438 runCcLibraryTestCase(t, Bp2buildTestCase{
4439 Description: "cc_library changes hidden visibility flag to feature",
4440 ModuleTypeUnderTest: "cc_library",
4441 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4442 Blueprint: `
4443cc_library {
4444 name: "foo",
4445 cflags: ["-fvisibility=hidden"],
4446}`,
4447 ExpectedBazelTargets: []string{
4448 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4449 "features": `["visibility_hidden"]`,
4450 "local_includes": `["."]`,
4451 }),
4452 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4453 "features": `["visibility_hidden"]`,
4454 "local_includes": `["."]`,
4455 }),
4456 },
4457 })
4458}
4459
4460func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4461 runCcLibraryTestCase(t, Bp2buildTestCase{
4462 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4463 ModuleTypeUnderTest: "cc_library",
4464 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4465 Blueprint: `
4466cc_library {
4467 name: "foo",
4468 shared: {
4469 cflags: ["-fvisibility=hidden"],
4470 },
4471}`,
4472 ExpectedBazelTargets: []string{
4473 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4474 "local_includes": `["."]`,
4475 }),
4476 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4477 "features": `["visibility_hidden"]`,
4478 "local_includes": `["."]`,
4479 }),
4480 },
4481 })
4482}
4483
4484func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4485 runCcLibraryTestCase(t, Bp2buildTestCase{
4486 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4487 ModuleTypeUnderTest: "cc_library",
4488 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4489 Blueprint: `
4490cc_library {
4491 name: "foo",
4492 static: {
4493 cflags: ["-fvisibility=hidden"],
4494 },
4495}`,
4496 ExpectedBazelTargets: []string{
4497 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4498 "features": `["visibility_hidden"]`,
4499 "local_includes": `["."]`,
4500 }),
4501 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4502 "local_includes": `["."]`,
4503 }),
4504 },
4505 })
4506}
4507
4508func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4509 runCcLibraryTestCase(t, Bp2buildTestCase{
4510 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4511 ModuleTypeUnderTest: "cc_library",
4512 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4513 Blueprint: `
4514cc_library {
4515 name: "foo",
4516 target: {
4517 android: {
4518 cflags: ["-fvisibility=hidden"],
4519 },
4520 },
4521}`,
4522 ExpectedBazelTargets: []string{
4523 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4524 "features": `select({
4525 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4526 "//conditions:default": [],
4527 })`,
4528 "local_includes": `["."]`,
4529 }),
4530 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4531 "features": `select({
4532 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4533 "//conditions:default": [],
4534 })`,
4535 "local_includes": `["."]`,
4536 }),
4537 },
4538 })
4539}
Spandan Das4242f102023-04-19 22:31:54 +00004540
4541// Test that a config_setting specific to an apex is created by cc_library.
4542func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4543 runCcLibraryTestCase(t, Bp2buildTestCase{
4544 Description: "cc_library creates a config_setting for each apex in apex_available",
4545 ModuleTypeUnderTest: "cc_library",
4546 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4547 Dir: "build/bazel/rules/apex",
4548 Blueprint: `
4549cc_library {
4550 name: "foo",
4551 apex_available: [
4552 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4553 "myapex"
4554 ],
4555}`,
4556 ExpectedBazelTargets: []string{
4557 MakeBazelTargetNoRestrictions(
4558 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004559 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004560 AttrNameToString{
4561 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004562 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004563 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004564 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004565 },
4566 ),
4567 },
4568 })
4569}
Yu Liu93893ba2023-05-01 13:49:52 -07004570
4571func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4572 runCcLibraryTestCase(t, Bp2buildTestCase{
4573 Description: "cc_library cppflags in product variables",
4574 ModuleTypeUnderTest: "cc_library",
4575 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4576 Blueprint: soongCcLibraryPreamble + `cc_library {
4577 name: "a",
4578 srcs: ["a.cpp"],
4579 cppflags: [
4580 "-Wextra",
4581 "-DDEBUG_ONLY_CODE=0",
4582 ],
4583 product_variables: {
4584 eng: {
4585 cppflags: [
4586 "-UDEBUG_ONLY_CODE",
4587 "-DDEBUG_ONLY_CODE=1",
4588 ],
4589 },
4590 },
4591 include_build_directory: false,
4592}
4593`,
4594 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4595 "cppflags": `[
4596 "-Wextra",
4597 "-DDEBUG_ONLY_CODE=0",
4598 ] + select({
4599 "//build/bazel/product_variables:eng": [
4600 "-UDEBUG_ONLY_CODE",
4601 "-DDEBUG_ONLY_CODE=1",
4602 ],
4603 "//conditions:default": [],
4604 })`,
4605 "srcs": `["a.cpp"]`,
4606 }),
4607 },
4608 )
4609}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004610
4611func TestCcLibraryYaccConversion(t *testing.T) {
4612 runCcLibraryTestCase(t, Bp2buildTestCase{
4613 Description: "cc_library is built from .y/.yy files",
4614 ModuleTypeUnderTest: "cc_library",
4615 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4616 Blueprint: soongCcLibraryPreamble + `cc_library {
4617 name: "a",
4618 srcs: [
4619 "a.cpp",
4620 "a.yy",
4621 ],
4622 shared_libs: ["sharedlib"],
4623 static_libs: ["staticlib"],
4624 yacc: {
4625 flags: ["someYaccFlag"],
4626 gen_location_hh: true,
4627 gen_position_hh: true,
4628 },
4629}
4630cc_library_static {
4631 name: "staticlib",
4632 bazel_module: { bp2build_available: false },
4633}
4634cc_library {
4635 name: "sharedlib",
4636 bazel_module: { bp2build_available: false },
4637}
4638`,
4639 ExpectedBazelTargets: []string{
4640 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4641 "src": `"a.yy"`,
4642 "implementation_deps": `[":staticlib"]`,
4643 "implementation_dynamic_deps": `[":sharedlib"]`,
4644 "flags": `["someYaccFlag"]`,
4645 "gen_location_hh": "True",
4646 "gen_position_hh": "True",
4647 "local_includes": `["."]`,
4648 }),
4649 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4650 "srcs": `["a.cpp"]`,
4651 "implementation_deps": `[":staticlib"]`,
4652 "implementation_dynamic_deps": `[":sharedlib"]`,
4653 "implementation_whole_archive_deps": `[":a_yacc"]`,
4654 "local_includes": `["."]`,
4655 }),
4656 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4657 "srcs": `["a.cpp"]`,
4658 "implementation_deps": `[":staticlib"]`,
4659 "implementation_dynamic_deps": `[":sharedlib"]`,
4660 "implementation_whole_archive_deps": `[":a_yacc"]`,
4661 "local_includes": `["."]`,
4662 }),
4663 },
4664 })
4665}