ax_valgrind_check.m4 8.8 KB

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