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