1 get_cmake_property(ENABLED_LANGUAGES ENABLED_LANGUAGES)
2 include(CheckCXXCompilerFlag)
3 include(CheckCCompilerFlag)
5 function(list_append var)
7 list(FIND ${var} ${arg} index)
9 list(APPEND ${var} ${arg})
12 set(${var} ${${var}} PARENT_SCOPE)
17 list_append(SANITIZE_CC ${arg})
18 list_append(SANITIZE_XX ${arg})
22 macro(sanitize_flag_cx)
24 string(REPLACE "+" "x" var ${arg})
25 string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
26 list(FIND ENABLED_LANGUAGES C index)
27 if(${index} GREATER -1)
28 check_c_compiler_flag(${arg} cc${var})
30 list_append(SANITIZE_CC ${arg})
33 list(FIND ENABLED_LANGUAGES CXX index)
34 if(${index} GREATER -1)
35 check_cxx_compiler_flag(${arg} xx${var})
37 list_append(SANITIZE_XX ${arg})
45 macro(sanitize_flag_ld)
47 string(REPLACE "+" "x" var ${arg})
48 string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
49 list(FIND ENABLED_LANGUAGES CXX index)
50 if(${index} GREATER -1)
51 check_cxx_compiler_flag(${arg} ld${var})
53 list_append(SANITIZE_LD ${arg})
56 list(FIND ENABLED_LANGUAGES C index)
57 if(${index} GREATER -1)
58 check_c_compiler_flag(${arg} ld${var})
60 list_append(SANITIZE_LD ${arg})
69 CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR
70 CMAKE_C_COMPILER_ID MATCHES "IntelLLVM" OR
71 CMAKE_C_COMPILER_ID MATCHES "GNU" OR
72 CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR
73 CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM" OR
74 CMAKE_CXX_COMPILER_ID MATCHES "GNU"
76 sanitize_flag(-fsanitize=address)
77 sanitize_flag_cx(-fsanitize=undefined)
78 sanitize_flag_cx(-fno-omit-frame-pointer)
79 sanitize_flag_cx(-fsanitize-recover=address)
81 CMAKE_C_COMPILER_ID MATCHES "Apple[Cc]lang" OR
82 CMAKE_CXX_COMPILER_ID MATCHES "Apple[Cc]lang"
84 sanitize_flag_cx(-fsanitize=leak)
87 CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR
88 CMAKE_C_COMPILER_ID MATCHES "IntelLLVM" OR
89 CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR
90 CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM"
92 sanitize_flag_ld(-static-libsan)
95 CMAKE_C_COMPILER_ID MATCHES "MSVC" OR
96 CMAKE_CXX_COMPILER_ID MATCHES "MSVC"
98 sanitize_flag_cx(/fsanitize=address)
101 function(target_compile_sanitize_3_3)
102 foreach(target ${ARGN})
104 target_compile_options(${target} ${scope} ${SANITIZE_CC} ${SANITIZE_XX})
109 function(target_compile_sanitize)
111 if(CMAKE_VERSION VERSION_LESS 3.3)
112 target_compile_sanitize_3_3(${ARGN})
115 foreach(target ${ARGN})
116 if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
120 target_compile_options(${target} ${scope}
121 $<$<COMPILE_LANGUAGE:C>:${SANITIZE_CC}>
122 $<$<COMPILE_LANGUAGE:CXX>:${SANITIZE_XX}>
127 function(target_link_sanitize_3_13)
128 foreach(target ${ARGN})
130 get_property(LINK_FLAGS TARGET ${target} PROPERTY LINK_FLAGS)
131 string_append(LINK_FLAGS ${SANITIZE_CC} ${SANITIZE_XX} ${SANITIZE_LD})
132 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${LINK_FLAGS}")
137 function(target_link_sanitize)
141 if(CMAKE_VERSION VERSION_LESS 3.13)
142 target_link_sanitize_3_13(${ARGN})
146 foreach(target ${ARGN})
147 if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
151 target_link_options(${target} ${scope}
152 $<$<COMPILE_LANGUAGE:C>:${SANITIZE_CC}>
153 $<$<COMPILE_LANGUAGE:CXX>:${SANITIZE_XX}>