CMakeLists.txt 4.38 KB
Newer Older
1 2 3 4 5 6
#
# Copyright(c) 2015 Ruslan Baratov.
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
#

cmake_minimum_required(VERSION 3.1)
gabime's avatar
gabime committed
7
project(spdlog VERSION 1.3.0 LANGUAGES CXX)
8 9 10 11
include(CTest)
include(CMakeDependentOption)
include(GNUInstallDirs)

12 13 14
#---------------------------------------------------------------------------------------
# set default build to release
#---------------------------------------------------------------------------------------
15
if(NOT CMAKE_BUILD_TYPE)
16 17 18
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()

19
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
20

21 22 23
#---------------------------------------------------------------------------------------
# compiler config
#---------------------------------------------------------------------------------------
24 25
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
26
set(CMAKE_CXX_EXTENSIONS OFF)
27 28

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
29 30
    add_compile_options("-Wall")
    add_compile_options("-Wextra")
31
    add_compile_options("-Wconversion")
32
    add_compile_options("-pedantic")
33
    add_compile_options("-Wfatal-errors")
34
    
35 36
endif()

37 38 39
#---------------------------------------------------------------------------------------
# address sanitizers check
#---------------------------------------------------------------------------------------
40 41
include(cmake/sanitizers.cmake)

42 43 44
#---------------------------------------------------------------------------------------
# spdlog target
#---------------------------------------------------------------------------------------
45
add_library(spdlog INTERFACE)
46
add_library(spdlog::spdlog ALIAS spdlog)
47

48 49 50 51 52 53 54 55
# Check if spdlog is being used directly or via add_subdirectory
set(SPDLOG_MASTER_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  set(SPDLOG_MASTER_PROJECT ON)
endif()

option(SPDLOG_BUILD_EXAMPLES "Build examples" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_BUILD_BENCH "Build benchmarks" ${SPDLOG_MASTER_PROJECT})
56

57
cmake_dependent_option(SPDLOG_BUILD_TESTING
58
    "Build spdlog tests" ${SPDLOG_MASTER_PROJECT}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    "BUILD_TESTING" OFF
)

target_include_directories(
    spdlog
    INTERFACE
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include")

if(SPDLOG_BUILD_EXAMPLES)
    add_subdirectory(example)
endif()

if(SPDLOG_BUILD_TESTING)
    add_subdirectory(tests)
endif()

79 80 81 82
if(SPDLOG_BUILD_BENCH)
    add_subdirectory(bench)
endif()

83 84 85
#---------------------------------------------------------------------------------------
# Install/export targets and files
#---------------------------------------------------------------------------------------
86
# set files and directories
87 88 89 90 91 92 93 94 95
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
set(include_install_dir "${CMAKE_INSTALL_INCLUDEDIR}")
set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
set(version_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(project_config "${PROJECT_NAME}Config.cmake")
set(pkg_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc")
set(targets_export_name "${PROJECT_NAME}Targets")
set(namespace "${PROJECT_NAME}::")

96
# generate package version file
97 98 99 100 101
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "${version_config}" COMPATIBILITY SameMajorVersion
)

102
# configure pkg config file
103 104
configure_file("cmake/spdlog.pc.in" "${pkg_config}" @ONLY)

105
# install targets
106 107 108 109 110
install(
    TARGETS spdlog
    EXPORT "${targets_export_name}"
)

111
# install headers
112 113 114 115 116
install(
    DIRECTORY "${HEADER_BASE}/${PROJECT_NAME}"
    DESTINATION "${include_install_dir}"
)

117
# install project version file
118 119 120 121 122
install(
    FILES "${version_config}"
    DESTINATION "${config_install_dir}"
)

123
# install pkg config file
124 125 126 127 128
install(
    FILES "${pkg_config}"
    DESTINATION "${pkgconfig_install_dir}"
)

129
# install project config file
130 131 132 133 134 135 136
install(
    EXPORT "${targets_export_name}"
    NAMESPACE "${namespace}"
    DESTINATION "${config_install_dir}"
    FILE ${project_config}
)

137
# export build directory config file
138 139 140 141 142 143
export(
    EXPORT ${targets_export_name}
    NAMESPACE "${namespace}"
    FILE ${project_config}
)

144
# register project in CMake user registry
145 146 147 148
export(PACKAGE ${PROJECT_NAME})

file(GLOB_RECURSE spdlog_include_SRCS "${HEADER_BASE}/*.h")
add_custom_target(spdlog_headers_for_ide SOURCES ${spdlog_include_SRCS})