m4/ax_valgrind_check.m4
changeset 109 2947214c450e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/m4/ax_valgrind_check.m4	Tue Apr 25 17:41:00 2023 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +# ===========================================================================
     1.5 +#     http://www.gnu.org/software/autoconf-archive/ax_valgrind_check.html
     1.6 +# ===========================================================================
     1.7 +#
     1.8 +# SYNOPSIS
     1.9 +#
    1.10 +#   AX_VALGRIND_CHECK()
    1.11 +#
    1.12 +# DESCRIPTION
    1.13 +#
    1.14 +#   Checks whether Valgrind is present and, if so, allows running `make
    1.15 +#   check` under a variety of Valgrind tools to check for memory and
    1.16 +#   threading errors.
    1.17 +#
    1.18 +#   Defines VALGRIND_CHECK_RULES which should be substituted in your
    1.19 +#   Makefile; and $enable_valgrind which can be used in subsequent configure
    1.20 +#   output. VALGRIND_ENABLED is defined and substituted, and corresponds to
    1.21 +#   the value of the --enable-valgrind option, which defaults to being
    1.22 +#   enabled if Valgrind is installed and disabled otherwise.
    1.23 +#
    1.24 +#   If unit tests are written using a shell script and automake's
    1.25 +#   LOG_COMPILER system, the $(VALGRIND) variable can be used within the
    1.26 +#   shell scripts to enable Valgrind, as described here:
    1.27 +#
    1.28 +#     https://www.gnu.org/software/gnulib/manual/html_node/Running-self_002dtests-under-valgrind.html
    1.29 +#
    1.30 +#   Usage example:
    1.31 +#
    1.32 +#   configure.ac:
    1.33 +#
    1.34 +#     AX_VALGRIND_CHECK
    1.35 +#
    1.36 +#   Makefile.am:
    1.37 +#
    1.38 +#     @VALGRIND_CHECK_RULES@
    1.39 +#     VALGRIND_SUPPRESSIONS_FILES = my-project.supp
    1.40 +#     EXTRA_DIST = my-project.supp
    1.41 +#
    1.42 +#   This results in a "check-valgrind" rule being added to any Makefile.am
    1.43 +#   which includes "@VALGRIND_CHECK_RULES@" (assuming the module has been
    1.44 +#   configured with --enable-valgrind). Running `make check-valgrind` in
    1.45 +#   that directory will run the module's test suite (`make check`) once for
    1.46 +#   each of the available Valgrind tools (out of memcheck, helgrind, drd and
    1.47 +#   sgcheck), and will output results to test-suite-$toolname.log for each.
    1.48 +#   The target will succeed if there are zero errors and fail otherwise.
    1.49 +#
    1.50 +#   Alternatively, a "check-valgrind-$TOOL" rule will be added, for $TOOL in
    1.51 +#   memcheck, helgrind, drd and sgcheck. These are useful because often only
    1.52 +#   some of those tools can be ran cleanly on a codebase.
    1.53 +#
    1.54 +#   The macro supports running with and without libtool.
    1.55 +#
    1.56 +# LICENSE
    1.57 +#
    1.58 +#   Copyright (c) 2014, 2015, 2016 Philip Withnall <philip.withnall@collabora.co.uk>
    1.59 +#
    1.60 +#   Copying and distribution of this file, with or without modification, are
    1.61 +#   permitted in any medium without royalty provided the copyright notice
    1.62 +#   and this notice are preserved.  This file is offered as-is, without any
    1.63 +#   warranty.
    1.64 +
    1.65 +#serial 9
    1.66 +
    1.67 +AC_DEFUN([AX_VALGRIND_CHECK],[
    1.68 +	dnl Check for --enable-valgrind
    1.69 +	AC_ARG_ENABLE([valgrind],
    1.70 +	              [AS_HELP_STRING([--enable-valgrind], [Whether to enable Valgrind on the unit tests])],
    1.71 +	              [enable_valgrind=$enableval],[enable_valgrind=])
    1.72 +
    1.73 +	AS_IF([test "$enable_valgrind" != "no"],[
    1.74 +		# Check for Valgrind.
    1.75 +		AC_CHECK_PROG([VALGRIND],[valgrind],[valgrind])
    1.76 +		AS_IF([test "$VALGRIND" = ""],[
    1.77 +			AS_IF([test "$enable_valgrind" = "yes"],[
    1.78 +				AC_MSG_ERROR([Could not find valgrind; either install it or reconfigure with --disable-valgrind])
    1.79 +			],[
    1.80 +				enable_valgrind=no
    1.81 +			])
    1.82 +		],[
    1.83 +			enable_valgrind=yes
    1.84 +		])
    1.85 +	])
    1.86 +
    1.87 +	AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
    1.88 +	AC_SUBST([VALGRIND_ENABLED],[$enable_valgrind])
    1.89 +
    1.90 +	# Check for Valgrind tools we care about.
    1.91 +	m4_define([valgrind_tool_list],[[memcheck], [helgrind], [drd], [exp-sgcheck]])
    1.92 +
    1.93 +	AS_IF([test "$VALGRIND" != ""],[
    1.94 +		m4_foreach([vgtool],[valgrind_tool_list],[
    1.95 +			m4_define([vgtooln],AS_TR_SH(vgtool))
    1.96 +			m4_define([ax_cv_var],[ax_cv_valgrind_tool_]vgtooln)
    1.97 +			AC_CACHE_CHECK([for Valgrind tool ]vgtool,ax_cv_var,[
    1.98 +				ax_cv_var=
    1.99 +				AS_IF([`$VALGRIND --tool=vgtool --help >/dev/null 2>&1`],[
   1.100 +					ax_cv_var="vgtool"
   1.101 +				])
   1.102 +			])
   1.103 +
   1.104 +			AC_SUBST([VALGRIND_HAVE_TOOL_]vgtooln,[$ax_cv_var])
   1.105 +		])
   1.106 +	])
   1.107 +
   1.108 +[VALGRIND_CHECK_RULES='
   1.109 +# Valgrind check
   1.110 +#
   1.111 +# Optional:
   1.112 +#  - VALGRIND_SUPPRESSIONS_FILES: Space-separated list of Valgrind suppressions
   1.113 +#    files to load. (Default: empty)
   1.114 +#  - VALGRIND_FLAGS: General flags to pass to all Valgrind tools.
   1.115 +#    (Default: --num-callers=30)
   1.116 +#  - VALGRIND_$toolname_FLAGS: Flags to pass to Valgrind $toolname (one of:
   1.117 +#    memcheck, helgrind, drd, sgcheck). (Default: various)
   1.118 +
   1.119 +# Optional variables
   1.120 +VALGRIND_SUPPRESSIONS ?= $(addprefix --suppressions=,$(VALGRIND_SUPPRESSIONS_FILES))
   1.121 +VALGRIND_FLAGS ?= --num-callers=30
   1.122 +VALGRIND_memcheck_FLAGS ?= --leak-check=full --show-reachable=no
   1.123 +VALGRIND_helgrind_FLAGS ?= --history-level=approx
   1.124 +VALGRIND_drd_FLAGS ?=
   1.125 +VALGRIND_sgcheck_FLAGS ?=
   1.126 +
   1.127 +# Internal use
   1.128 +valgrind_tools = memcheck helgrind drd sgcheck
   1.129 +valgrind_log_files = $(addprefix test-suite-,$(addsuffix .log,$(valgrind_tools)))
   1.130 +
   1.131 +valgrind_memcheck_flags = --tool=memcheck $(VALGRIND_memcheck_FLAGS)
   1.132 +valgrind_helgrind_flags = --tool=helgrind $(VALGRIND_helgrind_FLAGS)
   1.133 +valgrind_drd_flags = --tool=drd $(VALGRIND_drd_FLAGS)
   1.134 +valgrind_sgcheck_flags = --tool=exp-sgcheck $(VALGRIND_sgcheck_FLAGS)
   1.135 +
   1.136 +valgrind_quiet = $(valgrind_quiet_$(V))
   1.137 +valgrind_quiet_ = $(valgrind_quiet_$(AM_DEFAULT_VERBOSITY))
   1.138 +valgrind_quiet_0 = --quiet
   1.139 +
   1.140 +# Support running with and without libtool.
   1.141 +ifneq ($(LIBTOOL),)
   1.142 +valgrind_lt = $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=execute
   1.143 +else
   1.144 +valgrind_lt =
   1.145 +endif
   1.146 +
   1.147 +# Use recursive makes in order to ignore errors during check
   1.148 +check-valgrind:
   1.149 +ifeq ($(VALGRIND_ENABLED),yes)
   1.150 +	-$(foreach tool,$(valgrind_tools), \
   1.151 +		$(if $(VALGRIND_HAVE_TOOL_$(tool))$(VALGRIND_HAVE_TOOL_exp_$(tool)), \
   1.152 +			$(MAKE) $(AM_MAKEFLAGS) -k check-valgrind-tool VALGRIND_TOOL=$(tool); \
   1.153 +		) \
   1.154 +	)
   1.155 +else
   1.156 +	@echo "Need to reconfigure with --enable-valgrind"
   1.157 +endif
   1.158 +
   1.159 +# Valgrind running
   1.160 +VALGRIND_TESTS_ENVIRONMENT = \
   1.161 +	$(TESTS_ENVIRONMENT) \
   1.162 +	env VALGRIND=$(VALGRIND) \
   1.163 +	G_SLICE=always-malloc,debug-blocks \
   1.164 +	G_DEBUG=fatal-warnings,fatal-criticals,gc-friendly
   1.165 +
   1.166 +VALGRIND_LOG_COMPILER = \
   1.167 +	$(valgrind_lt) \
   1.168 +	$(VALGRIND) $(VALGRIND_SUPPRESSIONS) --error-exitcode=1 $(VALGRIND_FLAGS)
   1.169 +
   1.170 +check-valgrind-tool:
   1.171 +ifeq ($(VALGRIND_ENABLED),yes)
   1.172 +	$(MAKE) check-TESTS \
   1.173 +		TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \
   1.174 +		LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \
   1.175 +		LOG_FLAGS="$(valgrind_$(VALGRIND_TOOL)_flags)" \
   1.176 +		TEST_SUITE_LOG=test-suite-$(VALGRIND_TOOL).log
   1.177 +else
   1.178 +	@echo "Need to reconfigure with --enable-valgrind"
   1.179 +endif
   1.180 +
   1.181 +check-valgrind-memcheck:
   1.182 +ifeq ($(VALGRIND_ENABLED),yes)
   1.183 +	$(MAKE) check-TESTS \
   1.184 +		TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \
   1.185 +		LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \
   1.186 +		LOG_FLAGS="$(valgrind_memcheck_flags)" \
   1.187 +		TEST_SUITE_LOG=test-suite-memcheck.log
   1.188 +else
   1.189 +	@echo "Need to reconfigure with --enable-valgrind"
   1.190 +endif
   1.191 +
   1.192 +check-valgrind-helgrind:
   1.193 +ifeq ($(VALGRIND_ENABLED),yes)
   1.194 +	$(MAKE) check-TESTS \
   1.195 +		TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \
   1.196 +		LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \
   1.197 +		LOG_FLAGS="$(valgrind_helgrind_flags)" \
   1.198 +		TEST_SUITE_LOG=test-suite-helgrind.log
   1.199 +else
   1.200 +	@echo "Need to reconfigure with --enable-valgrind"
   1.201 +endif
   1.202 +
   1.203 +check-valgrind-drd:
   1.204 +ifeq ($(VALGRIND_ENABLED),yes)
   1.205 +	$(MAKE) check-TESTS \
   1.206 +		TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \
   1.207 +		LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \
   1.208 +		LOG_FLAGS="$(valgrind_drd_flags)" \
   1.209 +		TEST_SUITE_LOG=test-suite-drd.log
   1.210 +else
   1.211 +	@echo "Need to reconfigure with --enable-valgrind"
   1.212 +endif
   1.213 +
   1.214 +check-valgrind-sgcheck:
   1.215 +ifeq ($(VALGRIND_ENABLED),yes)
   1.216 +	$(MAKE) check-TESTS \
   1.217 +		TESTS_ENVIRONMENT="$(VALGRIND_TESTS_ENVIRONMENT)" \
   1.218 +		LOG_COMPILER="$(VALGRIND_LOG_COMPILER)" \
   1.219 +		LOG_FLAGS="$(valgrind_sgcheck_flags)" \
   1.220 +		TEST_SUITE_LOG=test-suite-sgcheck.log
   1.221 +else
   1.222 +	@echo "Need to reconfigure with --enable-valgrind"
   1.223 +endif
   1.224 +
   1.225 +A''M_DISTCHECK_CONFIGURE_FLAGS ?=
   1.226 +A''M_DISTCHECK_CONFIGURE_FLAGS += --disable-valgrind
   1.227 +
   1.228 +MOSTLYCLEANFILES ?=
   1.229 +MOSTLYCLEANFILES += $(valgrind_log_files)
   1.230 +
   1.231 +.PHONY: check-valgrind check-valgrind-tool
   1.232 +']
   1.233 +
   1.234 +	AC_SUBST([VALGRIND_CHECK_RULES])
   1.235 +	m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([VALGRIND_CHECK_RULES])])
   1.236 +])