xref: /aosp_15_r20/external/fmtlib/CMakeLists.txt (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1cmake_minimum_required(VERSION 3.8...3.28)
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.
10if (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}")
15  endif ()
16endif ()
17
18# Joins arguments and places the results in ${result_var}.
19function(join result_var)
20  set(result "")
21  foreach (arg ${ARGN})
22    set(result "${result}${arg}")
23  endforeach ()
24  set(${result_var} "${result}" PARENT_SCOPE)
25endfunction()
26
27# DEPRECATED! Should be merged into add_module_library.
28function(enable_module target)
29  if (MSVC)
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)
36  endif ()
37endfunction()
38
39set(FMT_USE_CMAKE_MODULES FALSE)
40if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28 AND
41    CMAKE_GENERATOR STREQUAL "Ninja")
42  set(FMT_USE_CMAKE_MODULES TRUE)
43endif ()
44
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])
52function(add_module_library name)
53  cmake_parse_arguments(AML "" "IF" "FALLBACK" ${ARGN})
54  set(sources ${AML_UNPARSED_ARGUMENTS})
55
56  add_library(${name})
57  set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
58
59  if (NOT ${${AML_IF}})
60    # Create a non-modular library.
61    target_sources(${name} PRIVATE ${AML_FALLBACK})
62    set_target_properties(${name} PROPERTIES CXX_SCAN_FOR_MODULES OFF)
63    return()
64  endif ()
65
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)
70  endif ()
71
72  target_compile_definitions(${name} PRIVATE FMT_MODULE)
73
74  if (FMT_USE_CMAKE_MODULES)
75    target_sources(${name} PUBLIC FILE_SET fmt TYPE CXX_MODULES
76                   FILES ${sources})
77  else()
78    # `std` is affected by CMake options and may be higher than C++20.
79    get_target_property(std ${name} CXX_STANDARD)
80
81    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
82      set(pcms)
83      foreach (src ${sources})
84        get_filename_component(pcm ${src} NAME_WE)
85        set(pcm ${pcm}.pcm)
86
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})
90
91        # Use an absolute path to prevent target_link_libraries prepending -l
92        # to it.
93        set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm})
94        add_custom_command(
95          OUTPUT ${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.
101          COMMAND_EXPAND_LISTS
102          DEPENDS ${src})
103      endforeach ()
104
105      # Add .pcm files as sources to make sure they are built before the library.
106      set(sources)
107      foreach (pcm ${pcms})
108        get_filename_component(pcm_we ${pcm} NAME_WE)
109        set(obj ${pcm_we}.o)
110        # Use an absolute path to prevent target_link_libraries prepending -l.
111        set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj})
112        add_custom_command(
113          OUTPUT ${obj}
114          COMMAND ${CMAKE_CXX_COMPILER} $<TARGET_PROPERTY:${name},COMPILE_OPTIONS>
115                  -c -o ${obj} ${pcm}
116          DEPENDS ${pcm})
117      endforeach ()
118    endif ()
119    target_sources(${name} PRIVATE ${sources})
120  endif()
121endfunction()
122
123include(CMakeParseArguments)
124
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.
128function(set_verbose)
129  # cmake_parse_arguments is broken in CMake 3.4 (cannot parse CACHE) so use
130  # list instead.
131  list(GET ARGN 0 var)
132  list(REMOVE_AT ARGN 0)
133  list(GET ARGN 0 val)
134  list(REMOVE_AT ARGN 0)
135  list(REMOVE_AT ARGN 0)
136  list(GET ARGN 0 type)
137  list(REMOVE_AT ARGN 0)
138  join(doc ${ARGN})
139  set(${var} ${val} CACHE ${type} ${doc})
140endfunction()
141
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).
145if (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.")
149endif ()
150
151project(FMT CXX)
152include(GNUInstallDirs)
153set_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.")
156
157option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF)
158option(FMT_WERROR "Halt the compilation with an error on compiler warnings."
159       OFF)
160
161# Options that control generation of various targets.
162option(FMT_DOC "Generate the doc target." ${FMT_MASTER_PROJECT})
163option(FMT_INSTALL "Generate the install target." ON)
164option(FMT_TEST "Generate the test target." ${FMT_MASTER_PROJECT})
165option(FMT_FUZZ "Generate the fuzz target." OFF)
166option(FMT_CUDA_TEST "Generate the cuda-test target." OFF)
167option(FMT_OS "Include OS-specific APIs." ON)
168option(FMT_MODULE "Build a module instead of a traditional library." OFF)
169option(FMT_SYSTEM_HEADERS "Expose headers with marking them as system." OFF)
170option(FMT_UNICODE "Enable Unicode support." ON)
171
172if (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'.")
175endif ()
176set(FMT_SYSTEM_HEADERS_ATTRIBUTE "")
177if (FMT_SYSTEM_HEADERS)
178  set(FMT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
179endif ()
180if (CMAKE_SYSTEM_NAME STREQUAL "MSDOS")
181  set(FMT_TEST OFF)
182  message(STATUS "MSDOS is incompatible with gtest")
183endif ()
184
185# Get version from base.h
186file(READ include/fmt/base.h base_h)
187if (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.")
189endif ()
190# Use math to skip leading zeros if any.
191math(EXPR CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
192math(EXPR CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
193math(EXPR CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
194join(FMT_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.
195                 ${CPACK_PACKAGE_VERSION_PATCH})
196message(STATUS "{fmt} version: ${FMT_VERSION}")
197
198message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
199
200if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
201  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
202endif ()
203
204set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
205  "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake")
206
207include(CheckCXXCompilerFlag)
208include(JoinPaths)
209
210if (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
214               hidden default)
215endif ()
216
217if (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")
220endif ()
221
222if (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
227      -Wcast-align
228      -Wctor-dtor-privacy -Wdisabled-optimization
229      -Winvalid-pch -Woverloaded-virtual
230      -Wconversion -Wundef
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)
235  endif ()
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)
240  endif ()
241  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
242      set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
243          -Wduplicated-cond)
244      # Workaround for GCC regression
245      # [12/13/14/15 regression] New (since gcc 12) false positive null-dereference in vector.resize
246      # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108860
247      if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0)
248        set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wnull-dereference)
249      endif ()
250  endif ()
251  set(WERROR_FLAG -Werror)
252endif ()
253
254if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
255  set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
256      -Wdeprecated -Wweak-vtables -Wshadow
257      -Wno-gnu-zero-variadic-macro-arguments)
258  check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
259  if (HAS_NULLPTR_WARNING)
260    set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
261        -Wzero-as-null-pointer-constant)
262  endif ()
263  set(WERROR_FLAG -Werror)
264endif ()
265
266if (MSVC)
267  set(PEDANTIC_COMPILE_FLAGS /W3)
268  set(WERROR_FLAG /WX)
269endif ()
270
271if (FMT_MASTER_PROJECT AND CMAKE_GENERATOR MATCHES "Visual Studio")
272  # If Microsoft SDK is installed create script run-msbuild.bat that
273  # calls SetEnv.cmd to set up build environment and runs msbuild.
274  # It is useful when building Visual Studio projects with the SDK
275  # toolchain rather than Visual Studio.
276  include(FindSetEnv)
277  if (WINSDK_SETENV)
278    set(MSBUILD_SETUP "call \"${WINSDK_SETENV}\"")
279  endif ()
280  # Set FrameworkPathOverride to get rid of MSB3644 warnings.
281  join(netfxpath
282       "C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\"
283       ".NETFramework\\v4.0")
284  file(WRITE run-msbuild.bat "
285    ${MSBUILD_SETUP}
286    ${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
287endif ()
288
289function(add_headers VAR)
290  set(headers ${${VAR}})
291  foreach (header ${ARGN})
292    set(headers ${headers} include/fmt/${header})
293  endforeach()
294  set(${VAR} ${headers} PARENT_SCOPE)
295endfunction()
296
297# Define the fmt library, its includes and the needed defines.
298add_headers(FMT_HEADERS args.h base.h chrono.h color.h compile.h core.h format.h
299                        format-inl.h os.h ostream.h printf.h ranges.h std.h
300                        xchar.h)
301set(FMT_SOURCES src/format.cc)
302
303add_module_library(fmt src/fmt.cc FALLBACK
304                   ${FMT_SOURCES} ${FMT_HEADERS} README.md ChangeLog.md
305                   IF FMT_MODULE)
306add_library(fmt::fmt ALIAS fmt)
307if (FMT_MODULE)
308  enable_module(fmt)
309elseif (FMT_OS)
310  target_sources(fmt PRIVATE src/os.cc)
311else()
312  target_compile_definitions(fmt PRIVATE FMT_OS=0)
313endif ()
314
315if (FMT_WERROR)
316  target_compile_options(fmt PRIVATE ${WERROR_FLAG})
317endif ()
318if (FMT_PEDANTIC)
319  target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
320endif ()
321
322if (cxx_std_11 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
323  target_compile_features(fmt PUBLIC cxx_std_11)
324else ()
325  message(WARNING "Feature cxx_std_11 is unknown for the CXX compiler")
326endif ()
327
328target_include_directories(fmt ${FMT_SYSTEM_HEADERS_ATTRIBUTE} BEFORE PUBLIC
329  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
330  $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
331
332set(FMT_DEBUG_POSTFIX d CACHE STRING "Debug library postfix.")
333
334set_target_properties(fmt PROPERTIES
335  VERSION ${FMT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
336  PUBLIC_HEADER "${FMT_HEADERS}"
337  DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}"
338
339  # Workaround for Visual Studio 2017:
340  # Ensure the .pdb is created with the same name and in the same directory
341  # as the .lib. Newer VS versions already do this by default, but there is no
342  # harm in setting it for those too. Ignored by other generators.
343  COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
344  COMPILE_PDB_NAME "fmt"
345  COMPILE_PDB_NAME_DEBUG "fmt${FMT_DEBUG_POSTFIX}")
346
347# Set FMT_LIB_NAME for pkg-config fmt.pc. We cannot use the OUTPUT_NAME target
348# property because it's not set by default.
349set(FMT_LIB_NAME fmt)
350if (CMAKE_BUILD_TYPE STREQUAL "Debug")
351  set(FMT_LIB_NAME ${FMT_LIB_NAME}${FMT_DEBUG_POSTFIX})
352endif ()
353
354if (BUILD_SHARED_LIBS)
355  target_compile_definitions(fmt PRIVATE FMT_LIB_EXPORT INTERFACE FMT_SHARED)
356endif ()
357if (FMT_SAFE_DURATION_CAST)
358  target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
359endif ()
360
361add_library(fmt-header-only INTERFACE)
362add_library(fmt::fmt-header-only ALIAS fmt-header-only)
363
364if (NOT MSVC)
365  # Unicode is always supported on compilers other than MSVC.
366elseif (FMT_UNICODE)
367  # Unicode support requires compiling with /utf-8.
368  target_compile_options(fmt PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
369  target_compile_options(fmt-header-only INTERFACE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
370else ()
371  target_compile_definitions(fmt PUBLIC FMT_UNICODE=0)
372endif ()
373
374target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
375target_compile_features(fmt-header-only INTERFACE cxx_std_11)
376
377target_include_directories(fmt-header-only
378  ${FMT_SYSTEM_HEADERS_ATTRIBUTE} BEFORE INTERFACE
379  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
380  $<INSTALL_INTERFACE:${FMT_INC_DIR}>)
381
382# Install targets.
383if (FMT_INSTALL)
384  include(CMakePackageConfigHelpers)
385  set_verbose(FMT_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/fmt CACHE STRING
386              "Installation directory for cmake files, a relative path that "
387              "will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute "
388              "path.")
389  set(version_config ${PROJECT_BINARY_DIR}/fmt-config-version.cmake)
390  set(project_config ${PROJECT_BINARY_DIR}/fmt-config.cmake)
391  set(pkgconfig ${PROJECT_BINARY_DIR}/fmt.pc)
392  set(targets_export_name fmt-targets)
393
394  set_verbose(FMT_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
395              "Installation directory for libraries, a relative path that "
396              "will be joined to ${CMAKE_INSTALL_PREFIX} or an absolute path.")
397
398  set_verbose(FMT_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE STRING
399              "Installation directory for pkgconfig (.pc) files, a relative "
400              "path that will be joined with ${CMAKE_INSTALL_PREFIX} or an "
401              "absolute path.")
402
403  # Generate the version, config and target files into the build directory.
404  write_basic_package_version_file(
405    ${version_config}
406    VERSION ${FMT_VERSION}
407    COMPATIBILITY AnyNewerVersion)
408
409  join_paths(libdir_for_pc_file "\${exec_prefix}" "${FMT_LIB_DIR}")
410  join_paths(includedir_for_pc_file "\${prefix}" "${FMT_INC_DIR}")
411
412  configure_file(
413    "${PROJECT_SOURCE_DIR}/support/cmake/fmt.pc.in"
414    "${pkgconfig}"
415    @ONLY)
416  configure_package_config_file(
417    ${PROJECT_SOURCE_DIR}/support/cmake/fmt-config.cmake.in
418    ${project_config}
419    INSTALL_DESTINATION ${FMT_CMAKE_DIR})
420
421  set(INSTALL_TARGETS fmt fmt-header-only)
422
423  set(INSTALL_FILE_SET)
424  if (FMT_USE_CMAKE_MODULES)
425    set(INSTALL_FILE_SET FILE_SET fmt DESTINATION "${FMT_INC_DIR}/fmt")
426  endif()
427
428  # Install the library and headers.
429  install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
430          LIBRARY DESTINATION ${FMT_LIB_DIR}
431          ARCHIVE DESTINATION ${FMT_LIB_DIR}
432          PUBLIC_HEADER DESTINATION "${FMT_INC_DIR}/fmt"
433          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
434          ${INSTALL_FILE_SET})
435
436  # Use a namespace because CMake provides better diagnostics for namespaced
437  # imported targets.
438  export(TARGETS ${INSTALL_TARGETS} NAMESPACE fmt::
439         FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
440
441  # Install version, config and target files.
442  install(
443    FILES ${project_config} ${version_config}
444    DESTINATION ${FMT_CMAKE_DIR})
445  install(EXPORT ${targets_export_name} DESTINATION ${FMT_CMAKE_DIR}
446          NAMESPACE fmt::)
447
448  install(FILES "${pkgconfig}" DESTINATION "${FMT_PKGCONFIG_DIR}")
449endif ()
450
451function(add_doc_target)
452  find_program(DOXYGEN doxygen
453    PATHS "$ENV{ProgramFiles}/doxygen/bin"
454          "$ENV{ProgramFiles\(x86\)}/doxygen/bin")
455  if (NOT DOXYGEN)
456    message(STATUS "Target 'doc' disabled because doxygen not found")
457    return ()
458  endif ()
459
460  find_program(MKDOCS mkdocs)
461  if (NOT MKDOCS)
462    message(STATUS "Target 'doc' disabled because mkdocs not found")
463    return ()
464  endif ()
465
466  set(sources )
467  foreach (source api.md index.md syntax.md get-started.md fmt.css fmt.js)
468    set(sources ${sources} doc/${source})
469  endforeach()
470
471  add_custom_target(
472    doc
473    COMMAND
474      ${CMAKE_COMMAND}
475        -E env PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/support/python
476        ${MKDOCS} build -f ${CMAKE_CURRENT_SOURCE_DIR}/support/mkdocs.yml
477        # MkDocs requires the site dir to be outside of the doc dir.
478                        --site-dir ${CMAKE_CURRENT_BINARY_DIR}/doc-html
479                        --no-directory-urls
480    SOURCES ${sources})
481
482  include(GNUInstallDirs)
483  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc-html/
484          DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/doc/fmt OPTIONAL)
485endfunction()
486
487if (FMT_DOC)
488  add_doc_target()
489endif ()
490
491if (FMT_TEST)
492  enable_testing()
493  add_subdirectory(test)
494endif ()
495
496# Control fuzzing independent of the unit tests.
497if (FMT_FUZZ)
498  add_subdirectory(test/fuzzing)
499
500  # The FMT_FUZZ macro is used to prevent resource exhaustion in fuzzing
501  # mode and make fuzzing practically possible. It is similar to
502  # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION but uses a different name to
503  # avoid interfering with fuzzing of projects that use {fmt}.
504  # See also https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode.
505  target_compile_definitions(fmt PUBLIC FMT_FUZZ)
506endif ()
507
508set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore)
509if (FMT_MASTER_PROJECT AND EXISTS ${gitignore})
510  # Get the list of ignored files from .gitignore.
511  file (STRINGS ${gitignore} lines)
512  list(REMOVE_ITEM lines /doc/html)
513  foreach (line ${lines})
514    string(REPLACE "." "[.]" line "${line}")
515    string(REPLACE "*" ".*" line "${line}")
516    set(ignored_files ${ignored_files} "${line}$" "${line}/")
517  endforeach ()
518  set(ignored_files ${ignored_files} /.git /build/doxyxml .vagrant)
519
520  set(CPACK_SOURCE_GENERATOR ZIP)
521  set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
522  set(CPACK_SOURCE_PACKAGE_FILE_NAME fmt-${FMT_VERSION})
523  set(CPACK_PACKAGE_NAME fmt)
524  set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md)
525  include(CPack)
526endif ()
527