ax_code_coverage.m4 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_CFLAGS and CODE_COVERAGE_LDFLAGS which should be
  12. # included in the CFLAGS and LIBS/LDFLAGS variables of every build target
  13. # (program or library) which should be built with code coverage support.
  14. # Also defines CODE_COVERAGE_RULES which should be substituted in your
  15. # Makefile; and $enable_code_coverage which can be used in subsequent
  16. # configure output. CODE_COVERAGE_ENABLED is defined and substituted, and
  17. # corresponds to the value of the --enable-code-coverage option, which
  18. # defaults to being disabled.
  19. #
  20. # Test also for gcov program and create GCOV variable that could be
  21. # substituted.
  22. #
  23. # Note that all optimisation flags in CFLAGS must be disabled when code
  24. # coverage is enabled.
  25. #
  26. # Usage example:
  27. #
  28. # configure.ac:
  29. #
  30. # AX_CODE_COVERAGE
  31. #
  32. # Makefile.am:
  33. #
  34. # @CODE_COVERAGE_RULES@
  35. # my_program_LIBS = ... $(CODE_COVERAGE_LDFLAGS) ...
  36. # my_program_CFLAGS = ... $(CODE_COVERAGE_CFLAGS) ...
  37. #
  38. # This results in a "check-code-coverage" rule being added to any
  39. # Makefile.am which includes "@CODE_COVERAGE_RULES@" (assuming the module
  40. # has been configured with --enable-code-coverage). Running `make
  41. # check-code-coverage` in that directory will run the module's test suite
  42. # (`make check`) and build a code coverage report detailing the code which
  43. # was touched, then print the URI for the report.
  44. #
  45. # This code was derived from Makefile.decl in GLib, originally licenced
  46. # under LGPLv2.1+.
  47. #
  48. # LICENSE
  49. #
  50. # Copyright (c) 2012 Philip Withnall
  51. # Copyright (c) 2012 Xan Lopez
  52. # Copyright (c) 2012 Christian Persch
  53. # Copyright (c) 2012 Paolo Borelli
  54. # Copyright (c) 2012 Dan Winship
  55. # Copyright (c) 2015 Bastien ROUCARIES
  56. #
  57. # This library is free software; you can redistribute it and/or modify it
  58. # under the terms of the GNU Lesser General Public License as published by
  59. # the Free Software Foundation; either version 2.1 of the License, or (at
  60. # your option) any later version.
  61. #
  62. # This library is distributed in the hope that it will be useful, but
  63. # WITHOUT ANY WARRANTY; without even the implied warranty of
  64. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  65. # General Public License for more details.
  66. #
  67. # You should have received a copy of the GNU Lesser General Public License
  68. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  69. #serial 5
  70. AC_DEFUN([AX_CODE_COVERAGE],[
  71. dnl Check for --enable-code-coverage
  72. AC_REQUIRE([AC_PROG_SED])
  73. # allow to override gcov location
  74. AC_ARG_WITH([gcov],
  75. [AS_HELP_STRING([--with-gcov[=GCOV]], [use given GCOV for coverage (GCOV=gcov).])],
  76. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=$with_gcov],
  77. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=gcov])
  78. AC_MSG_CHECKING([whether to build with code coverage support])
  79. AC_ARG_ENABLE([code-coverage],
  80. AS_HELP_STRING([--enable-code-coverage],
  81. [Whether to enable code coverage support]),,
  82. enable_code_coverage=no)
  83. AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test x$enable_code_coverage = xyes])
  84. AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
  85. AC_MSG_RESULT($enable_code_coverage)
  86. AS_IF([ test "$enable_code_coverage" = "yes" ], [
  87. # check for gcov
  88. AC_CHECK_TOOL([GCOV],
  89. [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],
  90. [:])
  91. AS_IF([test "X$GCOV" = "X:"],
  92. [AC_MSG_ERROR([gcov is needed to do coverage])])
  93. AC_SUBST([GCOV])
  94. dnl Check if gcc is being used
  95. AS_IF([ test "$GCC" = "no" ], [
  96. AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
  97. ])
  98. # List of supported lcov versions.
  99. lcov_version_list="1.6 1.7 1.8 1.9 1.10 1.11 1.13"
  100. AC_CHECK_PROG([LCOV], [lcov], [lcov])
  101. AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
  102. AS_IF([ test "$LCOV" ], [
  103. AC_CACHE_CHECK([for lcov version], ax_cv_lcov_version, [
  104. ax_cv_lcov_version=invalid
  105. lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'`
  106. for lcov_check_version in $lcov_version_list; do
  107. if test "$lcov_version" = "$lcov_check_version"; then
  108. ax_cv_lcov_version="$lcov_check_version (ok)"
  109. fi
  110. done
  111. ])
  112. ], [
  113. lcov_msg="To enable code coverage reporting you must have one of the following lcov versions installed: $lcov_version_list"
  114. AC_MSG_ERROR([$lcov_msg])
  115. ])
  116. case $ax_cv_lcov_version in
  117. ""|invalid[)]
  118. lcov_msg="You must have one of the following versions of lcov: $lcov_version_list (found: $lcov_version)."
  119. AC_MSG_ERROR([$lcov_msg])
  120. LCOV="exit 0;"
  121. ;;
  122. esac
  123. AS_IF([ test -z "$GENHTML" ], [
  124. AC_MSG_ERROR([Could not find genhtml from the lcov package])
  125. ])
  126. dnl Build the code coverage flags
  127. CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
  128. CODE_COVERAGE_LDFLAGS="-lgcov"
  129. AC_SUBST([CODE_COVERAGE_CFLAGS])
  130. AC_SUBST([CODE_COVERAGE_LDFLAGS])
  131. CODE_COVERAGE_RULES='
  132. # Code coverage
  133. #
  134. # Optional:
  135. # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
  136. # (Default: $(top_builddir))
  137. # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
  138. # by lcov for code coverage. (Default:
  139. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info)
  140. # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
  141. # reports to be created. (Default:
  142. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage)
  143. # - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov
  144. # - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the lcov instance.
  145. # (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  146. # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the lcov instance.
  147. # (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  148. # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
  149. # instance. (Default: empty)
  150. # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
  151. #
  152. # The generated report will be titled using the $(PACKAGE_NAME) and
  153. # $(PACKAGE_VERSION). In order to add the current git hash to the title,
  154. # use the git-version-gen script, available online.
  155. # Optional variables
  156. CODE_COVERAGE_DIRECTORY ?= $(top_builddir)
  157. CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info
  158. CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage
  159. CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool "$(GCOV)"
  160. CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= $(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  161. CODE_COVERAGE_LCOV_OPTIONS ?= $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  162. CODE_COVERAGE_GENHTML_OPTIONS ?=
  163. CODE_COVERAGE_IGNORE_PATTERN ?=
  164. code_coverage_quiet = $(code_coverage_quiet_$(V))
  165. code_coverage_quiet_ =
  166. code_coverage_quiet_0 = --quiet
  167. # Use recursive makes in order to ignore errors during check
  168. check-code-coverage:
  169. -$(MAKE) $(AM_MAKEFLAGS) -k check
  170. $(MAKE) $(AM_MAKEFLAGS) code-coverage-capture
  171. # Capture code coverage data
  172. code-coverage-capture: code-coverage-capture-hook
  173. $(LCOV) $(code_coverage_quiet) --directory $(CODE_COVERAGE_DIRECTORY) --capture --output-file "$(CODE_COVERAGE_OUTPUT_FILE).tmp" --test-name "$(PACKAGE_NAME)-$(PACKAGE_VERSION)" --no-checksum --compat-libtool $(CODE_COVERAGE_LCOV_OPTIONS)
  174. $(LCOV) $(code_coverage_quiet) --directory $(CODE_COVERAGE_DIRECTORY) --remove "$(CODE_COVERAGE_OUTPUT_FILE).tmp" "/tmp/*" $(CODE_COVERAGE_IGNORE_PATTERN) --output-file "$(CODE_COVERAGE_OUTPUT_FILE)"
  175. -@rm -f $(CODE_COVERAGE_OUTPUT_FILE).tmp
  176. LANG=C $(GENHTML) $(code_coverage_quiet) --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)
  177. @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html"
  178. # Hook rule executed before code-coverage-capture, overridable by the user
  179. code-coverage-capture-hook:
  180. clean: code-coverage-clean
  181. code-coverage-clean:
  182. -$(LCOV) --directory $(top_builddir) -z
  183. -rm -rf $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_FILE).tmp $(CODE_COVERAGE_OUTPUT_DIRECTORY)
  184. -find . -name "*.gcda" -o -name "*.gcov" -delete
  185. GITIGNOREFILES ?=
  186. GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY)
  187. DISTCHECK_CONFIGURE_FLAGS ?=
  188. DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage
  189. .PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean
  190. '
  191. AC_SUBST([CODE_COVERAGE_RULES])
  192. ])
  193. m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([CODE_COVERAGE_RULES])])
  194. ])