Skip to content

Commit

Permalink
Make sanitizer enabling consistent
Browse files Browse the repository at this point in the history
- Make sanitizer enabling in dynamic_project_options consistent with
  detect_sanitizers_support.
- detect MacOS OS version to keep gcc sanitizers enable on x86_64 macOS Monterey or older.
  • Loading branch information
FeignClaims committed Apr 16, 2024
1 parent 1916999 commit 6f25c7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/DynamicProjectOptions.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include_guard()

include("${CMAKE_CURRENT_LIST_DIR}/Sanitizers.cmake")

#[[.rst:
``dynamic_project_options``
Expand Down Expand Up @@ -101,16 +103,26 @@ macro(dynamic_project_options)
)
endif()

if(((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*") AND (NOT WIN32))
OR ((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND (NOT WIN32) AND (NOT APPLE))
check_sanitizers_support(
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
)
set(SUPPORTS_UBSAN ON)

if(ENABLE_SANITIZER_ADDRESS)
set(SUPPORTS_ASAN ON)
else()
set(SUPPORTS_UBSAN OFF)
set(SUPPORTS_ASAN OFF)
endif()

if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
set(SUPPORTS_UBSAN ON)
else()
set(SUPPORTS_UBSAN OFF)
endif()

# ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators
# note that it is possible to use Ninja with cl, so this still allows clang-tidy on Windows
# with CL.
Expand Down
29 changes: 24 additions & 5 deletions src/Sanitizers.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include_guard()

include("${CMAKE_CURRENT_LIST_DIR}/Utilities.cmake")

# Enable the sanitizers for the given project
function(
enable_sanitizers
Expand Down Expand Up @@ -133,11 +135,28 @@ function(
)
set(SANITIZERS "")
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
list(APPEND SANITIZERS "address")
list(APPEND SANITIZERS "undefined")
list(APPEND SANITIZERS "leak")
list(APPEND SANITIZERS "thread")
list(APPEND SANITIZERS "memory")
set(HAS_SANITIZER_SUPPORT ON)

# Disable gcc sanitizer on some macos according to https://github.com/orgs/Homebrew/discussions/3384#discussioncomment-6264292
if((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND APPLE)
detect_macos_version(MACOS_VERSION)
if(MACOS_VERSION VERSION_GREATER_EQUAL 13)
set(HAS_SANITIZER_SUPPORT OFF)
endif()

detect_architecture(ARCHITECTURE)
if(ARCHITECTURE STREQUAL "arm64")
set(HAS_SANITIZER_SUPPORT OFF)
endif()
endif()

if (HAS_SANITIZER_SUPPORT)
list(APPEND SANITIZERS "address")
list(APPEND SANITIZERS "undefined")
list(APPEND SANITIZERS "leak")
list(APPEND SANITIZERS "thread")
list(APPEND SANITIZERS "memory")
endif()
elseif(MSVC)
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
Expand Down

0 comments on commit 6f25c7f

Please sign in to comment.