1# =========================================================================== 2# http://www.gnu.org/software/autoconf-archive/ax_valgrind_check.html 3# =========================================================================== 4# 5# SYNOPSIS 6# 7# AX_VALGRIND_CHECK() 8# 9# DESCRIPTION 10# 11# Checks whether Valgrind is present and, if so, allows running `make 12# check` under a variety of Valgrind tools to check for memory and 13# threading errors. 14# 15# Defines VALGRIND_CHECK_RULES which should be substituted in your 16# Makefile; and $enable_valgrind which can be used in subsequent configure 17# output. VALGRIND_ENABLED is defined and substituted, and corresponds to 18# the value of the --enable-valgrind option, which defaults to being 19# enabled if Valgrind is installed and disabled otherwise. 20# 21# If unit tests are written using a shell script and automake's 22# LOG_COMPILER system, the $(VALGRIND) variable can be used within the 23# shell scripts to enable Valgrind, as described here: 24# 25# https://www.gnu.org/software/gnulib/manual/html_node/Running-self_002dtests-under-valgrind.html 26# 27# Usage example: 28# 29# configure.ac: 30# 31# AX_VALGRIND_CHECK 32# 33# Makefile.am: 34# 35# @VALGRIND_CHECK_RULES@ 36# VALGRIND_SUPPRESSIONS_FILES = my-project.supp 37# EXTRA_DIST = my-project.supp 38# 39# This results in a "check-valgrind" rule being added to any Makefile.am 40# which includes "@VALGRIND_CHECK_RULES@" (assuming the module has been 41# configured with --enable-valgrind). Running `make check-valgrind` in 42# that directory will run the module's test suite (`make check`) once for 43# each of the available Valgrind tools (out of memcheck, helgrind, drd and 44# sgcheck), and will output results to test-suite-$toolname.log for each. 45# The target will succeed if there are zero errors and fail otherwise. 46# 47# The macro supports running with and without libtool. 48# 49# LICENSE 50# 51# Copyright (c) 2014, 2015 Philip Withnall <philip.withnall@collabora.co.uk> 52# 53# Copying and distribution of this file, with or without modification, are 54# permitted in any medium without royalty provided the copyright notice 55# and this notice are preserved. This file is offered as-is, without any 56# warranty. 57 58#serial 4 59 60AC_DEFUN([AX_VALGRIND_CHECK],[ 61 dnl Check for --enable-valgrind 62 AC_MSG_CHECKING([whether to enable Valgrind on the unit tests]) 63 AC_ARG_ENABLE([valgrind], 64 [AS_HELP_STRING([--enable-valgrind], [Whether to enable Valgrind on the unit tests])], 65 [enable_valgrind=$enableval],[enable_valgrind=]) 66 67 # Check for Valgrind. 68 AC_CHECK_PROG([VALGRIND],[valgrind],[valgrind]) 69 70 AS_IF([test "$enable_valgrind" = "yes" -a "$VALGRIND" = ""],[ 71 AC_MSG_ERROR([Could not find valgrind; either install it or reconfigure with --disable-valgrind]) 72 ]) 73 AS_IF([test "$enable_valgrind" != "no"],[enable_valgrind=yes]) 74 75 AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"]) 76 AC_SUBST([VALGRIND_ENABLED],[$enable_valgrind]) 77 AC_MSG_RESULT([$enable_valgrind]) 78 79 # Check for Valgrind tools we care about. 80 m4_define([valgrind_tool_list],[[memcheck], [helgrind], [drd], [exp-sgcheck]]) 81 82 AS_IF([test "$VALGRIND" != ""],[ 83 m4_foreach([vgtool],[valgrind_tool_list],[ 84 m4_define([vgtooln],AS_TR_SH(vgtool)) 85 m4_define([ax_cv_var],[ax_cv_valgrind_tool_]vgtooln) 86 AC_CACHE_CHECK([for Valgrind tool ]vgtool,ax_cv_var,[ 87 ax_cv_var= 88 AS_IF([`$VALGRIND --tool=vgtool --help >/dev/null 2>&1`],[ 89 ax_cv_var="vgtool" 90 ]) 91 ]) 92 93 AC_SUBST([VALGRIND_HAVE_TOOL_]vgtooln,[$ax_cv_var]) 94 ]) 95 ]) 96 97VALGRIND_CHECK_RULES=' 98# Valgrind check 99# 100# Optional: 101# - VALGRIND_SUPPRESSIONS_FILES: Space-separated list of Valgrind suppressions 102# files to load. (Default: empty) 103# - VALGRIND_FLAGS: General flags to pass to all Valgrind tools. 104# (Default: --num-callers=30) 105# - VALGRIND_$toolname_FLAGS: Flags to pass to Valgrind $toolname (one of: 106# memcheck, helgrind, drd, sgcheck). (Default: various) 107 108# Optional variables 109VALGRIND_SUPPRESSIONS ?= $(addprefix --suppressions=,$(VALGRIND_SUPPRESSIONS_FILES)) 110VALGRIND_FLAGS ?= --num-callers=30 111VALGRIND_memcheck_FLAGS ?= --leak-check=full --show-reachable=no 112VALGRIND_helgrind_FLAGS ?= --history-level=approx 113VALGRIND_drd_FLAGS ?= 114VALGRIND_sgcheck_FLAGS ?= 115 116# Internal use 117valgrind_tools = memcheck helgrind drd sgcheck 118valgrind_log_files = $(addprefix test-suite-,$(addsuffix .log,$(valgrind_tools))) 119 120valgrind_memcheck_flags = --tool=memcheck $(VALGRIND_memcheck_FLAGS) 121valgrind_helgrind_flags = --tool=helgrind $(VALGRIND_helgrind_FLAGS) 122valgrind_drd_flags = --tool=drd $(VALGRIND_drd_FLAGS) 123valgrind_sgcheck_flags = --tool=exp-sgcheck $(VALGRIND_sgcheck_FLAGS) 124 125valgrind_quiet = $(valgrind_quiet_$(V)) 126valgrind_quiet_ = $(valgrind_quiet_$(AM_DEFAULT_VERBOSITY)) 127valgrind_quiet_0 = --quiet 128 129# Support running with and without libtool. 130ifneq ($(LIBTOOL),) 131valgrind_lt = $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=execute 132else 133valgrind_lt = 134endif 135 136# Use recursive makes in order to ignore errors during check 137check-valgrind: 138ifeq ($(VALGRIND_ENABLED),yes) 139 -$(foreach tool,$(valgrind_tools), \ 140 $(if $(VALGRIND_HAVE_TOOL_$(tool))$(VALGRIND_HAVE_TOOL_exp_$(tool)), \ 141 $(MAKE) $(AM_MAKEFLAGS) -k check-valgrind-tool VALGRIND_TOOL=$(tool); \ 142 ) \ 143 ) 144else 145 @echo "Need to reconfigure with --enable-valgrind" 146endif 147 148# Valgrind running 149VALGRIND_TESTS_ENVIRONMENT = \ 150 $(TESTS_ENVIRONMENT) \ 151 env VALGRIND=$(VALGRIND) \ 152 G_SLICE=always-malloc,debug-blocks \ 153 G_DEBUG=fatal-warnings,fatal-criticals,gc-friendly 154 155VALGRIND_LOG_COMPILER = \ 156 $(valgrind_lt) \ 157 $(VALGRIND) $(VALGRIND_SUPPRESSIONS) --error-exitcode=1 $(VALGRIND_FLAGS) 158 159check-valgrind-tool: 160ifeq ($(VALGRIND_ENABLED),yes) 161 $(MAKE) check-TESTS \ 162 TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \ 163 LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \ 164 LOG_FLAGS="$(valgrind_$(VALGRIND_TOOL)_flags)" \ 165 TEST_SUITE_LOG=test-suite-$(VALGRIND_TOOL).log 166else 167 @echo "Need to reconfigure with --enable-valgrind" 168endif 169 170DISTCHECK_CONFIGURE_FLAGS ?= 171DISTCHECK_CONFIGURE_FLAGS += --disable-valgrind 172 173MOSTLYCLEANFILES ?= 174MOSTLYCLEANFILES += $(valgrind_log_files) 175 176.PHONY: check-valgrind check-valgrind-tool 177' 178 179 AC_SUBST([VALGRIND_CHECK_RULES]) 180 m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([VALGRIND_CHECK_RULES])]) 181]) 182