Skip to content

Commit 81c9ee8

Browse files
committed
cmake: Add SANITIZERS option
1 parent 67ceebb commit 81c9ee8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

CMakeLists.txt

+34
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,40 @@ endif()
191191
include(AddThreadsIfNeeded)
192192
add_threads_if_needed()
193193

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

0 commit comments

Comments
 (0)