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 # Note: pthreads on MinGW is not supported, even if available 50 # instead, we use windows threading primitives 51 if (NOT gtest_disable_pthreads AND NOT MINGW) 52 # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. 53 find_package(Threads) 54 endif() 55 56 fix_default_compiler_settings_() 57 if (MSVC) 58 # Newlines inside flags variables break CMake's NMake generator. 59 # TODO(vladl (a] google.com): Add -RTCs and -RTCu to debug builds. 60 set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi") 61 if (MSVC_VERSION LESS 1400) # 1400 is Visual Studio 2005 62 # Suppress spurious warnings MSVC 7.1 sometimes issues. 63 # Forcing value to bool. 64 set(cxx_base_flags "${cxx_base_flags} -wd4800") 65 # Copy constructor and assignment operator could not be generated. 66 set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512") 67 # Compatibility warnings not applicable to Google Test. 68 # Resolved overload was found by argument-dependent lookup. 69 set(cxx_base_flags "${cxx_base_flags} -wd4675") 70 endif() 71 if (MSVC_VERSION LESS 1500) # 1500 is Visual Studio 2008 72 # Conditional expression is constant. 73 # When compiling with /W4, we get several instances of C4127 74 # (Conditional expression is constant). In our code, we disable that 75 # warning on a case-by-case basis. However, on Visual Studio 2005, 76 # the warning fires on std::list. Therefore on that compiler and earlier, 77 # we disable the warning project-wide. 78 set(cxx_base_flags "${cxx_base_flags} -wd4127") 79 endif() 80 if (NOT (MSVC_VERSION LESS 1700)) # 1700 is Visual Studio 2012. 81 # Suppress "unreachable code" warning on VS 2012 and later. 82 # http://stackoverflow.com/questions/3232669 explains the issue. 83 set(cxx_base_flags "${cxx_base_flags} -wd4702") 84 endif() 85 if (NOT (MSVC_VERSION GREATER 1900)) # 1900 is Visual Studio 2015 86 # BigObj required for tests. 87 set(cxx_base_flags "${cxx_base_flags} -bigobj") 88 endif() 89 90 set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32") 91 set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") 92 set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") 93 set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0") 94 set(cxx_no_rtti_flags "-GR-") 95 elseif (CMAKE_COMPILER_IS_GNUCXX) 96 set(cxx_base_flags "-Wall -Wshadow") 97 set(cxx_exception_flags "-fexceptions") 98 set(cxx_no_exception_flags "-fno-exceptions") 99 # Until version 4.3.2, GCC doesn't define a macro to indicate 100 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 101 # explicitly. 102 set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") 103 set(cxx_strict_flags 104 "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers") 105 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") 106 set(cxx_exception_flags "-features=except") 107 # Sun Pro doesn't provide macros to indicate whether exceptions and 108 # RTTI are enabled, so we define GTEST_HAS_* explicitly. 109 set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0") 110 set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0") 111 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR 112 CMAKE_CXX_COMPILER_ID STREQUAL "XL") 113 # CMake 2.8 changes Visual Age's compiler ID to "XL". 114 set(cxx_exception_flags "-qeh") 115 set(cxx_no_exception_flags "-qnoeh") 116 # Until version 9.0, Visual Age doesn't define a macro to indicate 117 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 118 # explicitly. 119 set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0") 120 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP") 121 set(cxx_base_flags "-AA -mt") 122 set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1") 123 set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0") 124 # RTTI can not be disabled in HP aCC compiler. 125 set(cxx_no_rtti_flags "") 126 endif() 127 128 if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed. 129 set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1") 130 else() 131 set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0") 132 endif() 133 134 # For building gtest's own tests and samples. 135 set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}") 136 set(cxx_no_exception 137 "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}") 138 set(cxx_default "${cxx_exception}") 139 set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}") 140 set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1") 141 142 # For building the gtest libraries. 143 set(cxx_strict "${cxx_default} ${cxx_strict_flags}") 144 endmacro() 145 146 # Defines the gtest & gtest_main libraries. User tests should link 147 # with one of them. 148 function(cxx_library_with_type name type cxx_flags) 149 # type can be either STATIC or SHARED to denote a static or shared library. 150 # ARGN refers to additional arguments after 'cxx_flags'. 151 add_library(${name} ${type} ${ARGN}) 152 set_target_properties(${name} 153 PROPERTIES 154 COMPILE_FLAGS "${cxx_flags}") 155 if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") 156 set_target_properties(${name} 157 PROPERTIES 158 COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") 159 endif() 160 if (CMAKE_USE_PTHREADS_INIT) 161 target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT}) 162 endif() 163 endfunction() 164 165 ######################################################################## 166 # 167 # Helper functions for creating build targets. 168 169 function(cxx_shared_library name cxx_flags) 170 cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN}) 171 endfunction() 172 173 function(cxx_library name cxx_flags) 174 cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN}) 175 endfunction() 176 177 # cxx_executable_with_flags(name cxx_flags libs srcs...) 178 # 179 # creates a named C++ executable that depends on the given libraries and 180 # is built from the given source files with the given compiler flags. 181 function(cxx_executable_with_flags name cxx_flags libs) 182 add_executable(${name} ${ARGN}) 183 if (cxx_flags) 184 set_target_properties(${name} 185 PROPERTIES 186 COMPILE_FLAGS "${cxx_flags}") 187 endif() 188 if (BUILD_SHARED_LIBS) 189 set_target_properties(${name} 190 PROPERTIES 191 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") 192 endif() 193 # To support mixing linking in static and dynamic libraries, link each 194 # library in with an extra call to target_link_libraries. 195 foreach (lib "${libs}") 196 target_link_libraries(${name} ${lib}) 197 endforeach() 198 endfunction() 199 200 # cxx_executable(name dir lib srcs...) 201 # 202 # creates a named target that depends on the given libs and is built 203 # from the given source files. dir/name.cc is implicitly included in 204 # the source file list. 205 function(cxx_executable name dir libs) 206 cxx_executable_with_flags( 207 ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) 208 endfunction() 209 210 # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE. 211 find_package(PythonInterp) 212 213 # cxx_test_with_flags(name cxx_flags libs srcs...) 214 # 215 # creates a named C++ test that depends on the given libs and is built 216 # from the given source files with the given compiler flags. 217 function(cxx_test_with_flags name cxx_flags libs) 218 cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) 219 add_test(${name} ${name}) 220 endfunction() 221 222 # cxx_test(name libs srcs...) 223 # 224 # creates a named test target that depends on the given libs and is 225 # built from the given source files. Unlike cxx_test_with_flags, 226 # test/name.cc is already implicitly included in the source file list. 227 function(cxx_test name libs) 228 cxx_test_with_flags("${name}" "${cxx_default}" "${libs}" 229 "test/${name}.cc" ${ARGN}) 230 endfunction() 231 232 # py_test(name) 233 # 234 # creates a Python test with the given name whose main module is in 235 # test/name.py. It does nothing if Python is not installed. 236 function(py_test name) 237 # We are not supporting Python tests on Linux yet as they consider 238 # all Linux environments to be google3 and try to use google3 features. 239 if (PYTHONINTERP_FOUND) 240 # ${CMAKE_BINARY_DIR} is known at configuration time, so we can 241 # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known 242 # only at ctest runtime (by calling ctest -c <Configuration>), so 243 # we have to escape $ to delay variable substitution here. 244 if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 245 add_test( 246 NAME ${name} 247 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 248 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>) 249 else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 250 add_test( 251 ${name} 252 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 253 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE}) 254 endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 255 endif() 256 endfunction() 257