You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeLists.txt 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. cmake_minimum_required(VERSION 3.1...3.18)
  2. # Fallback for using newer policies on CMake <3.12.
  3. if(${CMAKE_VERSION} VERSION_LESS 3.12)
  4. cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  5. endif()
  6. # Determine if fmt is built as a subproject (using add_subdirectory)
  7. # or if it is the master project.
  8. set(MASTER_PROJECT OFF)
  9. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  10. set(MASTER_PROJECT ON)
  11. message(STATUS "CMake version: ${CMAKE_VERSION}")
  12. endif ()
  13. # Joins arguments and places the results in ${result_var}.
  14. function(join result_var)
  15. set(result )
  16. foreach (arg ${ARGN})
  17. set(result "${result}${arg}")
  18. endforeach ()
  19. set(${result_var} "${result}" PARENT_SCOPE)
  20. endfunction()
  21. include(CMakeParseArguments)
  22. # Sets a cache variable with a docstring joined from multiple arguments:
  23. # set(<variable> <value>... CACHE <type> <docstring>...)
  24. # This allows splitting a long docstring for readability.
  25. function(set_verbose)
  26. # cmake_parse_arguments is broken in CMake 3.4 (cannot parse CACHE) so use
  27. # list instead.
  28. list(GET ARGN 0 var)
  29. list(REMOVE_AT ARGN 0)
  30. list(GET ARGN 0 val)
  31. list(REMOVE_AT ARGN 0)
  32. list(REMOVE_AT ARGN 0)
  33. list(GET ARGN 0 type)
  34. list(REMOVE_AT ARGN 0)
  35. join(doc ${ARGN})
  36. set(${var} ${val} CACHE ${type} ${doc})
  37. endfunction()
  38. # Set the default CMAKE_BUILD_TYPE to Release.
  39. # This should be done before the project command since the latter can set
  40. # CMAKE_BUILD_TYPE itself (it does so for nmake).
  41. if (MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
  42. set_verbose(CMAKE_BUILD_TYPE Release CACHE STRING
  43. "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or "
  44. "CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
  45. endif ()
  46. project(FMT CXX)
  47. include(GNUInstallDirs)
  48. set_verbose(FMT_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING
  49. "Installation directory for include files, a relative path that "
  50. "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.")
  51. option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF)
  52. option(FMT_WERROR "Halt the compilation with an error on compiler warnings."
  53. OFF)
  54. # Options that control generation of various targets.
  55. option(FMT_DOC "Generate the doc target." ${MASTER_PROJECT})
  56. option(FMT_INSTALL "Generate the install target." ${MASTER_PROJECT})
  57. option(FMT_TEST "Generate the test target." ${MASTER_PROJECT})
  58. option(FMT_FUZZ "Generate the fuzz target." OFF)
  59. option(FMT_CUDA_TEST "Generate the cuda-test target." OFF)
  60. option(FMT_OS "Include core requiring OS (Windows/Posix) " ON)
  61. # Get version from core.h
  62. file(READ include/fmt/core.h core_h)
  63. if (NOT core_h MATCHES "FMT_VERSION ([0-9]+)([0-9][0-9])([0-9][0-9])")
  64. message(FATAL_ERROR "Cannot get FMT_VERSION from core.h.")
  65. endif ()
  66. # Use math to skip leading zeros if any.
  67. math(EXPR CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
  68. math(EXPR CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
  69. math(EXPR CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
  70. join(FMT_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.
  71. ${CPACK_PACKAGE_VERSION_PATCH})
  72. message(STATUS "Version: ${FMT_VERSION}")
  73. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  74. if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  75. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  76. endif ()
  77. if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  78. set(PEDANTIC_COMPILE_FLAGS -pedantic-errors -Wall -Wextra -pedantic
  79. -Wold-style-cast -Wundef
  80. -Wredundant-decls -Wwrite-strings -Wpointer-arith
  81. -Wcast-qual -Wformat=2 -Wmissing-include-dirs
  82. -Wcast-align
  83. -Wctor-dtor-privacy -Wdisabled-optimization
  84. -Winvalid-pch -Woverloaded-virtual
  85. -Wconversion -Wswitch-enum -Wundef
  86. -Wno-ctor-dtor-privacy -Wno-format-nonliteral)
  87. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
  88. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wnoexcept
  89. -Wno-dangling-else -Wno-unused-local-typedefs)
  90. endif ()
  91. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  92. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wdouble-promotion
  93. -Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast
  94. -Wvector-operation-performance -Wsized-deallocation)
  95. endif ()
  96. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
  97. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
  98. -Wnull-dereference -Wduplicated-cond)
  99. endif ()
  100. set(WERROR_FLAG -Werror)
  101. endif ()
  102. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  103. set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
  104. -Wdeprecated -Wweak-vtables)
  105. set(WERROR_FLAG -Werror)
  106. endif ()
  107. if (MSVC)
  108. set(PEDANTIC_COMPILE_FLAGS /W3)
  109. set(WERROR_FLAG /WX)
  110. endif ()
  111. set(strtod_l_headers stdlib.h)
  112. if (APPLE)
  113. set(strtod_l_headers ${strtod_l_headers} xlocale.h)
  114. endif ()
  115. include(CheckSymbolExists)
  116. if (WIN32)
  117. check_symbol_exists(_strtod_l "${strtod_l_headers}" HAVE_STRTOD_L)
  118. else ()
  119. check_symbol_exists(strtod_l "${strtod_l_headers}" HAVE_STRTOD_L)
  120. endif ()
  121. function(add_headers VAR)
  122. set(headers ${${VAR}})
  123. foreach (header ${ARGN})
  124. set(headers ${headers} include/fmt/${header})
  125. endforeach()
  126. set(${VAR} ${headers} PARENT_SCOPE)
  127. endfunction()
  128. # Define the fmt library, its includes and the needed defines.
  129. add_headers(FMT_HEADERS chrono.h color.h compile.h core.h format.h format-inl.h
  130. locale.h os.h ostream.h posix.h printf.h ranges.h)
  131. if (FMT_OS)
  132. set(FMT_SOURCES src/format.cc src/os.cc)
  133. else()
  134. set(FMT_SOURCES src/format.cc)
  135. endif ()
  136. add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} README.rst)
  137. add_library(fmt::fmt ALIAS fmt)
  138. if (HAVE_STRTOD_L)
  139. target_compile_definitions(fmt PUBLIC FMT_LOCALE)
  140. endif ()
  141. if (FMT_WERROR)
  142. target_compile_options(fmt PRIVATE ${WERROR_FLAG})
  143. endif ()
  144. if (FMT_PEDANTIC)
  145. target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
  146. endif ()
  147. target_compile_features(fmt INTERFACE ${FMT_REQUIRED_FEATURES})
  148. target_include_directories(fmt PUBLIC
  149. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  150. $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
  151. set(FMT_DEBUG_POSTFIX d CACHE STRING "Debug library postfix.")
  152. set_target_properties(fmt PROPERTIES
  153. VERSION ${FMT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
  154. DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}")
  155. # Set FMT_LIB_NAME for pkg-config fmt.pc. We cannot use the OUTPUT_NAME target
  156. # property because it's not set by default.
  157. set(FMT_LIB_NAME fmt)
  158. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  159. set(FMT_LIB_NAME ${FMT_LIB_NAME}${FMT_DEBUG_POSTFIX})
  160. endif ()
  161. if (BUILD_SHARED_LIBS)
  162. if (UNIX AND NOT APPLE AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS" AND
  163. NOT EMSCRIPTEN)
  164. # Fix rpmlint warning:
  165. # unused-direct-shlib-dependency /usr/lib/libformat.so.1.1.0 /lib/libm.so.6.
  166. target_link_libraries(fmt -Wl,--as-needed)
  167. endif ()
  168. target_compile_definitions(fmt PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
  169. endif ()
  170. if (FMT_SAFE_DURATION_CAST)
  171. target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
  172. endif()
  173. add_library(fmt-header-only INTERFACE)
  174. add_library(fmt::fmt-header-only ALIAS fmt-header-only)
  175. target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
  176. target_compile_features(fmt-header-only INTERFACE ${FMT_REQUIRED_FEATURES})
  177. target_include_directories(fmt-header-only INTERFACE
  178. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  179. $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
  180. # Install targets.
  181. if (FMT_INSTALL)
  182. include(CMakePackageConfigHelpers)
  183. set_verbose(FMT_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/fmt CACHE STRING
  184. "Installation directory for cmake files, a relative path that "
  185. "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute "
  186. "path.")
  187. set(version_config ${PROJECT_BINARY_DIR}/fmt-config-version.cmake)
  188. set(project_config ${PROJECT_BINARY_DIR}/fmt-config.cmake)
  189. set(pkgconfig ${PROJECT_BINARY_DIR}/fmt.pc)
  190. set(targets_export_name fmt-targets)
  191. set_verbose(FMT_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
  192. "Installation directory for libraries, a relative path that "
  193. "will be joined to ${CMAKE_INSTALL_PREFIX} or an absolute path.")
  194. set_verbose(FMT_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH
  195. "Installation directory for pkgconfig (.pc) files, a relative "
  196. "path that will be joined with ${CMAKE_INSTALL_PREFIX} or an "
  197. "absolute path.")
  198. # Generate the version, config and target files into the build directory.
  199. write_basic_package_version_file(
  200. ${version_config}
  201. VERSION ${FMT_VERSION}
  202. COMPATIBILITY AnyNewerVersion)
  203. join_paths(libdir_for_pc_file "\${exec_prefix}" "${FMT_LIB_DIR}")
  204. join_paths(includedir_for_pc_file "\${prefix}" "${FMT_INC_DIR}")
  205. configure_file(
  206. "${PROJECT_SOURCE_DIR}/support/cmake/fmt.pc.in"
  207. "${pkgconfig}"
  208. @ONLY)
  209. configure_package_config_file(
  210. ${PROJECT_SOURCE_DIR}/support/cmake/fmt-config.cmake.in
  211. ${project_config}
  212. INSTALL_DESTINATION ${FMT_CMAKE_DIR})
  213. set(INSTALL_TARGETS fmt fmt-header-only)
  214. # Use a namespace because CMake provides better diagnostics for namespaced
  215. # imported targets.
  216. export(TARGETS ${INSTALL_TARGETS} NAMESPACE fmt::
  217. FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
  218. # Install version, config and target files.
  219. install(
  220. FILES ${project_config} ${version_config}
  221. DESTINATION ${FMT_CMAKE_DIR})
  222. install(EXPORT ${targets_export_name} DESTINATION ${FMT_CMAKE_DIR}
  223. NAMESPACE fmt::)
  224. # Install the library and headers.
  225. install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
  226. LIBRARY DESTINATION ${FMT_LIB_DIR}
  227. ARCHIVE DESTINATION ${FMT_LIB_DIR}
  228. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  229. install(FILES $<TARGET_PDB_FILE:${INSTALL_TARGETS}>
  230. DESTINATION ${FMT_LIB_DIR} OPTIONAL)
  231. install(FILES ${FMT_HEADERS} DESTINATION "${FMT_INC_DIR}/fmt")
  232. install(FILES "${pkgconfig}" DESTINATION "${FMT_PKGCONFIG_DIR}")
  233. endif ()
  234. if (FMT_DOC)
  235. add_subdirectory(doc)
  236. endif ()
  237. if (FMT_TEST)
  238. enable_testing()
  239. add_subdirectory(test)
  240. endif ()
  241. # Control fuzzing independent of the unit tests.
  242. if (FMT_FUZZ)
  243. add_subdirectory(test/fuzzing)
  244. # The FMT_FUZZ macro is used to prevent resource exhaustion in fuzzing
  245. # mode and make fuzzing practically possible. It is similar to
  246. # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION but uses a different name to
  247. # avoid interfering with fuzzing of projects that use {fmt}.
  248. # See also https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode.
  249. target_compile_definitions(fmt PUBLIC FMT_FUZZ)
  250. endif ()
  251. set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore)
  252. if (MASTER_PROJECT AND EXISTS ${gitignore})
  253. # Get the list of ignored files from .gitignore.
  254. file (STRINGS ${gitignore} lines)
  255. list(REMOVE_ITEM lines /doc/html)
  256. foreach (line ${lines})
  257. string(REPLACE "." "[.]" line "${line}")
  258. string(REPLACE "*" ".*" line "${line}")
  259. set(ignored_files ${ignored_files} "${line}$" "${line}/")
  260. endforeach ()
  261. set(ignored_files ${ignored_files}
  262. /.git /breathe /format-benchmark sphinx/ .buildinfo .doctrees)
  263. set(CPACK_SOURCE_GENERATOR ZIP)
  264. set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
  265. set(CPACK_SOURCE_PACKAGE_FILE_NAME fmt-${FMT_VERSION})
  266. set(CPACK_PACKAGE_NAME fmt)
  267. set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.rst)
  268. include(CPack)
  269. endif ()