From 32fb9d51b9f0a50216782b963851bd989953161d Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 13 May 2019 01:06:25 +0300 Subject: [PATCH] Cmake update to support both header-only and static --- CMakeLists.txt | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 017db67e..73fa9254 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,6 @@ else() set(SPDLOG_MASTER_PROJECT OFF) endif() -option(SPDLOG_STATIC_LIB "Build as static lib" ON) option(SPDLOG_BUILD_EXAMPLES "Build examples" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF) option(SPDLOG_BUILD_TESTS "Build tests" ON) @@ -55,29 +54,38 @@ option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include/spdlog") message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) -message(STATUS "Static lib: " ${SPDLOG_STATIC_LIB}) -if(SPDLOG_STATIC_LIB) - add_definitions(-DSPDLOG_STATIC_LIB) - set(SRC_BASE "${CMAKE_CURRENT_SOURCE_DIR}/src") - set(SRC_FILES "${SRC_BASE}/spdlog.cpp") - add_library(spdlog STATIC ${SRC_FILES}) - target_include_directories(spdlog PUBLIC "$") - target_link_libraries(spdlog) +# Build static lib +set(SRC_BASE "${CMAKE_CURRENT_SOURCE_DIR}/src") +set(STATIC_SRC_FILES "${SRC_BASE}/spdlog.cpp") +add_library(spdlog_static STATIC ${STATIC_SRC_FILES}) +add_library(spdlog::static ALIAS spdlog_static) + +if (CMAKE_BUILD_TYPE EQUAL "RELEASE") +set_target_properties(spdlog_static PROPERTIES OUTPUT_NAME "spdlog") else() - add_library(spdlog INTERFACE) - target_include_directories(spdlog INTERFACE "$") +set_target_properties(spdlog_static PROPERTIES OUTPUT_NAME "spdlog-debug") endif() -add_library(spdlog::spdlog ALIAS spdlog) + +target_compile_definitions(spdlog_static PUBLIC SPDLOG_STATIC_LIB ) +target_include_directories(spdlog_static PUBLIC "$") + +# headr only +add_library(spdlog_header_only INTERFACE) +target_include_directories(spdlog_header_only INTERFACE "$") +add_library(spdlog::spdlog ALIAS spdlog_header_only) if(SPDLOG_FMT_EXTERNAL AND NOT TARGET fmt::fmt) find_package(fmt REQUIRED CONFIG) endif() if(SPDLOG_FMT_EXTERNAL) - target_compile_definitions(spdlog INTERFACE SPDLOG_FMT_EXTERNAL) - target_link_libraries(spdlog INTERFACE fmt::fmt) + target_compile_definitions(spdlog_static INTERFACE SPDLOG_FMT_EXTERNAL) + target_link_libraries(spdlog_static INTERFACE fmt::fmt) + + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL) + target_link_libraries(spdlog_header_only INTERFACE fmt::fmt) endif() if(SPDLOG_BUILD_EXAMPLES) @@ -97,10 +105,8 @@ endif() # install #--------------------------------------------------------------------------------------- install(DIRECTORY ${HEADER_BASE} DESTINATION include) +install(TARGETS spdlog_static ARCHIVE DESTINATION lib) -if(SPDLOG_STATIC_LIB) - install(TARGETS spdlog ARCHIVE DESTINATION lib) -endif() #--------------------------------------------------------------------------------------- # register project in CMake user registry