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_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 26 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO 27 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 28 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 29 if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt) 30 # When Google Test is built as a shared library, it should also use 31 # shared runtime libraries. Otherwise, it may end up with multiple 32 # copies of runtime library data in different modules, resulting in 33 # hard-to-find crashes. When it is built as a static library, it is 34 # preferable to use CRT as static libraries, as we don't have to rely 35 # on CRT DLLs being available. CMake always defaults to using shared 36 # CRT libraries, so we override that default here. 37 string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}") 38 endif() 39 40 # We prefer more strict warning checking for building Google Test. 41 # Replaces /W3 with /W4 in defaults. 42 string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}") 43 44 # Prevent D9025 warning for targets that have exception handling 45 # turned off (/EHs-c- flag). Where required, exceptions are explicitly 46 # re-enabled using the cxx_exception_flags variable. 47 string(REPLACE "/EHsc" "" ${flag_var} "${${flag_var}}") 48 endforeach() 49 endif() 50endmacro() 51 52# Defines the compiler/linker flags used to build Google Test and 53# Google Mock. You can tweak these definitions to suit your need. A 54# variable's value is empty before it's explicitly assigned to. 55macro(config_compiler_and_linker) 56 # Note: pthreads on MinGW is not supported, even if available 57 # instead, we use windows threading primitives 58 unset(GTEST_HAS_PTHREAD) 59 if (NOT gtest_disable_pthreads AND NOT MINGW) 60 # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. 61 find_package(Threads) 62 if (CMAKE_USE_PTHREADS_INIT) 63 set(GTEST_HAS_PTHREAD ON) 64 endif() 65 endif() 66 67 fix_default_compiler_settings_() 68 if (MSVC) 69 # Newlines inside flags variables break CMake's NMake generator. 70 # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds. 71 set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi") 72 set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32") 73 set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") 74 set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") 75 set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0") 76 set(cxx_no_rtti_flags "-GR-") 77 # Suppress "unreachable code" warning 78 # http://stackoverflow.com/questions/3232669 explains the issue. 79 set(cxx_base_flags "${cxx_base_flags} -wd4702") 80 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 81 set(cxx_base_flags "-Wall -Wshadow -Werror -Wno-error=sign-conversion") 82 set(cxx_exception_flags "-fexceptions") 83 set(cxx_no_exception_flags "-fno-exceptions") 84 set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls") 85 elseif (CMAKE_COMPILER_IS_GNUCXX) 86 set(cxx_base_flags "-Wall -Wshadow -Werror") 87 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0) 88 set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else") 89 endif() 90 set(cxx_exception_flags "-fexceptions") 91 set(cxx_no_exception_flags "-fno-exceptions") 92 # Until version 4.3.2, GCC doesn't define a macro to indicate 93 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 94 # explicitly. 95 set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") 96 set(cxx_strict_flags 97 "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers") 98 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") 99 set(cxx_exception_flags "-features=except") 100 # Sun Pro doesn't provide macros to indicate whether exceptions and 101 # RTTI are enabled, so we define GTEST_HAS_* explicitly. 102 set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0") 103 set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0") 104 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR 105 CMAKE_CXX_COMPILER_ID STREQUAL "XL") 106 # CMake 2.8 changes Visual Age's compiler ID to "XL". 107 set(cxx_exception_flags "-qeh") 108 set(cxx_no_exception_flags "-qnoeh") 109 # Until version 9.0, Visual Age doesn't define a macro to indicate 110 # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI 111 # explicitly. 112 set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0") 113 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP") 114 set(cxx_base_flags "-AA -mt") 115 set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1") 116 set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0") 117 # RTTI can not be disabled in HP aCC compiler. 118 set(cxx_no_rtti_flags "") 119 endif() 120 121 # The pthreads library is available and allowed? 122 if (DEFINED GTEST_HAS_PTHREAD) 123 set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1") 124 else() 125 set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0") 126 endif() 127 set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}") 128 129 # For building gtest's own tests and samples. 130 set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}") 131 set(cxx_no_exception 132 "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}") 133 set(cxx_default "${cxx_exception}") 134 set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}") 135 136 # For building the gtest libraries. 137 set(cxx_strict "${cxx_default} ${cxx_strict_flags}") 138endmacro() 139 140# Defines the gtest & gtest_main libraries. User tests should link 141# with one of them. 142function(cxx_library_with_type name type cxx_flags) 143 # type can be either STATIC or SHARED to denote a static or shared library. 144 # ARGN refers to additional arguments after 'cxx_flags'. 145 add_library(${name} ${type} ${ARGN}) 146 set_target_properties(${name} 147 PROPERTIES 148 COMPILE_FLAGS "${cxx_flags}") 149 # Generate debug library name with a postfix. 150 set_target_properties(${name} 151 PROPERTIES 152 DEBUG_POSTFIX "d") 153 # Set the output directory for build artifacts 154 set_target_properties(${name} 155 PROPERTIES 156 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" 157 LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" 158 ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" 159 PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") 160 # make PDBs match library name 161 get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX) 162 set_target_properties(${name} 163 PROPERTIES 164 PDB_NAME "${name}" 165 PDB_NAME_DEBUG "${name}${pdb_debug_postfix}" 166 COMPILE_PDB_NAME "${name}" 167 COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}") 168 169 if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") 170 set_target_properties(${name} 171 PROPERTIES 172 COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") 173 if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11") 174 target_compile_definitions(${name} INTERFACE 175 $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>) 176 endif() 177 endif() 178 if (DEFINED GTEST_HAS_PTHREAD) 179 if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0") 180 set(threads_spec ${CMAKE_THREAD_LIBS_INIT}) 181 else() 182 set(threads_spec Threads::Threads) 183 endif() 184 target_link_libraries(${name} PUBLIC ${threads_spec}) 185 endif() 186endfunction() 187 188######################################################################## 189# 190# Helper functions for creating build targets. 191 192function(cxx_shared_library name cxx_flags) 193 cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN}) 194endfunction() 195 196function(cxx_library name cxx_flags) 197 cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN}) 198endfunction() 199 200# cxx_executable_with_flags(name cxx_flags libs srcs...) 201# 202# creates a named C++ executable that depends on the given libraries and 203# is built from the given source files with the given compiler flags. 204function(cxx_executable_with_flags name cxx_flags libs) 205 add_executable(${name} ${ARGN}) 206 if (MSVC) 207 # BigObj required for tests. 208 set(cxx_flags "${cxx_flags} -bigobj") 209 endif() 210 if (cxx_flags) 211 set_target_properties(${name} 212 PROPERTIES 213 COMPILE_FLAGS "${cxx_flags}") 214 endif() 215 if (BUILD_SHARED_LIBS) 216 set_target_properties(${name} 217 PROPERTIES 218 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") 219 endif() 220 # To support mixing linking in static and dynamic libraries, link each 221 # library in with an extra call to target_link_libraries. 222 foreach (lib "${libs}") 223 target_link_libraries(${name} ${lib}) 224 endforeach() 225endfunction() 226 227# cxx_executable(name dir lib srcs...) 228# 229# creates a named target that depends on the given libs and is built 230# from the given source files. dir/name.cc is implicitly included in 231# the source file list. 232function(cxx_executable name dir libs) 233 cxx_executable_with_flags( 234 ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) 235endfunction() 236 237# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE. 238find_package(PythonInterp) 239 240# cxx_test_with_flags(name cxx_flags libs srcs...) 241# 242# creates a named C++ test that depends on the given libs and is built 243# from the given source files with the given compiler flags. 244function(cxx_test_with_flags name cxx_flags libs) 245 cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) 246 if (WIN32 OR MINGW) 247 add_test(NAME ${name} 248 COMMAND "powershell" "-Command" "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1" "$<TARGET_FILE:${name}>") 249 else() 250 add_test(NAME ${name} 251 COMMAND "$<TARGET_FILE:${name}>") 252 endif() 253endfunction() 254 255# cxx_test(name libs srcs...) 256# 257# creates a named test target that depends on the given libs and is 258# built from the given source files. Unlike cxx_test_with_flags, 259# test/name.cc is already implicitly included in the source file list. 260function(cxx_test name libs) 261 cxx_test_with_flags("${name}" "${cxx_default}" "${libs}" 262 "test/${name}.cc" ${ARGN}) 263endfunction() 264 265# py_test(name) 266# 267# creates a Python test with the given name whose main module is in 268# test/name.py. It does nothing if Python is not installed. 269function(py_test name) 270 if (PYTHONINTERP_FOUND) 271 if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1) 272 if (CMAKE_CONFIGURATION_TYPES) 273 # Multi-configuration build generators as for Visual Studio save 274 # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug, 275 # Release etc.), so we have to provide it here. 276 if (WIN32 OR MINGW) 277 add_test(NAME ${name} 278 COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1 279 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 280 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN}) 281 else() 282 add_test(NAME ${name} 283 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 284 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN}) 285 endif() 286 else (CMAKE_CONFIGURATION_TYPES) 287 # Single-configuration build generators like Makefile generators 288 # don't have subdirs below CMAKE_CURRENT_BINARY_DIR. 289 if (WIN32 OR MINGW) 290 add_test(NAME ${name} 291 COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1 292 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 293 --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN}) 294 else() 295 add_test(NAME ${name} 296 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 297 --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN}) 298 endif() 299 endif (CMAKE_CONFIGURATION_TYPES) 300 else() 301 # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can 302 # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known 303 # only at ctest runtime (by calling ctest -c <Configuration>), so 304 # we have to escape $ to delay variable substitution here. 305 if (WIN32 OR MINGW) 306 add_test(NAME ${name} 307 COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1 308 ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 309 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN}) 310 else() 311 add_test(NAME ${name} 312 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py 313 --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN}) 314 endif() 315 endif() 316 endif(PYTHONINTERP_FOUND) 317endfunction() 318 319# install_project(targets...) 320# 321# Installs the specified targets and configures the associated pkgconfig files. 322function(install_project) 323 if(INSTALL_GTEST) 324 install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" 325 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 326 # Install the project targets. 327 install(TARGETS ${ARGN} 328 EXPORT ${targets_export_name} 329 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 330 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 331 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") 332 if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") 333 # Install PDBs 334 foreach(t ${ARGN}) 335 get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME) 336 get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG) 337 get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY) 338 install(FILES 339 "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb" 340 DESTINATION ${CMAKE_INSTALL_LIBDIR} 341 OPTIONAL) 342 endforeach() 343 endif() 344 # Configure and install pkgconfig files. 345 foreach(t ${ARGN}) 346 set(configured_pc "${generated_dir}/${t}.pc") 347 configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in" 348 "${configured_pc}" @ONLY) 349 install(FILES "${configured_pc}" 350 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 351 endforeach() 352 endif() 353endfunction() 354