From 30bd80bd85df4e848ef4cbb701b631acf2c10e05 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 29 May 2019 00:04:36 +0300 Subject: [PATCH] CMake improvements --- CMakeLists.txt | 36 +++++++++++++++++++++++++++--------- bench/CMakeLists.txt | 3 +-- example/CMakeLists.txt | 10 ++-------- tests/CMakeLists.txt | 19 ++++++++++++++----- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e336521..b30790cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,18 +3,17 @@ cmake_minimum_required(VERSION 3.1) project(spdlog VERSION 1.3.1 LANGUAGES CXX) -include(CMakeDependentOption) include(GNUInstallDirs) #--------------------------------------------------------------------------------------- -# set default build to release +# Set default build to release #--------------------------------------------------------------------------------------- if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE) endif() #--------------------------------------------------------------------------------------- -# compiler config +# Compiler config #--------------------------------------------------------------------------------------- set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -35,6 +34,7 @@ endif () 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_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_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) +#--------------------------------------------------------------------------------------- +# 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 #--------------------------------------------------------------------------------------- -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_include_directories(spdlog PUBLIC - "$" - "$") + "$" + "$") target_link_libraries(spdlog PUBLIC Threads::Threads) #--------------------------------------------------------------------------------------- # Header only version #--------------------------------------------------------------------------------------- add_library(spdlog_header_only INTERFACE) +add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only) target_include_directories(spdlog_header_only INTERFACE "$" "$") target_link_libraries(spdlog_header_only INTERFACE Threads::Threads) - #--------------------------------------------------------------------------------------- # Turn on compiler warnings and sanitizers if we build our own project #--------------------------------------------------------------------------------------- @@ -146,5 +165,4 @@ if (SPDLOG_INSTALL) #--------------------------------------------------------------------------------------- include(cmake/SpdlogCPack.cmake) -endif () - +endif () \ No newline at end of file diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt index 5b88d41a..6457a89d 100644 --- a/bench/CMakeLists.txt +++ b/bench/CMakeLists.txt @@ -2,7 +2,7 @@ # Distributed under the MIT License (http://opensource.org/licenses/MIT) cmake_minimum_required(VERSION 3.1) -project(SpdlogBench CXX) +project(spdlog_bench CXX) if(NOT TARGET spdlog) # Stand-alone build @@ -21,7 +21,6 @@ target_link_libraries(async_bench PRIVATE spdlog::spdlog) add_executable(latency latency.cpp) target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog) - add_executable(formatter-bench formatter-bench.cpp) target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 2fbbf1d2..994051f9 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -2,15 +2,9 @@ # Distributed under the MIT License (http://opensource.org/licenses/MIT) cmake_minimum_required(VERSION 3.1) -project(SpdlogExamples CXX) +project(spdlog_examples CXX) -if(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() +if(NOT TARGET spdlog) # Stand-alone build find_package(spdlog REQUIRED) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3275694a..a59cf07b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,10 +15,19 @@ set(SPDLOG_UTESTS_SOURCES test_sink.h 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") - 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() \ No newline at end of file