e3e046bf8c7be78accb56c79c458c294f4d84bbf
[m6w6/libmemcached] / CMake / CheckDebug.cmake
1 include(CMakePushCheckState)
2 include(CheckCXXCompilerFlag)
3
4 function(check_flag FLAG DEFAULT)
5 unset(FLAG_CONSTANT)
6 string(MAKE_C_IDENTIFIER CXX${FLAG} FLAG_CONSTANT)
7 check_cxx_compiler_flag(${FLAG} ${FLAG_CONSTANT})
8 if(${FLAG_CONSTANT})
9 add_compile_options(${FLAG})
10 elseif(DEFAULT)
11 add_compile_options(${DEFAULT})
12 endif()
13 endfunction()
14
15 function(check_debug)
16 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
17 add_definitions(-DDEBUG=1)
18 if(CMAKE_CXX_FLAGS MATCHES --coverage)
19 message("-- Coverage build detected!")
20 message("-- Skipping debug and sanitizer flag checks.")
21 else()
22 check_flag(-Og -O0)
23 check_flag(-ggdb -g)
24 foreach(FLAG IN ITEMS
25 -fno-inline
26 -fno-omit-frame-pointer
27 -fno-eliminate-unused-debug-types
28 -funsafe-loop-optimizations
29
30 -Wall
31 -Wextra
32
33 -Wdouble-promotion
34 -Wduplicated-cond
35 -Wduplicated-branches
36 -Wformat=2
37 -Wlogical-op
38 -Wnull-dereference
39 -Wrestrict
40 -Wshadow
41 -Wunknown-pragmas
42 -Wunsafe-loop-optimizations
43 )
44 check_flag(${FLAG} IGNORE)
45 endforeach()
46
47 if(ENABLE_SANITIZERS)
48 if(address IN_LIST ENABLE_SANITIZERS OR asan IN_LIST ENABLE_SANITIZERS)
49 cmake_push_check_state(RESET)
50 set(CMAKE_REQUIRED_LIBRARIES asan)
51 check_cxx_compiler_flag(-fsanitize=address HAVE_ASAN)
52 cmake_pop_check_state()
53 if(HAVE_ASAN)
54 add_compile_definitions(HAVE_ASAN)
55 add_compile_options(-fsanitize=address)
56 link_libraries(-fsanitize=address)
57 check_flag(-fsanitize-recover=address IGNORE)
58 endif()
59 endif()
60
61 if(undefined IN_LIST ENABLE_SANITIZERS OR ubsan IN_LIST ENABLE_SANITIZERS)
62 cmake_push_check_state(RESET)
63 set(CMAKE_REQUIRED_LIBRARIES ubsan)
64 check_cxx_compiler_flag(-fsanitize=undefined HAVE_UBSAN)
65 cmake_pop_check_state()
66 if(HAVE_UBSAN)
67 add_compile_definitions(HAVE_UBSAN)
68 add_compile_options(-fsanitize=undefined)
69 link_libraries(-fsanitize=undefined)
70 check_flag(-fsanitize-recover=undefined IGNORE)
71 endif()
72 endif()
73 endif()
74 endif()
75 else()
76 add_definitions(-DDEBUG=0)
77 endif()
78 endfunction()