xref: /freebsd-src/contrib/googletest/BUILD.bazel (revision 5ca8c28cd8c725b81781201cfdb5f9969396f934)
1b89a7cc2SEnji Cooper# Copyright 2017 Google Inc.
2b89a7cc2SEnji Cooper# All Rights Reserved.
3b89a7cc2SEnji Cooper#
4b89a7cc2SEnji Cooper#
5b89a7cc2SEnji Cooper# Redistribution and use in source and binary forms, with or without
6b89a7cc2SEnji Cooper# modification, are permitted provided that the following conditions are
7b89a7cc2SEnji Cooper# met:
8b89a7cc2SEnji Cooper#
9b89a7cc2SEnji Cooper#     * Redistributions of source code must retain the above copyright
10b89a7cc2SEnji Cooper# notice, this list of conditions and the following disclaimer.
11b89a7cc2SEnji Cooper#     * Redistributions in binary form must reproduce the above
12b89a7cc2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
13b89a7cc2SEnji Cooper# in the documentation and/or other materials provided with the
14b89a7cc2SEnji Cooper# distribution.
15b89a7cc2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
16b89a7cc2SEnji Cooper# contributors may be used to endorse or promote products derived from
17b89a7cc2SEnji Cooper# this software without specific prior written permission.
18b89a7cc2SEnji Cooper#
19b89a7cc2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20b89a7cc2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21b89a7cc2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22b89a7cc2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23b89a7cc2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24b89a7cc2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25b89a7cc2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26b89a7cc2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27b89a7cc2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28b89a7cc2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29b89a7cc2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30b89a7cc2SEnji Cooper#
31b89a7cc2SEnji Cooper#   Bazel Build for Google C++ Testing Framework(Google Test)
32b89a7cc2SEnji Cooper
33b89a7cc2SEnji Cooperpackage(default_visibility = ["//visibility:public"])
34b89a7cc2SEnji Cooper
35b89a7cc2SEnji Cooperlicenses(["notice"])
36b89a7cc2SEnji Cooper
3728f6c2f2SEnji Cooperexports_files(["LICENSE"])
3828f6c2f2SEnji Cooper
39b89a7cc2SEnji Cooperconfig_setting(
4028f6c2f2SEnji Cooper    name = "qnx",
4128f6c2f2SEnji Cooper    constraint_values = ["@platforms//os:qnx"],
42b89a7cc2SEnji Cooper)
43b89a7cc2SEnji Cooper
44b89a7cc2SEnji Cooperconfig_setting(
4528f6c2f2SEnji Cooper    name = "windows",
4628f6c2f2SEnji Cooper    constraint_values = ["@platforms//os:windows"],
4728f6c2f2SEnji Cooper)
4828f6c2f2SEnji Cooper
4928f6c2f2SEnji Cooperconfig_setting(
5028f6c2f2SEnji Cooper    name = "freebsd",
5128f6c2f2SEnji Cooper    constraint_values = ["@platforms//os:freebsd"],
5228f6c2f2SEnji Cooper)
5328f6c2f2SEnji Cooper
5428f6c2f2SEnji Cooperconfig_setting(
5528f6c2f2SEnji Cooper    name = "openbsd",
5628f6c2f2SEnji Cooper    constraint_values = ["@platforms//os:openbsd"],
5728f6c2f2SEnji Cooper)
5828f6c2f2SEnji Cooper
59*5ca8c28cSEnji Cooper# NOTE: Fuchsia is not an officially supported platform.
60*5ca8c28cSEnji Cooperconfig_setting(
61*5ca8c28cSEnji Cooper    name = "fuchsia",
62*5ca8c28cSEnji Cooper    constraint_values = ["@platforms//os:fuchsia"],
63*5ca8c28cSEnji Cooper)
64*5ca8c28cSEnji Cooper
6528f6c2f2SEnji Cooperconfig_setting(
6628f6c2f2SEnji Cooper    name = "msvc_compiler",
6728f6c2f2SEnji Cooper    flag_values = {
6828f6c2f2SEnji Cooper        "@bazel_tools//tools/cpp:compiler": "msvc-cl",
6928f6c2f2SEnji Cooper    },
7028f6c2f2SEnji Cooper    visibility = [":__subpackages__"],
71b89a7cc2SEnji Cooper)
72b89a7cc2SEnji Cooper
73b89a7cc2SEnji Cooperconfig_setting(
74b89a7cc2SEnji Cooper    name = "has_absl",
75b89a7cc2SEnji Cooper    values = {"define": "absl=1"},
76b89a7cc2SEnji Cooper)
77b89a7cc2SEnji Cooper
7828f6c2f2SEnji Cooper# Library that defines the FRIEND_TEST macro.
7928f6c2f2SEnji Coopercc_library(
8028f6c2f2SEnji Cooper    name = "gtest_prod",
8128f6c2f2SEnji Cooper    hdrs = ["googletest/include/gtest/gtest_prod.h"],
8228f6c2f2SEnji Cooper    includes = ["googletest/include"],
8328f6c2f2SEnji Cooper)
8428f6c2f2SEnji Cooper
85b89a7cc2SEnji Cooper# Google Test including Google Mock
86b89a7cc2SEnji Coopercc_library(
87b89a7cc2SEnji Cooper    name = "gtest",
88b89a7cc2SEnji Cooper    srcs = glob(
89b89a7cc2SEnji Cooper        include = [
90b89a7cc2SEnji Cooper            "googletest/src/*.cc",
91b89a7cc2SEnji Cooper            "googletest/src/*.h",
92b89a7cc2SEnji Cooper            "googletest/include/gtest/**/*.h",
93b89a7cc2SEnji Cooper            "googlemock/src/*.cc",
94b89a7cc2SEnji Cooper            "googlemock/include/gmock/**/*.h",
95b89a7cc2SEnji Cooper        ],
96b89a7cc2SEnji Cooper        exclude = [
97b89a7cc2SEnji Cooper            "googletest/src/gtest-all.cc",
98b89a7cc2SEnji Cooper            "googletest/src/gtest_main.cc",
99b89a7cc2SEnji Cooper            "googlemock/src/gmock-all.cc",
100b89a7cc2SEnji Cooper            "googlemock/src/gmock_main.cc",
101b89a7cc2SEnji Cooper        ],
102b89a7cc2SEnji Cooper    ),
103b89a7cc2SEnji Cooper    hdrs = glob([
104b89a7cc2SEnji Cooper        "googletest/include/gtest/*.h",
105b89a7cc2SEnji Cooper        "googlemock/include/gmock/*.h",
106b89a7cc2SEnji Cooper    ]),
10728f6c2f2SEnji Cooper    copts = select({
10828f6c2f2SEnji Cooper        ":qnx": [],
109b89a7cc2SEnji Cooper        ":windows": [],
110b89a7cc2SEnji Cooper        "//conditions:default": ["-pthread"],
11128f6c2f2SEnji Cooper    }),
11228f6c2f2SEnji Cooper    defines = select({
11328f6c2f2SEnji Cooper        ":has_absl": ["GTEST_HAS_ABSL=1"],
114b89a7cc2SEnji Cooper        "//conditions:default": [],
11528f6c2f2SEnji Cooper    }),
11628f6c2f2SEnji Cooper    features = select({
11728f6c2f2SEnji Cooper        ":windows": ["windows_export_all_symbols"],
11828f6c2f2SEnji Cooper        "//conditions:default": [],
11928f6c2f2SEnji Cooper    }),
120b89a7cc2SEnji Cooper    includes = [
121b89a7cc2SEnji Cooper        "googlemock",
122b89a7cc2SEnji Cooper        "googlemock/include",
123b89a7cc2SEnji Cooper        "googletest",
124b89a7cc2SEnji Cooper        "googletest/include",
125b89a7cc2SEnji Cooper    ],
126b89a7cc2SEnji Cooper    linkopts = select({
12728f6c2f2SEnji Cooper        ":qnx": ["-lregex"],
128b89a7cc2SEnji Cooper        ":windows": [],
12928f6c2f2SEnji Cooper        ":freebsd": [
13028f6c2f2SEnji Cooper            "-lm",
131b89a7cc2SEnji Cooper            "-pthread",
132b89a7cc2SEnji Cooper        ],
13328f6c2f2SEnji Cooper        ":openbsd": [
13428f6c2f2SEnji Cooper            "-lm",
13528f6c2f2SEnji Cooper            "-pthread",
13628f6c2f2SEnji Cooper        ],
13728f6c2f2SEnji Cooper        "//conditions:default": ["-pthread"],
138b89a7cc2SEnji Cooper    }),
13928f6c2f2SEnji Cooper    deps = select({
140b89a7cc2SEnji Cooper        ":has_absl": [
14128f6c2f2SEnji Cooper            "@com_google_absl//absl/container:flat_hash_set",
142b89a7cc2SEnji Cooper            "@com_google_absl//absl/debugging:failure_signal_handler",
143b89a7cc2SEnji Cooper            "@com_google_absl//absl/debugging:stacktrace",
144b89a7cc2SEnji Cooper            "@com_google_absl//absl/debugging:symbolize",
14528f6c2f2SEnji Cooper            "@com_google_absl//absl/flags:flag",
14628f6c2f2SEnji Cooper            "@com_google_absl//absl/flags:parse",
14728f6c2f2SEnji Cooper            "@com_google_absl//absl/flags:reflection",
14828f6c2f2SEnji Cooper            "@com_google_absl//absl/flags:usage",
149b89a7cc2SEnji Cooper            "@com_google_absl//absl/strings",
15028f6c2f2SEnji Cooper            "@com_google_absl//absl/types:any",
151b89a7cc2SEnji Cooper            "@com_google_absl//absl/types:optional",
152b89a7cc2SEnji Cooper            "@com_google_absl//absl/types:variant",
15328f6c2f2SEnji Cooper            "@com_googlesource_code_re2//:re2",
154b89a7cc2SEnji Cooper        ],
155b89a7cc2SEnji Cooper        "//conditions:default": [],
156*5ca8c28cSEnji Cooper    }) + select({
157*5ca8c28cSEnji Cooper        # `gtest-death-test.cc` has `EXPECT_DEATH` that spawns a process,
158*5ca8c28cSEnji Cooper        # expects it to crash and inspects its logs with the given matcher,
159*5ca8c28cSEnji Cooper        # so that's why these libraries are needed.
160*5ca8c28cSEnji Cooper        # Otherwise, builds targeting Fuchsia would fail to compile.
161*5ca8c28cSEnji Cooper        ":fuchsia": [
162*5ca8c28cSEnji Cooper            "@fuchsia_sdk//pkg/fdio",
163*5ca8c28cSEnji Cooper            "@fuchsia_sdk//pkg/syslog",
164*5ca8c28cSEnji Cooper            "@fuchsia_sdk//pkg/zx",
165*5ca8c28cSEnji Cooper        ],
166*5ca8c28cSEnji Cooper        "//conditions:default": [],
16728f6c2f2SEnji Cooper    }),
168b89a7cc2SEnji Cooper)
169b89a7cc2SEnji Cooper
170b89a7cc2SEnji Coopercc_library(
171b89a7cc2SEnji Cooper    name = "gtest_main",
17228f6c2f2SEnji Cooper    srcs = ["googlemock/src/gmock_main.cc"],
17328f6c2f2SEnji Cooper    features = select({
17428f6c2f2SEnji Cooper        ":windows": ["windows_export_all_symbols"],
17528f6c2f2SEnji Cooper        "//conditions:default": [],
17628f6c2f2SEnji Cooper    }),
177b89a7cc2SEnji Cooper    deps = [":gtest"],
178b89a7cc2SEnji Cooper)
179b89a7cc2SEnji Cooper
180b89a7cc2SEnji Cooper# The following rules build samples of how to use gTest.
181b89a7cc2SEnji Coopercc_library(
182b89a7cc2SEnji Cooper    name = "gtest_sample_lib",
183b89a7cc2SEnji Cooper    srcs = [
184b89a7cc2SEnji Cooper        "googletest/samples/sample1.cc",
185b89a7cc2SEnji Cooper        "googletest/samples/sample2.cc",
186b89a7cc2SEnji Cooper        "googletest/samples/sample4.cc",
187b89a7cc2SEnji Cooper    ],
188b89a7cc2SEnji Cooper    hdrs = [
189b89a7cc2SEnji Cooper        "googletest/samples/prime_tables.h",
190b89a7cc2SEnji Cooper        "googletest/samples/sample1.h",
191b89a7cc2SEnji Cooper        "googletest/samples/sample2.h",
192b89a7cc2SEnji Cooper        "googletest/samples/sample3-inl.h",
193b89a7cc2SEnji Cooper        "googletest/samples/sample4.h",
194b89a7cc2SEnji Cooper    ],
19528f6c2f2SEnji Cooper    features = select({
19628f6c2f2SEnji Cooper        ":windows": ["windows_export_all_symbols"],
19728f6c2f2SEnji Cooper        "//conditions:default": [],
19828f6c2f2SEnji Cooper    }),
199b89a7cc2SEnji Cooper)
200b89a7cc2SEnji Cooper
201b89a7cc2SEnji Coopercc_test(
202b89a7cc2SEnji Cooper    name = "gtest_samples",
203b89a7cc2SEnji Cooper    size = "small",
204b89a7cc2SEnji Cooper    # All Samples except:
205b89a7cc2SEnji Cooper    #   sample9 (main)
206b89a7cc2SEnji Cooper    #   sample10 (main and takes a command line option and needs to be separate)
207b89a7cc2SEnji Cooper    srcs = [
208b89a7cc2SEnji Cooper        "googletest/samples/sample1_unittest.cc",
209b89a7cc2SEnji Cooper        "googletest/samples/sample2_unittest.cc",
210b89a7cc2SEnji Cooper        "googletest/samples/sample3_unittest.cc",
211b89a7cc2SEnji Cooper        "googletest/samples/sample4_unittest.cc",
212b89a7cc2SEnji Cooper        "googletest/samples/sample5_unittest.cc",
213b89a7cc2SEnji Cooper        "googletest/samples/sample6_unittest.cc",
214b89a7cc2SEnji Cooper        "googletest/samples/sample7_unittest.cc",
215b89a7cc2SEnji Cooper        "googletest/samples/sample8_unittest.cc",
216b89a7cc2SEnji Cooper    ],
21728f6c2f2SEnji Cooper    linkstatic = 0,
218b89a7cc2SEnji Cooper    deps = [
219b89a7cc2SEnji Cooper        "gtest_sample_lib",
220b89a7cc2SEnji Cooper        ":gtest_main",
221b89a7cc2SEnji Cooper    ],
222b89a7cc2SEnji Cooper)
223b89a7cc2SEnji Cooper
224b89a7cc2SEnji Coopercc_test(
225b89a7cc2SEnji Cooper    name = "sample9_unittest",
226b89a7cc2SEnji Cooper    size = "small",
227b89a7cc2SEnji Cooper    srcs = ["googletest/samples/sample9_unittest.cc"],
228b89a7cc2SEnji Cooper    deps = [":gtest"],
229b89a7cc2SEnji Cooper)
230b89a7cc2SEnji Cooper
231b89a7cc2SEnji Coopercc_test(
232b89a7cc2SEnji Cooper    name = "sample10_unittest",
233b89a7cc2SEnji Cooper    size = "small",
234b89a7cc2SEnji Cooper    srcs = ["googletest/samples/sample10_unittest.cc"],
23528f6c2f2SEnji Cooper    deps = [":gtest"],
236b89a7cc2SEnji Cooper)
237