include(CheckCSourceCompiles)
include(CheckCSourceRuns)
include(CheckCCompilerFlag)
+include(CheckLinkerFlag)
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(CTest)
# Add multi-choice option
set(WITH_SANITIZER OFF CACHE STRING "Enable sanitizer support")
-set_property(CACHE WITH_SANITIZER PROPERTY STRINGS "OFF" "Memory" "Address" "Undefined" "Thread")
+set_property(CACHE WITH_SANITIZER PROPERTY STRINGS "OFF" "memory" "address" "undefined" "thread")
if(BASEARCH_ARM_FOUND)
cmake_dependent_option(WITH_ARMV6 "Build with ARMv6 SIMD" ON ARCH_32BIT OFF)
#
# Check whether a sanitizer was requested
#
-if(WITH_SANITIZER STREQUAL "Address")
+message(STATUS "Checking for sanitizer option")
+
+string(TOLOWER "${WITH_SANITIZER}" WITH_SANITIZER)
+if(NOT WITH_SANITIZER)
+ message(STATUS " sanitizer not enabled")
+elseif(WITH_SANITIZER STREQUAL "address")
add_address_sanitizer()
-elseif(WITH_SANITIZER STREQUAL "Memory")
+elseif(WITH_SANITIZER STREQUAL "memory")
add_memory_sanitizer()
-elseif(WITH_SANITIZER STREQUAL "Thread")
+elseif(WITH_SANITIZER STREQUAL "thread")
add_thread_sanitizer()
-elseif(WITH_SANITIZER STREQUAL "Undefined")
+elseif(WITH_SANITIZER STREQUAL "undefined")
add_undefined_sanitizer()
+else()
+ message(FATAL_ERROR "Unknown WITH_SANITIZER option: '${WITH_SANITIZER}'\n"
+ "WITH_SANITIZER must be one of: 'memory', 'address', 'undefined', 'thread'")
endif()
#
check_sanitizer_support("${known_checks}" supported_checks)
if(NOT ${supported_checks} STREQUAL "")
message(STATUS "Address sanitizer is enabled: ${supported_checks}")
- add_compile_options(-fsanitize=${supported_checks})
- add_link_options(-fsanitize=${supported_checks})
+ if(NOT MSVC)
+ add_compile_options("-fsanitize=${supported_checks}")
+ add_link_options("-fsanitize=${supported_checks}")
+ else()
+ add_compile_options("/fsanitize=${supported_checks}" "/Zi")
+ # Note that the MSVC linker doesn't use /fsanitizer
+ endif()
add_common_sanitizer_flags()
else()
- message(STATUS "Address sanitizer is not supported")
+ message(FATAL_ERROR "Address sanitizer is not supported")
endif()
if(CMAKE_CROSSCOMPILING_EMULATOR)
add_link_options(-fsanitize=${supported_checks})
add_common_sanitizer_flags()
else()
+ # The Microsoft C compiler doesn't support Leak detector,
+ # so don't make this an error that disables ASAN completely
message(STATUS "Leak sanitizer is not supported")
endif()
endif()
add_link_options(-fsanitize-memory-track-origins)
endif()
else()
- message(STATUS "Memory sanitizer is not supported")
+ message(FATAL_ERROR "Memory sanitizer is not supported")
endif()
endmacro()
add_link_options(-fsanitize=${supported_checks})
add_common_sanitizer_flags()
else()
- message(STATUS "Thread sanitizer is not supported")
+ message(FATAL_ERROR "Thread sanitizer is not supported")
endif()
endmacro()
add_common_sanitizer_flags()
else()
- message(STATUS "Undefined behavior sanitizer is not supported")
+ message(FATAL_ERROR "Undefined behavior sanitizer is not supported")
endif()
-endmacro()
+endmacro()
\ No newline at end of file
endif()
# Google test requires MSAN instrumented LLVM C++ libraries
- if(WITH_SANITIZER STREQUAL "Memory")
+ if(WITH_SANITIZER STREQUAL "memory")
if(NOT DEFINED ENV{LLVM_BUILD_DIR})
message(FATAL_ERROR "MSAN instrumented C++ libraries required!")
endif()
target_compile_options(gtest_zlib PRIVATE -D_XOPEN_SOURCE=700)
endif()
- if(WITH_SANITIZER STREQUAL "Memory")
+ if(WITH_SANITIZER STREQUAL "memory")
target_link_directories(gtest_zlib PRIVATE $ENV{LLVM_BUILD_DIR}/lib)
target_link_options(gtest_zlib PRIVATE
-stdlib=libc++