Skip to content

Commit

Permalink
Merge pull request #255 from FeignClaims/fix/disable_gcc_sanitizer_on…
Browse files Browse the repository at this point in the history
…_macos_by_default
  • Loading branch information
aminya authored Apr 16, 2024
2 parents 72832ca + 6f25c7f commit 4a8145d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
compiler: "gcc"
cmake: true
vcvarsall: true
- os: "macos-13"
compiler: "gcc"
cmake: true
vcvarsall: true
exclude:
# fails with an internal error
- os: "macos-12"
Expand Down Expand Up @@ -80,7 +84,7 @@ jobs:
cppcheck: true
clangtidy: true
task: true
doxygen: ${{ !contains(matrix.os, 'macos-11') }}
doxygen: ${{ !contains(matrix.os, 'macos-11') && !contains(matrix.os, 'macos-13') }}

- name: Test
if: ${{ !cancelled() }}
Expand Down
24 changes: 15 additions & 9 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,20 +103,24 @@ macro(dynamic_project_options)
)
endif()

if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*")
AND NOT WIN32
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((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*")
AND WIN32
)
set(SUPPORTS_ASAN OFF)
if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
set(SUPPORTS_UBSAN ON)
else()
set(SUPPORTS_ASAN ON)
set(SUPPORTS_UBSAN OFF)
endif()

# ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators
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
9 changes: 9 additions & 0 deletions src/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ function(detect_architecture arch)
endif()
endfunction()

function(detect_macos_version version)
find_program(SW_VERS_EXECUTABLE sw_vers)
execute_process(
COMMAND "${SW_VERS_EXECUTABLE}" -productVersion
OUTPUT_VARIABLE MACOS_VERSION
)
set(${version} "${MACOS_VERSION}" PARENT_SCOPE)
endfunction()

# convert semicolons in generator expression to $<SEMICOLON>
function(convert_genex_semicolons genex output)
set(result)
Expand Down
32 changes: 26 additions & 6 deletions tests/install/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ run_conan()

project(anotherproj VERSION 0.1.0 LANGUAGES CXX C)

option(FEATURE_TESTS "Enable the tests" ON)
if(FEATURE_TESTS)
# Enable sanitizers and static analyzers when running the tests
check_sanitizers_support(
ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_UNDEFINED_BEHAVIOR ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY
)
endif()

set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "ENABLE_INTERPROCEDURAL_OPTIMIZATION")
if(APPLE)
detect_macos_version(apple_version)
if(apple_version VERSION_GREATER_EQUAL 13)
# workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991
# although fixed, this problem still exists in github actions
add_link_options(-Wl,-ld_classic)
set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "")
endif()
endif()

# Initialize project_options
project_options(
ENABLE_CACHE
Expand All @@ -23,14 +43,14 @@ project_options(
DOXYGEN_THEME
"${CMAKE_CURRENT_LIST_DIR}/css/my_custom_theme.css"
"${CMAKE_CURRENT_LIST_DIR}/css/my_custom_theme_extra.css"
ENABLE_INTERPROCEDURAL_OPTIMIZATION
${ENABLE_INTERPROCEDURAL_OPTIMIZATION}
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
# ENABLE_SANITIZER_ADDRESS
# ENABLE_SANITIZER_LEAK
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
${ENABLE_SANITIZER_ADDRESS}
# ${ENABLE_SANITIZER_LEAK}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
# ${ENABLE_SANITIZER_THREAD}
# ${ENABLE_SANITIZER_MEMORY}
)

# add src, tests, etc here:
Expand Down
15 changes: 13 additions & 2 deletions tests/myproj/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ set(PCH_HEADERS
<string_view>
)

option(FEATURE_TESTS "Enable the tests" OFF)
option(FEATURE_TESTS "Enable the tests" ON)

if(FEATURE_TESTS)
# Enable sanitizers and static analyzers when running the tests
Expand All @@ -49,6 +49,17 @@ if(NOT MSVC)
#message(STATUS "Detect Linker: ${LINKER}")
endif()

set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "ENABLE_INTERPROCEDURAL_OPTIMIZATION")
if(APPLE)
detect_macos_version(apple_version)
if(apple_version VERSION_GREATER_EQUAL 13)
# workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991
# although fixed, this problem still exists in github actions
add_link_options(-Wl,-ld_classic)
set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "")
endif()
endif()

# Initialize project_options
# uncomment the options to enable them
project_options(
Expand All @@ -65,7 +76,7 @@ project_options(
# PCH_HEADERS
# ${PCH_HEADERS}
ENABLE_DOXYGEN
ENABLE_INTERPROCEDURAL_OPTIMIZATION
${ENABLE_INTERPROCEDURAL_OPTIMIZATION}
ENABLE_NATIVE_OPTIMIZATION
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
Expand Down

0 comments on commit 4a8145d

Please sign in to comment.