CMakeLists.txt 16 KB

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