CMakeLists.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. add_subdirectory(gtest)
  2. set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
  3. add_library(test-main STATIC ${TEST_MAIN_SRC})
  4. target_include_directories(test-main PUBLIC
  5. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
  6. target_link_libraries(test-main gtest fmt)
  7. function(add_fmt_executable name)
  8. add_executable(${name} ${ARGN})
  9. # (Wstringop-overflow) - [meta-bug] bogus/missing -Wstringop-overflow warnings
  10. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88443
  11. # Bogus -Wstringop-overflow warning
  12. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100395
  13. # [10 Regression] spurious -Wstringop-overflow writing to a trailing array plus offset
  14. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95353
  15. if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
  16. NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
  17. target_compile_options(${name} PRIVATE -Wno-stringop-overflow)
  18. # The linker flag is needed for LTO.
  19. target_link_libraries(${name} -Wno-stringop-overflow)
  20. endif ()
  21. endfunction()
  22. # Adds a test.
  23. # Usage: add_fmt_test(name srcs...)
  24. function(add_fmt_test name)
  25. cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE" "" "" ${ARGN})
  26. set(sources ${name}.cc ${ADD_FMT_TEST_UNPARSED_ARGUMENTS})
  27. if (ADD_FMT_TEST_HEADER_ONLY)
  28. set(sources ${sources} ${TEST_MAIN_SRC} ../src/os.cc)
  29. set(libs gtest fmt-header-only)
  30. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  31. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-weak-vtables)
  32. endif ()
  33. elseif (ADD_FMT_TEST_MODULE)
  34. set(libs gtest test-module)
  35. set_source_files_properties(${name}.cc PROPERTIES OBJECT_DEPENDS test-module)
  36. else ()
  37. set(libs test-main fmt)
  38. endif ()
  39. add_fmt_executable(${name} ${sources})
  40. target_link_libraries(${name} ${libs})
  41. # Define if certain C++ features can be used.
  42. if (FMT_PEDANTIC)
  43. target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
  44. endif ()
  45. if (FMT_WERROR)
  46. target_compile_options(${name} PRIVATE ${WERROR_FLAG})
  47. endif ()
  48. add_test(NAME ${name} COMMAND ${name})
  49. endfunction()
  50. add_fmt_test(args-test)
  51. add_fmt_test(assert-test)
  52. add_fmt_test(chrono-test)
  53. add_fmt_test(color-test)
  54. add_fmt_test(core-test)
  55. add_fmt_test(gtest-extra-test)
  56. add_fmt_test(format-test mock-allocator.h)
  57. if (MSVC)
  58. target_compile_options(format-test PRIVATE /bigobj)
  59. endif ()
  60. if (NOT (MSVC AND BUILD_SHARED_LIBS))
  61. add_fmt_test(format-impl-test HEADER_ONLY header-only-test.cc)
  62. endif ()
  63. add_fmt_test(ostream-test)
  64. add_fmt_test(compile-test)
  65. add_fmt_test(compile-fp-test HEADER_ONLY)
  66. if (MSVC)
  67. # Without this option, MSVC returns 199711L for the __cplusplus macro.
  68. target_compile_options(compile-fp-test PRIVATE /Zc:__cplusplus)
  69. endif()
  70. add_fmt_test(printf-test)
  71. add_fmt_test(ranges-test ranges-odr-test.cc)
  72. add_fmt_test(scan-test)
  73. add_fmt_test(std-test)
  74. try_compile(compile_result_unused
  75. ${CMAKE_CURRENT_BINARY_DIR}
  76. SOURCES ${CMAKE_CURRENT_LIST_DIR}/detect-stdfs.cc
  77. OUTPUT_VARIABLE RAWOUTPUT)
  78. string(REGEX REPLACE ".*libfound \"([^\"]*)\".*" "\\1" STDLIBFS "${RAWOUTPUT}")
  79. if (STDLIBFS)
  80. target_link_libraries(std-test ${STDLIBFS})
  81. endif ()
  82. add_fmt_test(unicode-test HEADER_ONLY)
  83. if (MSVC)
  84. target_compile_options(unicode-test PRIVATE /utf-8)
  85. endif ()
  86. add_fmt_test(xchar-test)
  87. add_fmt_test(enforce-checks-test)
  88. target_compile_definitions(enforce-checks-test PRIVATE
  89. -DFMT_ENFORCE_COMPILE_STRING)
  90. if (FMT_CAN_MODULE)
  91. # The tests need {fmt} to be compiled as traditional library
  92. # because of visibility of implementation details.
  93. # If module support is present the module tests require a
  94. # test-only module to be built from {fmt}
  95. add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc)
  96. target_compile_features(test-module PUBLIC ${FMT_REQUIRED_FEATURES})
  97. target_include_directories(test-module PUBLIC
  98. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
  99. enable_module(test-module)
  100. add_fmt_test(module-test MODULE test-main.cc)
  101. if (MSVC)
  102. target_compile_options(test-module PRIVATE /utf-8 /Zc:__cplusplus
  103. /Zc:externConstexpr /Zc:inline)
  104. target_compile_options(module-test PRIVATE /utf-8 /Zc:__cplusplus
  105. /Zc:externConstexpr /Zc:inline)
  106. endif ()
  107. endif ()
  108. if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
  109. foreach (flag_var
  110. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  111. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  112. if (${flag_var} MATCHES "^(/|-)(MT|MTd)")
  113. set(MSVC_STATIC_RUNTIME ON)
  114. break()
  115. endif()
  116. endforeach()
  117. endif()
  118. if (NOT MSVC_STATIC_RUNTIME)
  119. add_fmt_executable(posix-mock-test
  120. posix-mock-test.cc ../src/format.cc ${TEST_MAIN_SRC})
  121. target_include_directories(
  122. posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
  123. target_link_libraries(posix-mock-test gtest)
  124. if (FMT_PEDANTIC)
  125. target_compile_options(posix-mock-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
  126. endif ()
  127. add_test(NAME posix-mock-test COMMAND posix-mock-test)
  128. add_fmt_test(os-test)
  129. endif ()
  130. message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}")
  131. if (FMT_PEDANTIC)
  132. # Test that the library can be compiled with exceptions disabled.
  133. # -fno-exception is broken in icc: https://github.com/fmtlib/fmt/issues/822.
  134. if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
  135. check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
  136. endif ()
  137. if (HAVE_FNO_EXCEPTIONS_FLAG)
  138. add_library(noexception-test ../src/format.cc noexception-test.cc)
  139. target_include_directories(
  140. noexception-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
  141. target_compile_options(noexception-test PRIVATE -fno-exceptions)
  142. target_compile_options(noexception-test PRIVATE ${PEDANTIC_COMPILE_FLAGS})
  143. endif ()
  144. # Test that the library compiles without locale.
  145. add_library(nolocale-test ../src/format.cc)
  146. target_include_directories(
  147. nolocale-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
  148. target_compile_definitions(
  149. nolocale-test PRIVATE FMT_STATIC_THOUSANDS_SEPARATOR=1)
  150. endif ()
  151. # These tests are disabled on Windows because they take too long.
  152. if (FMT_PEDANTIC AND NOT WIN32)
  153. # Test if incorrect API usages produce compilation error.
  154. add_test(compile-error-test ${CMAKE_CTEST_COMMAND}
  155. --build-and-test
  156. "${CMAKE_CURRENT_SOURCE_DIR}/compile-error-test"
  157. "${CMAKE_CURRENT_BINARY_DIR}/compile-error-test"
  158. --build-generator ${CMAKE_GENERATOR}
  159. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  160. --build-options
  161. "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
  162. "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
  163. "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
  164. "-DCXX_STANDARD_FLAG=${CXX_STANDARD_FLAG}"
  165. "-DFMT_DIR=${CMAKE_SOURCE_DIR}")
  166. # Test if the targets are found from the build directory.
  167. add_test(find-package-test ${CMAKE_CTEST_COMMAND}
  168. -C ${CMAKE_BUILD_TYPE}
  169. --build-and-test
  170. "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
  171. "${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
  172. --build-generator ${CMAKE_GENERATOR}
  173. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  174. --build-options
  175. "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
  176. "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
  177. "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
  178. "-DFMT_DIR=${PROJECT_BINARY_DIR}"
  179. "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
  180. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  181. # Test if the targets are found when add_subdirectory is used.
  182. add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
  183. -C ${CMAKE_BUILD_TYPE}
  184. --build-and-test
  185. "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
  186. "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
  187. --build-generator ${CMAKE_GENERATOR}
  188. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  189. --build-options
  190. "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
  191. "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
  192. "-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
  193. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  194. endif ()
  195. # This test are disabled on Windows because it is only *NIX issue.
  196. if (FMT_PEDANTIC AND NOT WIN32)
  197. add_test(static-export-test ${CMAKE_CTEST_COMMAND}
  198. -C ${CMAKE_BUILD_TYPE}
  199. --build-and-test
  200. "${CMAKE_CURRENT_SOURCE_DIR}/static-export-test"
  201. "${CMAKE_CURRENT_BINARY_DIR}/static-export-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. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  208. endif ()
  209. # Activate optional CUDA tests if CUDA is found. For version selection see
  210. # https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
  211. if (FMT_CUDA_TEST)
  212. if (${CMAKE_VERSION} VERSION_LESS 3.15)
  213. find_package(CUDA 9.0)
  214. else ()
  215. include(CheckLanguage)
  216. check_language(CUDA)
  217. if (CMAKE_CUDA_COMPILER)
  218. enable_language(CUDA OPTIONAL)
  219. set(CUDA_FOUND TRUE)
  220. endif ()
  221. endif ()
  222. if (CUDA_FOUND)
  223. add_subdirectory(cuda-test)
  224. add_test(NAME cuda-test COMMAND fmt-in-cuda-test)
  225. endif ()
  226. endif ()