Skip to content

Commit 03bd505

Browse files
authored
CMake: Add sanitizers options (#732)
1 parent f579f88 commit 03bd505

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

prboom2/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1212
set(dsda_is_top_project TRUE)
1313
endif()
1414

15+
if(dsda_is_top_project)
16+
include(DsdaSanitiser)
17+
endif()
18+
1519
include(DsdaHelpers)
1620
if(dsda_is_top_project)
1721
dsda_set_default_build_config(RelWithDebInfo)

prboom2/cmake/DsdaSanitiser.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
include_guard()
2+
3+
include(CheckCCompilerFlag)
4+
include(CMakeDependentOption)
5+
6+
if(MSVC)
7+
check_c_compiler_flag(/fsanitize=address HAVE_ASAN)
8+
cmake_dependent_option(DSDA_ENABLE_ASAN
9+
"Enable address sanitiser"
10+
OFF "HAVE_ASAN" OFF
11+
)
12+
13+
if(DSDA_ENABLE_ASAN)
14+
string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
15+
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
16+
add_compile_options(/fsanitize=address /fsanitize-address-use-after-return)
17+
endif()
18+
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
19+
set(sanitiser_names address undefined thread)
20+
set(sanitiser_ids ASAN UBSAN TSAN)
21+
foreach(santiser_name sanitiser_id IN ZIP_LISTS sanitiser_names sanitiser_ids)
22+
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS -fsanitize=${sanitiser_name})
23+
check_c_compiler_flag(-fsanitize=${sanitiser_name} HAVE_${sanitiser_id})
24+
cmake_dependent_option(DSDA_ENABLE_${sanitiser_id}
25+
"Enable ${sanitiser_name} sanitiser"
26+
OFF "HAVE_${sanitiser_id}" OFF
27+
)
28+
list(POP_BACK CMAKE_REQUIRED_LINK_OPTIONS)
29+
endforeach()
30+
31+
if(DSDA_ENABLE_ASAN AND DSDA_ENABLE_TSAN)
32+
message(FATAL_ERROR
33+
"Invalid sanitizer combination:\n"
34+
" DSDA_ENABLE_ASAN: ${DSDA_ENABLE_ASAN}\n"
35+
" DSDA_ENABLE_UBSAN: ${DSDA_ENABLE_UBSAN}\n"
36+
" DSDA_ENABLE_TSAN: ${DSDA_ENABLE_TSAN}\n"
37+
)
38+
endif()
39+
40+
add_compile_options(
41+
-fno-omit-frame-pointer
42+
$<$<BOOL:${DSDA_ENABLE_ASAN}>:-fsanitize=address>
43+
$<$<BOOL:${DSDA_ENABLE_UBSAN}>:-fsanitize=undefined>
44+
$<$<BOOL:${DSDA_ENABLE_TSAN}>:-fsanitize=thread>
45+
)
46+
add_link_options(
47+
$<$<BOOL:${DSDA_ENABLE_ASAN}>:-fsanitize=address>
48+
$<$<BOOL:${DSDA_ENABLE_UBSAN}>:-fsanitize=undefined>
49+
$<$<BOOL:${DSDA_ENABLE_TSAN}>:-fsanitize=thread>
50+
)
51+
endif()

0 commit comments

Comments
 (0)