Skip to content

Commit 0cbae7b

Browse files
committed
build: add support to build with sanitization
Add support to configure the build to use sanitizers to aid in finding issues. The `DISPATCH_USE_SANIZITERS` is a cmake option that defaults to none, but can be one of `Address, Memory, MemoryWithOrigins, Undefined, Leaks, Address;Undefined`. This allows for easily building libdispatch and the tests with the sanitizers.
1 parent 291f34d commit 0cbae7b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ include(GNUInstallDirs)
2525
set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
2626

2727
include(DispatchAppleOptions)
28+
include(DispatchSanitization)
2829

2930
include(DispatchCompilerWarnings)
3031
dispatch_common_warnings()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
set(DISPATCH_USE_SANITIZER "" CACHE STRING
3+
"Define the sanitizer used to build binaries and tests.")
4+
5+
if(DISPATCH_USE_SANITIZER)
6+
# TODO(compnerd) ensure that the compiler supports these options before adding
7+
# them. At the moment, assume that this will just be used with a GNU
8+
# compatible driver and that the options are spelt correctly in light of that.
9+
add_compile_options("-fno-omit-frame-pointer")
10+
if(CMAKE_BUILD_TYPE MATCHES "Debug")
11+
add_compile_options("-O1")
12+
elseif(NOT CMAKE_BUILD_TYPE MATCHES "Debug" AND
13+
NOT CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
14+
add_compile_options("-gline-tables-only")
15+
endif()
16+
17+
if(LLVM_USE_SANITIZER STREQUAL "Address")
18+
add_compile_options("-fsanitize=address")
19+
elseif(DISPATCH_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
20+
add_compile_options("-fsanitize=memory")
21+
if(DISPATCH_USE_SANITIZER STREQUAL "MemoryWithOrigins")
22+
add_compile_options("-fsanitize-memory-track-origins")
23+
endif()
24+
elseif(DISPATCH_USE_SANITIZER STREQUAL "Undefined")
25+
add_compile_options("-fsanitize=undefined")
26+
add_compile_options("-fno-sanitize=vptr,function")
27+
add_compile_options("-fno-sanitize-recover=all")
28+
elseif(DISPATCH_USE_SANITIZER STREQUAL "Thread")
29+
add_compile_options("-fsanitize=thread")
30+
elseif(DISPATCH_USE_SANITIZER STREQUAL "Address;Undefined" OR
31+
DISPATCH_USE_SANITIZER STREQUAL "Undefined;Address")
32+
add_compile_options("-fsanitize=address,undefined")
33+
add_compile_options("-fno-sanitize=vptr,function")
34+
add_compile_options("-fno-sanitize-recover=all")
35+
elseif(DISPATCH_USE_SANITIZER STREQUAL "Leaks")
36+
add_compile_options("-fsanitize=leak")
37+
else()
38+
message(FATAL_ERROR "unsupported value of DISPATCH_USE_SANITIZER: ${DISPATCH_USE_SANITIZER}")
39+
endif()
40+
endif()

0 commit comments

Comments
 (0)