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