ax_code_coverage.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # ===========================================================================
  2. # http://www.gnu.org/software/autoconf-archive/ax_code_coverage.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_CODE_COVERAGE()
  8. #
  9. # DESCRIPTION
  10. #
  11. # Defines CODE_COVERAGE_CPPFLAGS, CODE_COVERAGE_CFLAGS,
  12. # CODE_COVERAGE_CXXFLAGS and CODE_COVERAGE_LIBS which should be included
  13. # in the CPPFLAGS, CFLAGS CXXFLAGS and LIBS/LIBADD variables of every
  14. # build target (program or library) which should be built with code
  15. # coverage support. Also defines CODE_COVERAGE_RULES which should be
  16. # substituted in your Makefile; and $enable_code_coverage which can be
  17. # used in subsequent configure output. CODE_COVERAGE_ENABLED is defined
  18. # and substituted, and corresponds to the value of the
  19. # --enable-code-coverage option, which defaults to being disabled.
  20. #
  21. # Test also for gcov program and create GCOV variable that could be
  22. # substituted.
  23. #
  24. # Note that all optimisation flags in CFLAGS must be disabled when code
  25. # coverage is enabled.
  26. #
  27. # Usage example:
  28. #
  29. # configure.ac:
  30. #
  31. # AX_CODE_COVERAGE
  32. #
  33. # Makefile.am:
  34. #
  35. # @CODE_COVERAGE_RULES@
  36. # my_program_LIBS = ... $(CODE_COVERAGE_LIBS) ...
  37. # my_program_CPPFLAGS = ... $(CODE_COVERAGE_CPPFLAGS) ...
  38. # my_program_CFLAGS = ... $(CODE_COVERAGE_CFLAGS) ...
  39. # my_program_CXXFLAGS = ... $(CODE_COVERAGE_CXXFLAGS) ...
  40. #
  41. # This results in a "check-code-coverage" rule being added to any
  42. # Makefile.am which includes "@CODE_COVERAGE_RULES@" (assuming the module
  43. # has been configured with --enable-code-coverage). Running `make
  44. # check-code-coverage` in that directory will run the module's test suite
  45. # (`make check`) and build a code coverage report detailing the code which
  46. # was touched, then print the URI for the report.
  47. #
  48. # In earlier versions of this macro, CODE_COVERAGE_LDFLAGS was defined
  49. # instead of CODE_COVERAGE_LIBS. They are both still defined, but use of
  50. # CODE_COVERAGE_LIBS is preferred for clarity; CODE_COVERAGE_LDFLAGS is
  51. # deprecated. They have the same value.
  52. #
  53. # This code was derived from Makefile.decl in GLib, originally licenced
  54. # under LGPLv2.1+.
  55. #
  56. # LICENSE
  57. #
  58. # Copyright (c) 2012, 2016 Philip Withnall
  59. # Copyright (c) 2012 Xan Lopez
  60. # Copyright (c) 2012 Christian Persch
  61. # Copyright (c) 2012 Paolo Borelli
  62. # Copyright (c) 2012 Dan Winship
  63. # Copyright (c) 2015 Bastien ROUCARIES
  64. #
  65. # This library is free software; you can redistribute it and/or modify it
  66. # under the terms of the GNU Lesser General Public License as published by
  67. # the Free Software Foundation; either version 3 of the License, or (at
  68. # your option) any later version.
  69. #
  70. # This library is distributed in the hope that it will be useful, but
  71. # WITHOUT ANY WARRANTY; without even the implied warranty of
  72. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  73. # General Public License for more details.
  74. #
  75. # You should have received a copy of the GNU Lesser General Public License
  76. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  77. #serial 17
  78. AC_DEFUN([AX_CODE_COVERAGE],[
  79. dnl Check for --enable-code-coverage
  80. AC_REQUIRE([AC_PROG_SED])
  81. # allow to override gcov location
  82. AC_ARG_WITH([gcov],
  83. [AS_HELP_STRING([--with-gcov[=GCOV]], [use given GCOV for coverage (GCOV=gcov).])],
  84. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=$with_gcov],
  85. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=gcov])
  86. AC_MSG_CHECKING([whether to build with code coverage support])
  87. AC_ARG_ENABLE([code-coverage],
  88. AS_HELP_STRING([--enable-code-coverage],
  89. [Whether to enable code coverage support]),,
  90. enable_code_coverage=no)
  91. AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test x$enable_code_coverage = xyes])
  92. AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
  93. AC_MSG_RESULT($enable_code_coverage)
  94. AS_IF([ test "$enable_code_coverage" = "yes" ], [
  95. # check for gcov
  96. AC_CHECK_TOOL([GCOV],
  97. [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],
  98. [:])
  99. AS_IF([test "X$GCOV" = "X:"],
  100. [AC_MSG_ERROR([gcov is needed to do coverage])])
  101. AC_SUBST([GCOV])
  102. dnl Check if gcc is being used
  103. AS_IF([ test "$GCC" = "no" ], [
  104. AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
  105. ])
  106. # List of supported lcov versions.
  107. lcov_version_list="1.6 1.7 1.8 1.9 1.10 1.11 1.12"
  108. AC_CHECK_PROG([LCOV], [lcov], [lcov])
  109. AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
  110. AS_IF([ test "$LCOV" ], [
  111. AC_CACHE_CHECK([for lcov version], ax_cv_lcov_version, [
  112. ax_cv_lcov_version=invalid
  113. lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'`
  114. for lcov_check_version in $lcov_version_list; do
  115. if test "$lcov_version" = "$lcov_check_version"; then
  116. ax_cv_lcov_version="$lcov_check_version (ok)"
  117. fi
  118. done
  119. ])
  120. ], [
  121. lcov_msg="To enable code coverage reporting you must have one of the following lcov versions installed: $lcov_version_list"
  122. AC_MSG_ERROR([$lcov_msg])
  123. ])
  124. case $ax_cv_lcov_version in
  125. ""|invalid[)]
  126. lcov_msg="You must have one of the following versions of lcov: $lcov_version_list (found: $lcov_version)."
  127. AC_MSG_ERROR([$lcov_msg])
  128. LCOV="exit 0;"
  129. ;;
  130. esac
  131. AS_IF([ test -z "$GENHTML" ], [
  132. AC_MSG_ERROR([Could not find genhtml from the lcov package])
  133. ])
  134. dnl Build the code coverage flags
  135. dnl Define CODE_COVERAGE_LDFLAGS for backwards compatibility
  136. CODE_COVERAGE_CPPFLAGS="-DNDEBUG"
  137. CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
  138. CODE_COVERAGE_CXXFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
  139. CODE_COVERAGE_LDFLAGS="-lgcov"
  140. CODE_COVERAGE_LDFLAGS="$CODE_COVERAGE_LIBS"
  141. AC_SUBST([CODE_COVERAGE_CPPFLAGS])
  142. AC_SUBST([CODE_COVERAGE_CFLAGS])
  143. AC_SUBST([CODE_COVERAGE_CXXFLAGS])
  144. AC_SUBST([CODE_COVERAGE_LIBS])
  145. AC_SUBST([CODE_COVERAGE_LDFLAGS])
  146. ])
  147. [CODE_COVERAGE_RULES='
  148. # Code coverage
  149. #
  150. # Optional:
  151. # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
  152. # Multiple directories may be specified, separated by whitespace.
  153. # (Default: $(top_builddir))
  154. # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
  155. # by lcov for code coverage. (Default:
  156. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info)
  157. # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
  158. # reports to be created. (Default:
  159. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage)
  160. # - CODE_COVERAGE_BRANCH_COVERAGE: Set to 1 to enforce branch coverage,
  161. # set to 0 to disable it and leave empty to stay with the default.
  162. # (Default: empty)
  163. # - CODE_COVERAGE_LCOV_SHOPTS_DEFAULT: Extra options shared between both lcov
  164. # instances. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
  165. # - CODE_COVERAGE_LCOV_SHOPTS: Extra options to shared between both lcov
  166. # instances. (Default: $CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
  167. # - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov
  168. # - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the
  169. # collecting lcov instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  170. # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the collecting lcov
  171. # instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  172. # - CODE_COVERAGE_LCOV_RMOPTS_DEFAULT: Extra options to pass to the filtering
  173. # lcov instance. (Default: empty)
  174. # - CODE_COVERAGE_LCOV_RMOPTS: Extra options to pass to the filtering lcov
  175. # instance. (Default: $CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
  176. # - CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT: Extra options to pass to the
  177. # genhtml instance. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
  178. # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
  179. # instance. (Default: $CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
  180. # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
  181. #
  182. # The generated report will be titled using the $(PACKAGE_NAME) and
  183. # $(PACKAGE_VERSION). In order to add the current git hash to the title,
  184. # use the git-version-gen script, available online.
  185. # Optional variables
  186. CODE_COVERAGE_DIRECTORY ?= $(top_builddir)
  187. CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info
  188. CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage
  189. CODE_COVERAGE_BRANCH_COVERAGE ?=
  190. CODE_COVERAGE_LCOV_SHOPTS_DEFAULT ?= $(if $(CODE_COVERAGE_BRANCH_COVERAGE),\
  191. --rc lcov_branch_coverage=$(CODE_COVERAGE_BRANCH_COVERAGE))
  192. CODE_COVERAGE_LCOV_SHOPTS ?= $(CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
  193. CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool "$(GCOV)"
  194. CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= $(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  195. CODE_COVERAGE_LCOV_OPTIONS ?= $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  196. CODE_COVERAGE_LCOV_RMOPTS_DEFAULT ?=
  197. CODE_COVERAGE_LCOV_RMOPTS ?= $(CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
  198. CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT ?=\
  199. $(if $(CODE_COVERAGE_BRANCH_COVERAGE),\
  200. --rc genhtml_branch_coverage=$(CODE_COVERAGE_BRANCH_COVERAGE))
  201. CODE_COVERAGE_GENHTML_OPTIONS ?= $(CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
  202. CODE_COVERAGE_IGNORE_PATTERN ?=
  203. code_coverage_v_lcov_cap = $(code_coverage_v_lcov_cap_$(V))
  204. code_coverage_v_lcov_cap_ = $(code_coverage_v_lcov_cap_$(AM_DEFAULT_VERBOSITY))
  205. code_coverage_v_lcov_cap_0 = @echo " LCOV --capture"\
  206. $(CODE_COVERAGE_OUTPUT_FILE);
  207. code_coverage_v_lcov_ign = $(code_coverage_v_lcov_ign_$(V))
  208. code_coverage_v_lcov_ign_ = $(code_coverage_v_lcov_ign_$(AM_DEFAULT_VERBOSITY))
  209. code_coverage_v_lcov_ign_0 = @echo " LCOV --remove /tmp/*"\
  210. $(CODE_COVERAGE_IGNORE_PATTERN);
  211. code_coverage_v_genhtml = $(code_coverage_v_genhtml_$(V))
  212. code_coverage_v_genhtml_ = $(code_coverage_v_genhtml_$(AM_DEFAULT_VERBOSITY))
  213. code_coverage_v_genhtml_0 = @echo " GEN " $(CODE_COVERAGE_OUTPUT_DIRECTORY);
  214. code_coverage_quiet = $(code_coverage_quiet_$(V))
  215. code_coverage_quiet_ = $(code_coverage_quiet_$(AM_DEFAULT_VERBOSITY))
  216. code_coverage_quiet_0 = --quiet
  217. # sanitizes the test-name: replaces with underscores: dashes and dots
  218. code_coverage_sanitize = $(subst -,_,$(subst .,_,$(1)))
  219. # Use recursive makes in order to ignore errors during check
  220. check-code-coverage:
  221. ifeq ($(CODE_COVERAGE_ENABLED),yes)
  222. -$(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) -k check
  223. $(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) code-coverage-capture
  224. else
  225. @echo "Need to reconfigure with --enable-code-coverage"
  226. endif
  227. # Capture code coverage data
  228. code-coverage-capture: code-coverage-capture-hook
  229. ifeq ($(CODE_COVERAGE_ENABLED),yes)
  230. $(code_coverage_v_lcov_cap)$(LCOV) $(code_coverage_quiet) $(addprefix --directory ,$(CODE_COVERAGE_DIRECTORY)) --capture --output-file "$(CODE_COVERAGE_OUTPUT_FILE).tmp" --test-name "$(call code_coverage_sanitize,$(PACKAGE_NAME)-$(PACKAGE_VERSION))" --no-checksum --compat-libtool $(CODE_COVERAGE_LCOV_SHOPTS) $(CODE_COVERAGE_LCOV_OPTIONS)
  231. $(code_coverage_v_lcov_ign)$(LCOV) $(code_coverage_quiet) $(addprefix --directory ,$(CODE_COVERAGE_DIRECTORY)) --remove "$(CODE_COVERAGE_OUTPUT_FILE).tmp" "/tmp/*" $(CODE_COVERAGE_IGNORE_PATTERN) --output-file "$(CODE_COVERAGE_OUTPUT_FILE)" $(CODE_COVERAGE_LCOV_SHOPTS) $(CODE_COVERAGE_LCOV_RMOPTS)
  232. -@rm -f "$(CODE_COVERAGE_OUTPUT_FILE).tmp"
  233. $(code_coverage_v_genhtml)LANG=C $(GENHTML) $(code_coverage_quiet) $(addprefix --prefix ,$(CODE_COVERAGE_DIRECTORY)) --output-directory "$(CODE_COVERAGE_OUTPUT_DIRECTORY)" --title "$(PACKAGE_NAME)-$(PACKAGE_VERSION) Code Coverage" --legend --show-details "$(CODE_COVERAGE_OUTPUT_FILE)" $(CODE_COVERAGE_GENHTML_OPTIONS)
  234. @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html"
  235. else
  236. @echo "Need to reconfigure with --enable-code-coverage"
  237. endif
  238. # Hook rule executed before code-coverage-capture, overridable by the user
  239. code-coverage-capture-hook:
  240. ifeq ($(CODE_COVERAGE_ENABLED),yes)
  241. clean: code-coverage-clean
  242. distclean: code-coverage-clean
  243. code-coverage-clean:
  244. -$(LCOV) --directory $(top_builddir) -z
  245. -rm -rf "$(CODE_COVERAGE_OUTPUT_FILE)" "$(CODE_COVERAGE_OUTPUT_FILE).tmp" "$(CODE_COVERAGE_OUTPUT_DIRECTORY)"
  246. -find . \( -name "*.gcda" -o -name "*.gcno" -o -name "*.gcov" \) -delete
  247. endif
  248. GITIGNOREFILES ?=
  249. GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY)
  250. A''M_DISTCHECK_CONFIGURE_FLAGS ?=
  251. A''M_DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage
  252. .PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean
  253. ']
  254. AC_SUBST([CODE_COVERAGE_RULES])
  255. m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([CODE_COVERAGE_RULES])])
  256. ])