|
20 | 20 | # Copyright (C) 2018 Scylladb, Ltd.
|
21 | 21 | #
|
22 | 22 |
|
23 |
| -include (CheckCXXSourceCompiles) |
24 |
| - |
25 |
| -set (CMAKE_REQUIRED_FLAGS -fsanitize=address) |
26 |
| -check_cxx_source_compiles ("int main() {}" Sanitizers_ADDRESS_FOUND) |
27 |
| - |
28 |
| -if (Sanitizers_ADDRESS_FOUND) |
29 |
| - set (Sanitizers_ADDRESS_COMPILER_OPTIONS -fsanitize=address) |
30 |
| -endif () |
| 23 | +foreach (component ${Sanitizers_FIND_COMPONENTS}) |
| 24 | + string (TOUPPER ${component} COMPONENT) |
| 25 | + set (compile_options "Sanitizers_${COMPONENT}_COMPILE_OPTIONS") |
| 26 | + if (component STREQUAL "address") |
| 27 | + list (APPEND ${compile_options} -fsanitize=address) |
| 28 | + elseif (component STREQUAL "undefined_behavior") |
| 29 | + list (APPEND ${compile_options} -fsanitize=undefined) |
| 30 | + # Disable vptr because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88684 |
| 31 | + list (APPEND ${compile_options} -fno-sanitize=vptr) |
| 32 | + else () |
| 33 | + message (FATAL_ERROR "Unsupported sanitizer: ${component}") |
| 34 | + endif () |
| 35 | + list(APPEND Sanitizers_COMPILE_OPTIONS "${${compile_options}}") |
| 36 | +endforeach () |
31 | 37 |
|
32 |
| -set (CMAKE_REQUIRED_FLAGS -fsanitize=undefined) |
33 |
| -check_cxx_source_compiles ("int main() {}" Sanitizers_UNDEFINED_BEHAVIOR_FOUND) |
| 38 | +include(CheckCXXSourceCompiles) |
| 39 | +include(CMakePushCheckState) |
34 | 40 |
|
35 |
| -if (Sanitizers_UNDEFINED_BEHAVIOR_FOUND) |
36 |
| - # Disable vptr because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88684 |
37 |
| - set (Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS "-fsanitize=undefined;-fno-sanitize=vptr") |
| 41 | +# -fsanitize=address cannot be combined with -fsanitize=thread, so let's test |
| 42 | +# the combination of the compiler options. |
| 43 | +cmake_push_check_state() |
| 44 | +string (REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${Sanitizers_COMPILE_OPTIONS}") |
| 45 | +set(CMAKE_REQUIRED_FLAGS ${Sanitizers_COMPILE_OPTIONS}) |
| 46 | +check_cxx_source_compiles("int main() {}" |
| 47 | + Sanitizers_SUPPORTED) |
| 48 | +if (Sanitizers_SUPPORTED) |
| 49 | + if ("address" IN_LIST Sanitizers_FIND_COMPONENTS) |
| 50 | + file (READ ${CMAKE_CURRENT_LIST_DIR}/code_tests/Sanitizers_fiber_test.cc _sanitizers_fiber_test_code) |
| 51 | + check_cxx_source_compiles ("${_sanitizers_fiber_test_code}" |
| 52 | + Sanitizers_FIBER_SUPPORT) |
| 53 | + endif () |
38 | 54 | endif ()
|
39 |
| - |
40 |
| -set (Sanitizers_COMPILER_OPTIONS |
41 |
| - ${Sanitizers_ADDRESS_COMPILER_OPTIONS} |
42 |
| - ${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS}) |
43 |
| - |
44 |
| -file (READ ${CMAKE_CURRENT_LIST_DIR}/code_tests/Sanitizers_fiber_test.cc _sanitizers_fiber_test_code) |
45 |
| -set (CMAKE_REQUIRED_FLAGS ${Sanitizers_COMPILER_OPTIONS}) |
46 |
| -check_cxx_source_compiles ("${_sanitizers_fiber_test_code}" Sanitizers_FIBER_SUPPORT) |
| 55 | +cmake_pop_check_state() |
47 | 56 |
|
48 | 57 | include (FindPackageHandleStandardArgs)
|
49 | 58 |
|
50 | 59 | find_package_handle_standard_args (Sanitizers
|
51 | 60 | REQUIRED_VARS
|
52 |
| - Sanitizers_ADDRESS_COMPILER_OPTIONS |
53 |
| - Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS) |
| 61 | + Sanitizers_COMPILE_OPTIONS |
| 62 | + Sanitizers_SUPPORTED) |
54 | 63 |
|
55 | 64 | if (Sanitizers_FOUND)
|
56 |
| - if (NOT (TARGET Sanitizers::address)) |
57 |
| - add_library (Sanitizers::address INTERFACE IMPORTED) |
58 |
| - |
59 |
| - set_target_properties (Sanitizers::address |
60 |
| - PROPERTIES |
61 |
| - INTERFACE_COMPILE_OPTIONS ${Sanitizers_ADDRESS_COMPILER_OPTIONS} |
62 |
| - INTERFACE_LINK_LIBRARIES ${Sanitizers_ADDRESS_COMPILER_OPTIONS}) |
63 |
| - endif () |
64 |
| - |
65 |
| - if (NOT (TARGET Sanitizers::undefined_behavior)) |
66 |
| - add_library (Sanitizers::undefined_behavior INTERFACE IMPORTED) |
67 |
| - |
68 |
| - set_target_properties (Sanitizers::undefined_behavior |
69 |
| - PROPERTIES |
70 |
| - INTERFACE_COMPILE_OPTIONS "${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS}" |
71 |
| - INTERFACE_LINK_LIBRARIES "${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS}") |
72 |
| - endif () |
| 65 | + foreach (component ${Sanitizers_FIND_COMPONENTS}) |
| 66 | + string (TOUPPER ${component} COMPONENT) |
| 67 | + set (library Sanitizers::${component}) |
| 68 | + if (NOT TARGET ${library}) |
| 69 | + add_library (${library} INTERFACE IMPORTED) |
| 70 | + set_target_properties (${library} |
| 71 | + PROPERTIES |
| 72 | + INTERFACE_COMPILE_OPTIONS "${Sanitizers_${COMPONENT}_COMPILE_OPTIONS}" |
| 73 | + INTERFACE_LINK_LIBRARIES "${Sanitizers_${COMPONENT}_COMPILE_OPTIONS}") |
| 74 | + endif () |
| 75 | + endforeach () |
73 | 76 | endif ()
|
0 commit comments