Skip to content

Commit

Permalink
Add ENABLE_SANITIZER cmake option, use it to enable asan on Travis
Browse files Browse the repository at this point in the history
The old method of simply adding -fsanitize=* to C/CXXFLAGS wasn't
reliable.  It seemed to work in some places, but it hasn't worked on
Fedora for a while since it causes CMake's FindThreads module to not
bother pulling in pthreads.  With this, we can run FindThreads
*before* adding the -fsanitize=* flag.

This also enables AddressSanitizer on Travis, which actually works now
that density is no longer enabled.
  • Loading branch information
nemequ committed Jun 14, 2016
1 parent 47dd6fc commit 0ce4ad8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ case "${1}" in
COMMON_COMPILER_FLAGS="-Werror -fno-omit-frame-pointer -fstack-protector-all -D_FORTIFY_SOURCE=2"
case "${BUILD_TYPE}" in
"asan")
COMMON_COMPILER_FLAGS="${COMMON_COMPILER_FLAGS} -fsanitize=address"
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} -DENABLE_SANITIZER=address"
;;
"tsan")
COMMON_COMPILER_FLAGS="${COMMON_COMPILER_FLAGS} -fsanitize=thread"
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} -DENABLE_SANITIZER=thread"
;;
"ubsan")
COMMON_COMPILER_FLAGS="${COMMON_COMPILER_FLAGS} -fsanitize=undefined"
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} -DENABLE_SANITIZER=undefined"
;;
esac

Expand Down
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ env:
- COMPILER=icc BUILD_TYPE=debug

# Sanitizers
# - COMPILER=gcc-6 BUILD_TYPE=asan
- COMPILER=gcc-6 BUILD_TYPE=asan
# - COMPILER=gcc-6 BUILD_TYPE=ubsan
# - COMPILER=gcc-6 BUILD_TYPE=tsan

Expand All @@ -40,7 +40,7 @@ env:
# - COMPILER=clang-3.8 BUILD_TYPE=debug

# Coverage analysis
- COMPILER=gcc-5 BUILD_TYPE=coverage
- COMPILER=gcc-6 BUILD_TYPE=coverage
global:
# Coveralls.io
- secure: "QDuBYD70tDc+2v4WdNEbmv4BE/jVHrDp9QZ+a7ZYPgnUe5hRnKfKSOhsuzGDRkMBnrJFbaeFITrGY7WQKcwLcMxKq11/Lk6Of9ai+TxN4VY+p1yt3Poz/7pDg7bVTPepnEgVUjjLQC8UoGXo7jaLscqJuU3vAl/LVQ9igC8TwKQ="
Expand All @@ -60,11 +60,11 @@ matrix:
- os: osx
env: COMPILER=icc BUILD_TYPE=debug
- os: osx
env: COMPILER=gcc-5 BUILD_TYPE=asan
env: COMPILER=gcc-6 BUILD_TYPE=asan
- os: osx
env: COMPILER=gcc-5 BUILD_TYPE=ubsan
env: COMPILER=gcc-6 BUILD_TYPE=ubsan
- os: osx
env: COMPILER=gcc-5 BUILD_TYPE=tsan
env: COMPILER=gcc-6 BUILD_TYPE=tsan
- os: osx
env: COMPILER=clang BUILD_TYPE=release
- os: osx
Expand All @@ -86,7 +86,7 @@ matrix:
- os: osx
env: COMPILER=clang-3.8 BUILD_TYPE=debug
- os: osx
env: COMPILER=gcc-5 BUILD_TYPE=coverage
env: COMPILER=gcc-6 BUILD_TYPE=coverage
before_install:
- ./.travis.sh deps
script:
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ include (CheckFunctionExists)
include (CheckIncludeFile)
include (AddCompilerFlags)

set(THREADS_PREFER_PTHREAD_FLAG)
find_package (Threads REQUIRED)

enable_testing ()

if (ENABLE_COVERAGE STREQUAL "yes")
Expand Down Expand Up @@ -171,6 +174,11 @@ if (ENABLE_COVERAGE STREQUAL "yes")
endif ()
endif ()

if (ENABLE_SANITIZER)
set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
endif ()

function (squash_target_add_coverage target)
if (ENABLE_COVERAGE)
set_property (TARGET ${target} APPEND_STRING PROPERTY COMPILE_FLAGS " -fprofile-arcs")
Expand Down
7 changes: 5 additions & 2 deletions squash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ if (NOT CMAKE_BUILD_TYPE MATCHES "Release")
endif ()
endif ()

find_package (Threads)
target_link_libraries (squash${SQUASH_VERSION_API} ${CMAKE_THREAD_LIBS_INIT})
if ($CMAKE_VERSION VERSION_LESS 3.1)
target_link_libraries (squash${SQUASH_VERSION_API} ${CMAKE_THREAD_LIBS_INIT})
else()
target_link_libraries (squash${SQUASH_VERSION_API} Threads::Threads)
endif()

squash_target_add_coverage (squash${SQUASH_VERSION_API})

Expand Down
8 changes: 6 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
enable_testing()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package (Threads REQUIRED)

set(SQUASH_TEST_SOURCES
munit/munit.c
Expand Down Expand Up @@ -46,10 +45,15 @@ add_executable (test-squash ${SQUASH_TEST_SOURCES})
set_property(TARGET test-squash
APPEND PROPERTY COMPILE_DEFINITIONS "SQUASH_TEST_PLUGIN_DIR=\"${CMAKE_BINARY_DIR}/plugins\"")
target_add_extra_warning_flags (test-squash)
target_link_libraries (test-squash squash${SQUASH_VERSION_API} ${CMAKE_THREAD_LIBS_INIT})
target_require_c_standard (test-squash "c99")
target_add_compiler_flags (test-squash ${extra_compiler_flags})

if ($CMAKE_VERSION VERSION_LESS 3.1)
target_link_libraries (test-squash squash${SQUASH_VERSION_API} ${CMAKE_THREAD_LIBS_INIT})
else()
target_link_libraries (test-squash squash${SQUASH_VERSION_API} Threads::Threads)
endif()

if (ENABLE_INSTALLED_TESTS)
add_executable (squash-all ${SQUASH_TEST_SOURCES})
target_add_extra_warning_flags (squash-all)
Expand Down

0 comments on commit 0ce4ad8

Please sign in to comment.