You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
12 KiB

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