Skip to content

Commit a4decad

Browse files
authored
Add sanitizer option to cmake (#147)
1 parent 3253dad commit a4decad

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

CMakeLists.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,31 @@ if (APPLE)
1111
# set(CMAKE_OSX_ARCHITECTURES arm64 x86_64)
1212
endif()
1313

14-
# Uncomment to enable tsan
15-
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
16-
14+
# sanitizer options, from https://github.com/sudara/cmake-includes/blob/main/Sanitizers.cmake
15+
# set the corresponding option ON to turn on Address/Thread Sanitizer
16+
option(WITH_ADDRESS_SANITIZER "Enable Address Sanitizer" OFF)
17+
option(WITH_THREAD_SANITIZER "Enable Thread Sanitizer" OFF)
18+
19+
message(STATUS "Sanitizers: ASan=${WITH_ADDRESS_SANITIZER} TSan=${WITH_THREAD_SANITIZER}")
20+
if (WITH_ADDRESS_SANITIZER)
21+
if (MSVC)
22+
add_compile_options(/fsanitize=address)
23+
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
24+
# also enable UndefinedBehaviorSanitizer
25+
# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
26+
add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer)
27+
link_libraries(-fsanitize=address)
28+
endif ()
29+
message("Address Sanitizer enabled")
30+
endif ()
31+
32+
if (WITH_THREAD_SANITIZER)
33+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
34+
add_compile_options(-fsanitize=thread -g -fno-omit-frame-pointer)
35+
link_libraries(-fsanitize=thread)
36+
message("Thread Sanitizer enabled")
37+
endif ()
38+
endif ()
1739

1840
# Adds all the module sources so they appear correctly in the IDE
1941
set_property(GLOBAL PROPERTY USE_FOLDERS YES)

0 commit comments

Comments
 (0)