FindGTest.cmake 5.2 KB

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