CMakeLists.txt 19 KB

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