BUILD.bazel 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. package(default_visibility = ["//visibility:public"])
  33. licenses(["notice"])
  34. exports_files(["LICENSE"])
  35. config_setting(
  36. name = "qnx",
  37. constraint_values = ["@platforms//os:qnx"],
  38. )
  39. config_setting(
  40. name = "windows",
  41. constraint_values = ["@platforms//os:windows"],
  42. )
  43. config_setting(
  44. name = "freebsd",
  45. constraint_values = ["@platforms//os:freebsd"],
  46. )
  47. config_setting(
  48. name = "openbsd",
  49. constraint_values = ["@platforms//os:openbsd"],
  50. )
  51. # NOTE: Fuchsia is not an officially supported platform.
  52. config_setting(
  53. name = "fuchsia",
  54. constraint_values = ["@platforms//os:fuchsia"],
  55. )
  56. config_setting(
  57. name = "msvc_compiler",
  58. flag_values = {
  59. "@bazel_tools//tools/cpp:compiler": "msvc-cl",
  60. },
  61. visibility = [":__subpackages__"],
  62. )
  63. config_setting(
  64. name = "has_absl",
  65. values = {"define": "absl=1"},
  66. )
  67. # Library that defines the FRIEND_TEST macro.
  68. cc_library(
  69. name = "gtest_prod",
  70. hdrs = ["googletest/include/gtest/gtest_prod.h"],
  71. includes = ["googletest/include"],
  72. )
  73. # Google Test including Google Mock
  74. cc_library(
  75. name = "gtest",
  76. srcs = glob(
  77. include = [
  78. "googletest/src/*.cc",
  79. "googletest/src/*.h",
  80. "googletest/include/gtest/**/*.h",
  81. "googlemock/src/*.cc",
  82. "googlemock/include/gmock/**/*.h",
  83. ],
  84. exclude = [
  85. "googletest/src/gtest-all.cc",
  86. "googletest/src/gtest_main.cc",
  87. "googlemock/src/gmock-all.cc",
  88. "googlemock/src/gmock_main.cc",
  89. ],
  90. ),
  91. hdrs = glob([
  92. "googletest/include/gtest/*.h",
  93. "googlemock/include/gmock/*.h",
  94. ]),
  95. copts = select({
  96. ":qnx": [],
  97. ":windows": [],
  98. "//conditions:default": ["-pthread"],
  99. }),
  100. defines = select({
  101. ":has_absl": ["GTEST_HAS_ABSL=1"],
  102. "//conditions:default": [],
  103. }),
  104. features = select({
  105. ":windows": ["windows_export_all_symbols"],
  106. "//conditions:default": [],
  107. }),
  108. includes = [
  109. "googlemock",
  110. "googlemock/include",
  111. "googletest",
  112. "googletest/include",
  113. ],
  114. linkopts = select({
  115. ":qnx": ["-lregex"],
  116. ":windows": [],
  117. ":freebsd": [
  118. "-lm",
  119. "-pthread",
  120. ],
  121. ":openbsd": [
  122. "-lm",
  123. "-pthread",
  124. ],
  125. "//conditions:default": ["-pthread"],
  126. }),
  127. deps = select({
  128. ":has_absl": [
  129. "@com_google_absl//absl/container:flat_hash_set",
  130. "@com_google_absl//absl/debugging:failure_signal_handler",
  131. "@com_google_absl//absl/debugging:stacktrace",
  132. "@com_google_absl//absl/debugging:symbolize",
  133. "@com_google_absl//absl/flags:flag",
  134. "@com_google_absl//absl/flags:parse",
  135. "@com_google_absl//absl/flags:reflection",
  136. "@com_google_absl//absl/flags:usage",
  137. "@com_google_absl//absl/strings",
  138. "@com_google_absl//absl/types:any",
  139. "@com_google_absl//absl/types:optional",
  140. "@com_google_absl//absl/types:variant",
  141. "@com_googlesource_code_re2//:re2",
  142. ],
  143. "//conditions:default": [],
  144. }) + select({
  145. # `gtest-death-test.cc` has `EXPECT_DEATH` that spawns a process,
  146. # expects it to crash and inspects its logs with the given matcher,
  147. # so that's why these libraries are needed.
  148. # Otherwise, builds targeting Fuchsia would fail to compile.
  149. ":fuchsia": [
  150. "@fuchsia_sdk//pkg/fdio",
  151. "@fuchsia_sdk//pkg/syslog",
  152. "@fuchsia_sdk//pkg/zx",
  153. ],
  154. "//conditions:default": [],
  155. }),
  156. )
  157. cc_library(
  158. name = "gtest_main",
  159. srcs = ["googlemock/src/gmock_main.cc"],
  160. features = select({
  161. ":windows": ["windows_export_all_symbols"],
  162. "//conditions:default": [],
  163. }),
  164. deps = [":gtest"],
  165. )
  166. # The following rules build samples of how to use gTest.
  167. cc_library(
  168. name = "gtest_sample_lib",
  169. srcs = [
  170. "googletest/samples/sample1.cc",
  171. "googletest/samples/sample2.cc",
  172. "googletest/samples/sample4.cc",
  173. ],
  174. hdrs = [
  175. "googletest/samples/prime_tables.h",
  176. "googletest/samples/sample1.h",
  177. "googletest/samples/sample2.h",
  178. "googletest/samples/sample3-inl.h",
  179. "googletest/samples/sample4.h",
  180. ],
  181. features = select({
  182. ":windows": ["windows_export_all_symbols"],
  183. "//conditions:default": [],
  184. }),
  185. )
  186. cc_test(
  187. name = "gtest_samples",
  188. size = "small",
  189. # All Samples except:
  190. # sample9 (main)
  191. # sample10 (main and takes a command line option and needs to be separate)
  192. srcs = [
  193. "googletest/samples/sample1_unittest.cc",
  194. "googletest/samples/sample2_unittest.cc",
  195. "googletest/samples/sample3_unittest.cc",
  196. "googletest/samples/sample4_unittest.cc",
  197. "googletest/samples/sample5_unittest.cc",
  198. "googletest/samples/sample6_unittest.cc",
  199. "googletest/samples/sample7_unittest.cc",
  200. "googletest/samples/sample8_unittest.cc",
  201. ],
  202. linkstatic = 0,
  203. deps = [
  204. "gtest_sample_lib",
  205. ":gtest_main",
  206. ],
  207. )
  208. cc_test(
  209. name = "sample9_unittest",
  210. size = "small",
  211. srcs = ["googletest/samples/sample9_unittest.cc"],
  212. deps = [":gtest"],
  213. )
  214. cc_test(
  215. name = "sample10_unittest",
  216. size = "small",
  217. srcs = ["googletest/samples/sample10_unittest.cc"],
  218. deps = [":gtest"],
  219. )