@@ -9,11 +9,27 @@ set(CMAKE_CXX_EXTENSIONS OFF)
99# Position Independent Code (required for TSan and good practice for libraries)
1010set (CMAKE_POSITION_INDEPENDENT_CODE ON )
1111
12- # Enable AddressSanitizer
13- if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" )
14- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS } -fsanitize=address -fno-omit-frame-pointer" )
15- set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS } -fsanitize=address" )
16- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS } -fsanitize=address" )
12+ # Build options
13+ option (BUILD_TESTS "Build tests" ON )
14+ option (BUILD_SHARED_LIBS "Build shared libraries" OFF )
15+ option (BUILD_COVERAGE "Build with coverage instrumentation" OFF )
16+ set (USE_SANITIZER "address" CACHE STRING "Sanitizer to use: address, thread, or none" )
17+
18+ # Configure Sanitizers
19+ if (NOT USE_SANITIZER STREQUAL "none" )
20+ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" )
21+ if (USE_SANITIZER STREQUAL "address" )
22+ set (SAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer" )
23+ elseif (USE_SANITIZER STREQUAL "thread" )
24+ set (SAN_FLAGS "-fsanitize=thread" )
25+ endif ()
26+
27+ if (SAN_FLAGS)
28+ add_compile_options (${SAN_FLAGS} )
29+ add_link_options (${SAN_FLAGS} )
30+ message (STATUS "Enabled sanitizer: ${USE_SANITIZER} " )
31+ endif ()
32+ endif ()
1733endif ()
1834
1935# Enable compiler warnings
@@ -38,11 +54,6 @@ else()
3854 message (WARNING "clang-tidy not found" )
3955endif ()
4056
41- # Build options
42- option (BUILD_TESTS "Build tests" ON )
43- option (BUILD_SHARED_LIBS "Build shared libraries" OFF )
44- option (BUILD_COVERAGE "Build with coverage instrumentation" OFF )
45-
4657if (BUILD_COVERAGE)
4758 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" )
4859 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS } --coverage" )
@@ -132,5 +143,5 @@ message(STATUS " Version: ${PROJECT_VERSION}")
132143message (STATUS " C++ Standard: ${CMAKE_CXX_STANDARD } " )
133144message (STATUS " Compiler: ${CMAKE_CXX_COMPILER_ID } " )
134145message (STATUS " Build Tests: ${BUILD_TESTS} " )
135- message (STATUS " Sanitizers: AddressSanitizer enabled " )
146+ message (STATUS " Sanitizer: ${USE_SANITIZER} " )
136147message (STATUS "=============================================" )
0 commit comments