Skip to content

Commit

Permalink
Merge pull request #283 from FeignClaims/fix/sanitizer_for_dynamic_pr…
Browse files Browse the repository at this point in the history
…oject_options
  • Loading branch information
aminya authored Oct 4, 2024
2 parents 9be7709 + ac6c7bc commit bd2be71
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ if(FEATURE_TESTS)
set(ENABLE_COVERAGE "ENABLE_COVERAGE")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
endif()
# Enable doxgen for the docs
Expand All @@ -123,7 +123,7 @@ project_options(
${ENABLE_DOXYGEN}
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
# ${ENABLE_SANITIZER_THREAD}
# ${ENABLE_SANITIZER_MEMORY}
# ENABLE_SANITIZER_POINTER_COMPARE
Expand Down
4 changes: 2 additions & 2 deletions docs/src/project_options_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if(FEATURE_TESTS)
set(ENABLE_COVERAGE "ENABLE_COVERAGE")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
endif()
# Enable doxgen for the docs
Expand All @@ -76,7 +76,7 @@ project_options(
${ENABLE_DOXYGEN}
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
# ${ENABLE_SANITIZER_THREAD}
# ${ENABLE_SANITIZER_MEMORY}
# ENABLE_SANITIZER_POINTER_COMPARE
Expand Down
37 changes: 14 additions & 23 deletions src/DynamicProjectOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,18 @@ macro(dynamic_project_options)
)
endif()

check_sanitizers_support(
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
ENABLE_SANITIZER_POINTER_COMPARE
ENABLE_SANITIZER_POINTER_SUBTRACT
)

if(ENABLE_SANITIZER_ADDRESS)
set(SUPPORTS_ASAN ON)
else()
set(SUPPORTS_ASAN OFF)
endif()
# Fallback for ENABLE_SANITIZER_UNDEFINED_BEHAVIOR option
foreach(default_type IN ITEMS DEFAULT DEVELOPER_DEFAULT USER_DEFAULT)
if(DEFINED ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type})
if(DEFINED ENABLE_SANITIZER_UNDEFINED_${default_type})
message(WARNING "Don't set both ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type} and ENABLE_SANITIZER_UNDEFINED_${default_type}. Use ENABLE_SANITIZER_UNDEFINED_${default_type} only.")
else()
message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type} is deprecated. Use ENABLE_SANITIZER_UNDEFINED_${default_type} instead.")
endif()

if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
set(SUPPORTS_UBSAN ON)
else()
set(SUPPORTS_UBSAN OFF)
endif()
set(ENABLE_SANITIZER_UNDEFINED_${default_type} ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type}})
endif()
endforeach()

# 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
Expand Down Expand Up @@ -163,9 +154,9 @@ macro(dynamic_project_options)
"0\;DISABLE_RTTI\;OFF\;OFF\;Disable RTTI (no-rtti flag)"
"0\;ENABLE_BUILD_WITH_TIME_TRACE\;OFF\;OFF\;Generates report of where compile-time is spent"
"0\;ENABLE_UNITY\;OFF\;OFF\;Merge C++ files into larger C++ files, can speed up compilation sometimes"
"0\;ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)"
"0\;ENABLE_SANITIZER_ADDRESS\;OFF\;ON\;Make memory errors into hard runtime errors (windows/linux/macos)"
"0\;ENABLE_SANITIZER_LEAK\;OFF\;OFF\;Make memory leaks into hard runtime errors"
"0\;ENABLE_SANITIZER_UNDEFINED_BEHAVIOR\;OFF\;${SUPPORTS_UBSAN}\;Make certain types (numeric mostly) of undefined behavior into runtime errors"
"0\;ENABLE_SANITIZER_UNDEFINED\;OFF\;ON\;Make certain types (numeric mostly) of undefined behavior into runtime errors"
"0\;ENABLE_SANITIZER_THREAD\;OFF\;OFF\;Make thread race conditions into hard runtime errors"
"0\;ENABLE_SANITIZER_MEMORY\;OFF\;OFF\;Make other memory errors into runtime errors"
"0\;ENABLE_CONTROL_FLOW_PROTECTION\;OFF\;OFF\;Enable control flow protection instrumentation"
Expand Down Expand Up @@ -277,7 +268,7 @@ macro(dynamic_project_options)
${ENABLE_UNITY_VALUE}
${ENABLE_SANITIZER_ADDRESS_VALUE}
${ENABLE_SANITIZER_LEAK_VALUE}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_VALUE}
${ENABLE_SANITIZER_UNDEFINED_VALUE}
${ENABLE_SANITIZER_THREAD_VALUE}
${ENABLE_SANITIZER_MEMORY_VALUE}
${ENABLE_CONTROL_FLOW_PROTECTION_VALUE}
Expand Down
18 changes: 15 additions & 3 deletions src/Index.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ include("${ProjectOptions_SRC_DIR}/Vcpkg.cmake")
those.
- ``ENABLE_SANITIZER_ADDRESS``: Enable address sanitizer
- ``ENABLE_SANITIZER_LEAK``: Enable leak sanitizer
- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Enable undefined behavior
- ``ENABLE_SANITIZER_UNDEFINED``: Enable undefined behavior
sanitizer
- ``ENABLE_SANITIZER_THREAD``: Enable thread sanitizer
- ``ENABLE_SANITIZER_MEMORY``: Enable memory sanitizer
Expand Down Expand Up @@ -148,7 +148,8 @@ macro(project_options)
ENABLE_UNITY
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_UNDEFINED
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR # deprecated, ENABLE_SANITIZER_UNDEFINED
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
ENABLE_SANITIZER_POINTER_COMPARE
Expand Down Expand Up @@ -260,12 +261,23 @@ macro(project_options)
enable_coverage(${_options_target})
endif()

# Fallback for deprecated ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED)
message(WARNING "Don't switch on both ENABLE_SANITIZER_UNDEFINED_BEHAVIOR and ENABLE_SANITIZER_UNDEFINED. Use ENABLE_SANITIZER_UNDEFINED only.")
else()
message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR is deprecated. Use ENABLE_SANITIZER_UNDEFINED instead.")
endif()

set(ProjectOptions_ENABLE_SANITIZER_UNDEFINED ${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR})
endif()

# sanitizer options if supported by compiler
enable_sanitizers(
${_options_target}
${ProjectOptions_ENABLE_SANITIZER_ADDRESS}
${ProjectOptions_ENABLE_SANITIZER_LEAK}
${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ProjectOptions_ENABLE_SANITIZER_UNDEFINED}
${ProjectOptions_ENABLE_SANITIZER_THREAD}
${ProjectOptions_ENABLE_SANITIZER_MEMORY}
${ProjectOptions_ENABLE_SANITIZER_POINTER_COMPARE}
Expand Down
17 changes: 10 additions & 7 deletions src/Sanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function(
_project_name
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_UNDEFINED
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
ENABLE_SANITIZER_POINTER_COMPARE
Expand All @@ -18,7 +18,7 @@ function(
# check if the sanitizers are supported
check_sanitizers_support(
SUPPORTS_SANITIZER_ADDRESS
SUPPORTS_SANITIZER_UNDEFINED_BEHAVIOR
SUPPORTS_SANITIZER_UNDEFINED
SUPPORTS_SANITIZER_LEAK
SUPPORTS_SANITIZER_THREAD
SUPPORTS_SANITIZER_MEMORY
Expand All @@ -38,8 +38,11 @@ function(
"pointer-compare"
"pointer-subtract"
)
if(${ENABLE_SANITIZER_${SANITIZER}})
if(${SUPPORTS_SANITIZER_${SANITIZER}})
set(SANITIZER_UPPERCASE "${SANITIZER}")
string(TOUPPER ${SANITIZER} SANITIZER_UPPERCASE)

if(${ENABLE_SANITIZER_${SANITIZER_UPPERCASE}})
if(${SUPPORTS_SANITIZER_${SANITIZER_UPPERCASE}})
list(APPEND SANITIZERS ${SANITIZER})
else()
# do not enable the sanitizer if it is not supported
Expand Down Expand Up @@ -134,7 +137,7 @@ Note that some sanitizers cannot be enabled together, and this function doesn't
Output variables:
- ``ENABLE_SANITIZER_ADDRESS``: Address sanitizer is supported
- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Undefined behavior sanitizer is supported
- ``ENABLE_SANITIZER_UNDEFINED``: Undefined behavior sanitizer is supported
- ``ENABLE_SANITIZER_LEAK``: Leak sanitizer is supported
- ``ENABLE_SANITIZER_THREAD``: Thread sanitizer is supported
- ``ENABLE_SANITIZER_MEMORY``: Memory sanitizer is supported
Expand All @@ -145,7 +148,7 @@ Output variables:
.. code:: cmake
check_sanitizers_support(ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_UNDEFINED
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
Expand All @@ -158,7 +161,7 @@ Output variables:
function(
check_sanitizers_support
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
ENABLE_SANITIZER_UNDEFINED
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_THREAD
ENABLE_SANITIZER_MEMORY
Expand Down
4 changes: 2 additions & 2 deletions tests/install/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ project(anotherproj VERSION 0.1.0 LANGUAGES CXX C)
option(FEATURE_TESTS "Enable the tests" ON)
if(FEATURE_TESTS)
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
endif()

# Initialize project_options
Expand All @@ -33,7 +33,7 @@ project_options(
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
# ${ENABLE_SANITIZER_LEAK}
# ${ENABLE_SANITIZER_THREAD}
# ${ENABLE_SANITIZER_MEMORY}
Expand Down
4 changes: 2 additions & 2 deletions tests/minimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if(ENABLE_TESTING)
if(NOT DISABLE_SANITIZER)
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
Expand All @@ -64,7 +64,7 @@ project_options(
ENABLE_VS_ANALYSIS
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
# DISABLE_EXCEPTIONS
# DISABLE_RTTI
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors
Expand Down
4 changes: 2 additions & 2 deletions tests/myproj/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ option(FEATURE_TESTS "Enable the tests" ON)
if(FEATURE_TESTS)
# Enable sanitizers and static analyzers when running the tests
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
set(ENABLE_SANITIZER_POINTER_COMPARE "ENABLE_SANITIZER_POINTER_COMPARE")
set(ENABLE_SANITIZER_POINTER_SUBTRACT "ENABLE_SANITIZER_POINTER_SUBTRACT")
endif()
Expand Down Expand Up @@ -71,7 +71,7 @@ project_options(
# ENABLE_UNITY
${ENABLE_SANITIZER_ADDRESS}
# ${ENABLE_SANITIZER_LEAK}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
${ENABLE_SANITIZER_POINTER_COMPARE}
${ENABLE_SANITIZER_POINTER_SUBTRACT}
# ${ENABLE_SANITIZER_THREAD}
Expand Down
4 changes: 2 additions & 2 deletions tests/rpi3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if(ENABLE_TESTING)
if(NOT DISABLE_SANITIZER)
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
Expand All @@ -90,7 +90,7 @@ project_options(
ENABLE_VS_ANALYSIS
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
DISABLE_EXCEPTIONS
DISABLE_RTTI
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors
Expand Down
4 changes: 2 additions & 2 deletions tests/rpi4-vcpkg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if(ENABLE_TESTING)
if(NOT DISABLE_SANITIZER)
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
Expand All @@ -64,7 +64,7 @@ project_options(
ENABLE_VS_ANALYSIS
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
# DISABLE_EXCEPTIONS
# DISABLE_RTTI
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors
Expand Down
4 changes: 2 additions & 2 deletions tests/rpi4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if(ENABLE_TESTING)
if(NOT DISABLE_SANITIZER)
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
Expand All @@ -82,7 +82,7 @@ project_options(
ENABLE_VS_ANALYSIS
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
${ENABLE_SANITIZER_UNDEFINED}
DISABLE_EXCEPTIONS
DISABLE_RTTI
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors
Expand Down

0 comments on commit bd2be71

Please sign in to comment.