|
1 | | -include(CheckCXXCompilerFlag) |
2 | 1 | include(CheckCXXSourceRuns) |
3 | 2 |
|
4 | 3 | set(ALL_SAN_FLAGS "") |
5 | 4 |
|
6 | 5 | # No sanitizers when cross compiling to prevent stuff like this: https://github.com/whoshuu/cpr/issues/582 |
7 | 6 | if(NOT CMAKE_CROSSCOMPILING) |
8 | 7 | # Thread sanitizer |
9 | | - set(THREAD_SAN_FLAGS "-fsanitize=thread") |
10 | | - set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
11 | | - set(CMAKE_REQUIRED_FLAGS "${THREAD_SAN_FLAGS}") |
12 | | - check_cxx_source_runs("int main() { return 0; }" THREAD_SANITIZER_AVAILABLE) |
13 | | - set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
14 | | - # Do not add the ThreadSanitizer for builds with all sanitizers enabled because it is incompatible with other sanitizers. |
| 8 | + if(CPR_DEBUG_SANITIZER_FLAG_THREAD) |
| 9 | + set(THREAD_SAN_FLAGS "-fsanitize=thread") |
| 10 | + set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
| 11 | + set(CMAKE_REQUIRED_FLAGS "${THREAD_SAN_FLAGS}") |
| 12 | + check_cxx_source_runs("int main() { return 0; }" THREAD_SANITIZER_AVAILABLE_AND_ENABLED) |
| 13 | + set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
| 14 | + # Do not add the ThreadSanitizer for builds with all sanitizers enabled because it is incompatible with other sanitizers. |
| 15 | + endif() |
15 | 16 |
|
16 | 17 | # Address sanitizer |
17 | | - set(ADDR_SAN_FLAGS "-fsanitize=address") |
18 | | - set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
19 | | - set(CMAKE_REQUIRED_FLAGS "${ADDR_SAN_FLAGS}") |
20 | | - check_cxx_source_runs("int main() { return 0; }" ADDRESS_SANITIZER_AVAILABLE) |
21 | | - set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
22 | | - if(ADDRESS_SANITIZER_AVAILABLE) |
23 | | - set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${ADDR_SAN_FLAGS}") |
| 18 | + if(CPR_DEBUG_SANITIZER_FLAG_ADDR) |
| 19 | + set(ADDR_SAN_FLAGS "-fsanitize=address") |
| 20 | + set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
| 21 | + set(CMAKE_REQUIRED_FLAGS "${ADDR_SAN_FLAGS}") |
| 22 | + check_cxx_source_runs("int main() { return 0; }" ADDRESS_SANITIZER_AVAILABLE_AND_ENABLED) |
| 23 | + set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
| 24 | + if(ADDRESS_SANITIZER_AVAILABLE_AND_ENABLED) |
| 25 | + set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${ADDR_SAN_FLAGS}") |
| 26 | + endif() |
24 | 27 | endif() |
25 | 28 |
|
26 | 29 | # Leak sanitizer |
27 | | - set(LEAK_SAN_FLAGS "-fsanitize=leak") |
28 | | - check_cxx_compiler_flag(${LEAK_SAN_FLAGS} LEAK_SANITIZER_AVAILABLE) |
29 | | - if(LEAK_SANITIZER_AVAILABLE) |
30 | | - set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${LEAK_SAN_FLAGS}") |
| 30 | + if(CPR_DEBUG_SANITIZER_FLAG_LEAK) |
| 31 | + set(LEAK_SAN_FLAGS "-fsanitize=leak") |
| 32 | + set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
| 33 | + set(CMAKE_REQUIRED_FLAGS "${LEAK_SAN_FLAGS}") |
| 34 | + check_cxx_source_runs("int main() { return 0; }" LEAK_SANITIZER_AVAILABLE_AND_ENABLED) |
| 35 | + set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
| 36 | + if(LEAK_SANITIZER_AVAILABLE_AND_ENABLED) |
| 37 | + set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${LEAK_SAN_FLAGS}") |
| 38 | + endif() |
31 | 39 | endif() |
32 | 40 |
|
33 | 41 | # Undefined behavior sanitizer |
34 | | - set(UDEF_SAN_FLAGS "-fsanitize=undefined") |
35 | | - check_cxx_compiler_flag(${UDEF_SAN_FLAGS} UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) |
36 | | - if(UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) |
37 | | - set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${UDEF_SAN_FLAGS}") |
| 42 | + if(CPR_DEBUG_SANITIZER_FLAG_UB) |
| 43 | + set(UDEF_SAN_FLAGS "-fsanitize=undefined") |
| 44 | + set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
| 45 | + set(CMAKE_REQUIRED_FLAGS "${UDEF_SAN_FLAGS}") |
| 46 | + check_cxx_source_runs("int main() { return 0; }" UNDEFINED_BEHAVIOR_SANITIZER_AVAILABLE_AND_ENABLED) |
| 47 | + set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
| 48 | + if(UNDEFINED_BEHAVIOR_SANITIZER_AVAILABLE_AND_ENABLED) |
| 49 | + set(ALL_SAN_FLAGS "${ALL_SAN_FLAGS} ${UDEF_SAN_FLAGS}") |
| 50 | + endif() |
38 | 51 | endif() |
39 | 52 |
|
40 | 53 | # All sanitizer (without thread sanitizer) |
41 | | - if(NOT ALL_SAN_FLAGS STREQUAL "") |
| 54 | + if(CPR_DEBUG_SANITIZER_FLAG_ALL AND NOT ALL_SAN_FLAGS STREQUAL "") |
42 | 55 | set(PREV_FLAG ${CMAKE_REQUIRED_FLAGS}) |
43 | 56 | set(CMAKE_REQUIRED_FLAGS "${ALL_SAN_FLAGS}") |
44 | | - check_cxx_source_runs("int main() { return 0; }" ALL_SANITIZERS_AVAILABLE) |
| 57 | + check_cxx_source_runs("int main() { return 0; }" ALL_SANITIZERS_AVAILABLE_AND_ENABLED) |
45 | 58 | set(CMAKE_REQUIRED_FLAGS ${PREV_FLAG}) |
46 | 59 | endif() |
47 | 60 |
|
48 | | - if(CPR_DEBUG_SANITIZER_FLAG_THREAD AND THREAD_SANITIZER_AVAILABLE) |
| 61 | + if(THREAD_SANITIZER_AVAILABLE_AND_ENABLED) |
49 | 62 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during thread sanitizer builds." FORCE) |
50 | 63 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${THREAD_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during thread sanitizer builds." FORCE) |
51 | 64 | set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during thread sanitizer builds" FORCE) |
52 | | - elseif(CPR_DEBUG_SANITIZER_FLAG_ADDR AND ADDRESS_SANITIZER_AVAILABLE) |
| 65 | + elseif(ADDRESS_SANITIZER_AVAILABLE_AND_ENABLED) |
53 | 66 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during address sanitizer builds." FORCE) |
54 | 67 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ADDR_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during address sanitizer builds." FORCE) |
55 | 68 | set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during address sanitizer builds" FORCE) |
56 | | - elseif(CPR_DEBUG_SANITIZER_FLAG_LEAK AND LEAK_SANITIZER_AVAILABLE) |
| 69 | + elseif(LEAK_SANITIZER_AVAILABLE_AND_ENABLED) |
57 | 70 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C compiler during leak sanitizer builds." FORCE) |
58 | 71 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${LEAK_SAN_FLAGS} -fno-omit-frame-pointer" CACHE INTERNAL "Flags used by the C++ compiler during leak sanitizer builds." FORCE) |
59 | 72 | set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during leak sanitizer builds" FORCE) |
60 | | - elseif(CPR_DEBUG_SANITIZER_FLAG_UB AND UNDEFINED_BEHAVIOUR_SANITIZER_AVAILABLE) |
61 | | - set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during undefined behaviour sanitizer builds." FORCE) |
62 | | - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during undefined behaviour sanitizer builds." FORCE) |
63 | | - set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behaviour sanitizer builds" FORCE) |
64 | | - elseif(CPR_DEBUG_SANITIZER_FLAG_ALL AND ALL_SANITIZERS_AVAILABLE) |
| 73 | + elseif(UNDEFINED_BEHAVIOR_SANITIZER_AVAILABLE_AND_ENABLED) |
| 74 | + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C compiler during undefined behavior sanitizer builds." FORCE) |
| 75 | + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${UDEF_SAN_FLAGS}" CACHE INTERNAL "Flags used by the C++ compiler during undefined behavior sanitizer builds." FORCE) |
| 76 | + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during undefined behavior sanitizer builds" FORCE) |
| 77 | + elseif(ALL_SANITIZERS_AVAILABLE_AND_ENABLED) |
65 | 78 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C compiler during most possible sanitizer builds." FORCE) |
66 | 79 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ALL_SAN_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls" CACHE INTERNAL "Flags used by the C++ compiler during most possible sanitizer builds." FORCE) |
67 | 80 | set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Flags used for the linker during most possible sanitizer builds" FORCE) |
|
0 commit comments