include(CMakePushCheckState)
include(CheckCXXCompilerFlag)
+function(check_flag FLAG DEFAULT)
+ unset(FLAG_CONSTANT)
+ string(MAKE_C_IDENTIFIER CXX${FLAG} FLAG_CONSTANT)
+ check_cxx_compiler_flag(${FLAG} ${FLAG_CONSTANT})
+ if(${FLAG_CONSTANT})
+ add_compile_options(${FLAG})
+ elseif(DEFAULT)
+ add_compile_options(${DEFAULT})
+ endif()
+endfunction()
+
function(check_debug)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
- add_compile_options(-O1)
+ check_flag(-Og -O0)
+ check_flag(-ggdb -g)
add_definitions(-DDEBUG=1)
foreach(FLAG IN ITEMS
-fno-inline
-fno-eliminate-unused-debug-types
-funsafe-loop-optimizations
- -g3
-
-Wall
-Wextra
-Wunknown-pragmas
-Wunsafe-loop-optimizations
)
- unset(FLAG_CONSTANT)
- string(MAKE_C_IDENTIFIER CXX${FLAG} FLAG_CONSTANT)
- check_cxx_compiler_flag(${FLAG} ${FLAG_CONSTANT})
- if(${FLAG_CONSTANT})
- add_compile_options(${FLAG})
- endif()
+ check_flag(${FLAG} IGNORE)
endforeach()
if(ENABLE_SANITIZERS)
check_cxx_compiler_flag(-fsanitize=address HAVE_ASAN)
cmake_pop_check_state()
if(HAVE_ASAN)
+ add_compile_definitions(HAVE_ASAN)
add_compile_options(-fsanitize=address)
- add_link_options(-lasan)
+ if(CMAKE_CXX_COMPILER_ID STREQUAL Clang)
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
+ endif()
+ link_libraries(asan)
+ check_flag(-fsanitize-recover=address IGNORE)
endif()
endif()
check_cxx_compiler_flag(-fsanitize=undefined HAVE_UBSAN)
cmake_pop_check_state()
if(HAVE_UBSAN)
+ add_compile_definitions(HAVE_UBSAN)
add_compile_options(-fsanitize=undefined)
- add_link_options(-lubsan)
+ if(CMAKE_CXX_COMPILER_ID STREQUAL Clang)
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
+ endif()
+ link_libraries(ubsan)
+ check_flag(-fsanitize-recover=undefined IGNORE)
endif()
endif()
endif()