1 # ===========================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_code_coverage.html
3 # ===========================================================================
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
21 # Test also for gcov program and create GCOV variable that could be
24 # Note that all optimization flags in CFLAGS must be disabled when code
25 # coverage is enabled.
35 # include $(top_srcdir)/aminclude_static.am
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) ...
42 # clean-local: code-coverage-clean
43 # distclean-local: code-coverage-dist-clean
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.
52 # This code was derived from Makefile.decl in GLib, originally licensed
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
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.
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.
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/>.
79 m4_define(_AX_CODE_COVERAGE_RULES,[
80 AX_ADD_AM_MACRO_STATIC([
84 # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
85 # Multiple directories may be specified, separated by whitespace.
86 # (Default: \$(top_builddir))
87 # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
88 # by lcov for code coverage. (Default:
89 # \$(PACKAGE_NAME)-\$(PACKAGE_VERSION)-coverage.info)
90 # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
91 # reports to be created. (Default:
92 # \$(PACKAGE_NAME)-\$(PACKAGE_VERSION)-coverage)
93 # - CODE_COVERAGE_BRANCH_COVERAGE: Set to 1 to enforce branch coverage,
94 # set to 0 to disable it and leave empty to stay with the default.
96 # - CODE_COVERAGE_LCOV_SHOPTS_DEFAULT: Extra options shared between both lcov
97 # instances. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
98 # - CODE_COVERAGE_LCOV_SHOPTS: Extra options to shared between both lcov
99 # instances. (Default: $CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
100 # - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov
101 # - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the
102 # collecting lcov instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
103 # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the collecting lcov
104 # instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
105 # - CODE_COVERAGE_LCOV_RMOPTS_DEFAULT: Extra options to pass to the filtering
106 # lcov instance. (Default: empty)
107 # - CODE_COVERAGE_LCOV_RMOPTS: Extra options to pass to the filtering lcov
108 # instance. (Default: $CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
109 # - CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT: Extra options to pass to the
110 # genhtml instance. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)
111 # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
112 # instance. (Default: $CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
113 # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
115 # The generated report will be titled using the \$(PACKAGE_NAME) and
116 # \$(PACKAGE_VERSION). In order to add the current git hash to the title,
117 # use the git-version-gen script, available online.
119 # run only on top dir
120 if CODE_COVERAGE_ENABLED
121 ifeq (\$(abs_builddir), \$(abs_top_builddir))
122 CODE_COVERAGE_DIRECTORY ?= \$(top_builddir)
123 CODE_COVERAGE_OUTPUT_FILE ?= \$(PACKAGE_NAME)-\$(PACKAGE_VERSION)-coverage.info
124 CODE_COVERAGE_OUTPUT_DIRECTORY ?= \$(PACKAGE_NAME)-\$(PACKAGE_VERSION)-coverage
126 CODE_COVERAGE_BRANCH_COVERAGE ?=
127 CODE_COVERAGE_LCOV_SHOPTS_DEFAULT ?= \$(if \$(CODE_COVERAGE_BRANCH_COVERAGE),\
128 --rc lcov_branch_coverage=\$(CODE_COVERAGE_BRANCH_COVERAGE))
129 CODE_COVERAGE_LCOV_SHOPTS ?= \$(CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
130 CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool \"\$(GCOV)\"
131 CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= \$(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
132 CODE_COVERAGE_LCOV_OPTIONS ?= \$(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
133 CODE_COVERAGE_LCOV_RMOPTS_DEFAULT ?=
134 CODE_COVERAGE_LCOV_RMOPTS ?= \$(CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
135 CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT ?=\
136 \$(if \$(CODE_COVERAGE_BRANCH_COVERAGE),\
137 --rc genhtml_branch_coverage=\$(CODE_COVERAGE_BRANCH_COVERAGE))
138 CODE_COVERAGE_GENHTML_OPTIONS ?= \$(CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
139 CODE_COVERAGE_IGNORE_PATTERN ?=
141 GITIGNOREFILES = \$(GITIGNOREFILES) \$(CODE_COVERAGE_OUTPUT_FILE) \$(CODE_COVERAGE_OUTPUT_DIRECTORY)
142 code_coverage_v_lcov_cap = \$(code_coverage_v_lcov_cap_\$(V))
143 code_coverage_v_lcov_cap_ = \$(code_coverage_v_lcov_cap_\$(AM_DEFAULT_VERBOSITY))
144 code_coverage_v_lcov_cap_0 = @echo \" LCOV --capture\" \$(CODE_COVERAGE_OUTPUT_FILE);
145 code_coverage_v_lcov_ign = \$(code_coverage_v_lcov_ign_\$(V))
146 code_coverage_v_lcov_ign_ = \$(code_coverage_v_lcov_ign_\$(AM_DEFAULT_VERBOSITY))
147 code_coverage_v_lcov_ign_0 = @echo \" LCOV --remove /tmp/*\" \$(CODE_COVERAGE_IGNORE_PATTERN);
148 code_coverage_v_genhtml = \$(code_coverage_v_genhtml_\$(V))
149 code_coverage_v_genhtml_ = \$(code_coverage_v_genhtml_\$(AM_DEFAULT_VERBOSITY))
150 code_coverage_v_genhtml_0 = @echo \" GEN \" \"\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\";
151 code_coverage_quiet = \$(code_coverage_quiet_\$(V))
152 code_coverage_quiet_ = \$(code_coverage_quiet_\$(AM_DEFAULT_VERBOSITY))
153 code_coverage_quiet_0 = --quiet
155 # sanitizes the test-name: replaces with underscores: dashes and dots
156 code_coverage_sanitize = \$(subst -,_,\$(subst .,_,\$(1)))
158 # Use recursive makes in order to ignore errors during check
160 -\$(AM_V_at)\$(MAKE) \$(AM_MAKEFLAGS) -k check
161 \$(AM_V_at)\$(MAKE) \$(AM_MAKEFLAGS) code-coverage-capture
163 # Capture code coverage data
164 code-coverage-capture: code-coverage-capture-hook
165 \$(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)
166 \$(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)
167 -@rm -f \"\$(CODE_COVERAGE_OUTPUT_FILE).tmp\"
168 \$(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)
169 @echo \"file://\$(abs_builddir)/\$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html\"
172 -\$(LCOV) --directory \$(top_builddir) -z
173 -rm -rf \"\$(CODE_COVERAGE_OUTPUT_FILE)\" \"\$(CODE_COVERAGE_OUTPUT_FILE).tmp\" \"\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\"
174 -find . \\( -name \"*.gcda\" -o -name \"*.gcno\" -o -name \"*.gcov\" \\) -delete
176 code-coverage-dist-clean:
178 A][M_DISTCHECK_CONFIGURE_FLAGS = \$(A][M_DISTCHECK_CONFIGURE_FLAGS) --disable-code-coverage
179 else # ifneq (\$(abs_builddir), \$(abs_top_builddir))
182 code-coverage-capture: code-coverage-capture-hook
186 code-coverage-dist-clean:
187 endif # ifeq (\$(abs_builddir), \$(abs_top_builddir))
188 else #! CODE_COVERAGE_ENABLED
189 # Use recursive makes in order to ignore errors during check
191 @echo \"Need to reconfigure with --enable-code-coverage\"
192 # Capture code coverage data
193 code-coverage-capture: code-coverage-capture-hook
194 @echo \"Need to reconfigure with --enable-code-coverage\"
198 code-coverage-dist-clean:
200 endif #CODE_COVERAGE_ENABLED
201 # Hook rule executed before code-coverage-capture, overridable by the user
202 code-coverage-capture-hook:
204 .PHONY: check-code-coverage code-coverage-capture code-coverage-dist-clean code-coverage-clean code-coverage-capture-hook
208 AC_DEFUN([_AX_CODE_COVERAGE_ENABLED],[
209 AX_CHECK_GNU_MAKE([],[AC_MSG_ERROR([not using GNU make that is needed for coverage])])
210 AC_REQUIRE([AX_ADD_AM_MACRO_STATIC])
212 AC_CHECK_TOOL([GCOV],
213 [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],
215 AS_IF([test "X$GCOV" = "X:"],
216 [AC_MSG_ERROR([gcov is needed to do coverage])])
219 dnl Check if gcc is being used
220 AS_IF([ test "$GCC" = "no" ], [
221 AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
224 AC_CHECK_PROG([LCOV], [lcov], [lcov])
225 AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
227 AS_IF([ test x"$LCOV" = x ], [
228 AC_MSG_ERROR([To enable code coverage reporting you must have lcov installed])
231 AS_IF([ test x"$GENHTML" = x ], [
232 AC_MSG_ERROR([Could not find genhtml from the lcov package])
235 dnl Build the code coverage flags
236 dnl Define CODE_COVERAGE_LDFLAGS for backwards compatibility
237 CODE_COVERAGE_CPPFLAGS="-DNDEBUG"
238 CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
239 CODE_COVERAGE_CXXFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
240 CODE_COVERAGE_LIBS="-lgcov"
242 AC_SUBST([CODE_COVERAGE_CPPFLAGS])
243 AC_SUBST([CODE_COVERAGE_CFLAGS])
244 AC_SUBST([CODE_COVERAGE_CXXFLAGS])
245 AC_SUBST([CODE_COVERAGE_LIBS])
248 AC_DEFUN([AX_CODE_COVERAGE],[
249 dnl Check for --enable-code-coverage
251 # allow to override gcov location
253 [AS_HELP_STRING([--with-gcov[=GCOV]], [use given GCOV for coverage (GCOV=gcov).])],
254 [_AX_CODE_COVERAGE_GCOV_PROG_WITH=$with_gcov],
255 [_AX_CODE_COVERAGE_GCOV_PROG_WITH=gcov])
257 AC_MSG_CHECKING([whether to build with code coverage support])
258 AC_ARG_ENABLE([code-coverage],
259 AS_HELP_STRING([--enable-code-coverage],
260 [Whether to enable code coverage support]),,
261 enable_code_coverage=no)
263 AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test "x$enable_code_coverage" = xyes])
264 AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
265 AC_MSG_RESULT($enable_code_coverage)
267 AS_IF([ test "x$enable_code_coverage" = xyes ], [
268 _AX_CODE_COVERAGE_ENABLED
271 _AX_CODE_COVERAGE_RULES