function(enable_sanitizers project_name)
    set(COMPILER_COMPATIBLE_WITH_SANITIZER FALSE)
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
        set(COMPILER_COMPATIBLE_WITH_SANITIZER TRUE)
    endif ()

    set(SANITIZERS "")

    option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" OFF)
    if (ENABLE_SANITIZER_ADDRESS)
        list(APPEND SANITIZERS "address")
    endif ()

    option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" OFF)
    if (ENABLE_SANITIZER_MEMORY)
        list(APPEND SANITIZERS "memory")
    endif ()

    option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" OFF)
    if (ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
        list(APPEND SANITIZERS "undefined")
    endif ()

    option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" OFF)
    if (ENABLE_SANITIZER_THREAD)
        list(APPEND SANITIZERS "thread")
    endif ()

    list(JOIN SANITIZERS "," LIST_OF_SANITIZERS)

    if (NOT "${LIST_OF_SANITIZERS}" STREQUAL "")
        if (COMPILER_COMPATIBLE_WITH_SANITIZER)
            target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
            target_link_libraries(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
            message(STATUS "Sanitizers enabled : ${LIST_OF_SANITIZERS}")
        else ()
            message(SEND_ERROR "Sanitizers requested but compiler is not compatible.")
        endif ()
    else ()
        message(STATUS "No sanitizers enabled.")
    endif ()
endfunction()
