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