|
| 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