# MIT License
#
# Copyright (c) 2014 Ivan Aksamentov <ivan.aksamentov@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# Adds CMake build types for C++ build with sanitizers

if(NOT WIN32)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    set(FOUND_GCC 1)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    set(FOUND_CLANG 1)
endif()

set(ASAN_C_FLAGS "-g -O0 -DNDEBUG=1 -fsanitize=address -fsanitize=leak -fsanitize-recover=all -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(ASAN_C_FLAGS_CLANG "")
set(ASAN_C_FLAGS_GCC "")
set(ASAN_C_FLAGS_GCC7 "-fsanitize-address-use-after-scope")
set(ASAN_EXE_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")
set(ASAN_SHARED_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")

set(MSAN_C_FLAGS "-g -O0 -DNDEBUG=1 -fsanitize=memory -fsanitize-recover=all -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(MSAN_C_FLAGS_CLANG "")
set(MSAN_C_FLAGS_GCC "")
set(MSAN_EXE_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")
set(MSAN_SHARED_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")

set(TSAN_C_FLAGS "-g -O0 -DNDEBUG=1 -fsanitize=thread -fsanitize-recover=all -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(TSAN_C_FLAGS_CLANG "")
set(TSAN_C_FLAGS_GCC "")
set(TSAN_EXE_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")
set(TSAN_SHARED_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")

set(UBSAN_C_FLAGS "-g -O0 -DNDEBUG=1 -fsanitize=undefined -fsanitize-recover=all -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(UBSAN_C_FLAGS_CLANG "")
set(UBSAN_C_FLAGS_GCC "")
set(UBSAN_EXE_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")
set(UBSAN_SHARED_LINKER_FLAGS_GCC "-static-libasan -static-libstdc++")


if(FOUND_GCC)
    set(ASAN_C_FLAGS "${ASAN_C_FLAGS} ${ASAN_C_FLAGS_GCC}")
    set(MSAN_C_FLAGS "${MSAN_C_FLAGS} ${MSAN_C_FLAGS_GCC}")
    set(TSAN_C_FLAGS "${TSAN_C_FLAGS} ${TSAN_C_FLAGS_GCC}")
    set(UBSAN_C_FLAGS "${UBSAN_C_FLAGS} ${UBSAN_C_FLAGS_GCC}")
    set(ASAN_EXE_LINKER_FLAGS "${ASAN_EXE_LINKER_FLAGS} ${ASAN_EXE_LINKER_FLAGS_GCC}")
    set(ASAN_SHARED_LINKER_FLAGS "${ASAN_SHARED_LINKER_FLAGS} ${ASAN_SHARED_LINKER_FLAGS_GCC}")
    set(MSAN_EXE_LINKER_FLAGS "${MSAN_EXE_LINKER_FLAGS} ${MSAN_EXE_LINKER_FLAGS_GCC}")
    set(MSAN_SHARED_LINKER_FLAGS "${MSAN_SHARED_LINKER_FLAGS} ${MSAN_SHARED_LINKER_FLAGS_GCC}")
    set(TSAN_EXE_LINKER_FLAGS "${TSAN_EXE_LINKER_FLAGS} ${TSAN_EXE_LINKER_FLAGS_GCC}")
    set(TSAN_SHARED_LINKER_FLAGS "${TSAN_SHARED_LINKER_FLAGS} ${TSAN_SHARED_LINKER_FLAGS_GCC}")
    set(UBSAN_EXE_LINKER_FLAGS "${UBSAN_EXE_LINKER_FLAGS} ${UBSAN_EXE_LINKER_FLAGS_GCC}")
    set(UBSAN_SHARED_LINKER_FLAGS "${UBSAN_SHARED_LINKER_FLAGS} ${UBSAN_SHARED_LINKER_FLAGS_GCC}")

    if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7)
        set(ASAN_C_FLAGS  "${ASAN_C_FLAGS} ${ASAN_C_FLAGS_GCC7}")
    endif()
elseif(FOUND_CLANG)
    set(ASAN_C_FLAGS "${ASAN_C_FLAGS} ${ASAN_C_FLAGS_CLANG}")
    set(MSAN_C_FLAGS "${MSAN_C_FLAGS} ${MSAN_C_FLAGS_CLANG}")
    set(TSAN_C_FLAGS "${TSAN_C_FLAGS} ${TSAN_C_FLAGS_CLANG}")
    set(UBSAN_C_FLAGS "${UBSAN_C_FLAGS} ${UBSAN_C_FLAGS_CLANG}")
    set(ASAN_EXE_LINKER_FLAGS "${ASAN_EXE_LINKER_FLAGS} ${ASAN_EXE_LINKER_FLAGS_CLANG}")
    set(ASAN_SHARED_LINKER_FLAGS "${ASAN_SHARED_LINKER_FLAGS} ${ASAN_SHARED_LINKER_FLAGS_CLANG}")
    set(MSAN_EXE_LINKER_FLAGS "${MSAN_EXE_LINKER_FLAGS} ${MSAN_EXE_LINKER_FLAGS_CLANG}")
    set(MSAN_SHARED_LINKER_FLAGS "${MSAN_SHARED_LINKER_FLAGS} ${MSAN_SHARED_LINKER_FLAGS_CLANG}")
    set(TSAN_EXE_LINKER_FLAGS "${TSAN_EXE_LINKER_FLAGS} ${TSAN_EXE_LINKER_FLAGS_CLANG}")
    set(TSAN_SHARED_LINKER_FLAGS "${TSAN_SHARED_LINKER_FLAGS} ${TSAN_SHARED_LINKER_FLAGS_CLANG}")
    set(UBSAN_EXE_LINKER_FLAGS "${UBSAN_EXE_LINKER_FLAGS} ${UBSAN_EXE_LINKER_FLAGS_CLANG}")
    set(UBSAN_SHARED_LINKER_FLAGS "${UBSAN_SHARED_LINKER_FLAGS} ${UBSAN_SHARED_LINKER_FLAGS_CLANG}")
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(UBSAN_C_FLAGS "${UBSAN_C_FLAGS} -fsanitize=integer,implicit-conversion,implicit-integer-truncation,implicit-integer-arithmetic-value-change,nullability")
  set(SAN_COMMON_FLAGS "-fstandalone-debug")
endif()

set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_SANITIZE} ${ASAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_C_FLAGS_MSAN "${CMAKE_C_FLAGS_SANITIZE} ${MSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_C_FLAGS_TSAN "${CMAKE_C_FLAGS_SANITIZE} ${TSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_C_FLAGS_UBSAN "${CMAKE_C_FLAGS_SANITIZE} ${UBSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")

set(CMAKE_CXX_FLAGS_ASAN "${CMAKE_CXX_FLAGS_SANITIZE} ${ASAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_MSAN "${CMAKE_CXX_FLAGS_SANITIZE} ${MSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_TSAN "${CMAKE_CXX_FLAGS_SANITIZE} ${TSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_UBSAN "${CMAKE_CXX_FLAGS_SANITIZE} ${UBSAN_C_FLAGS} ${SAN_COMMON_FLAGS}")

set(CMAKE_EXE_LINKER_FLAGS_ASAN "${CMAKE_EXE_LINKER_FLAGS_SANITIZE} ${ASAN_C_FLAGS} ${ASAN_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_MSAN "${CMAKE_EXE_LINKER_FLAGS_SANITIZE} ${MSAN_C_FLAGS} ${MSAN_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_TSAN "${CMAKE_EXE_LINKER_FLAGS_SANITIZE} ${TSAN_C_FLAGS} ${TSAN_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_UBSAN "${CMAKE_EXE_LINKER_FLAGS_SANITIZE} ${UBSAN_C_FLAGS} ${UBSAN_EXE_LINKER_FLAGS}")

set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${CMAKE_SHARED_LINKER_FLAGS_SANITIZE} ${ASAN_C_FLAGS} ${ASAN_SHARED_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_MSAN "${CMAKE_SHARED_LINKER_FLAGS_SANITIZE} ${MSAN_C_FLAGS} ${MSAN_SHARED_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_TSAN "${CMAKE_SHARED_LINKER_FLAGS_SANITIZE} ${TSAN_C_FLAGS} ${TSAN_SHARED_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_UBSAN "${CMAKE_SHARED_LINKER_FLAGS_SANITIZE} ${UBSAN_C_FLAGS} ${UBSAN_SHARED_LINKER_FLAGS}")

set(CMAKE_CONFIGURATION_TYPES
    ${CMAKE_CONFIGURATION_TYPES}
    ASAN
    MSAN
    TSAN
    UBSAN
    CACHE STRING INTERNAL FORCE
)

endif()
