FindGTest.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Locate the Google C++ Testing Framework.
  7. Imported targets
  8. ^^^^^^^^^^^^^^^^
  9. This module defines the following :prop_tgt:`IMPORTED` targets:
  10. ``GTest::GTest``
  11. The Google Test ``gtest`` library, if found; adds Thread::Thread
  12. automatically
  13. ``GTest::Main``
  14. The Google Test ``gtest_main`` library, if found
  15. Result variables
  16. ^^^^^^^^^^^^^^^^
  17. This module will set the following variables in your project:
  18. ``GTEST_FOUND``
  19. Found the Google Testing framework
  20. ``GTEST_INCLUDE_DIRS``
  21. the directory containing the Google Test headers
  22. The library variables below are set as normal variables. These
  23. contain debug/optimized keywords when a debugging library is found.
  24. ``GTEST_LIBRARIES``
  25. The Google Test ``gtest`` library; note it also requires linking
  26. with an appropriate thread library
  27. ``GTEST_MAIN_LIBRARIES``
  28. The Google Test ``gtest_main`` library
  29. ``GTEST_BOTH_LIBRARIES``
  30. Both ``gtest`` and ``gtest_main``
  31. Cache variables
  32. ^^^^^^^^^^^^^^^
  33. The following cache variables may also be set:
  34. ``GTEST_ROOT``
  35. The root directory of the Google Test installation (may also be
  36. set as an environment variable)
  37. ``GTEST_MSVC_SEARCH``
  38. If compiling with MSVC, this variable can be set to ``MT`` or
  39. ``MD`` (the default) to enable searching a GTest build tree
  40. Example usage
  41. ^^^^^^^^^^^^^
  42. ::
  43. enable_testing()
  44. find_package(GTest REQUIRED)
  45. add_executable(foo foo.cc)
  46. target_link_libraries(foo GTest::GTest GTest::Main)
  47. add_test(AllTestsInFoo foo)
  48. Deeper integration with CTest
  49. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  50. See :module:`GoogleTest` for information on the :command:`gtest_add_tests`
  51. and :command:`gtest_discover_tests` commands.
  52. #]=======================================================================]
  53. include(${CMAKE_CURRENT_LIST_DIR}/GoogleTest.cmake)
  54. function(__gtest_append_debugs _endvar _library)
  55. if(${_library} AND ${_library}_DEBUG)
  56. set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
  57. else()
  58. set(_output ${${_library}})
  59. endif()
  60. set(${_endvar} ${_output} PARENT_SCOPE)
  61. endfunction()
  62. function(__gtest_find_library _name)
  63. find_library(${_name}
  64. NAMES ${ARGN}
  65. HINTS
  66. ENV GTEST_ROOT
  67. ${GTEST_ROOT}
  68. PATH_SUFFIXES ${_gtest_libpath_suffixes}
  69. )
  70. mark_as_advanced(${_name})
  71. endfunction()
  72. macro(__gtest_determine_windows_library_type _var)
  73. if(EXISTS "${${_var}}")
  74. file(TO_NATIVE_PATH "${${_var}}" _lib_path)
  75. get_filename_component(_name "${${_var}}" NAME_WE)
  76. file(STRINGS "${${_var}}" _match REGEX "${_name}\\.dll" LIMIT_COUNT 1)
  77. if(NOT _match STREQUAL "")
  78. set(${_var}_TYPE SHARED PARENT_SCOPE)
  79. else()
  80. set(${_var}_TYPE UNKNOWN PARENT_SCOPE)
  81. endif()
  82. return()
  83. endif()
  84. endmacro()
  85. function(__gtest_determine_library_type _var)
  86. if(WIN32)
  87. # For now, at least, only Windows really needs to know the library type
  88. __gtest_determine_windows_library_type(${_var})
  89. __gtest_determine_windows_library_type(${_var}_RELEASE)
  90. __gtest_determine_windows_library_type(${_var}_DEBUG)
  91. endif()
  92. # If we get here, no determination was made from the above checks
  93. set(${_var}_TYPE UNKNOWN PARENT_SCOPE)
  94. endfunction()
  95. function(__gtest_import_library _target _var _config)
  96. if(_config)
  97. set(_config_suffix "_${_config}")
  98. else()
  99. set(_config_suffix "")
  100. endif()
  101. set(_lib "${${_var}${_config_suffix}}")
  102. if(EXISTS "${_lib}")
  103. if(_config)
  104. set_property(TARGET ${_target} APPEND PROPERTY
  105. IMPORTED_CONFIGURATIONS ${_config})
  106. endif()
  107. set_target_properties(${_target} PROPERTIES
  108. IMPORTED_LINK_INTERFACE_LANGUAGES${_config_suffix} "CXX")
  109. if(WIN32 AND ${_var}_TYPE STREQUAL SHARED)
  110. set_target_properties(${_target} PROPERTIES
  111. IMPORTED_IMPLIB${_config_suffix} "${_lib}")
  112. else()
  113. set_target_properties(${_target} PROPERTIES
  114. IMPORTED_LOCATION${_config_suffix} "${_lib}")
  115. endif()
  116. endif()
  117. endfunction()
  118. #
  119. if(NOT DEFINED GTEST_MSVC_SEARCH)
  120. set(GTEST_MSVC_SEARCH MD)
  121. endif()
  122. set(_gtest_libpath_suffixes lib)
  123. if(MSVC)
  124. if(GTEST_MSVC_SEARCH STREQUAL "MD")
  125. list(APPEND _gtest_libpath_suffixes
  126. msvc/gtest-md/Debug
  127. msvc/gtest-md/Release
  128. msvc/x64/Debug
  129. msvc/x64/Release
  130. msvc/2010/gtest-md/Win32-Debug
  131. msvc/2010/gtest-md/Win32-Release
  132. msvc/2010/gtest-md/x64-Debug
  133. msvc/2010/gtest-md/x64-Release
  134. )
  135. elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
  136. list(APPEND _gtest_libpath_suffixes
  137. msvc/gtest/Debug
  138. msvc/gtest/Release
  139. msvc/x64/Debug
  140. msvc/x64/Release
  141. msvc/2010/gtest/Win32-Debug
  142. msvc/2010/gtest/Win32-Release
  143. msvc/2010/gtest/x64-Debug
  144. msvc/2010/gtest/x64-Release
  145. )
  146. endif()
  147. endif()
  148. find_path(GTEST_INCLUDE_DIR gtest/gtest.h
  149. HINTS
  150. $ENV{GTEST_ROOT}/include
  151. ${GTEST_ROOT}/include
  152. )
  153. mark_as_advanced(GTEST_INCLUDE_DIR)
  154. if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
  155. # The provided /MD project files for Google Test add -md suffixes to the
  156. # library names.
  157. __gtest_find_library(GTEST_LIBRARY gtest-md gtest)
  158. __gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
  159. __gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
  160. __gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
  161. else()
  162. __gtest_find_library(GTEST_LIBRARY gtest)
  163. __gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
  164. __gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
  165. __gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
  166. endif()
  167. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  168. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
  169. if(GTEST_FOUND)
  170. set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
  171. __gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
  172. __gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
  173. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  174. find_package(Threads QUIET)
  175. if(NOT TARGET GTest::GTest)
  176. __gtest_determine_library_type(GTEST_LIBRARY)
  177. add_library(GTest::GTest ${GTEST_LIBRARY_TYPE} IMPORTED)
  178. if(TARGET Threads::Threads)
  179. set_target_properties(GTest::GTest PROPERTIES
  180. INTERFACE_LINK_LIBRARIES Threads::Threads)
  181. endif()
  182. if(GTEST_LIBRARY_TYPE STREQUAL "SHARED")
  183. set_target_properties(GTest::GTest PROPERTIES
  184. INTERFACE_COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  185. endif()
  186. if(GTEST_INCLUDE_DIRS)
  187. set_target_properties(GTest::GTest PROPERTIES
  188. INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}")
  189. endif()
  190. __gtest_import_library(GTest::GTest GTEST_LIBRARY "")
  191. __gtest_import_library(GTest::GTest GTEST_LIBRARY "RELEASE")
  192. __gtest_import_library(GTest::GTest GTEST_LIBRARY "DEBUG")
  193. endif()
  194. if(NOT TARGET GTest::Main)
  195. __gtest_determine_library_type(GTEST_MAIN_LIBRARY)
  196. add_library(GTest::Main ${GTEST_MAIN_LIBRARY_TYPE} IMPORTED)
  197. set_target_properties(GTest::Main PROPERTIES
  198. INTERFACE_LINK_LIBRARIES "GTest::GTest")
  199. __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "")
  200. __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "RELEASE")
  201. __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "DEBUG")
  202. endif()
  203. endif()