blob: 02ed57a514295abcc0ea3d4aab798e7a5f1fc864 [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 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000903 "srcs": `["a.cpp"]`,
904 "features": `["android_cfi_exports_map"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500905 }),
906 },
907 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200908}
909
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000910func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000911 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000912 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000913 ModuleTypeUnderTest: "cc_library",
914 ModuleTypeUnderTestFactory: cc.LibraryFactory,
915 Dir: "foo/bar",
916 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200917 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400918cc_library {
919 name: "a",
920 srcs: ["a.cpp"],
921 arch: {
922 arm: {
923 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000924 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400925 },
926 arm64: {
927 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000928 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400929 },
930 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200931
Liz Kammer8337ea42021-09-10 10:06:32 -0400932 bazel_module: { bp2build_available: true },
933 include_build_directory: false,
934}
Liz Kammerd366c902021-06-03 13:43:01 -0400935 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200936 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000937 Blueprint: soongCcLibraryPreamble,
938 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500939 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000940 "//build/bazel/platforms/arch:arm": [
941 "arm.map",
942 "dynamic_arm.list",
943 ],
944 "//build/bazel/platforms/arch:arm64": [
945 "arm64.map",
946 "dynamic_arm64.list",
947 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400948 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500949 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500950 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000951 "//build/bazel/platforms/arch:arm": [
952 "-Wl,--version-script,$(location arm.map)",
953 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
954 ],
955 "//build/bazel/platforms/arch:arm64": [
956 "-Wl,--version-script,$(location arm64.map)",
957 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
958 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400959 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500960 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500961 "srcs": `["a.cpp"]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000962 "features": `select({
963 "//build/bazel/platforms/arch:arm": ["android_cfi_exports_map"],
964 "//build/bazel/platforms/arch:arm64": ["android_cfi_exports_map"],
965 "//conditions:default": [],
966 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500967 }),
968 },
969 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200970}
971
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000972func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
973 runCcLibraryTestCase(t, Bp2buildTestCase{
974 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
975 ModuleTypeUnderTest: "cc_library",
976 ModuleTypeUnderTestFactory: cc.LibraryFactory,
977 Filesystem: map[string]string{
978 "version_script": "",
979 "dynamic.list": "",
980 },
981 Blueprint: `
982cc_library {
983 name: "foo",
984 ldflags: [
985 "--nospace_flag",
986 "-z spaceflag",
987 ],
988 version_script: "version_script",
989 dynamic_list: "dynamic.list",
990 include_build_directory: false,
991}
992`,
993 ExpectedBazelTargets: []string{
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000994 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
995 "features": `["android_cfi_exports_map"]`,
996 }),
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000997 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
998 "additional_linker_inputs": `[
999 "version_script",
1000 "dynamic.list",
1001 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001002 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001003 "linkopts": `[
1004 "--nospace_flag",
1005 "-z",
1006 "spaceflag",
1007 "-Wl,--version-script,$(location version_script)",
1008 "-Wl,--dynamic-list,$(location dynamic.list)",
1009 ]`,
1010 }),
1011 },
1012 })
1013}
1014
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001015func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001016 runCcLibraryTestCase(t, Bp2buildTestCase{
1017 Description: "cc_library shared_libs",
1018 ModuleTypeUnderTest: "cc_library",
1019 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1020 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001021cc_library {
1022 name: "mylib",
Liz Kammer8337ea42021-09-10 10:06:32 -04001023 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001024}
1025
1026cc_library {
1027 name: "a",
1028 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001029 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001030}
1031`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001032 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001033 "implementation_dynamic_deps": `[":mylib"]`,
1034 }),
1035 },
1036 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001037}
1038
Liz Kammer0eae52e2021-10-06 10:32:26 -04001039func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001040 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001042 "features": `[
1043 "disable_pack_relocations",
1044 "-no_undefined_symbols",
1045 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001046 "native_coverage": `False`,
1047 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001048 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001049 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001050 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001051 "//build/bazel/platforms/arch:x86_64": [
1052 "disable_pack_relocations",
1053 "-no_undefined_symbols",
1054 ],
1055 "//conditions:default": [],
1056 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001057 "native_coverage": `False`,
1058 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001059 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001060 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001061 "features": `select({
1062 "//build/bazel/platforms/os:darwin": [
1063 "disable_pack_relocations",
1064 "-no_undefined_symbols",
1065 ],
1066 "//conditions:default": [],
1067 })`,
1068 "srcs": `["c.cpp"]`,
1069 })...)
1070
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001071 runCcLibraryTestCase(t, Bp2buildTestCase{
1072 Description: "cc_library pack_relocations test",
1073 ModuleTypeUnderTest: "cc_library",
1074 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1075 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001076cc_library {
1077 name: "a",
1078 srcs: ["a.cpp"],
1079 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001080 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001081 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001082 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001083}
1084
1085cc_library {
1086 name: "b",
1087 srcs: ["b.cpp"],
1088 arch: {
1089 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001090 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001091 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001092 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001093 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001094 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001095 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001096}
1097
1098cc_library {
1099 name: "c",
1100 srcs: ["c.cpp"],
1101 target: {
1102 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001103 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001104 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001105 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001106 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001107 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001108}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001109 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001110 })
1111}
1112
1113func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001114 runCcLibraryTestCase(t, Bp2buildTestCase{
1115 Description: "cc_library spaces in copts",
1116 ModuleTypeUnderTest: "cc_library",
1117 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1118 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001119cc_library {
1120 name: "a",
1121 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001122 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001123}
1124`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001125 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001126 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001127 "-include",
1128 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001129 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001130 }),
1131 },
1132 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001133}
1134
1135func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001136 runCcLibraryTestCase(t, Bp2buildTestCase{
1137 Description: "cc_library cppflags usage",
1138 ModuleTypeUnderTest: "cc_library",
1139 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1140 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001141 name: "a",
1142 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001143 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001144 cppflags: [
1145 "-fsigned-char",
1146 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001147 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001148 arch: {
1149 arm64: {
1150 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001151 },
Liz Kammerd366c902021-06-03 13:43:01 -04001152 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001153 target: {
1154 android: {
1155 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001156 },
Liz Kammerd366c902021-06-03 13:43:01 -04001157 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001158 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001159}
1160`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001161 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001162 "copts": `["-Wall"]`,
1163 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001164 "-fsigned-char",
1165 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001166 ] + select({
1167 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1168 "//conditions:default": [],
1169 }) + select({
1170 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1171 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001172 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001173 "srcs": `["a.cpp"]`,
1174 }),
1175 },
1176 )
Jingwen Chen63930982021-03-24 10:04:33 -04001177}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001178
Liz Kammer47535c52021-06-02 16:02:22 -04001179func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001180 runCcLibraryTestCase(t, Bp2buildTestCase{
1181 ModuleTypeUnderTest: "cc_library",
1182 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1183 Filesystem: map[string]string{},
1184 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001185cc_library {
1186 name: "foo_static",
1187 srcs: ["common.c"],
1188 whole_static_libs: [
1189 "arm_whole_static_lib_excludes",
1190 "malloc_not_svelte_whole_static_lib_excludes"
1191 ],
1192 static_libs: [
1193 "arm_static_lib_excludes",
1194 "malloc_not_svelte_static_lib_excludes"
1195 ],
1196 shared_libs: [
1197 "arm_shared_lib_excludes",
1198 ],
1199 arch: {
1200 arm: {
1201 exclude_shared_libs: [
1202 "arm_shared_lib_excludes",
1203 ],
1204 exclude_static_libs: [
1205 "arm_static_lib_excludes",
1206 "arm_whole_static_lib_excludes",
1207 ],
1208 },
1209 },
1210 product_variables: {
1211 malloc_not_svelte: {
1212 shared_libs: ["malloc_not_svelte_shared_lib"],
1213 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1214 exclude_static_libs: [
1215 "malloc_not_svelte_static_lib_excludes",
1216 "malloc_not_svelte_whole_static_lib_excludes",
1217 ],
1218 },
1219 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001220 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001221}
1222
1223cc_library {
1224 name: "arm_whole_static_lib_excludes",
1225 bazel_module: { bp2build_available: false },
1226}
1227
1228cc_library {
1229 name: "malloc_not_svelte_whole_static_lib",
1230 bazel_module: { bp2build_available: false },
1231}
1232
1233cc_library {
1234 name: "malloc_not_svelte_whole_static_lib_excludes",
1235 bazel_module: { bp2build_available: false },
1236}
1237
1238cc_library {
1239 name: "arm_static_lib_excludes",
1240 bazel_module: { bp2build_available: false },
1241}
1242
1243cc_library {
1244 name: "malloc_not_svelte_static_lib_excludes",
1245 bazel_module: { bp2build_available: false },
1246}
1247
1248cc_library {
1249 name: "arm_shared_lib_excludes",
1250 bazel_module: { bp2build_available: false },
1251}
1252
1253cc_library {
1254 name: "malloc_not_svelte_shared_lib",
1255 bazel_module: { bp2build_available: false },
1256}
1257`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001258 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001259 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001260 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001261 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001262 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001263 "//build/bazel/product_config/config_settings:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001264 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001265 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001266 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001267 "//build/bazel/platforms/arch:arm": [],
1268 "//conditions:default": [":arm_shared_lib_excludes"],
1269 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001270 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
Liz Kammer7a210ac2021-09-22 15:52:58 -04001271 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001272 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001273 "srcs_c": `["common.c"]`,
1274 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001275 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001276 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001277 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001278 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
Chris Parsons953b3562021-09-20 15:14:39 -04001279 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001280 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001281 }),
1282 },
1283 )
Liz Kammer47535c52021-06-02 16:02:22 -04001284}
Liz Kammerd366c902021-06-03 13:43:01 -04001285
Zi Wang0a8a1292022-08-30 06:27:01 +00001286func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1287 runCcLibraryTestCase(t, Bp2buildTestCase{
1288 ModuleTypeUnderTest: "cc_library",
1289 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1290 Filesystem: map[string]string{},
1291 Blueprint: soongCcLibraryStaticPreamble + `
1292cc_library {
1293 name: "foo_static",
1294 srcs: ["common.c"],
1295 product_variables: {
1296 malloc_not_svelte: {
1297 header_libs: ["malloc_not_svelte_header_lib"],
1298 },
1299 },
1300 include_build_directory: false,
1301}
1302
1303cc_library {
1304 name: "malloc_not_svelte_header_lib",
1305 bazel_module: { bp2build_available: false },
1306}
1307`,
1308 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1309 "implementation_deps": `select({
Cole Faust87c0c332023-07-31 12:10:12 -07001310 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
Zi Wang0a8a1292022-08-30 06:27:01 +00001311 "//conditions:default": [],
1312 })`,
1313 "srcs_c": `["common.c"]`,
1314 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1315 }),
1316 },
1317 )
1318}
1319
Liz Kammerd366c902021-06-03 13:43:01 -04001320func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001321 runCcLibraryTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001322 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001323 ModuleTypeUnderTest: "cc_library",
1324 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1325 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001326 "impl.cpp": "",
1327 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001328 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001329cc_library {
1330 name: "foo-lib",
1331 srcs: ["impl.cpp"],
1332 nocrt: true,
1333 include_build_directory: false,
1334}
1335`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001336 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001337 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001338 "srcs": `["impl.cpp"]`,
1339 }),
1340 },
1341 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001342}
1343
1344func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001345 runCcLibraryTestCase(t, Bp2buildTestCase{
1346 Description: "cc_library - nocrt: false - does not emit attribute",
1347 ModuleTypeUnderTest: "cc_library",
1348 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1349 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001350 "impl.cpp": "",
1351 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001352 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001353cc_library {
1354 name: "foo-lib",
1355 srcs: ["impl.cpp"],
1356 nocrt: false,
1357 include_build_directory: false,
1358}
1359`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001360 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001361 "srcs": `["impl.cpp"]`,
1362 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001363 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001364}
1365
1366func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001367 runCcLibraryTestCase(t, Bp2buildTestCase{
1368 Description: "cc_library - nocrt in select",
1369 ModuleTypeUnderTest: "cc_library",
1370 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1371 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001372 "impl.cpp": "",
1373 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001374 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001375cc_library {
1376 name: "foo-lib",
1377 srcs: ["impl.cpp"],
1378 arch: {
1379 arm: {
1380 nocrt: true,
1381 },
1382 x86: {
1383 nocrt: false,
1384 },
1385 },
1386 include_build_directory: false,
1387}
1388`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001389 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1390 "features": `select({
1391 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1392 "//conditions:default": [],
1393 })`,
1394 "srcs": `["impl.cpp"]`,
1395 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001396 })
1397}
1398
1399func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001400 runCcLibraryTestCase(t, Bp2buildTestCase{
1401 ModuleTypeUnderTest: "cc_library",
1402 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1403 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001404 "impl.cpp": "",
1405 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001407cc_library {
1408 name: "foo-lib",
1409 srcs: ["impl.cpp"],
1410 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001411 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001412}
1413`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001414 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001415 "features": `["-use_libcrt"]`,
1416 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001417 }),
1418 })
1419}
1420
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001421func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001422 STATIC_ONLY_ATTRS := map[string]bool{}
1423 SHARED_ONLY_ATTRS := map[string]bool{
1424 "link_crt": true,
1425 "additional_linker_inputs": true,
1426 "linkopts": true,
1427 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001428 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001429 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001430 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001431 }
Wei Li81852ca2022-07-27 00:22:06 -07001432
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001433 sharedAttrs := AttrNameToString{}
1434 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001435 for key, val := range attrs {
1436 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1437 sharedAttrs[key] = val
1438 }
1439 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1440 staticAttrs[key] = val
1441 }
1442 }
Alixe06d75b2022-08-31 18:28:19 +00001443 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1444 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001445
1446 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001447}
1448
Jingwen Chen6ada5892021-09-17 11:38:09 +00001449func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001450 runCcLibraryTestCase(t, Bp2buildTestCase{
1451 ModuleTypeUnderTest: "cc_library",
1452 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1453 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001454 "impl.cpp": "",
1455 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001456 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001457cc_library {
1458 name: "foo-lib",
1459 srcs: ["impl.cpp"],
1460 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001461 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001462}
1463`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001464 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001465 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001466 }),
1467 })
Liz Kammerd366c902021-06-03 13:43:01 -04001468}
1469
Jingwen Chen6ada5892021-09-17 11:38:09 +00001470func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001471 runCcLibraryTestCase(t, Bp2buildTestCase{
1472 ModuleTypeUnderTest: "cc_library",
1473 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1474 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001475 "impl.cpp": "",
1476 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001477 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001478cc_library {
1479 name: "foo-lib",
1480 srcs: ["impl.cpp"],
1481 arch: {
1482 arm: {
1483 no_libcrt: true,
1484 },
1485 x86: {
1486 no_libcrt: true,
1487 },
1488 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001489 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001490}
1491`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001492 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001493 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001494 "features": `select({
1495 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1496 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1497 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001498 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001499 }),
1500 })
Liz Kammerd366c902021-06-03 13:43:01 -04001501}
1502
Chris Parsons58852a02021-12-09 18:10:18 -05001503func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001504 runCcLibraryTestCase(t, Bp2buildTestCase{
1505 ModuleTypeUnderTest: "cc_library",
1506 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1507 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001508 "impl.cpp": "",
1509 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001510 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001511cc_library {
1512 name: "foo-lib",
1513 srcs: ["impl.cpp"],
1514 arch: {
1515 arm: {
1516 no_libcrt: true,
1517 },
1518 x86: {
1519 no_libcrt: true,
1520 },
1521 },
1522 target: {
1523 darwin: {
1524 no_libcrt: true,
1525 }
1526 },
1527 include_build_directory: false,
1528}
1529`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001530 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001531 "features": `select({
1532 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1533 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1534 "//conditions:default": [],
1535 }) + select({
1536 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1537 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001538 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001539 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001540 }),
1541 })
1542}
1543
1544func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001545 runCcLibraryTestCase(t, Bp2buildTestCase{
1546 ModuleTypeUnderTest: "cc_library",
1547 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1548 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001549 "impl.cpp": "",
1550 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001551 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001552cc_library {
1553 name: "foo-lib",
1554 srcs: ["impl.cpp"],
1555 arch: {
1556 arm: {
1557 no_libcrt: true,
1558 },
1559 // This is expected to override the value for darwin_x86_64.
1560 x86_64: {
1561 no_libcrt: true,
1562 },
1563 },
1564 target: {
1565 darwin: {
1566 no_libcrt: false,
1567 }
1568 },
1569 include_build_directory: false,
1570}
1571`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001572 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001573 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001574 "features": `select({
1575 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1576 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1577 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001578 })`,
1579 }),
1580 })
1581}
1582
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001583func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001584 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001585 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001586 "strip": `{
1587 "all": True,
1588 }`,
1589 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001590 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001591 "strip": `{
1592 "keep_symbols": True,
1593 }`,
1594 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001595 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001596 "strip": `{
1597 "keep_symbols_and_debug_frame": True,
1598 }`,
1599 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001600 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001601 "strip": `{
1602 "keep_symbols_list": ["symbol"],
1603 }`,
1604 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001605 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001606 "strip": `{
1607 "none": True,
1608 }`,
1609 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001610 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001611
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001612 runCcLibraryTestCase(t, Bp2buildTestCase{
1613 Description: "cc_library strip args",
1614 ModuleTypeUnderTest: "cc_library",
1615 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1616 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001617cc_library {
1618 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001619 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001620}
1621cc_library {
1622 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001623 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001624 keep_symbols: true,
1625 },
1626 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001627}
1628cc_library {
1629 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001630 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001631 keep_symbols_and_debug_frame: true,
1632 },
1633 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001634}
1635cc_library {
1636 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001637 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001638 none: true,
1639 },
1640 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001641}
1642cc_library {
1643 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001644 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001645 keep_symbols_list: ["symbol"],
1646 },
1647 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001648}
1649cc_library {
1650 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001651 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001652 all: true,
1653 },
1654 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001655}
1656`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001657 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001658 })
1659}
1660
1661func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001662 runCcLibraryTestCase(t, Bp2buildTestCase{
1663 Description: "cc_library strip args",
1664 ModuleTypeUnderTest: "cc_library",
1665 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1666 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001667cc_library {
1668 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001669 target: {
1670 darwin: {
1671 strip: {
1672 keep_symbols_list: ["foo", "bar"]
1673 }
1674 },
1675 },
1676 arch: {
1677 arm: {
1678 strip: {
1679 keep_symbols_and_debug_frame: true,
1680 },
1681 },
1682 arm64: {
1683 strip: {
1684 keep_symbols: true,
1685 },
1686 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001687 },
1688 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001689}
1690`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001691 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001692 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001693 "keep_symbols": select({
1694 "//build/bazel/platforms/arch:arm64": True,
1695 "//conditions:default": None,
1696 }),
1697 "keep_symbols_and_debug_frame": select({
1698 "//build/bazel/platforms/arch:arm": True,
1699 "//conditions:default": None,
1700 }),
1701 "keep_symbols_list": select({
1702 "//build/bazel/platforms/os:darwin": [
1703 "foo",
1704 "bar",
1705 ],
1706 "//conditions:default": [],
1707 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001708 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001709 }),
1710 },
1711 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001712}
Chris Parsons51f8c392021-08-03 21:01:05 -04001713
1714func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001715 runCcLibraryTestCase(t, Bp2buildTestCase{
1716 Description: "cc_library system_shared_libs empty at root",
1717 ModuleTypeUnderTest: "cc_library",
1718 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1719 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001720cc_library {
1721 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001722 system_shared_libs: [],
1723 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001724}
1725`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001726 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001727 "system_dynamic_deps": `[]`,
1728 }),
1729 },
1730 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001731}
1732
1733func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001734 runCcLibraryTestCase(t, Bp2buildTestCase{
1735 Description: "cc_library system_shared_libs empty for static variant",
1736 ModuleTypeUnderTest: "cc_library",
1737 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1738 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001739cc_library {
1740 name: "static_empty",
1741 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001742 system_shared_libs: [],
1743 },
1744 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001745}
1746`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001747 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001748 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001749 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001750 }),
Alixe06d75b2022-08-31 18:28:19 +00001751 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001752 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001753 })
1754}
1755
1756func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001757 runCcLibraryTestCase(t, Bp2buildTestCase{
1758 Description: "cc_library system_shared_libs empty for shared variant",
1759 ModuleTypeUnderTest: "cc_library",
1760 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1761 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001762cc_library {
1763 name: "shared_empty",
1764 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001765 system_shared_libs: [],
1766 },
1767 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001768}
1769`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001770 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001771 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1772 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001773 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001774 }),
1775 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001776 })
1777}
1778
1779func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001780 runCcLibraryTestCase(t, Bp2buildTestCase{
1781 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1782 ModuleTypeUnderTest: "cc_library",
1783 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1784 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001785cc_library {
1786 name: "shared_empty",
1787 target: {
1788 bionic: {
1789 shared: {
1790 system_shared_libs: [],
1791 }
1792 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001793 },
1794 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001795}
1796`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001797 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001798 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1799 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001800 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001801 }),
1802 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001803 })
1804}
1805
1806func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1807 // Note that this behavior is technically incorrect (it's a simplification).
1808 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1809 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1810 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001811 runCcLibraryTestCase(t, Bp2buildTestCase{
1812 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1813 ModuleTypeUnderTest: "cc_library",
1814 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1815 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001816cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001817 name: "libc_musl",
1818 bazel_module: { bp2build_available: false },
1819}
1820
1821cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001822 name: "target_linux_bionic_empty",
1823 target: {
1824 linux_bionic: {
1825 system_shared_libs: [],
1826 },
1827 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001828 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001829}
1830`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001831 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001832 "system_dynamic_deps": `select({
1833 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1834 "//conditions:default": [],
1835 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001836 }),
1837 },
1838 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001839}
1840
1841func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001842 runCcLibraryTestCase(t, Bp2buildTestCase{
1843 Description: "cc_library system_shared_libs empty for bionic variant",
1844 ModuleTypeUnderTest: "cc_library",
1845 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1846 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001847cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001848 name: "libc_musl",
1849 bazel_module: { bp2build_available: false },
1850}
1851
1852cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001853 name: "target_bionic_empty",
1854 target: {
1855 bionic: {
1856 system_shared_libs: [],
1857 },
1858 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001859 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001860}
1861`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001862 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001863 "system_dynamic_deps": `select({
1864 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1865 "//conditions:default": [],
1866 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001867 }),
1868 },
1869 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001870}
1871
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001872func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1873 runCcLibraryTestCase(t, Bp2buildTestCase{
1874 Description: "cc_library system_shared_lib empty for musl variant",
1875 ModuleTypeUnderTest: "cc_library",
1876 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1877 Blueprint: soongCcLibraryPreamble + `
1878cc_library {
1879 name: "libc_musl",
1880 bazel_module: { bp2build_available: false },
1881}
1882
1883cc_library {
1884 name: "target_musl_empty",
1885 target: {
1886 musl: {
1887 system_shared_libs: [],
1888 },
1889 },
1890 include_build_directory: false,
1891}
1892`,
1893 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1894 "system_dynamic_deps": `[]`,
1895 }),
1896 })
1897}
1898
1899func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1900 runCcLibraryTestCase(t, Bp2buildTestCase{
1901 Description: "cc_library system_shared_lib empty for linux_musl variant",
1902 ModuleTypeUnderTest: "cc_library",
1903 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1904 Blueprint: soongCcLibraryPreamble + `
1905cc_library {
1906 name: "libc_musl",
1907 bazel_module: { bp2build_available: false },
1908}
1909
1910cc_library {
1911 name: "target_linux_musl_empty",
1912 target: {
1913 linux_musl: {
1914 system_shared_libs: [],
1915 },
1916 },
1917 include_build_directory: false,
1918}
1919`,
1920 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1921 "system_dynamic_deps": `[]`,
1922 }),
1923 })
1924}
Chris Parsons51f8c392021-08-03 21:01:05 -04001925func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001926 runCcLibraryTestCase(t, Bp2buildTestCase{
1927 Description: "cc_library system_shared_libs set for shared and root",
1928 ModuleTypeUnderTest: "cc_library",
1929 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1930 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001931cc_library {
1932 name: "libc",
1933 bazel_module: { bp2build_available: false },
1934}
1935cc_library {
1936 name: "libm",
1937 bazel_module: { bp2build_available: false },
1938}
Chris Parsons51f8c392021-08-03 21:01:05 -04001939
1940cc_library {
1941 name: "foo",
1942 system_shared_libs: ["libc"],
1943 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001944 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001945 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001946 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001947}
1948`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001949 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001950 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001951 "system_dynamic_deps": `[":libc"]`,
1952 }),
Alixe06d75b2022-08-31 18:28:19 +00001953 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001954 "system_dynamic_deps": `[
1955 ":libc",
1956 ":libm",
1957 ]`,
1958 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001959 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001960 })
1961}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001962
1963func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001964 runCcLibraryTestCase(t, Bp2buildTestCase{
1965 Description: "cc_library - selects for all os targets",
1966 ModuleTypeUnderTest: "cc_library",
1967 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1968 Filesystem: map[string]string{},
1969 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001970cc_library {
1971 name: "foo-lib",
1972 srcs: ["base.cpp"],
1973 target: {
1974 android: {
1975 srcs: ["android.cpp"],
1976 },
1977 linux: {
1978 srcs: ["linux.cpp"],
1979 },
1980 linux_glibc: {
1981 srcs: ["linux_glibc.cpp"],
1982 },
1983 darwin: {
1984 srcs: ["darwin.cpp"],
1985 },
1986 bionic: {
1987 srcs: ["bionic.cpp"],
1988 },
1989 linux_musl: {
1990 srcs: ["linux_musl.cpp"],
1991 },
1992 windows: {
1993 srcs: ["windows.cpp"],
1994 },
1995 },
1996 include_build_directory: false,
1997}
1998`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001999 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002000 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002001 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002002 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002003 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002004 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002005 ],
2006 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002007 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002008 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002009 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002010 ],
Colin Cross133782e2022-12-20 15:29:31 -08002011 "//build/bazel/platforms/os:linux_glibc": [
2012 "linux.cpp",
2013 "linux_glibc.cpp",
2014 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002015 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002016 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002017 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002018 ],
2019 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2020 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002021 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002022 }),
2023 },
2024 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002025}
2026
Yu Liu75be7b92022-02-01 09:54:09 -08002027func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002028 runCcLibraryTestCase(t, Bp2buildTestCase{
2029 Description: "cc_library - libcrypto hash injection",
2030 ModuleTypeUnderTest: "cc_library",
2031 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2032 Filesystem: map[string]string{},
2033 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002034cc_library {
2035 name: "libcrypto",
2036 target: {
2037 android: {
2038 inject_bssl_hash: true,
2039 },
2040 },
2041 include_build_directory: false,
2042}
2043`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002044 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002045 "inject_bssl_hash": `select({
2046 "//build/bazel/platforms/os:android": True,
2047 "//conditions:default": None,
2048 })`,
2049 }),
2050 },
2051 )
2052}
2053
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002054func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002055 type testCase struct {
2056 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002057 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002058 gnu_extensions string
2059 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002060 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002061 }
2062
2063 testCases := []testCase{
2064 // Existing usages of cpp_std in AOSP are:
2065 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2066 //
2067 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2068 // is set in the toolchain.)
2069 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002070 {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 +00002071 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2072 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002073 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2074 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2075 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002076 // Explicitly setting a c++ std does not use replace gnu++ std even if
2077 // gnu_extensions is true.
2078 // "c++11",
2079 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002080 {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 +00002081 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2082 // "c++17",
2083 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002084 {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 +00002085 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2086 // "c++2a",
2087 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002088 {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 +00002089 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2090 // "c++98",
2091 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002092 {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 +00002093 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2094 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2095 // "gnu++11",
2096 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002097 {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 +00002098 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2099 // "gnu++17",
2100 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002101 {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 +00002102 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002103
2104 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002105 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2106 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2107 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002108 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2109 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2110 {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 +00002111 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002112 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002113 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2114 t.Run(name, func(t *testing.T) {
2115 name_prefix := fmt.Sprintf("a_%v", i)
2116 cppStdProp := ""
2117 if tc.cpp_std != "" {
2118 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2119 }
2120 cStdProp := ""
2121 if tc.c_std != "" {
2122 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2123 }
2124 gnuExtensionsProp := ""
2125 if tc.gnu_extensions != "" {
2126 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2127 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002128 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002129 if tc.bazel_cpp_std != "" {
2130 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2131 }
2132 if tc.bazel_c_std != "" {
2133 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2134 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002135
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002136 runCcLibraryTestCase(t, Bp2buildTestCase{
2137 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002138 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002139 ModuleTypeUnderTest: "cc_library",
2140 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2141 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002142cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002143 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002144%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002145%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002146%s // gnu_extensions: *bool
2147 include_build_directory: false,
2148}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002149`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002150 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002151 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002152
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002153 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2154 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002155 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002156 ModuleTypeUnderTest: "cc_library_static",
2157 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2158 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002159cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002160 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002161%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002162%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002163%s // gnu_extensions: *bool
2164 include_build_directory: false,
2165}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002166`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002167 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002168 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002169 },
2170 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002171
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002172 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2173 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002174 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002175 ModuleTypeUnderTest: "cc_library_shared",
2176 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2177 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002178cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002179 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002180%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002181%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002182%s // gnu_extensions: *bool
2183 include_build_directory: false,
2184}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002185`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002186 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002187 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002188 },
2189 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002190 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002191 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002192}
Liz Kammer12615db2021-09-28 09:19:17 -04002193
2194func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002195 runCcLibraryTestCase(t, Bp2buildTestCase{
2196 ModuleTypeUnderTest: "cc_library",
2197 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2198 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002199 name: "foo",
2200 srcs: ["foo.proto"],
2201 include_build_directory: false,
2202}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002203 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002204 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002205 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002206 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002207 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002208 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002209 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002210 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002211 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002212 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2213 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002214 }),
2215 },
2216 })
2217}
2218
2219func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002220 runCcLibraryTestCase(t, Bp2buildTestCase{
2221 ModuleTypeUnderTest: "cc_library",
2222 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2223 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002224 name: "foo",
2225 srcs: ["foo.proto"],
2226 proto: { canonical_path_from_root: false},
2227 include_build_directory: false,
2228}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002229 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002230 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002231 "srcs": `["foo.proto"]`,
2232 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002233 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002234 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002235 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002236 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002237 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002238 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002239 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2240 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002241 }),
2242 },
2243 })
2244}
2245
2246func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002247 runCcLibraryTestCase(t, Bp2buildTestCase{
2248 ModuleTypeUnderTest: "cc_library",
2249 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2250 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002251 name: "foo",
2252 srcs: ["foo.proto"],
2253 proto: { canonical_path_from_root: true},
2254 include_build_directory: false,
2255}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002256 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002257 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002258 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002259 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002260 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002261 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002262 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002263 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002264 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002265 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2266 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002267 }),
2268 },
2269 })
2270}
2271
2272func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002273 runCcLibraryTestCase(t, Bp2buildTestCase{
2274 ModuleTypeUnderTest: "cc_library",
2275 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2276 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002277 name: "foo",
2278 srcs: ["foo.proto"],
2279 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002280 type: "full",
2281 },
2282 include_build_directory: false,
2283}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002284 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002285 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002286 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002287 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002288 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002289 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002290 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002291 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002292 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002293 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2294 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002295 }),
2296 },
2297 })
2298}
2299
2300func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002301 runCcLibraryTestCase(t, Bp2buildTestCase{
2302 ModuleTypeUnderTest: "cc_library",
2303 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2304 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002305 name: "foo",
2306 srcs: ["foo.proto"],
2307 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002308 type: "lite",
2309 },
2310 include_build_directory: false,
2311}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002312 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002313 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002314 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002315 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002316 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002317 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002318 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002319 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002320 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002321 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2322 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002323 }),
2324 },
2325 })
2326}
2327
2328func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002329 runCcLibraryTestCase(t, Bp2buildTestCase{
2330 ModuleTypeUnderTest: "cc_library",
2331 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2332 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002333 name: "foo",
2334 srcs: ["foo.proto"],
2335 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002336 export_proto_headers: true,
2337 },
2338 include_build_directory: false,
2339}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002340 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002341 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002342 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002343 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002344 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002345 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002346 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002347 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002348 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002349 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2350 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002351 }),
2352 },
2353 })
2354}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002355
Yu Liu2d136142022-08-18 14:46:13 -07002356func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2357 runCcLibraryTestCase(t, Bp2buildTestCase{
2358 ModuleTypeUnderTest: "cc_library",
2359 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2360 Blueprint: soongCcProtoPreamble + `cc_library {
2361 name: "foo",
2362 srcs: ["foo.proto"],
2363 proto: {
2364 include_dirs: ["external/protobuf/src"],
2365 },
2366 include_build_directory: false,
2367}`,
2368 ExpectedBazelTargets: []string{
2369 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2370 "srcs": `["foo.proto"]`,
2371 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2372 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2373 "deps": `[":foo_proto"]`,
2374 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2375 "deps": `[":libprotobuf-cpp-lite"]`,
2376 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2377 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002378 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2379 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002380 }),
2381 },
2382 })
2383}
2384
2385func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2386 runCcLibraryTestCase(t, Bp2buildTestCase{
2387 ModuleTypeUnderTest: "cc_library",
2388 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2389 Blueprint: soongCcProtoPreamble + `cc_library {
2390 name: "foo",
2391 srcs: ["foo.proto"],
2392 proto: {
2393 include_dirs: ["external/protobuf/abc"],
2394 },
2395 include_build_directory: false,
2396}`,
Spandan Dasec39d512023-08-15 22:08:18 +00002397 ExpectedErr: fmt.Errorf("module \"foo\": TODO: Add support for proto.include_dir: external/protobuf/abc. This directory does not contain an Android.bp file"),
Yu Liu2d136142022-08-18 14:46:13 -07002398 })
2399}
2400
Yu Liu2aa806b2022-09-01 11:54:47 -07002401func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2402 runCcLibraryTestCase(t, Bp2buildTestCase{
2403 ModuleTypeUnderTest: "cc_library",
2404 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2405 Blueprint: soongCcProtoPreamble + `
2406filegroup {
2407 name: "a_fg_proto",
2408 srcs: ["a_fg.proto"],
2409}
2410
2411cc_library {
2412 name: "a",
2413 srcs: [
2414 ":a_fg_proto",
2415 "a.proto",
2416 ],
2417 proto: {
2418 export_proto_headers: true,
2419 },
2420 include_build_directory: false,
2421}`,
2422 ExpectedBazelTargets: []string{
2423 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002424 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002425 "srcs": `["a.proto"]`,
2426 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2427 "deps": `[
2428 ":a_fg_proto_bp2build_converted",
2429 ":a_proto",
2430 ]`,
2431 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2432 "deps": `[":libprotobuf-cpp-lite"]`,
2433 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2434 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2435 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2436 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002437 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002438 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002439 "tags": `[
2440 "apex_available=//apex_available:anyapex",
2441 "manual",
2442 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002443 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2444 "actual": `"//.:a_fg_proto_proto"`,
2445 "tags": `[
2446 "apex_available=//apex_available:anyapex",
2447 "manual",
2448 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002449 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2450 "srcs": `["a_fg.proto"]`,
2451 }),
2452 },
2453 })
2454}
2455
2456func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2457 runCcLibraryTestCase(t, Bp2buildTestCase{
2458 ModuleTypeUnderTest: "cc_library",
2459 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2460 Blueprint: soongCcProtoPreamble + `
2461filegroup {
2462 name: "a_fg_proto",
2463 srcs: ["a_fg.proto"],
2464}
2465
2466cc_library {
2467 name: "a",
2468 srcs: [
2469 ":a_fg_proto",
2470 ],
2471 proto: {
2472 export_proto_headers: true,
2473 },
2474 include_build_directory: false,
2475}`,
2476 ExpectedBazelTargets: []string{
2477 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2478 "deps": `[":a_fg_proto_bp2build_converted"]`,
2479 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2480 "deps": `[":libprotobuf-cpp-lite"]`,
2481 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2482 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2483 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2484 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002485 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002486 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002487 "tags": `[
2488 "apex_available=//apex_available:anyapex",
2489 "manual",
2490 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002491 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2492 "actual": `"//.:a_fg_proto_proto"`,
2493 "tags": `[
2494 "apex_available=//apex_available:anyapex",
2495 "manual",
2496 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002497 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2498 "srcs": `["a_fg.proto"]`,
2499 }),
2500 },
2501 })
2502}
2503
2504func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2505 runCcLibraryTestCase(t, Bp2buildTestCase{
2506 ModuleTypeUnderTest: "cc_library",
2507 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2508 Filesystem: map[string]string{
2509 "path/to/A/Android.bp": `
2510filegroup {
2511 name: "a_fg_proto",
2512 srcs: ["a_fg.proto"],
2513}`,
2514 },
2515 Blueprint: soongCcProtoPreamble + `
2516cc_library {
2517 name: "a",
2518 srcs: [
2519 ":a_fg_proto",
2520 "a.proto",
2521 ],
2522 proto: {
2523 export_proto_headers: true,
2524 },
2525 include_build_directory: false,
2526}`,
2527 ExpectedBazelTargets: []string{
2528 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002529 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002530 "srcs": `["a.proto"]`,
2531 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2532 "deps": `[
2533 "//path/to/A:a_fg_proto_bp2build_converted",
2534 ":a_proto",
2535 ]`,
2536 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2537 "deps": `[":libprotobuf-cpp-lite"]`,
2538 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2539 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2540 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2541 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2542 }),
2543 },
2544 })
2545}
2546
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002547func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002548 runCcLibraryTestCase(t, Bp2buildTestCase{
2549 ModuleTypeUnderTest: "cc_library",
2550 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2551 Blueprint: soongCcProtoPreamble +
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002552 simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
2553 simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
2554 simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
2555 simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
2556cc_library {
2557 name: "a",
2558 srcs: [":a_fg_proto"],
2559 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002560 export_proto_headers: true,
2561 },
2562 include_build_directory: false,
2563}
2564
2565cc_library {
2566 name: "b",
2567 srcs: [":b_protos"],
2568 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002569 export_proto_headers: true,
2570 },
2571 include_build_directory: false,
2572}
2573
2574cc_library {
2575 name: "c",
2576 srcs: [":c-proto-srcs"],
2577 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002578 export_proto_headers: true,
2579 },
2580 include_build_directory: false,
2581}
2582
2583cc_library {
2584 name: "d",
2585 srcs: [":proto-srcs-d"],
2586 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002587 export_proto_headers: true,
2588 },
2589 include_build_directory: false,
2590}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002591 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002592 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002593 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002594 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002595 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002596 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002597 "deps": `[":libprotobuf-cpp-lite"]`,
2598 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2599 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2600 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2601 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002602 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002603 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2604 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2605 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2606 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2607 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002608 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002609 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002610 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002611 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002612 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002613 "deps": `[":libprotobuf-cpp-lite"]`,
2614 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2615 "srcs": `[":b_protos_cpp_srcs"]`,
2616 "srcs_as": `[":b_protos_as_srcs"]`,
2617 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002618 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002619 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2620 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2621 "srcs": `[":b_protos_cpp_srcs"]`,
2622 "srcs_as": `[":b_protos_as_srcs"]`,
2623 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002624 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002625 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002626 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002627 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002628 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002629 "deps": `[":libprotobuf-cpp-lite"]`,
2630 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2631 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2632 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2633 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002634 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002635 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2636 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2637 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2638 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2639 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002640 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002641 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002642 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002643 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002644 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002645 "deps": `[":libprotobuf-cpp-lite"]`,
2646 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2647 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2648 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2649 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002650 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002651 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2652 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2653 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2654 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2655 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2656 }),
2657 },
2658 })
2659}
Chris Parsons58852a02021-12-09 18:10:18 -05002660
2661func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002662 runCcLibraryTestCase(t, Bp2buildTestCase{
2663 ModuleTypeUnderTest: "cc_library",
2664 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2665 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002666 name: "foo",
2667 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002668 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002669 target: {
2670 darwin: {
2671 enabled: false,
2672 },
2673 windows: {
2674 enabled: false,
2675 },
2676 linux_glibc_x86: {
2677 enabled: false,
2678 },
2679 },
2680 include_build_directory: false,
2681}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002682 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002683 "srcs": `["foo.cpp"]`,
2684 "target_compatible_with": `select({
2685 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2686 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2687 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2688 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2689 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2690 "//conditions:default": [],
2691 })`,
2692 }),
2693 })
2694}
2695
2696func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002697 runCcLibraryTestCase(t, Bp2buildTestCase{
2698 ModuleTypeUnderTest: "cc_library",
2699 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2700 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002701 name: "foo",
2702 srcs: ["foo.cpp"],
2703 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002704 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002705 target: {
2706 darwin: {
2707 enabled: true,
2708 },
2709 windows: {
2710 enabled: false,
2711 },
2712 linux_glibc_x86: {
2713 enabled: false,
2714 },
2715 },
2716 include_build_directory: false,
2717}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002718 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002719 "srcs": `["foo.cpp"]`,
2720 "target_compatible_with": `select({
2721 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2722 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2723 "//conditions:default": ["@platforms//:incompatible"],
2724 })`,
2725 }),
2726 })
2727}
2728
2729func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002730 runCcLibraryTestCase(t, Bp2buildTestCase{
2731 ModuleTypeUnderTest: "cc_library",
2732 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2733 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002734 name: "foo",
2735 srcs: ["foo.cpp"],
2736 enabled: false,
2737 shared: {
2738 enabled: true,
2739 },
2740 target: {
2741 android: {
2742 shared: {
2743 enabled: false,
2744 },
2745 }
2746 },
2747 include_build_directory: false,
2748}`,
Alixe06d75b2022-08-31 18:28:19 +00002749 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002750 "srcs": `["foo.cpp"]`,
2751 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002752 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002753 "srcs": `["foo.cpp"]`,
2754 "target_compatible_with": `select({
2755 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2756 "//conditions:default": [],
2757 })`,
2758 }),
2759 },
2760 })
2761}
2762
2763func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002764 runCcLibraryTestCase(t, Bp2buildTestCase{
2765 ModuleTypeUnderTest: "cc_library",
2766 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2767 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002768 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002769 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002770 srcs: ["foo.cpp"],
2771 shared: {
2772 enabled: false
2773 },
2774 target: {
2775 darwin: {
2776 enabled: true,
2777 },
2778 windows: {
2779 enabled: false,
2780 },
2781 linux_glibc_x86: {
2782 shared: {
2783 enabled: true,
2784 },
2785 },
2786 },
2787 include_build_directory: false,
2788}`,
Alixe06d75b2022-08-31 18:28:19 +00002789 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002790 "srcs": `["foo.cpp"]`,
2791 "target_compatible_with": `select({
2792 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2793 "//conditions:default": [],
2794 })`,
Alixe06d75b2022-08-31 18:28:19 +00002795 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002796 "srcs": `["foo.cpp"]`,
2797 "target_compatible_with": `select({
2798 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2799 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2800 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2801 "//conditions:default": ["@platforms//:incompatible"],
2802 })`,
2803 }),
2804 }})
2805}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002806
2807func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002808 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002809 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002810 })
2811 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Sam Delmerico5f906492023-03-15 18:06:18 -04002812 "soname": `"a.so"`,
2813 "source_library_label": `"//foo/bar:a"`,
2814 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002815 "stubs_versions": `[
2816 "28",
2817 "29",
2818 "current",
2819 ]`,
2820 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002821 runCcLibraryTestCase(t, Bp2buildTestCase{
2822 Description: "cc_library stubs",
2823 ModuleTypeUnderTest: "cc_library",
2824 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2825 Dir: "foo/bar",
2826 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002827 "foo/bar/Android.bp": `
2828cc_library {
2829 name: "a",
2830 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2831 bazel_module: { bp2build_available: true },
2832 include_build_directory: false,
2833}
2834`,
2835 },
Wei Li81852ca2022-07-27 00:22:06 -07002836 Blueprint: soongCcLibraryPreamble,
2837 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002838 },
2839 )
2840}
Liz Kammerf38a8372022-02-04 15:39:00 -05002841
Spandan Das4238c652022-09-09 01:38:47 +00002842func TestCcApiContributionsWithHdrs(t *testing.T) {
2843 bp := `
2844 cc_library {
2845 name: "libfoo",
2846 stubs: { symbol_file: "libfoo.map.txt", versions: ["28", "29", "current"] },
2847 llndk: { symbol_file: "libfoo.map.txt", override_export_include_dirs: ["dir2"]},
2848 export_include_dirs: ["dir1"],
2849 }
2850 `
2851 expectedBazelTargets := []string{
2852 MakeBazelTarget(
2853 "cc_api_library_headers",
Spandan Das627fc3e2023-01-26 23:02:00 +00002854 "libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002855 AttrNameToString{
2856 "export_includes": `["dir1"]`,
2857 }),
2858 MakeBazelTarget(
2859 "cc_api_library_headers",
2860 "libfoo.vendorapi.headers",
2861 AttrNameToString{
2862 "export_includes": `["dir2"]`,
2863 }),
2864 MakeBazelTarget(
2865 "cc_api_contribution",
2866 "libfoo.contribution",
2867 AttrNameToString{
2868 "api": `"libfoo.map.txt"`,
2869 "library_name": `"libfoo"`,
2870 "api_surfaces": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002871 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002872 "vendorapi",
2873 ]`,
2874 "hdrs": `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002875 ":libfoo.module-libapi.headers",
Spandan Das4238c652022-09-09 01:38:47 +00002876 ":libfoo.vendorapi.headers",
2877 ]`,
2878 }),
2879 }
2880 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2881 Blueprint: bp,
Spandan Das627fc3e2023-01-26 23:02:00 +00002882 Description: "cc API contributions to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002883 ExpectedBazelTargets: expectedBazelTargets,
2884 })
2885}
2886
2887func TestCcApiSurfaceCombinations(t *testing.T) {
2888 testCases := []struct {
2889 bp string
2890 expectedApi string
2891 expectedApiSurfaces string
2892 description string
2893 }{
2894 {
2895 bp: `
2896 cc_library {
2897 name: "a",
2898 stubs: {symbol_file: "a.map.txt"},
2899 }`,
2900 expectedApi: `"a.map.txt"`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002901 expectedApiSurfaces: `["module-libapi"]`,
2902 description: "Library that contributes to module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002903 },
2904 {
2905 bp: `
2906 cc_library {
2907 name: "a",
2908 llndk: {symbol_file: "a.map.txt"},
2909 }`,
2910 expectedApi: `"a.map.txt"`,
2911 expectedApiSurfaces: `["vendorapi"]`,
2912 description: "Library that contributes to vendorapi",
2913 },
2914 {
2915 bp: `
2916 cc_library {
2917 name: "a",
2918 llndk: {symbol_file: "a.map.txt"},
2919 stubs: {symbol_file: "a.map.txt"},
2920 }`,
2921 expectedApi: `"a.map.txt"`,
2922 expectedApiSurfaces: `[
Spandan Das627fc3e2023-01-26 23:02:00 +00002923 "module-libapi",
Spandan Das4238c652022-09-09 01:38:47 +00002924 "vendorapi",
2925 ]`,
Spandan Das627fc3e2023-01-26 23:02:00 +00002926 description: "Library that contributes to module-libapi and vendorapi",
Spandan Das4238c652022-09-09 01:38:47 +00002927 },
2928 }
2929 for _, testCase := range testCases {
2930 expectedBazelTargets := []string{
2931 MakeBazelTarget(
2932 "cc_api_contribution",
2933 "a.contribution",
2934 AttrNameToString{
2935 "library_name": `"a"`,
2936 "hdrs": `[]`,
2937 "api": testCase.expectedApi,
2938 "api_surfaces": testCase.expectedApiSurfaces,
2939 },
2940 ),
2941 }
2942 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
2943 Blueprint: testCase.bp,
2944 Description: testCase.description,
2945 ExpectedBazelTargets: expectedBazelTargets,
2946 })
2947 }
2948}
2949
2950// llndk struct property in Soong provides users with several options to configure the exported include dirs
2951// Test the generated bazel targets for the different configurations
2952func TestCcVendorApiHeaders(t *testing.T) {
2953 testCases := []struct {
2954 bp string
2955 expectedIncludes string
2956 expectedSystemIncludes string
2957 description string
2958 }{
2959 {
2960 bp: `
2961 cc_library {
2962 name: "a",
2963 export_include_dirs: ["include"],
2964 export_system_include_dirs: ["base_system_include"],
2965 llndk: {
2966 symbol_file: "a.map.txt",
2967 export_headers_as_system: true,
2968 },
2969 }
2970 `,
2971 expectedIncludes: "",
2972 expectedSystemIncludes: `[
2973 "base_system_include",
2974 "include",
2975 ]`,
2976 description: "Headers are exported as system to API surface",
2977 },
2978 {
2979 bp: `
2980 cc_library {
2981 name: "a",
2982 export_include_dirs: ["include"],
2983 export_system_include_dirs: ["base_system_include"],
2984 llndk: {
2985 symbol_file: "a.map.txt",
2986 override_export_include_dirs: ["llndk_include"],
2987 },
2988 }
2989 `,
2990 expectedIncludes: `["llndk_include"]`,
2991 expectedSystemIncludes: `["base_system_include"]`,
2992 description: "Non-system Headers are ovverriden before export to API surface",
2993 },
2994 {
2995 bp: `
2996 cc_library {
2997 name: "a",
2998 export_include_dirs: ["include"],
2999 export_system_include_dirs: ["base_system_include"],
3000 llndk: {
3001 symbol_file: "a.map.txt",
3002 override_export_include_dirs: ["llndk_include"],
3003 export_headers_as_system: true,
3004 },
3005 }
3006 `,
3007 expectedIncludes: "", // includes are set to nil
3008 expectedSystemIncludes: `[
3009 "base_system_include",
3010 "llndk_include",
3011 ]`,
3012 description: "System Headers are extended before export to API surface",
3013 },
3014 }
3015 for _, testCase := range testCases {
3016 attrs := AttrNameToString{}
3017 if testCase.expectedIncludes != "" {
3018 attrs["export_includes"] = testCase.expectedIncludes
3019 }
3020 if testCase.expectedSystemIncludes != "" {
3021 attrs["export_system_includes"] = testCase.expectedSystemIncludes
3022 }
3023
3024 expectedBazelTargets := []string{
3025 MakeBazelTarget("cc_api_library_headers", "a.vendorapi.headers", attrs),
3026 // Create a target for cc_api_contribution target
3027 MakeBazelTarget("cc_api_contribution", "a.contribution", AttrNameToString{
3028 "api": `"a.map.txt"`,
3029 "api_surfaces": `["vendorapi"]`,
3030 "hdrs": `[":a.vendorapi.headers"]`,
3031 "library_name": `"a"`,
3032 }),
3033 }
3034 RunApiBp2BuildTestCase(t, cc.RegisterLibraryBuildComponents, Bp2buildTestCase{
3035 Blueprint: testCase.bp,
3036 ExpectedBazelTargets: expectedBazelTargets,
3037 })
3038 }
3039}
3040
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003041func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
3042 runCcLibraryTestCase(t, Bp2buildTestCase{
3043 Description: "stub target generation of the same lib across configs should not result in duplicates",
3044 ModuleTypeUnderTest: "cc_library",
3045 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3046 Filesystem: map[string]string{
3047 "bar.map.txt": "",
3048 },
3049 Blueprint: `
3050cc_library {
3051 name: "barlib",
3052 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3053 bazel_module: { bp2build_available: false },
3054}
3055cc_library {
3056 name: "foolib",
3057 shared_libs: ["barlib"],
3058 target: {
3059 android: {
3060 shared_libs: ["barlib"],
3061 },
3062 },
3063 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003064 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003065}`,
3066 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3067 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00003068 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
3069 "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003070 "//conditions:default": [":barlib"],
3071 })`,
3072 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003073 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00003074 }),
3075 })
3076}
3077
Liz Kammerffc17e42022-11-23 09:42:05 -05003078func TestCcLibraryExcludesLibsHost(t *testing.T) {
3079 runCcLibraryTestCase(t, Bp2buildTestCase{
3080 ModuleTypeUnderTest: "cc_library",
3081 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3082 Filesystem: map[string]string{
3083 "bar.map.txt": "",
3084 },
3085 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "bazlib") + `
3086cc_library {
3087 name: "quxlib",
3088 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
3089 bazel_module: { bp2build_available: false },
3090}
3091cc_library {
3092 name: "barlib",
3093 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
3094 bazel_module: { bp2build_available: false },
3095}
3096cc_library {
3097 name: "foolib",
3098 shared_libs: ["barlib", "quxlib"],
3099 target: {
3100 host: {
3101 shared_libs: ["bazlib"],
3102 exclude_shared_libs: ["barlib"],
3103 },
3104 },
3105 include_build_directory: false,
3106 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003107 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003108}`,
3109 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
3110 "implementation_dynamic_deps": `select({
3111 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003112 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003113 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003114 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
3115 "//build/bazel/platforms/os:windows": [":bazlib"],
3116 "//conditions:default": [],
3117 }) + select({
3118 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003119 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08003120 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05003121 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
3122 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00003123 "//build/bazel/rules/apex:foo": [
3124 "@api_surfaces//module-libapi/current:barlib",
3125 "@api_surfaces//module-libapi/current:quxlib",
3126 ],
3127 "//build/bazel/rules/apex:system": [
Spandan Das2518c022023-03-17 03:02:32 +00003128 "@api_surfaces//module-libapi/current:barlib",
3129 "@api_surfaces//module-libapi/current:quxlib",
Liz Kammerffc17e42022-11-23 09:42:05 -05003130 ],
3131 "//conditions:default": [
3132 ":barlib",
3133 ":quxlib",
3134 ],
3135 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003136 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05003137 }),
3138 })
3139}
3140
Liz Kammerf38a8372022-02-04 15:39:00 -05003141func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003142 runCcLibraryTestCase(t, Bp2buildTestCase{
3143 ModuleTypeUnderTest: "cc_library",
3144 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3145 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05003146 name: "foo",
3147 ldflags: ["-Wl,--rpath,${ORIGIN}"],
3148 include_build_directory: false,
3149}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003150 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05003151 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
3152 }),
3153 })
3154}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003155
3156func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003157 runCcLibraryTestCase(t, Bp2buildTestCase{
3158 ModuleTypeUnderTest: "cc_library",
3159 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3160 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003161 "foo.c": "",
3162 "bar.cc": "",
3163 "foo1.l": "",
3164 "bar1.ll": "",
3165 "foo2.l": "",
3166 "bar2.ll": "",
3167 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003168 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003169 name: "foo_lib",
3170 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
3171 lex: { flags: ["--foo_flags"] },
3172 include_build_directory: false,
3173 bazel_module: { bp2build_available: true },
3174}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003175 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00003176 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003177 "srcs": `[
3178 "foo1.l",
3179 "foo2.l",
3180 ]`,
3181 "lexopts": `["--foo_flags"]`,
3182 }),
Alixe06d75b2022-08-31 18:28:19 +00003183 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003184 "srcs": `[
3185 "bar1.ll",
3186 "bar2.ll",
3187 ]`,
3188 "lexopts": `["--foo_flags"]`,
3189 }),
3190 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003191 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003192 "srcs": `[
3193 "bar.cc",
3194 ":foo_lib_genlex_ll",
3195 ]`,
3196 "srcs_c": `[
3197 "foo.c",
3198 ":foo_lib_genlex_l",
3199 ]`,
3200 })...),
3201 })
3202}
Cole Faust6b29f592022-08-09 09:50:56 -07003203
3204func TestCCLibraryRuntimeDeps(t *testing.T) {
3205 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3206 Blueprint: `cc_library_shared {
3207 name: "bar",
3208}
3209
3210cc_library {
3211 name: "foo",
3212 runtime_libs: ["foo"],
3213}`,
3214 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003215 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003216 "local_includes": `["."]`,
3217 }),
Alixe06d75b2022-08-31 18:28:19 +00003218 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003219 "runtime_deps": `[":foo"]`,
3220 "local_includes": `["."]`,
3221 }),
Alixe06d75b2022-08-31 18:28:19 +00003222 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003223 "runtime_deps": `[":foo"]`,
3224 "local_includes": `["."]`,
3225 }),
3226 },
3227 })
3228}
Cole Faust5fa4e962022-08-22 14:31:04 -07003229
3230func TestCcLibraryWithInstructionSet(t *testing.T) {
3231 runCcLibraryTestCase(t, Bp2buildTestCase{
3232 ModuleTypeUnderTest: "cc_library",
3233 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3234 Blueprint: `cc_library {
3235 name: "foo",
3236 arch: {
3237 arm: {
3238 instruction_set: "arm",
3239 }
3240 }
3241}
3242`,
3243 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3244 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003245 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003246 "//conditions:default": [],
3247 })`,
3248 "local_includes": `["."]`,
3249 }),
3250 })
3251}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003252
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003253func TestCcLibraryEmptySuffix(t *testing.T) {
3254 runCcLibraryTestCase(t, Bp2buildTestCase{
3255 Description: "cc_library with empty suffix",
3256 ModuleTypeUnderTest: "cc_library",
3257 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3258 Filesystem: map[string]string{
3259 "foo.c": "",
3260 },
3261 Blueprint: `cc_library {
3262 name: "foo",
3263 suffix: "",
3264 srcs: ["foo.c"],
3265 include_build_directory: false,
3266}`,
3267 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003268 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 -05003269 "srcs_c": `["foo.c"]`,
3270 }),
Alixe06d75b2022-08-31 18:28:19 +00003271 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003272 "srcs_c": `["foo.c"]`,
3273 "suffix": `""`,
3274 }),
3275 },
3276 })
3277}
3278
3279func TestCcLibrarySuffix(t *testing.T) {
3280 runCcLibraryTestCase(t, Bp2buildTestCase{
3281 Description: "cc_library with suffix",
3282 ModuleTypeUnderTest: "cc_library",
3283 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3284 Filesystem: map[string]string{
3285 "foo.c": "",
3286 },
3287 Blueprint: `cc_library {
3288 name: "foo",
3289 suffix: "-suf",
3290 srcs: ["foo.c"],
3291 include_build_directory: false,
3292}`,
3293 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003294 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 -05003295 "srcs_c": `["foo.c"]`,
3296 }),
Alixe06d75b2022-08-31 18:28:19 +00003297 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003298 "srcs_c": `["foo.c"]`,
3299 "suffix": `"-suf"`,
3300 }),
3301 },
3302 })
3303}
3304
3305func TestCcLibraryArchVariantSuffix(t *testing.T) {
3306 runCcLibraryTestCase(t, Bp2buildTestCase{
3307 Description: "cc_library with arch-variant suffix",
3308 ModuleTypeUnderTest: "cc_library",
3309 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3310 Filesystem: map[string]string{
3311 "foo.c": "",
3312 },
3313 Blueprint: `cc_library {
3314 name: "foo",
3315 arch: {
3316 arm64: { suffix: "-64" },
3317 arm: { suffix: "-32" },
3318 },
3319 srcs: ["foo.c"],
3320 include_build_directory: false,
3321}`,
3322 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003323 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 -05003324 "srcs_c": `["foo.c"]`,
3325 }),
Alixe06d75b2022-08-31 18:28:19 +00003326 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003327 "srcs_c": `["foo.c"]`,
3328 "suffix": `select({
3329 "//build/bazel/platforms/arch:arm": "-32",
3330 "//build/bazel/platforms/arch:arm64": "-64",
3331 "//conditions:default": None,
3332 })`,
3333 }),
3334 },
3335 })
3336}
3337
Vinh Tran367d89d2023-04-28 11:21:25 -04003338func TestCcLibraryWithAidlLibrary(t *testing.T) {
3339 runCcLibraryTestCase(t, Bp2buildTestCase{
3340 Description: "cc_library with aidl_library",
3341 ModuleTypeUnderTest: "cc_library",
3342 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3343 Blueprint: `
3344aidl_library {
3345 name: "A_aidl",
3346 srcs: ["aidl/A.aidl"],
3347 hdrs: ["aidl/Header.aidl"],
3348 strip_import_prefix: "aidl",
3349}
3350cc_library {
3351 name: "foo",
3352 aidl: {
3353 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003354 },
3355 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003356}`,
3357 ExpectedBazelTargets: []string{
3358 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3359 "srcs": `["aidl/A.aidl"]`,
3360 "hdrs": `["aidl/Header.aidl"]`,
3361 "strip_import_prefix": `"aidl"`,
3362 "tags": `["apex_available=//apex_available:anyapex"]`,
3363 }),
3364 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003365 "deps": `[":A_aidl"]`,
3366 "local_includes": `["."]`,
3367 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003368 }),
3369 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3370 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3371 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003372 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003373 }),
3374 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3375 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3376 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003377 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003378 }),
3379 },
3380 })
3381}
3382
Vinh Tran9f6796a2022-08-16 13:10:31 -04003383func TestCcLibraryWithAidlSrcs(t *testing.T) {
3384 runCcLibraryTestCase(t, Bp2buildTestCase{
3385 Description: "cc_library with aidl srcs",
3386 ModuleTypeUnderTest: "cc_library",
3387 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3388 Blueprint: `
3389filegroup {
3390 name: "A_aidl",
3391 srcs: ["aidl/A.aidl"],
3392 path: "aidl",
3393}
3394cc_library {
3395 name: "foo",
3396 srcs: [
3397 ":A_aidl",
3398 "B.aidl",
3399 ],
3400}`,
3401 ExpectedBazelTargets: []string{
3402 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3403 "srcs": `["aidl/A.aidl"]`,
3404 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003405 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003406 }),
Alixe06d75b2022-08-31 18:28:19 +00003407 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003408 "srcs": `["B.aidl"]`,
3409 }),
Alixe06d75b2022-08-31 18:28:19 +00003410 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003411 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003412 "deps": `[
3413 ":A_aidl",
3414 ":foo_aidl_library",
3415 ]`,
3416 }),
Alixe06d75b2022-08-31 18:28:19 +00003417 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003418 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3419 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003420 }),
Alixe06d75b2022-08-31 18:28:19 +00003421 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003422 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3423 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003424 }),
3425 },
3426 })
3427}
3428
3429func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3430 runCcLibraryTestCase(t, Bp2buildTestCase{
3431 Description: "cc_library with non aidl filegroup",
3432 ModuleTypeUnderTest: "cc_library",
3433 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3434 Filesystem: map[string]string{
3435 "path/to/A/Android.bp": `
3436filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003437 name: "A_aidl",
3438 srcs: ["aidl/A.aidl"],
3439 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003440}`,
3441 },
3442 Blueprint: `
3443cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003444 name: "foo",
3445 srcs: [
3446 ":A_aidl",
3447 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003448}`,
3449 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003450 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003451 "local_includes": `["."]`,
3452 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003453 }),
Alixe06d75b2022-08-31 18:28:19 +00003454 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003455 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3456 "local_includes": `["."]`,
3457 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003458 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003459 "local_includes": `["."]`,
3460 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003461 }),
3462 },
3463 })
3464}
3465
3466func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003467 t.Parallel()
3468
3469 expectedBazelTargets := []string{
3470 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003471 "local_includes": `["."]`,
3472 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003473 }),
3474 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3475 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3476 "local_includes": `["."]`,
3477 }),
3478 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3479 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3480 "local_includes": `["."]`,
3481 }),
3482 }
3483 testCases := []struct {
3484 description string
3485 bp string
3486 expectedBazelTargets []string
3487 }{
3488 {
3489 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3490 bp: `
3491 cc_library {
3492 name: "foo",
3493 srcs: [
3494 "Foo.aidl",
3495 ],
3496 aidl: {
3497 export_aidl_headers: true,
3498 }
3499 }`,
3500 expectedBazelTargets: append(
3501 expectedBazelTargets,
3502 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3503 "srcs": `["Foo.aidl"]`,
3504 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003505 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003506 {
3507 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3508 bp: `
3509 aidl_library {
3510 name: "foo_aidl_library",
3511 srcs: ["Foo.aidl"],
3512 }
3513 cc_library {
3514 name: "foo",
3515 aidl: {
3516 libs: ["foo_aidl_library"],
3517 export_aidl_headers: true,
3518 }
3519 }`,
3520 expectedBazelTargets: append(
3521 expectedBazelTargets,
3522 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3523 "srcs": `["Foo.aidl"]`,
3524 "tags": `["apex_available=//apex_available:anyapex"]`,
3525 }),
3526 ),
3527 },
3528 }
3529
3530 for _, testCase := range testCases {
3531 runCcLibraryTestCase(t, Bp2buildTestCase{
3532 Description: "cc_library with export aidl headers",
3533 ModuleTypeUnderTest: "cc_library",
3534 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3535 Blueprint: testCase.bp,
3536 ExpectedBazelTargets: testCase.expectedBazelTargets,
3537 })
3538 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003539}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003540
3541func TestCcLibraryWithTargetApex(t *testing.T) {
3542 runCcLibraryTestCase(t, Bp2buildTestCase{
3543 Description: "cc_library with target.apex",
3544 ModuleTypeUnderTest: "cc_library",
3545 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3546 Blueprint: `
3547cc_library {
3548 name: "foo",
3549 shared_libs: ["bar", "baz"],
3550 static_libs: ["baz", "buh"],
3551 target: {
3552 apex: {
3553 exclude_shared_libs: ["bar"],
3554 exclude_static_libs: ["buh"],
3555 }
3556 }
3557}`,
3558 ExpectedBazelTargets: []string{
3559 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3560 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003561 "//build/bazel/rules/apex:in_apex": [],
3562 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003563 })`,
3564 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003565 "//build/bazel/rules/apex:in_apex": [],
3566 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003567 })`,
3568 "local_includes": `["."]`,
3569 }),
3570 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3571 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003572 "//build/bazel/rules/apex:in_apex": [],
3573 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003574 })`,
3575 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003576 "//build/bazel/rules/apex:in_apex": [],
3577 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003578 })`,
3579 "local_includes": `["."]`,
3580 }),
3581 },
3582 })
3583}
3584
3585func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3586 runCcLibraryTestCase(t, Bp2buildTestCase{
3587 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3588 ModuleTypeUnderTest: "cc_library",
3589 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3590 Blueprint: `
3591cc_library_static {
3592 name: "foo",
3593 shared_libs: ["bar", "baz"],
3594 static_libs: ["abc"],
3595 export_shared_lib_headers: ["baz"],
3596 export_static_lib_headers: ["abc"],
3597 target: {
3598 apex: {
3599 exclude_shared_libs: ["baz", "bar"],
3600 exclude_static_libs: ["abc"],
3601 }
3602 }
3603}`,
3604 ExpectedBazelTargets: []string{
3605 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3606 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003607 "//build/bazel/rules/apex:in_apex": [],
3608 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003609 })`,
3610 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003611 "//build/bazel/rules/apex:in_apex": [],
3612 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003613 })`,
3614 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003615 "//build/bazel/rules/apex:in_apex": [],
3616 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003617 })`,
3618 "local_includes": `["."]`,
3619 }),
3620 },
3621 })
3622}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003623
3624func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3625 runCcLibraryTestCase(t, Bp2buildTestCase{
3626 Description: "cc_library with sysprop sources",
3627 ModuleTypeUnderTest: "cc_library",
3628 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3629 Blueprint: `
3630cc_library {
3631 name: "foo",
3632 srcs: [
3633 "bar.sysprop",
3634 "baz.sysprop",
3635 "blah.cpp",
3636 ],
3637 min_sdk_version: "5",
3638}`,
3639 ExpectedBazelTargets: []string{
3640 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3641 "srcs": `[
3642 "bar.sysprop",
3643 "baz.sysprop",
3644 ]`,
3645 }),
3646 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3647 "dep": `":foo_sysprop_library"`,
3648 "min_sdk_version": `"5"`,
3649 }),
3650 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3651 "srcs": `["blah.cpp"]`,
3652 "local_includes": `["."]`,
3653 "min_sdk_version": `"5"`,
3654 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3655 }),
3656 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3657 "srcs": `["blah.cpp"]`,
3658 "local_includes": `["."]`,
3659 "min_sdk_version": `"5"`,
3660 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3661 }),
3662 },
3663 })
3664}
3665
3666func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3667 runCcLibraryTestCase(t, Bp2buildTestCase{
3668 Description: "cc_library with sysprop sources in some configs but not others",
3669 ModuleTypeUnderTest: "cc_library",
3670 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3671 Blueprint: `
3672cc_library {
3673 name: "foo",
3674 host_supported: true,
3675 srcs: [
3676 "blah.cpp",
3677 ],
3678 target: {
3679 android: {
3680 srcs: ["bar.sysprop"],
3681 },
3682 },
3683 min_sdk_version: "5",
3684}`,
3685 ExpectedBazelTargets: []string{
3686 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3687 "srcs": `select({
3688 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3689 "//conditions:default": [],
3690 })`,
3691 }),
3692 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3693 "dep": `":foo_sysprop_library"`,
3694 "min_sdk_version": `"5"`,
3695 }),
3696 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3697 "srcs": `["blah.cpp"]`,
3698 "local_includes": `["."]`,
3699 "min_sdk_version": `"5"`,
3700 "whole_archive_deps": `select({
3701 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3702 "//conditions:default": [],
3703 })`,
3704 }),
3705 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3706 "srcs": `["blah.cpp"]`,
3707 "local_includes": `["."]`,
3708 "min_sdk_version": `"5"`,
3709 "whole_archive_deps": `select({
3710 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3711 "//conditions:default": [],
3712 })`,
3713 }),
3714 },
3715 })
3716}
Vinh Tran395a1e92022-09-16 18:27:29 -04003717
Sam Delmerico512437b2023-03-17 11:34:15 -04003718func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003719 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003720 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003721 ModuleTypeUnderTest: "cc_library",
3722 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3723 Blueprint: `
3724cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003725 name: "foo",
3726 srcs: [
3727 "Foo.aidl",
3728 ],
3729 static_libs: [
3730 "bar-static",
3731 "baz-static",
3732 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003733 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003734 "bar-shared",
3735 "baz-shared",
3736 ],
3737 export_static_lib_headers: [
3738 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003739 ],
3740 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003741 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003742 ],
3743}` +
Sam Delmerico512437b2023-03-17 11:34:15 -04003744 simpleModuleDoNotConvertBp2build("cc_library_static", "bar-static") +
3745 simpleModuleDoNotConvertBp2build("cc_library_static", "baz-static") +
3746 simpleModuleDoNotConvertBp2build("cc_library", "bar-shared") +
3747 simpleModuleDoNotConvertBp2build("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003748 ExpectedBazelTargets: []string{
3749 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3750 "srcs": `["Foo.aidl"]`,
3751 }),
3752 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003753 "local_includes": `["."]`,
3754 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003755 "implementation_deps": `[
3756 ":baz-static",
3757 ":bar-static",
3758 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003759 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003760 ":baz-shared",
3761 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003762 ]`,
3763 }),
3764 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3765 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003766 "deps": `[":baz-static"]`,
3767 "implementation_deps": `[":bar-static"]`,
3768 "dynamic_deps": `[":baz-shared"]`,
3769 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003770 "local_includes": `["."]`,
3771 }),
3772 },
3773 })
3774}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003775
3776func TestCcLibraryWithTidy(t *testing.T) {
3777 runCcLibraryTestCase(t, Bp2buildTestCase{
3778 Description: "cc_library uses tidy properties",
3779 ModuleTypeUnderTest: "cc_library",
3780 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3781 Blueprint: `
3782cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003783 name: "foo",
3784 srcs: ["foo.cpp"],
3785}
3786cc_library_static {
3787 name: "foo-no-tidy",
3788 srcs: ["foo.cpp"],
3789 tidy: false,
3790}
3791cc_library_static {
3792 name: "foo-tidy",
3793 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003794 tidy: true,
3795 tidy_checks: ["check1", "check2"],
3796 tidy_checks_as_errors: ["check1error", "check2error"],
3797 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003798 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003799}`,
3800 ExpectedBazelTargets: []string{
3801 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3802 "local_includes": `["."]`,
3803 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003804 }),
3805 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3806 "local_includes": `["."]`,
3807 "srcs": `["foo.cpp"]`,
3808 "tidy": `"never"`,
3809 }),
3810 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3811 "local_includes": `["."]`,
3812 "srcs": `["foo.cpp"]`,
3813 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003814 "tidy_checks": `[
3815 "check1",
3816 "check2",
3817 ]`,
3818 "tidy_checks_as_errors": `[
3819 "check1error",
3820 "check2error",
3821 ]`,
3822 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003823 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003824 }),
3825 },
3826 })
3827}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003828
Vinh Tran99270ea2022-11-28 11:15:23 -05003829func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3830 bp := `
3831cc_library {
3832 name: "foo",
3833 afdo: true,
3834 include_build_directory: false,
3835}`
3836
3837 // TODO(b/260714900): Add test case for arch-specific afdo profile
3838 testCases := []struct {
3839 description string
3840 filesystem map[string]string
3841 expectedBazelTargets []string
3842 }{
3843 {
3844 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003845 filesystem: map[string]string{
3846 "vendor/google_data/pgo_profile/sampling/BUILD": "",
3847 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3848 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003849 expectedBazelTargets: []string{
3850 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3851 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3852 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3853 }),
3854 },
3855 },
3856 {
3857 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003858 filesystem: map[string]string{
3859 "toolchain/pgo-profiles/sampling/BUILD": "",
3860 "toolchain/pgo-profiles/sampling/foo.afdo": "",
3861 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003862 expectedBazelTargets: []string{
3863 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3864 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3865 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3866 }),
3867 },
3868 },
3869 {
3870 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003871 filesystem: map[string]string{
3872 "toolchain/pgo-profiles/sampling/BUILD": "",
3873 "toolchain/pgo-profiles/sampling/bar.afdo": "",
3874 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003875 expectedBazelTargets: []string{
3876 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3877 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3878 },
3879 },
3880 {
3881 description: "cc_library with afdo enabled but profile doesn't exist",
3882 expectedBazelTargets: []string{
3883 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3884 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3885 },
3886 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003887 {
3888 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3889 filesystem: map[string]string{
3890 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3891 },
3892 expectedBazelTargets: []string{
3893 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3894 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3895 },
3896 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003897 }
3898 for _, testCase := range testCases {
3899 t.Run(testCase.description, func(t *testing.T) {
3900 runCcLibraryTestCase(t, Bp2buildTestCase{
3901 ExpectedBazelTargets: testCase.expectedBazelTargets,
3902 ModuleTypeUnderTest: "cc_library",
3903 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3904 Description: testCase.description,
3905 Blueprint: binaryReplacer.Replace(bp),
3906 Filesystem: testCase.filesystem,
3907 })
3908 })
3909 }
3910}
3911
Yu Liu56ccb1a2022-11-12 10:47:07 -08003912func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3913 runCcLibraryTestCase(t, Bp2buildTestCase{
3914 Description: "cc_library with header abi checker",
3915 ModuleTypeUnderTest: "cc_library",
3916 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3917 Blueprint: `cc_library {
3918 name: "foo",
3919 header_abi_checker: {
3920 enabled: true,
3921 symbol_file: "a.map.txt",
3922 exclude_symbol_versions: [
3923 "29",
3924 "30",
3925 ],
3926 exclude_symbol_tags: [
3927 "tag1",
3928 "tag2",
3929 ],
3930 check_all_apis: true,
3931 diff_flags: ["-allow-adding-removing-weak-symbols"],
3932 },
3933 include_build_directory: false,
3934}`,
3935 ExpectedBazelTargets: []string{
3936 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3937 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3938 "abi_checker_enabled": `True`,
3939 "abi_checker_symbol_file": `"a.map.txt"`,
3940 "abi_checker_exclude_symbol_versions": `[
3941 "29",
3942 "30",
3943 ]`,
3944 "abi_checker_exclude_symbol_tags": `[
3945 "tag1",
3946 "tag2",
3947 ]`,
3948 "abi_checker_check_all_apis": `True`,
3949 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3950 }),
3951 },
3952 })
3953}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003954
3955func TestCcLibraryApexAvailable(t *testing.T) {
3956 runCcLibraryTestCase(t, Bp2buildTestCase{
3957 Description: "cc_library apex_available converted to 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"],
3965}
3966`,
3967 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3968 "tags": `["apex_available=com.android.foo"]`,
3969 "srcs": `["a.cpp"]`,
3970 "local_includes": `["."]`,
3971 }),
3972 },
3973 )
3974}
3975
3976func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3977 runCcLibraryTestCase(t, Bp2buildTestCase{
3978 Description: "cc_library apex_available converted to multiple tags",
3979 ModuleTypeUnderTest: "cc_library",
3980 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3981 Blueprint: soongCcLibraryPreamble + `
3982cc_library {
3983 name: "a",
3984 srcs: ["a.cpp"],
3985 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3986}
3987`,
3988 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3989 "tags": `[
3990 "apex_available=com.android.foo",
3991 "apex_available=//apex_available:platform",
3992 "apex_available=com.android.bar",
3993 ]`,
3994 "srcs": `["a.cpp"]`,
3995 "local_includes": `["."]`,
3996 }),
3997 },
3998 )
3999}
Zi Wang0f828442022-12-28 11:18:11 -08004000
4001// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
4002// In bp2build output, variant info(select) should go before general info.
4003// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
4004func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
4005 runCcLibraryTestCase(t, Bp2buildTestCase{
4006 Description: "cc_library variant prepend properties order",
4007 ModuleTypeUnderTest: "cc_library",
4008 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4009 Blueprint: soongCcLibraryPreamble + `
4010cc_library {
4011 name: "a",
4012 srcs: ["a.cpp"],
4013 export_include_dirs: ["eid1", "eid2"],
4014 export_system_include_dirs: ["esid1", "esid2"],
4015 target: {
4016 android: {
4017 export_include_dirs: ["android_eid1", "android_eid2"],
4018 export_system_include_dirs: ["android_esid1", "android_esid2"],
4019 },
4020 android_arm: {
4021 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
4022 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
4023 },
4024 linux: {
4025 export_include_dirs: ["linux_eid1", "linux_eid2"],
4026 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
4027 },
4028 },
4029 multilib: {
4030 lib32: {
4031 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
4032 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
4033 },
4034 },
4035 arch: {
4036 arm: {
4037 export_include_dirs: ["arm_eid1", "arm_eid2"],
4038 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
4039 },
4040 }
4041}
4042`,
4043 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4044 "export_includes": `select({
4045 "//build/bazel/platforms/os_arch:android_arm": [
4046 "android_arm_eid1",
4047 "android_arm_eid2",
4048 ],
4049 "//conditions:default": [],
4050 }) + select({
4051 "//build/bazel/platforms/os:android": [
4052 "android_eid1",
4053 "android_eid2",
4054 "linux_eid1",
4055 "linux_eid2",
4056 ],
4057 "//build/bazel/platforms/os:linux_bionic": [
4058 "linux_eid1",
4059 "linux_eid2",
4060 ],
4061 "//build/bazel/platforms/os:linux_glibc": [
4062 "linux_eid1",
4063 "linux_eid2",
4064 ],
4065 "//build/bazel/platforms/os:linux_musl": [
4066 "linux_eid1",
4067 "linux_eid2",
4068 ],
4069 "//conditions:default": [],
4070 }) + select({
4071 "//build/bazel/platforms/arch:arm": [
4072 "lib32_eid1",
4073 "lib32_eid2",
4074 "arm_eid1",
4075 "arm_eid2",
4076 ],
4077 "//build/bazel/platforms/arch:x86": [
4078 "lib32_eid1",
4079 "lib32_eid2",
4080 ],
4081 "//conditions:default": [],
4082 }) + [
4083 "eid1",
4084 "eid2",
4085 ]`,
4086 "export_system_includes": `select({
4087 "//build/bazel/platforms/os_arch:android_arm": [
4088 "android_arm_esid1",
4089 "android_arm_esid2",
4090 ],
4091 "//conditions:default": [],
4092 }) + select({
4093 "//build/bazel/platforms/os:android": [
4094 "android_esid1",
4095 "android_esid2",
4096 "linux_esid1",
4097 "linux_esid2",
4098 ],
4099 "//build/bazel/platforms/os:linux_bionic": [
4100 "linux_esid1",
4101 "linux_esid2",
4102 ],
4103 "//build/bazel/platforms/os:linux_glibc": [
4104 "linux_esid1",
4105 "linux_esid2",
4106 ],
4107 "//build/bazel/platforms/os:linux_musl": [
4108 "linux_esid1",
4109 "linux_esid2",
4110 ],
4111 "//conditions:default": [],
4112 }) + select({
4113 "//build/bazel/platforms/arch:arm": [
4114 "lib32_esid1",
4115 "lib32_esid2",
4116 "arm_esid1",
4117 "arm_esid2",
4118 ],
4119 "//build/bazel/platforms/arch:x86": [
4120 "lib32_esid1",
4121 "lib32_esid2",
4122 ],
4123 "//conditions:default": [],
4124 }) + [
4125 "esid1",
4126 "esid2",
4127 ]`,
4128 "srcs": `["a.cpp"]`,
4129 "local_includes": `["."]`,
4130 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
4131 }),
4132 },
4133 )
4134}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004135
4136func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
4137 runCcLibraryTestCase(t, Bp2buildTestCase{
4138 Description: "cc_library has correct features when integer_overflow property is provided",
4139 ModuleTypeUnderTest: "cc_library",
4140 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4141 Blueprint: `
4142cc_library {
4143 name: "foo",
4144 sanitize: {
4145 integer_overflow: true,
4146 },
4147}
4148`,
4149 ExpectedBazelTargets: []string{
4150 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4151 "features": `["ubsan_integer_overflow"]`,
4152 "local_includes": `["."]`,
4153 }),
4154 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4155 "features": `["ubsan_integer_overflow"]`,
4156 "local_includes": `["."]`,
4157 }),
4158 },
4159 })
4160}
4161
4162func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
4163 runCcLibraryTestCase(t, Bp2buildTestCase{
4164 Description: "cc_library has correct features when misc_undefined property is provided",
4165 ModuleTypeUnderTest: "cc_library",
4166 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4167 Blueprint: `
4168cc_library {
4169 name: "foo",
4170 sanitize: {
4171 misc_undefined: ["undefined", "nullability"],
4172 },
4173}
4174`,
4175 ExpectedBazelTargets: []string{
4176 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4177 "features": `[
4178 "ubsan_undefined",
4179 "ubsan_nullability",
4180 ]`,
4181 "local_includes": `["."]`,
4182 }),
4183 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4184 "features": `[
4185 "ubsan_undefined",
4186 "ubsan_nullability",
4187 ]`,
4188 "local_includes": `["."]`,
4189 }),
4190 },
4191 })
4192}
4193
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004194func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
4195 runCcLibraryTestCase(t, Bp2buildTestCase{
4196 Description: "cc_library has correct feature when sanitize.blocklist is provided",
4197 ModuleTypeUnderTest: "cc_library",
4198 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4199 Blueprint: `
4200cc_library {
4201 name: "foo",
4202 sanitize: {
4203 blocklist: "foo_blocklist.txt",
4204 },
4205}
4206`,
4207 ExpectedBazelTargets: []string{
4208 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004209 "copts": `select({
4210 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4211 "//conditions:default": [],
4212 })`,
4213 "additional_compiler_inputs": `select({
4214 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4215 "//conditions:default": [],
4216 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004217 "local_includes": `["."]`,
4218 }),
4219 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004220 "copts": `select({
4221 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4222 "//conditions:default": [],
4223 })`,
4224 "additional_compiler_inputs": `select({
4225 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4226 "//conditions:default": [],
4227 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004228 "local_includes": `["."]`,
4229 }),
4230 },
4231 })
4232}
4233
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004234func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4235 runCcLibraryTestCase(t, Bp2buildTestCase{
4236 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4237 ModuleTypeUnderTest: "cc_library",
4238 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4239 Blueprint: `
4240cc_library {
4241 name: "foo",
4242 sanitize: {
4243 misc_undefined: ["undefined", "nullability"],
4244 },
4245 target: {
4246 android: {
4247 sanitize: {
4248 misc_undefined: ["alignment"],
4249 },
4250 },
4251 linux_glibc: {
4252 sanitize: {
4253 integer_overflow: true,
4254 },
4255 },
4256 },
4257}
4258`,
4259 ExpectedBazelTargets: []string{
4260 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4261 "features": `[
4262 "ubsan_undefined",
4263 "ubsan_nullability",
4264 ] + select({
4265 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4266 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4267 "//conditions:default": [],
4268 })`,
4269 "local_includes": `["."]`,
4270 }),
4271 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4272 "features": `[
4273 "ubsan_undefined",
4274 "ubsan_nullability",
4275 ] + select({
4276 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4277 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4278 "//conditions:default": [],
4279 })`,
4280 "local_includes": `["."]`,
4281 }),
4282 },
4283 })
4284}
Yu Liu10174ff2023-02-21 12:05:26 -08004285
4286func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4287 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4288 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4289 ModuleTypeUnderTest: "cc_library",
4290 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4291 Blueprint: `
4292cc_library {
4293 name: "barlib",
4294 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4295 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004296 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004297}
4298cc_library {
4299 name: "bazlib",
4300 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
4301 bazel_module: { bp2build_available: false },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004302 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004303}
4304cc_library {
4305 name: "foo",
4306 shared_libs: ["barlib", "bazlib"],
4307 export_shared_lib_headers: ["bazlib"],
4308 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004309 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004310 ],
4311}`,
4312 ExpectedBazelTargets: []string{
4313 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004314 "implementation_dynamic_deps": `[":barlib"]`,
4315 "dynamic_deps": `[":bazlib"]`,
4316 "local_includes": `["."]`,
4317 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004318 }),
4319 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004320 "implementation_dynamic_deps": `[":barlib"]`,
4321 "dynamic_deps": `[":bazlib"]`,
4322 "local_includes": `["."]`,
4323 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004324 }),
4325 },
4326 })
4327}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004328
4329func TestCcLibraryWithThinLto(t *testing.T) {
4330 runCcLibraryTestCase(t, Bp2buildTestCase{
4331 Description: "cc_library has correct features when thin LTO is enabled",
4332 ModuleTypeUnderTest: "cc_library",
4333 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4334 Blueprint: `
4335cc_library {
4336 name: "foo",
4337 lto: {
4338 thin: true,
4339 },
4340}`,
4341 ExpectedBazelTargets: []string{
4342 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4343 "features": `["android_thin_lto"]`,
4344 "local_includes": `["."]`,
4345 }),
4346 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4347 "features": `["android_thin_lto"]`,
4348 "local_includes": `["."]`,
4349 }),
4350 },
4351 })
4352}
4353
4354func TestCcLibraryWithLtoNever(t *testing.T) {
4355 runCcLibraryTestCase(t, Bp2buildTestCase{
4356 Description: "cc_library has correct features when LTO is explicitly disabled",
4357 ModuleTypeUnderTest: "cc_library",
4358 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4359 Blueprint: `
4360cc_library {
4361 name: "foo",
4362 lto: {
4363 never: true,
4364 },
4365}`,
4366 ExpectedBazelTargets: []string{
4367 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4368 "features": `["-android_thin_lto"]`,
4369 "local_includes": `["."]`,
4370 }),
4371 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4372 "features": `["-android_thin_lto"]`,
4373 "local_includes": `["."]`,
4374 }),
4375 },
4376 })
4377}
4378
4379func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4380 runCcLibraryTestCase(t, Bp2buildTestCase{
4381 Description: "cc_library has correct features when LTO differs across arch and os variants",
4382 ModuleTypeUnderTest: "cc_library",
4383 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4384 Blueprint: `
4385cc_library {
4386 name: "foo",
4387 target: {
4388 android: {
4389 lto: {
4390 thin: true,
4391 },
4392 },
4393 },
4394 arch: {
4395 riscv64: {
4396 lto: {
4397 thin: false,
4398 },
4399 },
4400 },
4401}`,
4402 ExpectedBazelTargets: []string{
4403 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4404 "local_includes": `["."]`,
4405 "features": `select({
4406 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4407 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4408 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4409 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4410 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4411 "//conditions:default": [],
4412 })`}),
4413 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4414 "local_includes": `["."]`,
4415 "features": `select({
4416 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4417 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4418 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4419 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4420 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4421 "//conditions:default": [],
4422 })`}),
4423 },
4424 })
4425}
4426
4427func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4428 runCcLibraryTestCase(t, Bp2buildTestCase{
4429 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4430 ModuleTypeUnderTest: "cc_library",
4431 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4432 Blueprint: `
4433cc_library {
4434 name: "foo",
4435 lto: {
4436 never: true,
4437 },
4438 target: {
4439 android: {
4440 lto: {
4441 thin: true,
4442 never: false,
4443 },
4444 },
4445 },
4446}`,
4447 ExpectedBazelTargets: []string{
4448 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4449 "local_includes": `["."]`,
4450 "features": `select({
4451 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4452 "//conditions:default": ["-android_thin_lto"],
4453 })`,
4454 }),
4455 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4456 "local_includes": `["."]`,
4457 "features": `select({
4458 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4459 "//conditions:default": ["-android_thin_lto"],
4460 })`,
4461 }),
4462 },
4463 })
4464}
4465
4466func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4467 runCcLibraryTestCase(t, Bp2buildTestCase{
4468 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4469 ModuleTypeUnderTest: "cc_library",
4470 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4471 Blueprint: `
4472cc_library {
4473 name: "foo",
4474 lto: {
4475 thin: true,
4476 },
4477 whole_program_vtables: true,
4478}`,
4479 ExpectedBazelTargets: []string{
4480 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4481 "features": `[
4482 "android_thin_lto",
4483 "android_thin_lto_whole_program_vtables",
4484 ]`,
4485 "local_includes": `["."]`,
4486 }),
4487 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4488 "features": `[
4489 "android_thin_lto",
4490 "android_thin_lto_whole_program_vtables",
4491 ]`,
4492 "local_includes": `["."]`,
4493 }),
4494 },
4495 })
4496}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004497
4498func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4499 runCcLibraryTestCase(t, Bp2buildTestCase{
4500 Description: "cc_library changes hidden visibility flag to feature",
4501 ModuleTypeUnderTest: "cc_library",
4502 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4503 Blueprint: `
4504cc_library {
4505 name: "foo",
4506 cflags: ["-fvisibility=hidden"],
4507}`,
4508 ExpectedBazelTargets: []string{
4509 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4510 "features": `["visibility_hidden"]`,
4511 "local_includes": `["."]`,
4512 }),
4513 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4514 "features": `["visibility_hidden"]`,
4515 "local_includes": `["."]`,
4516 }),
4517 },
4518 })
4519}
4520
4521func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4522 runCcLibraryTestCase(t, Bp2buildTestCase{
4523 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4524 ModuleTypeUnderTest: "cc_library",
4525 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4526 Blueprint: `
4527cc_library {
4528 name: "foo",
4529 shared: {
4530 cflags: ["-fvisibility=hidden"],
4531 },
4532}`,
4533 ExpectedBazelTargets: []string{
4534 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4535 "local_includes": `["."]`,
4536 }),
4537 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4538 "features": `["visibility_hidden"]`,
4539 "local_includes": `["."]`,
4540 }),
4541 },
4542 })
4543}
4544
4545func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4546 runCcLibraryTestCase(t, Bp2buildTestCase{
4547 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4548 ModuleTypeUnderTest: "cc_library",
4549 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4550 Blueprint: `
4551cc_library {
4552 name: "foo",
4553 static: {
4554 cflags: ["-fvisibility=hidden"],
4555 },
4556}`,
4557 ExpectedBazelTargets: []string{
4558 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4559 "features": `["visibility_hidden"]`,
4560 "local_includes": `["."]`,
4561 }),
4562 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4563 "local_includes": `["."]`,
4564 }),
4565 },
4566 })
4567}
4568
4569func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4570 runCcLibraryTestCase(t, Bp2buildTestCase{
4571 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4572 ModuleTypeUnderTest: "cc_library",
4573 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4574 Blueprint: `
4575cc_library {
4576 name: "foo",
4577 target: {
4578 android: {
4579 cflags: ["-fvisibility=hidden"],
4580 },
4581 },
4582}`,
4583 ExpectedBazelTargets: []string{
4584 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4585 "features": `select({
4586 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4587 "//conditions:default": [],
4588 })`,
4589 "local_includes": `["."]`,
4590 }),
4591 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4592 "features": `select({
4593 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4594 "//conditions:default": [],
4595 })`,
4596 "local_includes": `["."]`,
4597 }),
4598 },
4599 })
4600}
Spandan Das4242f102023-04-19 22:31:54 +00004601
4602// Test that a config_setting specific to an apex is created by cc_library.
4603func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4604 runCcLibraryTestCase(t, Bp2buildTestCase{
4605 Description: "cc_library creates a config_setting for each apex in apex_available",
4606 ModuleTypeUnderTest: "cc_library",
4607 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4608 Dir: "build/bazel/rules/apex",
4609 Blueprint: `
4610cc_library {
4611 name: "foo",
4612 apex_available: [
4613 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4614 "myapex"
4615 ],
4616}`,
4617 ExpectedBazelTargets: []string{
4618 MakeBazelTargetNoRestrictions(
4619 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004620 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004621 AttrNameToString{
4622 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004623 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004624 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004625 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004626 },
4627 ),
4628 },
4629 })
4630}
Yu Liu93893ba2023-05-01 13:49:52 -07004631
4632func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4633 runCcLibraryTestCase(t, Bp2buildTestCase{
4634 Description: "cc_library cppflags in product variables",
4635 ModuleTypeUnderTest: "cc_library",
4636 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4637 Blueprint: soongCcLibraryPreamble + `cc_library {
4638 name: "a",
4639 srcs: ["a.cpp"],
4640 cppflags: [
4641 "-Wextra",
4642 "-DDEBUG_ONLY_CODE=0",
4643 ],
4644 product_variables: {
4645 eng: {
4646 cppflags: [
4647 "-UDEBUG_ONLY_CODE",
4648 "-DDEBUG_ONLY_CODE=1",
4649 ],
4650 },
4651 },
4652 include_build_directory: false,
4653}
4654`,
4655 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4656 "cppflags": `[
4657 "-Wextra",
4658 "-DDEBUG_ONLY_CODE=0",
4659 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004660 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004661 "-UDEBUG_ONLY_CODE",
4662 "-DDEBUG_ONLY_CODE=1",
4663 ],
4664 "//conditions:default": [],
4665 })`,
4666 "srcs": `["a.cpp"]`,
4667 }),
4668 },
4669 )
4670}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004671
4672func TestCcLibraryYaccConversion(t *testing.T) {
4673 runCcLibraryTestCase(t, Bp2buildTestCase{
4674 Description: "cc_library is built from .y/.yy files",
4675 ModuleTypeUnderTest: "cc_library",
4676 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4677 Blueprint: soongCcLibraryPreamble + `cc_library {
4678 name: "a",
4679 srcs: [
4680 "a.cpp",
4681 "a.yy",
4682 ],
4683 shared_libs: ["sharedlib"],
4684 static_libs: ["staticlib"],
4685 yacc: {
4686 flags: ["someYaccFlag"],
4687 gen_location_hh: true,
4688 gen_position_hh: true,
4689 },
4690}
4691cc_library_static {
4692 name: "staticlib",
4693 bazel_module: { bp2build_available: false },
4694}
4695cc_library {
4696 name: "sharedlib",
4697 bazel_module: { bp2build_available: false },
4698}
4699`,
4700 ExpectedBazelTargets: []string{
4701 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4702 "src": `"a.yy"`,
4703 "implementation_deps": `[":staticlib"]`,
4704 "implementation_dynamic_deps": `[":sharedlib"]`,
4705 "flags": `["someYaccFlag"]`,
4706 "gen_location_hh": "True",
4707 "gen_position_hh": "True",
4708 "local_includes": `["."]`,
4709 }),
4710 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4711 "srcs": `["a.cpp"]`,
4712 "implementation_deps": `[":staticlib"]`,
4713 "implementation_dynamic_deps": `[":sharedlib"]`,
4714 "implementation_whole_archive_deps": `[":a_yacc"]`,
4715 "local_includes": `["."]`,
4716 }),
4717 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4718 "srcs": `["a.cpp"]`,
4719 "implementation_deps": `[":staticlib"]`,
4720 "implementation_dynamic_deps": `[":sharedlib"]`,
4721 "implementation_whole_archive_deps": `[":a_yacc"]`,
4722 "local_includes": `["."]`,
4723 }),
4724 },
4725 })
4726}
Spandan Dasfb04c412023-05-15 18:35:36 +00004727
4728func TestCcLibraryHostLdLibs(t *testing.T) {
4729 runCcLibraryTestCase(t, Bp2buildTestCase{
4730 Description: "cc_binary linker flags for host_ldlibs",
4731 ModuleTypeUnderTest: "cc_binary",
4732 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4733 Blueprint: soongCcLibraryPreamble + `cc_binary {
4734 name: "a",
4735 host_supported: true,
4736 ldflags: ["-lcommon"],
4737 target: {
4738 linux: {
4739 host_ldlibs: [
4740 "-llinux",
4741 ],
4742 },
4743 darwin: {
4744 ldflags: ["-ldarwinadditional"],
4745 host_ldlibs: [
4746 "-ldarwin",
4747 ],
4748 },
4749 windows: {
4750 host_ldlibs: [
4751 "-lwindows",
4752 ],
4753 },
4754 },
4755}
4756`,
4757 ExpectedBazelTargets: []string{
4758 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4759 "linkopts": `["-lcommon"] + select({
4760 "//build/bazel/platforms/os:darwin": [
4761 "-ldarwinadditional",
4762 "-ldarwin",
4763 ],
4764 "//build/bazel/platforms/os:linux_glibc": ["-llinux"],
4765 "//build/bazel/platforms/os:windows": ["-lwindows"],
4766 "//conditions:default": [],
4767 })`,
4768 "local_includes": `["."]`,
4769 }),
4770 },
4771 })
4772}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004773
4774func TestCcLibraryWithCfi(t *testing.T) {
4775 runCcLibraryTestCase(t, Bp2buildTestCase{
4776 Description: "cc_library has correct features when cfi is enabled",
4777 ModuleTypeUnderTest: "cc_library",
4778 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4779 Blueprint: `
4780cc_library {
4781 name: "foo",
4782 sanitize: {
4783 cfi: true,
4784 },
4785}`,
4786 ExpectedBazelTargets: []string{
4787 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4788 "features": `["android_cfi"]`,
4789 "local_includes": `["."]`,
4790 }),
4791 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4792 "features": `["android_cfi"]`,
4793 "local_includes": `["."]`,
4794 }),
4795 },
4796 })
4797}
4798
4799func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4800 runCcLibraryTestCase(t, Bp2buildTestCase{
4801 Description: "cc_library has correct features when cfi is enabled for specific variants",
4802 ModuleTypeUnderTest: "cc_library",
4803 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4804 Blueprint: `
4805cc_library {
4806 name: "foo",
4807 target: {
4808 android: {
4809 sanitize: {
4810 cfi: true,
4811 },
4812 },
4813 },
4814}`,
4815 ExpectedBazelTargets: []string{
4816 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4817 "features": `select({
4818 "//build/bazel/platforms/os:android": ["android_cfi"],
4819 "//conditions:default": [],
4820 })`,
4821 "local_includes": `["."]`,
4822 }),
4823 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4824 "features": `select({
4825 "//build/bazel/platforms/os:android": ["android_cfi"],
4826 "//conditions:default": [],
4827 })`,
4828 "local_includes": `["."]`,
4829 }),
4830 },
4831 })
4832}
4833
4834func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4835 runCcLibraryTestCase(t, Bp2buildTestCase{
4836 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4837 ModuleTypeUnderTest: "cc_library",
4838 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4839 Blueprint: `
4840cc_library {
4841 name: "foo",
4842 sanitize: {
4843 cfi: true,
4844 config: {
4845 cfi_assembly_support: true,
4846 },
4847 },
4848}`,
4849 ExpectedBazelTargets: []string{
4850 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4851 "features": `[
4852 "android_cfi",
4853 "android_cfi_assembly_support",
4854 ]`,
4855 "local_includes": `["."]`,
4856 }),
4857 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4858 "features": `[
4859 "android_cfi",
4860 "android_cfi_assembly_support",
4861 ]`,
4862 "local_includes": `["."]`,
4863 }),
4864 },
4865 })
4866}
Spandan Das39ccf932023-05-26 18:03:39 +00004867
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004868func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4869 runCcLibraryTestCase(t, Bp2buildTestCase{
4870 Description: "cc_library disables cfi when explciitly set to false in the bp",
4871 ModuleTypeUnderTest: "cc_library",
4872 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4873 Blueprint: `
4874cc_library {
4875 name: "foo",
4876 sanitize: {
4877 cfi: false,
4878 },
4879}
4880`,
4881 ExpectedBazelTargets: []string{
4882 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4883 "features": `["-android_cfi"]`,
4884 "local_includes": `["."]`,
4885 }),
4886 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4887 "features": `["-android_cfi"]`,
4888 "local_includes": `["."]`,
4889 }),
4890 },
4891 })
4892}
4893
Spandan Das39ccf932023-05-26 18:03:39 +00004894func TestCcLibraryWithStem(t *testing.T) {
4895 runCcLibraryTestCase(t, Bp2buildTestCase{
4896 Description: "cc_library with stem property",
4897 ModuleTypeUnderTest: "cc_library_shared",
4898 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4899 Blueprint: soongCcLibraryPreamble + `
4900cc_library_shared {
4901 name: "foo_with_stem_simple",
4902 stem: "foo",
4903}
4904cc_library_shared {
4905 name: "foo_with_arch_variant_stem",
4906 arch: {
4907 arm: {
4908 stem: "foo-arm",
4909 },
4910 arm64: {
4911 stem: "foo-arm64",
4912 },
4913 },
4914}
4915`,
4916 ExpectedBazelTargets: []string{
4917 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4918 "stem": `"foo"`,
4919 "local_includes": `["."]`,
4920 }),
4921 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4922 "stem": `select({
4923 "//build/bazel/platforms/arch:arm": "foo-arm",
4924 "//build/bazel/platforms/arch:arm64": "foo-arm64",
4925 "//conditions:default": None,
4926 })`,
4927 "local_includes": `["."]`,
4928 }),
4929 },
4930 })
4931}
Spandan Dasc53767e2023-08-03 23:02:26 +00004932
4933// Bazel enforces that proto_library and the .proto file are in the same bazel package
4934func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4935 tc := Bp2buildTestCase{
4936 Description: "cc_library depends on .proto files from multiple packages",
4937 ModuleTypeUnderTest: "cc_library",
4938 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4939 Blueprint: `
4940cc_library_static {
4941 name: "foo",
4942 srcs: [
4943 "foo.proto",
4944 "bar/bar.proto", // Different package because there is a bar/Android.bp
4945 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4946 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004947 proto: {
4948 canonical_path_from_root: true,
4949 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004950}
4951` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
4952 Filesystem: map[string]string{
4953 "bar/Android.bp": "",
4954 "baz/subbaz/Android.bp": "",
4955 },
4956 }
4957
4958 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4959 // Root dir
4960 tc.ExpectedBazelTargets = []string{
4961 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4962 "local_includes": `["."]`,
4963 "deps": `[":libprotobuf-cpp-lite"]`,
4964 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4965 }),
4966 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4967 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004968 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004969 }),
4970 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4971 "deps": `[
4972 ":foo_proto",
4973 "//bar:foo_proto",
4974 "//baz/subbaz:foo_proto",
4975 ]`,
4976 }),
4977 }
4978 runCcLibraryTestCase(t, tc)
4979
4980 // bar dir
4981 tc.Dir = "bar"
4982 tc.ExpectedBazelTargets = []string{
4983 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004984 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004985 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004986 }),
4987 }
4988 runCcLibraryTestCase(t, tc)
4989
4990 // baz/subbaz dir
4991 tc.Dir = "baz/subbaz"
4992 tc.ExpectedBazelTargets = []string{
4993 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004994 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004995 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004996 }),
4997 }
4998 runCcLibraryTestCase(t, tc)
4999}
5000
5001// Bazel enforces that proto_library and the .proto file are in the same bazel package
5002func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
5003 tc := Bp2buildTestCase{
5004 Description: "cc_library depends on .proto files from multiple packages",
5005 ModuleTypeUnderTest: "cc_library",
5006 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5007 Blueprint: `
5008cc_library_static {
5009 name: "foo",
5010 srcs: [
5011 "foo.proto",
5012 "bar/bar.proto", // Different package because there is a bar/Android.bp
5013 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
5014 ],
5015 proto: {
5016 canonical_path_from_root: false,
5017 }
5018}
5019` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
5020 Filesystem: map[string]string{
5021 "bar/Android.bp": "",
5022 "baz/subbaz/Android.bp": "",
5023 },
5024 }
5025
5026 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
5027 // Root dir
5028 tc.ExpectedBazelTargets = []string{
5029 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5030 "local_includes": `["."]`,
5031 "deps": `[":libprotobuf-cpp-lite"]`,
5032 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5033 }),
5034 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5035 "srcs": `["foo.proto"]`,
5036 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00005037 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04005038 }),
5039 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5040 "deps": `[
5041 ":foo_proto",
5042 "//bar:foo_proto",
5043 "//baz/subbaz:foo_proto",
5044 ]`,
5045 }),
5046 }
5047 runCcLibraryTestCase(t, tc)
5048
5049 // bar dir
5050 tc.Dir = "bar"
5051 tc.ExpectedBazelTargets = []string{
5052 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5053 "srcs": `["//bar:bar.proto"]`,
5054 "strip_import_prefix": `""`,
5055 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00005056 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04005057 }),
5058 }
5059 runCcLibraryTestCase(t, tc)
5060
5061 // baz/subbaz dir
5062 tc.Dir = "baz/subbaz"
5063 tc.ExpectedBazelTargets = []string{
5064 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5065 "srcs": `["//baz/subbaz:baz.proto"]`,
5066 "strip_import_prefix": `""`,
5067 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00005068 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00005069 }),
5070 }
5071 runCcLibraryTestCase(t, tc)
5072}
Spandan Dasec39d512023-08-15 22:08:18 +00005073
5074func TestProtoIncludeDirs(t *testing.T) {
5075 tc := Bp2buildTestCase{
5076 Description: "cc_library depends on .proto files using proto.include_dirs",
5077 ModuleTypeUnderTest: "cc_library",
5078 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5079 Blueprint: `
5080cc_library_static {
5081 name: "foo",
5082 srcs: [
5083 "foo.proto",
5084 ],
5085 proto: {
5086 include_dirs: ["bar"],
5087 }
5088}
5089` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
5090 Filesystem: map[string]string{
5091 "bar/Android.bp": "",
5092 "bar/bar.proto": "",
5093 "bar/baz/Android.bp": "",
5094 "bar/baz/baz.proto": "",
5095 },
5096 }
5097
5098 // We will run the test 3 times and check in the root, bar and bar/baz directories
5099 // Root dir
5100 tc.ExpectedBazelTargets = []string{
5101 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5102 "local_includes": `["."]`,
5103 "deps": `[":libprotobuf-cpp-lite"]`,
5104 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5105 }),
5106 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5107 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00005108 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00005109 }),
5110 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5111 "deps": `[":foo_proto"]`,
5112 "transitive_deps": `[
5113 "//bar:bar.include_dir_bp2build_generated_proto",
5114 "//bar/baz:bar.include_dir_bp2build_generated_proto",
5115 ]`,
5116 }),
5117 }
5118 runCcLibraryTestCase(t, tc)
5119
5120 // bar dir
5121 tc.Dir = "bar"
5122 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005123 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00005124 "srcs": `["bar.proto"]`,
5125 "strip_import_prefix": `""`,
5126 "tags": `["manual"]`,
5127 }),
5128 }
5129 runCcLibraryTestCase(t, tc)
5130
5131 // bar/baz dir
5132 tc.Dir = "bar/baz"
5133 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005134 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00005135 "srcs": `["//bar/baz:baz.proto"]`,
5136 "strip_import_prefix": `""`,
5137 "import_prefix": `"baz"`,
5138 "tags": `["manual"]`,
5139 }),
5140 }
5141 runCcLibraryTestCase(t, tc)
5142}
Spandan Das4e5a1942023-08-22 19:20:39 +00005143
Spandan Dasf26ee152023-08-24 23:21:04 +00005144func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
5145 tc := Bp2buildTestCase{
5146 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
5147 ModuleTypeUnderTest: "cc_library",
5148 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5149 Blueprint: `
5150cc_library_static {
5151 name: "foo",
5152 srcs: [
5153 "foo.proto",
5154 "bar/bar.proto",
5155 ],
5156 proto: {
5157 include_dirs: ["baz"],
5158 }
5159}
5160` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
5161 Filesystem: map[string]string{
5162 "bar/Android.bp": "", // package boundary
5163 "baz/Android.bp": "",
5164 "baz/baz.proto": "",
5165 },
5166 }
5167
5168 tc.ExpectedBazelTargets = []string{
5169 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5170 "local_includes": `["."]`,
5171 "deps": `[":libprotobuf-cpp-lite"]`,
5172 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5173 }),
5174 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5175 "srcs": `["foo.proto"]`,
5176 "tags": `["manual"]`,
5177 }),
5178 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5179 "deps": `[
5180 ":foo_proto",
5181 "//bar:foo_proto",
5182 ]`,
5183 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
5184 }),
5185 }
5186 runCcLibraryTestCase(t, tc)
5187
5188}
5189
Spandan Das4e5a1942023-08-22 19:20:39 +00005190func TestProtoLocalIncludeDirs(t *testing.T) {
5191 tc := Bp2buildTestCase{
5192 Description: "cc_library depends on .proto files using proto.local_include_dirs",
5193 ModuleTypeUnderTest: "cc_library",
5194 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5195 Blueprint: simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
5196 Filesystem: map[string]string{
5197 "foo/Android.bp": `cc_library_static {
5198 name: "foo",
5199 srcs: [
5200 "foo.proto",
5201 ],
5202 proto: {
5203 local_include_dirs: ["foo_subdir"],
5204 },
5205 bazel_module: { bp2build_available: true },
5206}`,
5207 "foo/foo.proto": "",
5208 "foo/foo_subdir/Android.bp": "",
5209 "foo/foo_subdir/foo_subdir.proto": "",
5210 },
5211 }
5212
5213 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5214 // foo dir
5215 tc.Dir = "foo"
5216 tc.ExpectedBazelTargets = []string{
5217 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5218 "local_includes": `["."]`,
5219 "deps": `["//:libprotobuf-cpp-lite"]`,
5220 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5221 }),
5222 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5223 "srcs": `["foo.proto"]`,
5224 "tags": `["manual"]`,
5225 }),
5226 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5227 "deps": `[":foo_proto"]`,
5228 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5229 }),
5230 }
5231 runCcLibraryTestCase(t, tc)
5232
5233 // foo/foo_subdir
5234 tc.Dir = "foo/foo_subdir"
5235 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005236 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005237 "srcs": `["foo_subdir.proto"]`,
5238 "strip_import_prefix": `""`,
5239 "tags": `["manual"]`,
5240 }),
5241 }
5242 runCcLibraryTestCase(t, tc)
5243}