-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
memcheck.cmake
101 lines (86 loc) · 2.96 KB
/
memcheck.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# run like:
#
# ctest -S memcheck.cmake
#
# optionally, tell path to memory checker like:
#
# ctest -DMEMCHECK_ROOT=/path/to/bin/valgrind -S memcheck.cmake
cmake_minimum_required(VERSION 3.19)
list(APPEND opts -DCMAKE_BUILD_TYPE=Debug)
if(NOT DEFINED CTEST_MEMORYCHECK_TYPE)
set(CTEST_MEMORYCHECK_TYPE "Valgrind")
endif()
if(CTEST_MEMORYCHECK_TYPE STREQUAL "Valgrind")
# https://www.cprogramming.com/debugging/valgrind.html
find_program(CTEST_MEMORYCHECK_COMMAND NAMES valgrind HINTS ${MEMCHECK_ROOT} REQUIRED)
set(CTEST_MEMORYCHECK_COMMAND_OPTIONS --leak-check=full)
set(supp ${CMAKE_CURRENT_LIST_DIR}/valgrind.supp)
if(EXISTS ${supp})
list(APPEND CTEST_MEMORYCHECK_COMMAND_OPTIONS --suppressions=${supp})
endif()
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "DrMemory")
find_program(CTEST_MEMORYCHECK_COMMAND NAMES drmemory HINTS ${MEMCHECK_ROOT} REQUIRED)
set(CTEST_MEMORYCHECK_COMMAND_OPTIONS -light -count_leaks)
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "AddressSanitizer")
set(check_flags -fsanitize=address)
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "LeakSanitizer")
set(check_flags -fsanitize=leak)
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "MemorySanitizer")
set(check_flags -fsanitize=memory)
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "ThreadSanitizer")
set(check_flags -fsanitize=thread)
elseif(CTEST_MEMORYCHECK_TYPE STREQUAL "UndefinedBehaviorSanitizer")
set(check_flags -fsanitize=undefined)
else()
message(FATAL_ERROR "Unknown memory checker type: ${CTEST_MEMORYCHECK_TYPE}")
endif()
if(check_flags)
list(APPEND opts
-DCMAKE_C_FLAGS_DEBUG=${check_flags}
-DCMAKE_CXX_FLAGS_DEBUG=${check_flags}
-DCMAKE_EXE_LINKER_FLAGS_INIT=${check_flags}
)
endif()
set(CTEST_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
set(CTEST_BINARY_DIRECTORY ${CTEST_SOURCE_DIRECTORY}/build-${CTEST_MEMORYCHECK_TYPE})
set(CTEST_BUILD_CONFIGURATION Debug)
if(DEFINED ENV{CMAKE_GENERATOR})
set(CTEST_CMAKE_GENERATOR $ENV{CMAKE_GENERATOR})
elseif(WIN32)
set(CTEST_CMAKE_GENERATOR "MinGW Makefiles")
else()
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
endif()
message(STATUS "Checker ${CTEST_MEMORYCHECK_TYPE}: ${CTEST_MEMORYCHECK_COMMAND}")
string(REPLACE ";" " " CTEST_MEMORYCHECK_COMMAND_OPTIONS "${CTEST_MEMORYCHECK_COMMAND_OPTIONS}")
ctest_start(Experimental)
ctest_configure(
OPTIONS "${opts}"
RETURN_VALUE ret
CAPTURE_CMAKE_ERROR err
)
if(NOT (ret EQUAL 0 AND err EQUAL 0))
message(FATAL_ERROR "CMake configure failed: ${ret} ${err}")
endif()
ctest_build(
RETURN_VALUE ret
CAPTURE_CMAKE_ERROR err
)
if(NOT (ret EQUAL 0 AND err EQUAL 0))
message(FATAL_ERROR "CMake build failed: ${ret} ${err}")
endif()
ctest_memcheck(
INCLUDE ${include}
INCLUDE_LABEL ${include_label}
EXCLUDE ${exclude}
EXCLUDE_LABEL ${exclude_label}
RETURN_VALUE ret
CAPTURE_CMAKE_ERROR err
DEFECT_COUNT count
)
if(NOT (ret EQUAL 0 AND err EQUAL 0))
message(FATAL_ERROR "Memory check failed: ${ret} ${err}")
endif()
if(NOT count EQUAL 0)
message(FATAL_ERROR "Memory check found ${count} defects")
endif()