blob: 560123ea4ab0d746087d67038921bfb5e54afb4b [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",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001860}
1861
1862cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001863 name: "target_bionic_empty",
1864 target: {
1865 bionic: {
1866 system_shared_libs: [],
1867 },
1868 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001869 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001870}
1871`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001872 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001873 "system_dynamic_deps": `select({
1874 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1875 "//conditions:default": [],
1876 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001877 }),
1878 },
1879 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001880}
1881
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001882func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1883 runCcLibraryTestCase(t, Bp2buildTestCase{
1884 Description: "cc_library system_shared_lib empty for musl variant",
1885 ModuleTypeUnderTest: "cc_library",
1886 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001887 StubbedBuildDefinitions: []string{"libc_musl"},
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001888 Blueprint: soongCcLibraryPreamble + `
1889cc_library {
1890 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001891}
1892
1893cc_library {
1894 name: "target_musl_empty",
1895 target: {
1896 musl: {
1897 system_shared_libs: [],
1898 },
1899 },
1900 include_build_directory: false,
1901}
1902`,
1903 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1904 "system_dynamic_deps": `[]`,
1905 }),
1906 })
1907}
1908
1909func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1910 runCcLibraryTestCase(t, Bp2buildTestCase{
1911 Description: "cc_library system_shared_lib empty for linux_musl variant",
1912 ModuleTypeUnderTest: "cc_library",
1913 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons4c81ce02023-09-21 15:30:27 +00001914 StubbedBuildDefinitions: []string{"libc_musl"},
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001915 Blueprint: soongCcLibraryPreamble + `
1916cc_library {
1917 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001918}
1919
1920cc_library {
1921 name: "target_linux_musl_empty",
1922 target: {
1923 linux_musl: {
1924 system_shared_libs: [],
1925 },
1926 },
1927 include_build_directory: false,
1928}
1929`,
1930 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1931 "system_dynamic_deps": `[]`,
1932 }),
1933 })
1934}
Chris Parsons51f8c392021-08-03 21:01:05 -04001935func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001936 runCcLibraryTestCase(t, Bp2buildTestCase{
1937 Description: "cc_library system_shared_libs set for shared and root",
1938 ModuleTypeUnderTest: "cc_library",
1939 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001940 StubbedBuildDefinitions: []string{"libc", "libm"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001941 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001942cc_library {
1943 name: "libc",
Liz Kammer8337ea42021-09-10 10:06:32 -04001944}
1945cc_library {
1946 name: "libm",
Liz Kammer8337ea42021-09-10 10:06:32 -04001947}
Chris Parsons51f8c392021-08-03 21:01:05 -04001948
1949cc_library {
1950 name: "foo",
1951 system_shared_libs: ["libc"],
1952 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001953 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001954 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001955 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001956}
1957`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001958 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001959 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001960 "system_dynamic_deps": `[":libc"]`,
1961 }),
Alixe06d75b2022-08-31 18:28:19 +00001962 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001963 "system_dynamic_deps": `[
1964 ":libc",
1965 ":libm",
1966 ]`,
1967 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001968 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001969 })
1970}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001971
1972func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001973 runCcLibraryTestCase(t, Bp2buildTestCase{
1974 Description: "cc_library - selects for all os targets",
1975 ModuleTypeUnderTest: "cc_library",
1976 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1977 Filesystem: map[string]string{},
1978 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001979cc_library {
1980 name: "foo-lib",
1981 srcs: ["base.cpp"],
1982 target: {
1983 android: {
1984 srcs: ["android.cpp"],
1985 },
1986 linux: {
1987 srcs: ["linux.cpp"],
1988 },
1989 linux_glibc: {
1990 srcs: ["linux_glibc.cpp"],
1991 },
1992 darwin: {
1993 srcs: ["darwin.cpp"],
1994 },
1995 bionic: {
1996 srcs: ["bionic.cpp"],
1997 },
1998 linux_musl: {
1999 srcs: ["linux_musl.cpp"],
2000 },
2001 windows: {
2002 srcs: ["windows.cpp"],
2003 },
2004 },
2005 include_build_directory: false,
2006}
2007`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002008 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002009 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002010 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002011 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002012 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002013 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002014 ],
2015 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002016 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002017 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002018 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002019 ],
Colin Cross133782e2022-12-20 15:29:31 -08002020 "//build/bazel/platforms/os:linux_glibc": [
2021 "linux.cpp",
2022 "linux_glibc.cpp",
2023 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002024 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002025 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002026 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002027 ],
2028 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2029 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002030 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002031 }),
2032 },
2033 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002034}
2035
Yu Liu75be7b92022-02-01 09:54:09 -08002036func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002037 runCcLibraryTestCase(t, Bp2buildTestCase{
2038 Description: "cc_library - libcrypto hash injection",
2039 ModuleTypeUnderTest: "cc_library",
2040 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2041 Filesystem: map[string]string{},
2042 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002043cc_library {
2044 name: "libcrypto",
2045 target: {
2046 android: {
2047 inject_bssl_hash: true,
2048 },
2049 },
2050 include_build_directory: false,
2051}
2052`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002053 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002054 "inject_bssl_hash": `select({
2055 "//build/bazel/platforms/os:android": True,
2056 "//conditions:default": None,
2057 })`,
2058 }),
2059 },
2060 )
2061}
2062
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002063func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002064 type testCase struct {
2065 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002066 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002067 gnu_extensions string
2068 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002069 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002070 }
2071
2072 testCases := []testCase{
2073 // Existing usages of cpp_std in AOSP are:
2074 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2075 //
2076 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2077 // is set in the toolchain.)
2078 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002079 {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 +00002080 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2081 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002082 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2083 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2084 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002085 // Explicitly setting a c++ std does not use replace gnu++ std even if
2086 // gnu_extensions is true.
2087 // "c++11",
2088 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002089 {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 +00002090 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2091 // "c++17",
2092 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002093 {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 +00002094 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2095 // "c++2a",
2096 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002097 {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 +00002098 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2099 // "c++98",
2100 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002101 {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 +00002102 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2103 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2104 // "gnu++11",
2105 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002106 {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 +00002107 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2108 // "gnu++17",
2109 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002110 {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 +00002111 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002112
2113 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002114 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2115 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2116 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002117 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2118 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2119 {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 +00002120 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002121 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002122 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2123 t.Run(name, func(t *testing.T) {
2124 name_prefix := fmt.Sprintf("a_%v", i)
2125 cppStdProp := ""
2126 if tc.cpp_std != "" {
2127 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2128 }
2129 cStdProp := ""
2130 if tc.c_std != "" {
2131 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2132 }
2133 gnuExtensionsProp := ""
2134 if tc.gnu_extensions != "" {
2135 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2136 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002137 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002138 if tc.bazel_cpp_std != "" {
2139 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2140 }
2141 if tc.bazel_c_std != "" {
2142 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2143 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002144
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002145 runCcLibraryTestCase(t, Bp2buildTestCase{
2146 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002147 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002148 ModuleTypeUnderTest: "cc_library",
2149 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2150 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002151cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002152 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002153%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002154%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002155%s // gnu_extensions: *bool
2156 include_build_directory: false,
2157}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002158`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002159 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002160 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002161
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002162 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2163 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002164 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002165 ModuleTypeUnderTest: "cc_library_static",
2166 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2167 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002168cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002169 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002170%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002171%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002172%s // gnu_extensions: *bool
2173 include_build_directory: false,
2174}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002175`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002176 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002177 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002178 },
2179 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002180
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002181 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2182 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002183 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002184 ModuleTypeUnderTest: "cc_library_shared",
2185 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2186 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002187cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002188 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002189%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002190%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002191%s // gnu_extensions: *bool
2192 include_build_directory: false,
2193}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002194`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002195 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002196 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002197 },
2198 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002199 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002200 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002201}
Liz Kammer12615db2021-09-28 09:19:17 -04002202
2203func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002204 runCcLibraryTestCase(t, Bp2buildTestCase{
2205 ModuleTypeUnderTest: "cc_library",
2206 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2207 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002208 name: "foo",
2209 srcs: ["foo.proto"],
2210 include_build_directory: false,
2211}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002212 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002213 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002214 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002215 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002216 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002217 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002218 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002219 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002220 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002221 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2222 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002223 }),
2224 },
2225 })
2226}
2227
2228func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002229 runCcLibraryTestCase(t, Bp2buildTestCase{
2230 ModuleTypeUnderTest: "cc_library",
2231 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2232 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002233 name: "foo",
2234 srcs: ["foo.proto"],
2235 proto: { canonical_path_from_root: false},
2236 include_build_directory: false,
2237}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002238 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002239 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002240 "srcs": `["foo.proto"]`,
2241 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002242 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002243 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002244 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002245 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002246 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002247 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002248 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2249 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002250 }),
2251 },
2252 })
2253}
2254
2255func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002256 runCcLibraryTestCase(t, Bp2buildTestCase{
2257 ModuleTypeUnderTest: "cc_library",
2258 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2259 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002260 name: "foo",
2261 srcs: ["foo.proto"],
2262 proto: { canonical_path_from_root: true},
2263 include_build_directory: false,
2264}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002265 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002266 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002267 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002268 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002269 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002270 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002271 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002272 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002273 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002274 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2275 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002276 }),
2277 },
2278 })
2279}
2280
2281func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002282 runCcLibraryTestCase(t, Bp2buildTestCase{
2283 ModuleTypeUnderTest: "cc_library",
2284 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2285 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002286 name: "foo",
2287 srcs: ["foo.proto"],
2288 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002289 type: "full",
2290 },
2291 include_build_directory: false,
2292}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002293 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002294 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002295 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002296 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002297 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002298 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002299 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002300 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002301 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002302 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2303 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002304 }),
2305 },
2306 })
2307}
2308
2309func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002310 runCcLibraryTestCase(t, Bp2buildTestCase{
2311 ModuleTypeUnderTest: "cc_library",
2312 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2313 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002314 name: "foo",
2315 srcs: ["foo.proto"],
2316 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002317 type: "lite",
2318 },
2319 include_build_directory: false,
2320}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002321 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002322 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002323 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002324 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002325 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002326 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002327 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002328 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002329 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002330 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2331 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002332 }),
2333 },
2334 })
2335}
2336
2337func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002338 runCcLibraryTestCase(t, Bp2buildTestCase{
2339 ModuleTypeUnderTest: "cc_library",
2340 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2341 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002342 name: "foo",
2343 srcs: ["foo.proto"],
2344 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002345 export_proto_headers: true,
2346 },
2347 include_build_directory: false,
2348}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002349 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002350 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002351 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002352 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002353 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002354 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002355 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002356 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002357 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002358 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2359 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002360 }),
2361 },
2362 })
2363}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002364
Yu Liu2d136142022-08-18 14:46:13 -07002365func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2366 runCcLibraryTestCase(t, Bp2buildTestCase{
2367 ModuleTypeUnderTest: "cc_library",
2368 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2369 Blueprint: soongCcProtoPreamble + `cc_library {
2370 name: "foo",
2371 srcs: ["foo.proto"],
2372 proto: {
2373 include_dirs: ["external/protobuf/src"],
2374 },
2375 include_build_directory: false,
2376}`,
2377 ExpectedBazelTargets: []string{
2378 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2379 "srcs": `["foo.proto"]`,
2380 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2381 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2382 "deps": `[":foo_proto"]`,
2383 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2384 "deps": `[":libprotobuf-cpp-lite"]`,
2385 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2386 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002387 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2388 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002389 }),
2390 },
2391 })
2392}
2393
2394func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2395 runCcLibraryTestCase(t, Bp2buildTestCase{
2396 ModuleTypeUnderTest: "cc_library",
2397 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2398 Blueprint: soongCcProtoPreamble + `cc_library {
2399 name: "foo",
2400 srcs: ["foo.proto"],
2401 proto: {
2402 include_dirs: ["external/protobuf/abc"],
2403 },
2404 include_build_directory: false,
2405}`,
Spandan Dasec39d512023-08-15 22:08:18 +00002406 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 -07002407 })
2408}
2409
Yu Liu2aa806b2022-09-01 11:54:47 -07002410func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2411 runCcLibraryTestCase(t, Bp2buildTestCase{
2412 ModuleTypeUnderTest: "cc_library",
2413 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2414 Blueprint: soongCcProtoPreamble + `
2415filegroup {
2416 name: "a_fg_proto",
2417 srcs: ["a_fg.proto"],
2418}
2419
2420cc_library {
2421 name: "a",
2422 srcs: [
2423 ":a_fg_proto",
2424 "a.proto",
2425 ],
2426 proto: {
2427 export_proto_headers: true,
2428 },
2429 include_build_directory: false,
2430}`,
2431 ExpectedBazelTargets: []string{
2432 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002433 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002434 "srcs": `["a.proto"]`,
2435 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2436 "deps": `[
2437 ":a_fg_proto_bp2build_converted",
2438 ":a_proto",
2439 ]`,
2440 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2441 "deps": `[":libprotobuf-cpp-lite"]`,
2442 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2443 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2444 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2445 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002446 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002447 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002448 "tags": `[
2449 "apex_available=//apex_available:anyapex",
2450 "manual",
2451 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002452 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2453 "actual": `"//.:a_fg_proto_proto"`,
2454 "tags": `[
2455 "apex_available=//apex_available:anyapex",
2456 "manual",
2457 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002458 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2459 "srcs": `["a_fg.proto"]`,
2460 }),
2461 },
2462 })
2463}
2464
2465func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2466 runCcLibraryTestCase(t, Bp2buildTestCase{
2467 ModuleTypeUnderTest: "cc_library",
2468 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2469 Blueprint: soongCcProtoPreamble + `
2470filegroup {
2471 name: "a_fg_proto",
2472 srcs: ["a_fg.proto"],
2473}
2474
2475cc_library {
2476 name: "a",
2477 srcs: [
2478 ":a_fg_proto",
2479 ],
2480 proto: {
2481 export_proto_headers: true,
2482 },
2483 include_build_directory: false,
2484}`,
2485 ExpectedBazelTargets: []string{
2486 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2487 "deps": `[":a_fg_proto_bp2build_converted"]`,
2488 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2489 "deps": `[":libprotobuf-cpp-lite"]`,
2490 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2491 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2492 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2493 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002494 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002495 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002496 "tags": `[
2497 "apex_available=//apex_available:anyapex",
2498 "manual",
2499 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002500 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2501 "actual": `"//.:a_fg_proto_proto"`,
2502 "tags": `[
2503 "apex_available=//apex_available:anyapex",
2504 "manual",
2505 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002506 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2507 "srcs": `["a_fg.proto"]`,
2508 }),
2509 },
2510 })
2511}
2512
2513func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2514 runCcLibraryTestCase(t, Bp2buildTestCase{
2515 ModuleTypeUnderTest: "cc_library",
2516 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002517 StubbedBuildDefinitions: []string{"//path/to/A:a_fg_proto"},
Yu Liu2aa806b2022-09-01 11:54:47 -07002518 Filesystem: map[string]string{
2519 "path/to/A/Android.bp": `
2520filegroup {
2521 name: "a_fg_proto",
2522 srcs: ["a_fg.proto"],
2523}`,
2524 },
2525 Blueprint: soongCcProtoPreamble + `
2526cc_library {
2527 name: "a",
2528 srcs: [
2529 ":a_fg_proto",
2530 "a.proto",
2531 ],
2532 proto: {
2533 export_proto_headers: true,
2534 },
2535 include_build_directory: false,
2536}`,
2537 ExpectedBazelTargets: []string{
2538 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002539 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002540 "srcs": `["a.proto"]`,
2541 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2542 "deps": `[
2543 "//path/to/A:a_fg_proto_bp2build_converted",
2544 ":a_proto",
2545 ]`,
2546 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2547 "deps": `[":libprotobuf-cpp-lite"]`,
2548 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2549 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2550 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2551 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2552 }),
2553 },
2554 })
2555}
2556
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002557func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002558 runCcLibraryTestCase(t, Bp2buildTestCase{
2559 ModuleTypeUnderTest: "cc_library",
2560 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002561 StubbedBuildDefinitions: []string{"a_fg_proto", "b_protos", "c-proto-srcs", "proto-srcs-d"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002562 Blueprint: soongCcProtoPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +00002563 simpleModule("filegroup", "a_fg_proto") +
2564 simpleModule("filegroup", "b_protos") +
2565 simpleModule("filegroup", "c-proto-srcs") +
2566 simpleModule("filegroup", "proto-srcs-d") + `
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002567cc_library {
2568 name: "a",
2569 srcs: [":a_fg_proto"],
2570 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002571 export_proto_headers: true,
2572 },
2573 include_build_directory: false,
2574}
2575
2576cc_library {
2577 name: "b",
2578 srcs: [":b_protos"],
2579 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002580 export_proto_headers: true,
2581 },
2582 include_build_directory: false,
2583}
2584
2585cc_library {
2586 name: "c",
2587 srcs: [":c-proto-srcs"],
2588 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002589 export_proto_headers: true,
2590 },
2591 include_build_directory: false,
2592}
2593
2594cc_library {
2595 name: "d",
2596 srcs: [":proto-srcs-d"],
2597 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002598 export_proto_headers: true,
2599 },
2600 include_build_directory: false,
2601}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002602 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002603 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002604 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002605 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002606 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002607 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002608 "deps": `[":libprotobuf-cpp-lite"]`,
2609 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2610 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2611 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2612 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002613 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002614 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2615 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2616 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2617 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2618 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002619 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002620 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002621 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002622 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002623 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002624 "deps": `[":libprotobuf-cpp-lite"]`,
2625 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2626 "srcs": `[":b_protos_cpp_srcs"]`,
2627 "srcs_as": `[":b_protos_as_srcs"]`,
2628 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002629 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002630 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2631 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2632 "srcs": `[":b_protos_cpp_srcs"]`,
2633 "srcs_as": `[":b_protos_as_srcs"]`,
2634 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002635 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002636 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002637 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002638 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002639 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002640 "deps": `[":libprotobuf-cpp-lite"]`,
2641 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2642 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2643 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2644 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002645 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002646 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2647 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2648 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2649 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2650 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002651 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002652 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002653 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002654 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002655 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002656 "deps": `[":libprotobuf-cpp-lite"]`,
2657 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2658 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2659 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2660 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002661 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002662 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2663 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2664 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2665 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2666 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2667 }),
2668 },
2669 })
2670}
Chris Parsons58852a02021-12-09 18:10:18 -05002671
2672func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002673 runCcLibraryTestCase(t, Bp2buildTestCase{
2674 ModuleTypeUnderTest: "cc_library",
2675 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2676 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002677 name: "foo",
2678 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002679 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002680 target: {
2681 darwin: {
2682 enabled: false,
2683 },
2684 windows: {
2685 enabled: false,
2686 },
2687 linux_glibc_x86: {
2688 enabled: false,
2689 },
2690 },
2691 include_build_directory: false,
2692}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002693 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002694 "srcs": `["foo.cpp"]`,
2695 "target_compatible_with": `select({
2696 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2697 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2698 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2699 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2700 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2701 "//conditions:default": [],
2702 })`,
2703 }),
2704 })
2705}
2706
2707func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002708 runCcLibraryTestCase(t, Bp2buildTestCase{
2709 ModuleTypeUnderTest: "cc_library",
2710 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2711 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002712 name: "foo",
2713 srcs: ["foo.cpp"],
2714 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002715 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002716 target: {
2717 darwin: {
2718 enabled: true,
2719 },
2720 windows: {
2721 enabled: false,
2722 },
2723 linux_glibc_x86: {
2724 enabled: false,
2725 },
2726 },
2727 include_build_directory: false,
2728}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002729 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002730 "srcs": `["foo.cpp"]`,
2731 "target_compatible_with": `select({
2732 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2733 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2734 "//conditions:default": ["@platforms//:incompatible"],
2735 })`,
2736 }),
2737 })
2738}
2739
2740func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002741 runCcLibraryTestCase(t, Bp2buildTestCase{
2742 ModuleTypeUnderTest: "cc_library",
2743 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2744 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002745 name: "foo",
2746 srcs: ["foo.cpp"],
2747 enabled: false,
2748 shared: {
2749 enabled: true,
2750 },
2751 target: {
2752 android: {
2753 shared: {
2754 enabled: false,
2755 },
2756 }
2757 },
2758 include_build_directory: false,
2759}`,
Alixe06d75b2022-08-31 18:28:19 +00002760 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002761 "srcs": `["foo.cpp"]`,
2762 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002763 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002764 "srcs": `["foo.cpp"]`,
2765 "target_compatible_with": `select({
2766 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2767 "//conditions:default": [],
2768 })`,
2769 }),
2770 },
2771 })
2772}
2773
2774func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002775 runCcLibraryTestCase(t, Bp2buildTestCase{
2776 ModuleTypeUnderTest: "cc_library",
2777 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2778 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002779 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002780 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002781 srcs: ["foo.cpp"],
2782 shared: {
2783 enabled: false
2784 },
2785 target: {
2786 darwin: {
2787 enabled: true,
2788 },
2789 windows: {
2790 enabled: false,
2791 },
2792 linux_glibc_x86: {
2793 shared: {
2794 enabled: true,
2795 },
2796 },
2797 },
2798 include_build_directory: false,
2799}`,
Alixe06d75b2022-08-31 18:28:19 +00002800 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002801 "srcs": `["foo.cpp"]`,
2802 "target_compatible_with": `select({
2803 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2804 "//conditions:default": [],
2805 })`,
Alixe06d75b2022-08-31 18:28:19 +00002806 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002807 "srcs": `["foo.cpp"]`,
2808 "target_compatible_with": `select({
2809 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2810 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2811 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2812 "//conditions:default": ["@platforms//:incompatible"],
2813 })`,
2814 }),
2815 }})
2816}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002817
2818func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002819 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002820 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002821 })
2822 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Spandan Das04f9f4c2023-09-13 23:59:05 +00002823 "api_surface": `"module-libapi"`,
Sam Delmerico5f906492023-03-15 18:06:18 -04002824 "soname": `"a.so"`,
2825 "source_library_label": `"//foo/bar:a"`,
2826 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002827 "stubs_versions": `[
2828 "28",
2829 "29",
2830 "current",
2831 ]`,
2832 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002833 runCcLibraryTestCase(t, Bp2buildTestCase{
2834 Description: "cc_library stubs",
2835 ModuleTypeUnderTest: "cc_library",
2836 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2837 Dir: "foo/bar",
2838 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002839 "foo/bar/Android.bp": `
2840cc_library {
2841 name: "a",
2842 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2843 bazel_module: { bp2build_available: true },
2844 include_build_directory: false,
2845}
2846`,
2847 },
Wei Li81852ca2022-07-27 00:22:06 -07002848 Blueprint: soongCcLibraryPreamble,
2849 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002850 },
2851 )
2852}
Liz Kammerf38a8372022-02-04 15:39:00 -05002853
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002854func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2855 runCcLibraryTestCase(t, Bp2buildTestCase{
2856 Description: "stub target generation of the same lib across configs should not result in duplicates",
2857 ModuleTypeUnderTest: "cc_library",
2858 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2859 Filesystem: map[string]string{
2860 "bar.map.txt": "",
2861 },
Chris Parsonscd209032023-09-19 01:12:48 +00002862 StubbedBuildDefinitions: []string{"barlib"},
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002863 Blueprint: `
2864cc_library {
2865 name: "barlib",
2866 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002867}
2868cc_library {
2869 name: "foolib",
2870 shared_libs: ["barlib"],
2871 target: {
2872 android: {
2873 shared_libs: ["barlib"],
2874 },
2875 },
2876 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002877 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002878}`,
2879 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2880 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00002881 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002882 "//conditions:default": [":barlib"],
2883 })`,
2884 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002885 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002886 }),
2887 })
2888}
2889
Liz Kammerffc17e42022-11-23 09:42:05 -05002890func TestCcLibraryExcludesLibsHost(t *testing.T) {
2891 runCcLibraryTestCase(t, Bp2buildTestCase{
2892 ModuleTypeUnderTest: "cc_library",
2893 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2894 Filesystem: map[string]string{
2895 "bar.map.txt": "",
2896 },
Chris Parsonscd209032023-09-19 01:12:48 +00002897 StubbedBuildDefinitions: []string{"bazlib", "quxlib", "barlib"},
2898 Blueprint: simpleModule("cc_library", "bazlib") + `
Liz Kammerffc17e42022-11-23 09:42:05 -05002899cc_library {
2900 name: "quxlib",
2901 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002902}
2903cc_library {
2904 name: "barlib",
2905 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002906}
2907cc_library {
2908 name: "foolib",
2909 shared_libs: ["barlib", "quxlib"],
2910 target: {
2911 host: {
2912 shared_libs: ["bazlib"],
2913 exclude_shared_libs: ["barlib"],
2914 },
2915 },
2916 include_build_directory: false,
2917 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002918 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002919}`,
2920 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2921 "implementation_dynamic_deps": `select({
2922 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002923 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002924 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002925 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
2926 "//build/bazel/platforms/os:windows": [":bazlib"],
2927 "//conditions:default": [],
2928 }) + select({
2929 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002930 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002931 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002932 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
2933 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00002934 "//build/bazel/rules/apex:foo": [
2935 "@api_surfaces//module-libapi/current:barlib",
2936 "@api_surfaces//module-libapi/current:quxlib",
2937 ],
Liz Kammerffc17e42022-11-23 09:42:05 -05002938 "//conditions:default": [
2939 ":barlib",
2940 ":quxlib",
2941 ],
2942 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002943 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05002944 }),
2945 })
2946}
2947
Liz Kammerf38a8372022-02-04 15:39:00 -05002948func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002949 runCcLibraryTestCase(t, Bp2buildTestCase{
2950 ModuleTypeUnderTest: "cc_library",
2951 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2952 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05002953 name: "foo",
2954 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2955 include_build_directory: false,
2956}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002957 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05002958 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2959 }),
2960 })
2961}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002962
2963func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002964 runCcLibraryTestCase(t, Bp2buildTestCase{
2965 ModuleTypeUnderTest: "cc_library",
2966 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2967 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002968 "foo.c": "",
2969 "bar.cc": "",
2970 "foo1.l": "",
2971 "bar1.ll": "",
2972 "foo2.l": "",
2973 "bar2.ll": "",
2974 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002975 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002976 name: "foo_lib",
2977 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2978 lex: { flags: ["--foo_flags"] },
2979 include_build_directory: false,
2980 bazel_module: { bp2build_available: true },
2981}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002982 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00002983 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002984 "srcs": `[
2985 "foo1.l",
2986 "foo2.l",
2987 ]`,
2988 "lexopts": `["--foo_flags"]`,
2989 }),
Alixe06d75b2022-08-31 18:28:19 +00002990 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002991 "srcs": `[
2992 "bar1.ll",
2993 "bar2.ll",
2994 ]`,
2995 "lexopts": `["--foo_flags"]`,
2996 }),
2997 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002998 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002999 "srcs": `[
3000 "bar.cc",
3001 ":foo_lib_genlex_ll",
3002 ]`,
3003 "srcs_c": `[
3004 "foo.c",
3005 ":foo_lib_genlex_l",
3006 ]`,
3007 })...),
3008 })
3009}
Cole Faust6b29f592022-08-09 09:50:56 -07003010
3011func TestCCLibraryRuntimeDeps(t *testing.T) {
3012 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3013 Blueprint: `cc_library_shared {
3014 name: "bar",
3015}
3016
3017cc_library {
3018 name: "foo",
Chris Parsonscd209032023-09-19 01:12:48 +00003019 runtime_libs: ["bar"],
Cole Faust6b29f592022-08-09 09:50:56 -07003020}`,
3021 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003022 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003023 "local_includes": `["."]`,
3024 }),
Alixe06d75b2022-08-31 18:28:19 +00003025 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003026 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003027 "local_includes": `["."]`,
3028 }),
Alixe06d75b2022-08-31 18:28:19 +00003029 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003030 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003031 "local_includes": `["."]`,
3032 }),
3033 },
3034 })
3035}
Cole Faust5fa4e962022-08-22 14:31:04 -07003036
3037func TestCcLibraryWithInstructionSet(t *testing.T) {
3038 runCcLibraryTestCase(t, Bp2buildTestCase{
3039 ModuleTypeUnderTest: "cc_library",
3040 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3041 Blueprint: `cc_library {
3042 name: "foo",
3043 arch: {
3044 arm: {
3045 instruction_set: "arm",
3046 }
3047 }
3048}
3049`,
3050 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3051 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003052 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003053 "//conditions:default": [],
3054 })`,
3055 "local_includes": `["."]`,
3056 }),
3057 })
3058}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003059
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003060func TestCcLibraryEmptySuffix(t *testing.T) {
3061 runCcLibraryTestCase(t, Bp2buildTestCase{
3062 Description: "cc_library with empty suffix",
3063 ModuleTypeUnderTest: "cc_library",
3064 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3065 Filesystem: map[string]string{
3066 "foo.c": "",
3067 },
3068 Blueprint: `cc_library {
3069 name: "foo",
3070 suffix: "",
3071 srcs: ["foo.c"],
3072 include_build_directory: false,
3073}`,
3074 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003075 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 -05003076 "srcs_c": `["foo.c"]`,
3077 }),
Alixe06d75b2022-08-31 18:28:19 +00003078 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003079 "srcs_c": `["foo.c"]`,
3080 "suffix": `""`,
3081 }),
3082 },
3083 })
3084}
3085
3086func TestCcLibrarySuffix(t *testing.T) {
3087 runCcLibraryTestCase(t, Bp2buildTestCase{
3088 Description: "cc_library with suffix",
3089 ModuleTypeUnderTest: "cc_library",
3090 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3091 Filesystem: map[string]string{
3092 "foo.c": "",
3093 },
3094 Blueprint: `cc_library {
3095 name: "foo",
3096 suffix: "-suf",
3097 srcs: ["foo.c"],
3098 include_build_directory: false,
3099}`,
3100 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003101 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 -05003102 "srcs_c": `["foo.c"]`,
3103 }),
Alixe06d75b2022-08-31 18:28:19 +00003104 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003105 "srcs_c": `["foo.c"]`,
3106 "suffix": `"-suf"`,
3107 }),
3108 },
3109 })
3110}
3111
3112func TestCcLibraryArchVariantSuffix(t *testing.T) {
3113 runCcLibraryTestCase(t, Bp2buildTestCase{
3114 Description: "cc_library with arch-variant suffix",
3115 ModuleTypeUnderTest: "cc_library",
3116 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3117 Filesystem: map[string]string{
3118 "foo.c": "",
3119 },
3120 Blueprint: `cc_library {
3121 name: "foo",
3122 arch: {
3123 arm64: { suffix: "-64" },
3124 arm: { suffix: "-32" },
3125 },
3126 srcs: ["foo.c"],
3127 include_build_directory: false,
3128}`,
3129 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003130 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 -05003131 "srcs_c": `["foo.c"]`,
3132 }),
Alixe06d75b2022-08-31 18:28:19 +00003133 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003134 "srcs_c": `["foo.c"]`,
3135 "suffix": `select({
3136 "//build/bazel/platforms/arch:arm": "-32",
3137 "//build/bazel/platforms/arch:arm64": "-64",
3138 "//conditions:default": None,
3139 })`,
3140 }),
3141 },
3142 })
3143}
3144
Vinh Tran367d89d2023-04-28 11:21:25 -04003145func TestCcLibraryWithAidlLibrary(t *testing.T) {
3146 runCcLibraryTestCase(t, Bp2buildTestCase{
3147 Description: "cc_library with aidl_library",
3148 ModuleTypeUnderTest: "cc_library",
3149 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3150 Blueprint: `
3151aidl_library {
3152 name: "A_aidl",
3153 srcs: ["aidl/A.aidl"],
3154 hdrs: ["aidl/Header.aidl"],
3155 strip_import_prefix: "aidl",
3156}
3157cc_library {
3158 name: "foo",
3159 aidl: {
3160 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003161 },
3162 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003163}`,
3164 ExpectedBazelTargets: []string{
3165 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3166 "srcs": `["aidl/A.aidl"]`,
3167 "hdrs": `["aidl/Header.aidl"]`,
3168 "strip_import_prefix": `"aidl"`,
3169 "tags": `["apex_available=//apex_available:anyapex"]`,
3170 }),
3171 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003172 "deps": `[":A_aidl"]`,
3173 "local_includes": `["."]`,
3174 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003175 }),
3176 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3177 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3178 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003179 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003180 }),
3181 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3182 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3183 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003184 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003185 }),
3186 },
3187 })
3188}
3189
Vinh Tran9f6796a2022-08-16 13:10:31 -04003190func TestCcLibraryWithAidlSrcs(t *testing.T) {
3191 runCcLibraryTestCase(t, Bp2buildTestCase{
3192 Description: "cc_library with aidl srcs",
3193 ModuleTypeUnderTest: "cc_library",
3194 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3195 Blueprint: `
3196filegroup {
3197 name: "A_aidl",
3198 srcs: ["aidl/A.aidl"],
3199 path: "aidl",
3200}
3201cc_library {
3202 name: "foo",
3203 srcs: [
3204 ":A_aidl",
3205 "B.aidl",
3206 ],
3207}`,
3208 ExpectedBazelTargets: []string{
3209 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3210 "srcs": `["aidl/A.aidl"]`,
3211 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003212 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003213 }),
Alixe06d75b2022-08-31 18:28:19 +00003214 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003215 "srcs": `["B.aidl"]`,
3216 }),
Alixe06d75b2022-08-31 18:28:19 +00003217 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003218 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003219 "deps": `[
3220 ":A_aidl",
3221 ":foo_aidl_library",
3222 ]`,
3223 }),
Alixe06d75b2022-08-31 18:28:19 +00003224 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003225 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3226 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003227 }),
Alixe06d75b2022-08-31 18:28:19 +00003228 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003229 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3230 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003231 }),
3232 },
3233 })
3234}
3235
3236func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3237 runCcLibraryTestCase(t, Bp2buildTestCase{
3238 Description: "cc_library with non aidl filegroup",
3239 ModuleTypeUnderTest: "cc_library",
3240 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003241 StubbedBuildDefinitions: []string{"//path/to/A:A_aidl"},
Vinh Tran9f6796a2022-08-16 13:10:31 -04003242 Filesystem: map[string]string{
3243 "path/to/A/Android.bp": `
3244filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003245 name: "A_aidl",
3246 srcs: ["aidl/A.aidl"],
3247 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003248}`,
3249 },
3250 Blueprint: `
3251cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003252 name: "foo",
3253 srcs: [
3254 ":A_aidl",
3255 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003256}`,
3257 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003258 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003259 "local_includes": `["."]`,
3260 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003261 }),
Alixe06d75b2022-08-31 18:28:19 +00003262 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003263 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3264 "local_includes": `["."]`,
3265 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003266 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003267 "local_includes": `["."]`,
3268 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003269 }),
3270 },
3271 })
3272}
3273
3274func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003275 t.Parallel()
3276
3277 expectedBazelTargets := []string{
3278 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003279 "local_includes": `["."]`,
3280 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003281 }),
3282 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3283 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3284 "local_includes": `["."]`,
3285 }),
3286 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3287 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3288 "local_includes": `["."]`,
3289 }),
3290 }
3291 testCases := []struct {
3292 description string
3293 bp string
3294 expectedBazelTargets []string
3295 }{
3296 {
3297 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3298 bp: `
3299 cc_library {
3300 name: "foo",
3301 srcs: [
3302 "Foo.aidl",
3303 ],
3304 aidl: {
3305 export_aidl_headers: true,
3306 }
3307 }`,
3308 expectedBazelTargets: append(
3309 expectedBazelTargets,
3310 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3311 "srcs": `["Foo.aidl"]`,
3312 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003313 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003314 {
3315 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3316 bp: `
3317 aidl_library {
3318 name: "foo_aidl_library",
3319 srcs: ["Foo.aidl"],
3320 }
3321 cc_library {
3322 name: "foo",
3323 aidl: {
3324 libs: ["foo_aidl_library"],
3325 export_aidl_headers: true,
3326 }
3327 }`,
3328 expectedBazelTargets: append(
3329 expectedBazelTargets,
3330 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3331 "srcs": `["Foo.aidl"]`,
3332 "tags": `["apex_available=//apex_available:anyapex"]`,
3333 }),
3334 ),
3335 },
3336 }
3337
3338 for _, testCase := range testCases {
3339 runCcLibraryTestCase(t, Bp2buildTestCase{
3340 Description: "cc_library with export aidl headers",
3341 ModuleTypeUnderTest: "cc_library",
3342 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3343 Blueprint: testCase.bp,
3344 ExpectedBazelTargets: testCase.expectedBazelTargets,
3345 })
3346 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003347}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003348
3349func TestCcLibraryWithTargetApex(t *testing.T) {
3350 runCcLibraryTestCase(t, Bp2buildTestCase{
3351 Description: "cc_library with target.apex",
3352 ModuleTypeUnderTest: "cc_library",
3353 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3354 Blueprint: `
3355cc_library {
3356 name: "foo",
3357 shared_libs: ["bar", "baz"],
3358 static_libs: ["baz", "buh"],
3359 target: {
3360 apex: {
3361 exclude_shared_libs: ["bar"],
3362 exclude_static_libs: ["buh"],
3363 }
3364 }
3365}`,
3366 ExpectedBazelTargets: []string{
3367 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3368 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003369 "//build/bazel/rules/apex:in_apex": [],
3370 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003371 })`,
3372 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003373 "//build/bazel/rules/apex:in_apex": [],
3374 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003375 })`,
3376 "local_includes": `["."]`,
3377 }),
3378 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3379 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003380 "//build/bazel/rules/apex:in_apex": [],
3381 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003382 })`,
3383 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003384 "//build/bazel/rules/apex:in_apex": [],
3385 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003386 })`,
3387 "local_includes": `["."]`,
3388 }),
3389 },
3390 })
3391}
3392
3393func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3394 runCcLibraryTestCase(t, Bp2buildTestCase{
3395 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3396 ModuleTypeUnderTest: "cc_library",
3397 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3398 Blueprint: `
3399cc_library_static {
3400 name: "foo",
3401 shared_libs: ["bar", "baz"],
3402 static_libs: ["abc"],
3403 export_shared_lib_headers: ["baz"],
3404 export_static_lib_headers: ["abc"],
3405 target: {
3406 apex: {
3407 exclude_shared_libs: ["baz", "bar"],
3408 exclude_static_libs: ["abc"],
3409 }
3410 }
3411}`,
3412 ExpectedBazelTargets: []string{
3413 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3414 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003415 "//build/bazel/rules/apex:in_apex": [],
3416 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003417 })`,
3418 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003419 "//build/bazel/rules/apex:in_apex": [],
3420 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003421 })`,
3422 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003423 "//build/bazel/rules/apex:in_apex": [],
3424 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003425 })`,
3426 "local_includes": `["."]`,
3427 }),
3428 },
3429 })
3430}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003431
3432func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3433 runCcLibraryTestCase(t, Bp2buildTestCase{
3434 Description: "cc_library with sysprop sources",
3435 ModuleTypeUnderTest: "cc_library",
3436 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3437 Blueprint: `
3438cc_library {
3439 name: "foo",
3440 srcs: [
3441 "bar.sysprop",
3442 "baz.sysprop",
3443 "blah.cpp",
3444 ],
3445 min_sdk_version: "5",
3446}`,
3447 ExpectedBazelTargets: []string{
3448 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3449 "srcs": `[
3450 "bar.sysprop",
3451 "baz.sysprop",
3452 ]`,
3453 }),
3454 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3455 "dep": `":foo_sysprop_library"`,
3456 "min_sdk_version": `"5"`,
3457 }),
3458 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3459 "srcs": `["blah.cpp"]`,
3460 "local_includes": `["."]`,
3461 "min_sdk_version": `"5"`,
3462 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3463 }),
3464 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3465 "srcs": `["blah.cpp"]`,
3466 "local_includes": `["."]`,
3467 "min_sdk_version": `"5"`,
3468 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3469 }),
3470 },
3471 })
3472}
3473
3474func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3475 runCcLibraryTestCase(t, Bp2buildTestCase{
3476 Description: "cc_library with sysprop sources in some configs but not others",
3477 ModuleTypeUnderTest: "cc_library",
3478 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3479 Blueprint: `
3480cc_library {
3481 name: "foo",
3482 host_supported: true,
3483 srcs: [
3484 "blah.cpp",
3485 ],
3486 target: {
3487 android: {
3488 srcs: ["bar.sysprop"],
3489 },
3490 },
3491 min_sdk_version: "5",
3492}`,
3493 ExpectedBazelTargets: []string{
3494 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3495 "srcs": `select({
3496 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3497 "//conditions:default": [],
3498 })`,
3499 }),
3500 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3501 "dep": `":foo_sysprop_library"`,
3502 "min_sdk_version": `"5"`,
3503 }),
3504 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3505 "srcs": `["blah.cpp"]`,
3506 "local_includes": `["."]`,
3507 "min_sdk_version": `"5"`,
3508 "whole_archive_deps": `select({
3509 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3510 "//conditions:default": [],
3511 })`,
3512 }),
3513 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3514 "srcs": `["blah.cpp"]`,
3515 "local_includes": `["."]`,
3516 "min_sdk_version": `"5"`,
3517 "whole_archive_deps": `select({
3518 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3519 "//conditions:default": [],
3520 })`,
3521 }),
3522 },
3523 })
3524}
Vinh Tran395a1e92022-09-16 18:27:29 -04003525
Sam Delmerico512437b2023-03-17 11:34:15 -04003526func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003527 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003528 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003529 ModuleTypeUnderTest: "cc_library",
3530 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003531 StubbedBuildDefinitions: []string{"bar-static", "baz-static", "bar-shared", "baz-shared"},
Vinh Tran395a1e92022-09-16 18:27:29 -04003532 Blueprint: `
3533cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003534 name: "foo",
3535 srcs: [
3536 "Foo.aidl",
3537 ],
3538 static_libs: [
3539 "bar-static",
3540 "baz-static",
3541 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003542 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003543 "bar-shared",
3544 "baz-shared",
3545 ],
3546 export_static_lib_headers: [
3547 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003548 ],
3549 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003550 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003551 ],
3552}` +
Chris Parsonscd209032023-09-19 01:12:48 +00003553 simpleModule("cc_library_static", "bar-static") +
3554 simpleModule("cc_library_static", "baz-static") +
3555 simpleModule("cc_library", "bar-shared") +
3556 simpleModule("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003557 ExpectedBazelTargets: []string{
3558 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3559 "srcs": `["Foo.aidl"]`,
3560 }),
3561 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003562 "local_includes": `["."]`,
3563 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003564 "implementation_deps": `[
3565 ":baz-static",
3566 ":bar-static",
3567 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003568 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003569 ":baz-shared",
3570 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003571 ]`,
3572 }),
3573 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3574 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003575 "deps": `[":baz-static"]`,
3576 "implementation_deps": `[":bar-static"]`,
3577 "dynamic_deps": `[":baz-shared"]`,
3578 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003579 "local_includes": `["."]`,
3580 }),
3581 },
3582 })
3583}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003584
3585func TestCcLibraryWithTidy(t *testing.T) {
3586 runCcLibraryTestCase(t, Bp2buildTestCase{
3587 Description: "cc_library uses tidy properties",
3588 ModuleTypeUnderTest: "cc_library",
3589 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3590 Blueprint: `
3591cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003592 name: "foo",
3593 srcs: ["foo.cpp"],
3594}
3595cc_library_static {
3596 name: "foo-no-tidy",
3597 srcs: ["foo.cpp"],
3598 tidy: false,
3599}
3600cc_library_static {
3601 name: "foo-tidy",
3602 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003603 tidy: true,
3604 tidy_checks: ["check1", "check2"],
3605 tidy_checks_as_errors: ["check1error", "check2error"],
3606 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003607 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003608}`,
3609 ExpectedBazelTargets: []string{
3610 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3611 "local_includes": `["."]`,
3612 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003613 }),
3614 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3615 "local_includes": `["."]`,
3616 "srcs": `["foo.cpp"]`,
3617 "tidy": `"never"`,
3618 }),
3619 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3620 "local_includes": `["."]`,
3621 "srcs": `["foo.cpp"]`,
3622 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003623 "tidy_checks": `[
3624 "check1",
3625 "check2",
3626 ]`,
3627 "tidy_checks_as_errors": `[
3628 "check1error",
3629 "check2error",
3630 ]`,
3631 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003632 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003633 }),
3634 },
3635 })
3636}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003637
Vinh Tran99270ea2022-11-28 11:15:23 -05003638func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3639 bp := `
3640cc_library {
3641 name: "foo",
3642 afdo: true,
3643 include_build_directory: false,
3644}`
3645
3646 // TODO(b/260714900): Add test case for arch-specific afdo profile
3647 testCases := []struct {
3648 description string
3649 filesystem map[string]string
3650 expectedBazelTargets []string
3651 }{
3652 {
3653 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003654 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003655 "vendor/google_data/pgo_profile/sampling/Android.bp": "",
3656 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003657 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003658 expectedBazelTargets: []string{
3659 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3660 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3661 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3662 }),
3663 },
3664 },
3665 {
3666 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003667 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003668 "toolchain/pgo-profiles/sampling/Android.bp": "",
3669 "toolchain/pgo-profiles/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003670 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003671 expectedBazelTargets: []string{
3672 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3673 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3674 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3675 }),
3676 },
3677 },
3678 {
3679 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003680 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003681 "toolchain/pgo-profiles/sampling/Android.bp": "",
3682 "toolchain/pgo-profiles/sampling/bar.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003683 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003684 expectedBazelTargets: []string{
3685 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3686 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3687 },
3688 },
3689 {
3690 description: "cc_library with afdo enabled but profile doesn't exist",
3691 expectedBazelTargets: []string{
3692 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3693 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3694 },
3695 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003696 {
3697 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3698 filesystem: map[string]string{
3699 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3700 },
3701 expectedBazelTargets: []string{
3702 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3703 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3704 },
3705 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003706 }
3707 for _, testCase := range testCases {
3708 t.Run(testCase.description, func(t *testing.T) {
3709 runCcLibraryTestCase(t, Bp2buildTestCase{
3710 ExpectedBazelTargets: testCase.expectedBazelTargets,
3711 ModuleTypeUnderTest: "cc_library",
3712 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3713 Description: testCase.description,
3714 Blueprint: binaryReplacer.Replace(bp),
3715 Filesystem: testCase.filesystem,
3716 })
3717 })
3718 }
3719}
3720
Yu Liu56ccb1a2022-11-12 10:47:07 -08003721func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3722 runCcLibraryTestCase(t, Bp2buildTestCase{
3723 Description: "cc_library with header abi checker",
3724 ModuleTypeUnderTest: "cc_library",
3725 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3726 Blueprint: `cc_library {
3727 name: "foo",
3728 header_abi_checker: {
3729 enabled: true,
3730 symbol_file: "a.map.txt",
3731 exclude_symbol_versions: [
3732 "29",
3733 "30",
3734 ],
3735 exclude_symbol_tags: [
3736 "tag1",
3737 "tag2",
3738 ],
3739 check_all_apis: true,
3740 diff_flags: ["-allow-adding-removing-weak-symbols"],
3741 },
3742 include_build_directory: false,
3743}`,
3744 ExpectedBazelTargets: []string{
3745 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3746 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3747 "abi_checker_enabled": `True`,
3748 "abi_checker_symbol_file": `"a.map.txt"`,
3749 "abi_checker_exclude_symbol_versions": `[
3750 "29",
3751 "30",
3752 ]`,
3753 "abi_checker_exclude_symbol_tags": `[
3754 "tag1",
3755 "tag2",
3756 ]`,
3757 "abi_checker_check_all_apis": `True`,
3758 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3759 }),
3760 },
3761 })
3762}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003763
3764func TestCcLibraryApexAvailable(t *testing.T) {
3765 runCcLibraryTestCase(t, Bp2buildTestCase{
3766 Description: "cc_library apex_available converted to tags",
3767 ModuleTypeUnderTest: "cc_library",
3768 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3769 Blueprint: soongCcLibraryPreamble + `
3770cc_library {
3771 name: "a",
3772 srcs: ["a.cpp"],
3773 apex_available: ["com.android.foo"],
3774}
3775`,
3776 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3777 "tags": `["apex_available=com.android.foo"]`,
3778 "srcs": `["a.cpp"]`,
3779 "local_includes": `["."]`,
3780 }),
3781 },
3782 )
3783}
3784
3785func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3786 runCcLibraryTestCase(t, Bp2buildTestCase{
3787 Description: "cc_library apex_available converted to multiple tags",
3788 ModuleTypeUnderTest: "cc_library",
3789 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3790 Blueprint: soongCcLibraryPreamble + `
3791cc_library {
3792 name: "a",
3793 srcs: ["a.cpp"],
3794 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3795}
3796`,
3797 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3798 "tags": `[
3799 "apex_available=com.android.foo",
3800 "apex_available=//apex_available:platform",
3801 "apex_available=com.android.bar",
3802 ]`,
3803 "srcs": `["a.cpp"]`,
3804 "local_includes": `["."]`,
3805 }),
3806 },
3807 )
3808}
Zi Wang0f828442022-12-28 11:18:11 -08003809
3810// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3811// In bp2build output, variant info(select) should go before general info.
3812// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3813func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3814 runCcLibraryTestCase(t, Bp2buildTestCase{
3815 Description: "cc_library variant prepend properties order",
3816 ModuleTypeUnderTest: "cc_library",
3817 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3818 Blueprint: soongCcLibraryPreamble + `
3819cc_library {
3820 name: "a",
3821 srcs: ["a.cpp"],
3822 export_include_dirs: ["eid1", "eid2"],
3823 export_system_include_dirs: ["esid1", "esid2"],
3824 target: {
3825 android: {
3826 export_include_dirs: ["android_eid1", "android_eid2"],
3827 export_system_include_dirs: ["android_esid1", "android_esid2"],
3828 },
3829 android_arm: {
3830 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3831 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3832 },
3833 linux: {
3834 export_include_dirs: ["linux_eid1", "linux_eid2"],
3835 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3836 },
3837 },
3838 multilib: {
3839 lib32: {
3840 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3841 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3842 },
3843 },
3844 arch: {
3845 arm: {
3846 export_include_dirs: ["arm_eid1", "arm_eid2"],
3847 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3848 },
3849 }
3850}
3851`,
3852 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3853 "export_includes": `select({
3854 "//build/bazel/platforms/os_arch:android_arm": [
3855 "android_arm_eid1",
3856 "android_arm_eid2",
3857 ],
3858 "//conditions:default": [],
3859 }) + select({
3860 "//build/bazel/platforms/os:android": [
3861 "android_eid1",
3862 "android_eid2",
3863 "linux_eid1",
3864 "linux_eid2",
3865 ],
3866 "//build/bazel/platforms/os:linux_bionic": [
3867 "linux_eid1",
3868 "linux_eid2",
3869 ],
3870 "//build/bazel/platforms/os:linux_glibc": [
3871 "linux_eid1",
3872 "linux_eid2",
3873 ],
3874 "//build/bazel/platforms/os:linux_musl": [
3875 "linux_eid1",
3876 "linux_eid2",
3877 ],
3878 "//conditions:default": [],
3879 }) + select({
3880 "//build/bazel/platforms/arch:arm": [
3881 "lib32_eid1",
3882 "lib32_eid2",
3883 "arm_eid1",
3884 "arm_eid2",
3885 ],
3886 "//build/bazel/platforms/arch:x86": [
3887 "lib32_eid1",
3888 "lib32_eid2",
3889 ],
3890 "//conditions:default": [],
3891 }) + [
3892 "eid1",
3893 "eid2",
3894 ]`,
3895 "export_system_includes": `select({
3896 "//build/bazel/platforms/os_arch:android_arm": [
3897 "android_arm_esid1",
3898 "android_arm_esid2",
3899 ],
3900 "//conditions:default": [],
3901 }) + select({
3902 "//build/bazel/platforms/os:android": [
3903 "android_esid1",
3904 "android_esid2",
3905 "linux_esid1",
3906 "linux_esid2",
3907 ],
3908 "//build/bazel/platforms/os:linux_bionic": [
3909 "linux_esid1",
3910 "linux_esid2",
3911 ],
3912 "//build/bazel/platforms/os:linux_glibc": [
3913 "linux_esid1",
3914 "linux_esid2",
3915 ],
3916 "//build/bazel/platforms/os:linux_musl": [
3917 "linux_esid1",
3918 "linux_esid2",
3919 ],
3920 "//conditions:default": [],
3921 }) + select({
3922 "//build/bazel/platforms/arch:arm": [
3923 "lib32_esid1",
3924 "lib32_esid2",
3925 "arm_esid1",
3926 "arm_esid2",
3927 ],
3928 "//build/bazel/platforms/arch:x86": [
3929 "lib32_esid1",
3930 "lib32_esid2",
3931 ],
3932 "//conditions:default": [],
3933 }) + [
3934 "esid1",
3935 "esid2",
3936 ]`,
3937 "srcs": `["a.cpp"]`,
3938 "local_includes": `["."]`,
3939 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
3940 }),
3941 },
3942 )
3943}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00003944
3945func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
3946 runCcLibraryTestCase(t, Bp2buildTestCase{
3947 Description: "cc_library has correct features when integer_overflow property is provided",
3948 ModuleTypeUnderTest: "cc_library",
3949 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3950 Blueprint: `
3951cc_library {
3952 name: "foo",
3953 sanitize: {
3954 integer_overflow: true,
3955 },
3956}
3957`,
3958 ExpectedBazelTargets: []string{
3959 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3960 "features": `["ubsan_integer_overflow"]`,
3961 "local_includes": `["."]`,
3962 }),
3963 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3964 "features": `["ubsan_integer_overflow"]`,
3965 "local_includes": `["."]`,
3966 }),
3967 },
3968 })
3969}
3970
3971func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
3972 runCcLibraryTestCase(t, Bp2buildTestCase{
3973 Description: "cc_library has correct features when misc_undefined property is provided",
3974 ModuleTypeUnderTest: "cc_library",
3975 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3976 Blueprint: `
3977cc_library {
3978 name: "foo",
3979 sanitize: {
3980 misc_undefined: ["undefined", "nullability"],
3981 },
3982}
3983`,
3984 ExpectedBazelTargets: []string{
3985 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3986 "features": `[
3987 "ubsan_undefined",
3988 "ubsan_nullability",
3989 ]`,
3990 "local_includes": `["."]`,
3991 }),
3992 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3993 "features": `[
3994 "ubsan_undefined",
3995 "ubsan_nullability",
3996 ]`,
3997 "local_includes": `["."]`,
3998 }),
3999 },
4000 })
4001}
4002
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004003func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
4004 runCcLibraryTestCase(t, Bp2buildTestCase{
4005 Description: "cc_library has correct feature when sanitize.blocklist is provided",
4006 ModuleTypeUnderTest: "cc_library",
4007 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4008 Blueprint: `
4009cc_library {
4010 name: "foo",
4011 sanitize: {
4012 blocklist: "foo_blocklist.txt",
4013 },
4014}
4015`,
4016 ExpectedBazelTargets: []string{
4017 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004018 "copts": `select({
4019 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4020 "//conditions:default": [],
4021 })`,
4022 "additional_compiler_inputs": `select({
4023 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4024 "//conditions:default": [],
4025 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004026 "local_includes": `["."]`,
4027 }),
4028 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004029 "copts": `select({
4030 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4031 "//conditions:default": [],
4032 })`,
4033 "additional_compiler_inputs": `select({
4034 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4035 "//conditions:default": [],
4036 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004037 "local_includes": `["."]`,
4038 }),
4039 },
4040 })
4041}
4042
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004043func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4044 runCcLibraryTestCase(t, Bp2buildTestCase{
4045 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4046 ModuleTypeUnderTest: "cc_library",
4047 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4048 Blueprint: `
4049cc_library {
4050 name: "foo",
4051 sanitize: {
4052 misc_undefined: ["undefined", "nullability"],
4053 },
4054 target: {
4055 android: {
4056 sanitize: {
4057 misc_undefined: ["alignment"],
4058 },
4059 },
4060 linux_glibc: {
4061 sanitize: {
4062 integer_overflow: true,
4063 },
4064 },
4065 },
4066}
4067`,
4068 ExpectedBazelTargets: []string{
4069 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4070 "features": `[
4071 "ubsan_undefined",
4072 "ubsan_nullability",
4073 ] + select({
4074 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4075 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4076 "//conditions:default": [],
4077 })`,
4078 "local_includes": `["."]`,
4079 }),
4080 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4081 "features": `[
4082 "ubsan_undefined",
4083 "ubsan_nullability",
4084 ] + select({
4085 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4086 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4087 "//conditions:default": [],
4088 })`,
4089 "local_includes": `["."]`,
4090 }),
4091 },
4092 })
4093}
Yu Liu10174ff2023-02-21 12:05:26 -08004094
4095func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4096 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4097 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4098 ModuleTypeUnderTest: "cc_library",
4099 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004100 StubbedBuildDefinitions: []string{"barlib", "bazlib"},
Yu Liu10174ff2023-02-21 12:05:26 -08004101 Blueprint: `
4102cc_library {
4103 name: "barlib",
4104 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004105 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004106}
4107cc_library {
4108 name: "bazlib",
4109 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004110 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004111}
4112cc_library {
4113 name: "foo",
4114 shared_libs: ["barlib", "bazlib"],
4115 export_shared_lib_headers: ["bazlib"],
4116 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004117 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004118 ],
4119}`,
4120 ExpectedBazelTargets: []string{
4121 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004122 "implementation_dynamic_deps": `[":barlib"]`,
4123 "dynamic_deps": `[":bazlib"]`,
4124 "local_includes": `["."]`,
4125 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004126 }),
4127 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004128 "implementation_dynamic_deps": `[":barlib"]`,
4129 "dynamic_deps": `[":bazlib"]`,
4130 "local_includes": `["."]`,
4131 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004132 }),
4133 },
4134 })
4135}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004136
4137func TestCcLibraryWithThinLto(t *testing.T) {
4138 runCcLibraryTestCase(t, Bp2buildTestCase{
4139 Description: "cc_library has correct features when thin LTO is enabled",
4140 ModuleTypeUnderTest: "cc_library",
4141 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4142 Blueprint: `
4143cc_library {
4144 name: "foo",
4145 lto: {
4146 thin: true,
4147 },
4148}`,
4149 ExpectedBazelTargets: []string{
4150 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4151 "features": `["android_thin_lto"]`,
4152 "local_includes": `["."]`,
4153 }),
4154 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4155 "features": `["android_thin_lto"]`,
4156 "local_includes": `["."]`,
4157 }),
4158 },
4159 })
4160}
4161
4162func TestCcLibraryWithLtoNever(t *testing.T) {
4163 runCcLibraryTestCase(t, Bp2buildTestCase{
4164 Description: "cc_library has correct features when LTO is explicitly disabled",
4165 ModuleTypeUnderTest: "cc_library",
4166 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4167 Blueprint: `
4168cc_library {
4169 name: "foo",
4170 lto: {
4171 never: true,
4172 },
4173}`,
4174 ExpectedBazelTargets: []string{
4175 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4176 "features": `["-android_thin_lto"]`,
4177 "local_includes": `["."]`,
4178 }),
4179 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4180 "features": `["-android_thin_lto"]`,
4181 "local_includes": `["."]`,
4182 }),
4183 },
4184 })
4185}
4186
4187func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4188 runCcLibraryTestCase(t, Bp2buildTestCase{
4189 Description: "cc_library has correct features when LTO differs across arch and os variants",
4190 ModuleTypeUnderTest: "cc_library",
4191 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4192 Blueprint: `
4193cc_library {
4194 name: "foo",
4195 target: {
4196 android: {
4197 lto: {
4198 thin: true,
4199 },
4200 },
4201 },
4202 arch: {
4203 riscv64: {
4204 lto: {
4205 thin: false,
4206 },
4207 },
4208 },
4209}`,
4210 ExpectedBazelTargets: []string{
4211 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4212 "local_includes": `["."]`,
4213 "features": `select({
4214 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4215 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4216 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4217 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4218 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4219 "//conditions:default": [],
4220 })`}),
4221 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4222 "local_includes": `["."]`,
4223 "features": `select({
4224 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4225 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4226 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4227 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4228 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4229 "//conditions:default": [],
4230 })`}),
4231 },
4232 })
4233}
4234
4235func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4236 runCcLibraryTestCase(t, Bp2buildTestCase{
4237 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4238 ModuleTypeUnderTest: "cc_library",
4239 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4240 Blueprint: `
4241cc_library {
4242 name: "foo",
4243 lto: {
4244 never: true,
4245 },
4246 target: {
4247 android: {
4248 lto: {
4249 thin: true,
4250 never: false,
4251 },
4252 },
4253 },
4254}`,
4255 ExpectedBazelTargets: []string{
4256 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4257 "local_includes": `["."]`,
4258 "features": `select({
4259 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4260 "//conditions:default": ["-android_thin_lto"],
4261 })`,
4262 }),
4263 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4264 "local_includes": `["."]`,
4265 "features": `select({
4266 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4267 "//conditions:default": ["-android_thin_lto"],
4268 })`,
4269 }),
4270 },
4271 })
4272}
4273
4274func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4275 runCcLibraryTestCase(t, Bp2buildTestCase{
4276 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4277 ModuleTypeUnderTest: "cc_library",
4278 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4279 Blueprint: `
4280cc_library {
4281 name: "foo",
4282 lto: {
4283 thin: true,
4284 },
4285 whole_program_vtables: true,
4286}`,
4287 ExpectedBazelTargets: []string{
4288 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4289 "features": `[
4290 "android_thin_lto",
4291 "android_thin_lto_whole_program_vtables",
4292 ]`,
4293 "local_includes": `["."]`,
4294 }),
4295 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4296 "features": `[
4297 "android_thin_lto",
4298 "android_thin_lto_whole_program_vtables",
4299 ]`,
4300 "local_includes": `["."]`,
4301 }),
4302 },
4303 })
4304}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004305
4306func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4307 runCcLibraryTestCase(t, Bp2buildTestCase{
4308 Description: "cc_library changes hidden visibility flag to feature",
4309 ModuleTypeUnderTest: "cc_library",
4310 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4311 Blueprint: `
4312cc_library {
4313 name: "foo",
4314 cflags: ["-fvisibility=hidden"],
4315}`,
4316 ExpectedBazelTargets: []string{
4317 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4318 "features": `["visibility_hidden"]`,
4319 "local_includes": `["."]`,
4320 }),
4321 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4322 "features": `["visibility_hidden"]`,
4323 "local_includes": `["."]`,
4324 }),
4325 },
4326 })
4327}
4328
4329func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4330 runCcLibraryTestCase(t, Bp2buildTestCase{
4331 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4332 ModuleTypeUnderTest: "cc_library",
4333 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4334 Blueprint: `
4335cc_library {
4336 name: "foo",
4337 shared: {
4338 cflags: ["-fvisibility=hidden"],
4339 },
4340}`,
4341 ExpectedBazelTargets: []string{
4342 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4343 "local_includes": `["."]`,
4344 }),
4345 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4346 "features": `["visibility_hidden"]`,
4347 "local_includes": `["."]`,
4348 }),
4349 },
4350 })
4351}
4352
4353func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4354 runCcLibraryTestCase(t, Bp2buildTestCase{
4355 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4356 ModuleTypeUnderTest: "cc_library",
4357 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4358 Blueprint: `
4359cc_library {
4360 name: "foo",
4361 static: {
4362 cflags: ["-fvisibility=hidden"],
4363 },
4364}`,
4365 ExpectedBazelTargets: []string{
4366 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4367 "features": `["visibility_hidden"]`,
4368 "local_includes": `["."]`,
4369 }),
4370 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4371 "local_includes": `["."]`,
4372 }),
4373 },
4374 })
4375}
4376
4377func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4378 runCcLibraryTestCase(t, Bp2buildTestCase{
4379 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4380 ModuleTypeUnderTest: "cc_library",
4381 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4382 Blueprint: `
4383cc_library {
4384 name: "foo",
4385 target: {
4386 android: {
4387 cflags: ["-fvisibility=hidden"],
4388 },
4389 },
4390}`,
4391 ExpectedBazelTargets: []string{
4392 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4393 "features": `select({
4394 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4395 "//conditions:default": [],
4396 })`,
4397 "local_includes": `["."]`,
4398 }),
4399 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4400 "features": `select({
4401 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4402 "//conditions:default": [],
4403 })`,
4404 "local_includes": `["."]`,
4405 }),
4406 },
4407 })
4408}
Spandan Das4242f102023-04-19 22:31:54 +00004409
4410// Test that a config_setting specific to an apex is created by cc_library.
4411func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4412 runCcLibraryTestCase(t, Bp2buildTestCase{
4413 Description: "cc_library creates a config_setting for each apex in apex_available",
4414 ModuleTypeUnderTest: "cc_library",
4415 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4416 Dir: "build/bazel/rules/apex",
4417 Blueprint: `
4418cc_library {
4419 name: "foo",
4420 apex_available: [
4421 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4422 "myapex"
4423 ],
4424}`,
4425 ExpectedBazelTargets: []string{
4426 MakeBazelTargetNoRestrictions(
4427 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004428 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004429 AttrNameToString{
4430 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004431 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004432 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004433 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004434 },
4435 ),
4436 },
4437 })
4438}
Yu Liu93893ba2023-05-01 13:49:52 -07004439
4440func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4441 runCcLibraryTestCase(t, Bp2buildTestCase{
4442 Description: "cc_library cppflags in product variables",
4443 ModuleTypeUnderTest: "cc_library",
4444 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4445 Blueprint: soongCcLibraryPreamble + `cc_library {
4446 name: "a",
4447 srcs: ["a.cpp"],
4448 cppflags: [
4449 "-Wextra",
4450 "-DDEBUG_ONLY_CODE=0",
4451 ],
4452 product_variables: {
4453 eng: {
4454 cppflags: [
4455 "-UDEBUG_ONLY_CODE",
4456 "-DDEBUG_ONLY_CODE=1",
4457 ],
4458 },
4459 },
4460 include_build_directory: false,
4461}
4462`,
4463 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4464 "cppflags": `[
4465 "-Wextra",
4466 "-DDEBUG_ONLY_CODE=0",
4467 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004468 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004469 "-UDEBUG_ONLY_CODE",
4470 "-DDEBUG_ONLY_CODE=1",
4471 ],
4472 "//conditions:default": [],
4473 })`,
4474 "srcs": `["a.cpp"]`,
4475 }),
4476 },
4477 )
4478}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004479
4480func TestCcLibraryYaccConversion(t *testing.T) {
4481 runCcLibraryTestCase(t, Bp2buildTestCase{
4482 Description: "cc_library is built from .y/.yy files",
4483 ModuleTypeUnderTest: "cc_library",
4484 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004485 StubbedBuildDefinitions: []string{"staticlib", "sharedlib"},
Spandan Dasdf4c2132023-05-09 23:58:52 +00004486 Blueprint: soongCcLibraryPreamble + `cc_library {
4487 name: "a",
4488 srcs: [
4489 "a.cpp",
4490 "a.yy",
4491 ],
4492 shared_libs: ["sharedlib"],
4493 static_libs: ["staticlib"],
4494 yacc: {
4495 flags: ["someYaccFlag"],
4496 gen_location_hh: true,
4497 gen_position_hh: true,
4498 },
4499}
4500cc_library_static {
4501 name: "staticlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004502}
4503cc_library {
4504 name: "sharedlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004505}
4506`,
4507 ExpectedBazelTargets: []string{
4508 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4509 "src": `"a.yy"`,
4510 "implementation_deps": `[":staticlib"]`,
4511 "implementation_dynamic_deps": `[":sharedlib"]`,
4512 "flags": `["someYaccFlag"]`,
4513 "gen_location_hh": "True",
4514 "gen_position_hh": "True",
4515 "local_includes": `["."]`,
4516 }),
4517 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4518 "srcs": `["a.cpp"]`,
4519 "implementation_deps": `[":staticlib"]`,
4520 "implementation_dynamic_deps": `[":sharedlib"]`,
4521 "implementation_whole_archive_deps": `[":a_yacc"]`,
4522 "local_includes": `["."]`,
4523 }),
4524 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4525 "srcs": `["a.cpp"]`,
4526 "implementation_deps": `[":staticlib"]`,
4527 "implementation_dynamic_deps": `[":sharedlib"]`,
4528 "implementation_whole_archive_deps": `[":a_yacc"]`,
4529 "local_includes": `["."]`,
4530 }),
4531 },
4532 })
4533}
Spandan Dasfb04c412023-05-15 18:35:36 +00004534
4535func TestCcLibraryHostLdLibs(t *testing.T) {
4536 runCcLibraryTestCase(t, Bp2buildTestCase{
4537 Description: "cc_binary linker flags for host_ldlibs",
4538 ModuleTypeUnderTest: "cc_binary",
4539 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4540 Blueprint: soongCcLibraryPreamble + `cc_binary {
4541 name: "a",
4542 host_supported: true,
4543 ldflags: ["-lcommon"],
4544 target: {
4545 linux: {
4546 host_ldlibs: [
4547 "-llinux",
4548 ],
4549 },
4550 darwin: {
4551 ldflags: ["-ldarwinadditional"],
4552 host_ldlibs: [
4553 "-ldarwin",
4554 ],
4555 },
4556 windows: {
4557 host_ldlibs: [
4558 "-lwindows",
4559 ],
4560 },
4561 },
4562}
4563`,
4564 ExpectedBazelTargets: []string{
4565 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4566 "linkopts": `["-lcommon"] + select({
4567 "//build/bazel/platforms/os:darwin": [
4568 "-ldarwinadditional",
4569 "-ldarwin",
4570 ],
4571 "//build/bazel/platforms/os:linux_glibc": ["-llinux"],
4572 "//build/bazel/platforms/os:windows": ["-lwindows"],
4573 "//conditions:default": [],
4574 })`,
4575 "local_includes": `["."]`,
4576 }),
4577 },
4578 })
4579}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004580
4581func TestCcLibraryWithCfi(t *testing.T) {
4582 runCcLibraryTestCase(t, Bp2buildTestCase{
4583 Description: "cc_library has correct features when cfi is enabled",
4584 ModuleTypeUnderTest: "cc_library",
4585 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4586 Blueprint: `
4587cc_library {
4588 name: "foo",
4589 sanitize: {
4590 cfi: true,
4591 },
4592}`,
4593 ExpectedBazelTargets: []string{
4594 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4595 "features": `["android_cfi"]`,
4596 "local_includes": `["."]`,
4597 }),
4598 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4599 "features": `["android_cfi"]`,
4600 "local_includes": `["."]`,
4601 }),
4602 },
4603 })
4604}
4605
4606func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4607 runCcLibraryTestCase(t, Bp2buildTestCase{
4608 Description: "cc_library has correct features when cfi is enabled for specific variants",
4609 ModuleTypeUnderTest: "cc_library",
4610 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4611 Blueprint: `
4612cc_library {
4613 name: "foo",
4614 target: {
4615 android: {
4616 sanitize: {
4617 cfi: true,
4618 },
4619 },
4620 },
4621}`,
4622 ExpectedBazelTargets: []string{
4623 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4624 "features": `select({
4625 "//build/bazel/platforms/os:android": ["android_cfi"],
4626 "//conditions:default": [],
4627 })`,
4628 "local_includes": `["."]`,
4629 }),
4630 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4631 "features": `select({
4632 "//build/bazel/platforms/os:android": ["android_cfi"],
4633 "//conditions:default": [],
4634 })`,
4635 "local_includes": `["."]`,
4636 }),
4637 },
4638 })
4639}
4640
4641func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4642 runCcLibraryTestCase(t, Bp2buildTestCase{
4643 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4644 ModuleTypeUnderTest: "cc_library",
4645 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4646 Blueprint: `
4647cc_library {
4648 name: "foo",
4649 sanitize: {
4650 cfi: true,
4651 config: {
4652 cfi_assembly_support: true,
4653 },
4654 },
4655}`,
4656 ExpectedBazelTargets: []string{
4657 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4658 "features": `[
4659 "android_cfi",
4660 "android_cfi_assembly_support",
4661 ]`,
4662 "local_includes": `["."]`,
4663 }),
4664 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4665 "features": `[
4666 "android_cfi",
4667 "android_cfi_assembly_support",
4668 ]`,
4669 "local_includes": `["."]`,
4670 }),
4671 },
4672 })
4673}
Spandan Das39ccf932023-05-26 18:03:39 +00004674
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004675func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4676 runCcLibraryTestCase(t, Bp2buildTestCase{
4677 Description: "cc_library disables cfi when explciitly set to false in the bp",
4678 ModuleTypeUnderTest: "cc_library",
4679 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4680 Blueprint: `
4681cc_library {
4682 name: "foo",
4683 sanitize: {
4684 cfi: false,
4685 },
4686}
4687`,
4688 ExpectedBazelTargets: []string{
4689 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4690 "features": `["-android_cfi"]`,
4691 "local_includes": `["."]`,
4692 }),
4693 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4694 "features": `["-android_cfi"]`,
4695 "local_includes": `["."]`,
4696 }),
4697 },
4698 })
4699}
4700
Spandan Das39ccf932023-05-26 18:03:39 +00004701func TestCcLibraryWithStem(t *testing.T) {
4702 runCcLibraryTestCase(t, Bp2buildTestCase{
4703 Description: "cc_library with stem property",
4704 ModuleTypeUnderTest: "cc_library_shared",
4705 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4706 Blueprint: soongCcLibraryPreamble + `
4707cc_library_shared {
4708 name: "foo_with_stem_simple",
4709 stem: "foo",
4710}
4711cc_library_shared {
4712 name: "foo_with_arch_variant_stem",
4713 arch: {
4714 arm: {
4715 stem: "foo-arm",
4716 },
4717 arm64: {
4718 stem: "foo-arm64",
4719 },
4720 },
4721}
4722`,
4723 ExpectedBazelTargets: []string{
4724 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4725 "stem": `"foo"`,
4726 "local_includes": `["."]`,
4727 }),
4728 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4729 "stem": `select({
4730 "//build/bazel/platforms/arch:arm": "foo-arm",
4731 "//build/bazel/platforms/arch:arm64": "foo-arm64",
4732 "//conditions:default": None,
4733 })`,
4734 "local_includes": `["."]`,
4735 }),
4736 },
4737 })
4738}
Spandan Dasc53767e2023-08-03 23:02:26 +00004739
4740// Bazel enforces that proto_library and the .proto file are in the same bazel package
4741func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4742 tc := Bp2buildTestCase{
4743 Description: "cc_library depends on .proto files from multiple packages",
4744 ModuleTypeUnderTest: "cc_library",
4745 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4746 Blueprint: `
4747cc_library_static {
4748 name: "foo",
4749 srcs: [
4750 "foo.proto",
4751 "bar/bar.proto", // Different package because there is a bar/Android.bp
4752 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4753 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004754 proto: {
4755 canonical_path_from_root: true,
4756 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004757}
Chris Parsonscd209032023-09-19 01:12:48 +00004758` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasc53767e2023-08-03 23:02:26 +00004759 Filesystem: map[string]string{
4760 "bar/Android.bp": "",
4761 "baz/subbaz/Android.bp": "",
4762 },
4763 }
4764
4765 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4766 // Root dir
4767 tc.ExpectedBazelTargets = []string{
4768 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4769 "local_includes": `["."]`,
4770 "deps": `[":libprotobuf-cpp-lite"]`,
4771 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4772 }),
4773 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4774 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004775 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004776 }),
4777 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4778 "deps": `[
4779 ":foo_proto",
4780 "//bar:foo_proto",
4781 "//baz/subbaz:foo_proto",
4782 ]`,
4783 }),
4784 }
4785 runCcLibraryTestCase(t, tc)
4786
4787 // bar dir
4788 tc.Dir = "bar"
4789 tc.ExpectedBazelTargets = []string{
4790 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004791 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004792 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004793 }),
4794 }
4795 runCcLibraryTestCase(t, tc)
4796
4797 // baz/subbaz dir
4798 tc.Dir = "baz/subbaz"
4799 tc.ExpectedBazelTargets = []string{
4800 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004801 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004802 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004803 }),
4804 }
4805 runCcLibraryTestCase(t, tc)
4806}
4807
4808// Bazel enforces that proto_library and the .proto file are in the same bazel package
4809func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
4810 tc := Bp2buildTestCase{
4811 Description: "cc_library depends on .proto files from multiple packages",
4812 ModuleTypeUnderTest: "cc_library",
4813 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4814 Blueprint: `
4815cc_library_static {
4816 name: "foo",
4817 srcs: [
4818 "foo.proto",
4819 "bar/bar.proto", // Different package because there is a bar/Android.bp
4820 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4821 ],
4822 proto: {
4823 canonical_path_from_root: false,
4824 }
4825}
Chris Parsonscd209032023-09-19 01:12:48 +00004826` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004827 Filesystem: map[string]string{
4828 "bar/Android.bp": "",
4829 "baz/subbaz/Android.bp": "",
4830 },
4831 }
4832
4833 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4834 // Root dir
4835 tc.ExpectedBazelTargets = []string{
4836 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4837 "local_includes": `["."]`,
4838 "deps": `[":libprotobuf-cpp-lite"]`,
4839 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4840 }),
4841 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4842 "srcs": `["foo.proto"]`,
4843 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004844 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004845 }),
4846 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4847 "deps": `[
4848 ":foo_proto",
4849 "//bar:foo_proto",
4850 "//baz/subbaz:foo_proto",
4851 ]`,
4852 }),
4853 }
4854 runCcLibraryTestCase(t, tc)
4855
4856 // bar dir
4857 tc.Dir = "bar"
4858 tc.ExpectedBazelTargets = []string{
4859 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4860 "srcs": `["//bar:bar.proto"]`,
4861 "strip_import_prefix": `""`,
4862 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00004863 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004864 }),
4865 }
4866 runCcLibraryTestCase(t, tc)
4867
4868 // baz/subbaz dir
4869 tc.Dir = "baz/subbaz"
4870 tc.ExpectedBazelTargets = []string{
4871 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4872 "srcs": `["//baz/subbaz:baz.proto"]`,
4873 "strip_import_prefix": `""`,
4874 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00004875 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004876 }),
4877 }
4878 runCcLibraryTestCase(t, tc)
4879}
Spandan Dasec39d512023-08-15 22:08:18 +00004880
4881func TestProtoIncludeDirs(t *testing.T) {
4882 tc := Bp2buildTestCase{
4883 Description: "cc_library depends on .proto files using proto.include_dirs",
4884 ModuleTypeUnderTest: "cc_library",
4885 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4886 Blueprint: `
4887cc_library_static {
4888 name: "foo",
4889 srcs: [
4890 "foo.proto",
4891 ],
4892 proto: {
4893 include_dirs: ["bar"],
4894 }
4895}
Chris Parsonscd209032023-09-19 01:12:48 +00004896` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasec39d512023-08-15 22:08:18 +00004897 Filesystem: map[string]string{
4898 "bar/Android.bp": "",
4899 "bar/bar.proto": "",
4900 "bar/baz/Android.bp": "",
4901 "bar/baz/baz.proto": "",
4902 },
4903 }
4904
4905 // We will run the test 3 times and check in the root, bar and bar/baz directories
4906 // Root dir
4907 tc.ExpectedBazelTargets = []string{
4908 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4909 "local_includes": `["."]`,
4910 "deps": `[":libprotobuf-cpp-lite"]`,
4911 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4912 }),
4913 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4914 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004915 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00004916 }),
4917 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4918 "deps": `[":foo_proto"]`,
4919 "transitive_deps": `[
4920 "//bar:bar.include_dir_bp2build_generated_proto",
4921 "//bar/baz:bar.include_dir_bp2build_generated_proto",
4922 ]`,
4923 }),
4924 }
4925 runCcLibraryTestCase(t, tc)
4926
4927 // bar dir
4928 tc.Dir = "bar"
4929 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004930 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004931 "srcs": `["bar.proto"]`,
4932 "strip_import_prefix": `""`,
4933 "tags": `["manual"]`,
4934 }),
4935 }
4936 runCcLibraryTestCase(t, tc)
4937
4938 // bar/baz dir
4939 tc.Dir = "bar/baz"
4940 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004941 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004942 "srcs": `["//bar/baz:baz.proto"]`,
4943 "strip_import_prefix": `""`,
4944 "import_prefix": `"baz"`,
4945 "tags": `["manual"]`,
4946 }),
4947 }
4948 runCcLibraryTestCase(t, tc)
4949}
Spandan Das4e5a1942023-08-22 19:20:39 +00004950
Spandan Dasf26ee152023-08-24 23:21:04 +00004951func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
4952 tc := Bp2buildTestCase{
4953 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
4954 ModuleTypeUnderTest: "cc_library",
4955 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4956 Blueprint: `
4957cc_library_static {
4958 name: "foo",
4959 srcs: [
4960 "foo.proto",
4961 "bar/bar.proto",
4962 ],
4963 proto: {
4964 include_dirs: ["baz"],
4965 }
4966}
Chris Parsonscd209032023-09-19 01:12:48 +00004967` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasf26ee152023-08-24 23:21:04 +00004968 Filesystem: map[string]string{
4969 "bar/Android.bp": "", // package boundary
4970 "baz/Android.bp": "",
4971 "baz/baz.proto": "",
4972 },
4973 }
4974
4975 tc.ExpectedBazelTargets = []string{
4976 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4977 "local_includes": `["."]`,
4978 "deps": `[":libprotobuf-cpp-lite"]`,
4979 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4980 }),
4981 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4982 "srcs": `["foo.proto"]`,
4983 "tags": `["manual"]`,
4984 }),
4985 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4986 "deps": `[
4987 ":foo_proto",
4988 "//bar:foo_proto",
4989 ]`,
4990 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
4991 }),
4992 }
4993 runCcLibraryTestCase(t, tc)
4994
4995}
4996
Spandan Das4e5a1942023-08-22 19:20:39 +00004997func TestProtoLocalIncludeDirs(t *testing.T) {
4998 tc := Bp2buildTestCase{
4999 Description: "cc_library depends on .proto files using proto.local_include_dirs",
5000 ModuleTypeUnderTest: "cc_library",
5001 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005002 Blueprint: `
5003cc_library {
5004 name: "libprotobuf-cpp-lite",
5005 // TODO: b/285631638 - A stubbed proto library dependency does not work as a protolib
5006 // dependency of cc_library_static.
5007 bazel_module: { bp2build_available: false },
5008}
5009`,
Spandan Das4e5a1942023-08-22 19:20:39 +00005010 Filesystem: map[string]string{
5011 "foo/Android.bp": `cc_library_static {
5012 name: "foo",
5013 srcs: [
5014 "foo.proto",
5015 ],
5016 proto: {
5017 local_include_dirs: ["foo_subdir"],
5018 },
5019 bazel_module: { bp2build_available: true },
5020}`,
5021 "foo/foo.proto": "",
5022 "foo/foo_subdir/Android.bp": "",
5023 "foo/foo_subdir/foo_subdir.proto": "",
5024 },
5025 }
5026
5027 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5028 // foo dir
5029 tc.Dir = "foo"
5030 tc.ExpectedBazelTargets = []string{
5031 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5032 "local_includes": `["."]`,
5033 "deps": `["//:libprotobuf-cpp-lite"]`,
5034 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5035 }),
5036 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5037 "srcs": `["foo.proto"]`,
5038 "tags": `["manual"]`,
5039 }),
5040 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5041 "deps": `[":foo_proto"]`,
5042 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5043 }),
5044 }
5045 runCcLibraryTestCase(t, tc)
5046
5047 // foo/foo_subdir
5048 tc.Dir = "foo/foo_subdir"
5049 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005050 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005051 "srcs": `["foo_subdir.proto"]`,
5052 "strip_import_prefix": `""`,
5053 "tags": `["manual"]`,
5054 }),
5055 }
5056 runCcLibraryTestCase(t, tc)
5057}
Spandan Dasab29f572023-09-01 19:43:56 +00005058
5059// `foo_device` and `bar_host` can depend on .proto files of a specific dir,
5060// the dynamically generated proto_library should not have any target_compatible_with
5061func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) {
5062 tc := Bp2buildTestCase{
5063 Description: "proto_library generated for proto.include_dirs is compatible for all axes",
5064 ModuleTypeUnderTest: "cc_library",
5065 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005066 Blueprint: simpleModule("cc_library", "libprotobuf-cpp-lite") + `
Spandan Dasab29f572023-09-01 19:43:56 +00005067cc_library {
5068 name: "foo_device",
5069 device_supported: true, // this is the default behavior, but added explicitly here for illustration
5070 host_supported: false,
5071 proto: {include_dirs: ["dir"]},
5072}
5073cc_library {
5074 name: "bar_host",
5075 device_supported: false,
5076 host_supported: true,
5077 srcs: ["bar.proto"],
5078 proto: {include_dirs: ["dir"]},
5079}
5080`,
5081 Filesystem: map[string]string{
5082 "dir/Android.bp": "",
5083 "dir/dir.proto": "",
5084 },
5085 Dir: "dir", // check for the generated proto_library
5086 ExpectedBazelTargets: []string{
5087 MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{
5088 "srcs": `["dir.proto"]`,
5089 "strip_import_prefix": `""`,
5090 "tags": `["manual"]`,
5091 }),
5092 },
5093 }
5094 runCcLibraryTestCase(t, tc)
5095}
Spandan Dase1cb14b2023-09-05 19:31:12 +00005096
5097func TestCcCompileMultilibConversion(t *testing.T) {
5098 tc := Bp2buildTestCase{
5099 Description: "cc_library with compile_multilib",
5100 ModuleTypeUnderTest: "cc_library",
5101 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5102 Blueprint: `
5103cc_library {
5104 name: "lib32",
5105 compile_multilib: "32",
5106}
5107cc_library {
5108 name: "lib64",
5109 compile_multilib: "64",
5110}
5111`,
5112 ExpectedBazelTargets: []string{
5113 MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{
5114 "local_includes": `["."]`,
5115 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5116 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5117 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5118 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5119 "//conditions:default": [],
5120 })`,
5121 }),
5122 MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{
5123 "local_includes": `["."]`,
5124 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5125 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5126 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5127 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5128 "//conditions:default": [],
5129 })`,
5130 }),
5131 MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{
5132 "local_includes": `["."]`,
5133 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5134 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5135 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5136 "//conditions:default": [],
5137 })`,
5138 }),
5139 MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{
5140 "local_includes": `["."]`,
5141 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5142 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5143 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5144 "//conditions:default": [],
5145 })`,
5146 }),
5147 },
5148 }
5149 runCcLibraryTestCase(t, tc)
5150}
Spandan Das63acae92023-09-14 22:34:34 +00005151
5152func TestNdkLibraryConversion(t *testing.T) {
5153 tc := Bp2buildTestCase{
5154 Description: "ndk_library conversion",
5155 ModuleTypeUnderTest: "cc_library",
5156 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5157 Blueprint: `
5158cc_library {
5159 name: "libfoo",
5160 bazel_module: { bp2build_available: false },
5161}
5162ndk_library {
5163 name: "libfoo",
5164 first_version: "29",
5165 symbol_file: "libfoo.map.txt",
5166}
5167`,
5168 ExpectedBazelTargets: []string{
5169 MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{
5170 "api_surface": `"publicapi"`,
5171 "soname": `"libfoo.so"`,
5172 "source_library_label": `"//:libfoo"`,
5173 "symbol_file": `"libfoo.map.txt"`,
5174 "versions": `[
5175 "29",
5176 "30",
5177 "S",
5178 "Tiramisu",
5179 "current",
5180 ]`,
5181 }),
5182 },
5183 }
5184 runCcLibraryTestCase(t, tc)
5185}
Spandan Das319711b2023-09-19 19:04:41 +00005186
5187func TestNdkHeadersConversion(t *testing.T) {
5188 tc := Bp2buildTestCase{
5189 Description: "ndk_headers conversion",
5190 ModuleTypeUnderTest: "ndk_headers",
5191 ModuleTypeUnderTestFactory: cc.NdkHeadersFactory,
5192 Blueprint: `
5193ndk_headers {
5194 name: "libfoo_headers",
5195 from: "from",
5196 to: "to",
5197 srcs: ["foo.h", "foo_other.h"]
5198}
5199`,
5200 ExpectedBazelTargets: []string{
5201 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5202 "strip_import_prefix": `"from"`,
5203 "import_prefix": `"to"`,
5204 "hdrs": `[
5205 "foo.h",
5206 "foo_other.h",
5207 ]`,
5208 }),
5209 },
5210 }
5211 runCcLibraryTestCase(t, tc)
5212}