1#------------------------------------------------------------------------------ 2# Build the google test library 3 4# We compile Google Test ourselves instead of using pre-compiled libraries. 5# See the Google Test FAQ "Why is it not recommended to install a 6# pre-compiled copy of Google Test (for example, into /usr/local)?" 7# at http://code.google.com/p/googletest/wiki/FAQ for more details. 8add_library(gmock STATIC 9 gmock-gtest-all.cc gmock/gmock.h gtest/gtest.h gtest/gtest-spi.h) 10target_compile_definitions(gmock PUBLIC GTEST_HAS_STD_WSTRING=1) 11target_include_directories(gmock SYSTEM PUBLIC . gmock gtest) 12 13find_package(Threads) 14if (Threads_FOUND) 15 target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT}) 16else () 17 target_compile_definitions(gmock PUBLIC GTEST_HAS_PTHREAD=0) 18endif () 19 20target_compile_definitions(gmock PUBLIC GTEST_LANG_CXX11=0) 21 22if (MSVC) 23 # Workaround a bug in implementation of variadic templates in MSVC11. 24 target_compile_definitions(gmock PUBLIC _VARIADIC_MAX=10) 25 26 # Disable MSVC warnings of _CRT_INSECURE_DEPRECATE functions. 27 target_compile_definitions(gmock PRIVATE _CRT_SECURE_NO_WARNINGS) 28 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 29 # Disable MSVC warnings of POSIX functions. 30 target_compile_options(gmock PUBLIC -Wno-deprecated-declarations) 31 endif () 32endif () 33 34# GTest doesn't detect <tuple> with clang. 35if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 36 target_compile_definitions(gmock PUBLIC GTEST_USE_OWN_TR1_TUPLE=1) 37endif () 38 39# Silence MSVC tr1 deprecation warning in gmock. 40target_compile_definitions(gmock 41 PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING=1) 42 43#------------------------------------------------------------------------------ 44# Build the actual library tests 45 46set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc) 47add_library(test-main STATIC ${TEST_MAIN_SRC}) 48target_include_directories(test-main SYSTEM PUBLIC gtest gmock) 49target_link_libraries(test-main gmock fmt) 50 51include(CheckCXXCompilerFlag) 52 53# Workaround GTest bug https://github.com/google/googletest/issues/705. 54check_cxx_compiler_flag( 55 -fno-delete-null-pointer-checks HAVE_FNO_DELETE_NULL_POINTER_CHECKS) 56if (HAVE_FNO_DELETE_NULL_POINTER_CHECKS) 57 target_compile_options(test-main PUBLIC -fno-delete-null-pointer-checks) 58endif () 59 60# Use less strict pedantic flags for the tests because GMock doesn't compile 61# cleanly with -pedantic and -std=c++98. 62if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) 63 #set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -Wno-long-long -Wno-variadic-macros) 64endif () 65 66function(add_fmt_executable name) 67 add_executable(${name} ${ARGN}) 68 if (MINGW) 69 target_link_libraries(${name} -static-libgcc -static-libstdc++) 70 endif () 71endfunction() 72 73# Adds a test. 74# Usage: add_fmt_test(name srcs...) 75function(add_fmt_test name) 76 add_fmt_executable(${name} ${name}.cc ${ARGN}) 77 target_link_libraries(${name} test-main) 78 79 # Define if certain C++ features can be used. 80 if (FMT_PEDANTIC) 81 target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS}) 82 endif () 83 if (FMT_WERROR) 84 target_compile_options(${name} PRIVATE ${WERROR_FLAG}) 85 endif () 86 target_include_directories(${name} SYSTEM PUBLIC gtest gmock) 87 add_test(NAME ${name} COMMAND ${name}) 88endfunction() 89 90add_fmt_test(assert-test) 91add_fmt_test(chrono-test) 92add_fmt_test(color-test) 93add_fmt_test(core-test) 94add_fmt_test(gtest-extra-test) 95add_fmt_test(format-test mock-allocator.h) 96if (MSVC) 97 target_compile_options(format-test PRIVATE /bigobj) 98endif () 99if (NOT (MSVC AND BUILD_SHARED_LIBS)) 100 add_fmt_test(format-impl-test) 101endif () 102add_fmt_test(locale-test) 103add_fmt_test(ostream-test) 104add_fmt_test(compile-test) 105add_fmt_test(printf-test) 106add_fmt_test(ranges-test) 107add_fmt_test(scan-test) 108 109if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC) 110 foreach (flag_var 111 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 112 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 113 if (${flag_var} MATCHES "^(/|-)(MT|MTd)") 114 set(MSVC_STATIC_RUNTIME ON) 115 break() 116 endif() 117 endforeach() 118endif() 119 120if (NOT MSVC_STATIC_RUNTIME) 121 add_fmt_executable(posix-mock-test 122 posix-mock-test.cc ../src/format.cc ${TEST_MAIN_SRC}) 123 target_include_directories( 124 posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR}/include) 125 target_link_libraries(posix-mock-test gmock) 126 target_include_directories(posix-mock-test SYSTEM PUBLIC gtest gmock) 127 if (FMT_PEDANTIC) 128 target_compile_options(posix-mock-test PRIVATE ${PEDANTIC_COMPILE_FLAGS}) 129 endif () 130 if (HAVE_STRTOD_L) 131 target_compile_definitions(posix-mock-test PRIVATE FMT_LOCALE) 132 endif () 133 add_test(NAME posix-mock-test COMMAND posix-mock-test) 134 add_fmt_test(os-test) 135endif () 136 137add_fmt_executable(header-only-test 138 header-only-test.cc header-only-test2.cc test-main.cc) 139target_link_libraries(header-only-test gmock) 140target_include_directories(header-only-test SYSTEM PUBLIC gtest gmock) 141if (TARGET fmt-header-only) 142 target_link_libraries(header-only-test fmt-header-only) 143else () 144 target_include_directories( 145 header-only-test PRIVATE ${PROJECT_SOURCE_DIR}/include) 146 target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1) 147endif () 148 149message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}") 150 151if (FMT_PEDANTIC) 152 # MSVC fails to compile GMock when C++17 is enabled. 153 if (FMT_HAS_VARIANT AND NOT MSVC) 154 add_fmt_test(std-format-test) 155 set_property(TARGET std-format-test PROPERTY CXX_STANDARD 17) 156 endif () 157 158 # Test that the library can be compiled with exceptions disabled. 159 # -fno-exception is broken in icc: https://github.com/fmtlib/fmt/issues/822. 160 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 161 check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG) 162 endif () 163 if (HAVE_FNO_EXCEPTIONS_FLAG) 164 add_library(noexception-test ../src/format.cc) 165 target_include_directories( 166 noexception-test PRIVATE ${PROJECT_SOURCE_DIR}/include) 167 target_compile_options(noexception-test PRIVATE -fno-exceptions) 168 if (FMT_PEDANTIC) 169 target_compile_options(noexception-test PRIVATE ${PEDANTIC_COMPILE_FLAGS}) 170 endif () 171 endif () 172 173 # Test that the library compiles without locale. 174 add_library(nolocale-test ../src/format.cc) 175 target_include_directories( 176 nolocale-test PRIVATE ${PROJECT_SOURCE_DIR}/include) 177 target_compile_definitions( 178 nolocale-test PRIVATE FMT_STATIC_THOUSANDS_SEPARATOR=1) 179 180 add_test(compile-error-test ${CMAKE_CTEST_COMMAND} 181 --build-and-test 182 "${CMAKE_CURRENT_SOURCE_DIR}/compile-error-test" 183 "${CMAKE_CURRENT_BINARY_DIR}/compile-error-test" 184 --build-generator ${CMAKE_GENERATOR} 185 --build-makeprogram ${CMAKE_MAKE_PROGRAM} 186 --build-options 187 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 188 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" 189 "-DCXX_STANDARD_FLAG=${CXX_STANDARD_FLAG}" 190 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}" 191 "-DSUPPORTS_USER_DEFINED_LITERALS=${SUPPORTS_USER_DEFINED_LITERALS}") 192endif () 193 194# These tests are disabled on Windows because they take too long. 195if (FMT_PEDANTIC AND NOT WIN32) 196 # Test if the targets are found from the build directory. 197 add_test(find-package-test ${CMAKE_CTEST_COMMAND} 198 -C ${CMAKE_BUILD_TYPE} 199 --build-and-test 200 "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test" 201 "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" 202 --build-generator ${CMAKE_GENERATOR} 203 --build-makeprogram ${CMAKE_MAKE_PROGRAM} 204 --build-options 205 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 206 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" 207 "-DFMT_DIR=${PROJECT_BINARY_DIR}" 208 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}" 209 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") 210 211 # Test if the targets are found when add_subdirectory is used. 212 add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND} 213 -C ${CMAKE_BUILD_TYPE} 214 --build-and-test 215 "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test" 216 "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test" 217 --build-generator ${CMAKE_GENERATOR} 218 --build-makeprogram ${CMAKE_MAKE_PROGRAM} 219 --build-options 220 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 221 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" 222 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}" 223 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") 224endif () 225 226# Activate optional CUDA tests if CUDA is found. For version selection see 227# https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features 228if (FMT_CUDA_TEST) 229 if (${CMAKE_VERSION} VERSION_LESS 3.15) 230 find_package(CUDA 9.0) 231 else () 232 include(CheckLanguage) 233 check_language(CUDA) 234 if (CMAKE_CUDA_COMPILER) 235 enable_language(CUDA OPTIONAL) 236 set(CUDA_FOUND TRUE) 237 endif () 238 endif () 239 240 if (CUDA_FOUND) 241 add_subdirectory(cuda-test) 242 add_test(NAME cuda-test COMMAND fmt-in-cuda-test) 243 endif () 244endif () 245