Skip to content

Commit fdb27ad

Browse files
committed
fix
1 parent a8bc5e8 commit fdb27ad

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

cmake/sanitizers.cmake

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
set(AVAILABLE_SANITIZERS "address;leak;memory;thread;undefined")
2-
OPTION(USE_SANITIZER "Enable sanitizer(s). Options are: ${AVAILABLE_SANITIZERS}. Case insensitive; multiple options delimited by comma or space possible." "")
1+
set(AVALAIBLE_SANITIZERS "address;leak;memory;thread;undefined")
2+
OPTION(USE_SANITIZER "Enable sanitizer(s). Options are: ${AVALAIBLE_SANITIZERS}. Case insensitive; multiple options delimited by comma or space possible." "")
33
string(TOLOWER "${USE_SANITIZER}" USE_SANITIZER)
44

5-
if(USE_SANITIZER AND NOT (CMAKE_BUILD_TYPE IN_LIST "Debug;RelWithDebInfo"))
6-
message(FATAL_ERROR "❌ Sanitizer only supported in Debug and RelWithDebInfo build types. Current: ${CMAKE_BUILD_TYPE}")
5+
if((CMAKE_BUILD_TYPE IN_LIST "Debug;RelWithDebInfo") AND USE_SANITIZER)
6+
message(FATAL_ERROR "❌ Sanitizer only supported in Debug and RelWithDebInfo build types.")
77
endif()
88

99
if(USE_SANITIZER)
1010
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
1111
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
1212

1313
if(USE_SANITIZER MATCHES "address")
14-
string(APPEND CMAKE_CXX_FLAGS " /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION")
15-
string(APPEND CMAKE_EXE_LINKER_FLAGS " /fsanitize=address")
14+
list(APPEND SANITIZER_COMPILE_OPTIONS /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION)
1615
else()
1716
message(FATAL_ERROR "❌ Sanitizer not supported by MSVC: ${USE_SANITIZER}. It only supports 'address'.")
1817
endif()
1918
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
2019
if(USE_SANITIZER MATCHES "address")
21-
string(APPEND CMAKE_CXX_FLAGS " /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION")
22-
string(APPEND CMAKE_EXE_LINKER_FLAGS " /fsanitize=address")
20+
list(APPEND SANITIZER_COMPILE_OPTIONS /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION)
21+
list(APPEND SANITIZER_LINK_LIBRARIES clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64)
2322
else()
2423
message(FATAL_ERROR "❌ Sanitizer not supported by Clang-MSVC: ${USE_SANITIZER}. It only supports 'address'.")
2524
endif()
2625
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
2726
foreach(sanitizer ${USE_SANITIZER})
28-
if(NOT ${sanitizer} IN_LIST AVAILABLE_SANITIZERS)
29-
message(FATAL_ERROR "❌ Sanitizer not supported: ${sanitizer}. It should be one of: ${AVAILABLE_SANITIZERS}.")
27+
if(NOT ${sanitizer} IN_LIST AVALAIBLE_SANITIZERS)
28+
message(FATAL_ERROR "❌ Sanitizer not supported: ${sanitizer}. It should be one of: ${AVALAIBLE_SANITIZERS}.")
3029
endif()
31-
string(APPEND CMAKE_CXX_FLAGS " -fsanitize=${sanitizer}")
32-
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fsanitize=${sanitizer}")
33-
if(${sanitizer} MATCHES "memory")
34-
string(APPEND CMAKE_CXX_FLAGS " -fsanitize-memory-track-origins -fPIE -pie")
35-
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fsanitize-memory-track-origins -fPIE -pie")
30+
list(APPEND SANITIZER_COMPILE_OPTIONS -fsanitize=${sanitizer})
31+
list(APPEND SANITIZER_LINK_OPTIONS -fsanitize=${sanitizer})
32+
if (${sanitizer} MATCHES "memory")
33+
list(APPEND SANITIZER_LINK_LIBRARIES -fsanitize-memory-track-origins -fPIE -pie)
34+
list(APPEND SANITIZER_LINK_OPTIONS -fsanitize-memory-track-origins -fPIE -pie)
3635
endif()
3736
endforeach()
38-
string(APPEND CMAKE_CXX_FLAGS " -fno-omit-frame-pointer")
37+
list(APPEND SANITIZER_COMPILE_OPTIONS -fno-omit-frame-pointer)
3938
else()
4039
message(FATAL_ERROR "❌ Sanitizer: Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}")
4140
endif()
4241

42+
list(REMOVE_DUPLICATES SANITIZER_COMPILE_OPTIONS)
43+
list(REMOVE_DUPLICATES SANITIZER_LINK_OPTIONS)
44+
list(REMOVE_DUPLICATES SANITIZER_LINK_LIBRARIES)
45+
4346
message(STATUS "🔍 Using sanitizer: ${USE_SANITIZER}")
4447
endif()

0 commit comments

Comments
 (0)