CMakeLists.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # Copyright(c) 2015 Ruslan Baratov.
  3. # Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. #
  5. cmake_minimum_required(VERSION 3.1)
  6. project(spdlog VERSION 1.2.0 LANGUAGES CXX)
  7. include(CTest)
  8. include(CMakeDependentOption)
  9. include(GNUInstallDirs)
  10. #---------------------------------------------------------------------------------------
  11. # set default build to release
  12. #---------------------------------------------------------------------------------------
  13. if(NOT CMAKE_BUILD_TYPE)
  14. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
  15. endif()
  16. message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
  17. #---------------------------------------------------------------------------------------
  18. # compiler config
  19. #---------------------------------------------------------------------------------------
  20. set(CMAKE_CXX_STANDARD 11)
  21. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  22. set(CMAKE_CXX_EXTENSIONS OFF)
  23. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  24. add_compile_options("-Wall")
  25. add_compile_options("-Wextra")
  26. add_compile_options("-Wconversion")
  27. add_compile_options("-pedantic")
  28. add_compile_options("-Wfatal-errors")
  29. endif()
  30. #---------------------------------------------------------------------------------------
  31. # address sanitizers check
  32. #---------------------------------------------------------------------------------------
  33. include(cmake/sanitizers.cmake)
  34. #---------------------------------------------------------------------------------------
  35. # spdlog target
  36. #---------------------------------------------------------------------------------------
  37. add_library(spdlog INTERFACE)
  38. add_library(spdlog::spdlog ALIAS spdlog)
  39. # Check if spdlog is being used directly or via add_subdirectory
  40. set(SPDLOG_MASTER_PROJECT OFF)
  41. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  42. set(SPDLOG_MASTER_PROJECT ON)
  43. endif()
  44. option(SPDLOG_BUILD_EXAMPLES "Build examples" ${SPDLOG_MASTER_PROJECT})
  45. option(SPDLOG_BUILD_BENCH "Build benchmarks" ${SPDLOG_MASTER_PROJECT})
  46. cmake_dependent_option(SPDLOG_BUILD_TESTING
  47. "Build spdlog tests" ${SPDLOG_MASTER_PROJECT}
  48. "BUILD_TESTING" OFF
  49. )
  50. target_include_directories(
  51. spdlog
  52. INTERFACE
  53. "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
  54. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
  55. )
  56. set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include")
  57. if(SPDLOG_BUILD_EXAMPLES)
  58. add_subdirectory(example)
  59. endif()
  60. if(SPDLOG_BUILD_TESTING)
  61. add_subdirectory(tests)
  62. endif()
  63. if(SPDLOG_BUILD_BENCH)
  64. add_subdirectory(bench)
  65. endif()
  66. #---------------------------------------------------------------------------------------
  67. # Install/export targets and files
  68. #---------------------------------------------------------------------------------------
  69. # set files and directories
  70. set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  71. set(include_install_dir "${CMAKE_INSTALL_INCLUDEDIR}")
  72. set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  73. set(version_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
  74. set(project_config "${PROJECT_NAME}Config.cmake")
  75. set(pkg_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc")
  76. set(targets_export_name "${PROJECT_NAME}Targets")
  77. set(namespace "${PROJECT_NAME}::")
  78. # generate package version file
  79. include(CMakePackageConfigHelpers)
  80. write_basic_package_version_file(
  81. "${version_config}" COMPATIBILITY SameMajorVersion
  82. )
  83. # configure pkg config file
  84. configure_file("cmake/spdlog.pc.in" "${pkg_config}" @ONLY)
  85. # install targets
  86. install(
  87. TARGETS spdlog
  88. EXPORT "${targets_export_name}"
  89. )
  90. # install headers
  91. install(
  92. DIRECTORY "${HEADER_BASE}/${PROJECT_NAME}"
  93. DESTINATION "${include_install_dir}"
  94. )
  95. # install project version file
  96. install(
  97. FILES "${version_config}"
  98. DESTINATION "${config_install_dir}"
  99. )
  100. # install pkg config file
  101. install(
  102. FILES "${pkg_config}"
  103. DESTINATION "${pkgconfig_install_dir}"
  104. )
  105. # install project config file
  106. install(
  107. EXPORT "${targets_export_name}"
  108. NAMESPACE "${namespace}"
  109. DESTINATION "${config_install_dir}"
  110. FILE ${project_config}
  111. )
  112. # export build directory config file
  113. export(
  114. EXPORT ${targets_export_name}
  115. NAMESPACE "${namespace}"
  116. FILE ${project_config}
  117. )
  118. # register project in CMake user registry
  119. export(PACKAGE ${PROJECT_NAME})
  120. file(GLOB_RECURSE spdlog_include_SRCS "${HEADER_BASE}/*.h")
  121. add_custom_target(spdlog_headers_for_ide SOURCES ${spdlog_include_SRCS})