set(USE_SANITIZER
	"NONE"
	CACHE STRING "Build with sanitizer active"
)

set_property(CACHE USE_SANITIZER PROPERTY STRINGS "NONE" "ASAN" "MSAN" "UBSAN" "TSAN")

if(USE_SANITIZER STREQUAL "ASAN")
	if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
		set(ACTIVE_SANITIZER_FLAGS "-fsanitize=address -g -O0")
	elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
		set(ACTIVE_SANITIZER_FLAGS "/fsanitize=address")
	endif()
elseif(USE_SANITIZER STREQUAL "MSAN")
	set(ACTIVE_SANITIZER_FLAGS "-fsanitize=memory -O0")
elseif(USE_SANITIZER STREQUAL "UBSAN")
	set(ACTIVE_SANITIZER_FLAGS "-fsanitize=undefined -O0")
elseif(USE_SANITIZER STREQUAL "TSAN")
	set(ACTIVE_SANITIZER_FLAGS "-fsanitize=thread -O0")
endif()

if(ACTIVE_SANITIZER_FLAGS)
	set(CMAKE_CXX_FLAGS
		${CMAKE_CXX_FLAGS} ${ACTIVE_SANITIZER_FLAGS}
		CACHE STRING "" FORCE
	)
endif()
