include_guard() include(CheckCXXSourceCompiles) include(CMakePushCheckState) function(target_enable_sanitizers _target) if(MSVC) set(_compile_options /fsanitize=address) set(_link_options /INCREMENTAL:NO) # Use the same settings for this target and the capability check. set_property(TARGET ${_target} PROPERTY MSVC_RUNTIME_CHECKS "") set_property(TARGET ${_target} PROPERTY MSVC_DEBUG_INFORMATION_FORMAT ProgramDatabase) set(CMAKE_MSVC_RUNTIME_CHECKS "") set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT ProgramDatabase) elseif(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$") set(_compile_options -fsanitize=address,undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer) set(_link_options -fsanitize=address,undefined) else() message(FATAL_ERROR "Sanitizers are not configured for ${CMAKE_CXX_COMPILER_ID}") endif() cmake_push_check_state(RESET) string(JOIN " " CMAKE_REQUIRED_FLAGS ${_compile_options}) set(CMAKE_REQUIRED_LINK_OPTIONS ${_link_options}) set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) check_cxx_source_compiles("int main() { return 0; }" REPO_LENS_SANITIZERS_SUPPORTED) cmake_pop_check_state() if(NOT REPO_LENS_SANITIZERS_SUPPORTED) message(FATAL_ERROR "The requested sanitizer configuration cannot compile and link. Check compiler/runtime installation or configure with ENABLE_SANITIZERS=OFF.") endif() target_compile_options(${_target} PRIVATE ${_compile_options}) target_link_options(${_target} PRIVATE ${_link_options}) endfunction()