|
| 1 | +function(enable_sanitizers project_name) |
| 2 | + |
| 3 | + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") |
| 4 | + option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" OFF) |
| 5 | + |
| 6 | + if(ENABLE_COVERAGE) |
| 7 | + target_compile_options(${project_name} INTERFACE --coverage -O0 -g -fno-omit-frame-pointer) |
| 8 | + target_link_libraries(${project_name} INTERFACE --coverage) |
| 9 | + endif() |
| 10 | + |
| 11 | + set(SANITIZERS "") |
| 12 | + |
| 13 | + option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" OFF) |
| 14 | + if(ENABLE_SANITIZER_ADDRESS) |
| 15 | + list(APPEND SANITIZERS "address") |
| 16 | + endif() |
| 17 | + |
| 18 | + option(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" OFF) |
| 19 | + if(ENABLE_SANITIZER_LEAK) |
| 20 | + list(APPEND SANITIZERS "leak") |
| 21 | + endif() |
| 22 | + |
| 23 | + option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" OFF) |
| 24 | + if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) |
| 25 | + list(APPEND SANITIZERS "undefined") |
| 26 | + endif() |
| 27 | + |
| 28 | + option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" OFF) |
| 29 | + if(ENABLE_SANITIZER_THREAD) |
| 30 | + if("address" IN_LIST SANITIZERS OR "leak" IN_LIST SANITIZERS) |
| 31 | + message(WARNING "Thread sanitizer does not work with Address and Leak sanitizer enabled") |
| 32 | + else() |
| 33 | + list(APPEND SANITIZERS "thread") |
| 34 | + endif() |
| 35 | + endif() |
| 36 | + |
| 37 | + option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" OFF) |
| 38 | + if(ENABLE_SANITIZER_MEMORY AND CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") |
| 39 | + message(WARNING "Memory sanitizer requires all the code (including libc++) \ |
| 40 | + to be MSan-instrumented otherwise it reports false positives") |
| 41 | + if("address" IN_LIST SANITIZERS |
| 42 | + OR "thread" IN_LIST SANITIZERS |
| 43 | + OR "leak" IN_LIST SANITIZERS) |
| 44 | + message(WARNING "Memory sanitizer does not work with Address, Thread and Leak sanitizer enabled") |
| 45 | + else() |
| 46 | + list(APPEND SANITIZERS "memory") |
| 47 | + endif() |
| 48 | + endif() |
| 49 | + |
| 50 | + list( |
| 51 | + JOIN |
| 52 | + SANITIZERS |
| 53 | + "," |
| 54 | + LIST_OF_SANITIZERS) |
| 55 | + |
| 56 | + endif() |
| 57 | + |
| 58 | + if(LIST_OF_SANITIZERS) |
| 59 | + if(NOT |
| 60 | + "${LIST_OF_SANITIZERS}" |
| 61 | + STREQUAL |
| 62 | + "") |
| 63 | + target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) |
| 64 | + target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) |
| 65 | + endif() |
| 66 | + endif() |
| 67 | + |
| 68 | +endfunction() |
0 commit comments