glove/cmake/Sanitizers.cmake
2026-07-19 21:06:11 -04:00

41 lines
1.5 KiB
CMake

include_guard(GLOBAL)
# glove_apply_sanitizers(<target>)
# Apply sanitizer flags from GLOVE_SANITIZER to <target>.
function(glove_apply_sanitizers target)
if(GLOVE_SANITIZER STREQUAL "none" OR NOT GLOVE_SANITIZER)
return()
endif()
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
message(WARNING "GLOVE_SANITIZER=${GLOVE_SANITIZER} ignored: unsupported compiler")
return()
endif()
set(flags "")
if(GLOVE_SANITIZER STREQUAL "address")
list(APPEND flags -fsanitize=address)
elseif(GLOVE_SANITIZER STREQUAL "address+undefined")
list(APPEND flags -fsanitize=address,undefined -fno-sanitize-recover=all)
elseif(GLOVE_SANITIZER STREQUAL "thread")
list(APPEND flags -fsanitize=thread)
elseif(GLOVE_SANITIZER STREQUAL "memory")
if(APPLE)
message(FATAL_ERROR "MemorySanitizer is not supported on macOS / Apple clang")
endif()
list(APPEND flags -fsanitize=memory -fsanitize-memory-track-origins=2)
else()
message(FATAL_ERROR "Unknown GLOVE_SANITIZER=${GLOVE_SANITIZER}")
endif()
list(APPEND flags -fno-omit-frame-pointer -g)
get_target_property(target_type ${target} TYPE)
if(target_type STREQUAL "INTERFACE_LIBRARY")
target_compile_options(${target} INTERFACE ${flags})
target_link_options(${target} INTERFACE ${flags})
else()
target_compile_options(${target} PRIVATE ${flags})
target_link_options(${target} PRIVATE ${flags})
endif()
endfunction()