OLD | NEW |
| (Empty) |
1 # Defines functions and macros useful for building Google Test and | |
2 # Google Mock. | |
3 # | |
4 # Note: | |
5 # | |
6 # - This file will be run twice when building Google Mock (once via | |
7 # Google Test's CMakeLists.txt, and once via Google Mock's). | |
8 # Therefore it shouldn't have any side effects other than defining | |
9 # the functions and macros. | |
10 # | |
11 # - The functions/macros defined in this file may depend on Google | |
12 # Test and Google Mock's option() definitions, and thus must be | |
13 # called *after* the options have been defined. | |
14 | |
15 # Tweaks CMake's default compiler/linker settings to suit Google Test's needs. | |
16 # | |
17 # This must be a macro(), as inside a function string() can only | |
18 # update variables in the function scope. | |
19 macro(fix_default_compiler_settings_) | |
20 if (MSVC) | |
21 # For MSVC, CMake sets certain flags to defaults we want to override. | |
22 # This replacement code is taken from sample in the CMake Wiki at | |
23 # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace. | |
24 foreach (flag_var | |
25 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE | |
26 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) | |
27 if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt) | |
28 # When Google Test is built as a shared library, it should also use | |
29 # shared runtime libraries. Otherwise, it may end up with multiple | |
30 # copies of runtime library data in different modules, resulting in | |
31 # hard-to-find crashes. When it is built as a static library, it is | |
32 # preferable to use CRT as static libraries, as we don't have to rely | |
33 # on CRT DLLs being available. CMake always defaults to using shared | |
34 # CRT libraries, so we override that default here. | |
35 string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}") | |
36 endif() | |
37 | |
38 # We prefer more strict warning checking for building Google Test. | |
39 # Replaces /W3 with /W4 in defaults. | |
40 string(REPLACE "/W3" "-W4" ${flag_var} "${${flag_var}}") | |
41 endforeach() | |
42 endif() | |
43 endmacro() | |
44 | |
45 # Defines the compiler/linker flags used to build Google Test and | |
46 # Google Mock. You can tweak these definitions to suit your need. A | |
47 # variable's value is empty before it's explicitly assigned to. | |
48 macro(config_compiler_and_linker) | |
49 if (NOT gtest_disable_pthreads) | |
50 # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. | |
51 find_package(Threads) | |
52 endif() | |
53 | |
54 fix_default_compiler_settings_() | |
55 if (MSVC) | |
56 # Newlines inside flags variables break CMake's NMake generator. | |
57 # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds. | |
58 set(cxx_base_flags "-GS -W4 -WX -wd4127 -wd4251 -wd4275 -nologo -J -Zi") | |
59 set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32"
) | |
60 set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") | |
61 set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") | |
62 set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0") | |
63 set(cxx_no_rtti_flags "-GR-") | |
64 elseif (CMAKE_COMPILER_IS_GNUCXX) | |
65 set(cxx_base_flags "-Wall -Wshadow") | |
66 set(cxx_exception_flags "-fexceptions") | |
67 set(cxx_no_exception_flags "-fno-exceptions") | |
68 # Until version 4.3.2, GCC doesn't define a macro to indicate | |
69 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI | |
70 # explicitly. | |
71 set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") | |
72 set(cxx_strict_flags "-Wextra") | |
73 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") | |
74 set(cxx_exception_flags "-features=except") | |
75 # Sun Pro doesn't provide macros to indicate whether exceptions and | |
76 # RTTI are enabled, so we define GTEST_HAS_* explicitly. | |
77 set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0") | |
78 set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0") | |
79 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR | |
80 CMAKE_CXX_COMPILER_ID STREQUAL "XL") | |
81 # CMake 2.8 changes Visual Age's compiler ID to "XL". | |
82 set(cxx_exception_flags "-qeh") | |
83 set(cxx_no_exception_flags "-qnoeh") | |
84 # Until version 9.0, Visual Age doesn't define a macro to indicate | |
85 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI | |
86 # explicitly. | |
87 set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0") | |
88 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP") | |
89 set(cxx_base_flags "-AA -mt") | |
90 set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1") | |
91 set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0") | |
92 # RTTI can not be disabled in HP aCC compiler. | |
93 set(cxx_no_rtti_flags "") | |
94 endif() | |
95 | |
96 if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed. | |
97 set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1") | |
98 else() | |
99 set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0") | |
100 endif() | |
101 | |
102 # For building gtest's own tests and samples. | |
103 set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}
") | |
104 set(cxx_no_exception | |
105 "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}") | |
106 set(cxx_default "${cxx_exception}") | |
107 set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}") | |
108 set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1") | |
109 | |
110 # For building the gtest libraries. | |
111 set(cxx_strict "${cxx_default} ${cxx_strict_flags}") | |
112 endmacro() | |
113 | |
114 # Defines the gtest & gtest_main libraries. User tests should link | |
115 # with one of them. | |
116 function(cxx_library_with_type name type cxx_flags) | |
117 # type can be either STATIC or SHARED to denote a static or shared library. | |
118 # ARGN refers to additional arguments after 'cxx_flags'. | |
119 add_library(${name} ${type} ${ARGN}) | |
120 set_target_properties(${name} | |
121 PROPERTIES | |
122 COMPILE_FLAGS "${cxx_flags}") | |
123 if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") | |
124 set_target_properties(${name} | |
125 PROPERTIES | |
126 COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") | |
127 endif() | |
128 if (CMAKE_USE_PTHREADS_INIT) | |
129 target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT}) | |
130 endif() | |
131 endfunction() | |
132 | |
133 ######################################################################## | |
134 # | |
135 # Helper functions for creating build targets. | |
136 | |
137 function(cxx_shared_library name cxx_flags) | |
138 cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN}) | |
139 endfunction() | |
140 | |
141 function(cxx_library name cxx_flags) | |
142 cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN}) | |
143 endfunction() | |
144 | |
145 # cxx_executable_with_flags(name cxx_flags libs srcs...) | |
146 # | |
147 # creates a named C++ executable that depends on the given libraries and | |
148 # is built from the given source files with the given compiler flags. | |
149 function(cxx_executable_with_flags name cxx_flags libs) | |
150 add_executable(${name} ${ARGN}) | |
151 if (cxx_flags) | |
152 set_target_properties(${name} | |
153 PROPERTIES | |
154 COMPILE_FLAGS "${cxx_flags}") | |
155 endif() | |
156 if (BUILD_SHARED_LIBS) | |
157 set_target_properties(${name} | |
158 PROPERTIES | |
159 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") | |
160 endif() | |
161 # To support mixing linking in static and dynamic libraries, link each | |
162 # library in with an extra call to target_link_libraries. | |
163 foreach (lib "${libs}") | |
164 target_link_libraries(${name} ${lib}) | |
165 endforeach() | |
166 endfunction() | |
167 | |
168 # cxx_executable(name dir lib srcs...) | |
169 # | |
170 # creates a named target that depends on the given libs and is built | |
171 # from the given source files. dir/name.cc is implicitly included in | |
172 # the source file list. | |
173 function(cxx_executable name dir libs) | |
174 cxx_executable_with_flags( | |
175 ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) | |
176 endfunction() | |
177 | |
178 # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE. | |
179 find_package(PythonInterp) | |
180 | |
181 # cxx_test_with_flags(name cxx_flags libs srcs...) | |
182 # | |
183 # creates a named C++ test that depends on the given libs and is built | |
184 # from the given source files with the given compiler flags. | |
185 function(cxx_test_with_flags name cxx_flags libs) | |
186 cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) | |
187 add_test(${name} ${name}) | |
188 endfunction() | |
189 | |
190 # cxx_test(name libs srcs...) | |
191 # | |
192 # creates a named test target that depends on the given libs and is | |
193 # built from the given source files. Unlike cxx_test_with_flags, | |
194 # test/name.cc is already implicitly included in the source file list. | |
195 function(cxx_test name libs) | |
196 cxx_test_with_flags("${name}" "${cxx_default}" "${libs}" | |
197 "test/${name}.cc" ${ARGN}) | |
198 endfunction() | |
199 | |
200 # py_test(name) | |
201 # | |
202 # creates a Python test with the given name whose main module is in | |
203 # test/name.py. It does nothing if Python is not installed. | |
204 function(py_test name) | |
205 # We are not supporting Python tests on Linux yet as they consider | |
206 # all Linux environments to be google3 and try to use google3 features. | |
207 if (PYTHONINTERP_FOUND) | |
208 # ${CMAKE_BINARY_DIR} is known at configuration time, so we can | |
209 # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known | |
210 # only at ctest runtime (by calling ctest -c <Configuration>), so | |
211 # we have to escape $ to delay variable substitution here. | |
212 add_test(${name} | |
213 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py | |
214 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE}) | |
215 endif() | |
216 endfunction() | |
OLD | NEW |