Skip to content

Commit 354e266

Browse files
committed
Add sanitizer flags to cmake
1 parent 9bf7a0d commit 354e266

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

CMakeLists.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ option(UMF_BUILD_BENCHMARKS "Build UMF benchmarks" OFF)
2626
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
2727
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors" OFF)
2828
option(UMF_FORMAT_CODE_STYLE "Format UMF code with clang-format" OFF)
29+
option(USE_ASAN "Enable AddressSanitizer checks" OFF)
30+
option(USE_UBSAN "Enable UndefinedBehaviorSanitizer checks" OFF)
2931

3032
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
3133
set(LINUX TRUE)
@@ -57,6 +59,14 @@ if(MSVC)
5759
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
5860
endif()
5961

62+
# Sanitizer flags
63+
if(USE_ASAN)
64+
add_sanitizer_flag(address)
65+
endif()
66+
if(USE_UBSAN)
67+
add_sanitizer_flag(undefined)
68+
endif()
69+
6070
# A header only library to specify include directories in transitive
6171
# dependencies.
6272
add_library(umf_headers INTERFACE)
@@ -94,7 +104,7 @@ endif()
94104
if(UMF_FORMAT_CODE_STYLE)
95105
find_program(CLANG_FORMAT NAMES clang-format-15 clang-format-15.0 clang-format)
96106

97-
if(CLANG_FORMAT)
107+
if(CLANG_FORMAT)
98108
get_program_version_major_minor(${CLANG_FORMAT} CLANG_FORMAT_VERSION)
99109
message(STATUS "Found clang-format: ${CLANG_FORMAT} (version: ${CLANG_FORMAT_VERSION})")
100110

@@ -105,7 +115,7 @@ if(UMF_FORMAT_CODE_STYLE)
105115
else()
106116
message(FATAL_ERROR "UMF_FORMAT_CODE_STYLE=ON, but clang-format not found (required version: ${CLANG_FORMAT_REQUIRED})")
107117
endif()
108-
118+
109119
# Obtain files for clang-format check
110120
set(format_glob)
111121
foreach(DIR IN ITEMS include src test benchmark)
@@ -123,11 +133,11 @@ if(UMF_FORMAT_CODE_STYLE)
123133
file(GLOB_RECURSE format_list ${format_glob})
124134

125135
message(STATUS "Adding clang-format-check and clang-format-apply make targets")
126-
136+
127137
add_custom_target(clang-format-check
128138
COMMAND ${CLANG_FORMAT}
129-
--style=file
130-
--dry-run
139+
--style=file
140+
--dry-run
131141
-Werror
132142
${format_list}
133143
COMMENT "Check files formatting using clang-format")
@@ -137,7 +147,7 @@ if(UMF_FORMAT_CODE_STYLE)
137147
--style=file
138148
--i
139149
${format_list}
140-
COMMENT "Format files using clang-format")
150+
COMMENT "Format files using clang-format")
141151
endif()
142152

143153
# Add license to the installation path

cmake/helpers.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
# helpers.cmake -- helper functions for top-level CMakeLists.txt
77
#
88

9+
# CMake modules that check whether the C/C++ compiler supports a given flag
10+
include(CheckCCompilerFlag)
11+
include(CheckCXXCompilerFlag)
12+
913
# Sets ${ret} to version of program specified by ${name} in major.minor format
1014
function(get_program_version_major_minor name ret)
1115
execute_process(COMMAND ${name} --version
@@ -104,3 +108,31 @@ function(add_umf_library)
104108
add_umf_target_compile_options(${ARG_NAME})
105109
add_umf_target_link_options(${ARG_NAME})
106110
endfunction()
111+
112+
# Add sanitizer ${flag}, if it is supported, for both C and C++ compiler
113+
macro(add_sanitizer_flag flag)
114+
set(SAVED_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
115+
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} -fsanitize=${flag}")
116+
117+
if(${flag} STREQUAL "address")
118+
set(check_name "HAS_ASAN")
119+
elseif(${flag} STREQUAL "undefined")
120+
set(check_name "HAS_UBSAN")
121+
endif()
122+
123+
check_c_compiler_flag("-fsanitize=${flag}" "C_${check_name}")
124+
check_cxx_compiler_flag("-fsanitize=${flag}" "CXX_${check_name}")
125+
if (${C_${check_name}} OR ${CXX_${check_name}})
126+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${flag} -fno-sanitize-recover=all")
127+
if (${C_${check_name}})
128+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${flag} -fno-sanitize-recover=all")
129+
endif()
130+
if (${CXX_${check_name}})
131+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${flag} -fno-sanitize-recover=all")
132+
endif()
133+
else()
134+
message(STATUS " ${flag} sanitizer is not supported (neither by C nor CXX compiler)")
135+
endif()
136+
137+
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
138+
endmacro()

0 commit comments

Comments
 (0)