ax_valgrind_check.m4 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. # in each Makefile.am with tests:
  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. Running `make
  46. # check-valgrind` in that directory will recursively run the module's test
  47. # suite (`make check`) once for each of the available Valgrind tools (out
  48. # of memcheck, helgrind and drd) while the sgcheck will be skipped unless
  49. # enabled again on the commandline with --enable-valgrind-sgcheck. The
  50. # results for each check will be output to test-suite-$toolname.log. The
  51. # target will succeed if there are zero errors and fail otherwise.
  52. #
  53. # Alternatively, a "check-valgrind-$TOOL" rule will be added, for $TOOL in
  54. # memcheck, helgrind, drd and sgcheck. These are useful because often only
  55. # some of those tools can be ran cleanly on a codebase.
  56. #
  57. # The macro supports running with and without libtool.
  58. #
  59. # LICENSE
  60. #
  61. # Copyright (c) 2014, 2015, 2016 Philip Withnall <philip.withnall@collabora.co.uk>
  62. #
  63. # Copying and distribution of this file, with or without modification, are
  64. # permitted in any medium without royalty provided the copyright notice
  65. # and this notice are preserved. This file is offered as-is, without any
  66. # warranty.
  67. #serial 17
  68. dnl Configured tools
  69. m4_define([valgrind_tool_list], [[memcheck], [helgrind], [drd], [sgcheck]])
  70. m4_set_add_all([valgrind_exp_tool_set], [sgcheck])
  71. m4_foreach([vgtool], [valgrind_tool_list],
  72. [m4_define([en_dflt_valgrind_]vgtool, [on])])
  73. AC_DEFUN([AX_VALGRIND_DFLT],[
  74. m4_define([en_dflt_valgrind_$1], [$2])
  75. ])dnl
  76. AM_EXTRA_RECURSIVE_TARGETS([check-valgrind])
  77. m4_foreach([vgtool], [valgrind_tool_list],
  78. [AM_EXTRA_RECURSIVE_TARGETS([check-valgrind-]vgtool)])
  79. AC_DEFUN([AX_VALGRIND_CHECK],[
  80. dnl Check for --enable-valgrind
  81. AC_ARG_ENABLE([valgrind],
  82. [AS_HELP_STRING([--enable-valgrind], [Whether to enable Valgrind on the unit tests])],
  83. [enable_valgrind=$enableval],[enable_valgrind=])
  84. AS_IF([test "$enable_valgrind" != "no"],[
  85. # Check for Valgrind.
  86. AC_CHECK_PROG([VALGRIND],[valgrind],[valgrind])
  87. AS_IF([test "$VALGRIND" = ""],[
  88. AS_IF([test "$enable_valgrind" = "yes"],[
  89. AC_MSG_ERROR([Could not find valgrind; either install it or reconfigure with --disable-valgrind])
  90. ],[
  91. enable_valgrind=no
  92. ])
  93. ],[
  94. enable_valgrind=yes
  95. ])
  96. ])
  97. AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
  98. AC_SUBST([VALGRIND_ENABLED],[$enable_valgrind])
  99. # Check for Valgrind tools we care about.
  100. [valgrind_enabled_tools=]
  101. m4_foreach([vgtool],[valgrind_tool_list],[
  102. AC_ARG_ENABLE([valgrind-]vgtool,
  103. m4_if(m4_defn([en_dflt_valgrind_]vgtool),[off],dnl
  104. [AS_HELP_STRING([--enable-valgrind-]vgtool, [Whether to use ]vgtool[ during the Valgrind tests])],dnl
  105. [AS_HELP_STRING([--disable-valgrind-]vgtool, [Whether to skip ]vgtool[ during the Valgrind tests])]),
  106. [enable_valgrind_]vgtool[=$enableval],
  107. [enable_valgrind_]vgtool[=])
  108. AS_IF([test "$enable_valgrind" = "no"],[
  109. enable_valgrind_]vgtool[=no],
  110. [test "$enable_valgrind_]vgtool[" ]dnl
  111. m4_if(m4_defn([en_dflt_valgrind_]vgtool), [off], [= "yes"], [!= "no"]),[
  112. AC_CACHE_CHECK([for Valgrind tool ]vgtool,
  113. [ax_cv_valgrind_tool_]vgtool,[
  114. ax_cv_valgrind_tool_]vgtool[=no
  115. m4_set_contains([valgrind_exp_tool_set],vgtool,
  116. [m4_define([vgtoolx],[exp-]vgtool)],
  117. [m4_define([vgtoolx],vgtool)])
  118. AS_IF([`$VALGRIND --tool=]vgtoolx[ --help >/dev/null 2>&1`],[
  119. ax_cv_valgrind_tool_]vgtool[=yes
  120. ])
  121. ])
  122. AS_IF([test "$ax_cv_valgrind_tool_]vgtool[" = "no"],[
  123. AS_IF([test "$enable_valgrind_]vgtool[" = "yes"],[
  124. AC_MSG_ERROR([Valgrind does not support ]vgtool[; reconfigure with --disable-valgrind-]vgtool)
  125. ],[
  126. enable_valgrind_]vgtool[=no
  127. ])
  128. ],[
  129. enable_valgrind_]vgtool[=yes
  130. ])
  131. ])
  132. AS_IF([test "$enable_valgrind_]vgtool[" = "yes"],[
  133. valgrind_enabled_tools="$valgrind_enabled_tools ]m4_bpatsubst(vgtool,[^exp-])["
  134. ])
  135. AC_SUBST([ENABLE_VALGRIND_]vgtool,[$enable_valgrind_]vgtool)
  136. ])
  137. AC_SUBST([valgrind_tools],["]m4_join([ ], valgrind_tool_list)["])
  138. AC_SUBST([valgrind_enabled_tools],[$valgrind_enabled_tools])
  139. [VALGRIND_CHECK_RULES='
  140. # Valgrind check
  141. #
  142. # Optional:
  143. # - VALGRIND_SUPPRESSIONS_FILES: Space-separated list of Valgrind suppressions
  144. # files to load. (Default: empty)
  145. # - VALGRIND_FLAGS: General flags to pass to all Valgrind tools.
  146. # (Default: --num-callers=30)
  147. # - VALGRIND_$toolname_FLAGS: Flags to pass to Valgrind $toolname (one of:
  148. # memcheck, helgrind, drd, sgcheck). (Default: various)
  149. # Optional variables
  150. VALGRIND_SUPPRESSIONS ?= $(addprefix --suppressions=,$(VALGRIND_SUPPRESSIONS_FILES))
  151. VALGRIND_FLAGS ?= --num-callers=30
  152. VALGRIND_memcheck_FLAGS ?= --leak-check=full --show-reachable=no
  153. VALGRIND_helgrind_FLAGS ?= --history-level=approx
  154. VALGRIND_drd_FLAGS ?=
  155. VALGRIND_sgcheck_FLAGS ?=
  156. # Internal use
  157. valgrind_log_files = $(addprefix test-suite-,$(addsuffix .log,$(valgrind_tools)))
  158. valgrind_memcheck_flags = --tool=memcheck $(VALGRIND_memcheck_FLAGS)
  159. valgrind_helgrind_flags = --tool=helgrind $(VALGRIND_helgrind_FLAGS)
  160. valgrind_drd_flags = --tool=drd $(VALGRIND_drd_FLAGS)
  161. valgrind_sgcheck_flags = --tool=exp-sgcheck $(VALGRIND_sgcheck_FLAGS)
  162. valgrind_quiet = $(valgrind_quiet_$(V))
  163. valgrind_quiet_ = $(valgrind_quiet_$(AM_DEFAULT_VERBOSITY))
  164. valgrind_quiet_0 = --quiet
  165. valgrind_v_use = $(valgrind_v_use_$(V))
  166. valgrind_v_use_ = $(valgrind_v_use_$(AM_DEFAULT_VERBOSITY))
  167. valgrind_v_use_0 = @echo " USE " $(patsubst check-valgrind-%-am,%,$''@):;
  168. # Support running with and without libtool.
  169. ifneq ($(LIBTOOL),)
  170. valgrind_lt = $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=execute
  171. else
  172. valgrind_lt =
  173. endif
  174. # Use recursive makes in order to ignore errors during check
  175. check-valgrind-am:
  176. ifeq ($(VALGRIND_ENABLED),yes)
  177. $(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) -k \
  178. $(foreach tool, $(valgrind_enabled_tools), check-valgrind-$(tool))
  179. else
  180. @echo "Need to reconfigure with --enable-valgrind"
  181. endif
  182. # Valgrind running
  183. VALGRIND_TESTS_ENVIRONMENT = \
  184. $(TESTS_ENVIRONMENT) \
  185. env VALGRIND=$(VALGRIND) \
  186. G_SLICE=always-malloc,debug-blocks \
  187. G_DEBUG=fatal-warnings,fatal-criticals,gc-friendly
  188. VALGRIND_LOG_COMPILER = \
  189. $(valgrind_lt) \
  190. $(VALGRIND) $(VALGRIND_SUPPRESSIONS) --error-exitcode=1 $(VALGRIND_FLAGS)
  191. define valgrind_tool_rule
  192. check-valgrind-$(1)-am:
  193. ifeq ($$(VALGRIND_ENABLED)-$$(ENABLE_VALGRIND_$(1)),yes-yes)
  194. ifneq ($$(TESTS),)
  195. $$(valgrind_v_use)$$(MAKE) check-TESTS \
  196. TESTS_ENVIRONMENT="$$(VALGRIND_TESTS_ENVIRONMENT)" \
  197. LOG_COMPILER="$$(VALGRIND_LOG_COMPILER)" \
  198. LOG_FLAGS="$$(valgrind_$(1)_flags)" \
  199. TEST_SUITE_LOG=test-suite-$(1).log
  200. endif
  201. else ifeq ($$(VALGRIND_ENABLED),yes)
  202. @echo "Need to reconfigure with --enable-valgrind-$(1)"
  203. else
  204. @echo "Need to reconfigure with --enable-valgrind"
  205. endif
  206. endef
  207. $(foreach tool,$(valgrind_tools),$(eval $(call valgrind_tool_rule,$(tool))))
  208. A''M_DISTCHECK_CONFIGURE_FLAGS ?=
  209. A''M_DISTCHECK_CONFIGURE_FLAGS += --disable-valgrind
  210. MOSTLYCLEANFILES ?=
  211. MOSTLYCLEANFILES += $(valgrind_log_files)
  212. .PHONY: check-valgrind $(add-prefix check-valgrind-,$(valgrind_tools))
  213. ']
  214. AC_SUBST([VALGRIND_CHECK_RULES])
  215. m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([VALGRIND_CHECK_RULES])])
  216. ])