X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=CMake%2FCheckDebug.cmake;h=fb188529afed4551c3daa189a65159e1b81405e4;hb=aeac01630eed825d8a624e7fd7c107d47bb2e40c;hp=749e188b975d638f4c9a252e6d3113178587cf4a;hpb=cbec7a4b9613b8ae3807539fae3a29ed8aff4984;p=awesomized%2Flibmemcached diff --git a/CMake/CheckDebug.cmake b/CMake/CheckDebug.cmake index 749e188b..fb188529 100644 --- a/CMake/CheckDebug.cmake +++ b/CMake/CheckDebug.cmake @@ -1,18 +1,51 @@ -include(CMakePushCheckState) -include(CheckCXXCompilerFlag) -function(check_debug) - if(CMAKE_BUILD_TYPE STREQUAL "Debug") - add_compile_options(-O1) - add_compile_definitions(DEBUG=1) +function(set_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() + +macro(check_sanitizer VAR NAME LIB) + message(STATUS "Checking for sanitizer: ${NAME} (-l${LIB})") + if(${NAME} IN_LIST ${VAR} OR ${LIB} IN_LIST ${VAR}) + make_have_identifier(${LIB} HAVE) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES ${LIB}) + check_cxx_compiler_flag(-fsanitize=${NAME} ${HAVE}) + cmake_pop_check_state() + if(${HAVE}) + add_compile_definitions(${HAVE}) + add_compile_options(-fsanitize=${NAME}) + link_libraries(-fsanitize=${NAME}) + set_flag(-fsanitize-recover=${NAME} IGNORE) + message(STATUS " OK: sanitizer ${NAME}") + else() + message(STATUS " NO: not supported") + endif() + else() + message(STATUS " NO: not requested") + endif() +endmacro() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT MSVC) + add_definitions(-DDEBUG=1) + if(CMAKE_CXX_FLAGS MATCHES --coverage) + message("-- Coverage build detected!") + message("-- Skipping debug and sanitizer flag checks.") + else() + set_flag(-Og -O0) + set_flag(-ggdb -g) foreach(FLAG IN ITEMS -fno-inline -fno-omit-frame-pointer -fno-eliminate-unused-debug-types -funsafe-loop-optimizations - -g3 - -Wall -Wextra @@ -27,38 +60,16 @@ function(check_debug) -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() + set_flag(${FLAG} IGNORE) endforeach() if(ENABLE_SANITIZERS) - if(address IN_LIST ENABLE_SANITIZERS OR asan IN_LIST ENABLE_SANITIZERS) - cmake_push_check_state(RESET) - set(CMAKE_REQUIRED_LIBRARIES asan) - check_cxx_compiler_flag(-fsanitize=address HAVE_ASAN) - cmake_pop_check_state() - if(HAVE_ASAN) - add_compile_options(-fsanitize=address) - add_link_options(-lasan) - endif() - endif() - - if(undefined IN_LIST ENABLE_SANITIZERS OR ubsan IN_LIST ENABLE_SANITIZERS) - cmake_push_check_state(RESET) - set(CMAKE_REQUIRED_LIBRARIES ubsan) - check_cxx_compiler_flag(-fsanitize=undefined HAVE_UBSAN) - cmake_pop_check_state() - if(HAVE_UBSAN) - add_compile_options(-fsanitize=undefined) - add_link_options(-lubsan) - endif() - endif() + check_sanitizer(ENABLE_SANITIZERS address asan) + check_sanitizer(ENABLE_SANITIZERS undefined ubsan) + check_sanitizer(ENABLE_SANITIZERS thread tsan) + check_sanitizer(ENABLE_SANITIZERS leak lsan) endif() - else() - add_compile_definitions(DEBUG=0) endif() -endfunction() +else() + add_definitions(-DDEBUG=0) +endif()