CMake refactoring to functions
This commit is contained in:
parent
80740f0e46
commit
68a0193d95
@ -7,7 +7,8 @@ cmake_minimum_required(VERSION 3.1)
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Start spdlog project
|
||||
#---------------------------------------------------------------------------------------
|
||||
include(cmake/version.cmake)
|
||||
include(cmake/utils.cmake)
|
||||
spdlog_extract_version()
|
||||
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
|
||||
message(STATUS "Build spdlog: ${SPDLOG_VERSION}")
|
||||
|
||||
@ -49,7 +50,6 @@ option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
|
||||
|
||||
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
|
||||
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
@ -63,6 +63,7 @@ target_include_directories(spdlog PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||
target_link_libraries(spdlog PUBLIC Threads::Threads)
|
||||
spdlog_enable_warnings(spdlog)
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Header only version
|
||||
@ -75,19 +76,6 @@ target_include_directories(spdlog_header_only INTERFACE
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||
target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Turn on compiler warnings and sanitizers if we build our own project
|
||||
#---------------------------------------------------------------------------------------
|
||||
if(SPDLOG_MASTER_PROJECT)
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
|
||||
target_compile_options( spdlog PRIVATE -Wall -Wextra -Wconversion -pedantic -Wfatal-errors)
|
||||
endif()
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
||||
target_compile_options( spdlog PRIVATE /W3 /WX )
|
||||
endif()
|
||||
|
||||
include(cmake/sanitizers.cmake)
|
||||
endif()
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
# use fmt package if using exertnal fmt
|
||||
|
@ -13,6 +13,7 @@ find_package(Threads REQUIRED)
|
||||
find_package(benchmark CONFIG REQUIRED)
|
||||
|
||||
add_executable(bench bench.cpp)
|
||||
spdlog_enable_warnings(bench)
|
||||
target_link_libraries(bench PRIVATE spdlog::spdlog)
|
||||
|
||||
add_executable(async_bench async_bench.cpp)
|
||||
|
@ -1,21 +0,0 @@
|
||||
if(SPDLOG_SANITIZE_THREAD AND SPDLOG_SANITIZE_ADDRESS)
|
||||
message(FATAL_ERROR "AddressSanitizer is not compatible with ThreadSanitizer.")
|
||||
endif()
|
||||
|
||||
if(SPDLOG_SANITIZE_ADDRESS)
|
||||
message(STATUS "AddressSanitizer enabled")
|
||||
set(SANITIZER_FLAGS "-fsanitize=address,undefined")
|
||||
add_compile_options("-fno-sanitize=signed-integer-overflow")
|
||||
endif()
|
||||
|
||||
if(SPDLOG_SANITIZE_THREAD)
|
||||
message(STATUS "ThreadSanitizer enabled")
|
||||
set(SANITIZER_FLAGS "-fsanitize=thread")
|
||||
endif()
|
||||
|
||||
if(SPDLOG_SANITIZE_THREAD OR SPDLOG_SANITIZE_ADDRESS)
|
||||
add_compile_options(${SANITIZER_FLAGS})
|
||||
add_compile_options("-fno-sanitize-recover=all")
|
||||
add_compile_options("-fno-omit-frame-pointer")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAGS} -fuse-ld=gold")
|
||||
endif()
|
41
cmake/utils.cmake
Normal file
41
cmake/utils.cmake
Normal file
@ -0,0 +1,41 @@
|
||||
# Get spdlog version from include/spdlog/version.h and put it in SPDLOG_VERSION
|
||||
function(spdlog_extract_version)
|
||||
file(READ "${CMAKE_CURRENT_LIST_DIR}/include/spdlog/version.h" file_contents)
|
||||
string(REGEX MATCH "SPDLOG_VER_MAJOR ([0-9]+)" _ "${file_contents}")
|
||||
set(ver_major ${CMAKE_MATCH_1})
|
||||
|
||||
string(REGEX MATCH "SPDLOG_VER_MINOR ([0-9]+)" _ "${file_contents}")
|
||||
set(ver_minor ${CMAKE_MATCH_1})
|
||||
string(REGEX MATCH "SPDLOG_VER_PATCH ([0-9]+)" _ "${file_contents}")
|
||||
set(ver_patch ${CMAKE_MATCH_1})
|
||||
|
||||
if (NOT ver_major OR NOT ver_minor OR NOT ver_patch)
|
||||
message(FATAL_ERROR "Could not extract valid version from spdlog/version.h")
|
||||
endif()
|
||||
set (SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
|
||||
# Turn on warnings on the given target
|
||||
function(spdlog_enable_warnings target_name)
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
|
||||
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -pedantic -Wfatal-errors)
|
||||
endif()
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
||||
target_compile_options(${target_name} PRIVATE /W4 /WX )
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
|
||||
# Enable address sanitizer (gcc/clang only)
|
||||
function(spdlog_enable_sanitizer target_name)
|
||||
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
||||
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
|
||||
endif()
|
||||
message(STATUS "Address sanitizer enabled")
|
||||
target_compile_options(${target_name} "-fsanitize=address,undefined")
|
||||
target_compile_options(${target_name} "-fno-sanitize=signed-integer-overflow")
|
||||
target_compile_options(${target_name} "-fno-sanitize-recover=all")
|
||||
target_compile_options(${target_name} "-fno-omit-frame-pointer")
|
||||
target_link_libraries(${target_name} "-fsanitize=address,undefined -fuse-ld=gold")
|
||||
endfunction()
|
@ -1,16 +0,0 @@
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Get spdlog version from include/spdlog/version.h
|
||||
#---------------------------------------------------------------------------------------
|
||||
file(READ "${CMAKE_CURRENT_LIST_DIR}/../include/spdlog/version.h" SPDLOG_VERSION_FILE)
|
||||
string(REGEX MATCH "SPDLOG_VER_MAJOR ([0-9]+)" _ "${SPDLOG_VERSION_FILE}")
|
||||
set(ver_major ${CMAKE_MATCH_1})
|
||||
|
||||
string(REGEX MATCH "SPDLOG_VER_MINOR ([0-9]+)" _ "${SPDLOG_VERSION_FILE}")
|
||||
set(ver_minor ${CMAKE_MATCH_1})
|
||||
string(REGEX MATCH "SPDLOG_VER_PATCH ([0-9]+)" _ "${SPDLOG_VERSION_FILE}")
|
||||
set(ver_patch ${CMAKE_MATCH_1})
|
||||
|
||||
if (NOT ver_major OR NOT ver_minor OR NOT ver_patch)
|
||||
message(FATAL_ERROR "Could not extract valid version from spdlog/version.h")
|
||||
endif()
|
||||
set (SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}")
|
@ -13,12 +13,14 @@ endif()
|
||||
# Example of using pre-compiled library
|
||||
#---------------------------------------------------------------------------------------
|
||||
add_executable(example example.cpp)
|
||||
spdlog_enable_warnings(example)
|
||||
target_link_libraries(example spdlog::spdlog)
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Example of using header-only library
|
||||
#---------------------------------------------------------------------------------------
|
||||
add_executable(example_header_only example.cpp)
|
||||
spdlog_enable_warnings(example_header_only)
|
||||
target_link_libraries(example_header_only spdlog::spdlog_header_only)
|
||||
|
||||
# Create logs directory
|
||||
|
@ -21,13 +21,22 @@ enable_testing()
|
||||
# The compiled library tests
|
||||
if(SPDLOG_BUILD_TESTS)
|
||||
add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES})
|
||||
spdlog_enable_warnings(spdlog-utests)
|
||||
target_link_libraries(spdlog-utests spdlog)
|
||||
add_test(NAME spdlog-utests COMMAND spdlog-utests)
|
||||
if(SPDLOG_SANITIZE_ADDRESS)
|
||||
spdlog_enable_sanitizer(spdlog-utests)
|
||||
endif()
|
||||
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})
|
||||
spdlog_enable_warnings(spdlog-utests-ho)
|
||||
target_link_libraries(spdlog-utests-ho spdlog::spdlog_header_only)
|
||||
if(SPDLOG_SANITIZE_ADDRESS)
|
||||
spdlog_set_address_sanitizer(spdlog-utests-ho)
|
||||
endif()
|
||||
add_test(NAME spdlog-utests-ho COMMAND spdlog-utests-ho)
|
||||
endif()
|
Loading…
Reference in New Issue
Block a user