CMake improvements

This commit is contained in:
gabime 2019-05-29 00:04:36 +03:00
parent 5709cb10d1
commit 30bd80bd85
4 changed files with 44 additions and 24 deletions

View File

@ -3,18 +3,17 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(spdlog VERSION 1.3.1 LANGUAGES CXX) project(spdlog VERSION 1.3.1 LANGUAGES CXX)
include(CMakeDependentOption)
include(GNUInstallDirs) include(GNUInstallDirs)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# set default build to release # Set default build to release
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif() endif()
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# compiler config # Compiler config
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@ -35,6 +34,7 @@ endif ()
option(SPDLOG_BUILD_EXAMPLES "Build examples" ON) option(SPDLOG_BUILD_EXAMPLES "Build examples" ON)
option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF) option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
option(SPDLOG_BUILD_TESTS "Build tests" OFF) option(SPDLOG_BUILD_TESTS "Build tests" OFF)
option(SPDLOG_BUILD_HO_TESTS "Build tests using the header only version" OFF)
option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
@ -42,27 +42,46 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
#---------------------------------------------------------------------------------------
# IDE support for headers
#---------------------------------------------------------------------------------------
set(SPDLOG_HEADERS_DIR "${CMAKE_CURRENT_LIST_DIR}/include")
file(GLOB SPDLOG_TOP_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/*.h")
file(GLOB SPDLOG_DETAILS_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/details/*.h")
file(GLOB SPDLOG_SINKS_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/sinks/*.h")
file(GLOB SPDLOG_FMT_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/*.h")
file(GLOB SPDLOG_FMT_BUNDELED_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/bundled/*.h")
set(SPDLOG_ALL_HEADERS ${SPDLOG_TOP_HEADERS} ${SPDLOG_DETAILS_HEADERS} ${SPDLOG_SINKS_HEADERS} ${SPDLOG_FMT_HEADERS} ${SPDLOG_FMT_BUNDELED_HEADERS})
source_group("Header Files\\spdlog" FILES ${SPDLOG_TOP_HEADERS})
source_group("Header Files\\spdlog\\details" FILES ${SPDLOG_DETAILS_HEADERS})
source_group("Header Files\\spdlog\\sinks" FILES ${SPDLOG_SINKS_HEADERS})
source_group("Header Files\\spdlog\\fmt" FILES ${SPDLOG_FMT_HEADERS})
source_group("Header Files\\spdlog\\fmt\\bundled\\" FILES ${SPDLOG_FMT_BUNDELED_HEADERS})
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# Static library version # Static library version
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
add_library(spdlog STATIC src/spdlog.cpp) add_library(spdlog STATIC src/spdlog.cpp ${SPDLOG_ALL_HEADERS})
add_library(spdlog::spdlog ALIAS spdlog)
target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB) target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
target_include_directories(spdlog PUBLIC target_include_directories(spdlog PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog PUBLIC Threads::Threads) target_link_libraries(spdlog PUBLIC Threads::Threads)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# Header only version # Header only version
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
add_library(spdlog_header_only INTERFACE) add_library(spdlog_header_only INTERFACE)
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
target_include_directories(spdlog_header_only INTERFACE target_include_directories(spdlog_header_only INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog_header_only INTERFACE Threads::Threads) target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# Turn on compiler warnings and sanitizers if we build our own project # Turn on compiler warnings and sanitizers if we build our own project
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
@ -146,5 +165,4 @@ if (SPDLOG_INSTALL)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
include(cmake/SpdlogCPack.cmake) include(cmake/SpdlogCPack.cmake)
endif () endif ()

View File

@ -2,7 +2,7 @@
# Distributed under the MIT License (http://opensource.org/licenses/MIT) # Distributed under the MIT License (http://opensource.org/licenses/MIT)
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(SpdlogBench CXX) project(spdlog_bench CXX)
if(NOT TARGET spdlog) if(NOT TARGET spdlog)
# Stand-alone build # Stand-alone build
@ -21,7 +21,6 @@ target_link_libraries(async_bench PRIVATE spdlog::spdlog)
add_executable(latency latency.cpp) add_executable(latency latency.cpp)
target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog) target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog)
add_executable(formatter-bench formatter-bench.cpp) add_executable(formatter-bench formatter-bench.cpp)
target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog) target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog)

View File

@ -2,15 +2,9 @@
# Distributed under the MIT License (http://opensource.org/licenses/MIT) # Distributed under the MIT License (http://opensource.org/licenses/MIT)
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(SpdlogExamples CXX) project(spdlog_examples CXX)
if(TARGET spdlog) if(NOT TARGET spdlog)
# If we're running this example as part of the primary spdlog applciation
# then add an alias. This allows us to use the same "spdlog::spdlog"
# below that a user would use (with the namespace)
add_library(spdlog::spdlog ALIAS spdlog)
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
else()
# Stand-alone build # Stand-alone build
find_package(spdlog REQUIRED) find_package(spdlog REQUIRED)
endif() endif()

View File

@ -15,10 +15,19 @@ set(SPDLOG_UTESTS_SOURCES
test_sink.h test_sink.h
test_fmt_helper.cpp) test_fmt_helper.cpp)
add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES})
target_link_libraries(spdlog-utests spdlog)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")
enable_testing() enable_testing()
add_test(NAME spdlog-utests COMMAND spdlog-utests)
# The compiled library tests
if(SPDLOG_BUILD_TESTS)
add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES})
target_link_libraries(spdlog-utests spdlog)
add_test(NAME spdlog-utests COMMAND spdlog-utests)
endif()
# The header-only library version tests
if(SPDLOG_BUILD_HO_TESTS)
add_executable(spdlog-utests-ho ${SPDLOG_UTESTS_SOURCES})
target_link_libraries(spdlog-utests-ho spdlog::spdlog_header_only)
add_test(NAME spdlog-utests-ho COMMAND spdlog-utests-ho)
endif()