FindGTest.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Locate the Google C++ Testing Framework.
  2. #
  3. # Defines the following variables:
  4. #
  5. # GTEST_FOUND - Found the Google Testing framework
  6. # GTEST_INCLUDE_DIRS - Include directories
  7. #
  8. # Also defines the library variables below as normal
  9. # variables. These contain debug/optimized keywords when
  10. # a debugging library is found.
  11. #
  12. # GTEST_BOTH_LIBRARIES - Both libgtest & libgtest-main
  13. # GTEST_LIBRARIES - libgtest
  14. # GTEST_MAIN_LIBRARIES - libgtest-main
  15. #
  16. # Accepts the following variables as input:
  17. #
  18. # GTEST_ROOT - (as a CMake or environment variable)
  19. # The root directory of the gtest install prefix
  20. #
  21. # GTEST_MSVC_SEARCH - If compiling with MSVC, this variable can be set to
  22. # "MD" or "MT" to enable searching a GTest build tree
  23. # (defaults: "MD")
  24. #
  25. #-----------------------
  26. # Example Usage:
  27. #
  28. # enable_testing()
  29. # find_package(GTest REQUIRED)
  30. # include_directories(${GTEST_INCLUDE_DIRS})
  31. #
  32. # add_executable(foo foo.cc)
  33. # target_link_libraries(foo ${GTEST_BOTH_LIBRARIES})
  34. #
  35. # add_test(AllTestsInFoo foo)
  36. #
  37. #-----------------------
  38. #
  39. # If you would like each Google test to show up in CTest as
  40. # a test you may use the following macro.
  41. # NOTE: It will slow down your tests by running an executable
  42. # for each test and test fixture. You will also have to rerun
  43. # CMake after adding or removing tests or test fixtures.
  44. #
  45. # GTEST_ADD_TESTS(executable extra_args ARGN)
  46. # executable = The path to the test executable
  47. # extra_args = Pass a list of extra arguments to be passed to
  48. # executable enclosed in quotes (or "" for none)
  49. # ARGN = A list of source files to search for tests & test
  50. # fixtures.
  51. #
  52. # Example:
  53. # set(FooTestArgs --foo 1 --bar 2)
  54. # add_executable(FooTest FooUnitTest.cc)
  55. # GTEST_ADD_TESTS(FooTest "${FooTestArgs}" FooUnitTest.cc)
  56. #=============================================================================
  57. # Copyright 2009 Kitware, Inc.
  58. # Copyright 2009 Philip Lowman <[email protected]>
  59. # Copyright 2009 Daniel Blezek <[email protected]>
  60. #
  61. # Distributed under the OSI-approved BSD License (the "License");
  62. # see accompanying file Copyright.txt for details.
  63. #
  64. # This software is distributed WITHOUT ANY WARRANTY; without even the
  65. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  66. # See the License for more information.
  67. #=============================================================================
  68. # (To distribute this file outside of CMake, substitute the full
  69. # License text for the above reference.)
  70. #
  71. # Thanks to Daniel Blezek <[email protected]> for the GTEST_ADD_TESTS code
  72. function(GTEST_ADD_TESTS executable extra_args)
  73. if(NOT ARGN)
  74. message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
  75. endif()
  76. foreach(source ${ARGN})
  77. file(READ "${source}" contents)
  78. string(REGEX MATCHALL "TEST_?F?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
  79. foreach(hit ${found_tests})
  80. string(REGEX REPLACE ".*\\( *([A-Za-z_0-9]+), *([A-Za-z_0-9]+) *\\).*" "\\1.\\2" test_name ${hit})
  81. add_test(${test_name} ${executable} --gtest_filter=${test_name} ${extra_args})
  82. endforeach()
  83. endforeach()
  84. endfunction()
  85. function(_gtest_append_debugs _endvar _library)
  86. if(${_library} AND ${_library}_DEBUG)
  87. set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
  88. else()
  89. set(_output ${${_library}})
  90. endif()
  91. set(${_endvar} ${_output} PARENT_SCOPE)
  92. endfunction()
  93. function(_gtest_find_library _name)
  94. find_library(${_name}
  95. NAMES ${ARGN}
  96. HINTS
  97. $ENV{GTEST_ROOT}
  98. ${GTEST_ROOT}
  99. PATH_SUFFIXES ${_gtest_libpath_suffixes}
  100. )
  101. mark_as_advanced(${_name})
  102. endfunction()
  103. #
  104. if(NOT DEFINED GTEST_MSVC_SEARCH)
  105. set(GTEST_MSVC_SEARCH MD)
  106. endif()
  107. set(_gtest_libpath_suffixes lib)
  108. if(MSVC)
  109. if(GTEST_MSVC_SEARCH STREQUAL "MD")
  110. list(APPEND _gtest_libpath_suffixes
  111. msvc/gtest-md/Debug
  112. msvc/gtest-md/Release)
  113. elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
  114. list(APPEND _gtest_libpath_suffixes
  115. msvc/gtest/Debug
  116. msvc/gtest/Release)
  117. endif()
  118. endif()
  119. find_path(GTEST_INCLUDE_DIR gtest/gtest.h
  120. HINTS
  121. $ENV{GTEST_ROOT}/include
  122. ${GTEST_ROOT}/include
  123. )
  124. mark_as_advanced(GTEST_INCLUDE_DIR)
  125. if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
  126. # The provided /MD project files for Google Test add -md suffixes to the
  127. # library names.
  128. _gtest_find_library(GTEST_LIBRARY gtest-md gtest)
  129. _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
  130. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
  131. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
  132. else()
  133. _gtest_find_library(GTEST_LIBRARY gtest)
  134. _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
  135. _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
  136. _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
  137. endif()
  138. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  139. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
  140. if(GTEST_FOUND)
  141. set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
  142. _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
  143. _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
  144. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  145. endif()