Skip to content

Commit 552285f

Browse files
committed
cmake: Add SANITIZERS option
1 parent 6f9ac5e commit 552285f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,37 @@ endif()
200200
include(AddThreadsIfNeeded)
201201
add_threads_if_needed()
202202

203+
if(SANITIZERS)
204+
# First check if the compiler accepts flags. If an incompatible pair like
205+
# -fsanitize=address,thread is used here, this check will fail. This will also
206+
# fail if a bad argument is passed, e.g. -fsanitize=undfeined
207+
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET core
208+
RESULT_VAR cxx_supports_sanitizers
209+
)
210+
if(NOT cxx_supports_sanitizers)
211+
message(FATAL_ERROR "Compiler did not accept requested flags.")
212+
endif()
213+
214+
# Some compilers (e.g. GCC) require additional libraries like libasan,
215+
# libtsan, libubsan, etc. Make sure linking still works with the sanitize
216+
# flag. This is a separate check so we can give a better error message when
217+
# the sanitize flags are supported by the compiler but the actual sanitizer
218+
# libs are missing.
219+
try_append_linker_flag("-fsanitize=${SANITIZERS}" TARGET core
220+
SOURCE "
221+
#include <cstdint>
222+
#include <cstddef>
223+
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
224+
__attribute__((weak)) // allow for libFuzzer linking
225+
int main() { return 0; }
226+
"
227+
RESULT_VAR linker_supports_sanitizers
228+
)
229+
if(NOT linker_supports_sanitizers)
230+
message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.")
231+
endif()
232+
endif()
233+
203234
include(AddBoostIfNeeded)
204235
add_boost_if_needed()
205236

0 commit comments

Comments
 (0)