immutable: Add tests.
[gnulib.git] / m4 / valgrind-tests.m4
blob50d90e1456112a52675db6df8e1dfdf572bee26d
1 # valgrind-tests.m4 serial 6
2 dnl Copyright (C) 2008-2021 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl From Simon Josefsson
9 # gl_VALGRIND_TESTS()
10 # -------------------
11 # Check if valgrind is available, and set VALGRIND to it if available.
12 AC_DEFUN([gl_VALGRIND_TESTS],
14   AC_ARG_ENABLE([valgrind-tests],
15     AS_HELP_STRING([--disable-valgrind-tests],
16                    [don't try to run self tests under valgrind]),
17     [opt_valgrind_tests=$enableval], [opt_valgrind_tests=yes])
19   # Run self-tests under valgrind?
20   if test "$opt_valgrind_tests" = "yes" && test "$cross_compiling" = no; then
21     AC_CHECK_PROGS([VALGRIND], [valgrind])
23     if test -n "$VALGRIND"; then
24       dnl On Ubuntu 16.04, /usr/bin/valgrind works only on 64-bit executables
25       dnl but fails on 32-bit executables (with exit code 1) and on x86_64-x32
26       dnl executables (with exit code 126).
27       AC_CACHE_CHECK([whether valgrind works on executables produced by the compiler],
28         [gl_cv_prog_valgrind_works],
29         [AC_RUN_IFELSE(
30            [AC_LANG_SOURCE([[int main () { return 0; }]])],
31            [$VALGRIND ./conftest$ac_exeext 2>/dev/null
32             if test $? = 0; then
33               gl_cv_prog_valgrind_works=yes
34             else
35               gl_cv_prog_valgrind_works=no
36             fi
37            ],
38            [gl_cv_prog_valgrind_works=no])
39         ])
40       if test $gl_cv_prog_valgrind_works != yes; then
41         VALGRIND=
42       fi
43     fi
45     if test -n "$VALGRIND"; then
46       AC_CACHE_CHECK([for valgrind options for tests],
47         [gl_cv_opt_valgrind_tests],
48         [gl_valgrind_opts='-q --error-exitcode=1 --leak-check=full'
49          if $VALGRIND $gl_valgrind_opts ls > /dev/null 2>&1; then
50            gl_cv_opt_valgrind_tests="$gl_valgrind_opts"
51          else
52            gl_cv_opt_valgrind_tests=no
53          fi
54         ])
55       if test "$gl_cv_opt_valgrind_tests" != no; then
56         VALGRIND="$VALGRIND $gl_cv_opt_valgrind_tests"
57       fi
58     fi
59   fi