FindGTest.cmake 5.7 KB

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