ax_valgrind_check.m4 7.6 KB

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