CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. if (NOT DEFINED FMT_MASTER_PROJECT)
  9. set(FMT_MASTER_PROJECT OFF)
  10. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  11. set(FMT_MASTER_PROJECT ON)
  12. message(STATUS "CMake version: ${CMAKE_VERSION}")
  13. endif ()
  14. endif ()
  15. # Joins arguments and places the results in ${result_var}.
  16. function(join result_var)
  17. set(result "")
  18. foreach (arg ${ARGN})
  19. set(result "${result}${arg}")
  20. endforeach ()
  21. set(${result_var} "${result}" PARENT_SCOPE)
  22. endfunction()
  23. function(enable_module target)
  24. if (MSVC)
  25. set(BMI ${CMAKE_CURRENT_BINARY_DIR}/${target}.ifc)
  26. target_compile_options(${target}
  27. PRIVATE /interface /ifcOutput ${BMI}
  28. INTERFACE /reference fmt=${BMI})
  29. endif ()
  30. set_target_properties(${target} PROPERTIES ADDITIONAL_CLEAN_FILES ${BMI})
  31. set_source_files_properties(${BMI} PROPERTIES GENERATED ON)
  32. endfunction()
  33. include(CMakeParseArguments)
  34. # Sets a cache variable with a docstring joined from multiple arguments:
  35. # set(<variable> <value>... CACHE <type> <docstring>...)
  36. # This allows splitting a long docstring for readability.
  37. function(set_verbose)
  38. # cmake_parse_arguments is broken in CMake 3.4 (cannot parse CACHE) so use
  39. # list instead.
  40. list(GET ARGN 0 var)
  41. list(REMOVE_AT ARGN 0)
  42. list(GET ARGN 0 val)
  43. list(REMOVE_AT ARGN 0)
  44. list(REMOVE_AT ARGN 0)
  45. list(GET ARGN 0 type)
  46. list(REMOVE_AT ARGN 0)
  47. join(doc ${ARGN})
  48. set(${var} ${val} CACHE ${type} ${doc})
  49. endfunction()
  50. # Set the default CMAKE_BUILD_TYPE to Release.
  51. # This should be done before the project command since the latter can set
  52. # CMAKE_BUILD_TYPE itself (it does so for nmake).
  53. if (FMT_MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
  54. set_verbose(CMAKE_BUILD_TYPE Release CACHE STRING
  55. "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or "
  56. "CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
  57. endif ()
  58. project(FMT CXX)
  59. include(GNUInstallDirs)
  60. set_verbose(FMT_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING
  61. "Installation directory for include files, a relative path that "
  62. "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.")
  63. option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF)
  64. option(FMT_WERROR "Halt the compilation with an error on compiler warnings."
  65. OFF)
  66. # Options that control generation of various targets.
  67. option(FMT_DOC "Generate the doc target." ${FMT_MASTER_PROJECT})
  68. option(FMT_INSTALL "Generate the install target." ${FMT_MASTER_PROJECT})
  69. option(FMT_TEST "Generate the test target." ${FMT_MASTER_PROJECT})
  70. option(FMT_FUZZ "Generate the fuzz target." OFF)
  71. option(FMT_CUDA_TEST "Generate the cuda-test target." OFF)
  72. option(FMT_OS "Include core requiring OS (Windows/Posix) " ON)
  73. option(FMT_MODULE "Build a module instead of a traditional library." OFF)
  74. option(FMT_SYSTEM_HEADERS "Expose headers with marking them as system." OFF)
  75. set(FMT_CAN_MODULE OFF)
  76. if (CMAKE_CXX_STANDARD GREATER 17 AND
  77. # msvc 16.10-pre4
  78. MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 19.29.30035)
  79. set(FMT_CAN_MODULE OFF)
  80. endif ()
  81. if (NOT FMT_CAN_MODULE)
  82. set(FMT_MODULE OFF)
  83. message(STATUS "Module support is disabled.")
  84. endif ()
  85. if (FMT_TEST AND FMT_MODULE)
  86. # The tests require {fmt} to be compiled as traditional library
  87. message(STATUS "Testing is incompatible with build mode 'module'.")
  88. endif ()
  89. set(FMT_SYSTEM_HEADERS_ATTRIBUTE "")
  90. if (FMT_SYSTEM_HEADERS)
  91. set(FMT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
  92. endif ()
  93. # Get version from core.h
  94. file(READ include/fmt/core.h core_h)
  95. if (NOT core_h MATCHES "FMT_VERSION ([0-9]+)([0-9][0-9])([0-9][0-9])")
  96. message(FATAL_ERROR "Cannot get FMT_VERSION from core.h.")
  97. endif ()
  98. # Use math to skip leading zeros if any.
  99. math(EXPR CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
  100. math(EXPR CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
  101. math(EXPR CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
  102. join(FMT_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.
  103. ${CPACK_PACKAGE_VERSION_PATCH})
  104. message(STATUS "Version: ${FMT_VERSION}")
  105. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  106. if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  107. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  108. endif ()
  109. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
  110. "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake")
  111. include(cxx14)
  112. include(JoinPaths)
  113. list(FIND CMAKE_CXX_COMPILE_FEATURES "cxx_variadic_templates" index)
  114. if (${index} GREATER -1)
  115. # Use cxx_variadic_templates instead of more appropriate cxx_std_11 for
  116. # compatibility with older CMake versions.
  117. set(FMT_REQUIRED_FEATURES cxx_variadic_templates)
  118. endif ()
  119. message(STATUS "Required features: ${FMT_REQUIRED_FEATURES}")
  120. if (FMT_MASTER_PROJECT AND NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
  121. set_verbose(CMAKE_CXX_VISIBILITY_PRESET hidden CACHE STRING
  122. "Preset for the export of private symbols")
  123. set_property(CACHE CMAKE_CXX_VISIBILITY_PRESET PROPERTY STRINGS
  124. hidden default)
  125. endif ()
  126. if (FMT_MASTER_PROJECT AND NOT DEFINED CMAKE_VISIBILITY_INLINES_HIDDEN)
  127. set_verbose(CMAKE_VISIBILITY_INLINES_HIDDEN ON CACHE BOOL
  128. "Whether to add a compile flag to hide symbols of inline functions")
  129. endif ()
  130. if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  131. set(PEDANTIC_COMPILE_FLAGS -pedantic-errors -Wall -Wextra -pedantic
  132. -Wold-style-cast -Wundef
  133. -Wredundant-decls -Wwrite-strings -Wpointer-arith
  134. -Wcast-qual -Wformat=2 -Wmissing-include-dirs
  135. -Wcast-align
  136. -Wctor-dtor-privacy -Wdisabled-optimization
  137. -Winvalid-pch -Woverloaded-virtual
  138. -Wconversion -Wundef
  139. -Wno-ctor-dtor-privacy -Wno-format-nonliteral)
  140. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
  141. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
  142. -Wno-dangling-else -Wno-unused-local-typedefs)
  143. endif ()
  144. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  145. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wdouble-promotion
  146. -Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast
  147. -Wvector-operation-performance -Wsized-deallocation -Wshadow)
  148. endif ()
  149. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
  150. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
  151. -Wnull-dereference -Wduplicated-cond)
  152. endif ()
  153. set(WERROR_FLAG -Werror)
  154. endif ()
  155. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  156. set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
  157. -Wdeprecated -Wweak-vtables -Wshadow
  158. -Wno-gnu-zero-variadic-macro-arguments)
  159. check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
  160. if (HAS_NULLPTR_WARNING)
  161. set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
  162. -Wzero-as-null-pointer-constant)
  163. endif ()
  164. set(WERROR_FLAG -Werror)
  165. endif ()
  166. if (MSVC)
  167. set(PEDANTIC_COMPILE_FLAGS /W3)
  168. set(WERROR_FLAG /WX)
  169. endif ()
  170. if (FMT_MASTER_PROJECT AND CMAKE_GENERATOR MATCHES "Visual Studio")
  171. # If Microsoft SDK is installed create script run-msbuild.bat that
  172. # calls SetEnv.cmd to set up build environment and runs msbuild.
  173. # It is useful when building Visual Studio projects with the SDK
  174. # toolchain rather than Visual Studio.
  175. include(FindSetEnv)
  176. if (WINSDK_SETENV)
  177. set(MSBUILD_SETUP "call \"${WINSDK_SETENV}\"")
  178. endif ()
  179. # Set FrameworkPathOverride to get rid of MSB3644 warnings.
  180. join(netfxpath
  181. "C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\"
  182. ".NETFramework\\v4.0")
  183. file(WRITE run-msbuild.bat "
  184. ${MSBUILD_SETUP}
  185. ${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
  186. endif ()
  187. function(add_headers VAR)
  188. set(headers ${${VAR}})
  189. foreach (header ${ARGN})
  190. set(headers ${headers} include/fmt/${header})
  191. endforeach()
  192. set(${VAR} ${headers} PARENT_SCOPE)
  193. endfunction()
  194. # Define the fmt library, its includes and the needed defines.
  195. add_headers(FMT_HEADERS args.h chrono.h color.h compile.h core.h format.h
  196. format-inl.h os.h ostream.h printf.h ranges.h std.h
  197. xchar.h)
  198. if (FMT_MODULE)
  199. set(FMT_SOURCES src/fmt.cc)
  200. elseif (FMT_OS)
  201. set(FMT_SOURCES src/format.cc src/os.cc)
  202. else()
  203. set(FMT_SOURCES src/format.cc)
  204. endif ()
  205. add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} README.rst ChangeLog.rst)
  206. add_library(fmt::fmt ALIAS fmt)
  207. if (FMT_WERROR)
  208. target_compile_options(fmt PRIVATE ${WERROR_FLAG})
  209. endif ()
  210. if (FMT_PEDANTIC)
  211. target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
  212. endif ()
  213. if (FMT_MODULE)
  214. enable_module(fmt)
  215. endif ()
  216. target_compile_features(fmt INTERFACE ${FMT_REQUIRED_FEATURES})
  217. target_include_directories(fmt ${FMT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
  218. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  219. $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
  220. set(FMT_DEBUG_POSTFIX d CACHE STRING "Debug library postfix.")
  221. set_target_properties(fmt PROPERTIES
  222. VERSION ${FMT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
  223. PUBLIC_HEADER "${FMT_HEADERS}"
  224. DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}")
  225. # Set FMT_LIB_NAME for pkg-config fmt.pc. We cannot use the OUTPUT_NAME target
  226. # property because it's not set by default.
  227. set(FMT_LIB_NAME fmt)
  228. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  229. set(FMT_LIB_NAME ${FMT_LIB_NAME}${FMT_DEBUG_POSTFIX})
  230. endif ()
  231. if (BUILD_SHARED_LIBS)
  232. target_compile_definitions(fmt PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
  233. endif ()
  234. if (FMT_SAFE_DURATION_CAST)
  235. target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
  236. endif()
  237. add_library(fmt-header-only INTERFACE)
  238. add_library(fmt::fmt-header-only ALIAS fmt-header-only)
  239. target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
  240. target_compile_features(fmt-header-only INTERFACE ${FMT_REQUIRED_FEATURES})
  241. target_include_directories(fmt-header-only ${FMT_SYSTEM_HEADERS_ATTRIBUTE} INTERFACE
  242. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  243. $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
  244. # Install targets.
  245. if (FMT_INSTALL)
  246. include(CMakePackageConfigHelpers)
  247. set_verbose(FMT_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/fmt CACHE STRING
  248. "Installation directory for cmake files, a relative path that "
  249. "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute "
  250. "path.")
  251. set(version_config ${PROJECT_BINARY_DIR}/fmt-config-version.cmake)
  252. set(project_config ${PROJECT_BINARY_DIR}/fmt-config.cmake)
  253. set(pkgconfig ${PROJECT_BINARY_DIR}/fmt.pc)
  254. set(targets_export_name fmt-targets)
  255. set_verbose(FMT_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
  256. "Installation directory for libraries, a relative path that "
  257. "will be joined to ${CMAKE_INSTALL_PREFIX} or an absolute path.")
  258. set_verbose(FMT_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH
  259. "Installation directory for pkgconfig (.pc) files, a relative "
  260. "path that will be joined with ${CMAKE_INSTALL_PREFIX} or an "
  261. "absolute path.")
  262. # Generate the version, config and target files into the build directory.
  263. write_basic_package_version_file(
  264. ${version_config}
  265. VERSION ${FMT_VERSION}
  266. COMPATIBILITY AnyNewerVersion)
  267. join_paths(libdir_for_pc_file "\${exec_prefix}" "${FMT_LIB_DIR}")
  268. join_paths(includedir_for_pc_file "\${prefix}" "${FMT_INC_DIR}")
  269. configure_file(
  270. "${PROJECT_SOURCE_DIR}/support/cmake/fmt.pc.in"
  271. "${pkgconfig}"
  272. @ONLY)
  273. configure_package_config_file(
  274. ${PROJECT_SOURCE_DIR}/support/cmake/fmt-config.cmake.in
  275. ${project_config}
  276. INSTALL_DESTINATION ${FMT_CMAKE_DIR})
  277. set(INSTALL_TARGETS fmt fmt-header-only)
  278. # Install the library and headers.
  279. install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
  280. LIBRARY DESTINATION ${FMT_LIB_DIR}
  281. ARCHIVE DESTINATION ${FMT_LIB_DIR}
  282. PUBLIC_HEADER DESTINATION "${FMT_INC_DIR}/fmt"
  283. FRAMEWORK DESTINATION "."
  284. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  285. # Use a namespace because CMake provides better diagnostics for namespaced
  286. # imported targets.
  287. export(TARGETS ${INSTALL_TARGETS} NAMESPACE fmt::
  288. FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
  289. # Install version, config and target files.
  290. install(
  291. FILES ${project_config} ${version_config}
  292. DESTINATION ${FMT_CMAKE_DIR})
  293. install(EXPORT ${targets_export_name} DESTINATION ${FMT_CMAKE_DIR}
  294. NAMESPACE fmt::)
  295. install(FILES $<TARGET_PDB_FILE:${INSTALL_TARGETS}>
  296. DESTINATION ${FMT_LIB_DIR} OPTIONAL)
  297. install(FILES "${pkgconfig}" DESTINATION "${FMT_PKGCONFIG_DIR}")
  298. endif ()
  299. if (FMT_DOC)
  300. add_subdirectory(doc)
  301. endif ()
  302. if (FMT_TEST)
  303. enable_testing()
  304. add_subdirectory(test)
  305. endif ()
  306. # Control fuzzing independent of the unit tests.
  307. if (FMT_FUZZ)
  308. add_subdirectory(test/fuzzing)
  309. # The FMT_FUZZ macro is used to prevent resource exhaustion in fuzzing
  310. # mode and make fuzzing practically possible. It is similar to
  311. # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION but uses a different name to
  312. # avoid interfering with fuzzing of projects that use {fmt}.
  313. # See also https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode.
  314. target_compile_definitions(fmt PUBLIC FMT_FUZZ)
  315. endif ()
  316. set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore)
  317. if (FMT_MASTER_PROJECT AND EXISTS ${gitignore})
  318. # Get the list of ignored files from .gitignore.
  319. file (STRINGS ${gitignore} lines)
  320. list(REMOVE_ITEM lines /doc/html)
  321. foreach (line ${lines})
  322. string(REPLACE "." "[.]" line "${line}")
  323. string(REPLACE "*" ".*" line "${line}")
  324. set(ignored_files ${ignored_files} "${line}$" "${line}/")
  325. endforeach ()
  326. set(ignored_files ${ignored_files}
  327. /.git /breathe /format-benchmark sphinx/ .buildinfo .doctrees)
  328. set(CPACK_SOURCE_GENERATOR ZIP)
  329. set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
  330. set(CPACK_SOURCE_PACKAGE_FILE_NAME fmt-${FMT_VERSION})
  331. set(CPACK_PACKAGE_NAME fmt)
  332. set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.rst)
  333. include(CPack)
  334. endif ()