cmake: enable AddressSanitizer in Debug builds if supported
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 40ddc0d..4e20eef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,7 @@ include(CheckSymbolExists)
include(CheckCSourceCompiles)
include(CheckCSourceRuns)
include(CheckCCompilerFlag)
+include(CheckCXXCompilerFlag)
include(CheckTypeSize)
include(CheckStructHasMember)
include(CMakeDependentOption)
@@ -2323,6 +2324,56 @@ if(SDL_STATIC)
endif()
endif()
+macro(asan_check_add_debug_flag ASAN_FLAG)
+ set(FLAG "-fsanitize=${ASAN_FLAG}")
+
+ check_c_compiler_flag(${FLAG} HAS_C_FLAG_${ASAN_FLAG})
+ if (HAS_C_FLAG_${ASAN_FLAG})
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${FLAG}")
+ endif()
+
+ check_cxx_compiler_flag("${FLAG}" HAS_CXX_${ASAN_FLAG})
+ if (HAS_CXX_${ASAN_FLAG})
+ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${FLAG}")
+ endif()
+endmacro()
+
+macro(asan_check_add_debug_flag2 ASAN_FLAG)
+ # for some sanitize flags we have to manipulate the CMAKE_REQUIRED_LIBRARIES:
+ # http://cmake.3232098.n2.nabble.com/CHECK-CXX-COMPILER-FLAG-doesn-t-give-correct-result-for-fsanitize-address-tp7600216p7600217.html
+
+ set(FLAG "-fsanitize=${ASAN_FLAG}")
+
+ set (STORED_REQLIBS ${CMAKE_REQUIRED_LIBRARIES})
+ set (CMAKE_REQUIRED_LIBRARIES "${FLAG};asan")
+ check_c_compiler_flag (${FLAG} HAS_C_FLAG_${ASAN_FLAG})
+ check_cxx_compiler_flag (${FLAG} HAS_CXX_FLAG_${ASAN_FLAG})
+ set (CMAKE_REQUIRED_LIBRARIES ${STORED_REQLIBS})
+
+ if (HAS_C_FLAG_${ASAN_FLAG})
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${FLAG}")
+ endif()
+
+ if (HAS_CXX_${ASAN_FLAG})
+ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${FLAG}")
+ endif()
+endmacro()
+
+# enable AddressSanitizer if supported
+asan_check_add_debug_flag2("address")
+asan_check_add_debug_flag("bool")
+asan_check_add_debug_flag("bounds")
+asan_check_add_debug_flag("enum")
+asan_check_add_debug_flag("float-cast-overflow")
+asan_check_add_debug_flag("float-divide-by-zero")
+asan_check_add_debug_flag("nonnull-attribute")
+asan_check_add_debug_flag("returns-nonnull-attribute")
+asan_check_add_debug_flag("signed-integer-overflow")
+asan_check_add_debug_flag("undefined")
+asan_check_add_debug_flag("vla-bound")
+asan_check_add_debug_flag("leak")
+asan_check_add_debug_flag("object-size")
+
##### Tests #####
if(SDL_TEST)