configure: Enable valgrind support

I've caught some issues with valgrind that were missed by the compiler
sanitizers (though were interestingly picked up by the cmake build of
the test suites).

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I96ac0a331c2d7466ee66a7ce2e8d8b2b8d497d5d
diff --git a/Makefile.am b/Makefile.am
index 75535d4..d630078 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -49,3 +49,5 @@
 noinst_LIBRARIES = tests/libtest-utils.a
 
 tests_libtest_utils_a_SOURCES = tests/test-utils.c tests/test-utils.h
+
+@VALGRIND_CHECK_RULES@
diff --git a/configure.ac b/configure.ac
index 138fa05..6b36124 100644
--- a/configure.ac
+++ b/configure.ac
@@ -127,6 +127,16 @@
 AM_CONDITIONAL([LIBMCTP_BINDING_serial], [true])
 AM_CONDITIONAL([LIBMCTP_BINDING_astlpc], [true])
 
+# Check for valgrind
+AS_IF([test "x$enable_tests" = "xno"], [enable_valgrind=no])
+m4_foreach([vgtool], [valgrind_tool_list],
+    [AX_VALGRIND_DFLT(vgtool, [off])])
+AX_VALGRIND_DFLT([memcheck], [on])
+AX_VALGRIND_CHECK
+AM_EXTRA_RECURSIVE_TARGETS([check-valgrind])
+m4_foreach([vgtool], [valgrind_tool_list],
+    [AM_EXTRA_RECURSIVE_TARGETS([check-valgrind-]vgtool)])
+
 AX_CODE_COVERAGE
 m4_ifdef([_AX_CODE_COVERAGE_RULES],
     [AM_CONDITIONAL(AUTOCONF_CODE_COVERAGE_2019_01_06, [true])],