xref: /freebsd/contrib/googletest/BUILD.bazel (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1# Copyright 2017 Google Inc.
2# All Rights Reserved.
3#
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31#   Bazel Build for Google C++ Testing Framework(Google Test)
32
33package(default_visibility = ["//visibility:public"])
34
35licenses(["notice"])
36
37exports_files(["LICENSE"])
38
39config_setting(
40    name = "qnx",
41    constraint_values = ["@platforms//os:qnx"],
42)
43
44config_setting(
45    name = "windows",
46    constraint_values = ["@platforms//os:windows"],
47)
48
49config_setting(
50    name = "freebsd",
51    constraint_values = ["@platforms//os:freebsd"],
52)
53
54config_setting(
55    name = "openbsd",
56    constraint_values = ["@platforms//os:openbsd"],
57)
58
59# NOTE: Fuchsia is not an officially supported platform.
60config_setting(
61    name = "fuchsia",
62    constraint_values = ["@platforms//os:fuchsia"],
63)
64
65config_setting(
66    name = "msvc_compiler",
67    flag_values = {
68        "@bazel_tools//tools/cpp:compiler": "msvc-cl",
69    },
70    visibility = [":__subpackages__"],
71)
72
73config_setting(
74    name = "has_absl",
75    values = {"define": "absl=1"},
76)
77
78# Library that defines the FRIEND_TEST macro.
79cc_library(
80    name = "gtest_prod",
81    hdrs = ["googletest/include/gtest/gtest_prod.h"],
82    includes = ["googletest/include"],
83)
84
85# Google Test including Google Mock
86cc_library(
87    name = "gtest",
88    srcs = glob(
89        include = [
90            "googletest/src/*.cc",
91            "googletest/src/*.h",
92            "googletest/include/gtest/**/*.h",
93            "googlemock/src/*.cc",
94            "googlemock/include/gmock/**/*.h",
95        ],
96        exclude = [
97            "googletest/src/gtest-all.cc",
98            "googletest/src/gtest_main.cc",
99            "googlemock/src/gmock-all.cc",
100            "googlemock/src/gmock_main.cc",
101        ],
102    ),
103    hdrs = glob([
104        "googletest/include/gtest/*.h",
105        "googlemock/include/gmock/*.h",
106    ]),
107    copts = select({
108        ":qnx": [],
109        ":windows": [],
110        "//conditions:default": ["-pthread"],
111    }),
112    defines = select({
113        ":has_absl": ["GTEST_HAS_ABSL=1"],
114        "//conditions:default": [],
115    }),
116    features = select({
117        ":windows": ["windows_export_all_symbols"],
118        "//conditions:default": [],
119    }),
120    includes = [
121        "googlemock",
122        "googlemock/include",
123        "googletest",
124        "googletest/include",
125    ],
126    linkopts = select({
127        ":qnx": ["-lregex"],
128        ":windows": [],
129        ":freebsd": [
130            "-lm",
131            "-pthread",
132        ],
133        ":openbsd": [
134            "-lm",
135            "-pthread",
136        ],
137        "//conditions:default": ["-pthread"],
138    }),
139    deps = select({
140        ":has_absl": [
141            "@com_google_absl//absl/container:flat_hash_set",
142            "@com_google_absl//absl/debugging:failure_signal_handler",
143            "@com_google_absl//absl/debugging:stacktrace",
144            "@com_google_absl//absl/debugging:symbolize",
145            "@com_google_absl//absl/flags:flag",
146            "@com_google_absl//absl/flags:parse",
147            "@com_google_absl//absl/flags:reflection",
148            "@com_google_absl//absl/flags:usage",
149            "@com_google_absl//absl/strings",
150            "@com_google_absl//absl/types:any",
151            "@com_google_absl//absl/types:optional",
152            "@com_google_absl//absl/types:variant",
153            "@com_googlesource_code_re2//:re2",
154        ],
155        "//conditions:default": [],
156    }) + select({
157        # `gtest-death-test.cc` has `EXPECT_DEATH` that spawns a process,
158        # expects it to crash and inspects its logs with the given matcher,
159        # so that's why these libraries are needed.
160        # Otherwise, builds targeting Fuchsia would fail to compile.
161        ":fuchsia": [
162            "@fuchsia_sdk//pkg/fdio",
163            "@fuchsia_sdk//pkg/syslog",
164            "@fuchsia_sdk//pkg/zx",
165        ],
166        "//conditions:default": [],
167    }),
168)
169
170cc_library(
171    name = "gtest_main",
172    srcs = ["googlemock/src/gmock_main.cc"],
173    features = select({
174        ":windows": ["windows_export_all_symbols"],
175        "//conditions:default": [],
176    }),
177    deps = [":gtest"],
178)
179
180# The following rules build samples of how to use gTest.
181cc_library(
182    name = "gtest_sample_lib",
183    srcs = [
184        "googletest/samples/sample1.cc",
185        "googletest/samples/sample2.cc",
186        "googletest/samples/sample4.cc",
187    ],
188    hdrs = [
189        "googletest/samples/prime_tables.h",
190        "googletest/samples/sample1.h",
191        "googletest/samples/sample2.h",
192        "googletest/samples/sample3-inl.h",
193        "googletest/samples/sample4.h",
194    ],
195    features = select({
196        ":windows": ["windows_export_all_symbols"],
197        "//conditions:default": [],
198    }),
199)
200
201cc_test(
202    name = "gtest_samples",
203    size = "small",
204    # All Samples except:
205    #   sample9 (main)
206    #   sample10 (main and takes a command line option and needs to be separate)
207    srcs = [
208        "googletest/samples/sample1_unittest.cc",
209        "googletest/samples/sample2_unittest.cc",
210        "googletest/samples/sample3_unittest.cc",
211        "googletest/samples/sample4_unittest.cc",
212        "googletest/samples/sample5_unittest.cc",
213        "googletest/samples/sample6_unittest.cc",
214        "googletest/samples/sample7_unittest.cc",
215        "googletest/samples/sample8_unittest.cc",
216    ],
217    linkstatic = 0,
218    deps = [
219        "gtest_sample_lib",
220        ":gtest_main",
221    ],
222)
223
224cc_test(
225    name = "sample9_unittest",
226    size = "small",
227    srcs = ["googletest/samples/sample9_unittest.cc"],
228    deps = [":gtest"],
229)
230
231cc_test(
232    name = "sample10_unittest",
233    size = "small",
234    srcs = ["googletest/samples/sample10_unittest.cc"],
235    deps = [":gtest"],
236)
237