github-actions
[awesomized/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 check_flag(-Og -O0)
18 check_flag(-ggdb -g)
19 add_definitions(-DDEBUG=1)
20 foreach(FLAG IN ITEMS
21 -fno-inline
22 -fno-omit-frame-pointer
23 -fno-eliminate-unused-debug-types
24 -funsafe-loop-optimizations
25
26 -Wall
27 -Wextra
28
29 -Wdouble-promotion
30 -Wduplicated-cond
31 -Wduplicated-branches
32 -Wformat=2
33 -Wlogical-op
34 -Wnull-dereference
35 -Wrestrict
36 -Wshadow
37 -Wunknown-pragmas
38 -Wunsafe-loop-optimizations
39 )
40 check_flag(${FLAG} IGNORE)
41 endforeach()
42
43 if(ENABLE_SANITIZERS)
44 if(address IN_LIST ENABLE_SANITIZERS OR asan IN_LIST ENABLE_SANITIZERS)
45 cmake_push_check_state(RESET)
46 set(CMAKE_REQUIRED_LIBRARIES asan)
47 check_cxx_compiler_flag(-fsanitize=address HAVE_ASAN)
48 cmake_pop_check_state()
49 if(HAVE_ASAN)
50 add_compile_definitions(HAVE_ASAN)
51 add_compile_options(-fsanitize=address)
52 link_libraries(-fsanitize=address)
53 check_flag(-fsanitize-recover=address IGNORE)
54 endif()
55 endif()
56
57 if(undefined IN_LIST ENABLE_SANITIZERS OR ubsan IN_LIST ENABLE_SANITIZERS)
58 cmake_push_check_state(RESET)
59 set(CMAKE_REQUIRED_LIBRARIES ubsan)
60 check_cxx_compiler_flag(-fsanitize=undefined HAVE_UBSAN)
61 cmake_pop_check_state()
62 if(HAVE_UBSAN)
63 add_compile_definitions(HAVE_UBSAN)
64 add_compile_options(-fsanitize=undefined)
65 link_libraries(-fsanitize=undefined)
66 check_flag(-fsanitize-recover=undefined IGNORE)
67 endif()
68 endif()
69 endif()
70 else()
71 add_definitions(-DDEBUG=0)
72 endif()
73 endfunction()