1 cmake_minimum_required(VERSION 3.8...3.28)
3 # Fallback for using newer policies on CMake <3.12.
4 if (${CMAKE_VERSION} VERSION_LESS 3.12)
5 cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
8 # Determine if fmt is built as a subproject (using add_subdirectory)
9 # or if it is the master project.
10 if (NOT DEFINED FMT_MASTER_PROJECT)
11 set(FMT_MASTER_PROJECT OFF)
12 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
13 set(FMT_MASTER_PROJECT ON)
14 message(STATUS "CMake version: ${CMAKE_VERSION}")
18 # Joins arguments and places the results in ${result_var}.
19 function(join result_var)
22 set(result "${result}${arg}")
24 set(${result_var} "${result}" PARENT_SCOPE)
27 # DEPRECATED! Should be merged into add_module_library.
28 function(enable_module target)
30 set(BMI ${CMAKE_CURRENT_BINARY_DIR}/${target}.ifc)
31 target_compile_options(${target}
32 PRIVATE /interface /ifcOutput ${BMI}
33 INTERFACE /reference fmt=${BMI})
34 set_target_properties(${target} PROPERTIES ADDITIONAL_CLEAN_FILES ${BMI})
35 set_source_files_properties(${BMI} PROPERTIES GENERATED ON)
39 set(FMT_USE_CMAKE_MODULES FALSE)
40 if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28 AND
41 CMAKE_GENERATOR STREQUAL "Ninja")
42 set(FMT_USE_CMAKE_MODULES TRUE)
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.
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})
57 set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
60 # Create a non-modular library.
61 target_sources(${name} PRIVATE ${AML_FALLBACK})
62 set_target_properties(${name} PROPERTIES CXX_SCAN_FOR_MODULES OFF)
66 # Modules require C++20.
67 target_compile_features(${name} PUBLIC cxx_std_20)
68 if (CMAKE_COMPILER_IS_GNUCXX)
69 target_compile_options(${name} PUBLIC -fmodules-ts)
72 target_compile_definitions(${name} PRIVATE FMT_MODULE)
74 if (FMT_USE_CMAKE_MODULES)
75 target_sources(${name} PUBLIC FILE_SET fmt TYPE CXX_MODULES
78 # `std` is affected by CMake options and may be higher than C++20.
79 get_target_property(std ${name} CXX_STANDARD)
81 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
83 foreach (src ${sources})
84 get_filename_component(pcm ${src} NAME_WE)
87 # Propagate -fmodule-file=*.pcm to targets that link with this library.
88 target_compile_options(
89 ${name} PUBLIC -fmodule-file=${CMAKE_CURRENT_BINARY_DIR}/${pcm})
91 # Use an absolute path to prevent target_link_libraries prepending -l
93 set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm})
96 COMMAND ${CMAKE_CXX_COMPILER}
97 -std=c++${std} -x c++-module --precompile -c
98 -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src}
99 "-I$<JOIN:$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>,;-I>"
100 # Required by the -I generator expression above.
105 # Add .pcm files as sources to make sure they are built before the library.
107 foreach (pcm ${pcms})
108 get_filename_component(pcm_we ${pcm} NAME_WE)
110 # Use an absolute path to prevent target_link_libraries prepending -l.
111 set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj})
114 COMMAND ${CMAKE_CXX_COMPILER} $<TARGET_PROPERTY:${name},COMPILE_OPTIONS>
119 target_sources(${name} PRIVATE ${sources})
123 include(CMakeParseArguments)
125 # Sets a cache variable with a docstring joined from multiple arguments:
126 # set(<variable> <value>... CACHE <type> <docstring>...)
127 # This allows splitting a long docstring for readability.
128 function(set_verbose)
129 # cmake_parse_arguments is broken in CMake 3.4 (cannot parse CACHE) so use
132 list(REMOVE_AT ARGN 0)
134 list(REMOVE_AT ARGN 0)
135 list(REMOVE_AT ARGN 0)
136 list(GET ARGN 0 type)
137 list(REMOVE_AT ARGN 0)
139 set(${var} ${val} CACHE ${type} ${doc})
142 # Set the default CMAKE_BUILD_TYPE to Release.
143 # This should be done before the project command since the latter can set
144 # CMAKE_BUILD_TYPE itself (it does so for nmake).
145 if (FMT_MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
146 set_verbose(CMAKE_BUILD_TYPE Release CACHE STRING
147 "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or "
148 "CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
152 include(GNUInstallDirs)
153 set_verbose(FMT_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING
154 "Installation directory for include files, a relative path that "
155 "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.")
157 option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF)
158 option(FMT_WERROR "Halt the compilation with an error on compiler warnings."
161 # Options that control generation of various targets.
162 option(FMT_DOC "Generate the doc target." ${FMT_MASTER_PROJECT})
163 option(FMT_INSTALL "Generate the install target." ON)
164 option(FMT_TEST "Generate the test target." ${FMT_MASTER_PROJECT})
165 option(FMT_FUZZ "Generate the fuzz target." OFF)
166 option(FMT_CUDA_TEST "Generate the cuda-test target." OFF)
167 option(FMT_OS "Include OS-specific APIs." ON)
168 option(FMT_MODULE "Build a module instead of a traditional library." OFF)
169 option(FMT_SYSTEM_HEADERS "Expose headers with marking them as system." OFF)
170 option(FMT_UNICODE "Enable Unicode support." ON)
172 if (FMT_TEST AND FMT_MODULE)
173 # The tests require {fmt} to be compiled as traditional library
174 message(STATUS "Testing is incompatible with build mode 'module'.")
176 set(FMT_SYSTEM_HEADERS_ATTRIBUTE "")
177 if (FMT_SYSTEM_HEADERS)
178 set(FMT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
180 if (CMAKE_SYSTEM_NAME STREQUAL "MSDOS")
182 message(STATUS "MSDOS is incompatible with gtest")
185 # Get version from base.h
186 file(READ include/fmt/base.h base_h)
187 if (NOT base_h MATCHES "FMT_VERSION ([0-9]+)([0-9][0-9])([0-9][0-9])")
188 message(FATAL_ERROR "Cannot get FMT_VERSION from base.h.")
190 # Use math to skip leading zeros if any.
191 math(EXPR CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
192 math(EXPR CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
193 math(EXPR CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
194 join(FMT_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.
195 ${CPACK_PACKAGE_VERSION_PATCH})
196 message(STATUS "{fmt} version: ${FMT_VERSION}")
198 message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
200 if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
201 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
204 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
205 "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake")
207 include(CheckCXXCompilerFlag)
210 if (FMT_MASTER_PROJECT AND NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
211 set_verbose(CMAKE_CXX_VISIBILITY_PRESET hidden CACHE STRING
212 "Preset for the export of private symbols")
213 set_property(CACHE CMAKE_CXX_VISIBILITY_PRESET PROPERTY STRINGS
217 if (FMT_MASTER_PROJECT AND NOT DEFINED CMAKE_VISIBILITY_INLINES_HIDDEN)
218 set_verbose(CMAKE_VISIBILITY_INLINES_HIDDEN ON CACHE BOOL
219 "Whether to add a compile flag to hide symbols of inline functions")
222 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
223 set(PEDANTIC_COMPILE_FLAGS -pedantic-errors -Wall -Wextra -pedantic
224 -Wold-style-cast -Wundef
225 -Wredundant-decls -Wwrite-strings -Wpointer-arith
226 -Wcast-qual -Wformat=2 -Wmissing-include-dirs
228 -Wctor-dtor-privacy -Wdisabled-optimization
229 -Winvalid-pch -Woverloaded-virtual
231 -Wno-ctor-dtor-privacy -Wno-format-nonliteral)
232 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
233 set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
234 -Wno-dangling-else -Wno-unused-local-typedefs)
236 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
237 set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wdouble-promotion
238 -Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast
239 -Wvector-operation-performance -Wsized-deallocation -Wshadow)
241 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
242 set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
243 -Wnull-dereference -Wduplicated-cond)
245 set(WERROR_FLAG -Werror)
248 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
249 set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
250 -Wdeprecated -Wweak-vtables -Wshadow
251 -Wno-gnu-zero-variadic-macro-arguments)
252 check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
253 if (HAS_NULLPTR_WARNING)
254 set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
255 -Wzero-as-null-pointer-constant)
257 set(WERROR_FLAG -Werror)
261 set(PEDANTIC_COMPILE_FLAGS /W3)
265 if (FMT_MASTER_PROJECT AND CMAKE_GENERATOR MATCHES "Visual Studio")
266 # If Microsoft SDK is installed create script run-msbuild.bat that
267 # calls SetEnv.cmd to set up build environment and runs msbuild.
268 # It is useful when building Visual Studio projects with the SDK
269 # toolchain rather than Visual Studio.
272 set(MSBUILD_SETUP "call \"${WINSDK_SETENV}\"")
274 # Set FrameworkPathOverride to get rid of MSB3644 warnings.
276 "C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\"
277 ".NETFramework\\v4.0")
278 file(WRITE run-msbuild.bat "
280 ${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
283 function(add_headers VAR)
284 set(headers ${${VAR}})
285 foreach (header ${ARGN})
286 set(headers ${headers} include/fmt/${header})
288 set(${VAR} ${headers} PARENT_SCOPE)
291 # Define the fmt library, its includes and the needed defines.
292 add_headers(FMT_HEADERS args.h base.h chrono.h color.h compile.h core.h format.h
293 format-inl.h os.h ostream.h printf.h ranges.h std.h
295 set(FMT_SOURCES src/format.cc)
297 add_module_library(fmt src/fmt.cc FALLBACK
298 ${FMT_SOURCES} ${FMT_HEADERS} README.md ChangeLog.md
300 add_library(fmt::fmt ALIAS fmt)
304 target_sources(fmt PRIVATE src/os.cc)
306 target_compile_definitions(fmt PRIVATE FMT_OS=0)
310 target_compile_options(fmt PRIVATE ${WERROR_FLAG})
313 target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
316 if (cxx_std_11 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
317 target_compile_features(fmt PUBLIC cxx_std_11)
319 message(WARNING "Feature cxx_std_11 is unknown for the CXX compiler")
322 target_include_directories(fmt ${FMT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
323 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
324 $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
326 set(FMT_DEBUG_POSTFIX d CACHE STRING "Debug library postfix.")
328 set_target_properties(fmt PROPERTIES
329 VERSION ${FMT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
330 PUBLIC_HEADER "${FMT_HEADERS}"
331 DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}"
333 # Workaround for Visual Studio 2017:
334 # Ensure the .pdb is created with the same name and in the same directory
335 # as the .lib. Newer VS versions already do this by default, but there is no
336 # harm in setting it for those too. Ignored by other generators.
337 COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
338 COMPILE_PDB_NAME "fmt"
339 COMPILE_PDB_NAME_DEBUG "fmt${FMT_DEBUG_POSTFIX}")
341 # Set FMT_LIB_NAME for pkg-config fmt.pc. We cannot use the OUTPUT_NAME target
342 # property because it's not set by default.
343 set(FMT_LIB_NAME fmt)
344 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
345 set(FMT_LIB_NAME ${FMT_LIB_NAME}${FMT_DEBUG_POSTFIX})
348 if (BUILD_SHARED_LIBS)
349 target_compile_definitions(fmt PRIVATE FMT_LIB_EXPORT INTERFACE FMT_SHARED)
351 if (FMT_SAFE_DURATION_CAST)
352 target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
355 add_library(fmt-header-only INTERFACE)
356 add_library(fmt::fmt-header-only ALIAS fmt-header-only)
359 # Unicode is always supported on compilers other than MSVC.
361 # Unicode support requires compiling with /utf-8.
362 target_compile_options(fmt PUBLIC $<$<COMPILE_LANGUAGE:CXX>:/utf-8>)
363 target_compile_options(fmt-header-only INTERFACE $<$<COMPILE_LANGUAGE:CXX>:/utf-8>)
365 target_compile_definitions(fmt PUBLIC FMT_UNICODE=0)
368 target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
369 target_compile_features(fmt-header-only INTERFACE cxx_std_11)
371 target_include_directories(fmt-header-only
372 ${FMT_SYSTEM_HEADERS_ATTRIBUTE} INTERFACE
373 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
374 $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
378 include(CMakePackageConfigHelpers)
379 set_verbose(FMT_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/fmt CACHE STRING
380 "Installation directory for cmake files, a relative path that "
381 "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute "
383 set(version_config ${PROJECT_BINARY_DIR}/fmt-config-version.cmake)
384 set(project_config ${PROJECT_BINARY_DIR}/fmt-config.cmake)
385 set(pkgconfig ${PROJECT_BINARY_DIR}/fmt.pc)
386 set(targets_export_name fmt-targets)
388 set_verbose(FMT_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
389 "Installation directory for libraries, a relative path that "
390 "will be joined to ${CMAKE_INSTALL_PREFIX} or an absolute path.")
392 set_verbose(FMT_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE STRING
393 "Installation directory for pkgconfig (.pc) files, a relative "
394 "path that will be joined with ${CMAKE_INSTALL_PREFIX} or an "
397 # Generate the version, config and target files into the build directory.
398 write_basic_package_version_file(
400 VERSION ${FMT_VERSION}
401 COMPATIBILITY AnyNewerVersion)
403 join_paths(libdir_for_pc_file "\${exec_prefix}" "${FMT_LIB_DIR}")
404 join_paths(includedir_for_pc_file "\${prefix}" "${FMT_INC_DIR}")
407 "${PROJECT_SOURCE_DIR}/support/cmake/fmt.pc.in"
410 configure_package_config_file(
411 ${PROJECT_SOURCE_DIR}/support/cmake/fmt-config.cmake.in
413 INSTALL_DESTINATION ${FMT_CMAKE_DIR})
415 set(INSTALL_TARGETS fmt fmt-header-only)
417 set(INSTALL_FILE_SET)
418 if (FMT_USE_CMAKE_MODULES)
419 set(INSTALL_FILE_SET FILE_SET fmt DESTINATION "${FMT_INC_DIR}/fmt")
422 # Install the library and headers.
423 install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
424 LIBRARY DESTINATION ${FMT_LIB_DIR}
425 ARCHIVE DESTINATION ${FMT_LIB_DIR}
426 PUBLIC_HEADER DESTINATION "${FMT_INC_DIR}/fmt"
427 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
430 # Use a namespace because CMake provides better diagnostics for namespaced
432 export(TARGETS ${INSTALL_TARGETS} NAMESPACE fmt::
433 FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
435 # Install version, config and target files.
437 FILES ${project_config} ${version_config}
438 DESTINATION ${FMT_CMAKE_DIR})
439 install(EXPORT ${targets_export_name} DESTINATION ${FMT_CMAKE_DIR}
442 install(FILES "${pkgconfig}" DESTINATION "${FMT_PKGCONFIG_DIR}")
445 function(add_doc_target)
446 find_program(DOXYGEN doxygen
447 PATHS "$ENV{ProgramFiles}/doxygen/bin"
448 "$ENV{ProgramFiles\(x86\)}/doxygen/bin")
450 message(STATUS "Target 'doc' disabled because doxygen not found")
454 find_program(MKDOCS mkdocs)
456 message(STATUS "Target 'doc' disabled because mkdocs not found")
461 foreach (source api.md index.md syntax.md get-started.md fmt.css fmt.js)
462 set(sources ${sources} doc/${source})
469 -E env PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/support/python
470 ${MKDOCS} build -f ${CMAKE_CURRENT_SOURCE_DIR}/support/mkdocs.yml
471 # MkDocs requires the site dir to be outside of the doc dir.
472 --site-dir ${CMAKE_CURRENT_BINARY_DIR}/doc-html
476 include(GNUInstallDirs)
477 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc-html/
478 DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/doc/fmt OPTIONAL)
487 add_subdirectory(test)
490 # Control fuzzing independent of the unit tests.
492 add_subdirectory(test/fuzzing)
494 # The FMT_FUZZ macro is used to prevent resource exhaustion in fuzzing
495 # mode and make fuzzing practically possible. It is similar to
496 # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION but uses a different name to
497 # avoid interfering with fuzzing of projects that use {fmt}.
498 # See also https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode.
499 target_compile_definitions(fmt PUBLIC FMT_FUZZ)
502 set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore)
503 if (FMT_MASTER_PROJECT AND EXISTS ${gitignore})
504 # Get the list of ignored files from .gitignore.
505 file (STRINGS ${gitignore} lines)
506 list(REMOVE_ITEM lines /doc/html)
507 foreach (line ${lines})
508 string(REPLACE "." "[.]" line "${line}")
509 string(REPLACE "*" ".*" line "${line}")
510 set(ignored_files ${ignored_files} "${line}$" "${line}/")
512 set(ignored_files ${ignored_files} /.git /build/doxyxml .vagrant)
514 set(CPACK_SOURCE_GENERATOR ZIP)
515 set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
516 set(CPACK_SOURCE_PACKAGE_FILE_NAME fmt-${FMT_VERSION})
517 set(CPACK_PACKAGE_NAME fmt)
518 set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md)