blob: c11600842f5d3816327cad0a518488a270fc8d64 [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",
Liz Kammerbaced712022-09-16 09:01:29 -040038}
39`
Liz Kammer12615db2021-09-28 09:19:17 -040040
41 soongCcProtoLibraries = `
42cc_library {
43 name: "libprotobuf-cpp-lite",
Liz Kammer12615db2021-09-28 09:19:17 -040044}
45
46cc_library {
47 name: "libprotobuf-cpp-full",
Liz Kammer12615db2021-09-28 09:19:17 -040048}`
49
50 soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
Jingwen Chen63930982021-03-24 10:04:33 -040051)
52
Sam Delmerico3177a6e2022-06-21 19:28:33 +000053func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040054 t.Helper()
Chris Parsonscd209032023-09-19 01:12:48 +000055 tc.StubbedBuildDefinitions = append(tc.StubbedBuildDefinitions, "libprotobuf-cpp-lite", "libprotobuf-cpp-full")
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056 RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057}
58
59func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
60 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000061 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020062 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040063 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Vinh Tran367d89d2023-04-28 11:21:25 -040065 ctx.RegisterModuleType("aidl_library", aidl_library.AidlLibraryFactory)
Spandan Das63acae92023-09-14 22:34:34 +000066 ctx.RegisterModuleType("ndk_library", cc.NdkLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020067}
68
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020069func TestCcLibrarySimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000070 runCcLibraryTestCase(t, Bp2buildTestCase{
71 Description: "cc_library - simple example",
72 ModuleTypeUnderTest: "cc_library",
73 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +000074 StubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion", "some-headers"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +000075 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 +
Chris Parsonscd209032023-09-19 01:12:48 +000097 simpleModule("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,
Chris Parsonscd209032023-09-19 01:12:48 +0000171 StubbedBuildDefinitions: []string{"libc_headers"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000172 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200173 "ld-android.cpp": "",
174 "linked_list.h": "",
175 "linker.h": "",
176 "linker_block_allocator.h": "",
177 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400178 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000179 Blueprint: soongCcLibraryPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +0000180 simpleModule("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400181cc_library {
182 name: "fake-ld-android",
183 srcs: ["ld_android.cpp"],
184 cflags: [
185 "-Wall",
186 "-Wextra",
187 "-Wunused",
188 "-Werror",
189 ],
190 header_libs: ["libc_headers"],
191 ldflags: [
192 "-Wl,--exclude-libs=libgcc.a",
193 "-Wl,--exclude-libs=libgcc_stripped.a",
194 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
195 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
196 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
197 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
198 ],
199 arch: {
200 x86: {
201 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
202 },
203 x86_64: {
204 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
205 },
206 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400207 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400208}
209`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000210 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500211 "srcs": `["ld_android.cpp"]`,
212 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400213 "-Wall",
214 "-Wextra",
215 "-Wunused",
216 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500217 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500218 "implementation_deps": `[":libc_headers"]`,
219 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400220 "-Wl,--exclude-libs=libgcc.a",
221 "-Wl,--exclude-libs=libgcc_stripped.a",
222 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
226 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000227 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
228 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400229 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500231 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200232 })
233}
234
235func TestCcLibraryExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000236 runCcLibraryTestCase(t, Bp2buildTestCase{
237 Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
238 ModuleTypeUnderTest: "cc_library",
239 ModuleTypeUnderTestFactory: cc.LibraryFactory,
240 Dir: "external",
241 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200242 "external/math/cosf.c": "",
243 "external/math/erf.c": "",
244 "external/math/erf_data.c": "",
245 "external/math/erff.c": "",
246 "external/math/erff_data.c": "",
247 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000248cc_library {
249 name: "fake-libarm-optimized-routines-math",
250 exclude_srcs: [
251 // Provided by:
252 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
253 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
254 "math/erf.c",
255 "math/erf_data.c",
256 "math/erff.c",
257 "math/erff_data.c",
258 ],
259 srcs: [
260 "math/*.c",
261 ],
262 // arch-specific settings
263 arch: {
264 arm64: {
265 cflags: [
266 "-DHAVE_FAST_FMA=1",
267 ],
268 },
269 },
270 bazel_module: { bp2build_available: true },
271}
272`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200273 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000274 Blueprint: soongCcLibraryPreamble,
275 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500276 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000277 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
278 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500279 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500280 "local_includes": `["."]`,
281 "srcs_c": `["math/cosf.c"]`,
282 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200283 })
284}
285
286func TestCcLibrarySharedStaticProps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000287 runCcLibraryTestCase(t, Bp2buildTestCase{
288 Description: "cc_library shared/static props",
289 ModuleTypeUnderTest: "cc_library",
290 ModuleTypeUnderTestFactory: cc.LibraryFactory,
291 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400292 "both.cpp": "",
293 "sharedonly.cpp": "",
294 "staticonly.cpp": "",
295 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000296 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000297cc_library {
298 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400299 srcs: ["both.cpp"],
300 cflags: ["bothflag"],
301 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400302 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
303 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400304 static: {
305 srcs: ["staticonly.cpp"],
306 cflags: ["staticflag"],
307 shared_libs: ["shared_dep_for_static"],
308 static_libs: ["static_dep_for_static"],
309 whole_static_libs: ["whole_static_lib_for_static"],
310 },
311 shared: {
312 srcs: ["sharedonly.cpp"],
313 cflags: ["sharedflag"],
314 shared_libs: ["shared_dep_for_shared"],
315 static_libs: ["static_dep_for_shared"],
316 whole_static_libs: ["whole_static_lib_for_shared"],
317 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400318 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000319}
320
Liz Kammer8337ea42021-09-10 10:06:32 -0400321cc_library_static {
322 name: "static_dep_for_shared",
Liz Kammer8337ea42021-09-10 10:06:32 -0400323}
Chris Parsons08648312021-05-06 16:23:19 -0400324
Liz Kammer8337ea42021-09-10 10:06:32 -0400325cc_library_static {
326 name: "static_dep_for_static",
Liz Kammer8337ea42021-09-10 10:06:32 -0400327}
Chris Parsons08648312021-05-06 16:23:19 -0400328
Liz Kammer8337ea42021-09-10 10:06:32 -0400329cc_library_static {
330 name: "static_dep_for_both",
Liz Kammer8337ea42021-09-10 10:06:32 -0400331}
Chris Parsons08648312021-05-06 16:23:19 -0400332
Liz Kammer8337ea42021-09-10 10:06:32 -0400333cc_library_static {
334 name: "whole_static_lib_for_shared",
Liz Kammer8337ea42021-09-10 10:06:32 -0400335}
Chris Parsons08648312021-05-06 16:23:19 -0400336
Liz Kammer8337ea42021-09-10 10:06:32 -0400337cc_library_static {
338 name: "whole_static_lib_for_static",
Liz Kammer8337ea42021-09-10 10:06:32 -0400339}
Chris Parsons08648312021-05-06 16:23:19 -0400340
Liz Kammer8337ea42021-09-10 10:06:32 -0400341cc_library_static {
342 name: "whole_static_lib_for_both",
Liz Kammer8337ea42021-09-10 10:06:32 -0400343}
Chris Parsons08648312021-05-06 16:23:19 -0400344
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400345cc_library_static {
346 name: "whole_and_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400347}
348
Liz Kammer8337ea42021-09-10 10:06:32 -0400349cc_library {
350 name: "shared_dep_for_shared",
Liz Kammer8337ea42021-09-10 10:06:32 -0400351}
Chris Parsons08648312021-05-06 16:23:19 -0400352
Liz Kammer8337ea42021-09-10 10:06:32 -0400353cc_library {
354 name: "shared_dep_for_static",
Liz Kammer8337ea42021-09-10 10:06:32 -0400355}
Chris Parsons08648312021-05-06 16:23:19 -0400356
Liz Kammer8337ea42021-09-10 10:06:32 -0400357cc_library {
358 name: "shared_dep_for_both",
Liz Kammer8337ea42021-09-10 10:06:32 -0400359}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000360`,
Chris Parsonscd209032023-09-19 01:12:48 +0000361 StubbedBuildDefinitions: []string{"static_dep_for_shared", "static_dep_for_static",
362 "static_dep_for_both", "whole_static_lib_for_shared", "whole_static_lib_for_static",
363 "whole_static_lib_for_both", "whole_and_static_lib_for_both", "shared_dep_for_shared",
364 "shared_dep_for_static", "shared_dep_for_both",
365 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000366 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000367 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500368 "copts": `[
369 "bothflag",
370 "staticflag",
371 ]`,
372 "implementation_deps": `[
373 ":static_dep_for_both",
374 ":static_dep_for_static",
375 ]`,
376 "implementation_dynamic_deps": `[
377 ":shared_dep_for_both",
378 ":shared_dep_for_static",
379 ]`,
380 "srcs": `[
381 "both.cpp",
382 "staticonly.cpp",
383 ]`,
384 "whole_archive_deps": `[
385 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400386 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500387 ":whole_static_lib_for_static",
388 ]`}),
Alixe06d75b2022-08-31 18:28:19 +0000389 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500390 "copts": `[
391 "bothflag",
392 "sharedflag",
393 ]`,
394 "implementation_deps": `[
395 ":static_dep_for_both",
396 ":static_dep_for_shared",
397 ]`,
398 "implementation_dynamic_deps": `[
399 ":shared_dep_for_both",
400 ":shared_dep_for_shared",
401 ]`,
402 "srcs": `[
403 "both.cpp",
404 "sharedonly.cpp",
405 ]`,
406 "whole_archive_deps": `[
407 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400408 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500409 ":whole_static_lib_for_shared",
410 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500411 }),
412 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200413 })
414}
415
Liz Kammer7a210ac2021-09-22 15:52:58 -0400416func TestCcLibraryDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000417 runCcLibraryTestCase(t, Bp2buildTestCase{
418 Description: "cc_library shared/static props",
419 ModuleTypeUnderTest: "cc_library",
420 ModuleTypeUnderTestFactory: cc.LibraryFactory,
421 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400422 "both.cpp": "",
423 "sharedonly.cpp": "",
424 "staticonly.cpp": "",
425 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000426 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400427cc_library {
428 name: "a",
429 srcs: ["both.cpp"],
430 cflags: ["bothflag"],
431 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
432 export_shared_lib_headers: ["shared_dep_for_both"],
433 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
434 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
435 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
436 static: {
437 srcs: ["staticonly.cpp"],
438 cflags: ["staticflag"],
439 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
440 export_shared_lib_headers: ["shared_dep_for_static"],
441 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
442 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
443 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
444 },
445 shared: {
446 srcs: ["sharedonly.cpp"],
447 cflags: ["sharedflag"],
448 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
449 export_shared_lib_headers: ["shared_dep_for_shared"],
450 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
451 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
452 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
453 },
454 include_build_directory: false,
455}
Chris Parsonscd209032023-09-19 01:12:48 +0000456` + simpleModule("cc_library_static", "static_dep_for_shared") +
457 simpleModule("cc_library_static", "implementation_static_dep_for_shared") +
458 simpleModule("cc_library_static", "static_dep_for_static") +
459 simpleModule("cc_library_static", "implementation_static_dep_for_static") +
460 simpleModule("cc_library_static", "static_dep_for_both") +
461 simpleModule("cc_library_static", "implementation_static_dep_for_both") +
462 simpleModule("cc_library_static", "whole_static_dep_for_shared") +
463 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
464 simpleModule("cc_library_static", "whole_static_dep_for_static") +
465 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
466 simpleModule("cc_library_static", "whole_static_dep_for_both") +
467 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
468 simpleModule("cc_library", "shared_dep_for_shared") +
469 simpleModule("cc_library", "implementation_shared_dep_for_shared") +
470 simpleModule("cc_library", "shared_dep_for_static") +
471 simpleModule("cc_library", "implementation_shared_dep_for_static") +
472 simpleModule("cc_library", "shared_dep_for_both") +
473 simpleModule("cc_library", "implementation_shared_dep_for_both"),
474 StubbedBuildDefinitions: []string{"static_dep_for_shared", "implementation_static_dep_for_shared",
475 "static_dep_for_static", "implementation_static_dep_for_static", "static_dep_for_both",
476 "implementation_static_dep_for_both", "whole_static_dep_for_shared",
477 "not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_static",
478 "not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_both",
479 "not_explicitly_exported_whole_static_dep_for_both", "shared_dep_for_shared",
480 "implementation_shared_dep_for_shared", "shared_dep_for_static",
481 "implementation_shared_dep_for_static", "shared_dep_for_both",
482 "implementation_shared_dep_for_both",
483 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000484 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000485 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500486 "copts": `[
487 "bothflag",
488 "staticflag",
489 ]`,
490 "deps": `[
491 ":static_dep_for_both",
492 ":static_dep_for_static",
493 ]`,
494 "dynamic_deps": `[
495 ":shared_dep_for_both",
496 ":shared_dep_for_static",
497 ]`,
498 "implementation_deps": `[
499 ":implementation_static_dep_for_both",
500 ":implementation_static_dep_for_static",
501 ]`,
502 "implementation_dynamic_deps": `[
503 ":implementation_shared_dep_for_both",
504 ":implementation_shared_dep_for_static",
505 ]`,
506 "srcs": `[
507 "both.cpp",
508 "staticonly.cpp",
509 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500510 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400511 ":not_explicitly_exported_whole_static_dep_for_both",
512 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500513 ":not_explicitly_exported_whole_static_dep_for_static",
514 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500515 ]`,
516 }),
Alixe06d75b2022-08-31 18:28:19 +0000517 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500518 "copts": `[
519 "bothflag",
520 "sharedflag",
521 ]`,
522 "deps": `[
523 ":static_dep_for_both",
524 ":static_dep_for_shared",
525 ]`,
526 "dynamic_deps": `[
527 ":shared_dep_for_both",
528 ":shared_dep_for_shared",
529 ]`,
530 "implementation_deps": `[
531 ":implementation_static_dep_for_both",
532 ":implementation_static_dep_for_shared",
533 ]`,
534 "implementation_dynamic_deps": `[
535 ":implementation_shared_dep_for_both",
536 ":implementation_shared_dep_for_shared",
537 ]`,
538 "srcs": `[
539 "both.cpp",
540 "sharedonly.cpp",
541 ]`,
542 "whole_archive_deps": `[
543 ":not_explicitly_exported_whole_static_dep_for_both",
544 ":whole_static_dep_for_both",
545 ":not_explicitly_exported_whole_static_dep_for_shared",
546 ":whole_static_dep_for_shared",
547 ]`,
548 })},
549 },
550 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400551}
552
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400553func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000554 runCcLibraryTestCase(t, Bp2buildTestCase{
555 ModuleTypeUnderTest: "cc_library",
556 ModuleTypeUnderTestFactory: cc.LibraryFactory,
557 Dir: "foo/bar",
Chris Parsonscd209032023-09-19 01:12:48 +0000558 StubbedBuildDefinitions: []string{"//foo/bar:prebuilt_whole_static_lib_for_shared", "//foo/bar:prebuilt_whole_static_lib_for_static",
559 "//foo/bar:prebuilt_whole_static_lib_for_both"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000560 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400561 "foo/bar/Android.bp": `
562cc_library {
563 name: "a",
564 whole_static_libs: ["whole_static_lib_for_both"],
565 static: {
566 whole_static_libs: ["whole_static_lib_for_static"],
567 },
568 shared: {
569 whole_static_libs: ["whole_static_lib_for_shared"],
570 },
571 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400572 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400573}
574
575cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
576
577cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
578
579cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
580`,
581 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000582 Blueprint: soongCcLibraryPreamble,
583 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000584 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500585 "whole_archive_deps": `[
586 ":whole_static_lib_for_both_alwayslink",
587 ":whole_static_lib_for_static_alwayslink",
588 ]`,
589 }),
Alixe06d75b2022-08-31 18:28:19 +0000590 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500591 "whole_archive_deps": `[
592 ":whole_static_lib_for_both_alwayslink",
593 ":whole_static_lib_for_shared_alwayslink",
594 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500595 }),
596 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500597 },
598 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400599}
600
Jingwen Chenbcf53042021-05-26 04:42:42 +0000601func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000602 runCcLibraryTestCase(t, Bp2buildTestCase{
603 Description: "cc_library shared/static props in arch",
604 ModuleTypeUnderTest: "cc_library",
605 ModuleTypeUnderTestFactory: cc.LibraryFactory,
606 Dir: "foo/bar",
607 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000608 "foo/bar/arm.cpp": "",
609 "foo/bar/x86.cpp": "",
610 "foo/bar/sharedonly.cpp": "",
611 "foo/bar/staticonly.cpp": "",
612 "foo/bar/Android.bp": `
613cc_library {
614 name: "a",
615 arch: {
616 arm: {
617 shared: {
618 srcs: ["arm_shared.cpp"],
619 cflags: ["-DARM_SHARED"],
620 static_libs: ["arm_static_dep_for_shared"],
621 whole_static_libs: ["arm_whole_static_dep_for_shared"],
622 shared_libs: ["arm_shared_dep_for_shared"],
623 },
624 },
625 x86: {
626 static: {
627 srcs: ["x86_static.cpp"],
628 cflags: ["-DX86_STATIC"],
629 static_libs: ["x86_dep_for_static"],
630 },
631 },
632 },
633 target: {
634 android: {
635 shared: {
636 srcs: ["android_shared.cpp"],
637 cflags: ["-DANDROID_SHARED"],
638 static_libs: ["android_dep_for_shared"],
639 },
640 },
641 android_arm: {
642 shared: {
643 cflags: ["-DANDROID_ARM_SHARED"],
644 },
645 },
646 },
647 srcs: ["both.cpp"],
648 cflags: ["bothflag"],
649 static_libs: ["static_dep_for_both"],
650 static: {
651 srcs: ["staticonly.cpp"],
652 cflags: ["staticflag"],
653 static_libs: ["static_dep_for_static"],
654 },
655 shared: {
656 srcs: ["sharedonly.cpp"],
657 cflags: ["sharedflag"],
658 static_libs: ["static_dep_for_shared"],
659 },
660 bazel_module: { bp2build_available: true },
661}
662
663cc_library_static { name: "static_dep_for_shared" }
664cc_library_static { name: "static_dep_for_static" }
665cc_library_static { name: "static_dep_for_both" }
666
667cc_library_static { name: "arm_static_dep_for_shared" }
668cc_library_static { name: "arm_whole_static_dep_for_shared" }
669cc_library_static { name: "arm_shared_dep_for_shared" }
670
671cc_library_static { name: "x86_dep_for_static" }
672
673cc_library_static { name: "android_dep_for_shared" }
674`,
675 },
Chris Parsonscd209032023-09-19 01:12:48 +0000676 StubbedBuildDefinitions: []string{"//foo/bar:static_dep_for_shared", "//foo/bar:static_dep_for_static",
677 "//foo/bar:static_dep_for_both", "//foo/bar:arm_static_dep_for_shared", "//foo/bar:arm_whole_static_dep_for_shared",
678 "//foo/bar:arm_shared_dep_for_shared", "//foo/bar:x86_dep_for_static", "//foo/bar:android_dep_for_shared",
679 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000680 Blueprint: soongCcLibraryPreamble,
681 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000682 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500683 "copts": `[
684 "bothflag",
685 "staticflag",
686 ] + select({
687 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
688 "//conditions:default": [],
689 })`,
690 "implementation_deps": `[
691 ":static_dep_for_both",
692 ":static_dep_for_static",
693 ] + select({
694 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
695 "//conditions:default": [],
696 })`,
697 "local_includes": `["."]`,
698 "srcs": `[
699 "both.cpp",
700 "staticonly.cpp",
701 ] + select({
702 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
703 "//conditions:default": [],
704 })`,
705 }),
Alixe06d75b2022-08-31 18:28:19 +0000706 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500707 "copts": `[
708 "bothflag",
709 "sharedflag",
710 ] + select({
711 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
712 "//conditions:default": [],
713 }) + select({
714 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
715 "//conditions:default": [],
716 }) + select({
717 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
718 "//conditions:default": [],
719 })`,
720 "implementation_deps": `[
721 ":static_dep_for_both",
722 ":static_dep_for_shared",
723 ] + select({
724 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
725 "//conditions:default": [],
726 }) + select({
727 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
728 "//conditions:default": [],
729 })`,
730 "implementation_dynamic_deps": `select({
731 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
732 "//conditions:default": [],
733 })`,
734 "local_includes": `["."]`,
735 "srcs": `[
736 "both.cpp",
737 "sharedonly.cpp",
738 ] + select({
739 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
740 "//conditions:default": [],
741 }) + select({
742 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
743 "//conditions:default": [],
744 })`,
745 "whole_archive_deps": `select({
746 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
747 "//conditions:default": [],
748 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500749 }),
750 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500751 },
752 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000753}
754
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000755func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000756 runCcLibraryTestCase(t, Bp2buildTestCase{
757 Description: "cc_library shared/static props with c/cpp/s mixed sources",
758 ModuleTypeUnderTest: "cc_library",
759 ModuleTypeUnderTestFactory: cc.LibraryFactory,
760 Dir: "foo/bar",
Chris Parsonscd209032023-09-19 01:12:48 +0000761 StubbedBuildDefinitions: []string{"//foo/bar:shared_filegroup", "//foo/bar:static_filegroup", "//foo/bar:both_filegroup"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000762 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000763 "foo/bar/both_source.cpp": "",
764 "foo/bar/both_source.cc": "",
765 "foo/bar/both_source.c": "",
766 "foo/bar/both_source.s": "",
767 "foo/bar/both_source.S": "",
768 "foo/bar/shared_source.cpp": "",
769 "foo/bar/shared_source.cc": "",
770 "foo/bar/shared_source.c": "",
771 "foo/bar/shared_source.s": "",
772 "foo/bar/shared_source.S": "",
773 "foo/bar/static_source.cpp": "",
774 "foo/bar/static_source.cc": "",
775 "foo/bar/static_source.c": "",
776 "foo/bar/static_source.s": "",
777 "foo/bar/static_source.S": "",
778 "foo/bar/Android.bp": `
779cc_library {
780 name: "a",
781 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400782 "both_source.cpp",
783 "both_source.cc",
784 "both_source.c",
785 "both_source.s",
786 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400787 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400788 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000789 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400790 srcs: [
791 "static_source.cpp",
792 "static_source.cc",
793 "static_source.c",
794 "static_source.s",
795 "static_source.S",
796 ":static_filegroup",
797 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000798 },
799 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400800 srcs: [
801 "shared_source.cpp",
802 "shared_source.cc",
803 "shared_source.c",
804 "shared_source.s",
805 "shared_source.S",
806 ":shared_filegroup",
807 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000808 },
809 bazel_module: { bp2build_available: true },
810}
811
812filegroup {
813 name: "both_filegroup",
814 srcs: [
815 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400816 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000817}
818
819filegroup {
820 name: "shared_filegroup",
821 srcs: [
822 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400823 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000824}
825
826filegroup {
827 name: "static_filegroup",
828 srcs: [
829 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400830 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000831}
832`,
833 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000834 Blueprint: soongCcLibraryPreamble,
835 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000836 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500837 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500838 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000839 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400840 "both_source.cc",
841 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500842 "static_source.cpp",
843 "static_source.cc",
844 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500845 ]`,
846 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000847 "both_source.s",
848 "both_source.S",
849 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500850 "static_source.s",
851 "static_source.S",
852 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500853 ]`,
854 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000855 "both_source.c",
856 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500857 "static_source.c",
858 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500859 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500860 }),
Alixe06d75b2022-08-31 18:28:19 +0000861 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500862 "local_includes": `["."]`,
863 "srcs": `[
864 "both_source.cpp",
865 "both_source.cc",
866 ":both_filegroup_cpp_srcs",
867 "shared_source.cpp",
868 "shared_source.cc",
869 ":shared_filegroup_cpp_srcs",
870 ]`,
871 "srcs_as": `[
872 "both_source.s",
873 "both_source.S",
874 ":both_filegroup_as_srcs",
875 "shared_source.s",
876 "shared_source.S",
877 ":shared_filegroup_as_srcs",
878 ]`,
879 "srcs_c": `[
880 "both_source.c",
881 ":both_filegroup_c_srcs",
882 "shared_source.c",
883 ":shared_filegroup_c_srcs",
884 ]`,
885 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000886}
887
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000888func TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000889 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000890 Description: "cc_library non-configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000891 ModuleTypeUnderTest: "cc_library",
892 ModuleTypeUnderTestFactory: cc.LibraryFactory,
893 Dir: "foo/bar",
894 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200895 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200896cc_library {
897 name: "a",
898 srcs: ["a.cpp"],
899 version_script: "v.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000900 dynamic_list: "dynamic.list",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200901 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400902 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200903}
904`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200905 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000906 Blueprint: soongCcLibraryPreamble,
907 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000908 "additional_linker_inputs": `[
909 "v.map",
910 "dynamic.list",
911 ]`,
912 "linkopts": `[
913 "-Wl,--version-script,$(location v.map)",
914 "-Wl,--dynamic-list,$(location dynamic.list)",
915 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000916 "srcs": `["a.cpp"]`,
917 "features": `["android_cfi_exports_map"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500918 }),
919 },
920 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200921}
922
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000923func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000924 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000925 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000926 ModuleTypeUnderTest: "cc_library",
927 ModuleTypeUnderTestFactory: cc.LibraryFactory,
928 Dir: "foo/bar",
929 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200930 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400931cc_library {
932 name: "a",
933 srcs: ["a.cpp"],
934 arch: {
935 arm: {
936 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000937 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400938 },
939 arm64: {
940 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000941 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400942 },
943 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200944
Liz Kammer8337ea42021-09-10 10:06:32 -0400945 bazel_module: { bp2build_available: true },
946 include_build_directory: false,
947}
Liz Kammerd366c902021-06-03 13:43:01 -0400948 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200949 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000950 Blueprint: soongCcLibraryPreamble,
951 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500952 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000953 "//build/bazel/platforms/arch:arm": [
954 "arm.map",
955 "dynamic_arm.list",
956 ],
957 "//build/bazel/platforms/arch:arm64": [
958 "arm64.map",
959 "dynamic_arm64.list",
960 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400961 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500962 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500963 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000964 "//build/bazel/platforms/arch:arm": [
965 "-Wl,--version-script,$(location arm.map)",
966 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
967 ],
968 "//build/bazel/platforms/arch:arm64": [
969 "-Wl,--version-script,$(location arm64.map)",
970 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
971 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400972 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500973 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500974 "srcs": `["a.cpp"]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000975 "features": `select({
976 "//build/bazel/platforms/arch:arm": ["android_cfi_exports_map"],
977 "//build/bazel/platforms/arch:arm64": ["android_cfi_exports_map"],
978 "//conditions:default": [],
979 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500980 }),
981 },
982 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200983}
984
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000985func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
986 runCcLibraryTestCase(t, Bp2buildTestCase{
987 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
988 ModuleTypeUnderTest: "cc_library",
989 ModuleTypeUnderTestFactory: cc.LibraryFactory,
990 Filesystem: map[string]string{
991 "version_script": "",
992 "dynamic.list": "",
993 },
994 Blueprint: `
995cc_library {
996 name: "foo",
997 ldflags: [
998 "--nospace_flag",
999 "-z spaceflag",
1000 ],
1001 version_script: "version_script",
1002 dynamic_list: "dynamic.list",
1003 include_build_directory: false,
1004}
1005`,
1006 ExpectedBazelTargets: []string{
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001007 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
1008 "features": `["android_cfi_exports_map"]`,
1009 }),
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001010 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1011 "additional_linker_inputs": `[
1012 "version_script",
1013 "dynamic.list",
1014 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001015 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001016 "linkopts": `[
1017 "--nospace_flag",
1018 "-z",
1019 "spaceflag",
1020 "-Wl,--version-script,$(location version_script)",
1021 "-Wl,--dynamic-list,$(location dynamic.list)",
1022 ]`,
1023 }),
1024 },
1025 })
1026}
1027
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001028func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001029 runCcLibraryTestCase(t, Bp2buildTestCase{
1030 Description: "cc_library shared_libs",
1031 ModuleTypeUnderTest: "cc_library",
1032 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001033 StubbedBuildDefinitions: []string{"mylib"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001034 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001035cc_library {
1036 name: "mylib",
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001037}
1038
1039cc_library {
1040 name: "a",
1041 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001042 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001043}
1044`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001045 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001046 "implementation_dynamic_deps": `[":mylib"]`,
1047 }),
1048 },
1049 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001050}
1051
Liz Kammer0eae52e2021-10-06 10:32:26 -04001052func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001053 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001054 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001055 "features": `[
1056 "disable_pack_relocations",
1057 "-no_undefined_symbols",
1058 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001059 "native_coverage": `False`,
1060 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001061 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001062 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001063 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001064 "//build/bazel/platforms/arch:x86_64": [
1065 "disable_pack_relocations",
1066 "-no_undefined_symbols",
1067 ],
1068 "//conditions:default": [],
1069 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001070 "native_coverage": `False`,
1071 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001072 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001073 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001074 "features": `select({
1075 "//build/bazel/platforms/os:darwin": [
1076 "disable_pack_relocations",
1077 "-no_undefined_symbols",
1078 ],
1079 "//conditions:default": [],
1080 })`,
1081 "srcs": `["c.cpp"]`,
1082 })...)
1083
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001084 runCcLibraryTestCase(t, Bp2buildTestCase{
1085 Description: "cc_library pack_relocations test",
1086 ModuleTypeUnderTest: "cc_library",
1087 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1088 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001089cc_library {
1090 name: "a",
1091 srcs: ["a.cpp"],
1092 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001093 allow_undefined_symbols: true,
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: "b",
1100 srcs: ["b.cpp"],
1101 arch: {
1102 x86_64: {
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,
Yu Liu8d82ac52022-05-17 15:13:28 -07001108 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001109}
1110
1111cc_library {
1112 name: "c",
1113 srcs: ["c.cpp"],
1114 target: {
1115 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001116 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001117 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001118 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001119 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001120 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001121}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001122 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001123 })
1124}
1125
1126func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001127 runCcLibraryTestCase(t, Bp2buildTestCase{
1128 Description: "cc_library spaces in copts",
1129 ModuleTypeUnderTest: "cc_library",
1130 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1131 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001132cc_library {
1133 name: "a",
1134 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001135 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001136}
1137`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001138 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001139 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001140 "-include",
1141 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001142 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001143 }),
1144 },
1145 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001146}
1147
1148func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001149 runCcLibraryTestCase(t, Bp2buildTestCase{
1150 Description: "cc_library cppflags usage",
1151 ModuleTypeUnderTest: "cc_library",
1152 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1153 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001154 name: "a",
1155 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001156 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001157 cppflags: [
1158 "-fsigned-char",
1159 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001160 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001161 arch: {
1162 arm64: {
1163 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001164 },
Liz Kammerd366c902021-06-03 13:43:01 -04001165 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001166 target: {
1167 android: {
1168 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001169 },
Liz Kammerd366c902021-06-03 13:43:01 -04001170 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001171 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001172}
1173`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001174 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001175 "copts": `["-Wall"]`,
1176 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001177 "-fsigned-char",
1178 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001179 ] + select({
1180 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1181 "//conditions:default": [],
1182 }) + select({
1183 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1184 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001185 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001186 "srcs": `["a.cpp"]`,
1187 }),
1188 },
1189 )
Jingwen Chen63930982021-03-24 10:04:33 -04001190}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001191
Liz Kammer47535c52021-06-02 16:02:22 -04001192func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001193 runCcLibraryTestCase(t, Bp2buildTestCase{
1194 ModuleTypeUnderTest: "cc_library",
1195 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1196 Filesystem: map[string]string{},
Chris Parsonscd209032023-09-19 01:12:48 +00001197 StubbedBuildDefinitions: []string{"arm_whole_static_lib_excludes", "malloc_not_svelte_whole_static_lib",
1198 "arm_static_lib_excludes", "malloc_not_svelte_whole_static_lib_excludes", "arm_shared_lib_excludes",
1199 "malloc_not_svelte_static_lib_excludes", "arm_shared_lib_excludes", "malloc_not_svelte_shared_lib",
1200 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001201 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001202cc_library {
1203 name: "foo_static",
1204 srcs: ["common.c"],
1205 whole_static_libs: [
1206 "arm_whole_static_lib_excludes",
1207 "malloc_not_svelte_whole_static_lib_excludes"
1208 ],
1209 static_libs: [
1210 "arm_static_lib_excludes",
1211 "malloc_not_svelte_static_lib_excludes"
1212 ],
1213 shared_libs: [
1214 "arm_shared_lib_excludes",
1215 ],
1216 arch: {
1217 arm: {
1218 exclude_shared_libs: [
1219 "arm_shared_lib_excludes",
1220 ],
1221 exclude_static_libs: [
1222 "arm_static_lib_excludes",
1223 "arm_whole_static_lib_excludes",
1224 ],
1225 },
1226 },
1227 product_variables: {
1228 malloc_not_svelte: {
1229 shared_libs: ["malloc_not_svelte_shared_lib"],
1230 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1231 exclude_static_libs: [
1232 "malloc_not_svelte_static_lib_excludes",
1233 "malloc_not_svelte_whole_static_lib_excludes",
1234 ],
1235 },
1236 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001237 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001238}
1239
1240cc_library {
1241 name: "arm_whole_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001242}
1243
1244cc_library {
1245 name: "malloc_not_svelte_whole_static_lib",
Liz Kammer47535c52021-06-02 16:02:22 -04001246}
1247
1248cc_library {
1249 name: "malloc_not_svelte_whole_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001250}
1251
1252cc_library {
1253 name: "arm_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001254}
1255
1256cc_library {
1257 name: "malloc_not_svelte_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001258}
1259
1260cc_library {
1261 name: "arm_shared_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001262}
1263
1264cc_library {
1265 name: "malloc_not_svelte_shared_lib",
Liz Kammer47535c52021-06-02 16:02:22 -04001266}
1267`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001268 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001269 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001270 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001271 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001272 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001273 "//build/bazel/product_config/config_settings:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001274 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001275 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001276 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001277 "//build/bazel/platforms/arch:arm": [],
1278 "//conditions:default": [":arm_shared_lib_excludes"],
1279 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001280 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
Liz Kammer7a210ac2021-09-22 15:52:58 -04001281 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001282 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001283 "srcs_c": `["common.c"]`,
1284 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001285 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001286 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001287 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001288 "//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 -04001289 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001290 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001291 }),
1292 },
1293 )
Liz Kammer47535c52021-06-02 16:02:22 -04001294}
Liz Kammerd366c902021-06-03 13:43:01 -04001295
Zi Wang0a8a1292022-08-30 06:27:01 +00001296func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1297 runCcLibraryTestCase(t, Bp2buildTestCase{
1298 ModuleTypeUnderTest: "cc_library",
1299 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1300 Filesystem: map[string]string{},
Chris Parsonscd209032023-09-19 01:12:48 +00001301 StubbedBuildDefinitions: []string{"malloc_not_svelte_header_lib"},
Zi Wang0a8a1292022-08-30 06:27:01 +00001302 Blueprint: soongCcLibraryStaticPreamble + `
1303cc_library {
1304 name: "foo_static",
1305 srcs: ["common.c"],
1306 product_variables: {
1307 malloc_not_svelte: {
1308 header_libs: ["malloc_not_svelte_header_lib"],
1309 },
1310 },
1311 include_build_directory: false,
1312}
1313
1314cc_library {
1315 name: "malloc_not_svelte_header_lib",
Zi Wang0a8a1292022-08-30 06:27:01 +00001316}
1317`,
1318 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1319 "implementation_deps": `select({
Cole Faust87c0c332023-07-31 12:10:12 -07001320 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
Zi Wang0a8a1292022-08-30 06:27:01 +00001321 "//conditions:default": [],
1322 })`,
1323 "srcs_c": `["common.c"]`,
1324 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1325 }),
1326 },
1327 )
1328}
1329
Liz Kammerd366c902021-06-03 13:43:01 -04001330func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001331 runCcLibraryTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001332 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001333 ModuleTypeUnderTest: "cc_library",
1334 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1335 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001336 "impl.cpp": "",
1337 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001338 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001339cc_library {
1340 name: "foo-lib",
1341 srcs: ["impl.cpp"],
1342 nocrt: true,
1343 include_build_directory: false,
1344}
1345`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001346 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001347 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001348 "srcs": `["impl.cpp"]`,
1349 }),
1350 },
1351 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001352}
1353
1354func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001355 runCcLibraryTestCase(t, Bp2buildTestCase{
1356 Description: "cc_library - nocrt: false - does not emit attribute",
1357 ModuleTypeUnderTest: "cc_library",
1358 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1359 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001360 "impl.cpp": "",
1361 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001362 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001363cc_library {
1364 name: "foo-lib",
1365 srcs: ["impl.cpp"],
1366 nocrt: false,
1367 include_build_directory: false,
1368}
1369`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001370 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001371 "srcs": `["impl.cpp"]`,
1372 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001373 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001374}
1375
1376func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001377 runCcLibraryTestCase(t, Bp2buildTestCase{
1378 Description: "cc_library - nocrt in select",
1379 ModuleTypeUnderTest: "cc_library",
1380 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1381 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001382 "impl.cpp": "",
1383 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001384 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001385cc_library {
1386 name: "foo-lib",
1387 srcs: ["impl.cpp"],
1388 arch: {
1389 arm: {
1390 nocrt: true,
1391 },
1392 x86: {
1393 nocrt: false,
1394 },
1395 },
1396 include_build_directory: false,
1397}
1398`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001399 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1400 "features": `select({
1401 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1402 "//conditions:default": [],
1403 })`,
1404 "srcs": `["impl.cpp"]`,
1405 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001406 })
1407}
1408
1409func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001410 runCcLibraryTestCase(t, Bp2buildTestCase{
1411 ModuleTypeUnderTest: "cc_library",
1412 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1413 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001414 "impl.cpp": "",
1415 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001416 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001417cc_library {
1418 name: "foo-lib",
1419 srcs: ["impl.cpp"],
1420 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001421 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001422}
1423`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001424 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001425 "features": `["-use_libcrt"]`,
1426 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001427 }),
1428 })
1429}
1430
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001431func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001432 STATIC_ONLY_ATTRS := map[string]bool{}
1433 SHARED_ONLY_ATTRS := map[string]bool{
1434 "link_crt": true,
1435 "additional_linker_inputs": true,
1436 "linkopts": true,
1437 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001438 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001439 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001440 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001441 }
Wei Li81852ca2022-07-27 00:22:06 -07001442
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001443 sharedAttrs := AttrNameToString{}
1444 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001445 for key, val := range attrs {
1446 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1447 sharedAttrs[key] = val
1448 }
1449 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1450 staticAttrs[key] = val
1451 }
1452 }
Alixe06d75b2022-08-31 18:28:19 +00001453 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1454 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001455
1456 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001457}
1458
Jingwen Chen6ada5892021-09-17 11:38:09 +00001459func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001460 runCcLibraryTestCase(t, Bp2buildTestCase{
1461 ModuleTypeUnderTest: "cc_library",
1462 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1463 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001464 "impl.cpp": "",
1465 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001466 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001467cc_library {
1468 name: "foo-lib",
1469 srcs: ["impl.cpp"],
1470 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001471 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001472}
1473`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001474 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001475 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001476 }),
1477 })
Liz Kammerd366c902021-06-03 13:43:01 -04001478}
1479
Jingwen Chen6ada5892021-09-17 11:38:09 +00001480func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001481 runCcLibraryTestCase(t, Bp2buildTestCase{
1482 ModuleTypeUnderTest: "cc_library",
1483 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1484 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001485 "impl.cpp": "",
1486 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001487 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001488cc_library {
1489 name: "foo-lib",
1490 srcs: ["impl.cpp"],
1491 arch: {
1492 arm: {
1493 no_libcrt: true,
1494 },
1495 x86: {
1496 no_libcrt: true,
1497 },
1498 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001499 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001500}
1501`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001502 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001503 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001504 "features": `select({
1505 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1506 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1507 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001508 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001509 }),
1510 })
Liz Kammerd366c902021-06-03 13:43:01 -04001511}
1512
Chris Parsons58852a02021-12-09 18:10:18 -05001513func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001514 runCcLibraryTestCase(t, Bp2buildTestCase{
1515 ModuleTypeUnderTest: "cc_library",
1516 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1517 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001518 "impl.cpp": "",
1519 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001520 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001521cc_library {
1522 name: "foo-lib",
1523 srcs: ["impl.cpp"],
1524 arch: {
1525 arm: {
1526 no_libcrt: true,
1527 },
1528 x86: {
1529 no_libcrt: true,
1530 },
1531 },
1532 target: {
1533 darwin: {
1534 no_libcrt: true,
1535 }
1536 },
1537 include_build_directory: false,
1538}
1539`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001540 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001541 "features": `select({
1542 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1543 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1544 "//conditions:default": [],
1545 }) + select({
1546 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1547 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001548 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001549 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001550 }),
1551 })
1552}
1553
1554func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001555 runCcLibraryTestCase(t, Bp2buildTestCase{
1556 ModuleTypeUnderTest: "cc_library",
1557 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1558 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001559 "impl.cpp": "",
1560 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001561 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001562cc_library {
1563 name: "foo-lib",
1564 srcs: ["impl.cpp"],
1565 arch: {
1566 arm: {
1567 no_libcrt: true,
1568 },
1569 // This is expected to override the value for darwin_x86_64.
1570 x86_64: {
1571 no_libcrt: true,
1572 },
1573 },
1574 target: {
1575 darwin: {
1576 no_libcrt: false,
1577 }
1578 },
1579 include_build_directory: false,
1580}
1581`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001582 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001583 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001584 "features": `select({
1585 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1586 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1587 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001588 })`,
1589 }),
1590 })
1591}
1592
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001593func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001594 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001595 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001596 "strip": `{
1597 "all": True,
1598 }`,
1599 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001600 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001601 "strip": `{
1602 "keep_symbols": True,
1603 }`,
1604 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001605 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001606 "strip": `{
1607 "keep_symbols_and_debug_frame": True,
1608 }`,
1609 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001610 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001611 "strip": `{
1612 "keep_symbols_list": ["symbol"],
1613 }`,
1614 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001615 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001616 "strip": `{
1617 "none": True,
1618 }`,
1619 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001620 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001621
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001622 runCcLibraryTestCase(t, Bp2buildTestCase{
1623 Description: "cc_library strip args",
1624 ModuleTypeUnderTest: "cc_library",
1625 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1626 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001627cc_library {
1628 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001629 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001630}
1631cc_library {
1632 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001633 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001634 keep_symbols: true,
1635 },
1636 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001637}
1638cc_library {
1639 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001640 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001641 keep_symbols_and_debug_frame: true,
1642 },
1643 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001644}
1645cc_library {
1646 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001647 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001648 none: true,
1649 },
1650 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001651}
1652cc_library {
1653 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001654 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001655 keep_symbols_list: ["symbol"],
1656 },
1657 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001658}
1659cc_library {
1660 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001661 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001662 all: true,
1663 },
1664 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001665}
1666`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001667 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001668 })
1669}
1670
1671func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001672 runCcLibraryTestCase(t, Bp2buildTestCase{
1673 Description: "cc_library strip args",
1674 ModuleTypeUnderTest: "cc_library",
1675 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1676 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001677cc_library {
1678 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001679 target: {
1680 darwin: {
1681 strip: {
1682 keep_symbols_list: ["foo", "bar"]
1683 }
1684 },
1685 },
1686 arch: {
1687 arm: {
1688 strip: {
1689 keep_symbols_and_debug_frame: true,
1690 },
1691 },
1692 arm64: {
1693 strip: {
1694 keep_symbols: true,
1695 },
1696 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001697 },
1698 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001699}
1700`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001701 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001702 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001703 "keep_symbols": select({
1704 "//build/bazel/platforms/arch:arm64": True,
1705 "//conditions:default": None,
1706 }),
1707 "keep_symbols_and_debug_frame": select({
1708 "//build/bazel/platforms/arch:arm": True,
1709 "//conditions:default": None,
1710 }),
1711 "keep_symbols_list": select({
1712 "//build/bazel/platforms/os:darwin": [
1713 "foo",
1714 "bar",
1715 ],
1716 "//conditions:default": [],
1717 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001718 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001719 }),
1720 },
1721 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001722}
Chris Parsons51f8c392021-08-03 21:01:05 -04001723
1724func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001725 runCcLibraryTestCase(t, Bp2buildTestCase{
1726 Description: "cc_library system_shared_libs empty at root",
1727 ModuleTypeUnderTest: "cc_library",
1728 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1729 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001730cc_library {
1731 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001732 system_shared_libs: [],
1733 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001734}
1735`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001736 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001737 "system_dynamic_deps": `[]`,
1738 }),
1739 },
1740 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001741}
1742
1743func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001744 runCcLibraryTestCase(t, Bp2buildTestCase{
1745 Description: "cc_library system_shared_libs empty for static variant",
1746 ModuleTypeUnderTest: "cc_library",
1747 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1748 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001749cc_library {
1750 name: "static_empty",
1751 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001752 system_shared_libs: [],
1753 },
1754 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001755}
1756`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001757 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001758 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001759 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001760 }),
Alixe06d75b2022-08-31 18:28:19 +00001761 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001762 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001763 })
1764}
1765
1766func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001767 runCcLibraryTestCase(t, Bp2buildTestCase{
1768 Description: "cc_library system_shared_libs empty for shared variant",
1769 ModuleTypeUnderTest: "cc_library",
1770 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1771 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001772cc_library {
1773 name: "shared_empty",
1774 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001775 system_shared_libs: [],
1776 },
1777 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001778}
1779`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001780 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001781 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1782 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001783 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001784 }),
1785 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001786 })
1787}
1788
1789func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001790 runCcLibraryTestCase(t, Bp2buildTestCase{
1791 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1792 ModuleTypeUnderTest: "cc_library",
1793 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1794 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001795cc_library {
1796 name: "shared_empty",
1797 target: {
1798 bionic: {
1799 shared: {
1800 system_shared_libs: [],
1801 }
1802 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001803 },
1804 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001805}
1806`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001807 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001808 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1809 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001810 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001811 }),
1812 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001813 })
1814}
1815
1816func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1817 // Note that this behavior is technically incorrect (it's a simplification).
1818 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1819 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1820 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001821 runCcLibraryTestCase(t, Bp2buildTestCase{
1822 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1823 ModuleTypeUnderTest: "cc_library",
1824 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001825 StubbedBuildDefinitions: []string{"libc_musl"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001826 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001827cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001828 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001829}
1830
1831cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001832 name: "target_linux_bionic_empty",
1833 target: {
1834 linux_bionic: {
1835 system_shared_libs: [],
1836 },
1837 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001838 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001839}
1840`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001841 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001842 "system_dynamic_deps": `select({
1843 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1844 "//conditions:default": [],
1845 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001846 }),
1847 },
1848 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001849}
1850
1851func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001852 runCcLibraryTestCase(t, Bp2buildTestCase{
1853 Description: "cc_library system_shared_libs empty for bionic variant",
1854 ModuleTypeUnderTest: "cc_library",
1855 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001856 StubbedBuildDefinitions: []string{"libc_musl"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001857 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001858cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001859 name: "libc_musl",
1860 bazel_module: { bp2build_available: false },
1861}
1862
1863cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001864 name: "target_bionic_empty",
1865 target: {
1866 bionic: {
1867 system_shared_libs: [],
1868 },
1869 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001870 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001871}
1872`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001873 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001874 "system_dynamic_deps": `select({
1875 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1876 "//conditions:default": [],
1877 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001878 }),
1879 },
1880 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001881}
1882
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001883func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1884 runCcLibraryTestCase(t, Bp2buildTestCase{
1885 Description: "cc_library system_shared_lib empty for musl variant",
1886 ModuleTypeUnderTest: "cc_library",
1887 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001888 StubbedBuildDefinitions: []string{"libc_musl"},
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001889 Blueprint: soongCcLibraryPreamble + `
1890cc_library {
1891 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001892}
1893
1894cc_library {
1895 name: "target_musl_empty",
1896 target: {
1897 musl: {
1898 system_shared_libs: [],
1899 },
1900 },
1901 include_build_directory: false,
1902}
1903`,
1904 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1905 "system_dynamic_deps": `[]`,
1906 }),
1907 })
1908}
1909
1910func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1911 runCcLibraryTestCase(t, Bp2buildTestCase{
1912 Description: "cc_library system_shared_lib empty for linux_musl variant",
1913 ModuleTypeUnderTest: "cc_library",
1914 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1915 Blueprint: soongCcLibraryPreamble + `
1916cc_library {
1917 name: "libc_musl",
1918 bazel_module: { bp2build_available: false },
1919}
1920
1921cc_library {
1922 name: "target_linux_musl_empty",
1923 target: {
1924 linux_musl: {
1925 system_shared_libs: [],
1926 },
1927 },
1928 include_build_directory: false,
1929}
1930`,
1931 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1932 "system_dynamic_deps": `[]`,
1933 }),
1934 })
1935}
Chris Parsons51f8c392021-08-03 21:01:05 -04001936func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001937 runCcLibraryTestCase(t, Bp2buildTestCase{
1938 Description: "cc_library system_shared_libs set for shared and root",
1939 ModuleTypeUnderTest: "cc_library",
1940 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001941 StubbedBuildDefinitions: []string{"libc", "libm"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001942 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001943cc_library {
1944 name: "libc",
Liz Kammer8337ea42021-09-10 10:06:32 -04001945}
1946cc_library {
1947 name: "libm",
Liz Kammer8337ea42021-09-10 10:06:32 -04001948}
Chris Parsons51f8c392021-08-03 21:01:05 -04001949
1950cc_library {
1951 name: "foo",
1952 system_shared_libs: ["libc"],
1953 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001954 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001955 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001956 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001957}
1958`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001959 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001960 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001961 "system_dynamic_deps": `[":libc"]`,
1962 }),
Alixe06d75b2022-08-31 18:28:19 +00001963 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001964 "system_dynamic_deps": `[
1965 ":libc",
1966 ":libm",
1967 ]`,
1968 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001969 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001970 })
1971}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001972
1973func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001974 runCcLibraryTestCase(t, Bp2buildTestCase{
1975 Description: "cc_library - selects for all os targets",
1976 ModuleTypeUnderTest: "cc_library",
1977 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1978 Filesystem: map[string]string{},
1979 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001980cc_library {
1981 name: "foo-lib",
1982 srcs: ["base.cpp"],
1983 target: {
1984 android: {
1985 srcs: ["android.cpp"],
1986 },
1987 linux: {
1988 srcs: ["linux.cpp"],
1989 },
1990 linux_glibc: {
1991 srcs: ["linux_glibc.cpp"],
1992 },
1993 darwin: {
1994 srcs: ["darwin.cpp"],
1995 },
1996 bionic: {
1997 srcs: ["bionic.cpp"],
1998 },
1999 linux_musl: {
2000 srcs: ["linux_musl.cpp"],
2001 },
2002 windows: {
2003 srcs: ["windows.cpp"],
2004 },
2005 },
2006 include_build_directory: false,
2007}
2008`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002009 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002010 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002011 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002012 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002013 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002014 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002015 ],
2016 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002017 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002018 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002019 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002020 ],
Colin Cross133782e2022-12-20 15:29:31 -08002021 "//build/bazel/platforms/os:linux_glibc": [
2022 "linux.cpp",
2023 "linux_glibc.cpp",
2024 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002025 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002026 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002027 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002028 ],
2029 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2030 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002031 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002032 }),
2033 },
2034 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002035}
2036
Yu Liu75be7b92022-02-01 09:54:09 -08002037func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002038 runCcLibraryTestCase(t, Bp2buildTestCase{
2039 Description: "cc_library - libcrypto hash injection",
2040 ModuleTypeUnderTest: "cc_library",
2041 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2042 Filesystem: map[string]string{},
2043 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002044cc_library {
2045 name: "libcrypto",
2046 target: {
2047 android: {
2048 inject_bssl_hash: true,
2049 },
2050 },
2051 include_build_directory: false,
2052}
2053`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002054 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002055 "inject_bssl_hash": `select({
2056 "//build/bazel/platforms/os:android": True,
2057 "//conditions:default": None,
2058 })`,
2059 }),
2060 },
2061 )
2062}
2063
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002064func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002065 type testCase struct {
2066 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002067 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002068 gnu_extensions string
2069 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002070 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002071 }
2072
2073 testCases := []testCase{
2074 // Existing usages of cpp_std in AOSP are:
2075 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2076 //
2077 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2078 // is set in the toolchain.)
2079 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002080 {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 +00002081 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2082 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002083 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2084 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2085 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002086 // Explicitly setting a c++ std does not use replace gnu++ std even if
2087 // gnu_extensions is true.
2088 // "c++11",
2089 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002090 {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 +00002091 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2092 // "c++17",
2093 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002094 {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 +00002095 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2096 // "c++2a",
2097 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002098 {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 +00002099 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2100 // "c++98",
2101 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002102 {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 +00002103 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2104 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2105 // "gnu++11",
2106 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002107 {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 +00002108 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2109 // "gnu++17",
2110 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002111 {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 +00002112 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002113
2114 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002115 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2116 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2117 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002118 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2119 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2120 {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 +00002121 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002122 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002123 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2124 t.Run(name, func(t *testing.T) {
2125 name_prefix := fmt.Sprintf("a_%v", i)
2126 cppStdProp := ""
2127 if tc.cpp_std != "" {
2128 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2129 }
2130 cStdProp := ""
2131 if tc.c_std != "" {
2132 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2133 }
2134 gnuExtensionsProp := ""
2135 if tc.gnu_extensions != "" {
2136 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2137 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002138 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002139 if tc.bazel_cpp_std != "" {
2140 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2141 }
2142 if tc.bazel_c_std != "" {
2143 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2144 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002145
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002146 runCcLibraryTestCase(t, Bp2buildTestCase{
2147 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002148 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002149 ModuleTypeUnderTest: "cc_library",
2150 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2151 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002152cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002153 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002154%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002155%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002156%s // gnu_extensions: *bool
2157 include_build_directory: false,
2158}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002159`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002160 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002161 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002162
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002163 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2164 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002165 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002166 ModuleTypeUnderTest: "cc_library_static",
2167 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2168 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002169cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002170 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002171%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002172%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002173%s // gnu_extensions: *bool
2174 include_build_directory: false,
2175}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002176`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002177 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002178 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002179 },
2180 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002181
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002182 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2183 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002184 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002185 ModuleTypeUnderTest: "cc_library_shared",
2186 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2187 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002188cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002189 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002190%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002191%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002192%s // gnu_extensions: *bool
2193 include_build_directory: false,
2194}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002195`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002196 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002197 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002198 },
2199 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002200 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002201 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002202}
Liz Kammer12615db2021-09-28 09:19:17 -04002203
2204func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002205 runCcLibraryTestCase(t, Bp2buildTestCase{
2206 ModuleTypeUnderTest: "cc_library",
2207 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2208 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002209 name: "foo",
2210 srcs: ["foo.proto"],
2211 include_build_directory: false,
2212}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002213 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002214 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002215 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002216 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002217 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002218 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002219 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002220 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002221 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002222 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2223 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002224 }),
2225 },
2226 })
2227}
2228
2229func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002230 runCcLibraryTestCase(t, Bp2buildTestCase{
2231 ModuleTypeUnderTest: "cc_library",
2232 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2233 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002234 name: "foo",
2235 srcs: ["foo.proto"],
2236 proto: { canonical_path_from_root: false},
2237 include_build_directory: false,
2238}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002239 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002240 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002241 "srcs": `["foo.proto"]`,
2242 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002243 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002244 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002245 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002246 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002247 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002248 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002249 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2250 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002251 }),
2252 },
2253 })
2254}
2255
2256func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002257 runCcLibraryTestCase(t, Bp2buildTestCase{
2258 ModuleTypeUnderTest: "cc_library",
2259 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2260 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002261 name: "foo",
2262 srcs: ["foo.proto"],
2263 proto: { canonical_path_from_root: true},
2264 include_build_directory: false,
2265}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002266 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002267 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002268 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002269 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002270 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002271 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002272 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002273 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002274 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002275 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2276 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002277 }),
2278 },
2279 })
2280}
2281
2282func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002283 runCcLibraryTestCase(t, Bp2buildTestCase{
2284 ModuleTypeUnderTest: "cc_library",
2285 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2286 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002287 name: "foo",
2288 srcs: ["foo.proto"],
2289 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002290 type: "full",
2291 },
2292 include_build_directory: false,
2293}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002294 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002295 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002296 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002297 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002298 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002299 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002300 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002301 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002302 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002303 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2304 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002305 }),
2306 },
2307 })
2308}
2309
2310func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002311 runCcLibraryTestCase(t, Bp2buildTestCase{
2312 ModuleTypeUnderTest: "cc_library",
2313 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2314 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002315 name: "foo",
2316 srcs: ["foo.proto"],
2317 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002318 type: "lite",
2319 },
2320 include_build_directory: false,
2321}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002322 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002323 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002324 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002325 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002326 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002327 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002328 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002329 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002330 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002331 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2332 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002333 }),
2334 },
2335 })
2336}
2337
2338func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002339 runCcLibraryTestCase(t, Bp2buildTestCase{
2340 ModuleTypeUnderTest: "cc_library",
2341 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2342 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002343 name: "foo",
2344 srcs: ["foo.proto"],
2345 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002346 export_proto_headers: true,
2347 },
2348 include_build_directory: false,
2349}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002350 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002351 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002352 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002353 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002354 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002355 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002356 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002357 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002358 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002359 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2360 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002361 }),
2362 },
2363 })
2364}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002365
Yu Liu2d136142022-08-18 14:46:13 -07002366func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2367 runCcLibraryTestCase(t, Bp2buildTestCase{
2368 ModuleTypeUnderTest: "cc_library",
2369 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2370 Blueprint: soongCcProtoPreamble + `cc_library {
2371 name: "foo",
2372 srcs: ["foo.proto"],
2373 proto: {
2374 include_dirs: ["external/protobuf/src"],
2375 },
2376 include_build_directory: false,
2377}`,
2378 ExpectedBazelTargets: []string{
2379 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2380 "srcs": `["foo.proto"]`,
2381 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2382 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2383 "deps": `[":foo_proto"]`,
2384 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2385 "deps": `[":libprotobuf-cpp-lite"]`,
2386 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2387 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002388 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2389 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002390 }),
2391 },
2392 })
2393}
2394
2395func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2396 runCcLibraryTestCase(t, Bp2buildTestCase{
2397 ModuleTypeUnderTest: "cc_library",
2398 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2399 Blueprint: soongCcProtoPreamble + `cc_library {
2400 name: "foo",
2401 srcs: ["foo.proto"],
2402 proto: {
2403 include_dirs: ["external/protobuf/abc"],
2404 },
2405 include_build_directory: false,
2406}`,
Spandan Dasec39d512023-08-15 22:08:18 +00002407 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 -07002408 })
2409}
2410
Yu Liu2aa806b2022-09-01 11:54:47 -07002411func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2412 runCcLibraryTestCase(t, Bp2buildTestCase{
2413 ModuleTypeUnderTest: "cc_library",
2414 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2415 Blueprint: soongCcProtoPreamble + `
2416filegroup {
2417 name: "a_fg_proto",
2418 srcs: ["a_fg.proto"],
2419}
2420
2421cc_library {
2422 name: "a",
2423 srcs: [
2424 ":a_fg_proto",
2425 "a.proto",
2426 ],
2427 proto: {
2428 export_proto_headers: true,
2429 },
2430 include_build_directory: false,
2431}`,
2432 ExpectedBazelTargets: []string{
2433 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002434 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002435 "srcs": `["a.proto"]`,
2436 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2437 "deps": `[
2438 ":a_fg_proto_bp2build_converted",
2439 ":a_proto",
2440 ]`,
2441 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2442 "deps": `[":libprotobuf-cpp-lite"]`,
2443 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2444 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2445 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2446 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002447 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002448 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002449 "tags": `[
2450 "apex_available=//apex_available:anyapex",
2451 "manual",
2452 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002453 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2454 "actual": `"//.:a_fg_proto_proto"`,
2455 "tags": `[
2456 "apex_available=//apex_available:anyapex",
2457 "manual",
2458 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002459 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2460 "srcs": `["a_fg.proto"]`,
2461 }),
2462 },
2463 })
2464}
2465
2466func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2467 runCcLibraryTestCase(t, Bp2buildTestCase{
2468 ModuleTypeUnderTest: "cc_library",
2469 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2470 Blueprint: soongCcProtoPreamble + `
2471filegroup {
2472 name: "a_fg_proto",
2473 srcs: ["a_fg.proto"],
2474}
2475
2476cc_library {
2477 name: "a",
2478 srcs: [
2479 ":a_fg_proto",
2480 ],
2481 proto: {
2482 export_proto_headers: true,
2483 },
2484 include_build_directory: false,
2485}`,
2486 ExpectedBazelTargets: []string{
2487 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2488 "deps": `[":a_fg_proto_bp2build_converted"]`,
2489 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2490 "deps": `[":libprotobuf-cpp-lite"]`,
2491 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2492 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2493 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2494 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002495 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002496 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002497 "tags": `[
2498 "apex_available=//apex_available:anyapex",
2499 "manual",
2500 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002501 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2502 "actual": `"//.:a_fg_proto_proto"`,
2503 "tags": `[
2504 "apex_available=//apex_available:anyapex",
2505 "manual",
2506 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002507 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2508 "srcs": `["a_fg.proto"]`,
2509 }),
2510 },
2511 })
2512}
2513
2514func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2515 runCcLibraryTestCase(t, Bp2buildTestCase{
2516 ModuleTypeUnderTest: "cc_library",
2517 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002518 StubbedBuildDefinitions: []string{"//path/to/A:a_fg_proto"},
Yu Liu2aa806b2022-09-01 11:54:47 -07002519 Filesystem: map[string]string{
2520 "path/to/A/Android.bp": `
2521filegroup {
2522 name: "a_fg_proto",
2523 srcs: ["a_fg.proto"],
2524}`,
2525 },
2526 Blueprint: soongCcProtoPreamble + `
2527cc_library {
2528 name: "a",
2529 srcs: [
2530 ":a_fg_proto",
2531 "a.proto",
2532 ],
2533 proto: {
2534 export_proto_headers: true,
2535 },
2536 include_build_directory: false,
2537}`,
2538 ExpectedBazelTargets: []string{
2539 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002540 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002541 "srcs": `["a.proto"]`,
2542 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2543 "deps": `[
2544 "//path/to/A:a_fg_proto_bp2build_converted",
2545 ":a_proto",
2546 ]`,
2547 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2548 "deps": `[":libprotobuf-cpp-lite"]`,
2549 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2550 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2551 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2552 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2553 }),
2554 },
2555 })
2556}
2557
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002558func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002559 runCcLibraryTestCase(t, Bp2buildTestCase{
2560 ModuleTypeUnderTest: "cc_library",
2561 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002562 StubbedBuildDefinitions: []string{"a_fg_proto", "b_protos", "c-proto-srcs", "proto-srcs-d"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002563 Blueprint: soongCcProtoPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +00002564 simpleModule("filegroup", "a_fg_proto") +
2565 simpleModule("filegroup", "b_protos") +
2566 simpleModule("filegroup", "c-proto-srcs") +
2567 simpleModule("filegroup", "proto-srcs-d") + `
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002568cc_library {
2569 name: "a",
2570 srcs: [":a_fg_proto"],
2571 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002572 export_proto_headers: true,
2573 },
2574 include_build_directory: false,
2575}
2576
2577cc_library {
2578 name: "b",
2579 srcs: [":b_protos"],
2580 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002581 export_proto_headers: true,
2582 },
2583 include_build_directory: false,
2584}
2585
2586cc_library {
2587 name: "c",
2588 srcs: [":c-proto-srcs"],
2589 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002590 export_proto_headers: true,
2591 },
2592 include_build_directory: false,
2593}
2594
2595cc_library {
2596 name: "d",
2597 srcs: [":proto-srcs-d"],
2598 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002599 export_proto_headers: true,
2600 },
2601 include_build_directory: false,
2602}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002603 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002604 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002605 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002606 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002607 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002608 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002609 "deps": `[":libprotobuf-cpp-lite"]`,
2610 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2611 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2612 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2613 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002614 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002615 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2616 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2617 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2618 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2619 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002620 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002621 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002622 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002623 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002624 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002625 "deps": `[":libprotobuf-cpp-lite"]`,
2626 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2627 "srcs": `[":b_protos_cpp_srcs"]`,
2628 "srcs_as": `[":b_protos_as_srcs"]`,
2629 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002630 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002631 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2632 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2633 "srcs": `[":b_protos_cpp_srcs"]`,
2634 "srcs_as": `[":b_protos_as_srcs"]`,
2635 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002636 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002637 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002638 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002639 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002640 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002641 "deps": `[":libprotobuf-cpp-lite"]`,
2642 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2643 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2644 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2645 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002646 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002647 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2648 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2649 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2650 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2651 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002652 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002653 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002654 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002655 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002656 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002657 "deps": `[":libprotobuf-cpp-lite"]`,
2658 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2659 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2660 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2661 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002662 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002663 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2664 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2665 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2666 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2667 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2668 }),
2669 },
2670 })
2671}
Chris Parsons58852a02021-12-09 18:10:18 -05002672
2673func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002674 runCcLibraryTestCase(t, Bp2buildTestCase{
2675 ModuleTypeUnderTest: "cc_library",
2676 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2677 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002678 name: "foo",
2679 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002680 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002681 target: {
2682 darwin: {
2683 enabled: false,
2684 },
2685 windows: {
2686 enabled: false,
2687 },
2688 linux_glibc_x86: {
2689 enabled: false,
2690 },
2691 },
2692 include_build_directory: false,
2693}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002694 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002695 "srcs": `["foo.cpp"]`,
2696 "target_compatible_with": `select({
2697 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2698 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2699 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2700 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2701 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2702 "//conditions:default": [],
2703 })`,
2704 }),
2705 })
2706}
2707
2708func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002709 runCcLibraryTestCase(t, Bp2buildTestCase{
2710 ModuleTypeUnderTest: "cc_library",
2711 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2712 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002713 name: "foo",
2714 srcs: ["foo.cpp"],
2715 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002716 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002717 target: {
2718 darwin: {
2719 enabled: true,
2720 },
2721 windows: {
2722 enabled: false,
2723 },
2724 linux_glibc_x86: {
2725 enabled: false,
2726 },
2727 },
2728 include_build_directory: false,
2729}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002730 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002731 "srcs": `["foo.cpp"]`,
2732 "target_compatible_with": `select({
2733 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2734 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2735 "//conditions:default": ["@platforms//:incompatible"],
2736 })`,
2737 }),
2738 })
2739}
2740
2741func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002742 runCcLibraryTestCase(t, Bp2buildTestCase{
2743 ModuleTypeUnderTest: "cc_library",
2744 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2745 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002746 name: "foo",
2747 srcs: ["foo.cpp"],
2748 enabled: false,
2749 shared: {
2750 enabled: true,
2751 },
2752 target: {
2753 android: {
2754 shared: {
2755 enabled: false,
2756 },
2757 }
2758 },
2759 include_build_directory: false,
2760}`,
Alixe06d75b2022-08-31 18:28:19 +00002761 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002762 "srcs": `["foo.cpp"]`,
2763 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002764 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002765 "srcs": `["foo.cpp"]`,
2766 "target_compatible_with": `select({
2767 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2768 "//conditions:default": [],
2769 })`,
2770 }),
2771 },
2772 })
2773}
2774
2775func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002776 runCcLibraryTestCase(t, Bp2buildTestCase{
2777 ModuleTypeUnderTest: "cc_library",
2778 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2779 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002780 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002781 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002782 srcs: ["foo.cpp"],
2783 shared: {
2784 enabled: false
2785 },
2786 target: {
2787 darwin: {
2788 enabled: true,
2789 },
2790 windows: {
2791 enabled: false,
2792 },
2793 linux_glibc_x86: {
2794 shared: {
2795 enabled: true,
2796 },
2797 },
2798 },
2799 include_build_directory: false,
2800}`,
Alixe06d75b2022-08-31 18:28:19 +00002801 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002802 "srcs": `["foo.cpp"]`,
2803 "target_compatible_with": `select({
2804 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2805 "//conditions:default": [],
2806 })`,
Alixe06d75b2022-08-31 18:28:19 +00002807 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002808 "srcs": `["foo.cpp"]`,
2809 "target_compatible_with": `select({
2810 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2811 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2812 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2813 "//conditions:default": ["@platforms//:incompatible"],
2814 })`,
2815 }),
2816 }})
2817}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002818
2819func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002820 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002821 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002822 })
2823 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Spandan Das04f9f4c2023-09-13 23:59:05 +00002824 "api_surface": `"module-libapi"`,
Sam Delmerico5f906492023-03-15 18:06:18 -04002825 "soname": `"a.so"`,
2826 "source_library_label": `"//foo/bar:a"`,
2827 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002828 "stubs_versions": `[
2829 "28",
2830 "29",
2831 "current",
2832 ]`,
2833 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002834 runCcLibraryTestCase(t, Bp2buildTestCase{
2835 Description: "cc_library stubs",
2836 ModuleTypeUnderTest: "cc_library",
2837 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2838 Dir: "foo/bar",
2839 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002840 "foo/bar/Android.bp": `
2841cc_library {
2842 name: "a",
2843 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2844 bazel_module: { bp2build_available: true },
2845 include_build_directory: false,
2846}
2847`,
2848 },
Wei Li81852ca2022-07-27 00:22:06 -07002849 Blueprint: soongCcLibraryPreamble,
2850 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002851 },
2852 )
2853}
Liz Kammerf38a8372022-02-04 15:39:00 -05002854
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002855func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2856 runCcLibraryTestCase(t, Bp2buildTestCase{
2857 Description: "stub target generation of the same lib across configs should not result in duplicates",
2858 ModuleTypeUnderTest: "cc_library",
2859 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2860 Filesystem: map[string]string{
2861 "bar.map.txt": "",
2862 },
Chris Parsonscd209032023-09-19 01:12:48 +00002863 StubbedBuildDefinitions: []string{"barlib"},
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002864 Blueprint: `
2865cc_library {
2866 name: "barlib",
2867 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002868}
2869cc_library {
2870 name: "foolib",
2871 shared_libs: ["barlib"],
2872 target: {
2873 android: {
2874 shared_libs: ["barlib"],
2875 },
2876 },
2877 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002878 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002879}`,
2880 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2881 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00002882 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002883 "//conditions:default": [":barlib"],
2884 })`,
2885 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002886 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002887 }),
2888 })
2889}
2890
Liz Kammerffc17e42022-11-23 09:42:05 -05002891func TestCcLibraryExcludesLibsHost(t *testing.T) {
2892 runCcLibraryTestCase(t, Bp2buildTestCase{
2893 ModuleTypeUnderTest: "cc_library",
2894 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2895 Filesystem: map[string]string{
2896 "bar.map.txt": "",
2897 },
Chris Parsonscd209032023-09-19 01:12:48 +00002898 StubbedBuildDefinitions: []string{"bazlib", "quxlib", "barlib"},
2899 Blueprint: simpleModule("cc_library", "bazlib") + `
Liz Kammerffc17e42022-11-23 09:42:05 -05002900cc_library {
2901 name: "quxlib",
2902 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002903}
2904cc_library {
2905 name: "barlib",
2906 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002907}
2908cc_library {
2909 name: "foolib",
2910 shared_libs: ["barlib", "quxlib"],
2911 target: {
2912 host: {
2913 shared_libs: ["bazlib"],
2914 exclude_shared_libs: ["barlib"],
2915 },
2916 },
2917 include_build_directory: false,
2918 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002919 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002920}`,
2921 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2922 "implementation_dynamic_deps": `select({
2923 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002924 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002925 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002926 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
2927 "//build/bazel/platforms/os:windows": [":bazlib"],
2928 "//conditions:default": [],
2929 }) + select({
2930 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002931 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002932 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002933 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
2934 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00002935 "//build/bazel/rules/apex:foo": [
2936 "@api_surfaces//module-libapi/current:barlib",
2937 "@api_surfaces//module-libapi/current:quxlib",
2938 ],
Liz Kammerffc17e42022-11-23 09:42:05 -05002939 "//conditions:default": [
2940 ":barlib",
2941 ":quxlib",
2942 ],
2943 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002944 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05002945 }),
2946 })
2947}
2948
Liz Kammerf38a8372022-02-04 15:39:00 -05002949func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002950 runCcLibraryTestCase(t, Bp2buildTestCase{
2951 ModuleTypeUnderTest: "cc_library",
2952 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2953 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05002954 name: "foo",
2955 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2956 include_build_directory: false,
2957}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002958 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05002959 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2960 }),
2961 })
2962}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002963
2964func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002965 runCcLibraryTestCase(t, Bp2buildTestCase{
2966 ModuleTypeUnderTest: "cc_library",
2967 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2968 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002969 "foo.c": "",
2970 "bar.cc": "",
2971 "foo1.l": "",
2972 "bar1.ll": "",
2973 "foo2.l": "",
2974 "bar2.ll": "",
2975 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002976 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002977 name: "foo_lib",
2978 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2979 lex: { flags: ["--foo_flags"] },
2980 include_build_directory: false,
2981 bazel_module: { bp2build_available: true },
2982}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002983 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00002984 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002985 "srcs": `[
2986 "foo1.l",
2987 "foo2.l",
2988 ]`,
2989 "lexopts": `["--foo_flags"]`,
2990 }),
Alixe06d75b2022-08-31 18:28:19 +00002991 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002992 "srcs": `[
2993 "bar1.ll",
2994 "bar2.ll",
2995 ]`,
2996 "lexopts": `["--foo_flags"]`,
2997 }),
2998 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002999 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003000 "srcs": `[
3001 "bar.cc",
3002 ":foo_lib_genlex_ll",
3003 ]`,
3004 "srcs_c": `[
3005 "foo.c",
3006 ":foo_lib_genlex_l",
3007 ]`,
3008 })...),
3009 })
3010}
Cole Faust6b29f592022-08-09 09:50:56 -07003011
3012func TestCCLibraryRuntimeDeps(t *testing.T) {
3013 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3014 Blueprint: `cc_library_shared {
3015 name: "bar",
3016}
3017
3018cc_library {
3019 name: "foo",
Chris Parsonscd209032023-09-19 01:12:48 +00003020 runtime_libs: ["bar"],
Cole Faust6b29f592022-08-09 09:50:56 -07003021}`,
3022 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003023 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003024 "local_includes": `["."]`,
3025 }),
Alixe06d75b2022-08-31 18:28:19 +00003026 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003027 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003028 "local_includes": `["."]`,
3029 }),
Alixe06d75b2022-08-31 18:28:19 +00003030 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003031 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003032 "local_includes": `["."]`,
3033 }),
3034 },
3035 })
3036}
Cole Faust5fa4e962022-08-22 14:31:04 -07003037
3038func TestCcLibraryWithInstructionSet(t *testing.T) {
3039 runCcLibraryTestCase(t, Bp2buildTestCase{
3040 ModuleTypeUnderTest: "cc_library",
3041 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3042 Blueprint: `cc_library {
3043 name: "foo",
3044 arch: {
3045 arm: {
3046 instruction_set: "arm",
3047 }
3048 }
3049}
3050`,
3051 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3052 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003053 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003054 "//conditions:default": [],
3055 })`,
3056 "local_includes": `["."]`,
3057 }),
3058 })
3059}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003060
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003061func TestCcLibraryEmptySuffix(t *testing.T) {
3062 runCcLibraryTestCase(t, Bp2buildTestCase{
3063 Description: "cc_library with empty suffix",
3064 ModuleTypeUnderTest: "cc_library",
3065 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3066 Filesystem: map[string]string{
3067 "foo.c": "",
3068 },
3069 Blueprint: `cc_library {
3070 name: "foo",
3071 suffix: "",
3072 srcs: ["foo.c"],
3073 include_build_directory: false,
3074}`,
3075 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003076 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 -05003077 "srcs_c": `["foo.c"]`,
3078 }),
Alixe06d75b2022-08-31 18:28:19 +00003079 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003080 "srcs_c": `["foo.c"]`,
3081 "suffix": `""`,
3082 }),
3083 },
3084 })
3085}
3086
3087func TestCcLibrarySuffix(t *testing.T) {
3088 runCcLibraryTestCase(t, Bp2buildTestCase{
3089 Description: "cc_library with suffix",
3090 ModuleTypeUnderTest: "cc_library",
3091 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3092 Filesystem: map[string]string{
3093 "foo.c": "",
3094 },
3095 Blueprint: `cc_library {
3096 name: "foo",
3097 suffix: "-suf",
3098 srcs: ["foo.c"],
3099 include_build_directory: false,
3100}`,
3101 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003102 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 -05003103 "srcs_c": `["foo.c"]`,
3104 }),
Alixe06d75b2022-08-31 18:28:19 +00003105 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003106 "srcs_c": `["foo.c"]`,
3107 "suffix": `"-suf"`,
3108 }),
3109 },
3110 })
3111}
3112
3113func TestCcLibraryArchVariantSuffix(t *testing.T) {
3114 runCcLibraryTestCase(t, Bp2buildTestCase{
3115 Description: "cc_library with arch-variant suffix",
3116 ModuleTypeUnderTest: "cc_library",
3117 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3118 Filesystem: map[string]string{
3119 "foo.c": "",
3120 },
3121 Blueprint: `cc_library {
3122 name: "foo",
3123 arch: {
3124 arm64: { suffix: "-64" },
3125 arm: { suffix: "-32" },
3126 },
3127 srcs: ["foo.c"],
3128 include_build_directory: false,
3129}`,
3130 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003131 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 -05003132 "srcs_c": `["foo.c"]`,
3133 }),
Alixe06d75b2022-08-31 18:28:19 +00003134 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003135 "srcs_c": `["foo.c"]`,
3136 "suffix": `select({
3137 "//build/bazel/platforms/arch:arm": "-32",
3138 "//build/bazel/platforms/arch:arm64": "-64",
3139 "//conditions:default": None,
3140 })`,
3141 }),
3142 },
3143 })
3144}
3145
Vinh Tran367d89d2023-04-28 11:21:25 -04003146func TestCcLibraryWithAidlLibrary(t *testing.T) {
3147 runCcLibraryTestCase(t, Bp2buildTestCase{
3148 Description: "cc_library with aidl_library",
3149 ModuleTypeUnderTest: "cc_library",
3150 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3151 Blueprint: `
3152aidl_library {
3153 name: "A_aidl",
3154 srcs: ["aidl/A.aidl"],
3155 hdrs: ["aidl/Header.aidl"],
3156 strip_import_prefix: "aidl",
3157}
3158cc_library {
3159 name: "foo",
3160 aidl: {
3161 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003162 },
3163 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003164}`,
3165 ExpectedBazelTargets: []string{
3166 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3167 "srcs": `["aidl/A.aidl"]`,
3168 "hdrs": `["aidl/Header.aidl"]`,
3169 "strip_import_prefix": `"aidl"`,
3170 "tags": `["apex_available=//apex_available:anyapex"]`,
3171 }),
3172 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003173 "deps": `[":A_aidl"]`,
3174 "local_includes": `["."]`,
3175 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003176 }),
3177 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3178 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3179 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003180 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003181 }),
3182 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3183 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3184 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003185 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003186 }),
3187 },
3188 })
3189}
3190
Vinh Tran9f6796a2022-08-16 13:10:31 -04003191func TestCcLibraryWithAidlSrcs(t *testing.T) {
3192 runCcLibraryTestCase(t, Bp2buildTestCase{
3193 Description: "cc_library with aidl srcs",
3194 ModuleTypeUnderTest: "cc_library",
3195 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3196 Blueprint: `
3197filegroup {
3198 name: "A_aidl",
3199 srcs: ["aidl/A.aidl"],
3200 path: "aidl",
3201}
3202cc_library {
3203 name: "foo",
3204 srcs: [
3205 ":A_aidl",
3206 "B.aidl",
3207 ],
3208}`,
3209 ExpectedBazelTargets: []string{
3210 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3211 "srcs": `["aidl/A.aidl"]`,
3212 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003213 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003214 }),
Alixe06d75b2022-08-31 18:28:19 +00003215 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003216 "srcs": `["B.aidl"]`,
3217 }),
Alixe06d75b2022-08-31 18:28:19 +00003218 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003219 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003220 "deps": `[
3221 ":A_aidl",
3222 ":foo_aidl_library",
3223 ]`,
3224 }),
Alixe06d75b2022-08-31 18:28:19 +00003225 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003226 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3227 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003228 }),
Alixe06d75b2022-08-31 18:28:19 +00003229 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003230 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3231 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003232 }),
3233 },
3234 })
3235}
3236
3237func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3238 runCcLibraryTestCase(t, Bp2buildTestCase{
3239 Description: "cc_library with non aidl filegroup",
3240 ModuleTypeUnderTest: "cc_library",
3241 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003242 StubbedBuildDefinitions: []string{"//path/to/A:A_aidl"},
Vinh Tran9f6796a2022-08-16 13:10:31 -04003243 Filesystem: map[string]string{
3244 "path/to/A/Android.bp": `
3245filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003246 name: "A_aidl",
3247 srcs: ["aidl/A.aidl"],
3248 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003249}`,
3250 },
3251 Blueprint: `
3252cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003253 name: "foo",
3254 srcs: [
3255 ":A_aidl",
3256 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003257}`,
3258 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003259 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003260 "local_includes": `["."]`,
3261 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003262 }),
Alixe06d75b2022-08-31 18:28:19 +00003263 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003264 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3265 "local_includes": `["."]`,
3266 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003267 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003268 "local_includes": `["."]`,
3269 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003270 }),
3271 },
3272 })
3273}
3274
3275func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003276 t.Parallel()
3277
3278 expectedBazelTargets := []string{
3279 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003280 "local_includes": `["."]`,
3281 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003282 }),
3283 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3284 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3285 "local_includes": `["."]`,
3286 }),
3287 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3288 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3289 "local_includes": `["."]`,
3290 }),
3291 }
3292 testCases := []struct {
3293 description string
3294 bp string
3295 expectedBazelTargets []string
3296 }{
3297 {
3298 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3299 bp: `
3300 cc_library {
3301 name: "foo",
3302 srcs: [
3303 "Foo.aidl",
3304 ],
3305 aidl: {
3306 export_aidl_headers: true,
3307 }
3308 }`,
3309 expectedBazelTargets: append(
3310 expectedBazelTargets,
3311 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3312 "srcs": `["Foo.aidl"]`,
3313 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003314 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003315 {
3316 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3317 bp: `
3318 aidl_library {
3319 name: "foo_aidl_library",
3320 srcs: ["Foo.aidl"],
3321 }
3322 cc_library {
3323 name: "foo",
3324 aidl: {
3325 libs: ["foo_aidl_library"],
3326 export_aidl_headers: true,
3327 }
3328 }`,
3329 expectedBazelTargets: append(
3330 expectedBazelTargets,
3331 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3332 "srcs": `["Foo.aidl"]`,
3333 "tags": `["apex_available=//apex_available:anyapex"]`,
3334 }),
3335 ),
3336 },
3337 }
3338
3339 for _, testCase := range testCases {
3340 runCcLibraryTestCase(t, Bp2buildTestCase{
3341 Description: "cc_library with export aidl headers",
3342 ModuleTypeUnderTest: "cc_library",
3343 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3344 Blueprint: testCase.bp,
3345 ExpectedBazelTargets: testCase.expectedBazelTargets,
3346 })
3347 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003348}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003349
3350func TestCcLibraryWithTargetApex(t *testing.T) {
3351 runCcLibraryTestCase(t, Bp2buildTestCase{
3352 Description: "cc_library with target.apex",
3353 ModuleTypeUnderTest: "cc_library",
3354 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3355 Blueprint: `
3356cc_library {
3357 name: "foo",
3358 shared_libs: ["bar", "baz"],
3359 static_libs: ["baz", "buh"],
3360 target: {
3361 apex: {
3362 exclude_shared_libs: ["bar"],
3363 exclude_static_libs: ["buh"],
3364 }
3365 }
3366}`,
3367 ExpectedBazelTargets: []string{
3368 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3369 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003370 "//build/bazel/rules/apex:in_apex": [],
3371 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003372 })`,
3373 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003374 "//build/bazel/rules/apex:in_apex": [],
3375 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003376 })`,
3377 "local_includes": `["."]`,
3378 }),
3379 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3380 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003381 "//build/bazel/rules/apex:in_apex": [],
3382 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003383 })`,
3384 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003385 "//build/bazel/rules/apex:in_apex": [],
3386 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003387 })`,
3388 "local_includes": `["."]`,
3389 }),
3390 },
3391 })
3392}
3393
3394func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3395 runCcLibraryTestCase(t, Bp2buildTestCase{
3396 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3397 ModuleTypeUnderTest: "cc_library",
3398 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3399 Blueprint: `
3400cc_library_static {
3401 name: "foo",
3402 shared_libs: ["bar", "baz"],
3403 static_libs: ["abc"],
3404 export_shared_lib_headers: ["baz"],
3405 export_static_lib_headers: ["abc"],
3406 target: {
3407 apex: {
3408 exclude_shared_libs: ["baz", "bar"],
3409 exclude_static_libs: ["abc"],
3410 }
3411 }
3412}`,
3413 ExpectedBazelTargets: []string{
3414 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3415 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003416 "//build/bazel/rules/apex:in_apex": [],
3417 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003418 })`,
3419 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003420 "//build/bazel/rules/apex:in_apex": [],
3421 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003422 })`,
3423 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003424 "//build/bazel/rules/apex:in_apex": [],
3425 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003426 })`,
3427 "local_includes": `["."]`,
3428 }),
3429 },
3430 })
3431}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003432
3433func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3434 runCcLibraryTestCase(t, Bp2buildTestCase{
3435 Description: "cc_library with sysprop sources",
3436 ModuleTypeUnderTest: "cc_library",
3437 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3438 Blueprint: `
3439cc_library {
3440 name: "foo",
3441 srcs: [
3442 "bar.sysprop",
3443 "baz.sysprop",
3444 "blah.cpp",
3445 ],
3446 min_sdk_version: "5",
3447}`,
3448 ExpectedBazelTargets: []string{
3449 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3450 "srcs": `[
3451 "bar.sysprop",
3452 "baz.sysprop",
3453 ]`,
3454 }),
3455 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3456 "dep": `":foo_sysprop_library"`,
3457 "min_sdk_version": `"5"`,
3458 }),
3459 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3460 "srcs": `["blah.cpp"]`,
3461 "local_includes": `["."]`,
3462 "min_sdk_version": `"5"`,
3463 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3464 }),
3465 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3466 "srcs": `["blah.cpp"]`,
3467 "local_includes": `["."]`,
3468 "min_sdk_version": `"5"`,
3469 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3470 }),
3471 },
3472 })
3473}
3474
3475func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3476 runCcLibraryTestCase(t, Bp2buildTestCase{
3477 Description: "cc_library with sysprop sources in some configs but not others",
3478 ModuleTypeUnderTest: "cc_library",
3479 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3480 Blueprint: `
3481cc_library {
3482 name: "foo",
3483 host_supported: true,
3484 srcs: [
3485 "blah.cpp",
3486 ],
3487 target: {
3488 android: {
3489 srcs: ["bar.sysprop"],
3490 },
3491 },
3492 min_sdk_version: "5",
3493}`,
3494 ExpectedBazelTargets: []string{
3495 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3496 "srcs": `select({
3497 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3498 "//conditions:default": [],
3499 })`,
3500 }),
3501 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3502 "dep": `":foo_sysprop_library"`,
3503 "min_sdk_version": `"5"`,
3504 }),
3505 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3506 "srcs": `["blah.cpp"]`,
3507 "local_includes": `["."]`,
3508 "min_sdk_version": `"5"`,
3509 "whole_archive_deps": `select({
3510 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3511 "//conditions:default": [],
3512 })`,
3513 }),
3514 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3515 "srcs": `["blah.cpp"]`,
3516 "local_includes": `["."]`,
3517 "min_sdk_version": `"5"`,
3518 "whole_archive_deps": `select({
3519 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3520 "//conditions:default": [],
3521 })`,
3522 }),
3523 },
3524 })
3525}
Vinh Tran395a1e92022-09-16 18:27:29 -04003526
Sam Delmerico512437b2023-03-17 11:34:15 -04003527func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003528 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003529 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003530 ModuleTypeUnderTest: "cc_library",
3531 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003532 StubbedBuildDefinitions: []string{"bar-static", "baz-static", "bar-shared", "baz-shared"},
Vinh Tran395a1e92022-09-16 18:27:29 -04003533 Blueprint: `
3534cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003535 name: "foo",
3536 srcs: [
3537 "Foo.aidl",
3538 ],
3539 static_libs: [
3540 "bar-static",
3541 "baz-static",
3542 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003543 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003544 "bar-shared",
3545 "baz-shared",
3546 ],
3547 export_static_lib_headers: [
3548 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003549 ],
3550 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003551 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003552 ],
3553}` +
Chris Parsonscd209032023-09-19 01:12:48 +00003554 simpleModule("cc_library_static", "bar-static") +
3555 simpleModule("cc_library_static", "baz-static") +
3556 simpleModule("cc_library", "bar-shared") +
3557 simpleModule("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003558 ExpectedBazelTargets: []string{
3559 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3560 "srcs": `["Foo.aidl"]`,
3561 }),
3562 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003563 "local_includes": `["."]`,
3564 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003565 "implementation_deps": `[
3566 ":baz-static",
3567 ":bar-static",
3568 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003569 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003570 ":baz-shared",
3571 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003572 ]`,
3573 }),
3574 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3575 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003576 "deps": `[":baz-static"]`,
3577 "implementation_deps": `[":bar-static"]`,
3578 "dynamic_deps": `[":baz-shared"]`,
3579 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003580 "local_includes": `["."]`,
3581 }),
3582 },
3583 })
3584}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003585
3586func TestCcLibraryWithTidy(t *testing.T) {
3587 runCcLibraryTestCase(t, Bp2buildTestCase{
3588 Description: "cc_library uses tidy properties",
3589 ModuleTypeUnderTest: "cc_library",
3590 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3591 Blueprint: `
3592cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003593 name: "foo",
3594 srcs: ["foo.cpp"],
3595}
3596cc_library_static {
3597 name: "foo-no-tidy",
3598 srcs: ["foo.cpp"],
3599 tidy: false,
3600}
3601cc_library_static {
3602 name: "foo-tidy",
3603 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003604 tidy: true,
3605 tidy_checks: ["check1", "check2"],
3606 tidy_checks_as_errors: ["check1error", "check2error"],
3607 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003608 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003609}`,
3610 ExpectedBazelTargets: []string{
3611 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3612 "local_includes": `["."]`,
3613 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003614 }),
3615 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3616 "local_includes": `["."]`,
3617 "srcs": `["foo.cpp"]`,
3618 "tidy": `"never"`,
3619 }),
3620 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3621 "local_includes": `["."]`,
3622 "srcs": `["foo.cpp"]`,
3623 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003624 "tidy_checks": `[
3625 "check1",
3626 "check2",
3627 ]`,
3628 "tidy_checks_as_errors": `[
3629 "check1error",
3630 "check2error",
3631 ]`,
3632 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003633 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003634 }),
3635 },
3636 })
3637}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003638
Vinh Tran99270ea2022-11-28 11:15:23 -05003639func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3640 bp := `
3641cc_library {
3642 name: "foo",
3643 afdo: true,
3644 include_build_directory: false,
3645}`
3646
3647 // TODO(b/260714900): Add test case for arch-specific afdo profile
3648 testCases := []struct {
3649 description string
3650 filesystem map[string]string
3651 expectedBazelTargets []string
3652 }{
3653 {
3654 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003655 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003656 "vendor/google_data/pgo_profile/sampling/Android.bp": "",
3657 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003658 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003659 expectedBazelTargets: []string{
3660 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3661 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3662 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3663 }),
3664 },
3665 },
3666 {
3667 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003668 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003669 "toolchain/pgo-profiles/sampling/Android.bp": "",
3670 "toolchain/pgo-profiles/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003671 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003672 expectedBazelTargets: []string{
3673 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3674 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3675 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3676 }),
3677 },
3678 },
3679 {
3680 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003681 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003682 "toolchain/pgo-profiles/sampling/Android.bp": "",
3683 "toolchain/pgo-profiles/sampling/bar.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003684 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003685 expectedBazelTargets: []string{
3686 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3687 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3688 },
3689 },
3690 {
3691 description: "cc_library with afdo enabled but profile doesn't exist",
3692 expectedBazelTargets: []string{
3693 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3694 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3695 },
3696 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003697 {
3698 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3699 filesystem: map[string]string{
3700 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3701 },
3702 expectedBazelTargets: []string{
3703 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3704 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3705 },
3706 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003707 }
3708 for _, testCase := range testCases {
3709 t.Run(testCase.description, func(t *testing.T) {
3710 runCcLibraryTestCase(t, Bp2buildTestCase{
3711 ExpectedBazelTargets: testCase.expectedBazelTargets,
3712 ModuleTypeUnderTest: "cc_library",
3713 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3714 Description: testCase.description,
3715 Blueprint: binaryReplacer.Replace(bp),
3716 Filesystem: testCase.filesystem,
3717 })
3718 })
3719 }
3720}
3721
Yu Liu56ccb1a2022-11-12 10:47:07 -08003722func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3723 runCcLibraryTestCase(t, Bp2buildTestCase{
3724 Description: "cc_library with header abi checker",
3725 ModuleTypeUnderTest: "cc_library",
3726 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3727 Blueprint: `cc_library {
3728 name: "foo",
3729 header_abi_checker: {
3730 enabled: true,
3731 symbol_file: "a.map.txt",
3732 exclude_symbol_versions: [
3733 "29",
3734 "30",
3735 ],
3736 exclude_symbol_tags: [
3737 "tag1",
3738 "tag2",
3739 ],
3740 check_all_apis: true,
3741 diff_flags: ["-allow-adding-removing-weak-symbols"],
3742 },
3743 include_build_directory: false,
3744}`,
3745 ExpectedBazelTargets: []string{
3746 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3747 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3748 "abi_checker_enabled": `True`,
3749 "abi_checker_symbol_file": `"a.map.txt"`,
3750 "abi_checker_exclude_symbol_versions": `[
3751 "29",
3752 "30",
3753 ]`,
3754 "abi_checker_exclude_symbol_tags": `[
3755 "tag1",
3756 "tag2",
3757 ]`,
3758 "abi_checker_check_all_apis": `True`,
3759 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3760 }),
3761 },
3762 })
3763}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003764
3765func TestCcLibraryApexAvailable(t *testing.T) {
3766 runCcLibraryTestCase(t, Bp2buildTestCase{
3767 Description: "cc_library apex_available converted to tags",
3768 ModuleTypeUnderTest: "cc_library",
3769 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3770 Blueprint: soongCcLibraryPreamble + `
3771cc_library {
3772 name: "a",
3773 srcs: ["a.cpp"],
3774 apex_available: ["com.android.foo"],
3775}
3776`,
3777 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3778 "tags": `["apex_available=com.android.foo"]`,
3779 "srcs": `["a.cpp"]`,
3780 "local_includes": `["."]`,
3781 }),
3782 },
3783 )
3784}
3785
3786func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3787 runCcLibraryTestCase(t, Bp2buildTestCase{
3788 Description: "cc_library apex_available converted to multiple tags",
3789 ModuleTypeUnderTest: "cc_library",
3790 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3791 Blueprint: soongCcLibraryPreamble + `
3792cc_library {
3793 name: "a",
3794 srcs: ["a.cpp"],
3795 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3796}
3797`,
3798 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3799 "tags": `[
3800 "apex_available=com.android.foo",
3801 "apex_available=//apex_available:platform",
3802 "apex_available=com.android.bar",
3803 ]`,
3804 "srcs": `["a.cpp"]`,
3805 "local_includes": `["."]`,
3806 }),
3807 },
3808 )
3809}
Zi Wang0f828442022-12-28 11:18:11 -08003810
3811// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3812// In bp2build output, variant info(select) should go before general info.
3813// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3814func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3815 runCcLibraryTestCase(t, Bp2buildTestCase{
3816 Description: "cc_library variant prepend properties order",
3817 ModuleTypeUnderTest: "cc_library",
3818 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3819 Blueprint: soongCcLibraryPreamble + `
3820cc_library {
3821 name: "a",
3822 srcs: ["a.cpp"],
3823 export_include_dirs: ["eid1", "eid2"],
3824 export_system_include_dirs: ["esid1", "esid2"],
3825 target: {
3826 android: {
3827 export_include_dirs: ["android_eid1", "android_eid2"],
3828 export_system_include_dirs: ["android_esid1", "android_esid2"],
3829 },
3830 android_arm: {
3831 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3832 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3833 },
3834 linux: {
3835 export_include_dirs: ["linux_eid1", "linux_eid2"],
3836 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3837 },
3838 },
3839 multilib: {
3840 lib32: {
3841 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3842 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3843 },
3844 },
3845 arch: {
3846 arm: {
3847 export_include_dirs: ["arm_eid1", "arm_eid2"],
3848 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3849 },
3850 }
3851}
3852`,
3853 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3854 "export_includes": `select({
3855 "//build/bazel/platforms/os_arch:android_arm": [
3856 "android_arm_eid1",
3857 "android_arm_eid2",
3858 ],
3859 "//conditions:default": [],
3860 }) + select({
3861 "//build/bazel/platforms/os:android": [
3862 "android_eid1",
3863 "android_eid2",
3864 "linux_eid1",
3865 "linux_eid2",
3866 ],
3867 "//build/bazel/platforms/os:linux_bionic": [
3868 "linux_eid1",
3869 "linux_eid2",
3870 ],
3871 "//build/bazel/platforms/os:linux_glibc": [
3872 "linux_eid1",
3873 "linux_eid2",
3874 ],
3875 "//build/bazel/platforms/os:linux_musl": [
3876 "linux_eid1",
3877 "linux_eid2",
3878 ],
3879 "//conditions:default": [],
3880 }) + select({
3881 "//build/bazel/platforms/arch:arm": [
3882 "lib32_eid1",
3883 "lib32_eid2",
3884 "arm_eid1",
3885 "arm_eid2",
3886 ],
3887 "//build/bazel/platforms/arch:x86": [
3888 "lib32_eid1",
3889 "lib32_eid2",
3890 ],
3891 "//conditions:default": [],
3892 }) + [
3893 "eid1",
3894 "eid2",
3895 ]`,
3896 "export_system_includes": `select({
3897 "//build/bazel/platforms/os_arch:android_arm": [
3898 "android_arm_esid1",
3899 "android_arm_esid2",
3900 ],
3901 "//conditions:default": [],
3902 }) + select({
3903 "//build/bazel/platforms/os:android": [
3904 "android_esid1",
3905 "android_esid2",
3906 "linux_esid1",
3907 "linux_esid2",
3908 ],
3909 "//build/bazel/platforms/os:linux_bionic": [
3910 "linux_esid1",
3911 "linux_esid2",
3912 ],
3913 "//build/bazel/platforms/os:linux_glibc": [
3914 "linux_esid1",
3915 "linux_esid2",
3916 ],
3917 "//build/bazel/platforms/os:linux_musl": [
3918 "linux_esid1",
3919 "linux_esid2",
3920 ],
3921 "//conditions:default": [],
3922 }) + select({
3923 "//build/bazel/platforms/arch:arm": [
3924 "lib32_esid1",
3925 "lib32_esid2",
3926 "arm_esid1",
3927 "arm_esid2",
3928 ],
3929 "//build/bazel/platforms/arch:x86": [
3930 "lib32_esid1",
3931 "lib32_esid2",
3932 ],
3933 "//conditions:default": [],
3934 }) + [
3935 "esid1",
3936 "esid2",
3937 ]`,
3938 "srcs": `["a.cpp"]`,
3939 "local_includes": `["."]`,
3940 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
3941 }),
3942 },
3943 )
3944}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00003945
3946func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
3947 runCcLibraryTestCase(t, Bp2buildTestCase{
3948 Description: "cc_library has correct features when integer_overflow property is provided",
3949 ModuleTypeUnderTest: "cc_library",
3950 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3951 Blueprint: `
3952cc_library {
3953 name: "foo",
3954 sanitize: {
3955 integer_overflow: true,
3956 },
3957}
3958`,
3959 ExpectedBazelTargets: []string{
3960 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3961 "features": `["ubsan_integer_overflow"]`,
3962 "local_includes": `["."]`,
3963 }),
3964 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3965 "features": `["ubsan_integer_overflow"]`,
3966 "local_includes": `["."]`,
3967 }),
3968 },
3969 })
3970}
3971
3972func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
3973 runCcLibraryTestCase(t, Bp2buildTestCase{
3974 Description: "cc_library has correct features when misc_undefined property is provided",
3975 ModuleTypeUnderTest: "cc_library",
3976 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3977 Blueprint: `
3978cc_library {
3979 name: "foo",
3980 sanitize: {
3981 misc_undefined: ["undefined", "nullability"],
3982 },
3983}
3984`,
3985 ExpectedBazelTargets: []string{
3986 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3987 "features": `[
3988 "ubsan_undefined",
3989 "ubsan_nullability",
3990 ]`,
3991 "local_includes": `["."]`,
3992 }),
3993 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3994 "features": `[
3995 "ubsan_undefined",
3996 "ubsan_nullability",
3997 ]`,
3998 "local_includes": `["."]`,
3999 }),
4000 },
4001 })
4002}
4003
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004004func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
4005 runCcLibraryTestCase(t, Bp2buildTestCase{
4006 Description: "cc_library has correct feature when sanitize.blocklist is provided",
4007 ModuleTypeUnderTest: "cc_library",
4008 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4009 Blueprint: `
4010cc_library {
4011 name: "foo",
4012 sanitize: {
4013 blocklist: "foo_blocklist.txt",
4014 },
4015}
4016`,
4017 ExpectedBazelTargets: []string{
4018 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004019 "copts": `select({
4020 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4021 "//conditions:default": [],
4022 })`,
4023 "additional_compiler_inputs": `select({
4024 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4025 "//conditions:default": [],
4026 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004027 "local_includes": `["."]`,
4028 }),
4029 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004030 "copts": `select({
4031 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4032 "//conditions:default": [],
4033 })`,
4034 "additional_compiler_inputs": `select({
4035 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4036 "//conditions:default": [],
4037 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004038 "local_includes": `["."]`,
4039 }),
4040 },
4041 })
4042}
4043
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004044func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4045 runCcLibraryTestCase(t, Bp2buildTestCase{
4046 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4047 ModuleTypeUnderTest: "cc_library",
4048 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4049 Blueprint: `
4050cc_library {
4051 name: "foo",
4052 sanitize: {
4053 misc_undefined: ["undefined", "nullability"],
4054 },
4055 target: {
4056 android: {
4057 sanitize: {
4058 misc_undefined: ["alignment"],
4059 },
4060 },
4061 linux_glibc: {
4062 sanitize: {
4063 integer_overflow: true,
4064 },
4065 },
4066 },
4067}
4068`,
4069 ExpectedBazelTargets: []string{
4070 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4071 "features": `[
4072 "ubsan_undefined",
4073 "ubsan_nullability",
4074 ] + select({
4075 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4076 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4077 "//conditions:default": [],
4078 })`,
4079 "local_includes": `["."]`,
4080 }),
4081 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4082 "features": `[
4083 "ubsan_undefined",
4084 "ubsan_nullability",
4085 ] + select({
4086 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4087 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4088 "//conditions:default": [],
4089 })`,
4090 "local_includes": `["."]`,
4091 }),
4092 },
4093 })
4094}
Yu Liu10174ff2023-02-21 12:05:26 -08004095
4096func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4097 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4098 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4099 ModuleTypeUnderTest: "cc_library",
4100 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004101 StubbedBuildDefinitions: []string{"barlib", "bazlib"},
Yu Liu10174ff2023-02-21 12:05:26 -08004102 Blueprint: `
4103cc_library {
4104 name: "barlib",
4105 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004106 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004107}
4108cc_library {
4109 name: "bazlib",
4110 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004111 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004112}
4113cc_library {
4114 name: "foo",
4115 shared_libs: ["barlib", "bazlib"],
4116 export_shared_lib_headers: ["bazlib"],
4117 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004118 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004119 ],
4120}`,
4121 ExpectedBazelTargets: []string{
4122 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004123 "implementation_dynamic_deps": `[":barlib"]`,
4124 "dynamic_deps": `[":bazlib"]`,
4125 "local_includes": `["."]`,
4126 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004127 }),
4128 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004129 "implementation_dynamic_deps": `[":barlib"]`,
4130 "dynamic_deps": `[":bazlib"]`,
4131 "local_includes": `["."]`,
4132 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004133 }),
4134 },
4135 })
4136}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004137
4138func TestCcLibraryWithThinLto(t *testing.T) {
4139 runCcLibraryTestCase(t, Bp2buildTestCase{
4140 Description: "cc_library has correct features when thin LTO is enabled",
4141 ModuleTypeUnderTest: "cc_library",
4142 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4143 Blueprint: `
4144cc_library {
4145 name: "foo",
4146 lto: {
4147 thin: true,
4148 },
4149}`,
4150 ExpectedBazelTargets: []string{
4151 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4152 "features": `["android_thin_lto"]`,
4153 "local_includes": `["."]`,
4154 }),
4155 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4156 "features": `["android_thin_lto"]`,
4157 "local_includes": `["."]`,
4158 }),
4159 },
4160 })
4161}
4162
4163func TestCcLibraryWithLtoNever(t *testing.T) {
4164 runCcLibraryTestCase(t, Bp2buildTestCase{
4165 Description: "cc_library has correct features when LTO is explicitly disabled",
4166 ModuleTypeUnderTest: "cc_library",
4167 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4168 Blueprint: `
4169cc_library {
4170 name: "foo",
4171 lto: {
4172 never: true,
4173 },
4174}`,
4175 ExpectedBazelTargets: []string{
4176 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4177 "features": `["-android_thin_lto"]`,
4178 "local_includes": `["."]`,
4179 }),
4180 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4181 "features": `["-android_thin_lto"]`,
4182 "local_includes": `["."]`,
4183 }),
4184 },
4185 })
4186}
4187
4188func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4189 runCcLibraryTestCase(t, Bp2buildTestCase{
4190 Description: "cc_library has correct features when LTO differs across arch and os variants",
4191 ModuleTypeUnderTest: "cc_library",
4192 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4193 Blueprint: `
4194cc_library {
4195 name: "foo",
4196 target: {
4197 android: {
4198 lto: {
4199 thin: true,
4200 },
4201 },
4202 },
4203 arch: {
4204 riscv64: {
4205 lto: {
4206 thin: false,
4207 },
4208 },
4209 },
4210}`,
4211 ExpectedBazelTargets: []string{
4212 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4213 "local_includes": `["."]`,
4214 "features": `select({
4215 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4216 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4217 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4218 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4219 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4220 "//conditions:default": [],
4221 })`}),
4222 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4223 "local_includes": `["."]`,
4224 "features": `select({
4225 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4226 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4227 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4228 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4229 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4230 "//conditions:default": [],
4231 })`}),
4232 },
4233 })
4234}
4235
4236func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4237 runCcLibraryTestCase(t, Bp2buildTestCase{
4238 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4239 ModuleTypeUnderTest: "cc_library",
4240 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4241 Blueprint: `
4242cc_library {
4243 name: "foo",
4244 lto: {
4245 never: true,
4246 },
4247 target: {
4248 android: {
4249 lto: {
4250 thin: true,
4251 never: false,
4252 },
4253 },
4254 },
4255}`,
4256 ExpectedBazelTargets: []string{
4257 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4258 "local_includes": `["."]`,
4259 "features": `select({
4260 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4261 "//conditions:default": ["-android_thin_lto"],
4262 })`,
4263 }),
4264 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4265 "local_includes": `["."]`,
4266 "features": `select({
4267 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4268 "//conditions:default": ["-android_thin_lto"],
4269 })`,
4270 }),
4271 },
4272 })
4273}
4274
4275func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4276 runCcLibraryTestCase(t, Bp2buildTestCase{
4277 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4278 ModuleTypeUnderTest: "cc_library",
4279 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4280 Blueprint: `
4281cc_library {
4282 name: "foo",
4283 lto: {
4284 thin: true,
4285 },
4286 whole_program_vtables: true,
4287}`,
4288 ExpectedBazelTargets: []string{
4289 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4290 "features": `[
4291 "android_thin_lto",
4292 "android_thin_lto_whole_program_vtables",
4293 ]`,
4294 "local_includes": `["."]`,
4295 }),
4296 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4297 "features": `[
4298 "android_thin_lto",
4299 "android_thin_lto_whole_program_vtables",
4300 ]`,
4301 "local_includes": `["."]`,
4302 }),
4303 },
4304 })
4305}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004306
4307func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4308 runCcLibraryTestCase(t, Bp2buildTestCase{
4309 Description: "cc_library changes hidden visibility flag to feature",
4310 ModuleTypeUnderTest: "cc_library",
4311 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4312 Blueprint: `
4313cc_library {
4314 name: "foo",
4315 cflags: ["-fvisibility=hidden"],
4316}`,
4317 ExpectedBazelTargets: []string{
4318 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4319 "features": `["visibility_hidden"]`,
4320 "local_includes": `["."]`,
4321 }),
4322 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4323 "features": `["visibility_hidden"]`,
4324 "local_includes": `["."]`,
4325 }),
4326 },
4327 })
4328}
4329
4330func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4331 runCcLibraryTestCase(t, Bp2buildTestCase{
4332 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4333 ModuleTypeUnderTest: "cc_library",
4334 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4335 Blueprint: `
4336cc_library {
4337 name: "foo",
4338 shared: {
4339 cflags: ["-fvisibility=hidden"],
4340 },
4341}`,
4342 ExpectedBazelTargets: []string{
4343 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4344 "local_includes": `["."]`,
4345 }),
4346 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4347 "features": `["visibility_hidden"]`,
4348 "local_includes": `["."]`,
4349 }),
4350 },
4351 })
4352}
4353
4354func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4355 runCcLibraryTestCase(t, Bp2buildTestCase{
4356 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4357 ModuleTypeUnderTest: "cc_library",
4358 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4359 Blueprint: `
4360cc_library {
4361 name: "foo",
4362 static: {
4363 cflags: ["-fvisibility=hidden"],
4364 },
4365}`,
4366 ExpectedBazelTargets: []string{
4367 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4368 "features": `["visibility_hidden"]`,
4369 "local_includes": `["."]`,
4370 }),
4371 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4372 "local_includes": `["."]`,
4373 }),
4374 },
4375 })
4376}
4377
4378func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4379 runCcLibraryTestCase(t, Bp2buildTestCase{
4380 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4381 ModuleTypeUnderTest: "cc_library",
4382 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4383 Blueprint: `
4384cc_library {
4385 name: "foo",
4386 target: {
4387 android: {
4388 cflags: ["-fvisibility=hidden"],
4389 },
4390 },
4391}`,
4392 ExpectedBazelTargets: []string{
4393 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4394 "features": `select({
4395 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4396 "//conditions:default": [],
4397 })`,
4398 "local_includes": `["."]`,
4399 }),
4400 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4401 "features": `select({
4402 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4403 "//conditions:default": [],
4404 })`,
4405 "local_includes": `["."]`,
4406 }),
4407 },
4408 })
4409}
Spandan Das4242f102023-04-19 22:31:54 +00004410
4411// Test that a config_setting specific to an apex is created by cc_library.
4412func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4413 runCcLibraryTestCase(t, Bp2buildTestCase{
4414 Description: "cc_library creates a config_setting for each apex in apex_available",
4415 ModuleTypeUnderTest: "cc_library",
4416 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4417 Dir: "build/bazel/rules/apex",
4418 Blueprint: `
4419cc_library {
4420 name: "foo",
4421 apex_available: [
4422 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4423 "myapex"
4424 ],
4425}`,
4426 ExpectedBazelTargets: []string{
4427 MakeBazelTargetNoRestrictions(
4428 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004429 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004430 AttrNameToString{
4431 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004432 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004433 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004434 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004435 },
4436 ),
4437 },
4438 })
4439}
Yu Liu93893ba2023-05-01 13:49:52 -07004440
4441func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4442 runCcLibraryTestCase(t, Bp2buildTestCase{
4443 Description: "cc_library cppflags in product variables",
4444 ModuleTypeUnderTest: "cc_library",
4445 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4446 Blueprint: soongCcLibraryPreamble + `cc_library {
4447 name: "a",
4448 srcs: ["a.cpp"],
4449 cppflags: [
4450 "-Wextra",
4451 "-DDEBUG_ONLY_CODE=0",
4452 ],
4453 product_variables: {
4454 eng: {
4455 cppflags: [
4456 "-UDEBUG_ONLY_CODE",
4457 "-DDEBUG_ONLY_CODE=1",
4458 ],
4459 },
4460 },
4461 include_build_directory: false,
4462}
4463`,
4464 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4465 "cppflags": `[
4466 "-Wextra",
4467 "-DDEBUG_ONLY_CODE=0",
4468 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004469 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004470 "-UDEBUG_ONLY_CODE",
4471 "-DDEBUG_ONLY_CODE=1",
4472 ],
4473 "//conditions:default": [],
4474 })`,
4475 "srcs": `["a.cpp"]`,
4476 }),
4477 },
4478 )
4479}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004480
4481func TestCcLibraryYaccConversion(t *testing.T) {
4482 runCcLibraryTestCase(t, Bp2buildTestCase{
4483 Description: "cc_library is built from .y/.yy files",
4484 ModuleTypeUnderTest: "cc_library",
4485 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004486 StubbedBuildDefinitions: []string{"staticlib", "sharedlib"},
Spandan Dasdf4c2132023-05-09 23:58:52 +00004487 Blueprint: soongCcLibraryPreamble + `cc_library {
4488 name: "a",
4489 srcs: [
4490 "a.cpp",
4491 "a.yy",
4492 ],
4493 shared_libs: ["sharedlib"],
4494 static_libs: ["staticlib"],
4495 yacc: {
4496 flags: ["someYaccFlag"],
4497 gen_location_hh: true,
4498 gen_position_hh: true,
4499 },
4500}
4501cc_library_static {
4502 name: "staticlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004503}
4504cc_library {
4505 name: "sharedlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004506}
4507`,
4508 ExpectedBazelTargets: []string{
4509 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4510 "src": `"a.yy"`,
4511 "implementation_deps": `[":staticlib"]`,
4512 "implementation_dynamic_deps": `[":sharedlib"]`,
4513 "flags": `["someYaccFlag"]`,
4514 "gen_location_hh": "True",
4515 "gen_position_hh": "True",
4516 "local_includes": `["."]`,
4517 }),
4518 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4519 "srcs": `["a.cpp"]`,
4520 "implementation_deps": `[":staticlib"]`,
4521 "implementation_dynamic_deps": `[":sharedlib"]`,
4522 "implementation_whole_archive_deps": `[":a_yacc"]`,
4523 "local_includes": `["."]`,
4524 }),
4525 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4526 "srcs": `["a.cpp"]`,
4527 "implementation_deps": `[":staticlib"]`,
4528 "implementation_dynamic_deps": `[":sharedlib"]`,
4529 "implementation_whole_archive_deps": `[":a_yacc"]`,
4530 "local_includes": `["."]`,
4531 }),
4532 },
4533 })
4534}
Spandan Dasfb04c412023-05-15 18:35:36 +00004535
4536func TestCcLibraryHostLdLibs(t *testing.T) {
4537 runCcLibraryTestCase(t, Bp2buildTestCase{
4538 Description: "cc_binary linker flags for host_ldlibs",
4539 ModuleTypeUnderTest: "cc_binary",
4540 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4541 Blueprint: soongCcLibraryPreamble + `cc_binary {
4542 name: "a",
4543 host_supported: true,
4544 ldflags: ["-lcommon"],
4545 target: {
4546 linux: {
4547 host_ldlibs: [
4548 "-llinux",
4549 ],
4550 },
4551 darwin: {
4552 ldflags: ["-ldarwinadditional"],
4553 host_ldlibs: [
4554 "-ldarwin",
4555 ],
4556 },
4557 windows: {
4558 host_ldlibs: [
4559 "-lwindows",
4560 ],
4561 },
4562 },
4563}
4564`,
4565 ExpectedBazelTargets: []string{
4566 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4567 "linkopts": `["-lcommon"] + select({
4568 "//build/bazel/platforms/os:darwin": [
4569 "-ldarwinadditional",
4570 "-ldarwin",
4571 ],
4572 "//build/bazel/platforms/os:linux_glibc": ["-llinux"],
4573 "//build/bazel/platforms/os:windows": ["-lwindows"],
4574 "//conditions:default": [],
4575 })`,
4576 "local_includes": `["."]`,
4577 }),
4578 },
4579 })
4580}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004581
4582func TestCcLibraryWithCfi(t *testing.T) {
4583 runCcLibraryTestCase(t, Bp2buildTestCase{
4584 Description: "cc_library has correct features when cfi is enabled",
4585 ModuleTypeUnderTest: "cc_library",
4586 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4587 Blueprint: `
4588cc_library {
4589 name: "foo",
4590 sanitize: {
4591 cfi: true,
4592 },
4593}`,
4594 ExpectedBazelTargets: []string{
4595 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4596 "features": `["android_cfi"]`,
4597 "local_includes": `["."]`,
4598 }),
4599 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4600 "features": `["android_cfi"]`,
4601 "local_includes": `["."]`,
4602 }),
4603 },
4604 })
4605}
4606
4607func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4608 runCcLibraryTestCase(t, Bp2buildTestCase{
4609 Description: "cc_library has correct features when cfi is enabled for specific variants",
4610 ModuleTypeUnderTest: "cc_library",
4611 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4612 Blueprint: `
4613cc_library {
4614 name: "foo",
4615 target: {
4616 android: {
4617 sanitize: {
4618 cfi: true,
4619 },
4620 },
4621 },
4622}`,
4623 ExpectedBazelTargets: []string{
4624 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4625 "features": `select({
4626 "//build/bazel/platforms/os:android": ["android_cfi"],
4627 "//conditions:default": [],
4628 })`,
4629 "local_includes": `["."]`,
4630 }),
4631 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4632 "features": `select({
4633 "//build/bazel/platforms/os:android": ["android_cfi"],
4634 "//conditions:default": [],
4635 })`,
4636 "local_includes": `["."]`,
4637 }),
4638 },
4639 })
4640}
4641
4642func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4643 runCcLibraryTestCase(t, Bp2buildTestCase{
4644 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4645 ModuleTypeUnderTest: "cc_library",
4646 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4647 Blueprint: `
4648cc_library {
4649 name: "foo",
4650 sanitize: {
4651 cfi: true,
4652 config: {
4653 cfi_assembly_support: true,
4654 },
4655 },
4656}`,
4657 ExpectedBazelTargets: []string{
4658 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4659 "features": `[
4660 "android_cfi",
4661 "android_cfi_assembly_support",
4662 ]`,
4663 "local_includes": `["."]`,
4664 }),
4665 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4666 "features": `[
4667 "android_cfi",
4668 "android_cfi_assembly_support",
4669 ]`,
4670 "local_includes": `["."]`,
4671 }),
4672 },
4673 })
4674}
Spandan Das39ccf932023-05-26 18:03:39 +00004675
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004676func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4677 runCcLibraryTestCase(t, Bp2buildTestCase{
4678 Description: "cc_library disables cfi when explciitly set to false in the bp",
4679 ModuleTypeUnderTest: "cc_library",
4680 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4681 Blueprint: `
4682cc_library {
4683 name: "foo",
4684 sanitize: {
4685 cfi: false,
4686 },
4687}
4688`,
4689 ExpectedBazelTargets: []string{
4690 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4691 "features": `["-android_cfi"]`,
4692 "local_includes": `["."]`,
4693 }),
4694 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4695 "features": `["-android_cfi"]`,
4696 "local_includes": `["."]`,
4697 }),
4698 },
4699 })
4700}
4701
Spandan Das39ccf932023-05-26 18:03:39 +00004702func TestCcLibraryWithStem(t *testing.T) {
4703 runCcLibraryTestCase(t, Bp2buildTestCase{
4704 Description: "cc_library with stem property",
4705 ModuleTypeUnderTest: "cc_library_shared",
4706 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4707 Blueprint: soongCcLibraryPreamble + `
4708cc_library_shared {
4709 name: "foo_with_stem_simple",
4710 stem: "foo",
4711}
4712cc_library_shared {
4713 name: "foo_with_arch_variant_stem",
4714 arch: {
4715 arm: {
4716 stem: "foo-arm",
4717 },
4718 arm64: {
4719 stem: "foo-arm64",
4720 },
4721 },
4722}
4723`,
4724 ExpectedBazelTargets: []string{
4725 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4726 "stem": `"foo"`,
4727 "local_includes": `["."]`,
4728 }),
4729 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4730 "stem": `select({
4731 "//build/bazel/platforms/arch:arm": "foo-arm",
4732 "//build/bazel/platforms/arch:arm64": "foo-arm64",
4733 "//conditions:default": None,
4734 })`,
4735 "local_includes": `["."]`,
4736 }),
4737 },
4738 })
4739}
Spandan Dasc53767e2023-08-03 23:02:26 +00004740
4741// Bazel enforces that proto_library and the .proto file are in the same bazel package
4742func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4743 tc := Bp2buildTestCase{
4744 Description: "cc_library depends on .proto files from multiple packages",
4745 ModuleTypeUnderTest: "cc_library",
4746 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4747 Blueprint: `
4748cc_library_static {
4749 name: "foo",
4750 srcs: [
4751 "foo.proto",
4752 "bar/bar.proto", // Different package because there is a bar/Android.bp
4753 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4754 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004755 proto: {
4756 canonical_path_from_root: true,
4757 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004758}
Chris Parsonscd209032023-09-19 01:12:48 +00004759` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasc53767e2023-08-03 23:02:26 +00004760 Filesystem: map[string]string{
4761 "bar/Android.bp": "",
4762 "baz/subbaz/Android.bp": "",
4763 },
4764 }
4765
4766 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4767 // Root dir
4768 tc.ExpectedBazelTargets = []string{
4769 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4770 "local_includes": `["."]`,
4771 "deps": `[":libprotobuf-cpp-lite"]`,
4772 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4773 }),
4774 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4775 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004776 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004777 }),
4778 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4779 "deps": `[
4780 ":foo_proto",
4781 "//bar:foo_proto",
4782 "//baz/subbaz:foo_proto",
4783 ]`,
4784 }),
4785 }
4786 runCcLibraryTestCase(t, tc)
4787
4788 // bar dir
4789 tc.Dir = "bar"
4790 tc.ExpectedBazelTargets = []string{
4791 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004792 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004793 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004794 }),
4795 }
4796 runCcLibraryTestCase(t, tc)
4797
4798 // baz/subbaz dir
4799 tc.Dir = "baz/subbaz"
4800 tc.ExpectedBazelTargets = []string{
4801 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004802 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004803 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004804 }),
4805 }
4806 runCcLibraryTestCase(t, tc)
4807}
4808
4809// Bazel enforces that proto_library and the .proto file are in the same bazel package
4810func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
4811 tc := Bp2buildTestCase{
4812 Description: "cc_library depends on .proto files from multiple packages",
4813 ModuleTypeUnderTest: "cc_library",
4814 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4815 Blueprint: `
4816cc_library_static {
4817 name: "foo",
4818 srcs: [
4819 "foo.proto",
4820 "bar/bar.proto", // Different package because there is a bar/Android.bp
4821 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4822 ],
4823 proto: {
4824 canonical_path_from_root: false,
4825 }
4826}
Chris Parsonscd209032023-09-19 01:12:48 +00004827` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004828 Filesystem: map[string]string{
4829 "bar/Android.bp": "",
4830 "baz/subbaz/Android.bp": "",
4831 },
4832 }
4833
4834 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4835 // Root dir
4836 tc.ExpectedBazelTargets = []string{
4837 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4838 "local_includes": `["."]`,
4839 "deps": `[":libprotobuf-cpp-lite"]`,
4840 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4841 }),
4842 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4843 "srcs": `["foo.proto"]`,
4844 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004845 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004846 }),
4847 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4848 "deps": `[
4849 ":foo_proto",
4850 "//bar:foo_proto",
4851 "//baz/subbaz:foo_proto",
4852 ]`,
4853 }),
4854 }
4855 runCcLibraryTestCase(t, tc)
4856
4857 // bar dir
4858 tc.Dir = "bar"
4859 tc.ExpectedBazelTargets = []string{
4860 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4861 "srcs": `["//bar:bar.proto"]`,
4862 "strip_import_prefix": `""`,
4863 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00004864 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004865 }),
4866 }
4867 runCcLibraryTestCase(t, tc)
4868
4869 // baz/subbaz dir
4870 tc.Dir = "baz/subbaz"
4871 tc.ExpectedBazelTargets = []string{
4872 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4873 "srcs": `["//baz/subbaz:baz.proto"]`,
4874 "strip_import_prefix": `""`,
4875 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00004876 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004877 }),
4878 }
4879 runCcLibraryTestCase(t, tc)
4880}
Spandan Dasec39d512023-08-15 22:08:18 +00004881
4882func TestProtoIncludeDirs(t *testing.T) {
4883 tc := Bp2buildTestCase{
4884 Description: "cc_library depends on .proto files using proto.include_dirs",
4885 ModuleTypeUnderTest: "cc_library",
4886 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4887 Blueprint: `
4888cc_library_static {
4889 name: "foo",
4890 srcs: [
4891 "foo.proto",
4892 ],
4893 proto: {
4894 include_dirs: ["bar"],
4895 }
4896}
Chris Parsonscd209032023-09-19 01:12:48 +00004897` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasec39d512023-08-15 22:08:18 +00004898 Filesystem: map[string]string{
4899 "bar/Android.bp": "",
4900 "bar/bar.proto": "",
4901 "bar/baz/Android.bp": "",
4902 "bar/baz/baz.proto": "",
4903 },
4904 }
4905
4906 // We will run the test 3 times and check in the root, bar and bar/baz directories
4907 // Root dir
4908 tc.ExpectedBazelTargets = []string{
4909 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4910 "local_includes": `["."]`,
4911 "deps": `[":libprotobuf-cpp-lite"]`,
4912 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4913 }),
4914 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4915 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004916 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00004917 }),
4918 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4919 "deps": `[":foo_proto"]`,
4920 "transitive_deps": `[
4921 "//bar:bar.include_dir_bp2build_generated_proto",
4922 "//bar/baz:bar.include_dir_bp2build_generated_proto",
4923 ]`,
4924 }),
4925 }
4926 runCcLibraryTestCase(t, tc)
4927
4928 // bar dir
4929 tc.Dir = "bar"
4930 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004931 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004932 "srcs": `["bar.proto"]`,
4933 "strip_import_prefix": `""`,
4934 "tags": `["manual"]`,
4935 }),
4936 }
4937 runCcLibraryTestCase(t, tc)
4938
4939 // bar/baz dir
4940 tc.Dir = "bar/baz"
4941 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004942 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004943 "srcs": `["//bar/baz:baz.proto"]`,
4944 "strip_import_prefix": `""`,
4945 "import_prefix": `"baz"`,
4946 "tags": `["manual"]`,
4947 }),
4948 }
4949 runCcLibraryTestCase(t, tc)
4950}
Spandan Das4e5a1942023-08-22 19:20:39 +00004951
Spandan Dasf26ee152023-08-24 23:21:04 +00004952func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
4953 tc := Bp2buildTestCase{
4954 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
4955 ModuleTypeUnderTest: "cc_library",
4956 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4957 Blueprint: `
4958cc_library_static {
4959 name: "foo",
4960 srcs: [
4961 "foo.proto",
4962 "bar/bar.proto",
4963 ],
4964 proto: {
4965 include_dirs: ["baz"],
4966 }
4967}
Chris Parsonscd209032023-09-19 01:12:48 +00004968` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasf26ee152023-08-24 23:21:04 +00004969 Filesystem: map[string]string{
4970 "bar/Android.bp": "", // package boundary
4971 "baz/Android.bp": "",
4972 "baz/baz.proto": "",
4973 },
4974 }
4975
4976 tc.ExpectedBazelTargets = []string{
4977 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4978 "local_includes": `["."]`,
4979 "deps": `[":libprotobuf-cpp-lite"]`,
4980 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4981 }),
4982 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4983 "srcs": `["foo.proto"]`,
4984 "tags": `["manual"]`,
4985 }),
4986 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4987 "deps": `[
4988 ":foo_proto",
4989 "//bar:foo_proto",
4990 ]`,
4991 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
4992 }),
4993 }
4994 runCcLibraryTestCase(t, tc)
4995
4996}
4997
Spandan Das4e5a1942023-08-22 19:20:39 +00004998func TestProtoLocalIncludeDirs(t *testing.T) {
4999 tc := Bp2buildTestCase{
5000 Description: "cc_library depends on .proto files using proto.local_include_dirs",
5001 ModuleTypeUnderTest: "cc_library",
5002 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005003 Blueprint: `
5004cc_library {
5005 name: "libprotobuf-cpp-lite",
5006 // TODO: b/285631638 - A stubbed proto library dependency does not work as a protolib
5007 // dependency of cc_library_static.
5008 bazel_module: { bp2build_available: false },
5009}
5010`,
Spandan Das4e5a1942023-08-22 19:20:39 +00005011 Filesystem: map[string]string{
5012 "foo/Android.bp": `cc_library_static {
5013 name: "foo",
5014 srcs: [
5015 "foo.proto",
5016 ],
5017 proto: {
5018 local_include_dirs: ["foo_subdir"],
5019 },
5020 bazel_module: { bp2build_available: true },
5021}`,
5022 "foo/foo.proto": "",
5023 "foo/foo_subdir/Android.bp": "",
5024 "foo/foo_subdir/foo_subdir.proto": "",
5025 },
5026 }
5027
5028 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5029 // foo dir
5030 tc.Dir = "foo"
5031 tc.ExpectedBazelTargets = []string{
5032 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5033 "local_includes": `["."]`,
5034 "deps": `["//:libprotobuf-cpp-lite"]`,
5035 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5036 }),
5037 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5038 "srcs": `["foo.proto"]`,
5039 "tags": `["manual"]`,
5040 }),
5041 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5042 "deps": `[":foo_proto"]`,
5043 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5044 }),
5045 }
5046 runCcLibraryTestCase(t, tc)
5047
5048 // foo/foo_subdir
5049 tc.Dir = "foo/foo_subdir"
5050 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005051 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005052 "srcs": `["foo_subdir.proto"]`,
5053 "strip_import_prefix": `""`,
5054 "tags": `["manual"]`,
5055 }),
5056 }
5057 runCcLibraryTestCase(t, tc)
5058}
Spandan Dasab29f572023-09-01 19:43:56 +00005059
5060// `foo_device` and `bar_host` can depend on .proto files of a specific dir,
5061// the dynamically generated proto_library should not have any target_compatible_with
5062func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) {
5063 tc := Bp2buildTestCase{
5064 Description: "proto_library generated for proto.include_dirs is compatible for all axes",
5065 ModuleTypeUnderTest: "cc_library",
5066 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005067 Blueprint: simpleModule("cc_library", "libprotobuf-cpp-lite") + `
Spandan Dasab29f572023-09-01 19:43:56 +00005068cc_library {
5069 name: "foo_device",
5070 device_supported: true, // this is the default behavior, but added explicitly here for illustration
5071 host_supported: false,
5072 proto: {include_dirs: ["dir"]},
5073}
5074cc_library {
5075 name: "bar_host",
5076 device_supported: false,
5077 host_supported: true,
5078 srcs: ["bar.proto"],
5079 proto: {include_dirs: ["dir"]},
5080}
5081`,
5082 Filesystem: map[string]string{
5083 "dir/Android.bp": "",
5084 "dir/dir.proto": "",
5085 },
5086 Dir: "dir", // check for the generated proto_library
5087 ExpectedBazelTargets: []string{
5088 MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{
5089 "srcs": `["dir.proto"]`,
5090 "strip_import_prefix": `""`,
5091 "tags": `["manual"]`,
5092 }),
5093 },
5094 }
5095 runCcLibraryTestCase(t, tc)
5096}
Spandan Dase1cb14b2023-09-05 19:31:12 +00005097
5098func TestCcCompileMultilibConversion(t *testing.T) {
5099 tc := Bp2buildTestCase{
5100 Description: "cc_library with compile_multilib",
5101 ModuleTypeUnderTest: "cc_library",
5102 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5103 Blueprint: `
5104cc_library {
5105 name: "lib32",
5106 compile_multilib: "32",
5107}
5108cc_library {
5109 name: "lib64",
5110 compile_multilib: "64",
5111}
5112`,
5113 ExpectedBazelTargets: []string{
5114 MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{
5115 "local_includes": `["."]`,
5116 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5117 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5118 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5119 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5120 "//conditions:default": [],
5121 })`,
5122 }),
5123 MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{
5124 "local_includes": `["."]`,
5125 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5126 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5127 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5128 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5129 "//conditions:default": [],
5130 })`,
5131 }),
5132 MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{
5133 "local_includes": `["."]`,
5134 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5135 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5136 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5137 "//conditions:default": [],
5138 })`,
5139 }),
5140 MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{
5141 "local_includes": `["."]`,
5142 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5143 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5144 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5145 "//conditions:default": [],
5146 })`,
5147 }),
5148 },
5149 }
5150 runCcLibraryTestCase(t, tc)
5151}
Spandan Das63acae92023-09-14 22:34:34 +00005152
5153func TestNdkLibraryConversion(t *testing.T) {
5154 tc := Bp2buildTestCase{
5155 Description: "ndk_library conversion",
5156 ModuleTypeUnderTest: "cc_library",
5157 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5158 Blueprint: `
5159cc_library {
5160 name: "libfoo",
5161 bazel_module: { bp2build_available: false },
5162}
5163ndk_library {
5164 name: "libfoo",
5165 first_version: "29",
5166 symbol_file: "libfoo.map.txt",
5167}
5168`,
5169 ExpectedBazelTargets: []string{
5170 MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{
5171 "api_surface": `"publicapi"`,
5172 "soname": `"libfoo.so"`,
5173 "source_library_label": `"//:libfoo"`,
5174 "symbol_file": `"libfoo.map.txt"`,
5175 "versions": `[
5176 "29",
5177 "30",
5178 "S",
5179 "Tiramisu",
5180 "current",
5181 ]`,
5182 }),
5183 },
5184 }
5185 runCcLibraryTestCase(t, tc)
5186}
Spandan Das319711b2023-09-19 19:04:41 +00005187
5188func TestNdkHeadersConversion(t *testing.T) {
5189 tc := Bp2buildTestCase{
5190 Description: "ndk_headers conversion",
5191 ModuleTypeUnderTest: "ndk_headers",
5192 ModuleTypeUnderTestFactory: cc.NdkHeadersFactory,
5193 Blueprint: `
5194ndk_headers {
5195 name: "libfoo_headers",
5196 from: "from",
5197 to: "to",
5198 srcs: ["foo.h", "foo_other.h"]
5199}
5200`,
5201 ExpectedBazelTargets: []string{
5202 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5203 "strip_import_prefix": `"from"`,
5204 "import_prefix": `"to"`,
5205 "hdrs": `[
5206 "foo.h",
5207 "foo_other.h",
5208 ]`,
5209 }),
5210 },
5211 }
5212 runCcLibraryTestCase(t, tc)
5213}