Commit bf3382d5448d4cb0487cc69510b6184d44e7eafc

Patrick Steinhardt 2018-07-19T15:22:18

cmake: remove need to add "-fsanitize=fuzzer" flag for fuzzers Right now, users are being instrucded to add the "-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=fuzzer" flag when they want to build our fuzzers. This is error-prone and user unfriendly. Instead, just add the flag to our fuzzers' build instructions so that it happens automatically. Adjust the README accordingly.

diff --git a/docs/fuzzing.md b/docs/fuzzing.md
index 03b1aff..9d32f87 100644
--- a/docs/fuzzing.md
+++ b/docs/fuzzing.md
@@ -19,8 +19,7 @@ automated fuzz testing. libFuzzer only works with clang.
    and [`leak`/`address,leak`](https://clang.llvm.org/docs/LeakSanitizer.html).
 3. Create the cmake build environment and configure the build with the
    sanitizer chosen: `CC=/usr/bin/clang-6.0 cmake
-   -DBUILD_CLAR=OFF -DBUILD_FUZZERS=ON -DUSE_SANIZER=address
-   -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=fuzzer"
+   -DBUILD_CLAR=OFF -DBUILD_FUZZERS=ON -DUSE_SANITIZER=address
    -DCMAKE_BUILD_TYPE=RelWithDebInfo ..`. Note that building the fuzzer targets
    is incompatible with the tests and examples.
 4. Build libgit2: `cmake --build .`
@@ -58,10 +57,9 @@ variable).
 ## Standalone mode
 
 In order to ensure that there are no regresions, each fuzzer target can be run
-in a standalone mode. This can be done by passing `-DUSE_STANDALONE_FUZZERS=ON`
-to `cmake` without setting `-DCMAKE_EXE_LINKER_FLAGS`. This makes it compatible
-with gcc. This does not use the fuzzing engine, but just invokes every file in
-the chosen corpus.
+in a standalone mode. This can be done by passing `-DUSE_STANDALONE_FUZZERS=ON`.
+This makes it compatible with gcc. This does not use the fuzzing engine, but
+just invokes every file in the chosen corpus.
 
 In order to get full coverage, though, you might want to also enable one of the
 sanitizers. You might need a recent version of clang to get full support.
diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt
index 13705fa..ea06123 100644
--- a/fuzzers/CMakeLists.txt
+++ b/fuzzers/CMakeLists.txt
@@ -1,6 +1,10 @@
 LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
 
+IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
+	ADD_C_FLAG(-fsanitize=fuzzer)
+ENDIF ()
+
 FILE(GLOB SRC_FUZZ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *_fuzzer.c)
 FOREACH(fuzz_target_src ${SRC_FUZZ})
 	STRING(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src})