blob: 188c81b2a8e55af4ec885c58a9210399d412191a [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 Chen9c2e3ee2023-10-11 10:51:28 +0000140 "//build/bazel_common_rules/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
141 "//build/bazel_common_rules/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000142 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143 })`,
Chris Parsons77acf2e2021-12-03 17:27:16 -0500144 "srcs": `["impl.cpp"] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000145 "//build/bazel_common_rules/platforms/arch:x86": ["x86.cpp"],
146 "//build/bazel_common_rules/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400147 "//conditions:default": [],
148 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000149 "//build/bazel_common_rules/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 Chen9c2e3ee2023-10-11 10:51:28 +0000153 "//build/bazel_common_rules/platforms/os:darwin": ["darwin.cpp"],
154 "//build/bazel_common_rules/platforms/os:linux_bionic": ["bionic.cpp"],
155 "//build/bazel_common_rules/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 Chen9c2e3ee2023-10-11 10:51:28 +0000231 "//build/bazel_common_rules/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
232 "//build/bazel_common_rules/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 Chen9c2e3ee2023-10-11 10:51:28 +0000281 "//build/bazel_common_rules/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000282 "//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 Parsonsd0783372023-10-05 15:47:07 +0000562 StubbedBuildDefinitions: []string{"//foo/bar:whole_static_lib_for_shared", "//foo/bar:whole_static_lib_for_static",
563 "//foo/bar: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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000691 "//build/bazel_common_rules/platforms/arch:x86": ["-DX86_STATIC"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500692 "//conditions:default": [],
693 })`,
694 "implementation_deps": `[
695 ":static_dep_for_both",
696 ":static_dep_for_static",
697 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000698 "//build/bazel_common_rules/platforms/arch:x86": [":x86_dep_for_static"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500699 "//conditions:default": [],
700 })`,
701 "local_includes": `["."]`,
702 "srcs": `[
703 "both.cpp",
704 "staticonly.cpp",
705 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000706 "//build/bazel_common_rules/platforms/arch:x86": ["x86_static.cpp"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500707 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000715 "//build/bazel_common_rules/platforms/arch:arm": ["-DARM_SHARED"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500716 "//conditions:default": [],
717 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000718 "//build/bazel_common_rules/platforms/os:android": ["-DANDROID_SHARED"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500719 "//conditions:default": [],
720 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000721 "//build/bazel_common_rules/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500722 "//conditions:default": [],
723 })`,
724 "implementation_deps": `[
725 ":static_dep_for_both",
726 ":static_dep_for_shared",
727 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000728 "//build/bazel_common_rules/platforms/arch:arm": [":arm_static_dep_for_shared"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500729 "//conditions:default": [],
730 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000731 "//build/bazel_common_rules/platforms/os:android": [":android_dep_for_shared"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500732 "//conditions:default": [],
733 })`,
734 "implementation_dynamic_deps": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000735 "//build/bazel_common_rules/platforms/arch:arm": [":arm_shared_dep_for_shared"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500736 "//conditions:default": [],
737 })`,
738 "local_includes": `["."]`,
739 "srcs": `[
740 "both.cpp",
741 "sharedonly.cpp",
742 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000743 "//build/bazel_common_rules/platforms/arch:arm": ["arm_shared.cpp"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500744 "//conditions:default": [],
745 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000746 "//build/bazel_common_rules/platforms/os:android": ["android_shared.cpp"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500747 "//conditions:default": [],
748 })`,
749 "whole_archive_deps": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000750 "//build/bazel_common_rules/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
Chris Parsons77acf2e2021-12-03 17:27:16 -0500751 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000957 "//build/bazel_common_rules/platforms/arch:arm": [
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000958 "arm.map",
959 "dynamic_arm.list",
960 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000961 "//build/bazel_common_rules/platforms/arch:arm64": [
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000962 "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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000968 "//build/bazel_common_rules/platforms/arch:arm": [
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000969 "-Wl,--version-script,$(location arm.map)",
970 "-Wl,--dynamic-list,$(location dynamic_arm.list)",
971 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000972 "//build/bazel_common_rules/platforms/arch:arm64": [
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000973 "-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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000980 "//build/bazel_common_rules/platforms/arch:arm": ["android_cfi_exports_map"],
981 "//build/bazel_common_rules/platforms/arch:arm64": ["android_cfi_exports_map"],
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000982 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001068 "//build/bazel_common_rules/platforms/arch:x86_64": [
Chris Parsons77acf2e2021-12-03 17:27:16 -05001069 "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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001079 "//build/bazel_common_rules/platforms/os:darwin": [
Chris Parsons77acf2e2021-12-03 17:27:16 -05001080 "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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001184 "//build/bazel_common_rules/platforms/arch:arm64": ["-DARM64=1"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001185 "//conditions:default": [],
1186 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001187 "//build/bazel_common_rules/platforms/os:android": ["-DANDROID=1"],
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001188 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001274 "//build/bazel_common_rules/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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001281 "//build/bazel_common_rules/platforms/arch:arm": [],
Liz Kammer7a210ac2021-09-22 15:52:58 -04001282 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001289 "//build/bazel_common_rules/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"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001328 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Zi Wang0a8a1292022-08-30 06:27:01 +00001329 }),
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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001405 "//build/bazel_common_rules/platforms/arch:arm": ["-link_crt"],
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001406 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001509 "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"],
1510 "//build/bazel_common_rules/platforms/arch:x86": ["-use_libcrt"],
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001511 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001546 "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"],
1547 "//build/bazel_common_rules/platforms/arch:x86": ["-use_libcrt"],
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001548 "//conditions:default": [],
1549 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001550 "//build/bazel_common_rules/platforms/os:darwin": ["-use_libcrt"],
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001551 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001589 "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"],
1590 "//build/bazel_common_rules/platforms/arch:x86_64": ["-use_libcrt"],
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001591 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001708 "//build/bazel_common_rules/platforms/arch:arm64": True,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001709 "//conditions:default": None,
1710 }),
1711 "keep_symbols_and_debug_frame": select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001712 "//build/bazel_common_rules/platforms/arch:arm": True,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001713 "//conditions:default": None,
1714 }),
1715 "keep_symbols_list": select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001716 "//build/bazel_common_rules/platforms/os:darwin": [
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001717 "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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001847 "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"],
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001848 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001878 "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"],
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +00001879 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002014 "//build/bazel_common_rules/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 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002019 "//build/bazel_common_rules/platforms/os:darwin": ["darwin.cpp"],
2020 "//build/bazel_common_rules/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 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002024 "//build/bazel_common_rules/platforms/os:linux_glibc": [
Colin Cross133782e2022-12-20 15:29:31 -08002025 "linux.cpp",
2026 "linux_glibc.cpp",
2027 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002028 "//build/bazel_common_rules/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 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002032 "//build/bazel_common_rules/platforms/os:windows": ["windows.cpp"],
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002033 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002059 "//build/bazel_common_rules/platforms/os:android": True,
Yu Liu75be7b92022-02-01 09:54:09 -08002060 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002700 "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
2701 "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
2702 "//build/bazel_common_rules/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
2703 "//build/bazel_common_rules/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
2704 "//build/bazel_common_rules/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
Chris Parsons58852a02021-12-09 18:10:18 -05002705 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002736 "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": [],
2737 "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": [],
Chris Parsons58852a02021-12-09 18:10:18 -05002738 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002770 "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"],
Chris Parsons58852a02021-12-09 18:10:18 -05002771 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002807 "//build/bazel_common_rules/platforms/os:windows": ["@platforms//:incompatible"],
Chris Parsons58852a02021-12-09 18:10:18 -05002808 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002813 "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": [],
2814 "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": [],
2815 "//build/bazel_common_rules/platforms/os_arch:linux_glibc_x86": [],
Chris Parsons58852a02021-12-09 18:10:18 -05002816 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002926 "//build/bazel_common_rules/platforms/os:darwin": [":bazlib"],
2927 "//build/bazel_common_rules/platforms/os:linux_bionic": [":bazlib"],
2928 "//build/bazel_common_rules/platforms/os:linux_glibc": [":bazlib"],
2929 "//build/bazel_common_rules/platforms/os:linux_musl": [":bazlib"],
2930 "//build/bazel_common_rules/platforms/os:windows": [":bazlib"],
Liz Kammerffc17e42022-11-23 09:42:05 -05002931 "//conditions:default": [],
2932 }) + select({
Spandan Das6d4d9da2023-04-18 06:20:40 +00002933 "//build/bazel/rules/apex:foo": [
2934 "@api_surfaces//module-libapi/current:barlib",
2935 "@api_surfaces//module-libapi/current:quxlib",
2936 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00002937 "//build/bazel_common_rules/platforms/os:darwin": [":quxlib"],
2938 "//build/bazel_common_rules/platforms/os:linux_bionic": [":quxlib"],
2939 "//build/bazel_common_rules/platforms/os:linux_glibc": [":quxlib"],
2940 "//build/bazel_common_rules/platforms/os:linux_musl": [":quxlib"],
2941 "//build/bazel_common_rules/platforms/os:windows": [":quxlib"],
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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003056 "//build/bazel_common_rules/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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003140 "//build/bazel_common_rules/platforms/arch:arm": "-32",
3141 "//build/bazel_common_rules/platforms/arch:arm64": "-64",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05003142 "//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,
Chris Parsonsd0783372023-10-05 15:47:07 +00003358 StubbedBuildDefinitions: []string{"bar", "baz", "buh"},
Vinh Tran85fb07c2022-09-16 16:17:48 -04003359 Blueprint: `
3360cc_library {
3361 name: "foo",
3362 shared_libs: ["bar", "baz"],
3363 static_libs: ["baz", "buh"],
3364 target: {
3365 apex: {
3366 exclude_shared_libs: ["bar"],
3367 exclude_static_libs: ["buh"],
3368 }
3369 }
Chris Parsonsd0783372023-10-05 15:47:07 +00003370}` + simpleModule("cc_library_static", "baz") +
3371 simpleModule("cc_library_static", "buh") +
3372 simpleModule("cc_library_static", "bar"),
Vinh Tran85fb07c2022-09-16 16:17:48 -04003373 ExpectedBazelTargets: []string{
3374 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Chris Parsonsd0783372023-10-05 15:47:07 +00003375 "implementation_deps": `[":baz"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003376 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003377 "//conditions:default": [":buh"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003378 })`,
Chris Parsonsd0783372023-10-05 15:47:07 +00003379 "implementation_dynamic_deps": `[":baz"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003380 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003381 "//conditions:default": [":bar"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003382 })`,
3383 "local_includes": `["."]`,
3384 }),
3385 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Chris Parsonsd0783372023-10-05 15:47:07 +00003386 "implementation_deps": `[":baz"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003387 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003388 "//conditions:default": [":buh"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003389 })`,
Chris Parsonsd0783372023-10-05 15:47:07 +00003390 "implementation_dynamic_deps": `[":baz"] + select({
Liz Kammer748d7072023-01-25 12:07:43 -05003391 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003392 "//conditions:default": [":bar"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003393 })`,
3394 "local_includes": `["."]`,
3395 }),
3396 },
3397 })
3398}
3399
3400func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
3401 runCcLibraryTestCase(t, Bp2buildTestCase{
3402 Description: "cc_library with target.apex and export_shared|static_lib_headers",
3403 ModuleTypeUnderTest: "cc_library",
3404 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3405 Blueprint: `
3406cc_library_static {
3407 name: "foo",
3408 shared_libs: ["bar", "baz"],
3409 static_libs: ["abc"],
3410 export_shared_lib_headers: ["baz"],
3411 export_static_lib_headers: ["abc"],
3412 target: {
3413 apex: {
3414 exclude_shared_libs: ["baz", "bar"],
3415 exclude_static_libs: ["abc"],
3416 }
3417 }
Chris Parsonsd0783372023-10-05 15:47:07 +00003418}` + simpleModule("cc_library_static", "bar") +
3419 simpleModule("cc_library_static", "baz") +
3420 simpleModule("cc_library_static", "abc"),
3421 StubbedBuildDefinitions: []string{"bar", "baz", "abc"},
Vinh Tran85fb07c2022-09-16 16:17:48 -04003422 ExpectedBazelTargets: []string{
3423 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3424 "implementation_dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003425 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003426 "//conditions:default": [":bar"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003427 })`,
3428 "dynamic_deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003429 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003430 "//conditions:default": [":baz"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003431 })`,
3432 "deps": `select({
Liz Kammer748d7072023-01-25 12:07:43 -05003433 "//build/bazel/rules/apex:in_apex": [],
Chris Parsonsd0783372023-10-05 15:47:07 +00003434 "//conditions:default": [":abc"],
Vinh Tran85fb07c2022-09-16 16:17:48 -04003435 })`,
3436 "local_includes": `["."]`,
3437 }),
3438 },
3439 })
3440}
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003441
3442func TestCcLibraryWithSyspropSrcs(t *testing.T) {
3443 runCcLibraryTestCase(t, Bp2buildTestCase{
3444 Description: "cc_library with sysprop sources",
3445 ModuleTypeUnderTest: "cc_library",
3446 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3447 Blueprint: `
3448cc_library {
3449 name: "foo",
3450 srcs: [
3451 "bar.sysprop",
3452 "baz.sysprop",
3453 "blah.cpp",
3454 ],
3455 min_sdk_version: "5",
3456}`,
3457 ExpectedBazelTargets: []string{
3458 MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
3459 "srcs": `[
3460 "bar.sysprop",
3461 "baz.sysprop",
3462 ]`,
3463 }),
3464 MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3465 "dep": `":foo_sysprop_library"`,
3466 "min_sdk_version": `"5"`,
3467 }),
3468 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3469 "srcs": `["blah.cpp"]`,
3470 "local_includes": `["."]`,
3471 "min_sdk_version": `"5"`,
3472 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3473 }),
3474 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3475 "srcs": `["blah.cpp"]`,
3476 "local_includes": `["."]`,
3477 "min_sdk_version": `"5"`,
3478 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
3479 }),
3480 },
3481 })
3482}
3483
3484func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
3485 runCcLibraryTestCase(t, Bp2buildTestCase{
3486 Description: "cc_library with sysprop sources in some configs but not others",
3487 ModuleTypeUnderTest: "cc_library",
3488 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3489 Blueprint: `
3490cc_library {
3491 name: "foo",
3492 host_supported: true,
3493 srcs: [
3494 "blah.cpp",
3495 ],
3496 target: {
3497 android: {
3498 srcs: ["bar.sysprop"],
3499 },
3500 },
3501 min_sdk_version: "5",
3502}`,
3503 ExpectedBazelTargets: []string{
3504 MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
3505 "srcs": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003506 "//build/bazel_common_rules/platforms/os:android": ["bar.sysprop"],
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003507 "//conditions:default": [],
3508 })`,
3509 }),
3510 MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
3511 "dep": `":foo_sysprop_library"`,
3512 "min_sdk_version": `"5"`,
3513 }),
3514 MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3515 "srcs": `["blah.cpp"]`,
3516 "local_includes": `["."]`,
3517 "min_sdk_version": `"5"`,
3518 "whole_archive_deps": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003519 "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"],
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003520 "//conditions:default": [],
3521 })`,
3522 }),
3523 MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
3524 "srcs": `["blah.cpp"]`,
3525 "local_includes": `["."]`,
3526 "min_sdk_version": `"5"`,
3527 "whole_archive_deps": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003528 "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"],
Trevor Radcliffecee4e052022-09-06 19:31:25 +00003529 "//conditions:default": [],
3530 })`,
3531 }),
3532 },
3533 })
3534}
Vinh Tran395a1e92022-09-16 18:27:29 -04003535
Sam Delmerico512437b2023-03-17 11:34:15 -04003536func TestCcLibraryWithAidlAndLibs(t *testing.T) {
Vinh Tran395a1e92022-09-16 18:27:29 -04003537 runCcLibraryTestCase(t, Bp2buildTestCase{
Sam Delmerico512437b2023-03-17 11:34:15 -04003538 Description: "cc_aidl_library depends on libs from parent cc_library_static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003539 ModuleTypeUnderTest: "cc_library",
3540 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00003541 StubbedBuildDefinitions: []string{"bar-static", "baz-static", "bar-shared", "baz-shared"},
Vinh Tran395a1e92022-09-16 18:27:29 -04003542 Blueprint: `
3543cc_library_static {
Sam Delmerico512437b2023-03-17 11:34:15 -04003544 name: "foo",
3545 srcs: [
3546 "Foo.aidl",
3547 ],
3548 static_libs: [
3549 "bar-static",
3550 "baz-static",
3551 ],
Vinh Tran395a1e92022-09-16 18:27:29 -04003552 shared_libs: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003553 "bar-shared",
3554 "baz-shared",
3555 ],
3556 export_static_lib_headers: [
3557 "baz-static",
Vinh Tran395a1e92022-09-16 18:27:29 -04003558 ],
3559 export_shared_lib_headers: [
Sam Delmerico512437b2023-03-17 11:34:15 -04003560 "baz-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003561 ],
3562}` +
Chris Parsonscd209032023-09-19 01:12:48 +00003563 simpleModule("cc_library_static", "bar-static") +
3564 simpleModule("cc_library_static", "baz-static") +
3565 simpleModule("cc_library", "bar-shared") +
3566 simpleModule("cc_library", "baz-shared"),
Vinh Tran395a1e92022-09-16 18:27:29 -04003567 ExpectedBazelTargets: []string{
3568 MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
3569 "srcs": `["Foo.aidl"]`,
3570 }),
3571 MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
Vinh Trane6842942023-04-28 11:21:25 -04003572 "local_includes": `["."]`,
3573 "deps": `[":foo_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003574 "implementation_deps": `[
3575 ":baz-static",
3576 ":bar-static",
3577 ]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003578 "implementation_dynamic_deps": `[
Sam Delmerico512437b2023-03-17 11:34:15 -04003579 ":baz-shared",
3580 ":bar-shared",
Vinh Tran395a1e92022-09-16 18:27:29 -04003581 ]`,
3582 }),
3583 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3584 "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
Sam Delmerico512437b2023-03-17 11:34:15 -04003585 "deps": `[":baz-static"]`,
3586 "implementation_deps": `[":bar-static"]`,
3587 "dynamic_deps": `[":baz-shared"]`,
3588 "implementation_dynamic_deps": `[":bar-shared"]`,
Vinh Tran395a1e92022-09-16 18:27:29 -04003589 "local_includes": `["."]`,
3590 }),
3591 },
3592 })
3593}
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003594
3595func TestCcLibraryWithTidy(t *testing.T) {
3596 runCcLibraryTestCase(t, Bp2buildTestCase{
3597 Description: "cc_library uses tidy properties",
3598 ModuleTypeUnderTest: "cc_library",
3599 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3600 Blueprint: `
3601cc_library_static {
Sam Delmerico63f0c932023-03-14 14:05:28 -04003602 name: "foo",
3603 srcs: ["foo.cpp"],
3604}
3605cc_library_static {
3606 name: "foo-no-tidy",
3607 srcs: ["foo.cpp"],
3608 tidy: false,
3609}
3610cc_library_static {
3611 name: "foo-tidy",
3612 srcs: ["foo.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003613 tidy: true,
3614 tidy_checks: ["check1", "check2"],
3615 tidy_checks_as_errors: ["check1error", "check2error"],
3616 tidy_disabled_srcs: ["bar.cpp"],
Sam Delmerico4c902d62022-11-02 14:17:15 -04003617 tidy_timeout_srcs: ["baz.cpp"],
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003618}`,
3619 ExpectedBazelTargets: []string{
3620 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
3621 "local_includes": `["."]`,
3622 "srcs": `["foo.cpp"]`,
Sam Delmerico63f0c932023-03-14 14:05:28 -04003623 }),
3624 MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{
3625 "local_includes": `["."]`,
3626 "srcs": `["foo.cpp"]`,
3627 "tidy": `"never"`,
3628 }),
3629 MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{
3630 "local_includes": `["."]`,
3631 "srcs": `["foo.cpp"]`,
3632 "tidy": `"local"`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003633 "tidy_checks": `[
3634 "check1",
3635 "check2",
3636 ]`,
3637 "tidy_checks_as_errors": `[
3638 "check1error",
3639 "check2error",
3640 ]`,
3641 "tidy_disabled_srcs": `["bar.cpp"]`,
Sam Delmerico4c902d62022-11-02 14:17:15 -04003642 "tidy_timeout_srcs": `["baz.cpp"]`,
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -04003643 }),
3644 },
3645 })
3646}
Yu Liu56ccb1a2022-11-12 10:47:07 -08003647
Vinh Tran99270ea2022-11-28 11:15:23 -05003648func TestCcLibraryWithAfdoEnabled(t *testing.T) {
3649 bp := `
3650cc_library {
3651 name: "foo",
3652 afdo: true,
3653 include_build_directory: false,
3654}`
3655
3656 // TODO(b/260714900): Add test case for arch-specific afdo profile
3657 testCases := []struct {
3658 description string
3659 filesystem map[string]string
3660 expectedBazelTargets []string
3661 }{
3662 {
3663 description: "cc_library with afdo enabled and existing profile",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003664 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003665 "vendor/google_data/pgo_profile/sampling/Android.bp": "",
3666 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003667 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003668 expectedBazelTargets: []string{
3669 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3670 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3671 "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
3672 }),
3673 },
3674 },
3675 {
3676 description: "cc_library with afdo enabled and existing profile in AOSP",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003677 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003678 "toolchain/pgo-profiles/sampling/Android.bp": "",
3679 "toolchain/pgo-profiles/sampling/foo.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003680 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003681 expectedBazelTargets: []string{
3682 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3683 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3684 "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
3685 }),
3686 },
3687 },
3688 {
3689 description: "cc_library with afdo enabled but profile filename doesn't match with module name",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003690 filesystem: map[string]string{
Vinh Trance40b922023-06-05 12:57:55 -04003691 "toolchain/pgo-profiles/sampling/Android.bp": "",
3692 "toolchain/pgo-profiles/sampling/bar.afdo": "",
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003693 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003694 expectedBazelTargets: []string{
3695 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3696 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3697 },
3698 },
3699 {
3700 description: "cc_library with afdo enabled but profile doesn't exist",
3701 expectedBazelTargets: []string{
3702 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3703 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3704 },
3705 },
Vinh Tranbc9c8b42022-12-09 12:03:52 -05003706 {
3707 description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist",
3708 filesystem: map[string]string{
3709 "vendor/google_data/pgo_profile/sampling/foo.afdo": "",
3710 },
3711 expectedBazelTargets: []string{
3712 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3713 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
3714 },
3715 },
Vinh Tran99270ea2022-11-28 11:15:23 -05003716 }
3717 for _, testCase := range testCases {
3718 t.Run(testCase.description, func(t *testing.T) {
3719 runCcLibraryTestCase(t, Bp2buildTestCase{
3720 ExpectedBazelTargets: testCase.expectedBazelTargets,
3721 ModuleTypeUnderTest: "cc_library",
3722 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3723 Description: testCase.description,
3724 Blueprint: binaryReplacer.Replace(bp),
3725 Filesystem: testCase.filesystem,
3726 })
3727 })
3728 }
3729}
3730
Yu Liu56ccb1a2022-11-12 10:47:07 -08003731func TestCcLibraryHeaderAbiChecker(t *testing.T) {
3732 runCcLibraryTestCase(t, Bp2buildTestCase{
3733 Description: "cc_library with header abi checker",
3734 ModuleTypeUnderTest: "cc_library",
3735 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3736 Blueprint: `cc_library {
3737 name: "foo",
3738 header_abi_checker: {
3739 enabled: true,
3740 symbol_file: "a.map.txt",
3741 exclude_symbol_versions: [
3742 "29",
3743 "30",
3744 ],
3745 exclude_symbol_tags: [
3746 "tag1",
3747 "tag2",
3748 ],
3749 check_all_apis: true,
3750 diff_flags: ["-allow-adding-removing-weak-symbols"],
3751 },
3752 include_build_directory: false,
3753}`,
3754 ExpectedBazelTargets: []string{
3755 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
3756 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3757 "abi_checker_enabled": `True`,
3758 "abi_checker_symbol_file": `"a.map.txt"`,
3759 "abi_checker_exclude_symbol_versions": `[
3760 "29",
3761 "30",
3762 ]`,
3763 "abi_checker_exclude_symbol_tags": `[
3764 "tag1",
3765 "tag2",
3766 ]`,
3767 "abi_checker_check_all_apis": `True`,
3768 "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`,
3769 }),
3770 },
3771 })
3772}
Jingwen Chenc4c34e12022-11-29 12:07:45 +00003773
3774func TestCcLibraryApexAvailable(t *testing.T) {
3775 runCcLibraryTestCase(t, Bp2buildTestCase{
3776 Description: "cc_library apex_available converted to tags",
3777 ModuleTypeUnderTest: "cc_library",
3778 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3779 Blueprint: soongCcLibraryPreamble + `
3780cc_library {
3781 name: "a",
3782 srcs: ["a.cpp"],
3783 apex_available: ["com.android.foo"],
3784}
3785`,
3786 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3787 "tags": `["apex_available=com.android.foo"]`,
3788 "srcs": `["a.cpp"]`,
3789 "local_includes": `["."]`,
3790 }),
3791 },
3792 )
3793}
3794
3795func TestCcLibraryApexAvailableMultiple(t *testing.T) {
3796 runCcLibraryTestCase(t, Bp2buildTestCase{
3797 Description: "cc_library apex_available converted to multiple tags",
3798 ModuleTypeUnderTest: "cc_library",
3799 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3800 Blueprint: soongCcLibraryPreamble + `
3801cc_library {
3802 name: "a",
3803 srcs: ["a.cpp"],
3804 apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"],
3805}
3806`,
3807 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3808 "tags": `[
3809 "apex_available=com.android.foo",
3810 "apex_available=//apex_available:platform",
3811 "apex_available=com.android.bar",
3812 ]`,
3813 "srcs": `["a.cpp"]`,
3814 "local_includes": `["."]`,
3815 }),
3816 },
3817 )
3818}
Zi Wang0f828442022-12-28 11:18:11 -08003819
3820// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag.
3821// In bp2build output, variant info(select) should go before general info.
3822// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"])
3823func TestCcLibraryVariantPrependPropOrder(t *testing.T) {
3824 runCcLibraryTestCase(t, Bp2buildTestCase{
3825 Description: "cc_library variant prepend properties order",
3826 ModuleTypeUnderTest: "cc_library",
3827 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3828 Blueprint: soongCcLibraryPreamble + `
3829cc_library {
3830 name: "a",
3831 srcs: ["a.cpp"],
3832 export_include_dirs: ["eid1", "eid2"],
3833 export_system_include_dirs: ["esid1", "esid2"],
3834 target: {
3835 android: {
3836 export_include_dirs: ["android_eid1", "android_eid2"],
3837 export_system_include_dirs: ["android_esid1", "android_esid2"],
3838 },
3839 android_arm: {
3840 export_include_dirs: ["android_arm_eid1", "android_arm_eid2"],
3841 export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"],
3842 },
3843 linux: {
3844 export_include_dirs: ["linux_eid1", "linux_eid2"],
3845 export_system_include_dirs: ["linux_esid1", "linux_esid2"],
3846 },
3847 },
3848 multilib: {
3849 lib32: {
3850 export_include_dirs: ["lib32_eid1", "lib32_eid2"],
3851 export_system_include_dirs: ["lib32_esid1", "lib32_esid2"],
3852 },
3853 },
3854 arch: {
3855 arm: {
3856 export_include_dirs: ["arm_eid1", "arm_eid2"],
3857 export_system_include_dirs: ["arm_esid1", "arm_esid2"],
3858 },
3859 }
3860}
3861`,
3862 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
3863 "export_includes": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003864 "//build/bazel_common_rules/platforms/os_arch:android_arm": [
Zi Wang0f828442022-12-28 11:18:11 -08003865 "android_arm_eid1",
3866 "android_arm_eid2",
3867 ],
3868 "//conditions:default": [],
3869 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003870 "//build/bazel_common_rules/platforms/os:android": [
Zi Wang0f828442022-12-28 11:18:11 -08003871 "android_eid1",
3872 "android_eid2",
3873 "linux_eid1",
3874 "linux_eid2",
3875 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003876 "//build/bazel_common_rules/platforms/os:linux_bionic": [
Zi Wang0f828442022-12-28 11:18:11 -08003877 "linux_eid1",
3878 "linux_eid2",
3879 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003880 "//build/bazel_common_rules/platforms/os:linux_glibc": [
Zi Wang0f828442022-12-28 11:18:11 -08003881 "linux_eid1",
3882 "linux_eid2",
3883 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003884 "//build/bazel_common_rules/platforms/os:linux_musl": [
Zi Wang0f828442022-12-28 11:18:11 -08003885 "linux_eid1",
3886 "linux_eid2",
3887 ],
3888 "//conditions:default": [],
3889 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003890 "//build/bazel_common_rules/platforms/arch:arm": [
Zi Wang0f828442022-12-28 11:18:11 -08003891 "lib32_eid1",
3892 "lib32_eid2",
3893 "arm_eid1",
3894 "arm_eid2",
3895 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003896 "//build/bazel_common_rules/platforms/arch:x86": [
Zi Wang0f828442022-12-28 11:18:11 -08003897 "lib32_eid1",
3898 "lib32_eid2",
3899 ],
3900 "//conditions:default": [],
3901 }) + [
3902 "eid1",
3903 "eid2",
3904 ]`,
3905 "export_system_includes": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003906 "//build/bazel_common_rules/platforms/os_arch:android_arm": [
Zi Wang0f828442022-12-28 11:18:11 -08003907 "android_arm_esid1",
3908 "android_arm_esid2",
3909 ],
3910 "//conditions:default": [],
3911 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003912 "//build/bazel_common_rules/platforms/os:android": [
Zi Wang0f828442022-12-28 11:18:11 -08003913 "android_esid1",
3914 "android_esid2",
3915 "linux_esid1",
3916 "linux_esid2",
3917 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003918 "//build/bazel_common_rules/platforms/os:linux_bionic": [
Zi Wang0f828442022-12-28 11:18:11 -08003919 "linux_esid1",
3920 "linux_esid2",
3921 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003922 "//build/bazel_common_rules/platforms/os:linux_glibc": [
Zi Wang0f828442022-12-28 11:18:11 -08003923 "linux_esid1",
3924 "linux_esid2",
3925 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003926 "//build/bazel_common_rules/platforms/os:linux_musl": [
Zi Wang0f828442022-12-28 11:18:11 -08003927 "linux_esid1",
3928 "linux_esid2",
3929 ],
3930 "//conditions:default": [],
3931 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003932 "//build/bazel_common_rules/platforms/arch:arm": [
Zi Wang0f828442022-12-28 11:18:11 -08003933 "lib32_esid1",
3934 "lib32_esid2",
3935 "arm_esid1",
3936 "arm_esid2",
3937 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003938 "//build/bazel_common_rules/platforms/arch:x86": [
Zi Wang0f828442022-12-28 11:18:11 -08003939 "lib32_esid1",
3940 "lib32_esid2",
3941 ],
3942 "//conditions:default": [],
3943 }) + [
3944 "esid1",
3945 "esid2",
3946 ]`,
3947 "srcs": `["a.cpp"]`,
3948 "local_includes": `["."]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00003949 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Zi Wang0f828442022-12-28 11:18:11 -08003950 }),
3951 },
3952 )
3953}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00003954
3955func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) {
3956 runCcLibraryTestCase(t, Bp2buildTestCase{
3957 Description: "cc_library has correct features when integer_overflow property is provided",
3958 ModuleTypeUnderTest: "cc_library",
3959 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3960 Blueprint: `
3961cc_library {
3962 name: "foo",
3963 sanitize: {
3964 integer_overflow: true,
3965 },
3966}
3967`,
3968 ExpectedBazelTargets: []string{
3969 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3970 "features": `["ubsan_integer_overflow"]`,
3971 "local_includes": `["."]`,
3972 }),
3973 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
3974 "features": `["ubsan_integer_overflow"]`,
3975 "local_includes": `["."]`,
3976 }),
3977 },
3978 })
3979}
3980
3981func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) {
3982 runCcLibraryTestCase(t, Bp2buildTestCase{
3983 Description: "cc_library has correct features when misc_undefined property is provided",
3984 ModuleTypeUnderTest: "cc_library",
3985 ModuleTypeUnderTestFactory: cc.LibraryFactory,
3986 Blueprint: `
3987cc_library {
3988 name: "foo",
3989 sanitize: {
3990 misc_undefined: ["undefined", "nullability"],
3991 },
3992}
3993`,
3994 ExpectedBazelTargets: []string{
3995 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
3996 "features": `[
3997 "ubsan_undefined",
3998 "ubsan_nullability",
3999 ]`,
4000 "local_includes": `["."]`,
4001 }),
4002 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4003 "features": `[
4004 "ubsan_undefined",
4005 "ubsan_nullability",
4006 ]`,
4007 "local_includes": `["."]`,
4008 }),
4009 },
4010 })
4011}
4012
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004013func TestCcLibraryWithSanitizerBlocklist(t *testing.T) {
4014 runCcLibraryTestCase(t, Bp2buildTestCase{
4015 Description: "cc_library has correct feature when sanitize.blocklist is provided",
4016 ModuleTypeUnderTest: "cc_library",
4017 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4018 Blueprint: `
4019cc_library {
4020 name: "foo",
4021 sanitize: {
4022 blocklist: "foo_blocklist.txt",
4023 },
4024}
4025`,
4026 ExpectedBazelTargets: []string{
4027 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004028 "copts": `select({
4029 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4030 "//conditions:default": [],
4031 })`,
4032 "additional_compiler_inputs": `select({
4033 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4034 "//conditions:default": [],
4035 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004036 "local_includes": `["."]`,
4037 }),
4038 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +00004039 "copts": `select({
4040 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
4041 "//conditions:default": [],
4042 })`,
4043 "additional_compiler_inputs": `select({
4044 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
4045 "//conditions:default": [],
4046 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +00004047 "local_includes": `["."]`,
4048 }),
4049 },
4050 })
4051}
4052
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004053func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) {
4054 runCcLibraryTestCase(t, Bp2buildTestCase{
4055 Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks",
4056 ModuleTypeUnderTest: "cc_library",
4057 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4058 Blueprint: `
4059cc_library {
4060 name: "foo",
4061 sanitize: {
4062 misc_undefined: ["undefined", "nullability"],
4063 },
4064 target: {
4065 android: {
4066 sanitize: {
4067 misc_undefined: ["alignment"],
4068 },
4069 },
4070 linux_glibc: {
4071 sanitize: {
4072 integer_overflow: true,
4073 },
4074 },
4075 },
4076}
4077`,
4078 ExpectedBazelTargets: []string{
4079 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4080 "features": `[
4081 "ubsan_undefined",
4082 "ubsan_nullability",
4083 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004084 "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"],
4085 "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004086 "//conditions:default": [],
4087 })`,
4088 "local_includes": `["."]`,
4089 }),
4090 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4091 "features": `[
4092 "ubsan_undefined",
4093 "ubsan_nullability",
4094 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004095 "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"],
4096 "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00004097 "//conditions:default": [],
4098 })`,
4099 "local_includes": `["."]`,
4100 }),
4101 },
4102 })
4103}
Yu Liu10174ff2023-02-21 12:05:26 -08004104
4105func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
4106 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
4107 Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
4108 ModuleTypeUnderTest: "cc_library",
4109 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004110 StubbedBuildDefinitions: []string{"barlib", "bazlib"},
Yu Liu10174ff2023-02-21 12:05:26 -08004111 Blueprint: `
4112cc_library {
4113 name: "barlib",
4114 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004115 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004116}
4117cc_library {
4118 name: "bazlib",
4119 stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
Spandan Das6d4d9da2023-04-18 06:20:40 +00004120 apex_available: ["//apex_available:platform",],
Yu Liu10174ff2023-02-21 12:05:26 -08004121}
4122cc_library {
4123 name: "foo",
4124 shared_libs: ["barlib", "bazlib"],
4125 export_shared_lib_headers: ["bazlib"],
4126 apex_available: [
Spandan Das6d4d9da2023-04-18 06:20:40 +00004127 "//apex_available:platform",
Yu Liu10174ff2023-02-21 12:05:26 -08004128 ],
4129}`,
4130 ExpectedBazelTargets: []string{
4131 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", 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 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
Spandan Das6d4d9da2023-04-18 06:20:40 +00004138 "implementation_dynamic_deps": `[":barlib"]`,
4139 "dynamic_deps": `[":bazlib"]`,
4140 "local_includes": `["."]`,
4141 "tags": `["apex_available=//apex_available:platform"]`,
Yu Liu10174ff2023-02-21 12:05:26 -08004142 }),
4143 },
4144 })
4145}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004146
4147func TestCcLibraryWithThinLto(t *testing.T) {
4148 runCcLibraryTestCase(t, Bp2buildTestCase{
4149 Description: "cc_library has correct features when thin LTO is enabled",
4150 ModuleTypeUnderTest: "cc_library",
4151 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4152 Blueprint: `
4153cc_library {
4154 name: "foo",
4155 lto: {
4156 thin: true,
4157 },
4158}`,
4159 ExpectedBazelTargets: []string{
4160 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4161 "features": `["android_thin_lto"]`,
4162 "local_includes": `["."]`,
4163 }),
4164 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4165 "features": `["android_thin_lto"]`,
4166 "local_includes": `["."]`,
4167 }),
4168 },
4169 })
4170}
4171
4172func TestCcLibraryWithLtoNever(t *testing.T) {
4173 runCcLibraryTestCase(t, Bp2buildTestCase{
4174 Description: "cc_library has correct features when LTO is explicitly disabled",
4175 ModuleTypeUnderTest: "cc_library",
4176 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4177 Blueprint: `
4178cc_library {
4179 name: "foo",
4180 lto: {
4181 never: true,
4182 },
4183}`,
4184 ExpectedBazelTargets: []string{
4185 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4186 "features": `["-android_thin_lto"]`,
4187 "local_includes": `["."]`,
4188 }),
4189 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4190 "features": `["-android_thin_lto"]`,
4191 "local_includes": `["."]`,
4192 }),
4193 },
4194 })
4195}
4196
4197func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) {
4198 runCcLibraryTestCase(t, Bp2buildTestCase{
4199 Description: "cc_library has correct features when LTO differs across arch and os variants",
4200 ModuleTypeUnderTest: "cc_library",
4201 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4202 Blueprint: `
4203cc_library {
4204 name: "foo",
4205 target: {
4206 android: {
4207 lto: {
4208 thin: true,
4209 },
4210 },
4211 },
4212 arch: {
4213 riscv64: {
4214 lto: {
4215 thin: false,
4216 },
4217 },
4218 },
4219}`,
4220 ExpectedBazelTargets: []string{
4221 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4222 "local_includes": `["."]`,
4223 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004224 "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"],
4225 "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"],
4226 "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4227 "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"],
4228 "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"],
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004229 "//conditions:default": [],
4230 })`}),
4231 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4232 "local_includes": `["."]`,
4233 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004234 "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"],
4235 "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"],
4236 "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
4237 "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"],
4238 "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"],
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004239 "//conditions:default": [],
4240 })`}),
4241 },
4242 })
4243}
4244
4245func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
4246 runCcLibraryTestCase(t, Bp2buildTestCase{
4247 Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant",
4248 ModuleTypeUnderTest: "cc_library",
4249 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4250 Blueprint: `
4251cc_library {
4252 name: "foo",
4253 lto: {
4254 never: true,
4255 },
4256 target: {
4257 android: {
4258 lto: {
4259 thin: true,
4260 never: false,
4261 },
4262 },
4263 },
4264}`,
4265 ExpectedBazelTargets: []string{
4266 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4267 "local_includes": `["."]`,
4268 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004269 "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"],
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004270 "//conditions:default": ["-android_thin_lto"],
4271 })`,
4272 }),
4273 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4274 "local_includes": `["."]`,
4275 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004276 "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"],
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00004277 "//conditions:default": ["-android_thin_lto"],
4278 })`,
4279 }),
4280 },
4281 })
4282}
4283
4284func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) {
4285 runCcLibraryTestCase(t, Bp2buildTestCase{
4286 Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables",
4287 ModuleTypeUnderTest: "cc_library",
4288 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4289 Blueprint: `
4290cc_library {
4291 name: "foo",
4292 lto: {
4293 thin: true,
4294 },
4295 whole_program_vtables: true,
4296}`,
4297 ExpectedBazelTargets: []string{
4298 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4299 "features": `[
4300 "android_thin_lto",
4301 "android_thin_lto_whole_program_vtables",
4302 ]`,
4303 "local_includes": `["."]`,
4304 }),
4305 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4306 "features": `[
4307 "android_thin_lto",
4308 "android_thin_lto_whole_program_vtables",
4309 ]`,
4310 "local_includes": `["."]`,
4311 }),
4312 },
4313 })
4314}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004315
4316func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) {
4317 runCcLibraryTestCase(t, Bp2buildTestCase{
4318 Description: "cc_library changes hidden visibility flag to feature",
4319 ModuleTypeUnderTest: "cc_library",
4320 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4321 Blueprint: `
4322cc_library {
4323 name: "foo",
4324 cflags: ["-fvisibility=hidden"],
4325}`,
4326 ExpectedBazelTargets: []string{
4327 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4328 "features": `["visibility_hidden"]`,
4329 "local_includes": `["."]`,
4330 }),
4331 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4332 "features": `["visibility_hidden"]`,
4333 "local_includes": `["."]`,
4334 }),
4335 },
4336 })
4337}
4338
4339func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) {
4340 runCcLibraryTestCase(t, Bp2buildTestCase{
4341 Description: "cc_library changes hidden visibility flag to feature when specific to shared variant",
4342 ModuleTypeUnderTest: "cc_library",
4343 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4344 Blueprint: `
4345cc_library {
4346 name: "foo",
4347 shared: {
4348 cflags: ["-fvisibility=hidden"],
4349 },
4350}`,
4351 ExpectedBazelTargets: []string{
4352 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4353 "local_includes": `["."]`,
4354 }),
4355 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4356 "features": `["visibility_hidden"]`,
4357 "local_includes": `["."]`,
4358 }),
4359 },
4360 })
4361}
4362
4363func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) {
4364 runCcLibraryTestCase(t, Bp2buildTestCase{
4365 Description: "cc_library changes hidden visibility flag to feature when specific to static variant",
4366 ModuleTypeUnderTest: "cc_library",
4367 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4368 Blueprint: `
4369cc_library {
4370 name: "foo",
4371 static: {
4372 cflags: ["-fvisibility=hidden"],
4373 },
4374}`,
4375 ExpectedBazelTargets: []string{
4376 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4377 "features": `["visibility_hidden"]`,
4378 "local_includes": `["."]`,
4379 }),
4380 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4381 "local_includes": `["."]`,
4382 }),
4383 },
4384 })
4385}
4386
4387func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
4388 runCcLibraryTestCase(t, Bp2buildTestCase{
4389 Description: "cc_library changes hidden visibility flag to feature when specific to an os",
4390 ModuleTypeUnderTest: "cc_library",
4391 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4392 Blueprint: `
4393cc_library {
4394 name: "foo",
4395 target: {
4396 android: {
4397 cflags: ["-fvisibility=hidden"],
4398 },
4399 },
4400}`,
4401 ExpectedBazelTargets: []string{
4402 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4403 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004404 "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"],
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004405 "//conditions:default": [],
4406 })`,
4407 "local_includes": `["."]`,
4408 }),
4409 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4410 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004411 "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"],
Trevor Radcliffea8b44162023-04-14 18:25:24 +00004412 "//conditions:default": [],
4413 })`,
4414 "local_includes": `["."]`,
4415 }),
4416 },
4417 })
4418}
Spandan Das4242f102023-04-19 22:31:54 +00004419
4420// Test that a config_setting specific to an apex is created by cc_library.
4421func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) {
4422 runCcLibraryTestCase(t, Bp2buildTestCase{
4423 Description: "cc_library creates a config_setting for each apex in apex_available",
4424 ModuleTypeUnderTest: "cc_library",
4425 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4426 Dir: "build/bazel/rules/apex",
4427 Blueprint: `
4428cc_library {
4429 name: "foo",
4430 apex_available: [
4431 "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex
4432 "myapex"
4433 ],
4434}`,
4435 ExpectedBazelTargets: []string{
4436 MakeBazelTargetNoRestrictions(
4437 "config_setting",
Spandan Das9cad90f2023-05-04 17:15:44 +00004438 "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004439 AttrNameToString{
4440 "flag_values": `{
Spandan Das9cad90f2023-05-04 17:15:44 +00004441 "//build/bazel/rules/apex:api_domain": "myapex",
Spandan Das4242f102023-04-19 22:31:54 +00004442 }`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004443 "constraint_values": `["//build/bazel_common_rules/platforms/os:android"]`,
Spandan Das4242f102023-04-19 22:31:54 +00004444 },
4445 ),
4446 },
4447 })
4448}
Yu Liu93893ba2023-05-01 13:49:52 -07004449
4450func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
4451 runCcLibraryTestCase(t, Bp2buildTestCase{
4452 Description: "cc_library cppflags in product variables",
4453 ModuleTypeUnderTest: "cc_library",
4454 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4455 Blueprint: soongCcLibraryPreamble + `cc_library {
4456 name: "a",
4457 srcs: ["a.cpp"],
4458 cppflags: [
4459 "-Wextra",
4460 "-DDEBUG_ONLY_CODE=0",
4461 ],
4462 product_variables: {
4463 eng: {
4464 cppflags: [
4465 "-UDEBUG_ONLY_CODE",
4466 "-DDEBUG_ONLY_CODE=1",
4467 ],
4468 },
4469 },
4470 include_build_directory: false,
4471}
4472`,
4473 ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
4474 "cppflags": `[
4475 "-Wextra",
4476 "-DDEBUG_ONLY_CODE=0",
4477 ] + select({
Cole Faust87c0c332023-07-31 12:10:12 -07004478 "//build/bazel/product_config/config_settings:eng": [
Yu Liu93893ba2023-05-01 13:49:52 -07004479 "-UDEBUG_ONLY_CODE",
4480 "-DDEBUG_ONLY_CODE=1",
4481 ],
4482 "//conditions:default": [],
4483 })`,
4484 "srcs": `["a.cpp"]`,
4485 }),
4486 },
4487 )
4488}
Spandan Dasdf4c2132023-05-09 23:58:52 +00004489
4490func TestCcLibraryYaccConversion(t *testing.T) {
4491 runCcLibraryTestCase(t, Bp2buildTestCase{
4492 Description: "cc_library is built from .y/.yy files",
4493 ModuleTypeUnderTest: "cc_library",
4494 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00004495 StubbedBuildDefinitions: []string{"staticlib", "sharedlib"},
Spandan Dasdf4c2132023-05-09 23:58:52 +00004496 Blueprint: soongCcLibraryPreamble + `cc_library {
4497 name: "a",
4498 srcs: [
4499 "a.cpp",
4500 "a.yy",
4501 ],
4502 shared_libs: ["sharedlib"],
4503 static_libs: ["staticlib"],
4504 yacc: {
4505 flags: ["someYaccFlag"],
4506 gen_location_hh: true,
4507 gen_position_hh: true,
4508 },
4509}
4510cc_library_static {
4511 name: "staticlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004512}
4513cc_library {
4514 name: "sharedlib",
Spandan Dasdf4c2132023-05-09 23:58:52 +00004515}
4516`,
4517 ExpectedBazelTargets: []string{
4518 MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
4519 "src": `"a.yy"`,
4520 "implementation_deps": `[":staticlib"]`,
4521 "implementation_dynamic_deps": `[":sharedlib"]`,
4522 "flags": `["someYaccFlag"]`,
4523 "gen_location_hh": "True",
4524 "gen_position_hh": "True",
4525 "local_includes": `["."]`,
4526 }),
4527 MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
4528 "srcs": `["a.cpp"]`,
4529 "implementation_deps": `[":staticlib"]`,
4530 "implementation_dynamic_deps": `[":sharedlib"]`,
4531 "implementation_whole_archive_deps": `[":a_yacc"]`,
4532 "local_includes": `["."]`,
4533 }),
4534 MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
4535 "srcs": `["a.cpp"]`,
4536 "implementation_deps": `[":staticlib"]`,
4537 "implementation_dynamic_deps": `[":sharedlib"]`,
4538 "implementation_whole_archive_deps": `[":a_yacc"]`,
4539 "local_includes": `["."]`,
4540 }),
4541 },
4542 })
4543}
Spandan Dasfb04c412023-05-15 18:35:36 +00004544
4545func TestCcLibraryHostLdLibs(t *testing.T) {
4546 runCcLibraryTestCase(t, Bp2buildTestCase{
4547 Description: "cc_binary linker flags for host_ldlibs",
4548 ModuleTypeUnderTest: "cc_binary",
4549 ModuleTypeUnderTestFactory: cc.BinaryFactory,
4550 Blueprint: soongCcLibraryPreamble + `cc_binary {
4551 name: "a",
4552 host_supported: true,
4553 ldflags: ["-lcommon"],
4554 target: {
4555 linux: {
4556 host_ldlibs: [
4557 "-llinux",
4558 ],
4559 },
4560 darwin: {
4561 ldflags: ["-ldarwinadditional"],
4562 host_ldlibs: [
4563 "-ldarwin",
4564 ],
4565 },
4566 windows: {
4567 host_ldlibs: [
4568 "-lwindows",
4569 ],
4570 },
4571 },
4572}
4573`,
4574 ExpectedBazelTargets: []string{
4575 MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{
4576 "linkopts": `["-lcommon"] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004577 "//build/bazel_common_rules/platforms/os:darwin": [
Spandan Dasfb04c412023-05-15 18:35:36 +00004578 "-ldarwinadditional",
4579 "-ldarwin",
4580 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004581 "//build/bazel_common_rules/platforms/os:linux_glibc": ["-llinux"],
4582 "//build/bazel_common_rules/platforms/os:windows": ["-lwindows"],
Spandan Dasfb04c412023-05-15 18:35:36 +00004583 "//conditions:default": [],
4584 })`,
4585 "local_includes": `["."]`,
4586 }),
4587 },
4588 })
4589}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004590
4591func TestCcLibraryWithCfi(t *testing.T) {
4592 runCcLibraryTestCase(t, Bp2buildTestCase{
4593 Description: "cc_library has correct features when cfi is enabled",
4594 ModuleTypeUnderTest: "cc_library",
4595 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4596 Blueprint: `
4597cc_library {
4598 name: "foo",
4599 sanitize: {
4600 cfi: true,
4601 },
4602}`,
4603 ExpectedBazelTargets: []string{
4604 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4605 "features": `["android_cfi"]`,
4606 "local_includes": `["."]`,
4607 }),
4608 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4609 "features": `["android_cfi"]`,
4610 "local_includes": `["."]`,
4611 }),
4612 },
4613 })
4614}
4615
4616func TestCcLibraryWithCfiOsSpecific(t *testing.T) {
4617 runCcLibraryTestCase(t, Bp2buildTestCase{
4618 Description: "cc_library has correct features when cfi is enabled for specific variants",
4619 ModuleTypeUnderTest: "cc_library",
4620 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4621 Blueprint: `
4622cc_library {
4623 name: "foo",
4624 target: {
4625 android: {
4626 sanitize: {
4627 cfi: true,
4628 },
4629 },
4630 },
4631}`,
4632 ExpectedBazelTargets: []string{
4633 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4634 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004635 "//build/bazel_common_rules/platforms/os:android": ["android_cfi"],
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004636 "//conditions:default": [],
4637 })`,
4638 "local_includes": `["."]`,
4639 }),
4640 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4641 "features": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004642 "//build/bazel_common_rules/platforms/os:android": ["android_cfi"],
Trevor Radcliffe27669c02023-03-28 20:47:10 +00004643 "//conditions:default": [],
4644 })`,
4645 "local_includes": `["."]`,
4646 }),
4647 },
4648 })
4649}
4650
4651func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) {
4652 runCcLibraryTestCase(t, Bp2buildTestCase{
4653 Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support",
4654 ModuleTypeUnderTest: "cc_library",
4655 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4656 Blueprint: `
4657cc_library {
4658 name: "foo",
4659 sanitize: {
4660 cfi: true,
4661 config: {
4662 cfi_assembly_support: true,
4663 },
4664 },
4665}`,
4666 ExpectedBazelTargets: []string{
4667 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4668 "features": `[
4669 "android_cfi",
4670 "android_cfi_assembly_support",
4671 ]`,
4672 "local_includes": `["."]`,
4673 }),
4674 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4675 "features": `[
4676 "android_cfi",
4677 "android_cfi_assembly_support",
4678 ]`,
4679 "local_includes": `["."]`,
4680 }),
4681 },
4682 })
4683}
Spandan Das39ccf932023-05-26 18:03:39 +00004684
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00004685func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
4686 runCcLibraryTestCase(t, Bp2buildTestCase{
4687 Description: "cc_library disables cfi when explciitly set to false in the bp",
4688 ModuleTypeUnderTest: "cc_library",
4689 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4690 Blueprint: `
4691cc_library {
4692 name: "foo",
4693 sanitize: {
4694 cfi: false,
4695 },
4696}
4697`,
4698 ExpectedBazelTargets: []string{
4699 MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
4700 "features": `["-android_cfi"]`,
4701 "local_includes": `["."]`,
4702 }),
4703 MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
4704 "features": `["-android_cfi"]`,
4705 "local_includes": `["."]`,
4706 }),
4707 },
4708 })
4709}
4710
Spandan Das39ccf932023-05-26 18:03:39 +00004711func TestCcLibraryWithStem(t *testing.T) {
4712 runCcLibraryTestCase(t, Bp2buildTestCase{
4713 Description: "cc_library with stem property",
4714 ModuleTypeUnderTest: "cc_library_shared",
4715 ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
4716 Blueprint: soongCcLibraryPreamble + `
4717cc_library_shared {
4718 name: "foo_with_stem_simple",
4719 stem: "foo",
4720}
4721cc_library_shared {
4722 name: "foo_with_arch_variant_stem",
4723 arch: {
4724 arm: {
4725 stem: "foo-arm",
4726 },
4727 arm64: {
4728 stem: "foo-arm64",
4729 },
4730 },
4731}
4732`,
4733 ExpectedBazelTargets: []string{
4734 MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{
4735 "stem": `"foo"`,
4736 "local_includes": `["."]`,
4737 }),
4738 MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{
4739 "stem": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00004740 "//build/bazel_common_rules/platforms/arch:arm": "foo-arm",
4741 "//build/bazel_common_rules/platforms/arch:arm64": "foo-arm64",
Spandan Das39ccf932023-05-26 18:03:39 +00004742 "//conditions:default": None,
4743 })`,
4744 "local_includes": `["."]`,
4745 }),
4746 },
4747 })
4748}
Spandan Dasc53767e2023-08-03 23:02:26 +00004749
4750// Bazel enforces that proto_library and the .proto file are in the same bazel package
4751func TestGenerateProtoLibraryInSamePackage(t *testing.T) {
4752 tc := Bp2buildTestCase{
4753 Description: "cc_library depends on .proto files from multiple packages",
4754 ModuleTypeUnderTest: "cc_library",
4755 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4756 Blueprint: `
4757cc_library_static {
4758 name: "foo",
4759 srcs: [
4760 "foo.proto",
4761 "bar/bar.proto", // Different package because there is a bar/Android.bp
4762 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4763 ],
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004764 proto: {
4765 canonical_path_from_root: true,
4766 }
Spandan Dasc53767e2023-08-03 23:02:26 +00004767}
Chris Parsonscd209032023-09-19 01:12:48 +00004768` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasc53767e2023-08-03 23:02:26 +00004769 Filesystem: map[string]string{
4770 "bar/Android.bp": "",
4771 "baz/subbaz/Android.bp": "",
4772 },
4773 }
4774
4775 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4776 // Root dir
4777 tc.ExpectedBazelTargets = []string{
4778 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4779 "local_includes": `["."]`,
4780 "deps": `[":libprotobuf-cpp-lite"]`,
4781 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4782 }),
4783 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4784 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004785 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004786 }),
4787 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4788 "deps": `[
4789 ":foo_proto",
4790 "//bar:foo_proto",
4791 "//baz/subbaz:foo_proto",
4792 ]`,
4793 }),
4794 }
4795 runCcLibraryTestCase(t, tc)
4796
4797 // bar dir
4798 tc.Dir = "bar"
4799 tc.ExpectedBazelTargets = []string{
4800 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004801 "srcs": `["//bar:bar.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004802 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004803 }),
4804 }
4805 runCcLibraryTestCase(t, tc)
4806
4807 // baz/subbaz dir
4808 tc.Dir = "baz/subbaz"
4809 tc.ExpectedBazelTargets = []string{
4810 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004811 "srcs": `["//baz/subbaz:baz.proto"]`,
Spandan Das215adb42023-08-14 16:52:24 +00004812 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004813 }),
4814 }
4815 runCcLibraryTestCase(t, tc)
4816}
4817
4818// Bazel enforces that proto_library and the .proto file are in the same bazel package
4819func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
4820 tc := Bp2buildTestCase{
4821 Description: "cc_library depends on .proto files from multiple packages",
4822 ModuleTypeUnderTest: "cc_library",
4823 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4824 Blueprint: `
4825cc_library_static {
4826 name: "foo",
4827 srcs: [
4828 "foo.proto",
4829 "bar/bar.proto", // Different package because there is a bar/Android.bp
4830 "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
4831 ],
4832 proto: {
4833 canonical_path_from_root: false,
4834 }
4835}
Chris Parsonscd209032023-09-19 01:12:48 +00004836` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004837 Filesystem: map[string]string{
4838 "bar/Android.bp": "",
4839 "baz/subbaz/Android.bp": "",
4840 },
4841 }
4842
4843 // We will run the test 3 times and check in the root, bar and baz/subbaz directories
4844 // Root dir
4845 tc.ExpectedBazelTargets = []string{
4846 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4847 "local_includes": `["."]`,
4848 "deps": `[":libprotobuf-cpp-lite"]`,
4849 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4850 }),
4851 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4852 "srcs": `["foo.proto"]`,
4853 "strip_import_prefix": `""`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004854 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004855 }),
4856 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4857 "deps": `[
4858 ":foo_proto",
4859 "//bar:foo_proto",
4860 "//baz/subbaz:foo_proto",
4861 ]`,
4862 }),
4863 }
4864 runCcLibraryTestCase(t, tc)
4865
4866 // bar dir
4867 tc.Dir = "bar"
4868 tc.ExpectedBazelTargets = []string{
4869 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4870 "srcs": `["//bar:bar.proto"]`,
4871 "strip_import_prefix": `""`,
4872 "import_prefix": `"bar"`,
Spandan Das215adb42023-08-14 16:52:24 +00004873 "tags": `["manual"]`,
Liz Kammer7dc6bcb2023-08-10 12:51:59 -04004874 }),
4875 }
4876 runCcLibraryTestCase(t, tc)
4877
4878 // baz/subbaz dir
4879 tc.Dir = "baz/subbaz"
4880 tc.ExpectedBazelTargets = []string{
4881 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4882 "srcs": `["//baz/subbaz:baz.proto"]`,
4883 "strip_import_prefix": `""`,
4884 "import_prefix": `"baz/subbaz"`,
Spandan Das215adb42023-08-14 16:52:24 +00004885 "tags": `["manual"]`,
Spandan Dasc53767e2023-08-03 23:02:26 +00004886 }),
4887 }
4888 runCcLibraryTestCase(t, tc)
4889}
Spandan Dasec39d512023-08-15 22:08:18 +00004890
4891func TestProtoIncludeDirs(t *testing.T) {
4892 tc := Bp2buildTestCase{
4893 Description: "cc_library depends on .proto files using proto.include_dirs",
4894 ModuleTypeUnderTest: "cc_library",
4895 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4896 Blueprint: `
4897cc_library_static {
4898 name: "foo",
4899 srcs: [
4900 "foo.proto",
4901 ],
4902 proto: {
4903 include_dirs: ["bar"],
4904 }
4905}
Chris Parsonscd209032023-09-19 01:12:48 +00004906` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasec39d512023-08-15 22:08:18 +00004907 Filesystem: map[string]string{
4908 "bar/Android.bp": "",
4909 "bar/bar.proto": "",
4910 "bar/baz/Android.bp": "",
4911 "bar/baz/baz.proto": "",
4912 },
4913 }
4914
4915 // We will run the test 3 times and check in the root, bar and bar/baz directories
4916 // Root dir
4917 tc.ExpectedBazelTargets = []string{
4918 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4919 "local_includes": `["."]`,
4920 "deps": `[":libprotobuf-cpp-lite"]`,
4921 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4922 }),
4923 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4924 "srcs": `["foo.proto"]`,
Spandan Dase0f2ed52023-08-22 18:19:44 +00004925 "tags": `["manual"]`,
Spandan Dasec39d512023-08-15 22:08:18 +00004926 }),
4927 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4928 "deps": `[":foo_proto"]`,
4929 "transitive_deps": `[
4930 "//bar:bar.include_dir_bp2build_generated_proto",
4931 "//bar/baz:bar.include_dir_bp2build_generated_proto",
4932 ]`,
4933 }),
4934 }
4935 runCcLibraryTestCase(t, tc)
4936
4937 // bar dir
4938 tc.Dir = "bar"
4939 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004940 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004941 "srcs": `["bar.proto"]`,
4942 "strip_import_prefix": `""`,
4943 "tags": `["manual"]`,
4944 }),
4945 }
4946 runCcLibraryTestCase(t, tc)
4947
4948 // bar/baz dir
4949 tc.Dir = "bar/baz"
4950 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00004951 MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Dasec39d512023-08-15 22:08:18 +00004952 "srcs": `["//bar/baz:baz.proto"]`,
4953 "strip_import_prefix": `""`,
4954 "import_prefix": `"baz"`,
4955 "tags": `["manual"]`,
4956 }),
4957 }
4958 runCcLibraryTestCase(t, tc)
4959}
Spandan Das4e5a1942023-08-22 19:20:39 +00004960
Spandan Dasf26ee152023-08-24 23:21:04 +00004961func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) {
4962 tc := Bp2buildTestCase{
4963 Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs",
4964 ModuleTypeUnderTest: "cc_library",
4965 ModuleTypeUnderTestFactory: cc.LibraryFactory,
4966 Blueprint: `
4967cc_library_static {
4968 name: "foo",
4969 srcs: [
4970 "foo.proto",
4971 "bar/bar.proto",
4972 ],
4973 proto: {
4974 include_dirs: ["baz"],
4975 }
4976}
Chris Parsonscd209032023-09-19 01:12:48 +00004977` + simpleModule("cc_library", "libprotobuf-cpp-lite"),
Spandan Dasf26ee152023-08-24 23:21:04 +00004978 Filesystem: map[string]string{
4979 "bar/Android.bp": "", // package boundary
4980 "baz/Android.bp": "",
4981 "baz/baz.proto": "",
4982 },
4983 }
4984
4985 tc.ExpectedBazelTargets = []string{
4986 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
4987 "local_includes": `["."]`,
4988 "deps": `[":libprotobuf-cpp-lite"]`,
4989 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
4990 }),
4991 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
4992 "srcs": `["foo.proto"]`,
4993 "tags": `["manual"]`,
4994 }),
4995 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
4996 "deps": `[
4997 ":foo_proto",
4998 "//bar:foo_proto",
4999 ]`,
5000 "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`,
5001 }),
5002 }
5003 runCcLibraryTestCase(t, tc)
5004
5005}
5006
Spandan Das4e5a1942023-08-22 19:20:39 +00005007func TestProtoLocalIncludeDirs(t *testing.T) {
5008 tc := Bp2buildTestCase{
5009 Description: "cc_library depends on .proto files using proto.local_include_dirs",
5010 ModuleTypeUnderTest: "cc_library",
5011 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005012 Blueprint: `
5013cc_library {
5014 name: "libprotobuf-cpp-lite",
5015 // TODO: b/285631638 - A stubbed proto library dependency does not work as a protolib
5016 // dependency of cc_library_static.
5017 bazel_module: { bp2build_available: false },
5018}
5019`,
Spandan Das4e5a1942023-08-22 19:20:39 +00005020 Filesystem: map[string]string{
5021 "foo/Android.bp": `cc_library_static {
5022 name: "foo",
5023 srcs: [
5024 "foo.proto",
5025 ],
5026 proto: {
5027 local_include_dirs: ["foo_subdir"],
5028 },
5029 bazel_module: { bp2build_available: true },
5030}`,
5031 "foo/foo.proto": "",
5032 "foo/foo_subdir/Android.bp": "",
5033 "foo/foo_subdir/foo_subdir.proto": "",
5034 },
5035 }
5036
5037 // We will run the test 2 times and check in foo and foo/foo_subdir directories
5038 // foo dir
5039 tc.Dir = "foo"
5040 tc.ExpectedBazelTargets = []string{
5041 MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
5042 "local_includes": `["."]`,
5043 "deps": `["//:libprotobuf-cpp-lite"]`,
5044 "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
5045 }),
5046 MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
5047 "srcs": `["foo.proto"]`,
5048 "tags": `["manual"]`,
5049 }),
5050 MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
5051 "deps": `[":foo_proto"]`,
5052 "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`,
5053 }),
5054 }
5055 runCcLibraryTestCase(t, tc)
5056
5057 // foo/foo_subdir
5058 tc.Dir = "foo/foo_subdir"
5059 tc.ExpectedBazelTargets = []string{
Spandan Dasf26ee152023-08-24 23:21:04 +00005060 MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{
Spandan Das4e5a1942023-08-22 19:20:39 +00005061 "srcs": `["foo_subdir.proto"]`,
5062 "strip_import_prefix": `""`,
5063 "tags": `["manual"]`,
5064 }),
5065 }
5066 runCcLibraryTestCase(t, tc)
5067}
Spandan Dasab29f572023-09-01 19:43:56 +00005068
5069// `foo_device` and `bar_host` can depend on .proto files of a specific dir,
5070// the dynamically generated proto_library should not have any target_compatible_with
5071func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) {
5072 tc := Bp2buildTestCase{
5073 Description: "proto_library generated for proto.include_dirs is compatible for all axes",
5074 ModuleTypeUnderTest: "cc_library",
5075 ModuleTypeUnderTestFactory: cc.LibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00005076 Blueprint: simpleModule("cc_library", "libprotobuf-cpp-lite") + `
Spandan Dasab29f572023-09-01 19:43:56 +00005077cc_library {
5078 name: "foo_device",
5079 device_supported: true, // this is the default behavior, but added explicitly here for illustration
5080 host_supported: false,
5081 proto: {include_dirs: ["dir"]},
5082}
5083cc_library {
5084 name: "bar_host",
5085 device_supported: false,
5086 host_supported: true,
5087 srcs: ["bar.proto"],
5088 proto: {include_dirs: ["dir"]},
5089}
5090`,
5091 Filesystem: map[string]string{
5092 "dir/Android.bp": "",
5093 "dir/dir.proto": "",
5094 },
5095 Dir: "dir", // check for the generated proto_library
5096 ExpectedBazelTargets: []string{
5097 MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{
5098 "srcs": `["dir.proto"]`,
5099 "strip_import_prefix": `""`,
5100 "tags": `["manual"]`,
5101 }),
5102 },
5103 }
5104 runCcLibraryTestCase(t, tc)
5105}
Spandan Dase1cb14b2023-09-05 19:31:12 +00005106
5107func TestCcCompileMultilibConversion(t *testing.T) {
5108 tc := Bp2buildTestCase{
5109 Description: "cc_library with compile_multilib",
5110 ModuleTypeUnderTest: "cc_library",
5111 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5112 Blueprint: `
5113cc_library {
5114 name: "lib32",
5115 compile_multilib: "32",
5116}
5117cc_library {
5118 name: "lib64",
5119 compile_multilib: "64",
5120}
5121`,
5122 ExpectedBazelTargets: []string{
5123 MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{
5124 "local_includes": `["."]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00005125 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({
5126 "//build/bazel_common_rules/platforms/arch:arm64": ["@platforms//:incompatible"],
5127 "//build/bazel_common_rules/platforms/arch:riscv64": ["@platforms//:incompatible"],
5128 "//build/bazel_common_rules/platforms/arch:x86_64": ["@platforms//:incompatible"],
Spandan Dase1cb14b2023-09-05 19:31:12 +00005129 "//conditions:default": [],
5130 })`,
5131 }),
5132 MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{
5133 "local_includes": `["."]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00005134 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({
5135 "//build/bazel_common_rules/platforms/arch:arm64": ["@platforms//:incompatible"],
5136 "//build/bazel_common_rules/platforms/arch:riscv64": ["@platforms//:incompatible"],
5137 "//build/bazel_common_rules/platforms/arch:x86_64": ["@platforms//:incompatible"],
Spandan Dase1cb14b2023-09-05 19:31:12 +00005138 "//conditions:default": [],
5139 })`,
5140 }),
5141 MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{
5142 "local_includes": `["."]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00005143 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({
5144 "//build/bazel_common_rules/platforms/arch:arm": ["@platforms//:incompatible"],
5145 "//build/bazel_common_rules/platforms/arch:x86": ["@platforms//:incompatible"],
Spandan Dase1cb14b2023-09-05 19:31:12 +00005146 "//conditions:default": [],
5147 })`,
5148 }),
5149 MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{
5150 "local_includes": `["."]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00005151 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({
5152 "//build/bazel_common_rules/platforms/arch:arm": ["@platforms//:incompatible"],
5153 "//build/bazel_common_rules/platforms/arch:x86": ["@platforms//:incompatible"],
Spandan Dase1cb14b2023-09-05 19:31:12 +00005154 "//conditions:default": [],
5155 })`,
5156 }),
5157 },
5158 }
5159 runCcLibraryTestCase(t, tc)
5160}
Spandan Das63acae92023-09-14 22:34:34 +00005161
5162func TestNdkLibraryConversion(t *testing.T) {
5163 tc := Bp2buildTestCase{
5164 Description: "ndk_library conversion",
5165 ModuleTypeUnderTest: "cc_library",
5166 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5167 Blueprint: `
5168cc_library {
5169 name: "libfoo",
Spandan Das63acae92023-09-14 22:34:34 +00005170}
5171ndk_library {
5172 name: "libfoo",
5173 first_version: "29",
5174 symbol_file: "libfoo.map.txt",
5175}
5176`,
Chris Parsons8a532b72023-09-27 23:11:26 +00005177 StubbedBuildDefinitions: []string{"libfoo"},
Spandan Das63acae92023-09-14 22:34:34 +00005178 ExpectedBazelTargets: []string{
5179 MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{
5180 "api_surface": `"publicapi"`,
Spandan Das17a27f02023-10-06 21:35:21 +00005181 "included_in_ndk": `True`,
Spandan Das63acae92023-09-14 22:34:34 +00005182 "soname": `"libfoo.so"`,
5183 "source_library_label": `"//:libfoo"`,
5184 "symbol_file": `"libfoo.map.txt"`,
5185 "versions": `[
5186 "29",
5187 "30",
5188 "S",
5189 "Tiramisu",
5190 "current",
5191 ]`,
5192 }),
5193 },
5194 }
5195 runCcLibraryTestCase(t, tc)
5196}
Spandan Das319711b2023-09-19 19:04:41 +00005197
5198func TestNdkHeadersConversion(t *testing.T) {
5199 tc := Bp2buildTestCase{
5200 Description: "ndk_headers conversion",
5201 ModuleTypeUnderTest: "ndk_headers",
5202 ModuleTypeUnderTestFactory: cc.NdkHeadersFactory,
5203 Blueprint: `
5204ndk_headers {
5205 name: "libfoo_headers",
5206 from: "from",
5207 to: "to",
Chris Parsonsd0783372023-10-05 15:47:07 +00005208 srcs: ["foo.h", "foo_other.h"]
Spandan Das319711b2023-09-19 19:04:41 +00005209}
5210`,
5211 ExpectedBazelTargets: []string{
5212 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5213 "strip_import_prefix": `"from"`,
5214 "import_prefix": `"to"`,
5215 "hdrs": `[
Chris Parsonsd0783372023-10-05 15:47:07 +00005216 "foo.h",
5217 "foo_other.h",
Spandan Das319711b2023-09-19 19:04:41 +00005218 ]`,
5219 }),
5220 },
5221 }
5222 runCcLibraryTestCase(t, tc)
5223}
Spandan Dasa7da3f02023-09-28 19:30:51 +00005224
5225func TestVersionedNdkHeadersConversion(t *testing.T) {
5226 tc := Bp2buildTestCase{
5227 Description: "versioned_ndk_headers conversion",
5228 ModuleTypeUnderTest: "versioned_ndk_headers",
5229 ModuleTypeUnderTestFactory: cc.VersionedNdkHeadersFactory,
5230 Blueprint: `
5231versioned_ndk_headers {
5232 name: "libfoo_headers",
5233 from: "from",
5234 to: "to",
5235}
5236`,
5237 Filesystem: map[string]string{
5238 "from/foo.h": "",
5239 "from/foo_other.h": "",
5240 },
5241 ExpectedBazelTargets: []string{
5242 MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{
5243 "strip_import_prefix": `"from"`,
5244 "import_prefix": `"to"`,
5245 "hdrs": `[
5246 "from/foo.h",
5247 "from/foo_other.h",
5248 ]`,
5249 "run_versioner": "True",
5250 }),
5251 },
5252 }
5253 runCcLibraryTestCase(t, tc)
5254}
Chris Parsons7123cc52023-10-04 22:27:40 +00005255
5256// Regression test for b/303307456.
5257// TODO: b/202299295 - Remove this test when cc rules have proper support
5258// for the `required` property
5259func TestCcModules_requiredProperty(t *testing.T) {
5260 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
5261 Description: "cc modules do not use the required property",
5262 Filesystem: map[string]string{
5263 "foo.c": "",
5264 "bar.c": "",
5265 },
5266 Blueprint: soongCcLibraryPreamble + `
5267cc_library {
5268 name: "foo_both",
5269 srcs: ["foo.c"],
5270 include_build_directory: false,
5271 required: ["bar"],
5272}
5273cc_library_shared {
5274 name: "foo_shared",
5275 srcs: ["foo.c"],
5276 include_build_directory: false,
5277 required: ["bar"],
5278}
5279cc_library_static {
5280 name: "foo_static",
5281 srcs: ["foo.c"],
5282 include_build_directory: false,
5283 required: ["bar"],
5284}
5285cc_library_static {
5286 name: "bar",
5287 srcs: ["bar.c"],
5288 include_build_directory: false,
5289}`,
5290 ExpectedBazelTargets: []string{
5291 MakeBazelTarget("cc_library_static", "foo_both_bp2build_cc_library_static", AttrNameToString{
5292 "srcs_c": `["foo.c"]`,
5293 }),
5294 MakeBazelTarget("cc_library_shared", "foo_both", AttrNameToString{
5295 "srcs_c": `["foo.c"]`,
5296 }),
5297 MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
5298 "srcs_c": `["foo.c"]`,
5299 }),
5300 MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
5301 "srcs_c": `["foo.c"]`,
5302 }),
5303 MakeBazelTarget("cc_library_static", "bar", AttrNameToString{
5304 "srcs_c": `["bar.c"]`,
5305 }),
5306 },
5307 })
5308}
Spandan Das17a27f02023-10-06 21:35:21 +00005309
5310func TestPropertiesIfStubLibraryIsInNdk(t *testing.T) {
5311 tc := Bp2buildTestCase{
5312 Description: "If an equivalent ndk_library exists, set included_in_ndk=true for module-libapi stubs",
5313 ModuleTypeUnderTest: "cc_library",
5314 ModuleTypeUnderTestFactory: cc.LibraryFactory,
5315 Blueprint: `
5316// libfoo is an ndk library and contributes to module-libapi
5317cc_library {
5318 name: "libfoo",
5319 stubs: {symbol_file: "libfoo.map.txt"},
5320}
5321ndk_library {
5322 name: "libfoo",
5323 first_version: "29",
5324 symbol_file: "libfoo.map.txt",
5325}
5326// libbar is not an ndk library, but contributes to module-libapi
5327cc_library {
5328 name: "libbar",
5329 stubs: {symbol_file: "libbar.map.txt"},
5330}
5331`,
5332 StubbedBuildDefinitions: []string{"libfoo.ndk"},
5333 ExpectedBazelTargets: []string{
5334 MakeBazelTarget("cc_library_static", "libfoo_bp2build_cc_library_static", AttrNameToString{
5335 "local_includes": `["."]`,
5336 }),
5337 MakeBazelTarget("cc_library_shared", "libfoo", AttrNameToString{
5338 "local_includes": `["."]`,
5339 "stubs_symbol_file": `"libfoo.map.txt"`,
5340 }),
5341 MakeBazelTarget("cc_stub_suite", "libfoo_stub_libs", AttrNameToString{
5342 "api_surface": `"module-libapi"`,
5343 "soname": `"libfoo.so"`,
5344 "source_library_label": `"//:libfoo"`,
5345 "symbol_file": `"libfoo.map.txt"`,
5346 "versions": `["current"]`,
5347 "included_in_ndk": `True`,
5348 }),
5349 MakeBazelTarget("cc_library_static", "libbar_bp2build_cc_library_static", AttrNameToString{
5350 "local_includes": `["."]`,
5351 }),
5352 MakeBazelTarget("cc_library_shared", "libbar", AttrNameToString{
5353 "local_includes": `["."]`,
5354 "stubs_symbol_file": `"libbar.map.txt"`,
5355 }),
5356 MakeBazelTarget("cc_stub_suite", "libbar_stub_libs", AttrNameToString{
5357 "api_surface": `"module-libapi"`,
5358 "soname": `"libbar.so"`,
5359 "source_library_label": `"//:libbar"`,
5360 "symbol_file": `"libbar.map.txt"`,
5361 "versions": `["current"]`,
5362 }),
5363 },
5364 }
5365 runCcLibraryTestCase(t, tc)
5366}