@@ -263,6 +263,41 @@ target_link_libraries(core_interface INTERFACE
263
263
Threads::Threads
264
264
)
265
265
266
+ add_library (sanitize_interface INTERFACE )
267
+ target_link_libraries (core_interface INTERFACE sanitize_interface )
268
+ if (SANITIZERS )
269
+ # First check if the compiler accepts flags. If an incompatible pair like
270
+ # -fsanitize=address,thread is used here, this check will fail. This will also
271
+ # fail if a bad argument is passed, e.g. -fsanitize=undfeined
272
+ try_append_cxx_flags ("-fsanitize=${SANITIZERS} " TARGET sanitize_interface
273
+ RESULT_VAR cxx_supports_sanitizers
274
+ SKIP_LINK
275
+ )
276
+ if (NOT cxx_supports_sanitizers )
277
+ message (FATAL_ERROR "Compiler did not accept requested flags." )
278
+ endif ()
279
+
280
+ # Some compilers (e.g. GCC) require additional libraries like libasan,
281
+ # libtsan, libubsan, etc. Make sure linking still works with the sanitize
282
+ # flag. This is a separate check so we can give a better error message when
283
+ # the sanitize flags are supported by the compiler but the actual sanitizer
284
+ # libs are missing.
285
+ try_append_linker_flag ("-fsanitize=${SANITIZERS} " VAR SANITIZER_LDFLAGS
286
+ SOURCE "
287
+ #include <cstdint>
288
+ #include <cstddef>
289
+ extern \" C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
290
+ __attribute__((weak)) // allow for libFuzzer linking
291
+ int main() { return 0; }
292
+ "
293
+ RESULT_VAR linker_supports_sanitizers
294
+ )
295
+ if (NOT linker_supports_sanitizers )
296
+ message (FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries." )
297
+ endif ()
298
+ endif ()
299
+ target_link_options (sanitize_interface INTERFACE ${SANITIZER_LDFLAGS} )
300
+
266
301
include (AddBoostIfNeeded )
267
302
add_boost_if_needed ()
268
303
0 commit comments