Debian Codegraph @

cmake_modules/FindGMock.cmake

raw
1 #.rst:
2 # FindGMock
3 # ---------
4 #
5 # Locate the Google C++ Mocking Framework.
6 #
7 # Defines the following variables:
8 #
9 # ::
10 #
11 # GMOCK_FOUND - Found the Google Mocking framework
12 # GMOCK_INCLUDE_DIRS - Include directories
13 #
14 #
15 #
16 # Also defines the library variables below as normal variables. These
17 # contain debug/optimized keywords when a debugging library is found.
18 #
19 # ::
20 #
21 # GMOCK_LIBRARIES - libgmock
22 #
23 #
24 #
25 # Accepts the following variables as input:
26 #
27 # ::
28 #
29 # GMOCK_ROOT - (as a CMake or environment variable)
30 # The root directory of the gmock install prefix
31 #
32 #
33 #
34 # ::
35 #
36 # GMOCK_MSVC_SEARCH - If compiling with MSVC, this variable can be set to
37 # "MD" or "MT" to enable searching a GMock build tree
38 # (defaults: "MD")
39 #
40 #
41 #
42 # Example Usage:
43 #
44 # ::
45 #
46 # find_package(GMock REQUIRED)
47 # include_directories(${GMOCK_INCLUDE_DIRS})
48 #
49 #
50 #
51 # ::
52 #
53 # add_executable(foo foo.cc)
54 # target_link_libraries(foo ${GMOCK_LIBRARIES})
55 #
56 #
57 #
58 # ::
59 #
60 # add_test(AllMocksInFoo foo)
61 #
62 #
63 #
64 #
65 #
66 # If you would like each Google test to show up in CMock as a test you
67 # may use the following macro. NOTE: It will slow down your tests by
68 # running an executable for each test and test fixture. You will also
69 # have to rerun CMake after adding or removing tests or test fixtures.
70 #
71 # GMOCK_ADD_MOCKS(executable extra_args ARGN)
72 #
73 # ::
74 #
75 # executable = The path to the test executable
76 # extra_args = Pass a list of extra arguments to be passed to
77 # executable enclosed in quotes (or "" for none)
78 # ARGN = A list of source files to search for tests & test
79 # fixtures.
80 #
81 #
82 #
83 # ::
84 #
85 # Example:
86 # set(FooMockArgs --foo 1 --bar 2)
87 # add_executable(FooMock FooUnitMock.cc)
88 # GMOCK_ADD_MOCKS(FooMock "${FooMockArgs}" FooUnitMock.cc)
89
90 #=============================================================================
91 # Copyright 2009 Kitware, Inc.
92 # Copyright 2009 Philip Lowman <[email protected]>
93 # Copyright 2009 Daniel Blezek <[email protected]>
94 #
95 # Distributed under the OSI-approved BSD License (the "License");
96 # see accompanying file Copyright.txt for details.
97 #
98 # This software is distributed WITHOUT ANY WARRANTY; without even the
99 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
100 # See the License for more information.
101 #=============================================================================
102 # (To distribute this file outside of CMake, substitute the full
103 # License text for the above reference.)
104 #
105 # Thanks to Daniel Blezek <[email protected]> for the GMOCK_ADD_MOCKS code
106
107 function(GMOCK_ADD_MOCKS executable extra_args)
108 if(NOT ARGN)
109 message(FATAL_ERROR "Missing ARGN: Read the documentation for GMOCK_ADD_MOCKS")
110 endif()
111 foreach(source ${ARGN})
112 file(READ "${source}" contents)
113 string(REGEX MATCHALL "MOCK_?F?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
114 foreach(hit ${found_tests})
115 string(REGEX REPLACE ".*\\( *([A-Za-z_0-9]+), *([A-Za-z_0-9]+) *\\).*" "\\1.\\2" test_name ${hit})
116 add_test(${test_name} ${executable} --gmock_filter=${test_name} ${extra_args})
117 endforeach()
118 endforeach()
119 endfunction()
120
121 function(_gmock_append_debugs _endvar _library)
122 if(${_library} AND ${_library}_DEBUG)
123 set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
124 else()
125 set(_output ${${_library}})
126 endif()
127 set(${_endvar} ${_output} PARENT_SCOPE)
128 endfunction()
129
130 function(_gmock_find_library _name)
131 find_library(${_name}
132 NAMES ${ARGN}
133 HINTS
134 ENV GMOCK_ROOT
135 ${GMOCK_ROOT}
136 PATH_SUFFIXES ${_gmock_libpath_suffixes}
137 )
138 mark_as_advanced(${_name})
139 endfunction()
140
141 #
142
143 if(NOT DEFINED GMOCK_MSVC_SEARCH)
144 set(GMOCK_MSVC_SEARCH MD)
145 endif()
146
147 set(_gmock_libpath_suffixes lib)
148 if(MSVC)
149 if(GMOCK_MSVC_SEARCH STREQUAL "MD")
150 list(APPEND _gmock_libpath_suffixes
151 msvc/gmock-md/Debug
152 msvc/gmock-md/Release)
153 elseif(GMOCK_MSVC_SEARCH STREQUAL "MT")
154 list(APPEND _gmock_libpath_suffixes
155 msvc/gmock/Debug
156 msvc/gmock/Release)
157 endif()
158 endif()
159
160
161 find_path(GMOCK_INCLUDE_DIR gmock/gmock.h
162 HINTS
163 $ENV{GMOCK_ROOT}/include
164 ${GMOCK_ROOT}/include
165 )
166 mark_as_advanced(GMOCK_INCLUDE_DIR)
167
168 if(MSVC AND GMOCK_MSVC_SEARCH STREQUAL "MD")
169 # The provided /MD project files for Google Mock add -md suffixes to the
170 # library names.
171 _gmock_find_library(GMOCK_LIBRARY gmock-md gmock)
172 _gmock_find_library(GMOCK_LIBRARY_DEBUG gmock-mdd gmockd)
173 else()
174 _gmock_find_library(GMOCK_LIBRARY gmock)
175 _gmock_find_library(GMOCK_LIBRARY_DEBUG gmockd)
176 endif()
177
178 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMock DEFAULT_MSG GMOCK_LIBRARY GMOCK_INCLUDE_DIR)
179
180 if(GMOCK_FOUND)
181 set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIR})
182 _gmock_append_debugs(GMOCK_LIBRARIES GMOCK_LIBRARY)
183 endif()
184