FindGTest.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. foreach(source ${ARGN})
  118. file(READ "${source}" contents)
  119. string(REGEX MATCHALL "TEST_?[FP]?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
  120. foreach(hit ${found_tests})
  121. string(REGEX MATCH "TEST_?[FP]?" test_type ${hit})
  122. # Parameterized tests have a different signature for the filter
  123. if(${test_type} STREQUAL "TEST_P")
  124. string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" test_name ${hit})
  125. else()
  126. string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" test_name ${hit})
  127. endif()
  128. add_test(${test_name} ${executable} --gtest_filter=${test_name} ${extra_args})
  129. endforeach()
  130. endforeach()
  131. endfunction()
  132. function(_gtest_append_debugs _endvar _library)
  133. if(${_library} AND ${_library}_DEBUG)
  134. set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
  135. else()
  136. set(_output ${${_library}})
  137. endif()
  138. set(${_endvar} ${_output} PARENT_SCOPE)
  139. endfunction()
  140. function(_gtest_find_library _name)
  141. find_library(${_name}
  142. NAMES ${ARGN}
  143. HINTS
  144. ENV GTEST_ROOT
  145. ${GTEST_ROOT}
  146. PATH_SUFFIXES ${_gtest_libpath_suffixes}
  147. )
  148. mark_as_advanced(${_name})
  149. endfunction()
  150. #
  151. if(NOT DEFINED GTEST_MSVC_SEARCH)
  152. set(GTEST_MSVC_SEARCH MD)
  153. endif()
  154. set(_gtest_libpath_suffixes lib)
  155. if(MSVC)
  156. if(GTEST_MSVC_SEARCH STREQUAL "MD")
  157. list(APPEND _gtest_libpath_suffixes
  158. msvc/gtest-md/Debug
  159. msvc/gtest-md/Release)
  160. elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
  161. list(APPEND _gtest_libpath_suffixes
  162. msvc/gtest/Debug
  163. msvc/gtest/Release)
  164. endif()
  165. endif()
  166. find_path(GTEST_INCLUDE_DIR gtest/gtest.h
  167. HINTS
  168. $ENV{GTEST_ROOT}/include
  169. ${GTEST_ROOT}/include
  170. )
  171. mark_as_advanced(GTEST_INCLUDE_DIR)
  172. if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
  173. # The provided /MD project files for Google Test add -md suffixes to the
  174. # library names.
  175. _gtest_find_library(GTEST_LIBRARY gtest-md gtest)
  176. _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
  177. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
  178. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
  179. else()
  180. _gtest_find_library(GTEST_LIBRARY gtest)
  181. _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
  182. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
  183. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
  184. endif()
  185. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  186. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
  187. if(GTEST_FOUND)
  188. set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
  189. _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
  190. _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
  191. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  192. endif()