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