1 1.1 riastrad # =========================================================================== 2 1.1 riastrad # https://www.gnu.org/software/autoconf-archive/ax_valgrind_check.html 3 1.1 riastrad # =========================================================================== 4 1.1 riastrad # 5 1.1 riastrad # SYNOPSIS 6 1.1 riastrad # 7 1.1 riastrad # AX_VALGRIND_DFLT(memcheck|helgrind|drd|sgcheck, on|off) 8 1.1 riastrad # AX_VALGRIND_CHECK() 9 1.1 riastrad # 10 1.1 riastrad # DESCRIPTION 11 1.1 riastrad # 12 1.1 riastrad # AX_VALGRIND_CHECK checks whether Valgrind is present and, if so, allows 13 1.1 riastrad # running `make check` under a variety of Valgrind tools to check for 14 1.1 riastrad # memory and threading errors. 15 1.1 riastrad # 16 1.1 riastrad # Defines VALGRIND_CHECK_RULES which should be substituted in your 17 1.1 riastrad # Makefile; and $enable_valgrind which can be used in subsequent configure 18 1.1 riastrad # output. VALGRIND_ENABLED is defined and substituted, and corresponds to 19 1.1 riastrad # the value of the --enable-valgrind option, which defaults to being 20 1.1 riastrad # enabled if Valgrind is installed and disabled otherwise. Individual 21 1.1 riastrad # Valgrind tools can be disabled via --disable-valgrind-<tool>, the 22 1.1 riastrad # default is configurable via the AX_VALGRIND_DFLT command or is to use 23 1.1 riastrad # all commands not disabled via AX_VALGRIND_DFLT. All AX_VALGRIND_DFLT 24 1.1 riastrad # calls must be made before the call to AX_VALGRIND_CHECK. 25 1.1 riastrad # 26 1.1 riastrad # If unit tests are written using a shell script and automake's 27 1.1 riastrad # LOG_COMPILER system, the $(VALGRIND) variable can be used within the 28 1.1 riastrad # shell scripts to enable Valgrind, as described here: 29 1.1 riastrad # 30 1.1 riastrad # https://www.gnu.org/software/gnulib/manual/html_node/Running-self_002dtests-under-valgrind.html 31 1.1 riastrad # 32 1.1 riastrad # Usage example: 33 1.1 riastrad # 34 1.1 riastrad # configure.ac: 35 1.1 riastrad # 36 1.1 riastrad # AX_VALGRIND_DFLT([sgcheck], [off]) 37 1.1 riastrad # AX_VALGRIND_CHECK 38 1.1 riastrad # 39 1.1 riastrad # Makefile.am: 40 1.1 riastrad # 41 1.1 riastrad # @VALGRIND_CHECK_RULES@ 42 1.1 riastrad # VALGRIND_SUPPRESSIONS_FILES = my-project.supp 43 1.1 riastrad # EXTRA_DIST = my-project.supp 44 1.1 riastrad # 45 1.1 riastrad # This results in a "check-valgrind" rule being added to any Makefile.am 46 1.1 riastrad # which includes "@VALGRIND_CHECK_RULES@" (assuming the module has been 47 1.1 riastrad # configured with --enable-valgrind). Running `make check-valgrind` in 48 1.1 riastrad # that directory will run the module's test suite (`make check`) once for 49 1.1 riastrad # each of the available Valgrind tools (out of memcheck, helgrind and drd) 50 1.1 riastrad # while the sgcheck will be skipped unless enabled again on the 51 1.1 riastrad # commandline with --enable-valgrind-sgcheck. The results for each check 52 1.1 riastrad # will be output to test-suite-$toolname.log. The target will succeed if 53 1.1 riastrad # there are zero errors and fail otherwise. 54 1.1 riastrad # 55 1.1 riastrad # Alternatively, a "check-valgrind-$TOOL" rule will be added, for $TOOL in 56 1.1 riastrad # memcheck, helgrind, drd and sgcheck. These are useful because often only 57 1.1 riastrad # some of those tools can be ran cleanly on a codebase. 58 1.1 riastrad # 59 1.1 riastrad # The macro supports running with and without libtool. 60 1.1 riastrad # 61 1.1 riastrad # LICENSE 62 1.1 riastrad # 63 1.1 riastrad # Copyright (c) 2014, 2015, 2016 Philip Withnall <philip.withnall (a] collabora.co.uk> 64 1.1 riastrad # 65 1.1 riastrad # Copying and distribution of this file, with or without modification, are 66 1.1 riastrad # permitted in any medium without royalty provided the copyright notice 67 1.1 riastrad # and this notice are preserved. This file is offered as-is, without any 68 1.1 riastrad # warranty. 69 1.1 riastrad 70 1.1 riastrad #serial 15 71 1.1 riastrad 72 1.1 riastrad dnl Configured tools 73 1.1 riastrad m4_define([valgrind_tool_list], [[memcheck], [helgrind], [drd], [sgcheck]]) 74 1.1 riastrad m4_set_add_all([valgrind_exp_tool_set], [sgcheck]) 75 1.1 riastrad m4_foreach([vgtool], [valgrind_tool_list], 76 1.1 riastrad [m4_define([en_dflt_valgrind_]vgtool, [on])]) 77 1.1 riastrad 78 1.1 riastrad AC_DEFUN([AX_VALGRIND_DFLT],[ 79 1.1 riastrad m4_define([en_dflt_valgrind_$1], [$2]) 80 1.1 riastrad ])dnl 81 1.1 riastrad 82 1.1 riastrad AC_DEFUN([AX_VALGRIND_CHECK],[ 83 1.1 riastrad dnl Check for --enable-valgrind 84 1.1 riastrad AC_ARG_ENABLE([valgrind], 85 1.1 riastrad [AS_HELP_STRING([--enable-valgrind], [Whether to enable Valgrind on the unit tests (requires GNU make)])], 86 1.1 riastrad [enable_valgrind=$enableval],[enable_valgrind=no]) 87 1.1 riastrad 88 1.1 riastrad AS_IF([test "$enable_valgrind" != "no"],[ 89 1.1 riastrad # Check for Valgrind. 90 1.1 riastrad AC_CHECK_PROG([VALGRIND],[valgrind],[valgrind]) 91 1.1 riastrad AS_IF([test "$VALGRIND" = ""],[ 92 1.1 riastrad AS_IF([test "$enable_valgrind" = "yes"],[ 93 1.1 riastrad AC_MSG_ERROR([Could not find valgrind; either install it or reconfigure with --disable-valgrind]) 94 1.1 riastrad ],[ 95 1.1 riastrad enable_valgrind=no 96 1.1 riastrad ]) 97 1.1 riastrad ],[ 98 1.1 riastrad enable_valgrind=yes 99 1.1 riastrad ]) 100 1.1 riastrad ]) 101 1.1 riastrad 102 1.1 riastrad AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"]) 103 1.1 riastrad AC_SUBST([VALGRIND_ENABLED],[$enable_valgrind]) 104 1.1 riastrad 105 1.1 riastrad # Check for Valgrind tools we care about. 106 1.1 riastrad [valgrind_enabled_tools=] 107 1.1 riastrad m4_foreach([vgtool],[valgrind_tool_list],[ 108 1.1 riastrad AC_ARG_ENABLE([valgrind-]vgtool, 109 1.1 riastrad m4_if(m4_defn([en_dflt_valgrind_]vgtool),[off],dnl 110 1.1 riastrad [AS_HELP_STRING([--enable-valgrind-]vgtool, [Whether to use ]vgtool[ during the Valgrind tests])],dnl 111 1.1 riastrad [AS_HELP_STRING([--disable-valgrind-]vgtool, [Whether to skip ]vgtool[ during the Valgrind tests])]), 112 1.1 riastrad [enable_valgrind_]vgtool[=$enableval], 113 1.1 riastrad [enable_valgrind_]vgtool[=]) 114 1.1 riastrad AS_IF([test "$enable_valgrind" = "no"],[ 115 1.1 riastrad enable_valgrind_]vgtool[=no], 116 1.1 riastrad [test "$enable_valgrind_]vgtool[" ]dnl 117 1.1 riastrad m4_if(m4_defn([en_dflt_valgrind_]vgtool), [off], [= "yes"], [!= "no"]),[ 118 1.1 riastrad AC_CACHE_CHECK([for Valgrind tool ]vgtool, 119 1.1 riastrad [ax_cv_valgrind_tool_]vgtool,[ 120 1.1 riastrad ax_cv_valgrind_tool_]vgtool[=no 121 1.1 riastrad m4_set_contains([valgrind_exp_tool_set],vgtool, 122 1.1 riastrad [m4_define([vgtoolx],[exp-]vgtool)], 123 1.1 riastrad [m4_define([vgtoolx],vgtool)]) 124 1.1 riastrad AS_IF([`$VALGRIND --tool=]vgtoolx[ --help >/dev/null 2>&1`],[ 125 1.1 riastrad ax_cv_valgrind_tool_]vgtool[=yes 126 1.1 riastrad ]) 127 1.1 riastrad ]) 128 1.1 riastrad AS_IF([test "$ax_cv_valgrind_tool_]vgtool[" = "no"],[ 129 1.1 riastrad AS_IF([test "$enable_valgrind_]vgtool[" = "yes"],[ 130 1.1 riastrad AC_MSG_ERROR([Valgrind does not support ]vgtool[; reconfigure with --disable-valgrind-]vgtool) 131 1.1 riastrad ],[ 132 1.1 riastrad enable_valgrind_]vgtool[=no 133 1.1 riastrad ]) 134 1.1 riastrad ],[ 135 1.1 riastrad enable_valgrind_]vgtool[=yes 136 1.1 riastrad ]) 137 1.1 riastrad ]) 138 1.1 riastrad AS_IF([test "$enable_valgrind_]vgtool[" = "yes"],[ 139 1.1 riastrad valgrind_enabled_tools="$valgrind_enabled_tools ]m4_bpatsubst(vgtool,[^exp-])[" 140 1.1 riastrad ]) 141 1.1 riastrad AC_SUBST([ENABLE_VALGRIND_]vgtool,[$enable_valgrind_]vgtool) 142 1.1 riastrad ]) 143 1.1 riastrad AC_SUBST([valgrind_tools],["]m4_join([ ], valgrind_tool_list)["]) 144 1.1 riastrad AC_SUBST([valgrind_enabled_tools],[$valgrind_enabled_tools]) 145 1.1 riastrad 146 1.1 riastrad [VALGRIND_CHECK_RULES=' 147 1.1 riastrad # Valgrind check 148 1.1 riastrad # 149 1.1 riastrad # Optional: 150 1.1 riastrad # - VALGRIND_SUPPRESSIONS_FILES: Space-separated list of Valgrind suppressions 151 1.1 riastrad # files to load. (Default: empty) 152 1.1 riastrad # - VALGRIND_FLAGS: General flags to pass to all Valgrind tools. 153 1.1 riastrad # (Default: --num-callers=30) 154 1.1 riastrad # - VALGRIND_$toolname_FLAGS: Flags to pass to Valgrind $toolname (one of: 155 1.1 riastrad # memcheck, helgrind, drd, sgcheck). (Default: various) 156 1.1 riastrad 157 1.1 riastrad # Optional variables 158 1.1 riastrad VALGRIND_SUPPRESSIONS ?= $(addprefix --suppressions=,$(VALGRIND_SUPPRESSIONS_FILES)) 159 1.1 riastrad VALGRIND_FLAGS ?= --num-callers=30 160 1.1 riastrad VALGRIND_memcheck_FLAGS ?= --leak-check=full --show-reachable=no 161 1.1 riastrad VALGRIND_helgrind_FLAGS ?= --history-level=approx 162 1.1 riastrad VALGRIND_drd_FLAGS ?= 163 1.1 riastrad VALGRIND_sgcheck_FLAGS ?= 164 1.1 riastrad 165 1.1 riastrad # Internal use 166 1.1 riastrad valgrind_log_files = $(addprefix test-suite-,$(addsuffix .log,$(valgrind_tools))) 167 1.1 riastrad 168 1.1 riastrad valgrind_memcheck_flags = --tool=memcheck $(VALGRIND_memcheck_FLAGS) 169 1.1 riastrad valgrind_helgrind_flags = --tool=helgrind $(VALGRIND_helgrind_FLAGS) 170 1.1 riastrad valgrind_drd_flags = --tool=drd $(VALGRIND_drd_FLAGS) 171 1.1 riastrad valgrind_sgcheck_flags = --tool=exp-sgcheck $(VALGRIND_sgcheck_FLAGS) 172 1.1 riastrad 173 1.1 riastrad valgrind_quiet = $(valgrind_quiet_$(V)) 174 1.1 riastrad valgrind_quiet_ = $(valgrind_quiet_$(AM_DEFAULT_VERBOSITY)) 175 1.1 riastrad valgrind_quiet_0 = --quiet 176 1.1 riastrad valgrind_v_use = $(valgrind_v_use_$(V)) 177 1.1 riastrad valgrind_v_use_ = $(valgrind_v_use_$(AM_DEFAULT_VERBOSITY)) 178 1.1 riastrad valgrind_v_use_0 = @echo " USE " $(patsubst check-valgrind-%,%,$''@):; 179 1.1 riastrad 180 1.1 riastrad # Support running with and without libtool. 181 1.1 riastrad ifneq ($(LIBTOOL),) 182 1.1 riastrad valgrind_lt = $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=execute 183 1.1 riastrad else 184 1.1 riastrad valgrind_lt = 185 1.1 riastrad endif 186 1.1 riastrad 187 1.1 riastrad # Use recursive makes in order to ignore errors during check 188 1.1 riastrad check-valgrind: 189 1.1 riastrad ifeq ($(VALGRIND_ENABLED),yes) 190 1.1 riastrad $(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) -k \ 191 1.1 riastrad $(foreach tool, $(valgrind_enabled_tools), check-valgrind-$(tool)) 192 1.1 riastrad else 193 1.1 riastrad @echo "Need to use GNU make and reconfigure with --enable-valgrind" 194 1.1 riastrad endif 195 1.1 riastrad 196 1.1 riastrad # Valgrind running 197 1.1 riastrad VALGRIND_TESTS_ENVIRONMENT = \ 198 1.1 riastrad $(TESTS_ENVIRONMENT) \ 199 1.1 riastrad env VALGRIND=$(VALGRIND) \ 200 1.1 riastrad G_SLICE=always-malloc,debug-blocks \ 201 1.1 riastrad G_DEBUG=fatal-warnings,fatal-criticals,gc-friendly 202 1.1 riastrad 203 1.1 riastrad VALGRIND_LOG_COMPILER = \ 204 1.1 riastrad $(valgrind_lt) \ 205 1.1 riastrad $(VALGRIND) $(VALGRIND_SUPPRESSIONS) --error-exitcode=1 $(VALGRIND_FLAGS) 206 1.1 riastrad 207 1.1 riastrad define valgrind_tool_rule = 208 1.1 riastrad check-valgrind-$(1): 209 1.1 riastrad ifeq ($$(VALGRIND_ENABLED)-$$(ENABLE_VALGRIND_$(1)),yes-yes) 210 1.1 riastrad $$(valgrind_v_use)$$(MAKE) check-TESTS \ 211 1.1 riastrad TESTS_ENVIRONMENT="$$(VALGRIND_TESTS_ENVIRONMENT)" \ 212 1.1 riastrad LOG_COMPILER="$$(VALGRIND_LOG_COMPILER)" \ 213 1.1 riastrad LOG_FLAGS="$$(valgrind_$(1)_flags)" \ 214 1.1 riastrad TEST_SUITE_LOG=test-suite-$(1).log 215 1.1 riastrad else ifeq ($$(VALGRIND_ENABLED),yes) 216 1.1 riastrad @echo "Need to reconfigure with --enable-valgrind-$(1)" 217 1.1 riastrad else 218 1.1 riastrad @echo "Need to reconfigure with --enable-valgrind" 219 1.1 riastrad endif 220 1.1 riastrad endef 221 1.1 riastrad 222 1.1 riastrad $(foreach tool,$(valgrind_tools),$(eval $(call valgrind_tool_rule,$(tool)))) 223 1.1 riastrad 224 1.1 riastrad A''M_DISTCHECK_CONFIGURE_FLAGS ?= 225 1.1 riastrad A''M_DISTCHECK_CONFIGURE_FLAGS += --disable-valgrind 226 1.1 riastrad 227 1.1 riastrad MOSTLYCLEANFILES ?= 228 1.1 riastrad MOSTLYCLEANFILES += $(valgrind_log_files) 229 1.1 riastrad 230 1.1 riastrad .PHONY: check-valgrind $(add-prefix check-valgrind-,$(valgrind_tools)) 231 1.1 riastrad '] 232 1.1 riastrad 233 1.1 riastrad AS_IF([test "$enable_valgrind" != "yes"], [ 234 1.1 riastrad VALGRIND_CHECK_RULES=' 235 1.1 riastrad check-valgrind: 236 1.1 riastrad @echo "Need to use GNU make and reconfigure with --enable-valgrind"' 237 1.1 riastrad ]) 238 1.1 riastrad 239 1.1 riastrad AC_SUBST([VALGRIND_CHECK_RULES]) 240 1.1 riastrad m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([VALGRIND_CHECK_RULES])]) 241 1.1 riastrad ]) 242