Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FindGTest.cmake
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 #=============================================================================
58 # Copyright 2009 Kitware, Inc.
59 # Copyright 2009 Philip Lowman <philip@yhbt.com>
60 # Copyright 2009 Daniel Blezek <blezek@gmail.com>
61 #
62 # Distributed under the OSI-approved BSD License (the "License");
63 # see accompanying file Copyright.txt for details.
64 #
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.)
71 #
72 # Thanks to Daniel Blezek <blezek@gmail.com> for the GTEST_ADD_TESTS code
73
74 function(GTEST_ADD_TESTS executable extra_args)
75     if(NOT ARGN)
76         message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
77     endif()
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})
84         endforeach()
85     endforeach()
86 endfunction()
87
88 function(_gtest_append_debugs _endvar _library)
89     if(${_library} AND ${_library}_DEBUG)
90         set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
91     else()
92         set(_output ${${_library}})
93     endif()
94     set(${_endvar} ${_output} PARENT_SCOPE)
95 endfunction()
96
97 function(_gtest_find_library _name)
98     find_library(${_name}
99         NAMES ${ARGN}
100         HINTS
101             ENV GTEST_ROOT
102             ${GTEST_ROOT}
103         PATH_SUFFIXES ${_gtest_libpath_suffixes}
104     )
105     mark_as_advanced(${_name})
106 endfunction()
107
108 #
109
110 if(NOT DEFINED GTEST_MSVC_SEARCH)
111     set(GTEST_MSVC_SEARCH MD)
112 endif()
113
114 set(_gtest_libpath_suffixes lib)
115 if(MSVC)
116     if(GTEST_MSVC_SEARCH STREQUAL "MD")
117         list(APPEND _gtest_libpath_suffixes
118             msvc/gtest-md/Debug
119             msvc/gtest-md/Release)
120     elseif(GTEST_MSVC_SEARCH STREQUAL "MT")
121         list(APPEND _gtest_libpath_suffixes
122             msvc/gtest/Debug
123             msvc/gtest/Release)
124     endif()
125 endif()
126
127
128 find_path(GTEST_INCLUDE_DIR gtest/gtest.h
129     HINTS
130         $ENV{GTEST_ROOT}/include
131         ${GTEST_ROOT}/include
132 )
133 mark_as_advanced(GTEST_INCLUDE_DIR)
134
135 if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD")
136     # The provided /MD project files for Google Test add -md suffixes to the
137     # library names.
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)
142 else()
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)
147 endif()
148
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)
151
152 if(GTEST_FOUND)
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})
157 endif()
158