ax_code_coverage.m4 12 KB

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