From 9575715b1edbc613161fac8ca74213a8f80bd0a4 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 31 Dec 2016 22:35:24 +0100 Subject: [PATCH] - Added support to GCC/Clang sanitizers. --- src/CMakeLists.txt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d006d2948..64e865a21 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -452,6 +452,33 @@ if( ZD_CMAKE_COMPILER_IS_GNUCXX_COMPATIBLE ) set( CMAKE_CXX_FLAGS_MINSIZEREL "${REL_CXX_FLAGS} ${CMAKE_CXX_FLAGS_MINSIZEREL}" ) set( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${REL_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" ) + # Support for the GCC/Clang sanitizers. + set( WITH_ASAN 0 CACHE BOOL "Enable the Address Sanitizer") + if( NOT CMAKE_COMPILER_IS_GNUCXX ) + set( WITH_MSAN 0 CACHE BOOL "Enable the Memory Sanitizer") + endif( NOT CMAKE_COMPILER_IS_GNUCXX ) + set( WITH_UBSAN 0 CACHE BOOL "Enable the Undefined Behavior Sanitizer") + if( WITH_MSAN ) + if ( WITH_ASAN OR WITH_UBSAN ) + message( SEND_ERROR "You can't use MSAN with either ASAN or UBSAN." ) + endif ( WITH_ASAN OR WITH_UBSAN ) + endif( WITH_MSAN ) + + set( SANITIZER_FLAG "" ) + if( WITH_ASAN ) + set( SANITIZER_FLAG "-fsanitize=address" ) + if ( WITH_UBSAN ) + set( SANITIZER_FLAG "${SANITIZER_FLAG},undefined" ) + endif( WITH_UBSAN ) + elseif( WITH_MSAN ) + set( SANITIZER_FLAG "-fsanitize=memory" ) + elseif( WITH_UBSAN ) + set( SANITIZER_FLAG "-fsanitize=undefined" ) + endif( WITH_ASAN ) + + set( CMAKE_CXX_FLAGS "${SANITIZER_FLAG} ${CMAKE_CXX_FLAGS}" ) + set( CMAKE_C_FLAGS "${SANITIZER_FLAG} ${CMAKE_C_FLAGS}" ) + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "4.5") set( CMAKE_C_FLAGS "-Wno-unused-result ${CMAKE_C_FLAGS}" ) set( CMAKE_CXX_FLAGS "-Wno-unused-result ${CMAKE_CXX_FLAGS}" )