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. 19macro(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 # https://gitlab.kitware.com/cmake/community/wikis/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 42 # Prevent D9025 warning for targets that have exception handling 43 # turned off (/EHs-c- flag). Where required, exceptions are explicitly 44 # re-enabled using the cxx_exception_flags variable. 45 string(REPLACE "/EHsc" "" ${flag_var} "${${flag_var}}") 46 endforeach() 47 endif() 48endmacro() 49 50# Defines the compiler/linker flags used to build Google Test and 51# Google Mock. You can tweak these definitions to suit your need. A 52# variable's value is empty before it's explicitly assigned to. 53macro(config_compiler_and_linker) 54 # Note: pthreads on MinGW is not supported, even if available 55 # instead, we use windows threading primitives 56 unset(GTEST_HAS_PTHREAD) 57 if (NOT gtest_disable_pthreads AND NOT MINGW) 58 # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. 59 set(THREADS_PREFER_PTHREAD_FLAG ON) 60 find_package(Threads) 61 if (CMAKE_USE_PTHREADS_INIT) 62 set(GTEST_HAS_PTHREAD ON) 63 endif() 64 endif() 65 66 fix_default_compiler_settings_() 67 if (MSVC) 68 # Newlines inside flags variables break CMake's NMake generator. 69 # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds. 70 set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi") 71 if (MSVC_VERSION LESS 1400) # 1400 is Visual Studio 2005 72 # Suppress spurious warnings MSVC 7.1 sometimes issues. 73 # Forcing value to bool. 74 set(cxx_base_flags "${cxx_base_flags} -wd4800") 75 # Copy constructor and assignment operator could not be generated. 76 set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512") 77 # Compatibility warnings not applicable to Google Test. 78 # Resolved overload was found by argument-dependent lookup. 79 set(cxx_base_flags "${cxx_base_flags} -wd4675") 80 endif() 81 if (MSVC_VERSION LESS 1500) # 1500 is Visual Studio 2008 82 # Conditional expression is constant. 83 # When compiling with /W4, we get several instances of C4127 84 # (Conditional expression is constant). In our code, we disable that 85 # warning on a case-by-case basis. However, on Visual Studio 2005, 86 # the warning fires on std::list. Therefore on that compiler and earlier, 87 # we disable the warning project-wide. 88 set(cxx_base_flags "${cxx_base_flags} -wd4127") 89 endif() 90 if (NOT (MSVC_VERSION LESS 1700)) # 1700 is Visual Studio 2012. 91 # Suppress "unreachable code" warning on VS 2012 and later. 92 # http://stackoverflow.com/questions/3232669 explains the issue. 93 set(cxx_base_flags "${cxx_base_flags} -wd4702") 94 endif() 95 96 set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32") 97 set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") 98 set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") 99 set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0") 100 set(cxx_no_rtti_flags "-GR-") 101 elseif (CMAKE_COMPILER_IS_GNUCXX) 102 set(cxx_base_flags "-Wall -Wshadow -Werror") 103 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0) 104 set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else") 105 endif() 106 set(cxx_exception_flags "-fexceptions") 107 set(cxx_no_exception_flags "-fno-exceptions") 108 # Until version 4.3.2, GCC doesn't define a macro to indicate 109 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 110 # explicitly. 111 set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") 112 set(cxx_strict_flags 113 "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers") 114 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") 115 set(cxx_exception_flags "-features=except") 116 # Sun Pro doesn't provide macros to indicate whether exceptions and 117 # RTTI are enabled, so we define GTEST_HAS_* explicitly. 118 set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0") 119 set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0") 120 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR 121 CMAKE_CXX_COMPILER_ID STREQUAL "XL") 122 # CMake 2.8 changes Visual Age's compiler ID to "XL". 123 set(cxx_exception_flags "-qeh") 124 set(cxx_no_exception_flags "-qnoeh") 125 # Until version 9.0, Visual Age doesn't define a macro to indicate 126 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 127 # explicitly. 128 set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0") 129 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP") 130 set(cxx_base_flags "-AA -mt") 131 set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1") 132 set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0") 133 # RTTI can not be disabled in HP aCC compiler. 134 set(cxx_no_rtti_flags "") 135 endif() 136 137 # The pthreads library is available and allowed? 138 if (DEFINED GTEST_HAS_PTHREAD) 139 set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1") 140 else() 141 set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0") 142 endif() 143 set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}") 144 145 # For building gtest's own tests and samples. 146 set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}") 147 set(cxx_no_exception 148 "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}") 149 set(cxx_default "${cxx_exception}") 150 set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}") 151 set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1") 152 153 # For building the gtest libraries. 154 set(cxx_strict "${cxx_default} ${cxx_strict_flags}") 155endmacro() 156 157# Defines the gtest & gtest_main libraries. User tests should link 158# with one of them. 159function(cxx_library_with_type name type cxx_flags) 160 # type can be either STATIC or SHARED to denote a static or shared library. 161 # ARGN refers to additional arguments after 'cxx_flags'. 162 add_library(${name} ${type} ${ARGN}) 163 set_target_properties(${name} 164 PROPERTIES 165 COMPILE_FLAGS "${cxx_flags}") 166 # Generate debug library name with a postfix. 167 set_target_properties(${name} 168 PROPERTIES 169 DEBUG_POSTFIX "d") 170 if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") 171 set_target_properties(${name} 172 PROPERTIES 173 COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") 174 if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11") 175 target_compile_definitions(${name} INTERFACE 176 $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>) 177 endif() 178 endif() 179 if (DEFINED GTEST_HAS_PTHREAD) 180 if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0") 181 set(threads_spec ${CMAKE_THREAD_LIBS_INIT}) 182 else() 183 set(threads_spec Threads::Threads) 184 endif() 185 target_link_libraries(${name} PUBLIC ${threads_spec}) 186 endif() 187endfunction() 188 189######################################################################## 190# 191# Helper functions for creating build targets. 192 193function(cxx_shared_library name cxx_flags) 194 cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN}) 195endfunction() 196 197function(cxx_library name cxx_flags) 198 cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN}) 199endfunction() 200 201# cxx_executable_with_flags(name cxx_flags libs srcs...) 202# 203# creates a named C++ executable that depends on the given libraries and 204# is built from the given source files with the given compiler flags. 205function(cxx_executable_with_flags name cxx_flags libs) 206 add_executable(${name} ${ARGN}) 207 if (MSVC AND (NOT (MSVC_VERSION LESS 1700))) # 1700 is Visual Studio 2012. 208 # BigObj required for tests. 209 set(cxx_flags "${cxx_flags} -bigobj") 210 endif() 211 if (cxx_flags) 212 set_target_properties(${name} 213 PROPERTIES 214 COMPILE_FLAGS "${cxx_flags}") 215 endif() 216 if (BUILD_SHARED_LIBS) 217 set_target_properties(${name} 218 PROPERTIES 219 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") 220 endif() 221 # To support mixing linking in static and dynamic libraries, link each 222 # library in with an extra call to target_link_libraries. 223 foreach (lib "${libs}") 224 target_link_libraries(${name} ${lib}) 225 endforeach() 226endfunction() 227 228# cxx_executable(name dir lib srcs...) 229# 230# creates a named target that depends on the given libs and is built 231# from the given source files. dir/name.cc is implicitly included in 232# the source file list. 233function(cxx_executable name dir libs) 234 cxx_executable_with_flags( 235 ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) 236endfunction() 237 238# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE. 239find_package(PythonInterp) 240 241# cxx_test_with_flags(name cxx_flags libs srcs...) 242# 243# creates a named C++ test that depends on the given libs and is built 244# from the given source files with the given compiler flags. 245function(cxx_test_with_flags name cxx_flags libs) 246 cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) 247 add_test(NAME ${name} COMMAND ${name}) 248endfunction() 249 250# cxx_test(name libs srcs...) 251# 252# creates a named test target that depends on the given libs and is 253# built from the given source files. Unlike cxx_test_with_flags, 254# test/name.cc is already implicitly included in the source file list. 255function(cxx_test name libs) 256 cxx_test_with_flags("${name}" "${cxx_default}" "${libs}" 257 "test/${name}.cc" ${ARGN}) 258endfunction() 259 260# py_test(name) 261# 262# creates a Python test with the given name whose main module is in 263# test/name.py. It does nothing if Python is not installed. 264function(py_test name) 265 if (PYTHONINTERP_FOUND) 266 if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 267 if (CMAKE_CONFIGURATION_TYPES) 268 # Multi-configuration build generators as for Visual Studio save 269 # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug, 270 # Release etc.), so we have to provide it here. 271 add_test( 272 NAME ${name} 273 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 274 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN}) 275 else (CMAKE_CONFIGURATION_TYPES) 276 # Single-configuration build generators like Makefile generators 277 # don't have subdirs below CMAKE_CURRENT_BINARY_DIR. 278 add_test( 279 NAME ${name} 280 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 281 --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN}) 282 endif (CMAKE_CONFIGURATION_TYPES) 283 else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 284 # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can 285 # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known 286 # only at ctest runtime (by calling ctest -c <Configuration>), so 287 # we have to escape $ to delay variable substitution here. 288 add_test( 289 ${name} 290 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 291 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN}) 292 endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) 293 endif(PYTHONINTERP_FOUND) 294endfunction() 295 296# install_project(targets...) 297# 298# Installs the specified targets and configures the associated pkgconfig files. 299function(install_project) 300 if(INSTALL_GTEST) 301 install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" 302 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 303 # Install the project targets. 304 install(TARGETS ${ARGN} 305 EXPORT ${targets_export_name} 306 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 307 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 308 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") 309 # Configure and install pkgconfig files. 310 foreach(t ${ARGN}) 311 set(configured_pc "${generated_dir}/${t}.pc") 312 configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in" 313 "${configured_pc}" @ONLY) 314 install(FILES "${configured_pc}" 315 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 316 endforeach() 317 endif() 318endfunction() 319