FindGTest.cmake 7.5 KB

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