Skip to content

Commit cadfd9f

Browse files
committed
fuzzing cmake target
1 parent b5c603e commit cadfd9f

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,25 @@ if (NOT MONGOCRYPT_CRYPTO STREQUAL none)
588588
endif ()
589589
endif ()
590590

591+
option (ENABLE_FUZZING "Build fuzzing targets. Requires a compiler supporting -fsanitize=fuzzer." OFF)
592+
if (ENABLE_FUZZING)
593+
include (CheckCCompilerFlag)
594+
set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
595+
check_c_compiler_flag ("-fsanitize=fuzzer-no-link" COMPILER_SUPPORTS_FUZZER)
596+
unset (CMAKE_TRY_COMPILE_TARGET_TYPE)
597+
if (NOT COMPILER_SUPPORTS_FUZZER)
598+
message (FATAL_ERROR
599+
"ENABLE_FUZZING is ON but the compiler does not support -fsanitize=fuzzer. "
600+
)
601+
endif ()
602+
603+
add_executable (fuzz_mongocrypt EXCLUDE_FROM_ALL test/fuzz_mongocrypt.c)
604+
target_include_directories (fuzz_mongocrypt PRIVATE ./src)
605+
target_link_libraries (fuzz_mongocrypt PRIVATE mongocrypt_static _mongocrypt::libbson_for_static)
606+
target_compile_options (fuzz_mongocrypt PRIVATE -fsanitize=fuzzer)
607+
target_link_options (fuzz_mongocrypt PRIVATE -fsanitize=fuzzer)
608+
endif ()
609+
591610
if (ENABLE_STATIC)
592611
set (TARGETS_TO_INSTALL mongocrypt mongocrypt_static)
593612
else ()

0 commit comments

Comments
 (0)