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