FindGTest.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #.rst:
  2. # FindGTest
  3. # ---------
  4. #
  5. # Locate the Google C++ Testing Framework.
  6. #
  7. # Defines the following variables:
  8. #
  9. # ::
  10. #
  11. # GTEST_FOUND - Found the Google Testing framework
  12. # GTEST_INCLUDE_DIRS - Include directories
  13. #
  14. #
  15. #
  16. # Also defines the library variables below as normal variables. These
  17. # contain debug/optimized keywords when a debugging library is found.
  18. #
  19. # ::
  20. #
  21. # GTEST_BOTH_LIBRARIES - Both libgtest & libgtest-main
  22. # GTEST_LIBRARIES - libgtest
  23. # GTEST_MAIN_LIBRARIES - libgtest-main
  24. #
  25. #
  26. #
  27. # Accepts the following variables as input:
  28. #
  29. # ::
  30. #
  31. # GTEST_ROOT - (as a CMake or environment variable)
  32. # The root directory of the gtest install prefix
  33. #
  34. #
  35. #
  36. # ::
  37. #
  38. # GTEST_MSVC_SEARCH - If compiling with MSVC, this variable can be set to
  39. # "MD" or "MT" to enable searching a GTest build tree
  40. # (defaults: "MD")
  41. #
  42. #
  43. #
  44. # Example Usage:
  45. #
  46. # ::
  47. #
  48. # enable_testing()
  49. # find_package(GTest REQUIRED)
  50. # include_directories(${GTEST_INCLUDE_DIRS})
  51. #
  52. #
  53. #
  54. # ::
  55. #
  56. # add_executable(foo foo.cc)
  57. # target_link_libraries(foo ${GTEST_BOTH_LIBRARIES})
  58. #
  59. #
  60. #
  61. # ::
  62. #
  63. # add_test(AllTestsInFoo foo)
  64. #
  65. #
  66. #
  67. #
  68. #
  69. # If you would like each Google test to show up in CTest as a test you
  70. # may use the following macro. NOTE: It will slow down your tests by
  71. # running an executable for each test and test fixture. You will also
  72. # have to rerun CMake after adding or removing tests or test fixtures.
  73. #
  74. # GTEST_ADD_TESTS(executable extra_args ARGN)
  75. #
  76. # ::
  77. #
  78. # executable = The path to the test executable
  79. # extra_args = Pass a list of extra arguments to be passed to
  80. # executable enclosed in quotes (or "" for none)
  81. # ARGN = A list of source files to search for tests & test
  82. # fixtures. Or AUTO to find them from executable target.
  83. #
  84. #
  85. #
  86. # ::
  87. #
  88. # Example:
  89. # set(FooTestArgs --foo 1 --bar 2)
  90. # add_executable(FooTest FooUnitTest.cc)
  91. # GTEST_ADD_TESTS(FooTest "${FooTestArgs}" AUTO)
  92. #=============================================================================
  93. # Copyright 2009 Kitware, Inc.
  94. # Copyright 2009 Philip Lowman <[email protected]>
  95. # Copyright 2009 Daniel Blezek <[email protected]>
  96. #
  97. # Distributed under the OSI-approved BSD License (the "License");
  98. # see accompanying file Copyright.txt for details.
  99. #
  100. # This software is distributed WITHOUT ANY WARRANTY; without even the
  101. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  102. # See the License for more information.
  103. #=============================================================================
  104. # (To distribute this file outside of CMake, substitute the full
  105. # License text for the above reference.)
  106. #
  107. # Thanks to Daniel Blezek <[email protected]> for the GTEST_ADD_TESTS code
  108. function(GTEST_ADD_TESTS executable extra_args)
  109. if(NOT ARGN)
  110. message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
  111. endif()
  112. if(ARGN STREQUAL "AUTO")
  113. # obtain sources used for building that executable
  114. get_property(ARGN TARGET ${executable} PROPERTY SOURCES)
  115. endif()
  116. set(gtest_case_name_regex ".*\\( *([A-Za-z_0-9]+) *, *([A-Za-z_0-9]+) *\\).*")
  117. set(gtest_test_type_regex "(TYPED_TEST|TEST_?[FP]?)")
  118. foreach(source ${ARGN})
  119. file(READ "${source}" contents)
  120. string(REGEX MATCHALL "${gtest_test_type_regex} *\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
  121. foreach(hit ${found_tests})
  122. string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
  123. # Parameterized tests have a different signature for the filter
  124. if(${test_type} STREQUAL "TEST_P")
  125. string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" test_name ${hit})
  126. elseif(${test_type} STREQUAL "TEST_F" OR ${test_type} STREQUAL "TEST")
  127. string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" test_name ${hit})
  128. elseif(${test_type} STREQUAL "TYPED_TEST")
  129. string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" test_name ${hit})
  130. else()
  131. message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
  132. continue()
  133. endif()
  134. add_test(NAME ${test_name} COMMAND ${executable} --gtest_filter=${test_name} ${extra_args})
  135. endforeach()
  136. endforeach()
  137. endfunction()
  138. function(_gtest_append_debugs _endvar _library)
  139. if(${_library} AND ${_library}_DEBUG)
  140. set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
  141. else()
  142. set(_output ${${_library}})
  143. endif()
  144. set(${_endvar} ${_output} PARENT_SCOPE)
  145. endfunction()
  146. function(_gtest_find_library _name)
  147. find_library(${_name}
  148. NAMES ${ARGN}
  149. HINTS
  150. ENV GTEST_ROOT
  151. ${GTEST_ROOT}
  152. PATH_SUFFIXES ${_gtest_libpath_suffixes}
  153. )
  154. mark_as_advanced(${_name})
  155. endfunction()
  156. #
  157. if(NOT DEFINED GTEST_MSVC_SEARCH)
  158. set(GTEST_MSVC_SEARCH MD)
  159. endif()
  160. set(_gtest_libpath_suffixes lib)
  161. if(MSVC)
  162. if(GTEST_MSVC_SEARCH STREQUAL "MD")
  163. list(APPEND _gtest_libpath_suffixes
  164. msvc/gtest-md/Debug
  165. msvc/gtest-md/Release)
  166. elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
  167. list(APPEND _gtest_libpath_suffixes
  168. msvc/gtest/Debug
  169. msvc/gtest/Release)
  170. endif()
  171. endif()
  172. find_path(GTEST_INCLUDE_DIR gtest/gtest.h
  173. HINTS
  174. $ENV{GTEST_ROOT}/include
  175. ${GTEST_ROOT}/include
  176. )
  177. mark_as_advanced(GTEST_INCLUDE_DIR)
  178. if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
  179. # The provided /MD project files for Google Test add -md suffixes to the
  180. # library names.
  181. _gtest_find_library(GTEST_LIBRARY gtest-md gtest)
  182. _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
  183. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
  184. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
  185. else()
  186. _gtest_find_library(GTEST_LIBRARY gtest)
  187. _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
  188. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
  189. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
  190. endif()
  191. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  192. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
  193. if(GTEST_FOUND)
  194. set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
  195. _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
  196. _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
  197. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  198. endif()