Fixed CMake address sanitizer

This commit is contained in:
gabime 2019-06-10 18:32:10 +03:00
parent 68a0193d95
commit cf64f2baca
4 changed files with 28 additions and 27 deletions

View File

@ -45,6 +45,7 @@ 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_BUILD_HO_TESTS "Build tests using the header only version" OFF)
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" 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)
@ -57,7 +58,7 @@ find_package(Threads REQUIRED)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
add_library(spdlog STATIC src/spdlog.cpp ${SPDLOG_ALL_HEADERS}) add_library(spdlog STATIC src/spdlog.cpp ${SPDLOG_ALL_HEADERS})
add_library(spdlog::spdlog ALIAS spdlog) 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>"

View File

@ -12,30 +12,30 @@ function(spdlog_extract_version)
if (NOT ver_major OR NOT ver_minor OR NOT ver_patch) if (NOT ver_major OR NOT ver_minor OR NOT ver_patch)
message(FATAL_ERROR "Could not extract valid version from spdlog/version.h") message(FATAL_ERROR "Could not extract valid version from spdlog/version.h")
endif() endif()
set (SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE) set (SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE)
endfunction() endfunction()
# Turn on warnings on the given target # Turn on warnings on the given target
function(spdlog_enable_warnings target_name) function(spdlog_enable_warnings target_name)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang") if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -pedantic -Wfatal-errors) target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -pedantic -Wfatal-errors)
endif() endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_options(${target_name} PRIVATE /W4 /WX ) target_compile_options(${target_name} PRIVATE /W4 /WX )
endif() endif()
endfunction() endfunction()
# Enable address sanitizer (gcc/clang only) # Enable address sanitizer (gcc/clang only)
function(spdlog_enable_sanitizer target_name) function(spdlog_enable_sanitizer target_name)
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(FATAL_ERROR "Sanitizer supported only for gcc/clang") message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
endif() endif()
message(STATUS "Address sanitizer enabled") message(STATUS "Address sanitizer enabled")
target_compile_options(${target_name} "-fsanitize=address,undefined") target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
target_compile_options(${target_name} "-fno-sanitize=signed-integer-overflow") target_compile_options(${target_name} PRIVATE -fno-sanitize=signed-integer-overflow)
target_compile_options(${target_name} "-fno-sanitize-recover=all") target_compile_options(${target_name} PRIVATE -fno-sanitize-recover=all)
target_compile_options(${target_name} "-fno-omit-frame-pointer") target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
target_link_libraries(${target_name} "-fsanitize=address,undefined -fuse-ld=gold") target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined -fuse-ld=gold)
endfunction() endfunction()

View File

@ -14,14 +14,14 @@ endif()
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
add_executable(example example.cpp) add_executable(example example.cpp)
spdlog_enable_warnings(example) spdlog_enable_warnings(example)
target_link_libraries(example spdlog::spdlog) target_link_libraries(example PRIVATE spdlog::spdlog)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# Example of using header-only library # Example of using header-only library
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
add_executable(example_header_only example.cpp) add_executable(example_header_only example.cpp)
spdlog_enable_warnings(example_header_only) spdlog_enable_warnings(example_header_only)
target_link_libraries(example_header_only spdlog::spdlog_header_only) target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only)
# Create logs directory # Create logs directory
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")

View File

@ -22,21 +22,21 @@ enable_testing()
if(SPDLOG_BUILD_TESTS) if(SPDLOG_BUILD_TESTS)
add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES}) add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES})
spdlog_enable_warnings(spdlog-utests) spdlog_enable_warnings(spdlog-utests)
target_link_libraries(spdlog-utests spdlog) target_link_libraries(spdlog-utests PRIVATE spdlog)
if(SPDLOG_SANITIZE_ADDRESS) if(SPDLOG_SANITIZE_ADDRESS)
spdlog_enable_sanitizer(spdlog-utests) spdlog_enable_sanitizer(spdlog-utests)
endif() endif()
add_test(NAME spdlog-utests COMMAND spdlog-utests) add_test(NAME spdlog-utests COMMAND spdlog-utests)
endif() endif()
# The header-only library version tests # The header-only library version tests
if(SPDLOG_BUILD_HO_TESTS) if(SPDLOG_BUILD_HO_TESTS)
add_executable(spdlog-utests-ho ${SPDLOG_UTESTS_SOURCES}) add_executable(spdlog-utests-ho ${SPDLOG_UTESTS_SOURCES})
spdlog_enable_warnings(spdlog-utests-ho) spdlog_enable_warnings(spdlog-utests-ho)
target_link_libraries(spdlog-utests-ho spdlog::spdlog_header_only) target_link_libraries(spdlog-utests-ho PRIVATE spdlog::spdlog_header_only)
if(SPDLOG_SANITIZE_ADDRESS) if(SPDLOG_SANITIZE_ADDRESS)
spdlog_set_address_sanitizer(spdlog-utests-ho) spdlog_set_address_sanitizer(spdlog-utests-ho)
endif() endif()
add_test(NAME spdlog-utests-ho COMMAND spdlog-utests-ho) add_test(NAME spdlog-utests-ho COMMAND spdlog-utests-ho)
endif() endif()