From 49d663f6c8001af5277cc4b63584f439934a684c Mon Sep 17 00:00:00 2001 From: Daniel Chabrowski Date: Sun, 12 Aug 2018 14:32:07 +0200 Subject: [PATCH] Move tsan/asan flag management to cmake Add sudo: required to fix asan builds --- .travis.yml | 13 ++++++++++--- CMakeLists.txt | 2 ++ cmake/sanitizers.cmake | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 cmake/sanitizers.cmake diff --git a/.travis.yml b/.travis.yml index 2c87f4a8..811d0503 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ # - Louis Dionne's Hana: https://github.com/ldionne/hana # - Paul Fultz II's FIT: https://github.com/pfultz2/Fit # - Eric Niebler's range-v3: https://github.com/ericniebler/range-v3 +sudo: required language: cpp addons: &gcc48 @@ -96,14 +97,20 @@ before_install: - valgrind --version install: - - if [ "$ASAN" == "On" ]; then export CXX_FLAGS="${CXX_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize=signed-integer-overflow -fuse-ld=gold"; fi - - if [ "$TSAN" == "On" ]; then export CXX_FLAGS="${CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -fuse-ld=gold"; fi - cd ${TRAVIS_BUILD_DIR} - mkdir -p build && cd build - - cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_STANDARD=$CPP -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" -DSPDLOG_BUILD_EXAMPLES=ON --warn-uninitialized + - | + cmake .. \ + --warn-uninitialized \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DCMAKE_CXX_STANDARD=$CPP \ + -DSPDLOG_BUILD_EXAMPLES=ON \ + -DSPDLOG_SANITIZE_THREAD=$TSAN \ + -DSPDLOG_SANITIZE_ADDRESS=$ASAN - VERBOSE=1 make -j2 script: + - export TSAN_OPTIONS=verbosity=1 - if [ "$ASAN" != "On" ] && [ "$TSAN" != "On" ]; then CTEST_FLAGS="-DExperimentalMemCheck"; fi - ctest -j2 -VV $CTEST_FLAGS diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a821129..a5cceb70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,8 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCH set(CMAKE_CXX_FLAGS "-Wall -O3 ${CMAKE_CXX_FLAGS}") endif() +include(cmake/sanitizers.cmake) + #--------------------------------------------------------------------------------------- # spdlog target #--------------------------------------------------------------------------------------- diff --git a/cmake/sanitizers.cmake b/cmake/sanitizers.cmake new file mode 100644 index 00000000..3b090835 --- /dev/null +++ b/cmake/sanitizers.cmake @@ -0,0 +1,21 @@ +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()