CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)
  2. cmake_minimum_required(VERSION 3.10)
  3. # ---------------------------------------------------------------------------------------
  4. # Start spdlog project
  5. # ---------------------------------------------------------------------------------------
  6. include(cmake/utils.cmake)
  7. include(cmake/ide.cmake)
  8. spdlog_extract_version()
  9. project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
  10. message(STATUS "Build spdlog: ${SPDLOG_VERSION}")
  11. include(GNUInstallDirs)
  12. # ---------------------------------------------------------------------------------------
  13. # Set default build to release
  14. # ---------------------------------------------------------------------------------------
  15. if(NOT CMAKE_BUILD_TYPE)
  16. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
  17. endif()
  18. # ---------------------------------------------------------------------------------------
  19. # Compiler config
  20. # ---------------------------------------------------------------------------------------
  21. if(NOT CMAKE_CXX_STANDARD)
  22. set(CMAKE_CXX_STANDARD 11)
  23. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  24. endif()
  25. set(CMAKE_CXX_EXTENSIONS OFF)
  26. if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS")
  27. set(CMAKE_CXX_EXTENSIONS ON)
  28. endif()
  29. # ---------------------------------------------------------------------------------------
  30. # Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
  31. # ---------------------------------------------------------------------------------------
  32. # Check if spdlog is being used directly or via add_subdirectory, but allow overriding
  33. if(NOT DEFINED SPDLOG_MASTER_PROJECT)
  34. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  35. set(SPDLOG_MASTER_PROJECT ON)
  36. else()
  37. set(SPDLOG_MASTER_PROJECT OFF)
  38. endif()
  39. endif()
  40. option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
  41. # build shared option
  42. option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
  43. # precompiled headers option
  44. option(SPDLOG_ENABLE_PCH "Build static or shared library using precompiled header to speed up compilation time" OFF)
  45. # example options
  46. option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
  47. option(SPDLOG_BUILD_EXAMPLE_HO "Build header only example" OFF)
  48. # testing options
  49. option(SPDLOG_BUILD_TESTS "Build tests" OFF)
  50. option(SPDLOG_BUILD_TESTS_HO "Build tests using the header only version" OFF)
  51. # bench options
  52. option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
  53. # sanitizer options
  54. option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
  55. # warning options
  56. option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
  57. # install options
  58. option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
  59. option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
  60. option(SPDLOG_FMT_EXTERNAL_HO "Use external fmt header-only library instead of bundled" OFF)
  61. option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
  62. if(SPDLOG_FMT_EXTERNAL AND SPDLOG_FMT_EXTERNAL_HO)
  63. message(FATAL_ERROR "SPDLOG_FMT_EXTERNAL and SPDLOG_FMT_EXTERNAL_HO are mutually exclusive")
  64. endif()
  65. # misc tweakme options
  66. if(WIN32)
  67. option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
  68. option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
  69. endif()
  70. if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  71. option(SPDLOG_CLOCK_COARSE
  72. "Use the much faster (but much less accurate) CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
  73. endif()
  74. option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
  75. option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF)
  76. option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" OFF)
  77. option(
  78. SPDLOG_NO_ATOMIC_LEVELS
  79. "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently"
  80. OFF)
  81. option(SPDLOG_DISABLE_DEFAULT_LOGGER "Disable default logger creation" OFF)
  82. # clang-tidy
  83. if(${CMAKE_VERSION} VERSION_GREATER "3.5")
  84. option(SPDLOG_TIDY "run clang-tidy" OFF)
  85. endif()
  86. if(SPDLOG_TIDY)
  87. set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
  88. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  89. message(STATUS "Enabled clang-tidy")
  90. endif()
  91. find_package(Threads REQUIRED)
  92. message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
  93. # ---------------------------------------------------------------------------------------
  94. # Static/Shared library (shared not supported in windows yet)
  95. # ---------------------------------------------------------------------------------------
  96. set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp)
  97. if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
  98. list(APPEND SPDLOG_SRCS src/fmt.cpp)
  99. endif()
  100. if(SPDLOG_BUILD_SHARED)
  101. if(WIN32)
  102. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
  103. list(APPEND SPDLOG_SRCS ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
  104. endif()
  105. add_library(spdlog SHARED ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
  106. target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB)
  107. if(MSVC)
  108. target_compile_options(spdlog PUBLIC /wd4251 /wd4275)
  109. endif()
  110. if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
  111. target_compile_definitions(spdlog PRIVATE FMT_EXPORT PUBLIC FMT_SHARED)
  112. endif()
  113. else()
  114. add_library(spdlog STATIC ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
  115. endif()
  116. add_library(spdlog::spdlog ALIAS spdlog)
  117. target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
  118. target_include_directories(spdlog PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
  119. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
  120. target_link_libraries(spdlog PUBLIC Threads::Threads)
  121. spdlog_enable_warnings(spdlog)
  122. set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR})
  123. set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)
  124. if(COMMAND target_precompile_headers AND SPDLOG_ENABLE_PCH)
  125. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/pch.h.in ${PROJECT_BINARY_DIR}/spdlog_pch.h @ONLY)
  126. target_precompile_headers(spdlog PRIVATE ${PROJECT_BINARY_DIR}/spdlog_pch.h)
  127. endif()
  128. # ---------------------------------------------------------------------------------------
  129. # Header only version
  130. # ---------------------------------------------------------------------------------------
  131. add_library(spdlog_header_only INTERFACE)
  132. add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
  133. target_include_directories(spdlog_header_only INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
  134. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
  135. target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)
  136. # ---------------------------------------------------------------------------------------
  137. # Use fmt package if using external fmt
  138. # ---------------------------------------------------------------------------------------
  139. if(SPDLOG_FMT_EXTERNAL OR SPDLOG_FMT_EXTERNAL_HO)
  140. if(NOT TARGET fmt::fmt)
  141. find_package(fmt 5.3.0 CONFIG REQUIRED)
  142. endif()
  143. target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL)
  144. target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL)
  145. # use external fmt-header-nly
  146. if(SPDLOG_FMT_EXTERNAL_HO)
  147. target_link_libraries(spdlog PUBLIC fmt::fmt-header-only)
  148. target_link_libraries(spdlog_header_only INTERFACE fmt::fmt-header-only)
  149. else() # use external compile fmt
  150. target_link_libraries(spdlog PUBLIC fmt::fmt)
  151. target_link_libraries(spdlog_header_only INTERFACE fmt::fmt)
  152. endif()
  153. set(PKG_CONFIG_REQUIRES fmt) # add dependency to pkg-config
  154. endif()
  155. # ---------------------------------------------------------------------------------------
  156. # Misc definitions according to tweak options
  157. # ---------------------------------------------------------------------------------------
  158. set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ${SPDLOG_WCHAR_SUPPORT})
  159. foreach(
  160. SPDLOG_OPTION
  161. SPDLOG_WCHAR_TO_UTF8_SUPPORT
  162. SPDLOG_WCHAR_FILENAMES
  163. SPDLOG_NO_EXCEPTIONS
  164. SPDLOG_CLOCK_COARSE
  165. SPDLOG_PREVENT_CHILD_FD
  166. SPDLOG_NO_THREAD_ID
  167. SPDLOG_NO_TLS
  168. SPDLOG_NO_ATOMIC_LEVELS
  169. SPDLOG_DISABLE_DEFAULT_LOGGER)
  170. if(${SPDLOG_OPTION})
  171. target_compile_definitions(spdlog PUBLIC ${SPDLOG_OPTION})
  172. target_compile_definitions(spdlog_header_only INTERFACE ${SPDLOG_OPTION})
  173. endif()
  174. endforeach()
  175. if(SPDLOG_NO_EXCEPTIONS AND NOT MSVC)
  176. target_compile_options(spdlog PRIVATE -fno-exceptions)
  177. endif()
  178. # ---------------------------------------------------------------------------------------
  179. # Build binaries
  180. # ---------------------------------------------------------------------------------------
  181. if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO OR SPDLOG_BUILD_ALL)
  182. message(STATUS "Generating example(s)")
  183. add_subdirectory(example)
  184. spdlog_enable_warnings(example)
  185. if(SPDLOG_BUILD_EXAMPLE_HO)
  186. spdlog_enable_warnings(example_header_only)
  187. endif()
  188. endif()
  189. if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_TESTS_HO OR SPDLOG_BUILD_ALL)
  190. message(STATUS "Generating tests")
  191. enable_testing()
  192. add_subdirectory(tests)
  193. endif()
  194. if(SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
  195. message(STATUS "Generating benchmarks")
  196. add_subdirectory(bench)
  197. endif()
  198. # ---------------------------------------------------------------------------------------
  199. # Install
  200. # ---------------------------------------------------------------------------------------
  201. if(SPDLOG_INSTALL)
  202. message(STATUS "Generating install")
  203. set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
  204. set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake")
  205. set(config_targets_file "spdlogConfigTargets.cmake")
  206. set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake")
  207. set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/spdlog")
  208. set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  209. set(pkg_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc")
  210. # ---------------------------------------------------------------------------------------
  211. # Include files
  212. # ---------------------------------------------------------------------------------------
  213. install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" PATTERN "fmt/bundled" EXCLUDE)
  214. install(
  215. TARGETS spdlog spdlog_header_only
  216. EXPORT spdlog
  217. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  218. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  219. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  220. if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
  221. install(DIRECTORY include/${PROJECT_NAME}/fmt/bundled/
  222. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/fmt/bundled/")
  223. endif()
  224. # ---------------------------------------------------------------------------------------
  225. # Install pkg-config file
  226. # ---------------------------------------------------------------------------------------
  227. get_target_property(PKG_CONFIG_DEFINES spdlog INTERFACE_COMPILE_DEFINITIONS)
  228. string(REPLACE ";" " -D" PKG_CONFIG_DEFINES "${PKG_CONFIG_DEFINES}")
  229. string(CONCAT PKG_CONFIG_DEFINES "-D" "${PKG_CONFIG_DEFINES}")
  230. configure_file("cmake/${PROJECT_NAME}.pc.in" "${pkg_config}" @ONLY)
  231. install(FILES "${pkg_config}" DESTINATION "${pkgconfig_install_dir}")
  232. # ---------------------------------------------------------------------------------------
  233. # Install CMake config files
  234. # ---------------------------------------------------------------------------------------
  235. install(EXPORT spdlog DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
  236. include(CMakePackageConfigHelpers)
  237. configure_file("${project_config_in}" "${project_config_out}" @ONLY)
  238. write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
  239. install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
  240. # ---------------------------------------------------------------------------------------
  241. # Support creation of installable packages
  242. # ---------------------------------------------------------------------------------------
  243. include(cmake/spdlogCPack.cmake)
  244. endif()