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