comparison dep/fmt/test/CMakeLists.txt @ 343:1faa72660932

*: transfer back to cmake from autotools autotools just made lots of things more complicated than they should have and many things broke (i.e. translations)
author Paper <paper@paper.us.eu.org>
date Thu, 20 Jun 2024 05:56:06 -0400
parents
children
comparison
equal deleted inserted replaced
342:adb79bdde329 343:1faa72660932
1 add_subdirectory(gtest)
2
3 include(CheckSymbolExists)
4
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)
10
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)
24 endif ()
25 endfunction()
26
27 # Adds a test.
28 # Usage: add_fmt_test(name srcs...)
29 function(add_fmt_test name)
30 cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE" "" "" ${ARGN})
31
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)
38 endif ()
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)
42 else ()
43 set(libs test-main fmt)
44 endif ()
45 add_fmt_executable(${name} ${sources})
46 target_link_libraries(${name} ${libs})
47
48 # Define if certain C++ features can be used.
49 if (FMT_PEDANTIC)
50 target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
51 endif ()
52 if (FMT_WERROR)
53 target_compile_options(${name} PRIVATE ${WERROR_FLAG})
54 endif ()
55 add_test(NAME ${name} COMMAND ${name})
56 endfunction()
57
58 if (FMT_MODULE)
59 return ()
60 endif ()
61
62 add_fmt_test(args-test)
63 add_fmt_test(assert-test)
64 add_fmt_test(chrono-test)
65 add_fmt_test(color-test)
66 add_fmt_test(core-test)
67 add_fmt_test(gtest-extra-test)
68 add_fmt_test(format-test mock-allocator.h)
69 if (MSVC)
70 target_compile_options(format-test PRIVATE /bigobj)
71 endif ()
72 if (NOT (MSVC AND BUILD_SHARED_LIBS))
73 add_fmt_test(format-impl-test HEADER_ONLY header-only-test.cc)
74 endif ()
75 add_fmt_test(ostream-test)
76 add_fmt_test(compile-test)
77 add_fmt_test(compile-fp-test HEADER_ONLY)
78 if (MSVC)
79 # Without this option, MSVC returns 199711L for the __cplusplus macro.
80 target_compile_options(compile-fp-test PRIVATE /Zc:__cplusplus)
81 endif()
82 add_fmt_test(printf-test)
83 add_fmt_test(ranges-test ranges-odr-test.cc)
84
85 add_fmt_test(scan-test)
86 check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
87 if (HAVE_STRPTIME)
88 target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME)
89 endif ()
90
91 add_fmt_test(std-test)
92 try_compile(compile_result_unused
93 ${CMAKE_CURRENT_BINARY_DIR}
94 SOURCES ${CMAKE_CURRENT_LIST_DIR}/detect-stdfs.cc
95 OUTPUT_VARIABLE RAWOUTPUT)
96 string(REGEX REPLACE ".*libfound \"([^\"]*)\".*" "\\1" STDLIBFS "${RAWOUTPUT}")
97 if (STDLIBFS)
98 target_link_libraries(std-test ${STDLIBFS})
99 endif ()
100 add_fmt_test(unicode-test HEADER_ONLY)
101 if (MSVC)
102 target_compile_options(unicode-test PRIVATE /utf-8)
103 endif ()
104 add_fmt_test(xchar-test)
105 add_fmt_test(enforce-checks-test)
106 target_compile_definitions(enforce-checks-test PRIVATE
107 -DFMT_ENFORCE_COMPILE_STRING)
108
109 if (FMT_MODULE)
110 # The tests need {fmt} to be compiled as traditional library
111 # because of visibility of implementation details.
112 # If module support is present the module tests require a
113 # test-only module to be built from {fmt}
114 add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc)
115 target_compile_features(test-module PUBLIC cxx_std_11)
116 target_include_directories(test-module PUBLIC
117 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
118 enable_module(test-module)
119
120 add_fmt_test(module-test MODULE test-main.cc)
121 if (MSVC)
122 target_compile_options(test-module PRIVATE /utf-8 /Zc:__cplusplus
123 /Zc:externConstexpr /Zc:inline)
124 target_compile_options(module-test PRIVATE /utf-8 /Zc:__cplusplus
125 /Zc:externConstexpr /Zc:inline)
126 endif ()
127 endif ()
128
129 if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
130 foreach (flag_var
131 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
132 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
133 if (${flag_var} MATCHES "^(/|-)(MT|MTd)")
134 set(MSVC_STATIC_RUNTIME ON)
135 break()
136 endif()
137 endforeach()
138 endif()
139
140 if (NOT MSVC_STATIC_RUNTIME)
141 add_fmt_executable(posix-mock-test
142 posix-mock-test.cc ../src/format.cc ${TEST_MAIN_SRC})
143 target_include_directories(
144 posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
145 target_link_libraries(posix-mock-test gtest)
146 if (FMT_PEDANTIC)
147 target_compile_options(posix-mock-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
148 endif ()
149 add_test(NAME posix-mock-test COMMAND posix-mock-test)
150 add_fmt_test(os-test)
151 endif ()
152
153 message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}")
154
155 if (FMT_PEDANTIC)
156 # Test that the library can be compiled with exceptions disabled.
157 # -fno-exception is broken in icc: https://github.com/fmtlib/fmt/issues/822.
158 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
159 check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
160 endif ()
161 if (HAVE_FNO_EXCEPTIONS_FLAG)
162 add_library(noexception-test ../src/format.cc noexception-test.cc)
163 target_include_directories(
164 noexception-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
165 target_compile_options(noexception-test PRIVATE -fno-exceptions)
166 target_compile_options(noexception-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
167 endif ()
168
169 # Test that the library compiles without locale.
170 add_library(nolocale-test ../src/format.cc)
171 target_include_directories(
172 nolocale-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
173 target_compile_definitions(
174 nolocale-test PRIVATE FMT_STATIC_THOUSANDS_SEPARATOR=1)
175 endif ()
176
177 # These tests are disabled on Windows because they take too long.
178 # They are disabled on GCC < 4.9 because it can not parse UDLs without
179 # a space after `operator""` but that is an incorrect syntax for any more
180 # modern compiler.
181 if (FMT_PEDANTIC AND NOT WIN32 AND NOT (
182 CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
183 CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))
184 # Test if incorrect API usages produce compilation error.
185 add_test(compile-error-test ${CMAKE_CTEST_COMMAND}
186 --build-and-test
187 "${CMAKE_CURRENT_SOURCE_DIR}/compile-error-test"
188 "${CMAKE_CURRENT_BINARY_DIR}/compile-error-test"
189 --build-generator ${CMAKE_GENERATOR}
190 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
191 --build-options
192 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
193 "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
194 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
195 "-DCXX_STANDARD_FLAG=${CXX_STANDARD_FLAG}"
196 "-DFMT_DIR=${CMAKE_SOURCE_DIR}")
197
198 # Test if the targets are found from the build directory.
199 add_test(find-package-test ${CMAKE_CTEST_COMMAND}
200 -C ${CMAKE_BUILD_TYPE}
201 --build-and-test
202 "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
203 "${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
204 --build-generator ${CMAKE_GENERATOR}
205 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
206 --build-options
207 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
208 "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
209 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
210 "-DFMT_DIR=${PROJECT_BINARY_DIR}"
211 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
212 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
213
214 # Test if the targets are found when add_subdirectory is used.
215 add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
216 -C ${CMAKE_BUILD_TYPE}
217 --build-and-test
218 "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
219 "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
220 --build-generator ${CMAKE_GENERATOR}
221 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
222 --build-options
223 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
224 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
225 "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
226 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
227 endif ()
228
229 # This test are disabled on Windows because it is only *NIX issue.
230 if (FMT_PEDANTIC AND NOT WIN32)
231 add_test(static-export-test ${CMAKE_CTEST_COMMAND}
232 -C ${CMAKE_BUILD_TYPE}
233 --build-and-test
234 "${CMAKE_CURRENT_SOURCE_DIR}/static-export-test"
235 "${CMAKE_CURRENT_BINARY_DIR}/static-export-test"
236 --build-generator ${CMAKE_GENERATOR}
237 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
238 --build-options
239 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
240 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
241 "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
242 endif ()
243
244 # Activate optional CUDA tests if CUDA is found. For version selection see
245 # https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
246 if (FMT_CUDA_TEST)
247 if (${CMAKE_VERSION} VERSION_LESS 3.15)
248 find_package(CUDA 9.0)
249 else ()
250 include(CheckLanguage)
251 check_language(CUDA)
252 if (CMAKE_CUDA_COMPILER)
253 enable_language(CUDA OPTIONAL)
254 set(CUDA_FOUND TRUE)
255 endif ()
256 endif ()
257
258 if (CUDA_FOUND)
259 add_subdirectory(cuda-test)
260 add_test(NAME cuda-test COMMAND fmt-in-cuda-test)
261 endif ()
262 endif ()