Skip to content

Commit

Permalink
Check ASAN support when ENABLE_ASAN=ON.
Browse files Browse the repository at this point in the history
We check if the compiler has asan support when the option is enabled.
If it does not, then we stop compiling with the proper message.

To test this, we use `gcc (GCC) 4.4.7` (no asan support) and
`gcc (GCC) 9.1.0` (asan is supported). Call cmake like this:

`cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_ASAN=On` and
`CXX=g++-4.4 cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_ASAN=On`

we then check that it fails for the older version and it works for
the newer gcc, like expected.

- Fix #409.
  • Loading branch information
denisfa authored and rkitover committed Sep 2, 2019
1 parent ade64db commit f1438e0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Expand Up @@ -480,6 +480,8 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(MY_C_DBG_FLAGS -g -fno-omit-frame-pointer)
endif()

include(CheckCXXCompilerFlag)

if(ENABLE_ASAN)
string(TOLOWER ${CMAKE_BUILD_TYPE} build)
if(NOT build STREQUAL "debug")
Expand All @@ -490,7 +492,13 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
if(SANITIZER STREQUAL "on" OR SANITIZER STREQUAL "true")
set(SANITIZER address)
endif()
set(MY_C_DBG_FLAGS ${MY_C_DBG_FLAGS} -fsanitize=${SANITIZER} -lasan)
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "-lasan")
check_cxx_compiler_flag("-fsanitize=${SANITIZER} -lasan" ASAN_SUPPORT_FLAG)
if(${ASAN_SUPPORT_FLAG})
set(MY_C_DBG_FLAGS ${MY_C_DBG_FLAGS} -fsanitize=${SANITIZER} -lasan)
else()
message(FATAL_ERROR "asan not available to compiler.")
endif()
endif()

# common flags
Expand All @@ -500,8 +508,6 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(MY_C_FLAGS ${MY_C_FLAGS} -fPIC)
endif()

include(CheckCXXCompilerFlag)

# check if ssp flags are supported for this version of gcc
if(CMAKE_COMPILER_IS_GNUCXX)
if(ENABLE_SSP)
Expand Down

0 comments on commit f1438e0

Please sign in to comment.