Skip to content

Commit a3872f0

Browse files
Flamefireerikd
authored andcommitted
Add option to enable sanitizers in CMake
Use -DLIBSAMPLERATE_ENABLE_SANITIZERS=ON when using CMake with Clang or Gcc to enable address and undefined behavior sanitizers.
1 parent 9225471 commit a3872f0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ if(WIN32)
3737
include_directories(Win32)
3838
endif()
3939

40+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
41+
option(LIBSAMPLERATE_ENABLE_SANITIZERS "Enable ASAN and UBSAN" OFF)
42+
43+
if(LIBSAMPLERATE_ENABLE_SANITIZERS)
44+
# Use ASAN and UBSAN, make it fail on any error, improve stack traces
45+
set(sanitizer_flags -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
46+
47+
add_compile_options(${sanitizer_flags})
48+
string(REPLACE ";" " " sanitizer_flags "${sanitizer_flags}")
49+
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${sanitizer_flags}")
50+
string(APPEND CMAKE_MODULE_LINKER_FLAGS " ${sanitizer_flags}")
51+
string(APPEND CMAKE_SHARED_LINKER_FLAGS " ${sanitizer_flags}")
52+
endif()
53+
endif()
54+
4055
test_big_endian(CPU_IS_BIG_ENDIAN)
4156
if(CPU_IS_BIG_ENDIAN)
4257
set(CPU_IS_LITTLE_ENDIAN 0)

0 commit comments

Comments
 (0)