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