# Sanitizer flags

sanitize_address {
    QMAKE_CFLAGS   += $$QMAKE_SANITIZE_ADDRESS_CFLAGS
    QMAKE_CXXFLAGS += $$QMAKE_SANITIZE_ADDRESS_CXXFLAGS
    QMAKE_LFLAGS   += $$QMAKE_SANITIZE_ADDRESS_LFLAGS
    android {
        # ARM 32 (armeabi-v7a & arm5) are not supported because Qt must be rebuilt with -marm
        equals(ANDROID_TARGET_ARCH, arm64-v8a): ANDROID_LIBCLANG_RT_FILE = "libclang_rt.asan-aarch64-android.so"
        else: equals(ANDROID_TARGET_ARCH, x86): ANDROID_LIBCLANG_RT_FILE = "libclang_rt.asan-i686-android.so"
        else: equals(ANDROID_TARGET_ARCH, x86_64): ANDROID_LIBCLANG_RT_FILE = "libclang_rt.asan-x86_64-android.so"
        else: error("ASAN: Unsupported platform $${ANDROID_TARGET_ARCH}")

        ANDROID_LIBCLANG_RT_PATH = $${NDK_LLVM_PATH}/lib64/clang
        ANDROID_CLANG_RT_VERSIONS = $$files($$ANDROID_LIBCLANG_RT_PATH/*)
        for (VERSION, ANDROID_CLANG_RT_VERSIONS) {
            greaterThan(VERSION, $$ANDROID_LIBCLANG_RT_PATH): ANDROID_LIBCLANG_RT_PATH = $$VERSION
        }
        ANDROID_LIBCLANG_RT_PATH = "$${ANDROID_LIBCLANG_RT_PATH}/lib/linux/"
        ANDROID_WRAP_SH_CONTENT  = "$$LITERAL_HASH!/system/bin/sh"
        ANDROID_WRAP_SH_CONTENT += "HERE=\"$(cd \"$(dirname \"$0\")\" && pwd)\""
        isEmpty(ANDROID_ASAN_OPTIONS): ANDROID_ASAN_OPTIONS = "log_to_syslog=false,allow_user_segv_handler=1"
        ANDROID_WRAP_SH_CONTENT += "export ASAN_OPTIONS=$${ANDROID_ASAN_OPTIONS}"
        ANDROID_WRAP_SH_CONTENT += "export LD_PRELOAD=$HERE/$${ANDROID_LIBCLANG_RT_FILE}"
        ANDROID_WRAP_SH_CONTENT += "exec \"$@\""
        write_file($$OUT_PWD/android-build/resources/lib/$${ANDROID_TARGET_ARCH}/wrap.sh, ANDROID_WRAP_SH_CONTENT) | error()
        libclang_rt.path = /libs/$$ANDROID_TARGET_ARCH/
        libclang_rt.files = "$${ANDROID_LIBCLANG_RT_PATH}/$${ANDROID_LIBCLANG_RT_FILE}"
        INSTALLS += libclang_rt
    }
}

sanitize_memory {
    QMAKE_CFLAGS   += $$QMAKE_SANITIZE_MEMORY_CFLAGS
    QMAKE_CXXFLAGS += $$QMAKE_SANITIZE_MEMORY_CXXFLAGS
    QMAKE_LFLAGS   += $$QMAKE_SANITIZE_MEMORY_LFLAGS
}

sanitize_thread {
    QMAKE_CFLAGS   += $$QMAKE_SANITIZE_THREAD_CFLAGS
    QMAKE_CXXFLAGS += $$QMAKE_SANITIZE_THREAD_CXXFLAGS
    QMAKE_LFLAGS   += $$QMAKE_SANITIZE_THREAD_LFLAGS
}

sanitize_fuzzer_no_link {
    QMAKE_CFLAGS   += $$QMAKE_SANITIZE_FUZZERNL_CFLAGS
    QMAKE_CXXFLAGS += $$QMAKE_SANITIZE_FUZZERNL_CXXFLAGS
    QMAKE_LFLAGS   += $$QMAKE_SANITIZE_FUZZERNL_LFLAGS
}

sanitize_undefined {
    QMAKE_CFLAGS   += $$QMAKE_SANITIZE_UNDEFINED_CFLAGS
    QMAKE_CXXFLAGS += $$QMAKE_SANITIZE_UNDEFINED_CXXFLAGS
    QMAKE_LFLAGS   += $$QMAKE_SANITIZE_UNDEFINED_LFLAGS
}

QMAKE_CFLAGS   += $$QMAKE_COMMON_SANITIZE_CFLAGS
QMAKE_CXXFLAGS += $$QMAKE_COMMON_SANITIZE_CXXFLAGS

