blob: 0685de1dbf26d705c84fa1c6bcec9f709e57235d [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"]`,
Spandan Dasaf725832023-09-19 19:51:52 +0000162 "deps": `select({
163 "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"],
164 "//conditions:default": [],
165 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500166 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500167 })
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200168}
169
170func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000171 runCcLibraryTestCase(t, Bp2buildTestCase{
172 Description: "cc_library - trimmed example of //bionic/linker:ld-android",
173 ModuleTypeUnderTest: "cc_library",
174 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +0000175 StubbedBuildDefinitions: []string{"libc_headers"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000176 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200177 "ld-android.cpp": "",
178 "linked_list.h": "",
179 "linker.h": "",
180 "linker_block_allocator.h": "",
181 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400182 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000183 Blueprint: soongCcLibraryPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +0000184 simpleModule("cc_library_headers", "libc_headers") + `
Jingwen Chen63930982021-03-24 10:04:33 -0400185cc_library {
186 name: "fake-ld-android",
187 srcs: ["ld_android.cpp"],
188 cflags: [
189 "-Wall",
190 "-Wextra",
191 "-Wunused",
192 "-Werror",
193 ],
194 header_libs: ["libc_headers"],
195 ldflags: [
196 "-Wl,--exclude-libs=libgcc.a",
197 "-Wl,--exclude-libs=libgcc_stripped.a",
198 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
199 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
200 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
201 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
202 ],
203 arch: {
204 x86: {
205 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
206 },
207 x86_64: {
208 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
209 },
210 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400211 include_build_directory: false,
Jingwen Chen63930982021-03-24 10:04:33 -0400212}
213`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000214 ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500215 "srcs": `["ld_android.cpp"]`,
216 "copts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400217 "-Wall",
218 "-Wextra",
219 "-Wunused",
220 "-Werror",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500221 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500222 "implementation_deps": `[":libc_headers"]`,
223 "linkopts": `[
Jingwen Chen63930982021-03-24 10:04:33 -0400224 "-Wl,--exclude-libs=libgcc.a",
225 "-Wl,--exclude-libs=libgcc_stripped.a",
226 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
227 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
228 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
229 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
230 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000231 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
232 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400233 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500234 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500235 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200236 })
237}
238
239func TestCcLibraryExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000240 runCcLibraryTestCase(t, Bp2buildTestCase{
241 Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
242 ModuleTypeUnderTest: "cc_library",
243 ModuleTypeUnderTestFactory: cc.LibraryFactory,
244 Dir: "external",
245 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200246 "external/math/cosf.c": "",
247 "external/math/erf.c": "",
248 "external/math/erf_data.c": "",
249 "external/math/erff.c": "",
250 "external/math/erff_data.c": "",
251 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000252cc_library {
253 name: "fake-libarm-optimized-routines-math",
254 exclude_srcs: [
255 // Provided by:
256 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
257 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
258 "math/erf.c",
259 "math/erf_data.c",
260 "math/erff.c",
261 "math/erff_data.c",
262 ],
263 srcs: [
264 "math/*.c",
265 ],
266 // arch-specific settings
267 arch: {
268 arm64: {
269 cflags: [
270 "-DHAVE_FAST_FMA=1",
271 ],
272 },
273 },
274 bazel_module: { bp2build_available: true },
275}
276`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200277 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000278 Blueprint: soongCcLibraryPreamble,
279 ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500280 "copts": `select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000281 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
282 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500283 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500284 "local_includes": `["."]`,
285 "srcs_c": `["math/cosf.c"]`,
286 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200287 })
288}
289
290func TestCcLibrarySharedStaticProps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000291 runCcLibraryTestCase(t, Bp2buildTestCase{
292 Description: "cc_library shared/static props",
293 ModuleTypeUnderTest: "cc_library",
294 ModuleTypeUnderTestFactory: cc.LibraryFactory,
295 Filesystem: map[string]string{
Liz Kammer8337ea42021-09-10 10:06:32 -0400296 "both.cpp": "",
297 "sharedonly.cpp": "",
298 "staticonly.cpp": "",
299 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000300 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000301cc_library {
302 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400303 srcs: ["both.cpp"],
304 cflags: ["bothflag"],
305 shared_libs: ["shared_dep_for_both"],
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400306 static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
307 whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400308 static: {
309 srcs: ["staticonly.cpp"],
310 cflags: ["staticflag"],
311 shared_libs: ["shared_dep_for_static"],
312 static_libs: ["static_dep_for_static"],
313 whole_static_libs: ["whole_static_lib_for_static"],
314 },
315 shared: {
316 srcs: ["sharedonly.cpp"],
317 cflags: ["sharedflag"],
318 shared_libs: ["shared_dep_for_shared"],
319 static_libs: ["static_dep_for_shared"],
320 whole_static_libs: ["whole_static_lib_for_shared"],
321 },
Liz Kammer8337ea42021-09-10 10:06:32 -0400322 include_build_directory: false,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000323}
324
Liz Kammer8337ea42021-09-10 10:06:32 -0400325cc_library_static {
326 name: "static_dep_for_shared",
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_static",
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: "static_dep_for_both",
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_shared",
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_static",
Liz Kammer8337ea42021-09-10 10:06:32 -0400343}
Chris Parsons08648312021-05-06 16:23:19 -0400344
Liz Kammer8337ea42021-09-10 10:06:32 -0400345cc_library_static {
346 name: "whole_static_lib_for_both",
Liz Kammer8337ea42021-09-10 10:06:32 -0400347}
Chris Parsons08648312021-05-06 16:23:19 -0400348
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400349cc_library_static {
350 name: "whole_and_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400351}
352
Liz Kammer8337ea42021-09-10 10:06:32 -0400353cc_library {
354 name: "shared_dep_for_shared",
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_static",
Liz Kammer8337ea42021-09-10 10:06:32 -0400359}
Chris Parsons08648312021-05-06 16:23:19 -0400360
Liz Kammer8337ea42021-09-10 10:06:32 -0400361cc_library {
362 name: "shared_dep_for_both",
Liz Kammer8337ea42021-09-10 10:06:32 -0400363}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000364`,
Chris Parsonscd209032023-09-19 01:12:48 +0000365 StubbedBuildDefinitions: []string{"static_dep_for_shared", "static_dep_for_static",
366 "static_dep_for_both", "whole_static_lib_for_shared", "whole_static_lib_for_static",
367 "whole_static_lib_for_both", "whole_and_static_lib_for_both", "shared_dep_for_shared",
368 "shared_dep_for_static", "shared_dep_for_both",
369 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000370 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000371 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500372 "copts": `[
373 "bothflag",
374 "staticflag",
375 ]`,
376 "implementation_deps": `[
377 ":static_dep_for_both",
378 ":static_dep_for_static",
379 ]`,
380 "implementation_dynamic_deps": `[
381 ":shared_dep_for_both",
382 ":shared_dep_for_static",
383 ]`,
384 "srcs": `[
385 "both.cpp",
386 "staticonly.cpp",
387 ]`,
388 "whole_archive_deps": `[
389 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400390 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500391 ":whole_static_lib_for_static",
392 ]`}),
Alixe06d75b2022-08-31 18:28:19 +0000393 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500394 "copts": `[
395 "bothflag",
396 "sharedflag",
397 ]`,
398 "implementation_deps": `[
399 ":static_dep_for_both",
400 ":static_dep_for_shared",
401 ]`,
402 "implementation_dynamic_deps": `[
403 ":shared_dep_for_both",
404 ":shared_dep_for_shared",
405 ]`,
406 "srcs": `[
407 "both.cpp",
408 "sharedonly.cpp",
409 ]`,
410 "whole_archive_deps": `[
411 ":whole_static_lib_for_both",
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400412 ":whole_and_static_lib_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500413 ":whole_static_lib_for_shared",
414 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500415 }),
416 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200417 })
418}
419
Liz Kammer7a210ac2021-09-22 15:52:58 -0400420func TestCcLibraryDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000421 runCcLibraryTestCase(t, Bp2buildTestCase{
422 Description: "cc_library shared/static props",
423 ModuleTypeUnderTest: "cc_library",
424 ModuleTypeUnderTestFactory: cc.LibraryFactory,
425 Filesystem: map[string]string{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400426 "both.cpp": "",
427 "sharedonly.cpp": "",
428 "staticonly.cpp": "",
429 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000430 Blueprint: soongCcLibraryPreamble + `
Liz Kammer7a210ac2021-09-22 15:52:58 -0400431cc_library {
432 name: "a",
433 srcs: ["both.cpp"],
434 cflags: ["bothflag"],
435 shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
436 export_shared_lib_headers: ["shared_dep_for_both"],
437 static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
438 export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
439 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
440 static: {
441 srcs: ["staticonly.cpp"],
442 cflags: ["staticflag"],
443 shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
444 export_shared_lib_headers: ["shared_dep_for_static"],
445 static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
446 export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
447 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
448 },
449 shared: {
450 srcs: ["sharedonly.cpp"],
451 cflags: ["sharedflag"],
452 shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
453 export_shared_lib_headers: ["shared_dep_for_shared"],
454 static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
455 export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
456 whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
457 },
458 include_build_directory: false,
459}
Chris Parsonscd209032023-09-19 01:12:48 +0000460` + simpleModule("cc_library_static", "static_dep_for_shared") +
461 simpleModule("cc_library_static", "implementation_static_dep_for_shared") +
462 simpleModule("cc_library_static", "static_dep_for_static") +
463 simpleModule("cc_library_static", "implementation_static_dep_for_static") +
464 simpleModule("cc_library_static", "static_dep_for_both") +
465 simpleModule("cc_library_static", "implementation_static_dep_for_both") +
466 simpleModule("cc_library_static", "whole_static_dep_for_shared") +
467 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
468 simpleModule("cc_library_static", "whole_static_dep_for_static") +
469 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
470 simpleModule("cc_library_static", "whole_static_dep_for_both") +
471 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
472 simpleModule("cc_library", "shared_dep_for_shared") +
473 simpleModule("cc_library", "implementation_shared_dep_for_shared") +
474 simpleModule("cc_library", "shared_dep_for_static") +
475 simpleModule("cc_library", "implementation_shared_dep_for_static") +
476 simpleModule("cc_library", "shared_dep_for_both") +
477 simpleModule("cc_library", "implementation_shared_dep_for_both"),
478 StubbedBuildDefinitions: []string{"static_dep_for_shared", "implementation_static_dep_for_shared",
479 "static_dep_for_static", "implementation_static_dep_for_static", "static_dep_for_both",
480 "implementation_static_dep_for_both", "whole_static_dep_for_shared",
481 "not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_static",
482 "not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_both",
483 "not_explicitly_exported_whole_static_dep_for_both", "shared_dep_for_shared",
484 "implementation_shared_dep_for_shared", "shared_dep_for_static",
485 "implementation_shared_dep_for_static", "shared_dep_for_both",
486 "implementation_shared_dep_for_both",
487 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000488 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000489 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500490 "copts": `[
491 "bothflag",
492 "staticflag",
493 ]`,
494 "deps": `[
495 ":static_dep_for_both",
496 ":static_dep_for_static",
497 ]`,
498 "dynamic_deps": `[
499 ":shared_dep_for_both",
500 ":shared_dep_for_static",
501 ]`,
502 "implementation_deps": `[
503 ":implementation_static_dep_for_both",
504 ":implementation_static_dep_for_static",
505 ]`,
506 "implementation_dynamic_deps": `[
507 ":implementation_shared_dep_for_both",
508 ":implementation_shared_dep_for_static",
509 ]`,
510 "srcs": `[
511 "both.cpp",
512 "staticonly.cpp",
513 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500514 "whole_archive_deps": `[
Liz Kammer7a210ac2021-09-22 15:52:58 -0400515 ":not_explicitly_exported_whole_static_dep_for_both",
516 ":whole_static_dep_for_both",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500517 ":not_explicitly_exported_whole_static_dep_for_static",
518 ":whole_static_dep_for_static",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500519 ]`,
520 }),
Alixe06d75b2022-08-31 18:28:19 +0000521 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500522 "copts": `[
523 "bothflag",
524 "sharedflag",
525 ]`,
526 "deps": `[
527 ":static_dep_for_both",
528 ":static_dep_for_shared",
529 ]`,
530 "dynamic_deps": `[
531 ":shared_dep_for_both",
532 ":shared_dep_for_shared",
533 ]`,
534 "implementation_deps": `[
535 ":implementation_static_dep_for_both",
536 ":implementation_static_dep_for_shared",
537 ]`,
538 "implementation_dynamic_deps": `[
539 ":implementation_shared_dep_for_both",
540 ":implementation_shared_dep_for_shared",
541 ]`,
542 "srcs": `[
543 "both.cpp",
544 "sharedonly.cpp",
545 ]`,
546 "whole_archive_deps": `[
547 ":not_explicitly_exported_whole_static_dep_for_both",
548 ":whole_static_dep_for_both",
549 ":not_explicitly_exported_whole_static_dep_for_shared",
550 ":whole_static_dep_for_shared",
551 ]`,
552 })},
553 },
554 )
Liz Kammer7a210ac2021-09-22 15:52:58 -0400555}
556
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400557func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000558 runCcLibraryTestCase(t, Bp2buildTestCase{
559 ModuleTypeUnderTest: "cc_library",
560 ModuleTypeUnderTestFactory: cc.LibraryFactory,
561 Dir: "foo/bar",
Chris Parsonscd209032023-09-19 01:12:48 +0000562 StubbedBuildDefinitions: []string{"//foo/bar:prebuilt_whole_static_lib_for_shared", "//foo/bar:prebuilt_whole_static_lib_for_static",
563 "//foo/bar:prebuilt_whole_static_lib_for_both"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000564 Filesystem: map[string]string{
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400565 "foo/bar/Android.bp": `
566cc_library {
567 name: "a",
568 whole_static_libs: ["whole_static_lib_for_both"],
569 static: {
570 whole_static_libs: ["whole_static_lib_for_static"],
571 },
572 shared: {
573 whole_static_libs: ["whole_static_lib_for_shared"],
574 },
575 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400576 include_build_directory: false,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400577}
578
579cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
580
581cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
582
583cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
584`,
585 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000586 Blueprint: soongCcLibraryPreamble,
587 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000588 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500589 "whole_archive_deps": `[
590 ":whole_static_lib_for_both_alwayslink",
591 ":whole_static_lib_for_static_alwayslink",
592 ]`,
593 }),
Alixe06d75b2022-08-31 18:28:19 +0000594 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500595 "whole_archive_deps": `[
596 ":whole_static_lib_for_both_alwayslink",
597 ":whole_static_lib_for_shared_alwayslink",
598 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500599 }),
600 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500601 },
602 )
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400603}
604
Jingwen Chenbcf53042021-05-26 04:42:42 +0000605func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000606 runCcLibraryTestCase(t, Bp2buildTestCase{
607 Description: "cc_library shared/static props in arch",
608 ModuleTypeUnderTest: "cc_library",
609 ModuleTypeUnderTestFactory: cc.LibraryFactory,
610 Dir: "foo/bar",
611 Filesystem: map[string]string{
Jingwen Chenbcf53042021-05-26 04:42:42 +0000612 "foo/bar/arm.cpp": "",
613 "foo/bar/x86.cpp": "",
614 "foo/bar/sharedonly.cpp": "",
615 "foo/bar/staticonly.cpp": "",
616 "foo/bar/Android.bp": `
617cc_library {
618 name: "a",
619 arch: {
620 arm: {
621 shared: {
622 srcs: ["arm_shared.cpp"],
623 cflags: ["-DARM_SHARED"],
624 static_libs: ["arm_static_dep_for_shared"],
625 whole_static_libs: ["arm_whole_static_dep_for_shared"],
626 shared_libs: ["arm_shared_dep_for_shared"],
627 },
628 },
629 x86: {
630 static: {
631 srcs: ["x86_static.cpp"],
632 cflags: ["-DX86_STATIC"],
633 static_libs: ["x86_dep_for_static"],
634 },
635 },
636 },
637 target: {
638 android: {
639 shared: {
640 srcs: ["android_shared.cpp"],
641 cflags: ["-DANDROID_SHARED"],
642 static_libs: ["android_dep_for_shared"],
643 },
644 },
645 android_arm: {
646 shared: {
647 cflags: ["-DANDROID_ARM_SHARED"],
648 },
649 },
650 },
651 srcs: ["both.cpp"],
652 cflags: ["bothflag"],
653 static_libs: ["static_dep_for_both"],
654 static: {
655 srcs: ["staticonly.cpp"],
656 cflags: ["staticflag"],
657 static_libs: ["static_dep_for_static"],
658 },
659 shared: {
660 srcs: ["sharedonly.cpp"],
661 cflags: ["sharedflag"],
662 static_libs: ["static_dep_for_shared"],
663 },
664 bazel_module: { bp2build_available: true },
665}
666
667cc_library_static { name: "static_dep_for_shared" }
668cc_library_static { name: "static_dep_for_static" }
669cc_library_static { name: "static_dep_for_both" }
670
671cc_library_static { name: "arm_static_dep_for_shared" }
672cc_library_static { name: "arm_whole_static_dep_for_shared" }
673cc_library_static { name: "arm_shared_dep_for_shared" }
674
675cc_library_static { name: "x86_dep_for_static" }
676
677cc_library_static { name: "android_dep_for_shared" }
678`,
679 },
Chris Parsonscd209032023-09-19 01:12:48 +0000680 StubbedBuildDefinitions: []string{"//foo/bar:static_dep_for_shared", "//foo/bar:static_dep_for_static",
681 "//foo/bar:static_dep_for_both", "//foo/bar:arm_static_dep_for_shared", "//foo/bar:arm_whole_static_dep_for_shared",
682 "//foo/bar:arm_shared_dep_for_shared", "//foo/bar:x86_dep_for_static", "//foo/bar:android_dep_for_shared",
683 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000684 Blueprint: soongCcLibraryPreamble,
685 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000686 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500687 "copts": `[
688 "bothflag",
689 "staticflag",
690 ] + select({
691 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
692 "//conditions:default": [],
693 })`,
694 "implementation_deps": `[
695 ":static_dep_for_both",
696 ":static_dep_for_static",
697 ] + select({
698 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
699 "//conditions:default": [],
700 })`,
701 "local_includes": `["."]`,
702 "srcs": `[
703 "both.cpp",
704 "staticonly.cpp",
705 ] + select({
706 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
707 "//conditions:default": [],
708 })`,
709 }),
Alixe06d75b2022-08-31 18:28:19 +0000710 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500711 "copts": `[
712 "bothflag",
713 "sharedflag",
714 ] + select({
715 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
716 "//conditions:default": [],
717 }) + select({
718 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
719 "//conditions:default": [],
720 }) + select({
721 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
722 "//conditions:default": [],
723 })`,
724 "implementation_deps": `[
725 ":static_dep_for_both",
726 ":static_dep_for_shared",
727 ] + select({
728 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
729 "//conditions:default": [],
730 }) + select({
731 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
732 "//conditions:default": [],
733 })`,
734 "implementation_dynamic_deps": `select({
735 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
736 "//conditions:default": [],
737 })`,
738 "local_includes": `["."]`,
739 "srcs": `[
740 "both.cpp",
741 "sharedonly.cpp",
742 ] + select({
743 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
744 "//conditions:default": [],
745 }) + select({
746 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
747 "//conditions:default": [],
748 })`,
749 "whole_archive_deps": `select({
750 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
751 "//conditions:default": [],
752 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500753 }),
754 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500755 },
756 )
Jingwen Chenbcf53042021-05-26 04:42:42 +0000757}
758
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000759func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000760 runCcLibraryTestCase(t, Bp2buildTestCase{
761 Description: "cc_library shared/static props with c/cpp/s mixed sources",
762 ModuleTypeUnderTest: "cc_library",
763 ModuleTypeUnderTestFactory: cc.LibraryFactory,
764 Dir: "foo/bar",
Chris Parsonscd209032023-09-19 01:12:48 +0000765 StubbedBuildDefinitions: []string{"//foo/bar:shared_filegroup", "//foo/bar:static_filegroup", "//foo/bar:both_filegroup"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000766 Filesystem: map[string]string{
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000767 "foo/bar/both_source.cpp": "",
768 "foo/bar/both_source.cc": "",
769 "foo/bar/both_source.c": "",
770 "foo/bar/both_source.s": "",
771 "foo/bar/both_source.S": "",
772 "foo/bar/shared_source.cpp": "",
773 "foo/bar/shared_source.cc": "",
774 "foo/bar/shared_source.c": "",
775 "foo/bar/shared_source.s": "",
776 "foo/bar/shared_source.S": "",
777 "foo/bar/static_source.cpp": "",
778 "foo/bar/static_source.cc": "",
779 "foo/bar/static_source.c": "",
780 "foo/bar/static_source.s": "",
781 "foo/bar/static_source.S": "",
782 "foo/bar/Android.bp": `
783cc_library {
784 name: "a",
785 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400786 "both_source.cpp",
787 "both_source.cc",
788 "both_source.c",
789 "both_source.s",
790 "both_source.S",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400791 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400792 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000793 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400794 srcs: [
795 "static_source.cpp",
796 "static_source.cc",
797 "static_source.c",
798 "static_source.s",
799 "static_source.S",
800 ":static_filegroup",
801 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000802 },
803 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -0400804 srcs: [
805 "shared_source.cpp",
806 "shared_source.cc",
807 "shared_source.c",
808 "shared_source.s",
809 "shared_source.S",
810 ":shared_filegroup",
811 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000812 },
813 bazel_module: { bp2build_available: true },
814}
815
816filegroup {
817 name: "both_filegroup",
818 srcs: [
819 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400820 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000821}
822
823filegroup {
824 name: "shared_filegroup",
825 srcs: [
826 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400827 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000828}
829
830filegroup {
831 name: "static_filegroup",
832 srcs: [
833 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400834 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000835}
836`,
837 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000838 Blueprint: soongCcLibraryPreamble,
839 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000840 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500841 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500842 "srcs": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000843 "both_source.cpp",
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400844 "both_source.cc",
845 ":both_filegroup_cpp_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500846 "static_source.cpp",
847 "static_source.cc",
848 ":static_filegroup_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500849 ]`,
850 "srcs_as": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000851 "both_source.s",
852 "both_source.S",
853 ":both_filegroup_as_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500854 "static_source.s",
855 "static_source.S",
856 ":static_filegroup_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500857 ]`,
858 "srcs_c": `[
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000859 "both_source.c",
860 ":both_filegroup_c_srcs",
Chris Parsons77acf2e2021-12-03 17:27:16 -0500861 "static_source.c",
862 ":static_filegroup_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500863 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500864 }),
Alixe06d75b2022-08-31 18:28:19 +0000865 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500866 "local_includes": `["."]`,
867 "srcs": `[
868 "both_source.cpp",
869 "both_source.cc",
870 ":both_filegroup_cpp_srcs",
871 "shared_source.cpp",
872 "shared_source.cc",
873 ":shared_filegroup_cpp_srcs",
874 ]`,
875 "srcs_as": `[
876 "both_source.s",
877 "both_source.S",
878 ":both_filegroup_as_srcs",
879 "shared_source.s",
880 "shared_source.S",
881 ":shared_filegroup_as_srcs",
882 ]`,
883 "srcs_c": `[
884 "both_source.c",
885 ":both_filegroup_c_srcs",
886 "shared_source.c",
887 ":shared_filegroup_c_srcs",
888 ]`,
889 })}})
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000890}
891
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000892func TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000893 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000894 Description: "cc_library non-configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000895 ModuleTypeUnderTest: "cc_library",
896 ModuleTypeUnderTestFactory: cc.LibraryFactory,
897 Dir: "foo/bar",
898 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200899 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200900cc_library {
901 name: "a",
902 srcs: ["a.cpp"],
903 version_script: "v.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000904 dynamic_list: "dynamic.list",
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200905 bazel_module: { bp2build_available: true },
Liz Kammer8337ea42021-09-10 10:06:32 -0400906 include_build_directory: false,
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200907}
908`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200909 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000910 Blueprint: soongCcLibraryPreamble,
911 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000912 "additional_linker_inputs": `[
913 "v.map",
914 "dynamic.list",
915 ]`,
916 "linkopts": `[
917 "-Wl,--version-script,$(location v.map)",
918 "-Wl,--dynamic-list,$(location dynamic.list)",
919 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000920 "srcs": `["a.cpp"]`,
921 "features": `["android_cfi_exports_map"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500922 }),
923 },
924 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200925}
926
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000927func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000928 runCcLibraryTestCase(t, Bp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000929 Description: "cc_library configured version script and dynamic list",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000930 ModuleTypeUnderTest: "cc_library",
931 ModuleTypeUnderTestFactory: cc.LibraryFactory,
932 Dir: "foo/bar",
933 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200934 "foo/bar/Android.bp": `
Liz Kammer8337ea42021-09-10 10:06:32 -0400935cc_library {
936 name: "a",
937 srcs: ["a.cpp"],
938 arch: {
939 arm: {
940 version_script: "arm.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000941 dynamic_list: "dynamic_arm.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400942 },
943 arm64: {
944 version_script: "arm64.map",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000945 dynamic_list: "dynamic_arm64.list",
Liz Kammer8337ea42021-09-10 10:06:32 -0400946 },
947 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200948
Liz Kammer8337ea42021-09-10 10:06:32 -0400949 bazel_module: { bp2build_available: true },
950 include_build_directory: false,
951}
Liz Kammerd366c902021-06-03 13:43:01 -0400952 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200953 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000954 Blueprint: soongCcLibraryPreamble,
955 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -0500956 "additional_linker_inputs": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000957 "//build/bazel/platforms/arch:arm": [
958 "arm.map",
959 "dynamic_arm.list",
960 ],
961 "//build/bazel/platforms/arch:arm64": [
962 "arm64.map",
963 "dynamic_arm64.list",
964 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400965 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500966 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500967 "linkopts": `select({
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000968 "//build/bazel/platforms/arch:arm": [
969 "-Wl,--version-script,$(location arm.map)",
970 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
971 ],
972 "//build/bazel/platforms/arch:arm64": [
973 "-Wl,--version-script,$(location arm64.map)",
974 "-Wl,--dynamic-list,$(location dynamic_arm64.list)",
975 ],
Liz Kammerd2871182021-10-04 13:54:37 -0400976 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500977 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500978 "srcs": `["a.cpp"]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000979 "features": `select({
980 "//build/bazel/platforms/arch:arm": ["android_cfi_exports_map"],
981 "//build/bazel/platforms/arch:arm64": ["android_cfi_exports_map"],
982 "//conditions:default": [],
983 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500984 }),
985 },
986 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200987}
988
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000989func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
990 runCcLibraryTestCase(t, Bp2buildTestCase{
991 Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
992 ModuleTypeUnderTest: "cc_library",
993 ModuleTypeUnderTestFactory: cc.LibraryFactory,
994 Filesystem: map[string]string{
995 "version_script": "",
996 "dynamic.list": "",
997 },
998 Blueprint: `
999cc_library {
1000 name: "foo",
1001 ldflags: [
1002 "--nospace_flag",
1003 "-z spaceflag",
1004 ],
1005 version_script: "version_script",
1006 dynamic_list: "dynamic.list",
1007 include_build_directory: false,
1008}
1009`,
1010 ExpectedBazelTargets: []string{
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001011 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
1012 "features": `["android_cfi_exports_map"]`,
1013 }),
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001014 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
1015 "additional_linker_inputs": `[
1016 "version_script",
1017 "dynamic.list",
1018 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +00001019 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001020 "linkopts": `[
1021 "--nospace_flag",
1022 "-z",
1023 "spaceflag",
1024 "-Wl,--version-script,$(location version_script)",
1025 "-Wl,--dynamic-list,$(location dynamic.list)",
1026 ]`,
1027 }),
1028 },
1029 })
1030}
1031
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001032func TestCcLibrarySharedLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001033 runCcLibraryTestCase(t, Bp2buildTestCase{
1034 Description: "cc_library shared_libs",
1035 ModuleTypeUnderTest: "cc_library",
1036 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001037 StubbedBuildDefinitions: []string{"mylib"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001038 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001039cc_library {
1040 name: "mylib",
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001041}
1042
1043cc_library {
1044 name: "a",
1045 shared_libs: ["mylib",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001046 include_build_directory: false,
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -04001047}
1048`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001049 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001050 "implementation_dynamic_deps": `[":mylib"]`,
1051 }),
1052 },
1053 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001054}
1055
Liz Kammer0eae52e2021-10-06 10:32:26 -04001056func TestCcLibraryFeatures(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001057 expected_targets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001058 expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001059 "features": `[
1060 "disable_pack_relocations",
1061 "-no_undefined_symbols",
1062 ]`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001063 "native_coverage": `False`,
1064 "srcs": `["a.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001065 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001066 expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
Yu Liuf01a0f02022-12-07 15:45:30 -08001067 "features": `select({
Chris Parsons77acf2e2021-12-03 17:27:16 -05001068 "//build/bazel/platforms/arch:x86_64": [
1069 "disable_pack_relocations",
1070 "-no_undefined_symbols",
1071 ],
1072 "//conditions:default": [],
1073 })`,
Yu Liuf01a0f02022-12-07 15:45:30 -08001074 "native_coverage": `False`,
1075 "srcs": `["b.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001076 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001077 expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001078 "features": `select({
1079 "//build/bazel/platforms/os:darwin": [
1080 "disable_pack_relocations",
1081 "-no_undefined_symbols",
1082 ],
1083 "//conditions:default": [],
1084 })`,
1085 "srcs": `["c.cpp"]`,
1086 })...)
1087
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001088 runCcLibraryTestCase(t, Bp2buildTestCase{
1089 Description: "cc_library pack_relocations test",
1090 ModuleTypeUnderTest: "cc_library",
1091 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1092 Blueprint: soongCcLibraryPreamble + `
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001093cc_library {
1094 name: "a",
1095 srcs: ["a.cpp"],
1096 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001097 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001098 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001099 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001100}
1101
1102cc_library {
1103 name: "b",
1104 srcs: ["b.cpp"],
1105 arch: {
1106 x86_64: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001107 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001108 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001109 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001110 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001111 include_build_directory: false,
Yu Liu8d82ac52022-05-17 15:13:28 -07001112 native_coverage: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001113}
1114
1115cc_library {
1116 name: "c",
1117 srcs: ["c.cpp"],
1118 target: {
1119 darwin: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001120 pack_relocations: false,
Liz Kammer0eae52e2021-10-06 10:32:26 -04001121 allow_undefined_symbols: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001122 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001123 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001124 include_build_directory: false,
Rupert Shuttleworth143be942021-05-09 23:55:51 -04001125}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001126 ExpectedBazelTargets: expected_targets,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001127 })
1128}
1129
1130func TestCcLibrarySpacesInCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001131 runCcLibraryTestCase(t, Bp2buildTestCase{
1132 Description: "cc_library spaces in copts",
1133 ModuleTypeUnderTest: "cc_library",
1134 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1135 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3950cd62021-05-12 04:33:00 +00001136cc_library {
1137 name: "a",
1138 cflags: ["-include header.h",],
Liz Kammer8337ea42021-09-10 10:06:32 -04001139 include_build_directory: false,
Jingwen Chen3950cd62021-05-12 04:33:00 +00001140}
1141`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001142 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001143 "copts": `[
Jingwen Chen3950cd62021-05-12 04:33:00 +00001144 "-include",
1145 "header.h",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001146 ]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001147 }),
1148 },
1149 )
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001150}
1151
1152func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001153 runCcLibraryTestCase(t, Bp2buildTestCase{
1154 Description: "cc_library cppflags usage",
1155 ModuleTypeUnderTest: "cc_library",
1156 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1157 Blueprint: soongCcLibraryPreamble + `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001158 name: "a",
1159 srcs: ["a.cpp"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001160 cflags: ["-Wall"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001161 cppflags: [
1162 "-fsigned-char",
1163 "-pedantic",
Liz Kammer8337ea42021-09-10 10:06:32 -04001164 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001165 arch: {
1166 arm64: {
1167 cppflags: ["-DARM64=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001168 },
Liz Kammerd366c902021-06-03 13:43:01 -04001169 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001170 target: {
1171 android: {
1172 cppflags: ["-DANDROID=1"],
Liz Kammer8337ea42021-09-10 10:06:32 -04001173 },
Liz Kammerd366c902021-06-03 13:43:01 -04001174 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001175 include_build_directory: false,
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001176}
1177`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001178 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001179 "copts": `["-Wall"]`,
1180 "cppflags": `[
Chris Parsons990c4f42021-05-25 12:10:58 -04001181 "-fsigned-char",
1182 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001183 ] + select({
1184 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1185 "//conditions:default": [],
1186 }) + select({
1187 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1188 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001189 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001190 "srcs": `["a.cpp"]`,
1191 }),
1192 },
1193 )
Jingwen Chen63930982021-03-24 10:04:33 -04001194}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001195
Liz Kammer47535c52021-06-02 16:02:22 -04001196func TestCcLibraryExcludeLibs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001197 runCcLibraryTestCase(t, Bp2buildTestCase{
1198 ModuleTypeUnderTest: "cc_library",
1199 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1200 Filesystem: map[string]string{},
Chris Parsonscd209032023-09-19 01:12:48 +00001201 StubbedBuildDefinitions: []string{"arm_whole_static_lib_excludes", "malloc_not_svelte_whole_static_lib",
1202 "arm_static_lib_excludes", "malloc_not_svelte_whole_static_lib_excludes", "arm_shared_lib_excludes",
1203 "malloc_not_svelte_static_lib_excludes", "arm_shared_lib_excludes", "malloc_not_svelte_shared_lib",
1204 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001205 Blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer47535c52021-06-02 16:02:22 -04001206cc_library {
1207 name: "foo_static",
1208 srcs: ["common.c"],
1209 whole_static_libs: [
1210 "arm_whole_static_lib_excludes",
1211 "malloc_not_svelte_whole_static_lib_excludes"
1212 ],
1213 static_libs: [
1214 "arm_static_lib_excludes",
1215 "malloc_not_svelte_static_lib_excludes"
1216 ],
1217 shared_libs: [
1218 "arm_shared_lib_excludes",
1219 ],
1220 arch: {
1221 arm: {
1222 exclude_shared_libs: [
1223 "arm_shared_lib_excludes",
1224 ],
1225 exclude_static_libs: [
1226 "arm_static_lib_excludes",
1227 "arm_whole_static_lib_excludes",
1228 ],
1229 },
1230 },
1231 product_variables: {
1232 malloc_not_svelte: {
1233 shared_libs: ["malloc_not_svelte_shared_lib"],
1234 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1235 exclude_static_libs: [
1236 "malloc_not_svelte_static_lib_excludes",
1237 "malloc_not_svelte_whole_static_lib_excludes",
1238 ],
1239 },
1240 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001241 include_build_directory: false,
Liz Kammer47535c52021-06-02 16:02:22 -04001242}
1243
1244cc_library {
1245 name: "arm_whole_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001246}
1247
1248cc_library {
1249 name: "malloc_not_svelte_whole_static_lib",
Liz Kammer47535c52021-06-02 16:02:22 -04001250}
1251
1252cc_library {
1253 name: "malloc_not_svelte_whole_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001254}
1255
1256cc_library {
1257 name: "arm_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001258}
1259
1260cc_library {
1261 name: "malloc_not_svelte_static_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001262}
1263
1264cc_library {
1265 name: "arm_shared_lib_excludes",
Liz Kammer47535c52021-06-02 16:02:22 -04001266}
1267
1268cc_library {
1269 name: "malloc_not_svelte_shared_lib",
Liz Kammer47535c52021-06-02 16:02:22 -04001270}
1271`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001272 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001273 "implementation_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001274 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001275 "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001276 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001277 "//build/bazel/product_config/config_settings:malloc_not_svelte": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001278 "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001279 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001280 "implementation_dynamic_deps": `select({
Liz Kammer7a210ac2021-09-22 15:52:58 -04001281 "//build/bazel/platforms/arch:arm": [],
1282 "//conditions:default": [":arm_shared_lib_excludes"],
1283 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001284 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
Liz Kammer7a210ac2021-09-22 15:52:58 -04001285 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001286 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001287 "srcs_c": `["common.c"]`,
1288 "whole_archive_deps": `select({
Liz Kammer47535c52021-06-02 16:02:22 -04001289 "//build/bazel/platforms/arch:arm": [],
Chris Parsons953b3562021-09-20 15:14:39 -04001290 "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer47535c52021-06-02 16:02:22 -04001291 }) + select({
Cole Faust87c0c332023-07-31 12:10:12 -07001292 "//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 -04001293 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001294 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001295 }),
1296 },
1297 )
Liz Kammer47535c52021-06-02 16:02:22 -04001298}
Liz Kammerd366c902021-06-03 13:43:01 -04001299
Zi Wang0a8a1292022-08-30 06:27:01 +00001300func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
1301 runCcLibraryTestCase(t, Bp2buildTestCase{
1302 ModuleTypeUnderTest: "cc_library",
1303 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1304 Filesystem: map[string]string{},
Chris Parsonscd209032023-09-19 01:12:48 +00001305 StubbedBuildDefinitions: []string{"malloc_not_svelte_header_lib"},
Zi Wang0a8a1292022-08-30 06:27:01 +00001306 Blueprint: soongCcLibraryStaticPreamble + `
1307cc_library {
1308 name: "foo_static",
1309 srcs: ["common.c"],
1310 product_variables: {
1311 malloc_not_svelte: {
1312 header_libs: ["malloc_not_svelte_header_lib"],
1313 },
1314 },
1315 include_build_directory: false,
1316}
1317
1318cc_library {
1319 name: "malloc_not_svelte_header_lib",
Zi Wang0a8a1292022-08-30 06:27:01 +00001320}
1321`,
1322 ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
1323 "implementation_deps": `select({
Cole Faust87c0c332023-07-31 12:10:12 -07001324 "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
Zi Wang0a8a1292022-08-30 06:27:01 +00001325 "//conditions:default": [],
1326 })`,
1327 "srcs_c": `["common.c"]`,
1328 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
1329 }),
1330 },
1331 )
1332}
1333
Liz Kammerd366c902021-06-03 13:43:01 -04001334func TestCCLibraryNoCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001335 runCcLibraryTestCase(t, Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001336 Description: "cc_library - nocrt: true disables feature",
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001337 ModuleTypeUnderTest: "cc_library",
1338 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1339 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001340 "impl.cpp": "",
1341 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001342 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001343cc_library {
1344 name: "foo-lib",
1345 srcs: ["impl.cpp"],
1346 nocrt: true,
1347 include_build_directory: false,
1348}
1349`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001350 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001351 "features": `["-link_crt"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001352 "srcs": `["impl.cpp"]`,
1353 }),
1354 },
1355 )
Jingwen Chen6ada5892021-09-17 11:38:09 +00001356}
1357
1358func TestCCLibraryNoCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001359 runCcLibraryTestCase(t, Bp2buildTestCase{
1360 Description: "cc_library - nocrt: false - does not emit attribute",
1361 ModuleTypeUnderTest: "cc_library",
1362 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1363 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001364 "impl.cpp": "",
1365 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001366 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001367cc_library {
1368 name: "foo-lib",
1369 srcs: ["impl.cpp"],
1370 nocrt: false,
1371 include_build_directory: false,
1372}
1373`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001374 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001375 "srcs": `["impl.cpp"]`,
1376 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001377 })
Jingwen Chen6ada5892021-09-17 11:38:09 +00001378}
1379
1380func TestCCLibraryNoCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001381 runCcLibraryTestCase(t, Bp2buildTestCase{
1382 Description: "cc_library - nocrt in select",
1383 ModuleTypeUnderTest: "cc_library",
1384 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1385 Filesystem: map[string]string{
Jingwen Chen6ada5892021-09-17 11:38:09 +00001386 "impl.cpp": "",
1387 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001388 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen6ada5892021-09-17 11:38:09 +00001389cc_library {
1390 name: "foo-lib",
1391 srcs: ["impl.cpp"],
1392 arch: {
1393 arm: {
1394 nocrt: true,
1395 },
1396 x86: {
1397 nocrt: false,
1398 },
1399 },
1400 include_build_directory: false,
1401}
1402`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001403 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
1404 "features": `select({
1405 "//build/bazel/platforms/arch:arm": ["-link_crt"],
1406 "//conditions:default": [],
1407 })`,
1408 "srcs": `["impl.cpp"]`,
1409 }),
Jingwen Chen6ada5892021-09-17 11:38:09 +00001410 })
1411}
1412
1413func TestCCLibraryNoLibCrtTrue(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001414 runCcLibraryTestCase(t, Bp2buildTestCase{
1415 ModuleTypeUnderTest: "cc_library",
1416 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1417 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001418 "impl.cpp": "",
1419 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001420 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001421cc_library {
1422 name: "foo-lib",
1423 srcs: ["impl.cpp"],
1424 no_libcrt: true,
Liz Kammer8337ea42021-09-10 10:06:32 -04001425 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001426}
1427`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001428 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001429 "features": `["-use_libcrt"]`,
1430 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001431 }),
1432 })
1433}
1434
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001435func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001436 STATIC_ONLY_ATTRS := map[string]bool{}
1437 SHARED_ONLY_ATTRS := map[string]bool{
1438 "link_crt": true,
1439 "additional_linker_inputs": true,
1440 "linkopts": true,
1441 "strip": true,
Yu Liu75be7b92022-02-01 09:54:09 -08001442 "inject_bssl_hash": true,
Yu Liu56ccb1a2022-11-12 10:47:07 -08001443 "stubs_symbol_file": true,
Liz Kammerbaced712022-09-16 09:01:29 -04001444 "use_version_lib": true,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001445 }
Wei Li81852ca2022-07-27 00:22:06 -07001446
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001447 sharedAttrs := AttrNameToString{}
1448 staticAttrs := AttrNameToString{}
Chris Parsons77acf2e2021-12-03 17:27:16 -05001449 for key, val := range attrs {
1450 if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
1451 sharedAttrs[key] = val
1452 }
1453 if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
1454 staticAttrs[key] = val
1455 }
1456 }
Alixe06d75b2022-08-31 18:28:19 +00001457 sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
1458 staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001459
1460 return []string{staticTarget, sharedTarget}
Liz Kammerd366c902021-06-03 13:43:01 -04001461}
1462
Jingwen Chen6ada5892021-09-17 11:38:09 +00001463func TestCCLibraryNoLibCrtFalse(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001464 runCcLibraryTestCase(t, Bp2buildTestCase{
1465 ModuleTypeUnderTest: "cc_library",
1466 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1467 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001468 "impl.cpp": "",
1469 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001470 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001471cc_library {
1472 name: "foo-lib",
1473 srcs: ["impl.cpp"],
1474 no_libcrt: false,
Liz Kammer8337ea42021-09-10 10:06:32 -04001475 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001476}
1477`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001478 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001479 "srcs": `["impl.cpp"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001480 }),
1481 })
Liz Kammerd366c902021-06-03 13:43:01 -04001482}
1483
Jingwen Chen6ada5892021-09-17 11:38:09 +00001484func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001485 runCcLibraryTestCase(t, Bp2buildTestCase{
1486 ModuleTypeUnderTest: "cc_library",
1487 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1488 Filesystem: map[string]string{
Liz Kammerd366c902021-06-03 13:43:01 -04001489 "impl.cpp": "",
1490 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001491 Blueprint: soongCcLibraryPreamble + `
Liz Kammerd366c902021-06-03 13:43:01 -04001492cc_library {
1493 name: "foo-lib",
1494 srcs: ["impl.cpp"],
1495 arch: {
1496 arm: {
1497 no_libcrt: true,
1498 },
1499 x86: {
1500 no_libcrt: true,
1501 },
1502 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001503 include_build_directory: false,
Liz Kammerd366c902021-06-03 13:43:01 -04001504}
1505`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001506 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001507 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001508 "features": `select({
1509 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1510 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1511 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001512 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001513 }),
1514 })
Liz Kammerd366c902021-06-03 13:43:01 -04001515}
1516
Chris Parsons58852a02021-12-09 18:10:18 -05001517func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001518 runCcLibraryTestCase(t, Bp2buildTestCase{
1519 ModuleTypeUnderTest: "cc_library",
1520 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1521 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001522 "impl.cpp": "",
1523 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001524 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001525cc_library {
1526 name: "foo-lib",
1527 srcs: ["impl.cpp"],
1528 arch: {
1529 arm: {
1530 no_libcrt: true,
1531 },
1532 x86: {
1533 no_libcrt: true,
1534 },
1535 },
1536 target: {
1537 darwin: {
1538 no_libcrt: true,
1539 }
1540 },
1541 include_build_directory: false,
1542}
1543`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001544 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001545 "features": `select({
1546 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1547 "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
1548 "//conditions:default": [],
1549 }) + select({
1550 "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
1551 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001552 })`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001553 "srcs": `["impl.cpp"]`,
Chris Parsons58852a02021-12-09 18:10:18 -05001554 }),
1555 })
1556}
1557
1558func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001559 runCcLibraryTestCase(t, Bp2buildTestCase{
1560 ModuleTypeUnderTest: "cc_library",
1561 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1562 Filesystem: map[string]string{
Chris Parsons58852a02021-12-09 18:10:18 -05001563 "impl.cpp": "",
1564 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001565 Blueprint: soongCcLibraryPreamble + `
Chris Parsons58852a02021-12-09 18:10:18 -05001566cc_library {
1567 name: "foo-lib",
1568 srcs: ["impl.cpp"],
1569 arch: {
1570 arm: {
1571 no_libcrt: true,
1572 },
1573 // This is expected to override the value for darwin_x86_64.
1574 x86_64: {
1575 no_libcrt: true,
1576 },
1577 },
1578 target: {
1579 darwin: {
1580 no_libcrt: false,
1581 }
1582 },
1583 include_build_directory: false,
1584}
1585`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001586 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05001587 "srcs": `["impl.cpp"]`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001588 "features": `select({
1589 "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
1590 "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
1591 "//conditions:default": [],
Chris Parsons58852a02021-12-09 18:10:18 -05001592 })`,
1593 }),
1594 })
1595}
1596
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001597func TestCcLibraryStrip(t *testing.T) {
Chris Parsons77acf2e2021-12-03 17:27:16 -05001598 expectedTargets := []string{}
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001599 expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001600 "strip": `{
1601 "all": True,
1602 }`,
1603 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001604 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001605 "strip": `{
1606 "keep_symbols": True,
1607 }`,
1608 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001609 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001610 "strip": `{
1611 "keep_symbols_and_debug_frame": True,
1612 }`,
1613 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001614 expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001615 "strip": `{
1616 "keep_symbols_list": ["symbol"],
1617 }`,
1618 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001619 expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001620 "strip": `{
1621 "none": True,
1622 }`,
1623 })...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001624 expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
Chris Parsons77acf2e2021-12-03 17:27:16 -05001625
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001626 runCcLibraryTestCase(t, Bp2buildTestCase{
1627 Description: "cc_library strip args",
1628 ModuleTypeUnderTest: "cc_library",
1629 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1630 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001631cc_library {
1632 name: "nothing",
Liz Kammer8337ea42021-09-10 10:06:32 -04001633 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001634}
1635cc_library {
1636 name: "keep_symbols",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001637 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001638 keep_symbols: true,
1639 },
1640 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001641}
1642cc_library {
1643 name: "keep_symbols_and_debug_frame",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001644 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001645 keep_symbols_and_debug_frame: true,
1646 },
1647 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001648}
1649cc_library {
1650 name: "none",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001651 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001652 none: true,
1653 },
1654 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001655}
1656cc_library {
1657 name: "keep_symbols_list",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001658 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001659 keep_symbols_list: ["symbol"],
1660 },
1661 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001662}
1663cc_library {
1664 name: "all",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001665 strip: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001666 all: true,
1667 },
1668 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001669}
1670`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001671 ExpectedBazelTargets: expectedTargets,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001672 })
1673}
1674
1675func TestCcLibraryStripWithArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001676 runCcLibraryTestCase(t, Bp2buildTestCase{
1677 Description: "cc_library strip args",
1678 ModuleTypeUnderTest: "cc_library",
1679 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1680 Blueprint: soongCcLibraryPreamble + `
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001681cc_library {
1682 name: "multi-arch",
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001683 target: {
1684 darwin: {
1685 strip: {
1686 keep_symbols_list: ["foo", "bar"]
1687 }
1688 },
1689 },
1690 arch: {
1691 arm: {
1692 strip: {
1693 keep_symbols_and_debug_frame: true,
1694 },
1695 },
1696 arm64: {
1697 strip: {
1698 keep_symbols: true,
1699 },
1700 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001701 },
1702 include_build_directory: false,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001703}
1704`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001705 ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001706 "strip": `{
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001707 "keep_symbols": select({
1708 "//build/bazel/platforms/arch:arm64": True,
1709 "//conditions:default": None,
1710 }),
1711 "keep_symbols_and_debug_frame": select({
1712 "//build/bazel/platforms/arch:arm": True,
1713 "//conditions:default": None,
1714 }),
1715 "keep_symbols_list": select({
1716 "//build/bazel/platforms/os:darwin": [
1717 "foo",
1718 "bar",
1719 ],
1720 "//conditions:default": [],
1721 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001722 }`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001723 }),
1724 },
1725 )
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001726}
Chris Parsons51f8c392021-08-03 21:01:05 -04001727
1728func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001729 runCcLibraryTestCase(t, Bp2buildTestCase{
1730 Description: "cc_library system_shared_libs empty at root",
1731 ModuleTypeUnderTest: "cc_library",
1732 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1733 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001734cc_library {
1735 name: "root_empty",
Liz Kammer8337ea42021-09-10 10:06:32 -04001736 system_shared_libs: [],
1737 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001738}
1739`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001740 ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001741 "system_dynamic_deps": `[]`,
1742 }),
1743 },
1744 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001745}
1746
1747func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001748 runCcLibraryTestCase(t, Bp2buildTestCase{
1749 Description: "cc_library system_shared_libs empty for static variant",
1750 ModuleTypeUnderTest: "cc_library",
1751 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1752 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001753cc_library {
1754 name: "static_empty",
1755 static: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001756 system_shared_libs: [],
1757 },
1758 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001759}
1760`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001761 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001762 MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001763 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001764 }),
Alixe06d75b2022-08-31 18:28:19 +00001765 MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001766 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001767 })
1768}
1769
1770func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 runCcLibraryTestCase(t, Bp2buildTestCase{
1772 Description: "cc_library system_shared_libs empty for shared variant",
1773 ModuleTypeUnderTest: "cc_library",
1774 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1775 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001776cc_library {
1777 name: "shared_empty",
1778 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001779 system_shared_libs: [],
1780 },
1781 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001782}
1783`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001784 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001785 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1786 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001787 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001788 }),
1789 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001790 })
1791}
1792
1793func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001794 runCcLibraryTestCase(t, Bp2buildTestCase{
1795 Description: "cc_library system_shared_libs empty for shared, bionic variant",
1796 ModuleTypeUnderTest: "cc_library",
1797 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1798 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001799cc_library {
1800 name: "shared_empty",
1801 target: {
1802 bionic: {
1803 shared: {
1804 system_shared_libs: [],
1805 }
1806 }
Liz Kammer8337ea42021-09-10 10:06:32 -04001807 },
1808 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001809}
1810`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001811 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001812 MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
1813 MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001814 "system_dynamic_deps": "[]",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001815 }),
1816 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001817 })
1818}
1819
1820func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1821 // Note that this behavior is technically incorrect (it's a simplification).
1822 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1823 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1824 // b/195791252 tracks the fix.
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001825 runCcLibraryTestCase(t, Bp2buildTestCase{
1826 Description: "cc_library system_shared_libs empty for linux_bionic variant",
1827 ModuleTypeUnderTest: "cc_library",
1828 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001829 StubbedBuildDefinitions: []string{"libc_musl"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001830 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001831cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001832 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001833}
1834
1835cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001836 name: "target_linux_bionic_empty",
1837 target: {
1838 linux_bionic: {
1839 system_shared_libs: [],
1840 },
1841 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001842 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001843}
1844`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001845 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001846 "system_dynamic_deps": `select({
1847 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1848 "//conditions:default": [],
1849 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001850 }),
1851 },
1852 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001853}
1854
1855func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001856 runCcLibraryTestCase(t, Bp2buildTestCase{
1857 Description: "cc_library system_shared_libs empty for bionic variant",
1858 ModuleTypeUnderTest: "cc_library",
1859 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001860 StubbedBuildDefinitions: []string{"libc_musl"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001861 Blueprint: soongCcLibraryPreamble + `
Chris Parsons51f8c392021-08-03 21:01:05 -04001862cc_library {
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001863 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001864}
1865
1866cc_library {
Chris Parsons51f8c392021-08-03 21:01:05 -04001867 name: "target_bionic_empty",
1868 target: {
1869 bionic: {
1870 system_shared_libs: [],
1871 },
1872 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001873 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001874}
1875`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001876 ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001877 "system_dynamic_deps": `select({
1878 "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
1879 "//conditions:default": [],
1880 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05001881 }),
1882 },
1883 )
Chris Parsons51f8c392021-08-03 21:01:05 -04001884}
1885
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001886func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
1887 runCcLibraryTestCase(t, Bp2buildTestCase{
1888 Description: "cc_library system_shared_lib empty for musl variant",
1889 ModuleTypeUnderTest: "cc_library",
1890 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001891 StubbedBuildDefinitions: []string{"libc_musl"},
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001892 Blueprint: soongCcLibraryPreamble + `
1893cc_library {
1894 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001895}
1896
1897cc_library {
1898 name: "target_musl_empty",
1899 target: {
1900 musl: {
1901 system_shared_libs: [],
1902 },
1903 },
1904 include_build_directory: false,
1905}
1906`,
1907 ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{
1908 "system_dynamic_deps": `[]`,
1909 }),
1910 })
1911}
1912
1913func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
1914 runCcLibraryTestCase(t, Bp2buildTestCase{
1915 Description: "cc_library system_shared_lib empty for linux_musl variant",
1916 ModuleTypeUnderTest: "cc_library",
1917 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsons4c81ce02023-09-21 15:30:27 +00001918 StubbedBuildDefinitions: []string{"libc_musl"},
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001919 Blueprint: soongCcLibraryPreamble + `
1920cc_library {
1921 name: "libc_musl",
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001922}
1923
1924cc_library {
1925 name: "target_linux_musl_empty",
1926 target: {
1927 linux_musl: {
1928 system_shared_libs: [],
1929 },
1930 },
1931 include_build_directory: false,
1932}
1933`,
1934 ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{
1935 "system_dynamic_deps": `[]`,
1936 }),
1937 })
1938}
Chris Parsons51f8c392021-08-03 21:01:05 -04001939func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001940 runCcLibraryTestCase(t, Bp2buildTestCase{
1941 Description: "cc_library system_shared_libs set for shared and root",
1942 ModuleTypeUnderTest: "cc_library",
1943 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001944 StubbedBuildDefinitions: []string{"libc", "libm"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001945 Blueprint: soongCcLibraryPreamble + `
Liz Kammer8337ea42021-09-10 10:06:32 -04001946cc_library {
1947 name: "libc",
Liz Kammer8337ea42021-09-10 10:06:32 -04001948}
1949cc_library {
1950 name: "libm",
Liz Kammer8337ea42021-09-10 10:06:32 -04001951}
Chris Parsons51f8c392021-08-03 21:01:05 -04001952
1953cc_library {
1954 name: "foo",
1955 system_shared_libs: ["libc"],
1956 shared: {
Liz Kammer8337ea42021-09-10 10:06:32 -04001957 system_shared_libs: ["libm"],
Chris Parsons51f8c392021-08-03 21:01:05 -04001958 },
Liz Kammer8337ea42021-09-10 10:06:32 -04001959 include_build_directory: false,
Chris Parsons51f8c392021-08-03 21:01:05 -04001960}
1961`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001962 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001963 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001964 "system_dynamic_deps": `[":libc"]`,
1965 }),
Alixe06d75b2022-08-31 18:28:19 +00001966 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05001967 "system_dynamic_deps": `[
1968 ":libc",
1969 ":libm",
1970 ]`,
1971 }),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001972 },
Chris Parsons51f8c392021-08-03 21:01:05 -04001973 })
1974}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001975
1976func TestCcLibraryOsSelects(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001977 runCcLibraryTestCase(t, Bp2buildTestCase{
1978 Description: "cc_library - selects for all os targets",
1979 ModuleTypeUnderTest: "cc_library",
1980 ModuleTypeUnderTestFactory: cc.LibraryFactory,
1981 Filesystem: map[string]string{},
1982 Blueprint: soongCcLibraryPreamble + `
Chris Parsons2dde0cb2021-10-01 14:45:30 -04001983cc_library {
1984 name: "foo-lib",
1985 srcs: ["base.cpp"],
1986 target: {
1987 android: {
1988 srcs: ["android.cpp"],
1989 },
1990 linux: {
1991 srcs: ["linux.cpp"],
1992 },
1993 linux_glibc: {
1994 srcs: ["linux_glibc.cpp"],
1995 },
1996 darwin: {
1997 srcs: ["darwin.cpp"],
1998 },
1999 bionic: {
2000 srcs: ["bionic.cpp"],
2001 },
2002 linux_musl: {
2003 srcs: ["linux_musl.cpp"],
2004 },
2005 windows: {
2006 srcs: ["windows.cpp"],
2007 },
2008 },
2009 include_build_directory: false,
2010}
2011`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002012 ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002013 "srcs": `["base.cpp"] + select({
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002014 "//build/bazel/platforms/os:android": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002015 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002016 "bionic.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002017 "android.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002018 ],
2019 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002020 "//build/bazel/platforms/os:linux_bionic": [
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002021 "linux.cpp",
Liz Kammer9bad9d62021-10-11 15:40:35 -04002022 "bionic.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002023 ],
Colin Cross133782e2022-12-20 15:29:31 -08002024 "//build/bazel/platforms/os:linux_glibc": [
2025 "linux.cpp",
2026 "linux_glibc.cpp",
2027 ],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002028 "//build/bazel/platforms/os:linux_musl": [
Liz Kammer9bad9d62021-10-11 15:40:35 -04002029 "linux.cpp",
Liz Kammerfdd72e62021-10-11 15:41:03 -04002030 "linux_musl.cpp",
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002031 ],
2032 "//build/bazel/platforms/os:windows": ["windows.cpp"],
2033 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05002034 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002035 }),
2036 },
2037 )
Jingwen Chen97b85312021-10-08 10:41:31 +00002038}
2039
Yu Liu75be7b92022-02-01 09:54:09 -08002040func TestLibcryptoHashInjection(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002041 runCcLibraryTestCase(t, Bp2buildTestCase{
2042 Description: "cc_library - libcrypto hash injection",
2043 ModuleTypeUnderTest: "cc_library",
2044 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2045 Filesystem: map[string]string{},
2046 Blueprint: soongCcLibraryPreamble + `
Yu Liu75be7b92022-02-01 09:54:09 -08002047cc_library {
2048 name: "libcrypto",
2049 target: {
2050 android: {
2051 inject_bssl_hash: true,
2052 },
2053 },
2054 include_build_directory: false,
2055}
2056`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002057 ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
Yu Liu75be7b92022-02-01 09:54:09 -08002058 "inject_bssl_hash": `select({
2059 "//build/bazel/platforms/os:android": True,
2060 "//conditions:default": None,
2061 })`,
2062 }),
2063 },
2064 )
2065}
2066
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002067func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
Jingwen Chen97b85312021-10-08 10:41:31 +00002068 type testCase struct {
2069 cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002070 c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002071 gnu_extensions string
2072 bazel_cpp_std string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002073 bazel_c_std string
Jingwen Chen97b85312021-10-08 10:41:31 +00002074 }
2075
2076 testCases := []testCase{
2077 // Existing usages of cpp_std in AOSP are:
2078 // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
2079 //
2080 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
2081 // is set in the toolchain.)
2082 {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
Liz Kammera5a29de2022-05-25 23:19:37 -04002083 {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 +00002084 {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
2085 // experimental defaults to gnu++2a
Liz Kammera5a29de2022-05-25 23:19:37 -04002086 {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
2087 {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
2088 {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
Jingwen Chen97b85312021-10-08 10:41:31 +00002089 // Explicitly setting a c++ std does not use replace gnu++ std even if
2090 // gnu_extensions is true.
2091 // "c++11",
2092 {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002093 {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 +00002094 {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
2095 // "c++17",
2096 {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002097 {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 +00002098 {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
2099 // "c++2a",
2100 {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002101 {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 +00002102 {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
2103 // "c++98",
2104 {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002105 {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 +00002106 {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
2107 // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
2108 // "gnu++11",
2109 {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002110 {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 +00002111 {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
2112 // "gnu++17",
2113 {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
Liz Kammera5a29de2022-05-25 23:19:37 -04002114 {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 +00002115 {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002116
2117 // some c_std test cases
Liz Kammera5a29de2022-05-25 23:19:37 -04002118 {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
2119 {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
2120 {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
Chris Parsons79bd2b72021-11-29 17:52:41 -05002121 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
2122 {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
2123 {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 +00002124 }
Chris Parsons79bd2b72021-11-29 17:52:41 -05002125 for i, tc := range testCases {
Liz Kammera5a29de2022-05-25 23:19:37 -04002126 name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
2127 t.Run(name, func(t *testing.T) {
2128 name_prefix := fmt.Sprintf("a_%v", i)
2129 cppStdProp := ""
2130 if tc.cpp_std != "" {
2131 cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
2132 }
2133 cStdProp := ""
2134 if tc.c_std != "" {
2135 cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
2136 }
2137 gnuExtensionsProp := ""
2138 if tc.gnu_extensions != "" {
2139 gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
2140 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002141 attrs := AttrNameToString{}
Liz Kammera5a29de2022-05-25 23:19:37 -04002142 if tc.bazel_cpp_std != "" {
2143 attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
2144 }
2145 if tc.bazel_c_std != "" {
2146 attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
2147 }
Jingwen Chen97b85312021-10-08 10:41:31 +00002148
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002149 runCcLibraryTestCase(t, Bp2buildTestCase{
2150 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002151 "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002152 ModuleTypeUnderTest: "cc_library",
2153 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2154 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen97b85312021-10-08 10:41:31 +00002155cc_library {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002156 name: "%s_full",
Jingwen Chen97b85312021-10-08 10:41:31 +00002157%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002158%s // c_std: *string
Jingwen Chen97b85312021-10-08 10:41:31 +00002159%s // gnu_extensions: *bool
2160 include_build_directory: false,
2161}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002162`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002163 ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002164 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002165
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002166 runCcLibraryStaticTestCase(t, Bp2buildTestCase{
2167 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002168 "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002169 ModuleTypeUnderTest: "cc_library_static",
2170 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
2171 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002172cc_library_static {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002173 name: "%s_static",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002174%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002175%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002176%s // gnu_extensions: *bool
2177 include_build_directory: false,
2178}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002179`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002180 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002181 MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002182 },
2183 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002184
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002185 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
2186 Description: fmt.Sprintf(
Liz Kammera5a29de2022-05-25 23:19:37 -04002187 "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002188 ModuleTypeUnderTest: "cc_library_shared",
2189 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
2190 Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002191cc_library_shared {
Chris Parsons79bd2b72021-11-29 17:52:41 -05002192 name: "%s_shared",
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002193%s // cpp_std: *string
Chris Parsons79bd2b72021-11-29 17:52:41 -05002194%s // c_std: *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002195%s // gnu_extensions: *bool
2196 include_build_directory: false,
2197}
Chris Parsons79bd2b72021-11-29 17:52:41 -05002198`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002199 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002200 MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
Liz Kammera5a29de2022-05-25 23:19:37 -04002201 },
2202 })
Jingwen Chen5b11ab12021-10-11 17:44:33 +00002203 })
Jingwen Chen97b85312021-10-08 10:41:31 +00002204 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002205}
Liz Kammer12615db2021-09-28 09:19:17 -04002206
2207func TestCcLibraryProtoSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002208 runCcLibraryTestCase(t, Bp2buildTestCase{
2209 ModuleTypeUnderTest: "cc_library",
2210 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2211 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002212 name: "foo",
2213 srcs: ["foo.proto"],
2214 include_build_directory: false,
2215}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002216 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002217 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002218 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002219 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002220 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002221 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002222 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002223 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002224 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002225 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2226 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002227 }),
2228 },
2229 })
2230}
2231
2232func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002233 runCcLibraryTestCase(t, Bp2buildTestCase{
2234 ModuleTypeUnderTest: "cc_library",
2235 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2236 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002237 name: "foo",
2238 srcs: ["foo.proto"],
2239 proto: { canonical_path_from_root: false},
2240 include_build_directory: false,
2241}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002242 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002243 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002244 "srcs": `["foo.proto"]`,
2245 "strip_import_prefix": `""`,
Alixe06d75b2022-08-31 18:28:19 +00002246 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002247 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002248 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002249 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002250 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002251 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002252 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2253 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002254 }),
2255 },
2256 })
2257}
2258
2259func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002260 runCcLibraryTestCase(t, Bp2buildTestCase{
2261 ModuleTypeUnderTest: "cc_library",
2262 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2263 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002264 name: "foo",
2265 srcs: ["foo.proto"],
2266 proto: { canonical_path_from_root: true},
2267 include_build_directory: false,
2268}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002269 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002270 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7756c8f2022-02-14 20:49:15 -05002271 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002272 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002273 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002274 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002275 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002276 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002277 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002278 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2279 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002280 }),
2281 },
2282 })
2283}
2284
2285func TestCcLibraryProtoFull(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002286 runCcLibraryTestCase(t, Bp2buildTestCase{
2287 ModuleTypeUnderTest: "cc_library",
2288 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2289 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002290 name: "foo",
2291 srcs: ["foo.proto"],
2292 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002293 type: "full",
2294 },
2295 include_build_directory: false,
2296}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002297 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002298 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002299 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002300 }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002301 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002302 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002303 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002304 "deps": `[":libprotobuf-cpp-full"]`,
Alixe06d75b2022-08-31 18:28:19 +00002305 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002306 "dynamic_deps": `[":libprotobuf-cpp-full"]`,
2307 "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002308 }),
2309 },
2310 })
2311}
2312
2313func TestCcLibraryProtoLite(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002314 runCcLibraryTestCase(t, Bp2buildTestCase{
2315 ModuleTypeUnderTest: "cc_library",
2316 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2317 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002318 name: "foo",
2319 srcs: ["foo.proto"],
2320 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002321 type: "lite",
2322 },
2323 include_build_directory: false,
2324}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002325 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002326 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002327 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002328 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002329 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002330 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002331 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Chris Parsons77acf2e2021-12-03 17:27:16 -05002332 "deps": `[":libprotobuf-cpp-lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002333 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002334 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2335 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002336 }),
2337 },
2338 })
2339}
2340
2341func TestCcLibraryProtoExportHeaders(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002342 runCcLibraryTestCase(t, Bp2buildTestCase{
2343 ModuleTypeUnderTest: "cc_library",
2344 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2345 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammer12615db2021-09-28 09:19:17 -04002346 name: "foo",
2347 srcs: ["foo.proto"],
2348 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -04002349 export_proto_headers: true,
2350 },
2351 include_build_directory: false,
2352}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002353 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002354 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002355 "srcs": `["foo.proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002356 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -04002357 "deps": `[":foo_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002358 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002359 "deps": `[":libprotobuf-cpp-lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002360 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Alixe06d75b2022-08-31 18:28:19 +00002361 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons77acf2e2021-12-03 17:27:16 -05002362 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2363 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
Liz Kammer12615db2021-09-28 09:19:17 -04002364 }),
2365 },
2366 })
2367}
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002368
Yu Liu2d136142022-08-18 14:46:13 -07002369func TestCcLibraryProtoIncludeDirs(t *testing.T) {
2370 runCcLibraryTestCase(t, Bp2buildTestCase{
2371 ModuleTypeUnderTest: "cc_library",
2372 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2373 Blueprint: soongCcProtoPreamble + `cc_library {
2374 name: "foo",
2375 srcs: ["foo.proto"],
2376 proto: {
2377 include_dirs: ["external/protobuf/src"],
2378 },
2379 include_build_directory: false,
2380}`,
2381 ExpectedBazelTargets: []string{
2382 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
2383 "srcs": `["foo.proto"]`,
2384 "deps": `["//external/protobuf:libprotobuf-proto"]`,
2385 }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
2386 "deps": `[":foo_proto"]`,
2387 }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
2388 "deps": `[":libprotobuf-cpp-lite"]`,
2389 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
2390 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04002391 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2392 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
Yu Liu2d136142022-08-18 14:46:13 -07002393 }),
2394 },
2395 })
2396}
2397
2398func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
2399 runCcLibraryTestCase(t, Bp2buildTestCase{
2400 ModuleTypeUnderTest: "cc_library",
2401 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2402 Blueprint: soongCcProtoPreamble + `cc_library {
2403 name: "foo",
2404 srcs: ["foo.proto"],
2405 proto: {
2406 include_dirs: ["external/protobuf/abc"],
2407 },
2408 include_build_directory: false,
2409}`,
Spandan Dasec39d512023-08-15 22:08:18 +00002410 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 -07002411 })
2412}
2413
Yu Liu2aa806b2022-09-01 11:54:47 -07002414func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
2415 runCcLibraryTestCase(t, Bp2buildTestCase{
2416 ModuleTypeUnderTest: "cc_library",
2417 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2418 Blueprint: soongCcProtoPreamble + `
2419filegroup {
2420 name: "a_fg_proto",
2421 srcs: ["a_fg.proto"],
2422}
2423
2424cc_library {
2425 name: "a",
2426 srcs: [
2427 ":a_fg_proto",
2428 "a.proto",
2429 ],
2430 proto: {
2431 export_proto_headers: true,
2432 },
2433 include_build_directory: false,
2434}`,
2435 ExpectedBazelTargets: []string{
2436 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002437 "deps": `[":a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002438 "srcs": `["a.proto"]`,
2439 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2440 "deps": `[
2441 ":a_fg_proto_bp2build_converted",
2442 ":a_proto",
2443 ]`,
2444 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2445 "deps": `[":libprotobuf-cpp-lite"]`,
2446 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2447 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2448 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2449 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002450 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002451 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002452 "tags": `[
2453 "apex_available=//apex_available:anyapex",
2454 "manual",
2455 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002456 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2457 "actual": `"//.:a_fg_proto_proto"`,
2458 "tags": `[
2459 "apex_available=//apex_available:anyapex",
2460 "manual",
2461 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002462 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2463 "srcs": `["a_fg.proto"]`,
2464 }),
2465 },
2466 })
2467}
2468
2469func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
2470 runCcLibraryTestCase(t, Bp2buildTestCase{
2471 ModuleTypeUnderTest: "cc_library",
2472 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2473 Blueprint: soongCcProtoPreamble + `
2474filegroup {
2475 name: "a_fg_proto",
2476 srcs: ["a_fg.proto"],
2477}
2478
2479cc_library {
2480 name: "a",
2481 srcs: [
2482 ":a_fg_proto",
2483 ],
2484 proto: {
2485 export_proto_headers: true,
2486 },
2487 include_build_directory: false,
2488}`,
2489 ExpectedBazelTargets: []string{
2490 MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2491 "deps": `[":a_fg_proto_bp2build_converted"]`,
2492 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2493 "deps": `[":libprotobuf-cpp-lite"]`,
2494 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2495 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2496 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2497 "whole_archive_deps": `[":a_cc_proto_lite"]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002498 }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -07002499 "srcs": `["a_fg.proto"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04002500 "tags": `[
2501 "apex_available=//apex_available:anyapex",
2502 "manual",
2503 ]`,
Spandan Dasdf3ec822023-08-04 02:19:53 +00002504 }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
2505 "actual": `"//.:a_fg_proto_proto"`,
2506 "tags": `[
2507 "apex_available=//apex_available:anyapex",
2508 "manual",
2509 ]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002510 }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
2511 "srcs": `["a_fg.proto"]`,
2512 }),
2513 },
2514 })
2515}
2516
2517func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
2518 runCcLibraryTestCase(t, Bp2buildTestCase{
2519 ModuleTypeUnderTest: "cc_library",
2520 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002521 StubbedBuildDefinitions: []string{"//path/to/A:a_fg_proto"},
Yu Liu2aa806b2022-09-01 11:54:47 -07002522 Filesystem: map[string]string{
2523 "path/to/A/Android.bp": `
2524filegroup {
2525 name: "a_fg_proto",
2526 srcs: ["a_fg.proto"],
2527}`,
2528 },
2529 Blueprint: soongCcProtoPreamble + `
2530cc_library {
2531 name: "a",
2532 srcs: [
2533 ":a_fg_proto",
2534 "a.proto",
2535 ],
2536 proto: {
2537 export_proto_headers: true,
2538 },
2539 include_build_directory: false,
2540}`,
2541 ExpectedBazelTargets: []string{
2542 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Yu Liu2a85fb12022-09-15 22:18:48 -07002543 "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
Yu Liu2aa806b2022-09-01 11:54:47 -07002544 "srcs": `["a.proto"]`,
2545 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
2546 "deps": `[
2547 "//path/to/A:a_fg_proto_bp2build_converted",
2548 ":a_proto",
2549 ]`,
2550 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
2551 "deps": `[":libprotobuf-cpp-lite"]`,
2552 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2553 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
2554 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2555 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2556 }),
2557 },
2558 })
2559}
2560
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002561func TestCcLibraryProtoFilegroups(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002562 runCcLibraryTestCase(t, Bp2buildTestCase{
2563 ModuleTypeUnderTest: "cc_library",
2564 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00002565 StubbedBuildDefinitions: []string{"a_fg_proto", "b_protos", "c-proto-srcs", "proto-srcs-d"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002566 Blueprint: soongCcProtoPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +00002567 simpleModule("filegroup", "a_fg_proto") +
2568 simpleModule("filegroup", "b_protos") +
2569 simpleModule("filegroup", "c-proto-srcs") +
2570 simpleModule("filegroup", "proto-srcs-d") + `
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002571cc_library {
2572 name: "a",
2573 srcs: [":a_fg_proto"],
2574 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002575 export_proto_headers: true,
2576 },
2577 include_build_directory: false,
2578}
2579
2580cc_library {
2581 name: "b",
2582 srcs: [":b_protos"],
2583 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002584 export_proto_headers: true,
2585 },
2586 include_build_directory: false,
2587}
2588
2589cc_library {
2590 name: "c",
2591 srcs: [":c-proto-srcs"],
2592 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002593 export_proto_headers: true,
2594 },
2595 include_build_directory: false,
2596}
2597
2598cc_library {
2599 name: "d",
2600 srcs: [":proto-srcs-d"],
2601 proto: {
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002602 export_proto_headers: true,
2603 },
2604 include_build_directory: false,
2605}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002606 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00002607 MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002608 "srcs": `[":a_fg_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002609 }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002610 "deps": `[":a_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002611 }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002612 "deps": `[":libprotobuf-cpp-lite"]`,
2613 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2614 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2615 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2616 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002617 }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002618 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2619 "whole_archive_deps": `[":a_cc_proto_lite"]`,
2620 "srcs": `[":a_fg_proto_cpp_srcs"]`,
2621 "srcs_as": `[":a_fg_proto_as_srcs"]`,
2622 "srcs_c": `[":a_fg_proto_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002623 }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002624 "srcs": `[":b_protos"]`,
Alixe06d75b2022-08-31 18:28:19 +00002625 }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002626 "deps": `[":b_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002627 }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002628 "deps": `[":libprotobuf-cpp-lite"]`,
2629 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2630 "srcs": `[":b_protos_cpp_srcs"]`,
2631 "srcs_as": `[":b_protos_as_srcs"]`,
2632 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002633 }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002634 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2635 "whole_archive_deps": `[":b_cc_proto_lite"]`,
2636 "srcs": `[":b_protos_cpp_srcs"]`,
2637 "srcs_as": `[":b_protos_as_srcs"]`,
2638 "srcs_c": `[":b_protos_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002639 }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002640 "srcs": `[":c-proto-srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002641 }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002642 "deps": `[":c_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002643 }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002644 "deps": `[":libprotobuf-cpp-lite"]`,
2645 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2646 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2647 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2648 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002649 }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002650 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2651 "whole_archive_deps": `[":c_cc_proto_lite"]`,
2652 "srcs": `[":c-proto-srcs_cpp_srcs"]`,
2653 "srcs_as": `[":c-proto-srcs_as_srcs"]`,
2654 "srcs_c": `[":c-proto-srcs_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002655 }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002656 "srcs": `[":proto-srcs-d"]`,
Alixe06d75b2022-08-31 18:28:19 +00002657 }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002658 "deps": `[":d_proto"]`,
Alixe06d75b2022-08-31 18:28:19 +00002659 }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002660 "deps": `[":libprotobuf-cpp-lite"]`,
2661 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2662 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2663 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2664 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
Alixe06d75b2022-08-31 18:28:19 +00002665 }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
Liz Kammeraabfb5d2021-12-08 15:25:06 -05002666 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
2667 "whole_archive_deps": `[":d_cc_proto_lite"]`,
2668 "srcs": `[":proto-srcs-d_cpp_srcs"]`,
2669 "srcs_as": `[":proto-srcs-d_as_srcs"]`,
2670 "srcs_c": `[":proto-srcs-d_c_srcs"]`,
2671 }),
2672 },
2673 })
2674}
Chris Parsons58852a02021-12-09 18:10:18 -05002675
2676func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002677 runCcLibraryTestCase(t, Bp2buildTestCase{
2678 ModuleTypeUnderTest: "cc_library",
2679 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2680 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002681 name: "foo",
2682 srcs: ["foo.cpp"],
Liz Kammerdfeb1202022-05-13 17:20:20 -04002683 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002684 target: {
2685 darwin: {
2686 enabled: false,
2687 },
2688 windows: {
2689 enabled: false,
2690 },
2691 linux_glibc_x86: {
2692 enabled: false,
2693 },
2694 },
2695 include_build_directory: false,
2696}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002697 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002698 "srcs": `["foo.cpp"]`,
2699 "target_compatible_with": `select({
2700 "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2701 "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2702 "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2703 "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2704 "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
2705 "//conditions:default": [],
2706 })`,
2707 }),
2708 })
2709}
2710
2711func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002712 runCcLibraryTestCase(t, Bp2buildTestCase{
2713 ModuleTypeUnderTest: "cc_library",
2714 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2715 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002716 name: "foo",
2717 srcs: ["foo.cpp"],
2718 enabled: false,
Liz Kammerdfeb1202022-05-13 17:20:20 -04002719 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002720 target: {
2721 darwin: {
2722 enabled: true,
2723 },
2724 windows: {
2725 enabled: false,
2726 },
2727 linux_glibc_x86: {
2728 enabled: false,
2729 },
2730 },
2731 include_build_directory: false,
2732}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002733 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002734 "srcs": `["foo.cpp"]`,
2735 "target_compatible_with": `select({
2736 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2737 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2738 "//conditions:default": ["@platforms//:incompatible"],
2739 })`,
2740 }),
2741 })
2742}
2743
2744func TestCcLibrarySharedDisabled(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002745 runCcLibraryTestCase(t, Bp2buildTestCase{
2746 ModuleTypeUnderTest: "cc_library",
2747 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2748 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002749 name: "foo",
2750 srcs: ["foo.cpp"],
2751 enabled: false,
2752 shared: {
2753 enabled: true,
2754 },
2755 target: {
2756 android: {
2757 shared: {
2758 enabled: false,
2759 },
2760 }
2761 },
2762 include_build_directory: false,
2763}`,
Alixe06d75b2022-08-31 18:28:19 +00002764 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002765 "srcs": `["foo.cpp"]`,
2766 "target_compatible_with": `["@platforms//:incompatible"]`,
Alixe06d75b2022-08-31 18:28:19 +00002767 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002768 "srcs": `["foo.cpp"]`,
2769 "target_compatible_with": `select({
2770 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
2771 "//conditions:default": [],
2772 })`,
2773 }),
2774 },
2775 })
2776}
2777
2778func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002779 runCcLibraryTestCase(t, Bp2buildTestCase{
2780 ModuleTypeUnderTest: "cc_library",
2781 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2782 Blueprint: soongCcProtoPreamble + `cc_library {
Chris Parsons58852a02021-12-09 18:10:18 -05002783 name: "foo",
Liz Kammerdfeb1202022-05-13 17:20:20 -04002784 host_supported: true,
Chris Parsons58852a02021-12-09 18:10:18 -05002785 srcs: ["foo.cpp"],
2786 shared: {
2787 enabled: false
2788 },
2789 target: {
2790 darwin: {
2791 enabled: true,
2792 },
2793 windows: {
2794 enabled: false,
2795 },
2796 linux_glibc_x86: {
2797 shared: {
2798 enabled: true,
2799 },
2800 },
2801 },
2802 include_build_directory: false,
2803}`,
Alixe06d75b2022-08-31 18:28:19 +00002804 ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002805 "srcs": `["foo.cpp"]`,
2806 "target_compatible_with": `select({
2807 "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
2808 "//conditions:default": [],
2809 })`,
Alixe06d75b2022-08-31 18:28:19 +00002810 }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -05002811 "srcs": `["foo.cpp"]`,
2812 "target_compatible_with": `select({
2813 "//build/bazel/platforms/os_arch:darwin_arm64": [],
2814 "//build/bazel/platforms/os_arch:darwin_x86_64": [],
2815 "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
2816 "//conditions:default": ["@platforms//:incompatible"],
2817 })`,
2818 }),
2819 }})
2820}
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002821
2822func TestCcLibraryStubs(t *testing.T) {
Wei Li81852ca2022-07-27 00:22:06 -07002823 expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
Yu Liu56ccb1a2022-11-12 10:47:07 -08002824 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002825 })
2826 expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
Spandan Das04f9f4c2023-09-13 23:59:05 +00002827 "api_surface": `"module-libapi"`,
Sam Delmerico5f906492023-03-15 18:06:18 -04002828 "soname": `"a.so"`,
2829 "source_library_label": `"//foo/bar:a"`,
2830 "stubs_symbol_file": `"a.map.txt"`,
Wei Li81852ca2022-07-27 00:22:06 -07002831 "stubs_versions": `[
2832 "28",
2833 "29",
2834 "current",
2835 ]`,
2836 }))
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002837 runCcLibraryTestCase(t, Bp2buildTestCase{
2838 Description: "cc_library stubs",
2839 ModuleTypeUnderTest: "cc_library",
2840 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2841 Dir: "foo/bar",
2842 Filesystem: map[string]string{
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002843 "foo/bar/Android.bp": `
2844cc_library {
2845 name: "a",
2846 stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
2847 bazel_module: { bp2build_available: true },
2848 include_build_directory: false,
2849}
2850`,
2851 },
Wei Li81852ca2022-07-27 00:22:06 -07002852 Blueprint: soongCcLibraryPreamble,
2853 ExpectedBazelTargets: expectedBazelTargets,
Jingwen Chen0ee88a62022-01-07 14:55:29 +00002854 },
2855 )
2856}
Liz Kammerf38a8372022-02-04 15:39:00 -05002857
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002858func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
2859 runCcLibraryTestCase(t, Bp2buildTestCase{
2860 Description: "stub target generation of the same lib across configs should not result in duplicates",
2861 ModuleTypeUnderTest: "cc_library",
2862 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2863 Filesystem: map[string]string{
2864 "bar.map.txt": "",
2865 },
Chris Parsonscd209032023-09-19 01:12:48 +00002866 StubbedBuildDefinitions: []string{"barlib"},
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002867 Blueprint: `
2868cc_library {
2869 name: "barlib",
2870 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002871}
2872cc_library {
2873 name: "foolib",
2874 shared_libs: ["barlib"],
2875 target: {
2876 android: {
2877 shared_libs: ["barlib"],
2878 },
2879 },
2880 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002881 apex_available: ["foo"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002882}`,
2883 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2884 "implementation_dynamic_deps": `select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00002885 "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"],
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002886 "//conditions:default": [":barlib"],
2887 })`,
2888 "local_includes": `["."]`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002889 "tags": `["apex_available=foo"]`,
Trevor Radcliffe0ed6b7d2022-09-12 20:19:26 +00002890 }),
2891 })
2892}
2893
Liz Kammerffc17e42022-11-23 09:42:05 -05002894func TestCcLibraryExcludesLibsHost(t *testing.T) {
2895 runCcLibraryTestCase(t, Bp2buildTestCase{
2896 ModuleTypeUnderTest: "cc_library",
2897 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2898 Filesystem: map[string]string{
2899 "bar.map.txt": "",
2900 },
Chris Parsonscd209032023-09-19 01:12:48 +00002901 StubbedBuildDefinitions: []string{"bazlib", "quxlib", "barlib"},
2902 Blueprint: simpleModule("cc_library", "bazlib") + `
Liz Kammerffc17e42022-11-23 09:42:05 -05002903cc_library {
2904 name: "quxlib",
2905 stubs: { symbol_file: "bar.map.txt", versions: ["current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002906}
2907cc_library {
2908 name: "barlib",
2909 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Liz Kammerffc17e42022-11-23 09:42:05 -05002910}
2911cc_library {
2912 name: "foolib",
2913 shared_libs: ["barlib", "quxlib"],
2914 target: {
2915 host: {
2916 shared_libs: ["bazlib"],
2917 exclude_shared_libs: ["barlib"],
2918 },
2919 },
2920 include_build_directory: false,
2921 bazel_module: { bp2build_available: true },
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002922 apex_available: ["foo"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002923}`,
2924 ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
2925 "implementation_dynamic_deps": `select({
2926 "//build/bazel/platforms/os:darwin": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002927 "//build/bazel/platforms/os:linux_bionic": [":bazlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002928 "//build/bazel/platforms/os:linux_glibc": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002929 "//build/bazel/platforms/os:linux_musl": [":bazlib"],
2930 "//build/bazel/platforms/os:windows": [":bazlib"],
2931 "//conditions:default": [],
2932 }) + select({
2933 "//build/bazel/platforms/os:darwin": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002934 "//build/bazel/platforms/os:linux_bionic": [":quxlib"],
Colin Cross133782e2022-12-20 15:29:31 -08002935 "//build/bazel/platforms/os:linux_glibc": [":quxlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002936 "//build/bazel/platforms/os:linux_musl": [":quxlib"],
2937 "//build/bazel/platforms/os:windows": [":quxlib"],
Spandan Das6d4d9da2023-04-18 06:20:40 +00002938 "//build/bazel/rules/apex:foo": [
2939 "@api_surfaces//module-libapi/current:barlib",
2940 "@api_surfaces//module-libapi/current:quxlib",
2941 ],
Liz Kammerffc17e42022-11-23 09:42:05 -05002942 "//conditions:default": [
2943 ":barlib",
2944 ":quxlib",
2945 ],
2946 })`,
Liz Kammer48cdbeb2023-03-17 10:17:50 -04002947 "tags": `["apex_available=foo"]`,
Liz Kammerffc17e42022-11-23 09:42:05 -05002948 }),
2949 })
2950}
2951
Liz Kammerf38a8372022-02-04 15:39:00 -05002952func TestCcLibraryEscapeLdflags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002953 runCcLibraryTestCase(t, Bp2buildTestCase{
2954 ModuleTypeUnderTest: "cc_library",
2955 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2956 Blueprint: soongCcProtoPreamble + `cc_library {
Liz Kammerf38a8372022-02-04 15:39:00 -05002957 name: "foo",
2958 ldflags: ["-Wl,--rpath,${ORIGIN}"],
2959 include_build_directory: false,
2960}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002961 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
Liz Kammerf38a8372022-02-04 15:39:00 -05002962 "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
2963 }),
2964 })
2965}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002966
2967func TestCcLibraryConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002968 runCcLibraryTestCase(t, Bp2buildTestCase{
2969 ModuleTypeUnderTest: "cc_library",
2970 ModuleTypeUnderTestFactory: cc.LibraryFactory,
2971 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002972 "foo.c": "",
2973 "bar.cc": "",
2974 "foo1.l": "",
2975 "bar1.ll": "",
2976 "foo2.l": "",
2977 "bar2.ll": "",
2978 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002979 Blueprint: `cc_library {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002980 name: "foo_lib",
2981 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
2982 lex: { flags: ["--foo_flags"] },
2983 include_build_directory: false,
2984 bazel_module: { bp2build_available: true },
2985}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00002986 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +00002987 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002988 "srcs": `[
2989 "foo1.l",
2990 "foo2.l",
2991 ]`,
2992 "lexopts": `["--foo_flags"]`,
2993 }),
Alixe06d75b2022-08-31 18:28:19 +00002994 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00002995 "srcs": `[
2996 "bar1.ll",
2997 "bar2.ll",
2998 ]`,
2999 "lexopts": `["--foo_flags"]`,
3000 }),
3001 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00003002 makeCcLibraryTargets("foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +00003003 "srcs": `[
3004 "bar.cc",
3005 ":foo_lib_genlex_ll",
3006 ]`,
3007 "srcs_c": `[
3008 "foo.c",
3009 ":foo_lib_genlex_l",
3010 ]`,
3011 })...),
3012 })
3013}
Cole Faust6b29f592022-08-09 09:50:56 -07003014
3015func TestCCLibraryRuntimeDeps(t *testing.T) {
3016 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
3017 Blueprint: `cc_library_shared {
3018 name: "bar",
3019}
3020
3021cc_library {
3022 name: "foo",
Chris Parsonscd209032023-09-19 01:12:48 +00003023 runtime_libs: ["bar"],
Cole Faust6b29f592022-08-09 09:50:56 -07003024}`,
3025 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003026 MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
Cole Faust6b29f592022-08-09 09:50:56 -07003027 "local_includes": `["."]`,
3028 }),
Alixe06d75b2022-08-31 18:28:19 +00003029 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003030 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003031 "local_includes": `["."]`,
3032 }),
Alixe06d75b2022-08-31 18:28:19 +00003033 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsonscd209032023-09-19 01:12:48 +00003034 "runtime_deps": `[":bar"]`,
Cole Faust6b29f592022-08-09 09:50:56 -07003035 "local_includes": `["."]`,
3036 }),
3037 },
3038 })
3039}
Cole Faust5fa4e962022-08-22 14:31:04 -07003040
3041func TestCcLibraryWithInstructionSet(t *testing.T) {
3042 runCcLibraryTestCase(t, Bp2buildTestCase{
3043 ModuleTypeUnderTest: "cc_library",
3044 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3045 Blueprint: `cc_library {
3046 name: "foo",
3047 arch: {
3048 arm: {
3049 instruction_set: "arm",
3050 }
3051 }
3052}
3053`,
3054 ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
3055 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +00003056 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -07003057 "//conditions:default": [],
3058 })`,
3059 "local_includes": `["."]`,
3060 }),
3061 })
3062}
Vinh Tran9f6796a2022-08-16 13:10:31 -04003063
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003064func TestCcLibraryEmptySuffix(t *testing.T) {
3065 runCcLibraryTestCase(t, Bp2buildTestCase{
3066 Description: "cc_library with empty suffix",
3067 ModuleTypeUnderTest: "cc_library",
3068 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3069 Filesystem: map[string]string{
3070 "foo.c": "",
3071 },
3072 Blueprint: `cc_library {
3073 name: "foo",
3074 suffix: "",
3075 srcs: ["foo.c"],
3076 include_build_directory: false,
3077}`,
3078 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003079 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 -05003080 "srcs_c": `["foo.c"]`,
3081 }),
Alixe06d75b2022-08-31 18:28:19 +00003082 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003083 "srcs_c": `["foo.c"]`,
3084 "suffix": `""`,
3085 }),
3086 },
3087 })
3088}
3089
3090func TestCcLibrarySuffix(t *testing.T) {
3091 runCcLibraryTestCase(t, Bp2buildTestCase{
3092 Description: "cc_library with suffix",
3093 ModuleTypeUnderTest: "cc_library",
3094 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3095 Filesystem: map[string]string{
3096 "foo.c": "",
3097 },
3098 Blueprint: `cc_library {
3099 name: "foo",
3100 suffix: "-suf",
3101 srcs: ["foo.c"],
3102 include_build_directory: false,
3103}`,
3104 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003105 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 -05003106 "srcs_c": `["foo.c"]`,
3107 }),
Alixe06d75b2022-08-31 18:28:19 +00003108 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003109 "srcs_c": `["foo.c"]`,
3110 "suffix": `"-suf"`,
3111 }),
3112 },
3113 })
3114}
3115
3116func TestCcLibraryArchVariantSuffix(t *testing.T) {
3117 runCcLibraryTestCase(t, Bp2buildTestCase{
3118 Description: "cc_library with arch-variant suffix",
3119 ModuleTypeUnderTest: "cc_library",
3120 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3121 Filesystem: map[string]string{
3122 "foo.c": "",
3123 },
3124 Blueprint: `cc_library {
3125 name: "foo",
3126 arch: {
3127 arm64: { suffix: "-64" },
3128 arm: { suffix: "-32" },
3129 },
3130 srcs: ["foo.c"],
3131 include_build_directory: false,
3132}`,
3133 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003134 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 -05003135 "srcs_c": `["foo.c"]`,
3136 }),
Alixe06d75b2022-08-31 18:28:19 +00003137 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003138 "srcs_c": `["foo.c"]`,
3139 "suffix": `select({
3140 "//build/bazel/platforms/arch:arm": "-32",
3141 "//build/bazel/platforms/arch:arm64": "-64",
3142 "//conditions:default": None,
3143 })`,
3144 }),
3145 },
3146 })
3147}
3148
Vinh Tran367d89d2023-04-28 11:21:25 -04003149func TestCcLibraryWithAidlLibrary(t *testing.T) {
3150 runCcLibraryTestCase(t, Bp2buildTestCase{
3151 Description: "cc_library with aidl_library",
3152 ModuleTypeUnderTest: "cc_library",
3153 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3154 Blueprint: `
3155aidl_library {
3156 name: "A_aidl",
3157 srcs: ["aidl/A.aidl"],
3158 hdrs: ["aidl/Header.aidl"],
3159 strip_import_prefix: "aidl",
3160}
3161cc_library {
3162 name: "foo",
3163 aidl: {
3164 libs: ["A_aidl"],
Vinh Trane6842942023-04-28 11:21:25 -04003165 },
3166 export_include_dirs: ["include"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003167}`,
3168 ExpectedBazelTargets: []string{
3169 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3170 "srcs": `["aidl/A.aidl"]`,
3171 "hdrs": `["aidl/Header.aidl"]`,
3172 "strip_import_prefix": `"aidl"`,
3173 "tags": `["apex_available=//apex_available:anyapex"]`,
3174 }),
3175 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003176 "deps": `[":A_aidl"]`,
3177 "local_includes": `["."]`,
3178 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003179 }),
3180 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3181 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3182 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003183 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003184 }),
3185 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3186 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3187 "local_includes": `["."]`,
Vinh Trane6842942023-04-28 11:21:25 -04003188 "export_includes": `["include"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003189 }),
3190 },
3191 })
3192}
3193
Vinh Tran9f6796a2022-08-16 13:10:31 -04003194func TestCcLibraryWithAidlSrcs(t *testing.T) {
3195 runCcLibraryTestCase(t, Bp2buildTestCase{
3196 Description: "cc_library with aidl srcs",
3197 ModuleTypeUnderTest: "cc_library",
3198 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3199 Blueprint: `
3200filegroup {
3201 name: "A_aidl",
3202 srcs: ["aidl/A.aidl"],
3203 path: "aidl",
3204}
3205cc_library {
3206 name: "foo",
3207 srcs: [
3208 ":A_aidl",
3209 "B.aidl",
3210 ],
3211}`,
3212 ExpectedBazelTargets: []string{
3213 MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
3214 "srcs": `["aidl/A.aidl"]`,
3215 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -04003216 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003217 }),
Alixe06d75b2022-08-31 18:28:19 +00003218 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
Vinh Tran9f6796a2022-08-16 13:10:31 -04003219 "srcs": `["B.aidl"]`,
3220 }),
Alixe06d75b2022-08-31 18:28:19 +00003221 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003222 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003223 "deps": `[
3224 ":A_aidl",
3225 ":foo_aidl_library",
3226 ]`,
3227 }),
Alixe06d75b2022-08-31 18:28:19 +00003228 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003229 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3230 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003231 }),
Alixe06d75b2022-08-31 18:28:19 +00003232 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003233 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3234 "local_includes": `["."]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003235 }),
3236 },
3237 })
3238}
3239
3240func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
3241 runCcLibraryTestCase(t, Bp2buildTestCase{
3242 Description: "cc_library with non aidl filegroup",
3243 ModuleTypeUnderTest: "cc_library",
3244 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003245 StubbedBuildDefinitions: []string{"//path/to/A:A_aidl"},
Vinh Tran9f6796a2022-08-16 13:10:31 -04003246 Filesystem: map[string]string{
3247 "path/to/A/Android.bp": `
3248filegroup {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003249 name: "A_aidl",
3250 srcs: ["aidl/A.aidl"],
3251 path: "aidl",
Vinh Tran9f6796a2022-08-16 13:10:31 -04003252}`,
3253 },
3254 Blueprint: `
3255cc_library {
Vinh Tranfde57eb2022-08-29 17:46:58 -04003256 name: "foo",
3257 srcs: [
3258 ":A_aidl",
3259 ],
Vinh Tran9f6796a2022-08-16 13:10:31 -04003260}`,
3261 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00003262 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003263 "local_includes": `["."]`,
3264 "deps": `["//path/to/A:A_aidl"]`,
Vinh Tran9f6796a2022-08-16 13:10:31 -04003265 }),
Alixe06d75b2022-08-31 18:28:19 +00003266 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Vinh Tranfde57eb2022-08-29 17:46:58 -04003267 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
3268 "local_includes": `["."]`,
3269 }),
Vinh Tranfde57eb2022-08-29 17:46:58 -04003270 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Liz Kammer84b0ecb2022-09-14 10:49:13 -04003271 "local_includes": `["."]`,
3272 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Vinh Tranfde57eb2022-08-29 17:46:58 -04003273 }),
3274 },
3275 })
3276}
3277
3278func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003279 t.Parallel()
3280
3281 expectedBazelTargets := []string{
3282 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003283 "local_includes": `["."]`,
3284 "deps": `[":foo_aidl_library"]`,
Vinh Tran367d89d2023-04-28 11:21:25 -04003285 }),
3286 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3287 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3288 "local_includes": `["."]`,
3289 }),
3290 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3291 "whole_archive_deps": `[":foo_cc_aidl_library"]`,
3292 "local_includes": `["."]`,
3293 }),
3294 }
3295 testCases := []struct {
3296 description string
3297 bp string
3298 expectedBazelTargets []string
3299 }{
3300 {
3301 description: "cc_library with aidl srcs and aidl.export_aidl_headers set",
3302 bp: `
3303 cc_library {
3304 name: "foo",
3305 srcs: [
3306 "Foo.aidl",
3307 ],
3308 aidl: {
3309 export_aidl_headers: true,
3310 }
3311 }`,
3312 expectedBazelTargets: append(
3313 expectedBazelTargets,
3314 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3315 "srcs": `["Foo.aidl"]`,
3316 })),
Vinh Tran9f6796a2022-08-16 13:10:31 -04003317 },
Vinh Tran367d89d2023-04-28 11:21:25 -04003318 {
3319 description: "cc_library with aidl.libs and aidl.export_aidl_headers set",
3320 bp: `
3321 aidl_library {
3322 name: "foo_aidl_library",
3323 srcs: ["Foo.aidl"],
3324 }
3325 cc_library {
3326 name: "foo",
3327 aidl: {
3328 libs: ["foo_aidl_library"],
3329 export_aidl_headers: true,
3330 }
3331 }`,
3332 expectedBazelTargets: append(
3333 expectedBazelTargets,
3334 MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{
3335 "srcs": `["Foo.aidl"]`,
3336 "tags": `["apex_available=//apex_available:anyapex"]`,
3337 }),
3338 ),
3339 },
3340 }
3341
3342 for _, testCase := range testCases {
3343 runCcLibraryTestCase(t, Bp2buildTestCase{
3344 Description: "cc_library with export aidl headers",
3345 ModuleTypeUnderTest: "cc_library",
3346 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3347 Blueprint: testCase.bp,
3348 ExpectedBazelTargets: testCase.expectedBazelTargets,
3349 })
3350 }
Vinh Tran9f6796a2022-08-16 13:10:31 -04003351}
Vinh Tran85fb07c2022-09-16 16:17:48 -04003352
3353func TestCcLibraryWithTargetApex(t *testing.T) {
3354 runCcLibraryTestCase(t, Bp2buildTestCase{
3355 Description: "cc_library with target.apex",
3356 ModuleTypeUnderTest: "cc_library",
3357 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3358 Blueprint: `
3359cc_library {
3360 name: "foo",
3361 shared_libs: ["bar", "baz"],
3362 static_libs: ["baz", "buh"],
3363 target: {
3364 apex: {
3365 exclude_shared_libs: ["bar"],
3366 exclude_static_libs: ["buh"],
3367 }
3368 }
3369}`,
3370 ExpectedBazelTargets: []string{
3371 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3372 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003373 "//build/bazel/rules/apex:in_apex": [],
3374 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003375 })`,
3376 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003377 "//build/bazel/rules/apex:in_apex": [],
3378 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003379 })`,
3380 "local_includes": `["."]`,
3381 }),
3382 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3383 "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003384 "//build/bazel/rules/apex:in_apex": [],
3385 "//conditions:default": [":buh__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003386 })`,
3387 "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003388 "//build/bazel/rules/apex:in_apex": [],
3389 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003390 })`,
3391 "local_includes": `["."]`,
3392 }),
3393 },
3394 })
3395}
3396
3397func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3398 runCcLibraryTestCase(t, Bp2buildTestCase{
3399 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3400 ModuleTypeUnderTest: "cc_library",
3401 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3402 Blueprint: `
3403cc_library_static {
3404 name: "foo",
3405 shared_libs: ["bar", "baz"],
3406 static_libs: ["abc"],
3407 export_shared_lib_headers: ["baz"],
3408 export_static_lib_headers: ["abc"],
3409 target: {
3410 apex: {
3411 exclude_shared_libs: ["baz", "bar"],
3412 exclude_static_libs: ["abc"],
3413 }
3414 }
3415}`,
3416 ExpectedBazelTargets: []string{
3417 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3418 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003419 "//build/bazel/rules/apex:in_apex": [],
3420 "//conditions:default": [":bar__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003421 })`,
3422 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003423 "//build/bazel/rules/apex:in_apex": [],
3424 "//conditions:default": [":baz__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003425 })`,
3426 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003427 "//build/bazel/rules/apex:in_apex": [],
3428 "//conditions:default": [":abc__BP2BUILD__MISSING__DEP"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003429 })`,
3430 "local_includes": `["."]`,
3431 }),
3432 },
3433 })
3434}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003435
3436func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3437 runCcLibraryTestCase(t, Bp2buildTestCase{
3438 Description: "cc_library with sysprop sources",
3439 ModuleTypeUnderTest: "cc_library",
3440 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3441 Blueprint: `
3442cc_library {
3443 name: "foo",
3444 srcs: [
3445 "bar.sysprop",
3446 "baz.sysprop",
3447 "blah.cpp",
3448 ],
3449 min_sdk_version: "5",
3450}`,
3451 ExpectedBazelTargets: []string{
3452 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3453 "srcs": `[
3454 "bar.sysprop",
3455 "baz.sysprop",
3456 ]`,
3457 }),
3458 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3459 "dep": `":foo_sysprop_library"`,
3460 "min_sdk_version": `"5"`,
3461 }),
3462 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3463 "srcs": `["blah.cpp"]`,
3464 "local_includes": `["."]`,
3465 "min_sdk_version": `"5"`,
3466 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3467 }),
3468 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3469 "srcs": `["blah.cpp"]`,
3470 "local_includes": `["."]`,
3471 "min_sdk_version": `"5"`,
3472 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3473 }),
3474 },
3475 })
3476}
3477
3478func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3479 runCcLibraryTestCase(t, Bp2buildTestCase{
3480 Description: "cc_library with sysprop sources in some configs but not others",
3481 ModuleTypeUnderTest: "cc_library",
3482 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3483 Blueprint: `
3484cc_library {
3485 name: "foo",
3486 host_supported: true,
3487 srcs: [
3488 "blah.cpp",
3489 ],
3490 target: {
3491 android: {
3492 srcs: ["bar.sysprop"],
3493 },
3494 },
3495 min_sdk_version: "5",
3496}`,
3497 ExpectedBazelTargets: []string{
3498 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3499 "srcs": `select({
3500 "//build/bazel/platforms/os:android": ["bar.sysprop"],
3501 "//conditions:default": [],
3502 })`,
3503 }),
3504 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3505 "dep": `":foo_sysprop_library"`,
3506 "min_sdk_version": `"5"`,
3507 }),
3508 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3509 "srcs": `["blah.cpp"]`,
3510 "local_includes": `["."]`,
3511 "min_sdk_version": `"5"`,
3512 "whole_archive_deps": `select({
3513 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3514 "//conditions:default": [],
3515 })`,
3516 }),
3517 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3518 "srcs": `["blah.cpp"]`,
3519 "local_includes": `["."]`,
3520 "min_sdk_version": `"5"`,
3521 "whole_archive_deps": `select({
3522 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
3523 "//conditions:default": [],
3524 })`,
3525 }),
3526 },
3527 })
3528}
Vinh Tran395a1e92022-09-16 18:27:29 -04003529
Sam Delmerico512437b2023-03-17 11:34:15 -04003530func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003531 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003532 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003533 ModuleTypeUnderTest: "cc_library",
3534 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003535 StubbedBuildDefinitions: []string{"bar-static", "baz-static", "bar-shared", "baz-shared"},
Vinh Tran395a1e92022-09-16 18:27:29 -04003536 Blueprint: `
3537cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003538 name: "foo",
3539 srcs: [
3540 "Foo.aidl",
3541 ],
3542 static_libs: [
3543 "bar-static",
3544 "baz-static",
3545 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003546 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003547 "bar-shared",
3548 "baz-shared",
3549 ],
3550 export_static_lib_headers: [
3551 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003552 ],
3553 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003554 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003555 ],
3556}` +
Chris Parsonscd209032023-09-19 01:12:48 +00003557 simpleModule("cc_library_static", "bar-static") +
3558 simpleModule("cc_library_static", "baz-static") +
3559 simpleModule("cc_library", "bar-shared") +
3560 simpleModule("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003561 ExpectedBazelTargets: []string{
3562 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3563 "srcs": `["Foo.aidl"]`,
3564 }),
3565 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003566 "local_includes": `["."]`,
3567 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003568 "implementation_deps": `[
3569 ":baz-static",
3570 ":bar-static",
3571 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003572 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003573 ":baz-shared",
3574 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003575 ]`,
3576 }),
3577 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3578 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003579 "deps": `[":baz-static"]`,
3580 "implementation_deps": `[":bar-static"]`,
3581 "dynamic_deps": `[":baz-shared"]`,
3582 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003583 "local_includes": `["."]`,
3584 }),
3585 },
3586 })
3587}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003588
3589func TestCcLibraryWithTidy(t *testing.T) {
3590 runCcLibraryTestCase(t, Bp2buildTestCase{
3591 Description: "cc_library uses tidy properties",
3592 ModuleTypeUnderTest: "cc_library",
3593 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3594 Blueprint: `
3595cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003596 name: "foo",
3597 srcs: ["foo.cpp"],
3598}
3599cc_library_static {
3600 name: "foo-no-tidy",
3601 srcs: ["foo.cpp"],
3602 tidy: false,
3603}
3604cc_library_static {
3605 name: "foo-tidy",
3606 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003607 tidy: true,
3608 tidy_checks: ["check1", "check2"],
3609 tidy_checks_as_errors: ["check1error", "check2error"],
3610 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003611 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003612}`,
3613 ExpectedBazelTargets: []string{
3614 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3615 "local_includes": `["."]`,
3616 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003617 }),
3618 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3619 "local_includes": `["."]`,
3620 "srcs": `["foo.cpp"]`,
3621 "tidy": `"never"`,
3622 }),
3623 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3624 "local_includes": `["."]`,
3625 "srcs": `["foo.cpp"]`,
3626 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003627 "tidy_checks": `[
3628 "check1",
3629 "check2",
3630 ]`,
3631 "tidy_checks_as_errors": `[
3632 "check1error",
3633 "check2error",
3634 ]`,
3635 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003636 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003637 }),
3638 },
3639 })
3640}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003641
Vinh Tran99270ea2022-11-28 11:15:23 -05003642func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3643 bp := `
3644cc_library {
3645 name: "foo",
3646 afdo: true,
3647 include_build_directory: false,
3648}`
3649
3650 // TODO(b/260714900): Add test case for arch-specific afdo profile
3651 testCases := []struct {
3652 description string
3653 filesystem map[string]string
3654 expectedBazelTargets []string
3655 }{
3656 {
3657 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003658 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003659 "vendor/google_data/pgo_profile/sampling/Android.bp": "",
3660 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003661 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003662 expectedBazelTargets: []string{
3663 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3664 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3665 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3666 }),
3667 },
3668 },
3669 {
3670 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003671 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003672 "toolchain/pgo-profiles/sampling/Android.bp": "",
3673 "toolchain/pgo-profiles/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003674 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003675 expectedBazelTargets: []string{
3676 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3677 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3678 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3679 }),
3680 },
3681 },
3682 {
3683 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003684 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003685 "toolchain/pgo-profiles/sampling/Android.bp": "",
3686 "toolchain/pgo-profiles/sampling/bar.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003687 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003688 expectedBazelTargets: []string{
3689 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3690 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3691 },
3692 },
3693 {
3694 description: "cc_library with afdo enabled but profile doesn't exist",
3695 expectedBazelTargets: []string{
3696 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3697 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3698 },
3699 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003700 {
3701 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3702 filesystem: map[string]string{
3703 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3704 },
3705 expectedBazelTargets: []string{
3706 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3707 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3708 },
3709 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003710 }
3711 for _, testCase := range testCases {
3712 t.Run(testCase.description, func(t *testing.T) {
3713 runCcLibraryTestCase(t, Bp2buildTestCase{
3714 ExpectedBazelTargets: testCase.expectedBazelTargets,
3715 ModuleTypeUnderTest: "cc_library",
3716 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3717 Description: testCase.description,
3718 Blueprint: binaryReplacer.Replace(bp),
3719 Filesystem: testCase.filesystem,
3720 })
3721 })
3722 }
3723}
3724
Yu Liu56ccb1a2022-11-12 10:47:07 -08003725func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3726 runCcLibraryTestCase(t, Bp2buildTestCase{
3727 Description: "cc_library with header abi checker",
3728 ModuleTypeUnderTest: "cc_library",
3729 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3730 Blueprint: `cc_library {
3731 name: "foo",
3732 header_abi_checker: {
3733 enabled: true,
3734 symbol_file: "a.map.txt",
3735 exclude_symbol_versions: [
3736 "29",
3737 "30",
3738 ],
3739 exclude_symbol_tags: [
3740 "tag1",
3741 "tag2",
3742 ],
3743 check_all_apis: true,
3744 diff_flags: ["-allow-adding-removing-weak-symbols"],
3745 },
3746 include_build_directory: false,
3747}`,
3748 ExpectedBazelTargets: []string{
3749 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3750 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3751 "abi_checker_enabled": `True`,
3752 "abi_checker_symbol_file": `"a.map.txt"`,
3753 "abi_checker_exclude_symbol_versions": `[
3754 "29",
3755 "30",
3756 ]`,
3757 "abi_checker_exclude_symbol_tags": `[
3758 "tag1",
3759 "tag2",
3760 ]`,
3761 "abi_checker_check_all_apis": `True`,
3762 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3763 }),
3764 },
3765 })
3766}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003767
3768func TestCcLibraryApexAvailable(t *testing.T) {
3769 runCcLibraryTestCase(t, Bp2buildTestCase{
3770 Description: "cc_library apex_available converted to tags",
3771 ModuleTypeUnderTest: "cc_library",
3772 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3773 Blueprint: soongCcLibraryPreamble + `
3774cc_library {
3775 name: "a",
3776 srcs: ["a.cpp"],
3777 apex_available: ["com.android.foo"],
3778}
3779`,
3780 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3781 "tags": `["apex_available=com.android.foo"]`,
3782 "srcs": `["a.cpp"]`,
3783 "local_includes": `["."]`,
3784 }),
3785 },
3786 )
3787}
3788
3789func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3790 runCcLibraryTestCase(t, Bp2buildTestCase{
3791 Description: "cc_library apex_available converted to multiple tags",
3792 ModuleTypeUnderTest: "cc_library",
3793 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3794 Blueprint: soongCcLibraryPreamble + `
3795cc_library {
3796 name: "a",
3797 srcs: ["a.cpp"],
3798 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3799}
3800`,
3801 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3802 "tags": `[
3803 "apex_available=com.android.foo",
3804 "apex_available=//apex_available:platform",
3805 "apex_available=com.android.bar",
3806 ]`,
3807 "srcs": `["a.cpp"]`,
3808 "local_includes": `["."]`,
3809 }),
3810 },
3811 )
3812}
Zi Wang0f828442022-12-28 11:18:11 -08003813
3814// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3815// In bp2build output, variant info(select) should go before general info.
3816// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3817func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3818 runCcLibraryTestCase(t, Bp2buildTestCase{
3819 Description: "cc_library variant prepend properties order",
3820 ModuleTypeUnderTest: "cc_library",
3821 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3822 Blueprint: soongCcLibraryPreamble + `
3823cc_library {
3824 name: "a",
3825 srcs: ["a.cpp"],
3826 export_include_dirs: ["eid1", "eid2"],
3827 export_system_include_dirs: ["esid1", "esid2"],
3828 target: {
3829 android: {
3830 export_include_dirs: ["android_eid1", "android_eid2"],
3831 export_system_include_dirs: ["android_esid1", "android_esid2"],
3832 },
3833 android_arm: {
3834 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3835 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3836 },
3837 linux: {
3838 export_include_dirs: ["linux_eid1", "linux_eid2"],
3839 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3840 },
3841 },
3842 multilib: {
3843 lib32: {
3844 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3845 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3846 },
3847 },
3848 arch: {
3849 arm: {
3850 export_include_dirs: ["arm_eid1", "arm_eid2"],
3851 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3852 },
3853 }
3854}
3855`,
3856 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3857 "export_includes": `select({
3858 "//build/bazel/platforms/os_arch:android_arm": [
3859 "android_arm_eid1",
3860 "android_arm_eid2",
3861 ],
3862 "//conditions:default": [],
3863 }) + select({
3864 "//build/bazel/platforms/os:android": [
3865 "android_eid1",
3866 "android_eid2",
3867 "linux_eid1",
3868 "linux_eid2",
3869 ],
3870 "//build/bazel/platforms/os:linux_bionic": [
3871 "linux_eid1",
3872 "linux_eid2",
3873 ],
3874 "//build/bazel/platforms/os:linux_glibc": [
3875 "linux_eid1",
3876 "linux_eid2",
3877 ],
3878 "//build/bazel/platforms/os:linux_musl": [
3879 "linux_eid1",
3880 "linux_eid2",
3881 ],
3882 "//conditions:default": [],
3883 }) + select({
3884 "//build/bazel/platforms/arch:arm": [
3885 "lib32_eid1",
3886 "lib32_eid2",
3887 "arm_eid1",
3888 "arm_eid2",
3889 ],
3890 "//build/bazel/platforms/arch:x86": [
3891 "lib32_eid1",
3892 "lib32_eid2",
3893 ],
3894 "//conditions:default": [],
3895 }) + [
3896 "eid1",
3897 "eid2",
3898 ]`,
3899 "export_system_includes": `select({
3900 "//build/bazel/platforms/os_arch:android_arm": [
3901 "android_arm_esid1",
3902 "android_arm_esid2",
3903 ],
3904 "//conditions:default": [],
3905 }) + select({
3906 "//build/bazel/platforms/os:android": [
3907 "android_esid1",
3908 "android_esid2",
3909 "linux_esid1",
3910 "linux_esid2",
3911 ],
3912 "//build/bazel/platforms/os:linux_bionic": [
3913 "linux_esid1",
3914 "linux_esid2",
3915 ],
3916 "//build/bazel/platforms/os:linux_glibc": [
3917 "linux_esid1",
3918 "linux_esid2",
3919 ],
3920 "//build/bazel/platforms/os:linux_musl": [
3921 "linux_esid1",
3922 "linux_esid2",
3923 ],
3924 "//conditions:default": [],
3925 }) + select({
3926 "//build/bazel/platforms/arch:arm": [
3927 "lib32_esid1",
3928 "lib32_esid2",
3929 "arm_esid1",
3930 "arm_esid2",
3931 ],
3932 "//build/bazel/platforms/arch:x86": [
3933 "lib32_esid1",
3934 "lib32_esid2",
3935 ],
3936 "//conditions:default": [],
3937 }) + [
3938 "esid1",
3939 "esid2",
3940 ]`,
3941 "srcs": `["a.cpp"]`,
3942 "local_includes": `["."]`,
3943 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
3944 }),
3945 },
3946 )
3947}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00003948
3949func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
3950 runCcLibraryTestCase(t, Bp2buildTestCase{
3951 Description: "cc_library has correct features when integer_overflow property is provided",
3952 ModuleTypeUnderTest: "cc_library",
3953 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3954 Blueprint: `
3955cc_library {
3956 name: "foo",
3957 sanitize: {
3958 integer_overflow: true,
3959 },
3960}
3961`,
3962 ExpectedBazelTargets: []string{
3963 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3964 "features": `["ubsan_integer_overflow"]`,
3965 "local_includes": `["."]`,
3966 }),
3967 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3968 "features": `["ubsan_integer_overflow"]`,
3969 "local_includes": `["."]`,
3970 }),
3971 },
3972 })
3973}
3974
3975func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
3976 runCcLibraryTestCase(t, Bp2buildTestCase{
3977 Description: "cc_library has correct features when misc_undefined property is provided",
3978 ModuleTypeUnderTest: "cc_library",
3979 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3980 Blueprint: `
3981cc_library {
3982 name: "foo",
3983 sanitize: {
3984 misc_undefined: ["undefined", "nullability"],
3985 },
3986}
3987`,
3988 ExpectedBazelTargets: []string{
3989 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3990 "features": `[
3991 "ubsan_undefined",
3992 "ubsan_nullability",
3993 ]`,
3994 "local_includes": `["."]`,
3995 }),
3996 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3997 "features": `[
3998 "ubsan_undefined",
3999 "ubsan_nullability",
4000 ]`,
4001 "local_includes": `["."]`,
4002 }),
4003 },
4004 })
4005}
4006
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004007func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
4008 runCcLibraryTestCase(t, Bp2buildTestCase{
4009 Description: "cc_library has correct feature when sanitize.blocklist is provided",
4010 ModuleTypeUnderTest: "cc_library",
4011 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4012 Blueprint: `
4013cc_library {
4014 name: "foo",
4015 sanitize: {
4016 blocklist: "foo_blocklist.txt",
4017 },
4018}
4019`,
4020 ExpectedBazelTargets: []string{
4021 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004022 "copts": `select({
4023 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4024 "//conditions:default": [],
4025 })`,
4026 "additional_compiler_inputs": `select({
4027 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4028 "//conditions:default": [],
4029 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004030 "local_includes": `["."]`,
4031 }),
4032 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004033 "copts": `select({
4034 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4035 "//conditions:default": [],
4036 })`,
4037 "additional_compiler_inputs": `select({
4038 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4039 "//conditions:default": [],
4040 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004041 "local_includes": `["."]`,
4042 }),
4043 },
4044 })
4045}
4046
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004047func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4048 runCcLibraryTestCase(t, Bp2buildTestCase{
4049 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4050 ModuleTypeUnderTest: "cc_library",
4051 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4052 Blueprint: `
4053cc_library {
4054 name: "foo",
4055 sanitize: {
4056 misc_undefined: ["undefined", "nullability"],
4057 },
4058 target: {
4059 android: {
4060 sanitize: {
4061 misc_undefined: ["alignment"],
4062 },
4063 },
4064 linux_glibc: {
4065 sanitize: {
4066 integer_overflow: true,
4067 },
4068 },
4069 },
4070}
4071`,
4072 ExpectedBazelTargets: []string{
4073 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4074 "features": `[
4075 "ubsan_undefined",
4076 "ubsan_nullability",
4077 ] + select({
4078 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4079 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4080 "//conditions:default": [],
4081 })`,
4082 "local_includes": `["."]`,
4083 }),
4084 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4085 "features": `[
4086 "ubsan_undefined",
4087 "ubsan_nullability",
4088 ] + select({
4089 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
4090 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
4091 "//conditions:default": [],
4092 })`,
4093 "local_includes": `["."]`,
4094 }),
4095 },
4096 })
4097}
Yu Liu10174ff2023-02-21 12:05:26 -08004098
4099func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4100 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4101 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4102 ModuleTypeUnderTest: "cc_library",
4103 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004104 StubbedBuildDefinitions: []string{"barlib", "bazlib"},
Yu Liu10174ff2023-02-21 12:05:26 -08004105 Blueprint: `
4106cc_library {
4107 name: "barlib",
4108 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004109 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004110}
4111cc_library {
4112 name: "bazlib",
4113 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004114 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004115}
4116cc_library {
4117 name: "foo",
4118 shared_libs: ["barlib", "bazlib"],
4119 export_shared_lib_headers: ["bazlib"],
4120 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004121 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004122 ],
4123}`,
4124 ExpectedBazelTargets: []string{
4125 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004126 "implementation_dynamic_deps": `[":barlib"]`,
4127 "dynamic_deps": `[":bazlib"]`,
4128 "local_includes": `["."]`,
4129 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004130 }),
4131 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004132 "implementation_dynamic_deps": `[":barlib"]`,
4133 "dynamic_deps": `[":bazlib"]`,
4134 "local_includes": `["."]`,
4135 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004136 }),
4137 },
4138 })
4139}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004140
4141func TestCcLibraryWithThinLto(t *testing.T) {
4142 runCcLibraryTestCase(t, Bp2buildTestCase{
4143 Description: "cc_library has correct features when thin LTO is enabled",
4144 ModuleTypeUnderTest: "cc_library",
4145 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4146 Blueprint: `
4147cc_library {
4148 name: "foo",
4149 lto: {
4150 thin: true,
4151 },
4152}`,
4153 ExpectedBazelTargets: []string{
4154 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4155 "features": `["android_thin_lto"]`,
4156 "local_includes": `["."]`,
4157 }),
4158 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4159 "features": `["android_thin_lto"]`,
4160 "local_includes": `["."]`,
4161 }),
4162 },
4163 })
4164}
4165
4166func TestCcLibraryWithLtoNever(t *testing.T) {
4167 runCcLibraryTestCase(t, Bp2buildTestCase{
4168 Description: "cc_library has correct features when LTO is explicitly disabled",
4169 ModuleTypeUnderTest: "cc_library",
4170 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4171 Blueprint: `
4172cc_library {
4173 name: "foo",
4174 lto: {
4175 never: true,
4176 },
4177}`,
4178 ExpectedBazelTargets: []string{
4179 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4180 "features": `["-android_thin_lto"]`,
4181 "local_includes": `["."]`,
4182 }),
4183 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4184 "features": `["-android_thin_lto"]`,
4185 "local_includes": `["."]`,
4186 }),
4187 },
4188 })
4189}
4190
4191func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4192 runCcLibraryTestCase(t, Bp2buildTestCase{
4193 Description: "cc_library has correct features when LTO differs across arch and os variants",
4194 ModuleTypeUnderTest: "cc_library",
4195 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4196 Blueprint: `
4197cc_library {
4198 name: "foo",
4199 target: {
4200 android: {
4201 lto: {
4202 thin: true,
4203 },
4204 },
4205 },
4206 arch: {
4207 riscv64: {
4208 lto: {
4209 thin: false,
4210 },
4211 },
4212 },
4213}`,
4214 ExpectedBazelTargets: []string{
4215 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4216 "local_includes": `["."]`,
4217 "features": `select({
4218 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4219 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4220 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4221 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4222 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4223 "//conditions:default": [],
4224 })`}),
4225 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4226 "local_includes": `["."]`,
4227 "features": `select({
4228 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
4229 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
4230 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4231 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
4232 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
4233 "//conditions:default": [],
4234 })`}),
4235 },
4236 })
4237}
4238
4239func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4240 runCcLibraryTestCase(t, Bp2buildTestCase{
4241 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4242 ModuleTypeUnderTest: "cc_library",
4243 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4244 Blueprint: `
4245cc_library {
4246 name: "foo",
4247 lto: {
4248 never: true,
4249 },
4250 target: {
4251 android: {
4252 lto: {
4253 thin: true,
4254 never: false,
4255 },
4256 },
4257 },
4258}`,
4259 ExpectedBazelTargets: []string{
4260 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4261 "local_includes": `["."]`,
4262 "features": `select({
4263 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4264 "//conditions:default": ["-android_thin_lto"],
4265 })`,
4266 }),
4267 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4268 "local_includes": `["."]`,
4269 "features": `select({
4270 "//build/bazel/platforms/os:android": ["android_thin_lto"],
4271 "//conditions:default": ["-android_thin_lto"],
4272 })`,
4273 }),
4274 },
4275 })
4276}
4277
4278func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4279 runCcLibraryTestCase(t, Bp2buildTestCase{
4280 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4281 ModuleTypeUnderTest: "cc_library",
4282 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4283 Blueprint: `
4284cc_library {
4285 name: "foo",
4286 lto: {
4287 thin: true,
4288 },
4289 whole_program_vtables: true,
4290}`,
4291 ExpectedBazelTargets: []string{
4292 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4293 "features": `[
4294 "android_thin_lto",
4295 "android_thin_lto_whole_program_vtables",
4296 ]`,
4297 "local_includes": `["."]`,
4298 }),
4299 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4300 "features": `[
4301 "android_thin_lto",
4302 "android_thin_lto_whole_program_vtables",
4303 ]`,
4304 "local_includes": `["."]`,
4305 }),
4306 },
4307 })
4308}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004309
4310func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4311 runCcLibraryTestCase(t, Bp2buildTestCase{
4312 Description: "cc_library changes hidden visibility flag to feature",
4313 ModuleTypeUnderTest: "cc_library",
4314 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4315 Blueprint: `
4316cc_library {
4317 name: "foo",
4318 cflags: ["-fvisibility=hidden"],
4319}`,
4320 ExpectedBazelTargets: []string{
4321 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4322 "features": `["visibility_hidden"]`,
4323 "local_includes": `["."]`,
4324 }),
4325 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4326 "features": `["visibility_hidden"]`,
4327 "local_includes": `["."]`,
4328 }),
4329 },
4330 })
4331}
4332
4333func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4334 runCcLibraryTestCase(t, Bp2buildTestCase{
4335 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4336 ModuleTypeUnderTest: "cc_library",
4337 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4338 Blueprint: `
4339cc_library {
4340 name: "foo",
4341 shared: {
4342 cflags: ["-fvisibility=hidden"],
4343 },
4344}`,
4345 ExpectedBazelTargets: []string{
4346 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4347 "local_includes": `["."]`,
4348 }),
4349 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4350 "features": `["visibility_hidden"]`,
4351 "local_includes": `["."]`,
4352 }),
4353 },
4354 })
4355}
4356
4357func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4358 runCcLibraryTestCase(t, Bp2buildTestCase{
4359 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4360 ModuleTypeUnderTest: "cc_library",
4361 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4362 Blueprint: `
4363cc_library {
4364 name: "foo",
4365 static: {
4366 cflags: ["-fvisibility=hidden"],
4367 },
4368}`,
4369 ExpectedBazelTargets: []string{
4370 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4371 "features": `["visibility_hidden"]`,
4372 "local_includes": `["."]`,
4373 }),
4374 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4375 "local_includes": `["."]`,
4376 }),
4377 },
4378 })
4379}
4380
4381func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4382 runCcLibraryTestCase(t, Bp2buildTestCase{
4383 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4384 ModuleTypeUnderTest: "cc_library",
4385 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4386 Blueprint: `
4387cc_library {
4388 name: "foo",
4389 target: {
4390 android: {
4391 cflags: ["-fvisibility=hidden"],
4392 },
4393 },
4394}`,
4395 ExpectedBazelTargets: []string{
4396 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4397 "features": `select({
4398 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4399 "//conditions:default": [],
4400 })`,
4401 "local_includes": `["."]`,
4402 }),
4403 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4404 "features": `select({
4405 "//build/bazel/platforms/os:android": ["visibility_hidden"],
4406 "//conditions:default": [],
4407 })`,
4408 "local_includes": `["."]`,
4409 }),
4410 },
4411 })
4412}
Spandan Das4242f102023-04-19 22:31:54 +00004413
4414// Test that a config_setting specific to an apex is created by cc_library.
4415func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4416 runCcLibraryTestCase(t, Bp2buildTestCase{
4417 Description: "cc_library creates a config_setting for each apex in apex_available",
4418 ModuleTypeUnderTest: "cc_library",
4419 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4420 Dir: "build/bazel/rules/apex",
4421 Blueprint: `
4422cc_library {
4423 name: "foo",
4424 apex_available: [
4425 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4426 "myapex"
4427 ],
4428}`,
4429 ExpectedBazelTargets: []string{
4430 MakeBazelTargetNoRestrictions(
4431 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004432 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004433 AttrNameToString{
4434 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004435 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004436 }`,
Spandan Das9cad90f2023-05-04 17:15:44 +00004437 "constraint_values": `["//build/bazel/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004438 },
4439 ),
4440 },
4441 })
4442}
Yu Liu93893ba2023-05-01 13:49:52 -07004443
4444func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4445 runCcLibraryTestCase(t, Bp2buildTestCase{
4446 Description: "cc_library cppflags in product variables",
4447 ModuleTypeUnderTest: "cc_library",
4448 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4449 Blueprint: soongCcLibraryPreamble + `cc_library {
4450 name: "a",
4451 srcs: ["a.cpp"],
4452 cppflags: [
4453 "-Wextra",
4454 "-DDEBUG_ONLY_CODE=0",
4455 ],
4456 product_variables: {
4457 eng: {
4458 cppflags: [
4459 "-UDEBUG_ONLY_CODE",
4460 "-DDEBUG_ONLY_CODE=1",
4461 ],
4462 },
4463 },
4464 include_build_directory: false,
4465}
4466`,
4467 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4468 "cppflags": `[
4469 "-Wextra",
4470 "-DDEBUG_ONLY_CODE=0",
4471 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004472 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004473 "-UDEBUG_ONLY_CODE",
4474 "-DDEBUG_ONLY_CODE=1",
4475 ],
4476 "//conditions:default": [],
4477 })`,
4478 "srcs": `["a.cpp"]`,
4479 }),
4480 },
4481 )
4482}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004483
4484func TestCcLibraryYaccConversion(t *testing.T) {
4485 runCcLibraryTestCase(t, Bp2buildTestCase{
4486 Description: "cc_library is built from .y/.yy files",
4487 ModuleTypeUnderTest: "cc_library",
4488 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004489 StubbedBuildDefinitions: []string{"staticlib", "sharedlib"},
Spandan Dasdf4c2132023-05-09 23:58:52 +00004490 Blueprint: soongCcLibraryPreamble + `cc_library {
4491 name: "a",
4492 srcs: [
4493 "a.cpp",
4494 "a.yy",
4495 ],
4496 shared_libs: ["sharedlib"],
4497 static_libs: ["staticlib"],
4498 yacc: {
4499 flags: ["someYaccFlag"],
4500 gen_location_hh: true,
4501 gen_position_hh: true,
4502 },
4503}
4504cc_library_static {
4505 name: "staticlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004506}
4507cc_library {
4508 name: "sharedlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004509}
4510`,
4511 ExpectedBazelTargets: []string{
4512 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4513 "src": `"a.yy"`,
4514 "implementation_deps": `[":staticlib"]`,
4515 "implementation_dynamic_deps": `[":sharedlib"]`,
4516 "flags": `["someYaccFlag"]`,
4517 "gen_location_hh": "True",
4518 "gen_position_hh": "True",
4519 "local_includes": `["."]`,
4520 }),
4521 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4522 "srcs": `["a.cpp"]`,
4523 "implementation_deps": `[":staticlib"]`,
4524 "implementation_dynamic_deps": `[":sharedlib"]`,
4525 "implementation_whole_archive_deps": `[":a_yacc"]`,
4526 "local_includes": `["."]`,
4527 }),
4528 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4529 "srcs": `["a.cpp"]`,
4530 "implementation_deps": `[":staticlib"]`,
4531 "implementation_dynamic_deps": `[":sharedlib"]`,
4532 "implementation_whole_archive_deps": `[":a_yacc"]`,
4533 "local_includes": `["."]`,
4534 }),
4535 },
4536 })
4537}
Spandan Dasfb04c412023-05-15 18:35:36 +00004538
4539func TestCcLibraryHostLdLibs(t *testing.T) {
4540 runCcLibraryTestCase(t, Bp2buildTestCase{
4541 Description: "cc_binary linker flags for host_ldlibs",
4542 ModuleTypeUnderTest: "cc_binary",
4543 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4544 Blueprint: soongCcLibraryPreamble + `cc_binary {
4545 name: "a",
4546 host_supported: true,
4547 ldflags: ["-lcommon"],
4548 target: {
4549 linux: {
4550 host_ldlibs: [
4551 "-llinux",
4552 ],
4553 },
4554 darwin: {
4555 ldflags: ["-ldarwinadditional"],
4556 host_ldlibs: [
4557 "-ldarwin",
4558 ],
4559 },
4560 windows: {
4561 host_ldlibs: [
4562 "-lwindows",
4563 ],
4564 },
4565 },
4566}
4567`,
4568 ExpectedBazelTargets: []string{
4569 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4570 "linkopts": `["-lcommon"] + select({
4571 "//build/bazel/platforms/os:darwin": [
4572 "-ldarwinadditional",
4573 "-ldarwin",
4574 ],
4575 "//build/bazel/platforms/os:linux_glibc": ["-llinux"],
4576 "//build/bazel/platforms/os:windows": ["-lwindows"],
4577 "//conditions:default": [],
4578 })`,
4579 "local_includes": `["."]`,
4580 }),
4581 },
4582 })
4583}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004584
4585func TestCcLibraryWithCfi(t *testing.T) {
4586 runCcLibraryTestCase(t, Bp2buildTestCase{
4587 Description: "cc_library has correct features when cfi is enabled",
4588 ModuleTypeUnderTest: "cc_library",
4589 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4590 Blueprint: `
4591cc_library {
4592 name: "foo",
4593 sanitize: {
4594 cfi: true,
4595 },
4596}`,
4597 ExpectedBazelTargets: []string{
4598 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4599 "features": `["android_cfi"]`,
4600 "local_includes": `["."]`,
4601 }),
4602 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4603 "features": `["android_cfi"]`,
4604 "local_includes": `["."]`,
4605 }),
4606 },
4607 })
4608}
4609
4610func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4611 runCcLibraryTestCase(t, Bp2buildTestCase{
4612 Description: "cc_library has correct features when cfi is enabled for specific variants",
4613 ModuleTypeUnderTest: "cc_library",
4614 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4615 Blueprint: `
4616cc_library {
4617 name: "foo",
4618 target: {
4619 android: {
4620 sanitize: {
4621 cfi: true,
4622 },
4623 },
4624 },
4625}`,
4626 ExpectedBazelTargets: []string{
4627 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4628 "features": `select({
4629 "//build/bazel/platforms/os:android": ["android_cfi"],
4630 "//conditions:default": [],
4631 })`,
4632 "local_includes": `["."]`,
4633 }),
4634 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4635 "features": `select({
4636 "//build/bazel/platforms/os:android": ["android_cfi"],
4637 "//conditions:default": [],
4638 })`,
4639 "local_includes": `["."]`,
4640 }),
4641 },
4642 })
4643}
4644
4645func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4646 runCcLibraryTestCase(t, Bp2buildTestCase{
4647 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4648 ModuleTypeUnderTest: "cc_library",
4649 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4650 Blueprint: `
4651cc_library {
4652 name: "foo",
4653 sanitize: {
4654 cfi: true,
4655 config: {
4656 cfi_assembly_support: true,
4657 },
4658 },
4659}`,
4660 ExpectedBazelTargets: []string{
4661 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4662 "features": `[
4663 "android_cfi",
4664 "android_cfi_assembly_support",
4665 ]`,
4666 "local_includes": `["."]`,
4667 }),
4668 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4669 "features": `[
4670 "android_cfi",
4671 "android_cfi_assembly_support",
4672 ]`,
4673 "local_includes": `["."]`,
4674 }),
4675 },
4676 })
4677}
Spandan Das39ccf932023-05-26 18:03:39 +00004678
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004679func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4680 runCcLibraryTestCase(t, Bp2buildTestCase{
4681 Description: "cc_library disables cfi when explciitly set to false in the bp",
4682 ModuleTypeUnderTest: "cc_library",
4683 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4684 Blueprint: `
4685cc_library {
4686 name: "foo",
4687 sanitize: {
4688 cfi: false,
4689 },
4690}
4691`,
4692 ExpectedBazelTargets: []string{
4693 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4694 "features": `["-android_cfi"]`,
4695 "local_includes": `["."]`,
4696 }),
4697 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4698 "features": `["-android_cfi"]`,
4699 "local_includes": `["."]`,
4700 }),
4701 },
4702 })
4703}
4704
Spandan Das39ccf932023-05-26 18:03:39 +00004705func TestCcLibraryWithStem(t *testing.T) {
4706 runCcLibraryTestCase(t, Bp2buildTestCase{
4707 Description: "cc_library with stem property",
4708 ModuleTypeUnderTest: "cc_library_shared",
4709 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4710 Blueprint: soongCcLibraryPreamble + `
4711cc_library_shared {
4712 name: "foo_with_stem_simple",
4713 stem: "foo",
4714}
4715cc_library_shared {
4716 name: "foo_with_arch_variant_stem",
4717 arch: {
4718 arm: {
4719 stem: "foo-arm",
4720 },
4721 arm64: {
4722 stem: "foo-arm64",
4723 },
4724 },
4725}
4726`,
4727 ExpectedBazelTargets: []string{
4728 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4729 "stem": `"foo"`,
4730 "local_includes": `["."]`,
4731 }),
4732 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4733 "stem": `select({
4734 "//build/bazel/platforms/arch:arm": "foo-arm",
4735 "//build/bazel/platforms/arch:arm64": "foo-arm64",
4736 "//conditions:default": None,
4737 })`,
4738 "local_includes": `["."]`,
4739 }),
4740 },
4741 })
4742}
Spandan Dasc53767e2023-08-03 23:02:26 +00004743
4744// Bazel enforces that proto_library and the .proto file are in the same bazel package
4745func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4746 tc := Bp2buildTestCase{
4747 Description: "cc_library depends on .proto files from multiple packages",
4748 ModuleTypeUnderTest: "cc_library",
4749 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4750 Blueprint: `
4751cc_library_static {
4752 name: "foo",
4753 srcs: [
4754 "foo.proto",
4755 "bar/bar.proto", // Different package because there is a bar/Android.bp
4756 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4757 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004758 proto: {
4759 canonical_path_from_root: true,
4760 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004761}
Chris Parsonscd209032023-09-19 01:12:48 +00004762` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasc53767e2023-08-03 23:02:26 +00004763 Filesystem: map[string]string{
4764 "bar/Android.bp": "",
4765 "baz/subbaz/Android.bp": "",
4766 },
4767 }
4768
4769 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4770 // Root dir
4771 tc.ExpectedBazelTargets = []string{
4772 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4773 "local_includes": `["."]`,
4774 "deps": `[":libprotobuf-cpp-lite"]`,
4775 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4776 }),
4777 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4778 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004779 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004780 }),
4781 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4782 "deps": `[
4783 ":foo_proto",
4784 "//bar:foo_proto",
4785 "//baz/subbaz:foo_proto",
4786 ]`,
4787 }),
4788 }
4789 runCcLibraryTestCase(t, tc)
4790
4791 // bar dir
4792 tc.Dir = "bar"
4793 tc.ExpectedBazelTargets = []string{
4794 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004795 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004796 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004797 }),
4798 }
4799 runCcLibraryTestCase(t, tc)
4800
4801 // baz/subbaz dir
4802 tc.Dir = "baz/subbaz"
4803 tc.ExpectedBazelTargets = []string{
4804 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004805 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004806 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004807 }),
4808 }
4809 runCcLibraryTestCase(t, tc)
4810}
4811
4812// Bazel enforces that proto_library and the .proto file are in the same bazel package
4813func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
4814 tc := Bp2buildTestCase{
4815 Description: "cc_library depends on .proto files from multiple packages",
4816 ModuleTypeUnderTest: "cc_library",
4817 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4818 Blueprint: `
4819cc_library_static {
4820 name: "foo",
4821 srcs: [
4822 "foo.proto",
4823 "bar/bar.proto", // Different package because there is a bar/Android.bp
4824 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4825 ],
4826 proto: {
4827 canonical_path_from_root: false,
4828 }
4829}
Chris Parsonscd209032023-09-19 01:12:48 +00004830` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004831 Filesystem: map[string]string{
4832 "bar/Android.bp": "",
4833 "baz/subbaz/Android.bp": "",
4834 },
4835 }
4836
4837 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4838 // Root dir
4839 tc.ExpectedBazelTargets = []string{
4840 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4841 "local_includes": `["."]`,
4842 "deps": `[":libprotobuf-cpp-lite"]`,
4843 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4844 }),
4845 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4846 "srcs": `["foo.proto"]`,
4847 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004848 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004849 }),
4850 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4851 "deps": `[
4852 ":foo_proto",
4853 "//bar:foo_proto",
4854 "//baz/subbaz:foo_proto",
4855 ]`,
4856 }),
4857 }
4858 runCcLibraryTestCase(t, tc)
4859
4860 // bar dir
4861 tc.Dir = "bar"
4862 tc.ExpectedBazelTargets = []string{
4863 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4864 "srcs": `["//bar:bar.proto"]`,
4865 "strip_import_prefix": `""`,
4866 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00004867 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004868 }),
4869 }
4870 runCcLibraryTestCase(t, tc)
4871
4872 // baz/subbaz dir
4873 tc.Dir = "baz/subbaz"
4874 tc.ExpectedBazelTargets = []string{
4875 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4876 "srcs": `["//baz/subbaz:baz.proto"]`,
4877 "strip_import_prefix": `""`,
4878 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00004879 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004880 }),
4881 }
4882 runCcLibraryTestCase(t, tc)
4883}
Spandan Dasec39d512023-08-15 22:08:18 +00004884
4885func TestProtoIncludeDirs(t *testing.T) {
4886 tc := Bp2buildTestCase{
4887 Description: "cc_library depends on .proto files using proto.include_dirs",
4888 ModuleTypeUnderTest: "cc_library",
4889 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4890 Blueprint: `
4891cc_library_static {
4892 name: "foo",
4893 srcs: [
4894 "foo.proto",
4895 ],
4896 proto: {
4897 include_dirs: ["bar"],
4898 }
4899}
Chris Parsonscd209032023-09-19 01:12:48 +00004900` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasec39d512023-08-15 22:08:18 +00004901 Filesystem: map[string]string{
4902 "bar/Android.bp": "",
4903 "bar/bar.proto": "",
4904 "bar/baz/Android.bp": "",
4905 "bar/baz/baz.proto": "",
4906 },
4907 }
4908
4909 // We will run the test 3 times and check in the root, bar and bar/baz directories
4910 // Root dir
4911 tc.ExpectedBazelTargets = []string{
4912 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4913 "local_includes": `["."]`,
4914 "deps": `[":libprotobuf-cpp-lite"]`,
4915 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4916 }),
4917 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4918 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004919 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00004920 }),
4921 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4922 "deps": `[":foo_proto"]`,
4923 "transitive_deps": `[
4924 "//bar:bar.include_dir_bp2build_generated_proto",
4925 "//bar/baz:bar.include_dir_bp2build_generated_proto",
4926 ]`,
4927 }),
4928 }
4929 runCcLibraryTestCase(t, tc)
4930
4931 // bar dir
4932 tc.Dir = "bar"
4933 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004934 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004935 "srcs": `["bar.proto"]`,
4936 "strip_import_prefix": `""`,
4937 "tags": `["manual"]`,
4938 }),
4939 }
4940 runCcLibraryTestCase(t, tc)
4941
4942 // bar/baz dir
4943 tc.Dir = "bar/baz"
4944 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004945 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004946 "srcs": `["//bar/baz:baz.proto"]`,
4947 "strip_import_prefix": `""`,
4948 "import_prefix": `"baz"`,
4949 "tags": `["manual"]`,
4950 }),
4951 }
4952 runCcLibraryTestCase(t, tc)
4953}
Spandan Das4e5a1942023-08-22 19:20:39 +00004954
Spandan Dasf26ee152023-08-24 23:21:04 +00004955func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
4956 tc := Bp2buildTestCase{
4957 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
4958 ModuleTypeUnderTest: "cc_library",
4959 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4960 Blueprint: `
4961cc_library_static {
4962 name: "foo",
4963 srcs: [
4964 "foo.proto",
4965 "bar/bar.proto",
4966 ],
4967 proto: {
4968 include_dirs: ["baz"],
4969 }
4970}
Chris Parsonscd209032023-09-19 01:12:48 +00004971` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasf26ee152023-08-24 23:21:04 +00004972 Filesystem: map[string]string{
4973 "bar/Android.bp": "", // package boundary
4974 "baz/Android.bp": "",
4975 "baz/baz.proto": "",
4976 },
4977 }
4978
4979 tc.ExpectedBazelTargets = []string{
4980 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4981 "local_includes": `["."]`,
4982 "deps": `[":libprotobuf-cpp-lite"]`,
4983 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4984 }),
4985 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4986 "srcs": `["foo.proto"]`,
4987 "tags": `["manual"]`,
4988 }),
4989 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4990 "deps": `[
4991 ":foo_proto",
4992 "//bar:foo_proto",
4993 ]`,
4994 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
4995 }),
4996 }
4997 runCcLibraryTestCase(t, tc)
4998
4999}
5000
Spandan Das4e5a1942023-08-22 19:20:39 +00005001func TestProtoLocalIncludeDirs(t *testing.T) {
5002 tc := Bp2buildTestCase{
5003 Description: "cc_library depends on .proto files using proto.local_include_dirs",
5004 ModuleTypeUnderTest: "cc_library",
5005 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005006 Blueprint: `
5007cc_library {
5008 name: "libprotobuf-cpp-lite",
5009 // TODO: b/285631638 - A stubbed proto library dependency does not work as a protolib
5010 // dependency of cc_library_static.
5011 bazel_module: { bp2build_available: false },
5012}
5013`,
Spandan Das4e5a1942023-08-22 19:20:39 +00005014 Filesystem: map[string]string{
5015 "foo/Android.bp": `cc_library_static {
5016 name: "foo",
5017 srcs: [
5018 "foo.proto",
5019 ],
5020 proto: {
5021 local_include_dirs: ["foo_subdir"],
5022 },
5023 bazel_module: { bp2build_available: true },
5024}`,
5025 "foo/foo.proto": "",
5026 "foo/foo_subdir/Android.bp": "",
5027 "foo/foo_subdir/foo_subdir.proto": "",
5028 },
5029 }
5030
5031 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5032 // foo dir
5033 tc.Dir = "foo"
5034 tc.ExpectedBazelTargets = []string{
5035 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5036 "local_includes": `["."]`,
5037 "deps": `["//:libprotobuf-cpp-lite"]`,
5038 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5039 }),
5040 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5041 "srcs": `["foo.proto"]`,
5042 "tags": `["manual"]`,
5043 }),
5044 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5045 "deps": `[":foo_proto"]`,
5046 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5047 }),
5048 }
5049 runCcLibraryTestCase(t, tc)
5050
5051 // foo/foo_subdir
5052 tc.Dir = "foo/foo_subdir"
5053 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005054 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005055 "srcs": `["foo_subdir.proto"]`,
5056 "strip_import_prefix": `""`,
5057 "tags": `["manual"]`,
5058 }),
5059 }
5060 runCcLibraryTestCase(t, tc)
5061}
Spandan Dasab29f572023-09-01 19:43:56 +00005062
5063// `foo_device` and `bar_host` can depend on .proto files of a specific dir,
5064// the dynamically generated proto_library should not have any target_compatible_with
5065func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) {
5066 tc := Bp2buildTestCase{
5067 Description: "proto_library generated for proto.include_dirs is compatible for all axes",
5068 ModuleTypeUnderTest: "cc_library",
5069 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005070 Blueprint: simpleModule("cc_library", "libprotobuf-cpp-lite") + `
Spandan Dasab29f572023-09-01 19:43:56 +00005071cc_library {
5072 name: "foo_device",
5073 device_supported: true, // this is the default behavior, but added explicitly here for illustration
5074 host_supported: false,
5075 proto: {include_dirs: ["dir"]},
5076}
5077cc_library {
5078 name: "bar_host",
5079 device_supported: false,
5080 host_supported: true,
5081 srcs: ["bar.proto"],
5082 proto: {include_dirs: ["dir"]},
5083}
5084`,
5085 Filesystem: map[string]string{
5086 "dir/Android.bp": "",
5087 "dir/dir.proto": "",
5088 },
5089 Dir: "dir", // check for the generated proto_library
5090 ExpectedBazelTargets: []string{
5091 MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{
5092 "srcs": `["dir.proto"]`,
5093 "strip_import_prefix": `""`,
5094 "tags": `["manual"]`,
5095 }),
5096 },
5097 }
5098 runCcLibraryTestCase(t, tc)
5099}
Spandan Dase1cb14b2023-09-05 19:31:12 +00005100
5101func TestCcCompileMultilibConversion(t *testing.T) {
5102 tc := Bp2buildTestCase{
5103 Description: "cc_library with compile_multilib",
5104 ModuleTypeUnderTest: "cc_library",
5105 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5106 Blueprint: `
5107cc_library {
5108 name: "lib32",
5109 compile_multilib: "32",
5110}
5111cc_library {
5112 name: "lib64",
5113 compile_multilib: "64",
5114}
5115`,
5116 ExpectedBazelTargets: []string{
5117 MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{
5118 "local_includes": `["."]`,
5119 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5120 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5121 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5122 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5123 "//conditions:default": [],
5124 })`,
5125 }),
5126 MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{
5127 "local_includes": `["."]`,
5128 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5129 "//build/bazel/platforms/arch:arm64": ["@platforms//:incompatible"],
5130 "//build/bazel/platforms/arch:riscv64": ["@platforms//:incompatible"],
5131 "//build/bazel/platforms/arch:x86_64": ["@platforms//:incompatible"],
5132 "//conditions:default": [],
5133 })`,
5134 }),
5135 MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{
5136 "local_includes": `["."]`,
5137 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5138 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5139 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5140 "//conditions:default": [],
5141 })`,
5142 }),
5143 MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{
5144 "local_includes": `["."]`,
5145 "target_compatible_with": `["//build/bazel/platforms/os:android"] + select({
5146 "//build/bazel/platforms/arch:arm": ["@platforms//:incompatible"],
5147 "//build/bazel/platforms/arch:x86": ["@platforms//:incompatible"],
5148 "//conditions:default": [],
5149 })`,
5150 }),
5151 },
5152 }
5153 runCcLibraryTestCase(t, tc)
5154}
Spandan Das63acae92023-09-14 22:34:34 +00005155
5156func TestNdkLibraryConversion(t *testing.T) {
5157 tc := Bp2buildTestCase{
5158 Description: "ndk_library conversion",
5159 ModuleTypeUnderTest: "cc_library",
5160 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5161 Blueprint: `
5162cc_library {
5163 name: "libfoo",
Spandan Das63acae92023-09-14 22:34:34 +00005164}
5165ndk_library {
5166 name: "libfoo",
5167 first_version: "29",
5168 symbol_file: "libfoo.map.txt",
5169}
5170`,
Chris Parsons8a532b72023-09-27 23:11:26 +00005171 StubbedBuildDefinitions: []string{"libfoo"},
Spandan Das63acae92023-09-14 22:34:34 +00005172 ExpectedBazelTargets: []string{
5173 MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{
5174 "api_surface": `"publicapi"`,
5175 "soname": `"libfoo.so"`,
5176 "source_library_label": `"//:libfoo"`,
5177 "symbol_file": `"libfoo.map.txt"`,
5178 "versions": `[
5179 "29",
5180 "30",
5181 "S",
5182 "Tiramisu",
5183 "current",
5184 ]`,
5185 }),
5186 },
5187 }
5188 runCcLibraryTestCase(t, tc)
5189}
Spandan Das319711b2023-09-19 19:04:41 +00005190
5191func TestNdkHeadersConversion(t *testing.T) {
5192 tc := Bp2buildTestCase{
5193 Description: "ndk_headers conversion",
5194 ModuleTypeUnderTest: "ndk_headers",
5195 ModuleTypeUnderTestFactory: cc.NdkHeadersFactory,
5196 Blueprint: `
5197ndk_headers {
5198 name: "libfoo_headers",
5199 from: "from",
5200 to: "to",
Spandan Dasa7da3f02023-09-28 19:30:51 +00005201 srcs: ["from/foo.h", "from/foo_other.h"]
Spandan Das319711b2023-09-19 19:04:41 +00005202}
5203`,
5204 ExpectedBazelTargets: []string{
5205 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5206 "strip_import_prefix": `"from"`,
5207 "import_prefix": `"to"`,
5208 "hdrs": `[
Spandan Dasa7da3f02023-09-28 19:30:51 +00005209 "from/foo.h",
5210 "from/foo_other.h",
Spandan Das319711b2023-09-19 19:04:41 +00005211 ]`,
5212 }),
5213 },
5214 }
5215 runCcLibraryTestCase(t, tc)
5216}
Spandan Dasa7da3f02023-09-28 19:30:51 +00005217
5218func TestVersionedNdkHeadersConversion(t *testing.T) {
5219 tc := Bp2buildTestCase{
5220 Description: "versioned_ndk_headers conversion",
5221 ModuleTypeUnderTest: "versioned_ndk_headers",
5222 ModuleTypeUnderTestFactory: cc.VersionedNdkHeadersFactory,
5223 Blueprint: `
5224versioned_ndk_headers {
5225 name: "libfoo_headers",
5226 from: "from",
5227 to: "to",
5228}
5229`,
5230 Filesystem: map[string]string{
5231 "from/foo.h": "",
5232 "from/foo_other.h": "",
5233 },
5234 ExpectedBazelTargets: []string{
5235 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5236 "strip_import_prefix": `"from"`,
5237 "import_prefix": `"to"`,
5238 "hdrs": `[
5239 "from/foo.h",
5240 "from/foo_other.h",
5241 ]`,
5242 "run_versioner": "True",
5243 }),
5244 },
5245 }
5246 runCcLibraryTestCase(t, tc)
5247}
Chris Parsons7123cc52023-10-04 22:27:40 +00005248
5249// Regression test for b/303307456.
5250// TODO: b/202299295 - Remove this test when cc rules have proper support
5251// for the `required` property
5252func TestCcModules_requiredProperty(t *testing.T) {
5253 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
5254 Description: "cc modules do not use the required property",
5255 Filesystem: map[string]string{
5256 "foo.c": "",
5257 "bar.c": "",
5258 },
5259 Blueprint: soongCcLibraryPreamble + `
5260cc_library {
5261 name: "foo_both",
5262 srcs: ["foo.c"],
5263 include_build_directory: false,
5264 required: ["bar"],
5265}
5266cc_library_shared {
5267 name: "foo_shared",
5268 srcs: ["foo.c"],
5269 include_build_directory: false,
5270 required: ["bar"],
5271}
5272cc_library_static {
5273 name: "foo_static",
5274 srcs: ["foo.c"],
5275 include_build_directory: false,
5276 required: ["bar"],
5277}
5278cc_library_static {
5279 name: "bar",
5280 srcs: ["bar.c"],
5281 include_build_directory: false,
5282}`,
5283 ExpectedBazelTargets: []string{
5284 MakeBazelTarget("cc_library_static", "foo_both_bp2build_cc_library_static", AttrNameToString{
5285 "srcs_c": `["foo.c"]`,
5286 }),
5287 MakeBazelTarget("cc_library_shared", "foo_both", AttrNameToString{
5288 "srcs_c": `["foo.c"]`,
5289 }),
5290 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
5291 "srcs_c": `["foo.c"]`,
5292 }),
5293 MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
5294 "srcs_c": `["foo.c"]`,
5295 }),
5296 MakeBazelTarget("cc_library_static", "bar", AttrNameToString{
5297 "srcs_c": `["bar.c"]`,
5298 }),
5299 },
5300 })
5301}