@@ -33,7 +33,7 @@ function(enable_sanitizer project_name lib_name)
3333 # https://devblogs.microsoft.com/cppblog/asan-for-windows-x64-and-debug-build-support/
3434 # https://learn.microsoft.com/en-us/cpp/sanitizers/asan-runtime?view=msvc-170
3535 endif ()
36- elseif ( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
36+ elseif ( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
3737 message ("Enable sanitizer" )
3838 # https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind
3939 set (SANTIZE_COMPILE_ARGS
@@ -54,5 +54,24 @@ function(enable_sanitizer project_name lib_name)
5454 target_link_options (${project_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
5555 target_compile_options (${lib_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
5656 target_link_options (${lib_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
57+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
58+ message ("Enable sanitizer" )
59+ # https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind
60+ set (SANTIZE_COMPILE_ARGS
61+ -fsanitize=address
62+ -fsanitize=undefined
63+ -fsanitize=leak
64+ -fno-sanitize-recover=all
65+ -fsanitize=float-divide-by-zero
66+ -fsanitize=float-cast-overflow
67+ -fsanitize=alignment
68+ $<$<CONFIG:Release>:-fno-omit-frame-pointer -g>
69+ $<$<CONFIG:MinSizeRel>:-fno-omit-frame-pointer -g>
70+ $<$<CONFIG:RelWithDebInfo>:-fno-omit-frame-pointer>
71+ )
72+ target_compile_options (${project_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
73+ target_link_options (${project_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
74+ target_compile_options (${lib_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
75+ target_link_options (${lib_name} PRIVATE ${SANTIZE_COMPILE_ARGS} )
5776 endif ()
5877endfunction ()
0 commit comments