FindGTest.cmake 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # FindGTest
  5. # ---------
  6. #
  7. # Locate the Google C++ Testing Framework.
  8. #
  9. # Imported targets
  10. # ^^^^^^^^^^^^^^^^
  11. #
  12. # This module defines the following :prop_tgt:`IMPORTED` targets:
  13. #
  14. # ``GTest::GTest``
  15. # The Google Test ``gtest`` library, if found; adds Thread::Thread
  16. # automatically
  17. # ``GTest::Main``
  18. # The Google Test ``gtest_main`` library, if found
  19. #
  20. #
  21. # Result variables
  22. # ^^^^^^^^^^^^^^^^
  23. #
  24. # This module will set the following variables in your project:
  25. #
  26. # ``GTEST_FOUND``
  27. # Found the Google Testing framework
  28. # ``GTEST_INCLUDE_DIRS``
  29. # the directory containing the Google Test headers
  30. #
  31. # The library variables below are set as normal variables. These
  32. # contain debug/optimized keywords when a debugging library is found.
  33. #
  34. # ``GTEST_LIBRARIES``
  35. # The Google Test ``gtest`` library; note it also requires linking
  36. # with an appropriate thread library
  37. # ``GTEST_MAIN_LIBRARIES``
  38. # The Google Test ``gtest_main`` library
  39. # ``GTEST_BOTH_LIBRARIES``
  40. # Both ``gtest`` and ``gtest_main``
  41. #
  42. # Cache variables
  43. # ^^^^^^^^^^^^^^^
  44. #
  45. # The following cache variables may also be set:
  46. #
  47. # ``GTEST_ROOT``
  48. # The root directory of the Google Test installation (may also be
  49. # set as an environment variable)
  50. # ``GTEST_MSVC_SEARCH``
  51. # If compiling with MSVC, this variable can be set to ``MT`` or
  52. # ``MD`` (the default) to enable searching a GTest build tree
  53. #
  54. #
  55. # Example usage
  56. # ^^^^^^^^^^^^^
  57. #
  58. # ::
  59. #
  60. # enable_testing()
  61. # find_package(GTest REQUIRED)
  62. #
  63. # add_executable(foo foo.cc)
  64. # target_link_libraries(foo GTest::GTest GTest::Main)
  65. #
  66. # add_test(AllTestsInFoo foo)
  67. #
  68. #
  69. # Deeper integration with CTest
  70. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. #
  72. # If you would like each Google test to show up in CTest as a test you
  73. # may use the following macro::
  74. #
  75. # GTEST_ADD_TESTS(executable extra_args files...)
  76. #
  77. # ``executable``
  78. # the path to the test executable
  79. # ``extra_args``
  80. # a list of extra arguments to be passed to executable enclosed in
  81. # quotes (or ``""`` for none)
  82. # ``files...``
  83. # a list of source files to search for tests and test fixtures. Or
  84. # ``AUTO`` to find them from executable target
  85. #
  86. # However, note that this macro will slow down your tests by running
  87. # an executable for each test and test fixture.
  88. #
  89. # Example usage::
  90. #
  91. # set(FooTestArgs --foo 1 --bar 2)
  92. # add_executable(FooTest FooUnitTest.cc)
  93. # GTEST_ADD_TESTS(FooTest "${FooTestArgs}" AUTO)
  94. #
  95. # Thanks to Daniel Blezek <[email protected]> for the GTEST_ADD_TESTS code
  96. function(GTEST_ADD_TESTS executable extra_args)
  97. if(NOT ARGN)
  98. message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
  99. endif()
  100. if(ARGN STREQUAL "AUTO")
  101. # obtain sources used for building that executable
  102. get_property(ARGN TARGET ${executable} PROPERTY SOURCES)
  103. endif()
  104. set(gtest_case_name_regex ".*\\( *([A-Za-z_0-9]+) *, *([A-Za-z_0-9]+) *\\).*")
  105. set(gtest_test_type_regex "(TYPED_TEST|TEST_?[FP]?)")
  106. foreach(source ${ARGN})
  107. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${source})
  108. file(READ "${source}" contents)
  109. string(REGEX MATCHALL "${gtest_test_type_regex} *\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
  110. foreach(hit ${found_tests})
  111. string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
  112. # Parameterized tests have a different signature for the filter
  113. if("x${test_type}" STREQUAL "xTEST_P")
  114. string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" test_name ${hit})
  115. elseif("x${test_type}" STREQUAL "xTEST_F" OR "x${test_type}" STREQUAL "xTEST")
  116. string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" test_name ${hit})
  117. elseif("x${test_type}" STREQUAL "xTYPED_TEST")
  118. string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" test_name ${hit})
  119. else()
  120. message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
  121. continue()
  122. endif()
  123. add_test(NAME ${test_name} COMMAND ${executable} --gtest_filter=${test_name} ${extra_args})
  124. endforeach()
  125. endforeach()
  126. endfunction()
  127. function(_gtest_append_debugs _endvar _library)
  128. if(${_library} AND ${_library}_DEBUG)
  129. set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
  130. else()
  131. set(_output ${${_library}})
  132. endif()
  133. set(${_endvar} ${_output} PARENT_SCOPE)
  134. endfunction()
  135. function(_gtest_find_library _name)
  136. find_library(${_name}
  137. NAMES ${ARGN}
  138. HINTS
  139. ENV GTEST_ROOT
  140. ${GTEST_ROOT}
  141. PATH_SUFFIXES ${_gtest_libpath_suffixes}
  142. )
  143. mark_as_advanced(${_name})
  144. endfunction()
  145. #
  146. if(NOT DEFINED GTEST_MSVC_SEARCH)
  147. set(GTEST_MSVC_SEARCH MD)
  148. endif()
  149. set(_gtest_libpath_suffixes lib)
  150. if(MSVC)
  151. if(GTEST_MSVC_SEARCH STREQUAL "MD")
  152. list(APPEND _gtest_libpath_suffixes
  153. msvc/gtest-md/Debug
  154. msvc/gtest-md/Release
  155. msvc/x64/Debug
  156. msvc/x64/Release
  157. )
  158. elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
  159. list(APPEND _gtest_libpath_suffixes
  160. msvc/gtest/Debug
  161. msvc/gtest/Release
  162. msvc/x64/Debug
  163. msvc/x64/Release
  164. )
  165. endif()
  166. endif()
  167. find_path(GTEST_INCLUDE_DIR gtest/gtest.h
  168. HINTS
  169. $ENV{GTEST_ROOT}/include
  170. ${GTEST_ROOT}/include
  171. )
  172. mark_as_advanced(GTEST_INCLUDE_DIR)
  173. if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
  174. # The provided /MD project files for Google Test add -md suffixes to the
  175. # library names.
  176. _gtest_find_library(GTEST_LIBRARY gtest-md gtest)
  177. _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
  178. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
  179. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
  180. else()
  181. _gtest_find_library(GTEST_LIBRARY gtest)
  182. _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
  183. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
  184. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
  185. endif()
  186. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  187. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
  188. if(GTEST_FOUND)
  189. set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
  190. _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
  191. _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
  192. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  193. include(CMakeFindDependencyMacro)
  194. find_dependency(Threads)
  195. if(NOT TARGET GTest::GTest)
  196. add_library(GTest::GTest UNKNOWN IMPORTED)
  197. set_target_properties(GTest::GTest PROPERTIES
  198. INTERFACE_LINK_LIBRARIES "Threads::Threads")
  199. if(GTEST_INCLUDE_DIRS)
  200. set_target_properties(GTest::GTest PROPERTIES
  201. INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}")
  202. endif()
  203. if(EXISTS "${GTEST_LIBRARY}")
  204. set_target_properties(GTest::GTest PROPERTIES
  205. IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
  206. IMPORTED_LOCATION "${GTEST_LIBRARY}")
  207. endif()
  208. if(EXISTS "${GTEST_LIBRARY_RELEASE}")
  209. set_property(TARGET GTest::GTest APPEND PROPERTY
  210. IMPORTED_CONFIGURATIONS RELEASE)
  211. set_target_properties(GTest::GTest PROPERTIES
  212. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  213. IMPORTED_LOCATION_RELEASE "${GTEST_LIBRARY_RELEASE}")
  214. endif()
  215. if(EXISTS "${GTEST_LIBRARY_DEBUG}")
  216. set_property(TARGET GTest::GTest APPEND PROPERTY
  217. IMPORTED_CONFIGURATIONS DEBUG)
  218. set_target_properties(GTest::GTest PROPERTIES
  219. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
  220. IMPORTED_LOCATION_DEBUG "${GTEST_LIBRARY_DEBUG}")
  221. endif()
  222. endif()
  223. if(NOT TARGET GTest::Main)
  224. add_library(GTest::Main UNKNOWN IMPORTED)
  225. set_target_properties(GTest::Main PROPERTIES
  226. INTERFACE_LINK_LIBRARIES "GTest::GTest")
  227. if(EXISTS "${GTEST_MAIN_LIBRARY}")
  228. set_target_properties(GTest::Main PROPERTIES
  229. IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
  230. IMPORTED_LOCATION "${GTEST_MAIN_LIBRARY}")
  231. endif()
  232. if(EXISTS "${GTEST_MAIN_LIBRARY_RELEASE}")
  233. set_property(TARGET GTest::Main APPEND PROPERTY
  234. IMPORTED_CONFIGURATIONS RELEASE)
  235. set_target_properties(GTest::Main PROPERTIES
  236. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  237. IMPORTED_LOCATION_RELEASE "${GTEST_MAIN_LIBRARY_RELEASE}")
  238. endif()
  239. if(EXISTS "${GTEST_MAIN_LIBRARY_DEBUG}")
  240. set_property(TARGET GTest::Main APPEND PROPERTY
  241. IMPORTED_CONFIGURATIONS DEBUG)
  242. set_target_properties(GTest::Main PROPERTIES
  243. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
  244. IMPORTED_LOCATION_DEBUG "${GTEST_MAIN_LIBRARY_DEBUG}")
  245. endif()
  246. endif()
  247. endif()