FindGTest.cmake 9.0 KB

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