|
|
@@ -1,17 +1,31 @@
|
|
|
-
|
|
|
# Enable address sanitizer (gcc/clang only)
|
|
|
-macro(ENABLE_SANITIZER)
|
|
|
-
|
|
|
- if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
|
|
- message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
|
|
|
- endif()
|
|
|
-
|
|
|
- set(SANITIZER_FLAGS "-fsanitize=address -fsanitize=leak -g")
|
|
|
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}")
|
|
|
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}")
|
|
|
+if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
|
|
+ set(SANITIZERS)
|
|
|
|
|
|
- set(LINKER_FLAGS "-fsanitize=address,undefined -fuse-ld=gold")
|
|
|
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
|
|
+ macro(add_sanitizer_option variable flag help)
|
|
|
+ option(${variable} "Enable ${help}" OFF)
|
|
|
+ if(${variable})
|
|
|
+ list(APPEND SANITIZERS ${flag})
|
|
|
+ endif()
|
|
|
+ mark_as_advanced(${variable})
|
|
|
+ endmacro()
|
|
|
|
|
|
-endmacro()
|
|
|
+ add_sanitizer_option(SANITIZE_ADDRESS "address"
|
|
|
+ "AddressSanitizer (detects memory violations, buffer overflows, memory leaks)")
|
|
|
+ add_sanitizer_option(SANITIZE_LEAK "leak"
|
|
|
+ "standalone LeakSanitizer (detects memory leaks only)")
|
|
|
+ add_sanitizer_option(SANITIZE_MEMORY "memory"
|
|
|
+ "MemorySanitizer (detects reads in uninitialized memory)")
|
|
|
+ add_sanitizer_option(SANITIZE_UNDEFINED "undefined"
|
|
|
+ "UndefinedBehaviorSanitizer (detects undefined behavior)")
|
|
|
+ add_sanitizer_option(SANITIZE_THREAD "thread"
|
|
|
+ "ThreadSanitizer (detects data races)")
|
|
|
|
|
|
+ if(SANITIZERS)
|
|
|
+ string(REPLACE ";" "," SANITIZER_FLAGS "${SANITIZERS}")
|
|
|
+ set(SANITIZER_FLAGS "-fsanitize=${SANITIZER_FLAGS}")
|
|
|
+ string(APPEND CMAKE_CXX_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
+ string(APPEND CMAKE_C_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
+ string(APPEND CMAKE_EXE_LINKER_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
+ endif()
|
|
|
+endif()
|