No Description
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.

ax_code_coverage.m4 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # ===========================================================================
  2. # https://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 optimization 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 2.1 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 <https://www.gnu.org/licenses/>.
  77. #
  78. # SPDX-License-Identifier: LGPL-2.1+
  79. #
  80. #serial 25
  81. AC_DEFUN([AX_CODE_COVERAGE],[
  82. dnl Check for --enable-code-coverage
  83. AC_REQUIRE([AC_PROG_SED])
  84. # allow to override gcov location
  85. AC_ARG_WITH([gcov],
  86. [AS_HELP_STRING([--with-gcov[=GCOV]], [use given GCOV for coverage (GCOV=gcov).])],
  87. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=$with_gcov],
  88. [_AX_CODE_COVERAGE_GCOV_PROG_WITH=gcov])
  89. AC_MSG_CHECKING([whether to build with code coverage support])
  90. AC_ARG_ENABLE([code-coverage],
  91. AS_HELP_STRING([--enable-code-coverage],
  92. [Whether to enable code coverage support]),,
  93. enable_code_coverage=no)
  94. AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test x$enable_code_coverage = xyes])
  95. AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
  96. AC_MSG_RESULT($enable_code_coverage)
  97. AS_IF([ test "$enable_code_coverage" = "yes" ], [
  98. # check for gcov
  99. AC_CHECK_TOOL([GCOV],
  100. [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],
  101. [:])
  102. AS_IF([test "X$GCOV" = "X:"],
  103. [AC_MSG_ERROR([gcov is needed to do coverage])])
  104. AC_SUBST([GCOV])
  105. dnl Check if gcc is being used
  106. AS_IF([ test "$GCC" = "no" ], [
  107. AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
  108. ])
  109. AC_CHECK_PROG([LCOV], [lcov], [lcov])
  110. AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
  111. AS_IF([ test -z "$LCOV" ], [
  112. AC_MSG_ERROR([To enable code coverage reporting you must have lcov installed])
  113. ])
  114. AS_IF([ test -z "$GENHTML" ], [
  115. AC_MSG_ERROR([Could not find genhtml from the lcov package])
  116. ])
  117. dnl Build the code coverage flags
  118. dnl Define CODE_COVERAGE_LDFLAGS for backwards compatibility
  119. CODE_COVERAGE_CPPFLAGS="-DNDEBUG"
  120. CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
  121. CODE_COVERAGE_CXXFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
  122. CODE_COVERAGE_LIBS="-lgcov"
  123. CODE_COVERAGE_LDFLAGS="$CODE_COVERAGE_LIBS"
  124. AC_SUBST([CODE_COVERAGE_CPPFLAGS])
  125. AC_SUBST([CODE_COVERAGE_CFLAGS])
  126. AC_SUBST([CODE_COVERAGE_CXXFLAGS])
  127. AC_SUBST([CODE_COVERAGE_LIBS])
  128. AC_SUBST([CODE_COVERAGE_LDFLAGS])
  129. [CODE_COVERAGE_RULES_CHECK='
  130. -$(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) -k check
  131. $(A''M_V_at)$(MAKE) $(AM_MAKEFLAGS) code-coverage-capture
  132. ']
  133. [CODE_COVERAGE_RULES_CAPTURE='
  134. $(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)
  135. $(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)
  136. -@rm -f $(CODE_COVERAGE_OUTPUT_FILE).tmp
  137. $(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)
  138. @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html"
  139. ']
  140. [CODE_COVERAGE_RULES_CLEAN='
  141. clean: code-coverage-clean
  142. distclean: code-coverage-clean
  143. code-coverage-clean:
  144. -$(LCOV) --directory $(top_builddir) -z
  145. -rm -rf $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_FILE).tmp $(CODE_COVERAGE_OUTPUT_DIRECTORY)
  146. -find . \( -name "*.gcda" -o -name "*.gcno" -o -name "*.gcov" \) -delete
  147. ']
  148. ], [
  149. [CODE_COVERAGE_RULES_CHECK='
  150. @echo "Need to reconfigure with --enable-code-coverage"
  151. ']
  152. CODE_COVERAGE_RULES_CAPTURE="$CODE_COVERAGE_RULES_CHECK"
  153. CODE_COVERAGE_RULES_CLEAN=''
  154. ])
  155. [CODE_COVERAGE_RULES='
  156. # Code coverage
  157. #
  158. # Optional:
  159. # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
  160. # Multiple directories may be specified, separated by whitespace.
  161. # (Default: $(top_builddir))
  162. # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
  163. # by lcov for code coverage. (Default:
  164. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info)
  165. # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
  166. # reports to be created. (Default:
  167. # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage)
  168. # - CODE_COVERAGE_BRANCH_COVERAGE: Set to 1 to enforce branch coverage,
  169. # set to 0 to disable it and leave empty to stay with the default.
  170. # (Default: empty)
  171. # - CODE_COVERAGE_LCOV_SHOPTS_DEFAULT: Extra options shared between both lcov
  172. # instances. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
  173. # - CODE_COVERAGE_LCOV_SHOPTS: Extra options to shared between both lcov
  174. # instances. (Default: $CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
  175. # - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov
  176. # - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the
  177. # collecting lcov instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  178. # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the collecting lcov
  179. # instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  180. # - CODE_COVERAGE_LCOV_RMOPTS_DEFAULT: Extra options to pass to the filtering
  181. # lcov instance. (Default: empty)
  182. # - CODE_COVERAGE_LCOV_RMOPTS: Extra options to pass to the filtering lcov
  183. # instance. (Default: $CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
  184. # - CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT: Extra options to pass to the
  185. # genhtml instance. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
  186. # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
  187. # instance. (Default: $CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
  188. # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
  189. #
  190. # The generated report will be titled using the $(PACKAGE_NAME) and
  191. # $(PACKAGE_VERSION). In order to add the current git hash to the title,
  192. # use the git-version-gen script, available online.
  193. # Optional variables
  194. CODE_COVERAGE_DIRECTORY ?= $(top_builddir)
  195. CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info
  196. CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage
  197. CODE_COVERAGE_BRANCH_COVERAGE ?=
  198. CODE_COVERAGE_LCOV_SHOPTS_DEFAULT ?= $(if $(CODE_COVERAGE_BRANCH_COVERAGE),\
  199. --rc lcov_branch_coverage=$(CODE_COVERAGE_BRANCH_COVERAGE))
  200. CODE_COVERAGE_LCOV_SHOPTS ?= $(CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
  201. CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool "$(GCOV)"
  202. CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= $(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
  203. CODE_COVERAGE_LCOV_OPTIONS ?= $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
  204. CODE_COVERAGE_LCOV_RMOPTS_DEFAULT ?=
  205. CODE_COVERAGE_LCOV_RMOPTS ?= $(CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
  206. CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT ?=\
  207. $(if $(CODE_COVERAGE_BRANCH_COVERAGE),\
  208. --rc genhtml_branch_coverage=$(CODE_COVERAGE_BRANCH_COVERAGE))
  209. CODE_COVERAGE_GENHTML_OPTIONS ?= $(CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
  210. CODE_COVERAGE_IGNORE_PATTERN ?=
  211. GITIGNOREFILES ?=
  212. GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY)
  213. code_coverage_v_lcov_cap = $(code_coverage_v_lcov_cap_$(V))
  214. code_coverage_v_lcov_cap_ = $(code_coverage_v_lcov_cap_$(AM_DEFAULT_VERBOSITY))
  215. code_coverage_v_lcov_cap_0 = @echo " LCOV --capture"\
  216. $(CODE_COVERAGE_OUTPUT_FILE);
  217. code_coverage_v_lcov_ign = $(code_coverage_v_lcov_ign_$(V))
  218. code_coverage_v_lcov_ign_ = $(code_coverage_v_lcov_ign_$(AM_DEFAULT_VERBOSITY))
  219. code_coverage_v_lcov_ign_0 = @echo " LCOV --remove /tmp/*"\
  220. $(CODE_COVERAGE_IGNORE_PATTERN);
  221. code_coverage_v_genhtml = $(code_coverage_v_genhtml_$(V))
  222. code_coverage_v_genhtml_ = $(code_coverage_v_genhtml_$(AM_DEFAULT_VERBOSITY))
  223. code_coverage_v_genhtml_0 = @echo " GEN " $(CODE_COVERAGE_OUTPUT_DIRECTORY);
  224. code_coverage_quiet = $(code_coverage_quiet_$(V))
  225. code_coverage_quiet_ = $(code_coverage_quiet_$(AM_DEFAULT_VERBOSITY))
  226. code_coverage_quiet_0 = --quiet
  227. # sanitizes the test-name: replaces with underscores: dashes and dots
  228. code_coverage_sanitize = $(subst -,_,$(subst .,_,$(1)))
  229. # Use recursive makes in order to ignore errors during check
  230. check-code-coverage:'"$CODE_COVERAGE_RULES_CHECK"'
  231. # Capture code coverage data
  232. code-coverage-capture: code-coverage-capture-hook'"$CODE_COVERAGE_RULES_CAPTURE"'
  233. # Hook rule executed before code-coverage-capture, overridable by the user
  234. code-coverage-capture-hook:
  235. '"$CODE_COVERAGE_RULES_CLEAN"'
  236. A''M_DISTCHECK_CONFIGURE_FLAGS ?=
  237. A''M_DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage
  238. .PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean
  239. ']
  240. AC_SUBST([CODE_COVERAGE_RULES])
  241. m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([CODE_COVERAGE_RULES])])
  242. ])