1 add_subdirectory(gtest)
3 include(CheckSymbolExists)
5 set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
6 add_library(test-main STATIC ${TEST_MAIN_SRC})
7 target_include_directories(test-main PUBLIC
8 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
9 target_link_libraries(test-main gtest fmt)
11 function(add_fmt_executable name)
12 add_executable(${name} ${ARGN})
13 # (Wstringop-overflow) - [meta-bug] bogus/missing -Wstringop-overflow warnings
14 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88443
15 # Bogus -Wstringop-overflow warning
16 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100395
17 # [10 Regression] spurious -Wstringop-overflow writing to a trailing array plus offset
18 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95353
19 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
20 NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
21 target_compile_options(${name} PRIVATE -Wno-stringop-overflow)
22 # The linker flag is needed for LTO.
23 target_link_libraries(${name} -Wno-stringop-overflow)
28 # Usage: add_fmt_test(name srcs...)
29 function(add_fmt_test name)
30 cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE" "" "" ${ARGN})
32 set(sources ${name}.cc ${ADD_FMT_TEST_UNPARSED_ARGUMENTS})
33 if (ADD_FMT_TEST_HEADER_ONLY)
34 set(sources ${sources} ${TEST_MAIN_SRC} ../src/os.cc)
35 set(libs gtest fmt-header-only)
36 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
37 set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-weak-vtables)
39 elseif (ADD_FMT_TEST_MODULE)
40 set(libs test-main test-module)
41 set_source_files_properties(${name}.cc PROPERTIES OBJECT_DEPENDS test-module)
43 set(libs test-main fmt)
45 add_fmt_executable(${name} ${sources})
46 target_link_libraries(${name} ${libs})
48 if (ADD_FMT_TEST_HEADER_ONLY AND NOT FMT_UNICODE)
49 target_compile_definitions(${name} PUBLIC FMT_UNICODE=0)
52 # Define if certain C++ features can be used.
54 target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
57 target_compile_options(${name} PRIVATE ${WERROR_FLAG})
59 add_test(NAME ${name} COMMAND ${name})
66 add_fmt_test(args-test)
67 add_fmt_test(base-test)
68 add_fmt_test(assert-test)
69 add_fmt_test(chrono-test)
70 add_fmt_test(color-test)
71 add_fmt_test(gtest-extra-test)
72 add_fmt_test(format-test mock-allocator.h)
74 target_compile_options(format-test PRIVATE /bigobj)
76 if (NOT (MSVC AND BUILD_SHARED_LIBS))
77 add_fmt_test(format-impl-test HEADER_ONLY header-only-test.cc)
79 add_fmt_test(ostream-test)
80 add_fmt_test(compile-test)
81 add_fmt_test(compile-fp-test HEADER_ONLY)
83 # Without this option, MSVC returns 199711L for the __cplusplus macro.
84 target_compile_options(compile-fp-test PRIVATE /Zc:__cplusplus)
86 add_fmt_test(printf-test)
87 add_fmt_test(ranges-test ranges-odr-test.cc)
89 add_fmt_test(scan-test HEADER_ONLY)
90 check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
92 target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME)
95 add_fmt_test(std-test)
96 try_compile(compile_result_unused
97 ${CMAKE_CURRENT_BINARY_DIR}
98 SOURCES ${CMAKE_CURRENT_LIST_DIR}/detect-stdfs.cc
99 OUTPUT_VARIABLE RAWOUTPUT)
100 string(REGEX REPLACE ".*libfound \"([^\"]*)\".*" "\\1" STDLIBFS "${RAWOUTPUT}")
102 target_link_libraries(std-test ${STDLIBFS})
104 add_fmt_test(unicode-test HEADER_ONLY)
106 target_compile_options(unicode-test PRIVATE /utf-8)
108 add_fmt_test(xchar-test)
109 add_fmt_test(enforce-checks-test)
110 target_compile_definitions(enforce-checks-test PRIVATE
111 -DFMT_ENFORCE_COMPILE_STRING)
113 add_executable(perf-sanity perf-sanity.cc)
114 target_link_libraries(perf-sanity fmt::fmt)
117 # The tests need {fmt} to be compiled as traditional library
118 # because of visibility of implementation details.
119 # If module support is present the module tests require a
120 # test-only module to be built from {fmt}
121 add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc)
122 target_compile_features(test-module PUBLIC cxx_std_11)
123 target_include_directories(test-module PUBLIC
124 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
125 enable_module(test-module)
127 add_fmt_test(module-test MODULE test-main.cc)
129 target_compile_options(test-module PRIVATE /utf-8 /Zc:__cplusplus
130 /Zc:externConstexpr /Zc:inline)
131 target_compile_options(module-test PRIVATE /utf-8 /Zc:__cplusplus
132 /Zc:externConstexpr /Zc:inline)
136 if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
138 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
139 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
140 if (${flag_var} MATCHES "^(/|-)(MT|MTd)")
141 set(MSVC_STATIC_RUNTIME ON)
147 if (NOT MSVC_STATIC_RUNTIME)
148 add_fmt_executable(posix-mock-test
149 posix-mock-test.cc ../src/format.cc ${TEST_MAIN_SRC})
150 target_include_directories(
151 posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
152 target_link_libraries(posix-mock-test gtest)
154 target_compile_options(posix-mock-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
157 target_compile_options(posix-mock-test PRIVATE /utf-8)
159 add_test(NAME posix-mock-test COMMAND posix-mock-test)
160 add_fmt_test(os-test)
163 message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}")
166 # Test that the library can be compiled with exceptions disabled.
167 # -fno-exception is broken in icc: https://github.com/fmtlib/fmt/issues/822.
168 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
169 check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
171 if (HAVE_FNO_EXCEPTIONS_FLAG)
172 add_library(noexception-test ../src/format.cc noexception-test.cc)
173 target_include_directories(
174 noexception-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
175 target_compile_options(noexception-test PRIVATE -fno-exceptions)
176 target_compile_options(noexception-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
179 # Test that the library compiles without locale.
180 add_library(nolocale-test ../src/format.cc)
181 target_include_directories(
182 nolocale-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
183 target_compile_definitions(
184 nolocale-test PRIVATE FMT_STATIC_THOUSANDS_SEPARATOR=1)
187 # These tests are disabled on Windows because they take too long.
188 # They are disabled on GCC < 4.9 because it can not parse UDLs without
189 # a space after `operator""` but that is an incorrect syntax for any more
191 if (FMT_PEDANTIC AND NOT WIN32 AND NOT (
192 CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
193 CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))
194 # Test if incorrect API usages produce compilation error.
195 add_test(compile-error-test ${CMAKE_CTEST_COMMAND}
197 "${CMAKE_CURRENT_SOURCE_DIR}/compile-error-test"
198 "${CMAKE_CURRENT_BINARY_DIR}/compile-error-test"
199 --build-generator ${CMAKE_GENERATOR}
200 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
202 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
203 "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
204 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
205 "-DCXX_STANDARD_FLAG=${CXX_STANDARD_FLAG}"
206 "-DFMT_DIR=${CMAKE_SOURCE_DIR}")
208 # Test if the targets are found from the build directory.
209 add_test(find-package-test ${CMAKE_CTEST_COMMAND}
210 -C ${CMAKE_BUILD_TYPE}
212 "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
213 "${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
214 --build-generator ${CMAKE_GENERATOR}
215 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
217 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
218 "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
219 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
220 "-DFMT_DIR=${PROJECT_BINARY_DIR}"
221 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
222 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
224 # Test if the targets are found when add_subdirectory is used.
225 add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
226 -C ${CMAKE_BUILD_TYPE}
228 "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
229 "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
230 --build-generator ${CMAKE_GENERATOR}
231 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
233 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
234 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
235 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
236 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
239 # This test is disabled on Windows because it is POSIX-specific.
240 if (FMT_PEDANTIC AND NOT WIN32)
241 add_test(static-export-test ${CMAKE_CTEST_COMMAND}
242 -C ${CMAKE_BUILD_TYPE}
244 "${CMAKE_CURRENT_SOURCE_DIR}/static-export-test"
245 "${CMAKE_CURRENT_BINARY_DIR}/static-export-test"
246 --build-generator ${CMAKE_GENERATOR}
247 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
249 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
250 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
251 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
254 # Activate optional CUDA tests if CUDA is found. For version selection see
255 # https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
257 if (${CMAKE_VERSION} VERSION_LESS 3.15)
258 find_package(CUDA 9.0)
260 include(CheckLanguage)
262 if (CMAKE_CUDA_COMPILER)
263 enable_language(CUDA OPTIONAL)
269 add_subdirectory(cuda-test)
270 add_test(NAME cuda-test COMMAND fmt-in-cuda-test)