1 # Locate the Google C++ Testing Framework.
3 # Defines the following variables:
5 # GTEST_FOUND - Found the Google Testing framework
6 # GTEST_INCLUDE_DIRS - Include directories
8 # Also defines the library variables below as normal
9 # variables. These contain debug/optimized keywords when
10 # a debugging library is found.
12 # GTEST_BOTH_LIBRARIES - Both libgtest & libgtest-main
13 # GTEST_LIBRARIES - libgtest
14 # GTEST_MAIN_LIBRARIES - libgtest-main
16 # Accepts the following variables as input:
18 # GTEST_ROOT - (as a CMake or environment variable)
19 # The root directory of the gtest install prefix
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
25 #-----------------------
29 # find_package(GTest REQUIRED)
30 # include_directories(${GTEST_INCLUDE_DIRS})
32 # add_executable(foo foo.cc)
33 # target_link_libraries(foo ${GTEST_BOTH_LIBRARIES})
35 # add_test(AllTestsInFoo foo)
37 #-----------------------
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.
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
53 # set(FooTestArgs --foo 1 --bar 2)
54 # add_executable(FooTest FooUnitTest.cc)
55 # GTEST_ADD_TESTS(FooTest "${FooTestArgs}" FooUnitTest.cc)
57 #=============================================================================
58 # Copyright 2009 Kitware, Inc.
59 # Copyright 2009 Philip Lowman <philip@yhbt.com>
60 # Copyright 2009 Daniel Blezek <blezek@gmail.com>
62 # Distributed under the OSI-approved BSD License (the "License");
63 # see accompanying file Copyright.txt for details.
65 # This software is distributed WITHOUT ANY WARRANTY; without even the
66 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
67 # See the License for more information.
68 #=============================================================================
69 # (To distribute this file outside of CMake, substitute the full
70 # License text for the above reference.)
72 # Thanks to Daniel Blezek <blezek@gmail.com> for the GTEST_ADD_TESTS code
74 function(GTEST_ADD_TESTS executable extra_args)
76 message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
78 foreach(source ${ARGN})
79 file(READ "${source}" contents)
80 string(REGEX MATCHALL "TEST_?F?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
81 foreach(hit ${found_tests})
82 string(REGEX REPLACE ".*\\( *([A-Za-z_0-9]+), *([A-Za-z_0-9]+) *\\).*" "\\1.\\2" test_name ${hit})
83 add_test(${test_name} ${executable} --gtest_filter=${test_name} ${extra_args})
88 function(_gtest_append_debugs _endvar _library)
89 if(${_library} AND ${_library}_DEBUG)
90 set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
92 set(_output ${${_library}})
94 set(${_endvar} ${_output} PARENT_SCOPE)
97 function(_gtest_find_library _name)
103 PATH_SUFFIXES ${_gtest_libpath_suffixes}
105 mark_as_advanced(${_name})
110 if(NOT DEFINED GTEST_MSVC_SEARCH)
111 set(GTEST_MSVC_SEARCH MD)
114 set(_gtest_libpath_suffixes lib)
116 if(GTEST_MSVC_SEARCH STREQUAL "MD")
117 list(APPEND _gtest_libpath_suffixes
119 msvc/gtest-md/Release)
120 elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
121 list(APPEND _gtest_libpath_suffixes
128 find_path(GTEST_INCLUDE_DIR gtest/gtest.h
130 $ENV{GTEST_ROOT}/include
131 ${GTEST_ROOT}/include
133 mark_as_advanced(GTEST_INCLUDE_DIR)
135 if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
136 # The provided /MD project files for Google Test add -md suffixes to the
138 _gtest_find_library(GTEST_LIBRARY gtest-md gtest)
139 _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd)
140 _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main)
141 _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind)
143 _gtest_find_library(GTEST_LIBRARY gtest)
144 _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd)
145 _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main)
146 _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind)
149 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
150 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
153 set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
154 _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY)
155 _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY)
156 set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})