blob: 5120c3bb1c089578bc8aef4d8939c5d9a68ca02f [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001# ===========================================================================
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_CFLAGS and CODE_COVERAGE_LDFLAGS which should be
12# included in the CFLAGS and LIBS/LDFLAGS variables of every build target
13# (program or library) which should be built with code coverage support.
14# Also defines CODE_COVERAGE_RULES which should be substituted in your
15# Makefile; and $enable_code_coverage which can be used in subsequent
16# configure output. CODE_COVERAGE_ENABLED is defined and substituted, and
17# corresponds to the value of the --enable-code-coverage option, which
18# defaults to being disabled.
19#
20# Test also for gcov program and create GCOV variable that could be
21# substituted.
22#
23# Note that all optimisation flags in CFLAGS must be disabled when code
24# coverage is enabled.
25#
26# Usage example:
27#
28# configure.ac:
29#
30# AX_CODE_COVERAGE
31#
32# Makefile.am:
33#
34# @CODE_COVERAGE_RULES@
35# my_program_LIBS = ... $(CODE_COVERAGE_LDFLAGS) ...
36# my_program_CFLAGS = ... $(CODE_COVERAGE_CFLAGS) ...
37#
38# This results in a "check-code-coverage" rule being added to any
39# Makefile.am which includes "@CODE_COVERAGE_RULES@" (assuming the module
40# has been configured with --enable-code-coverage). Running `make
41# check-code-coverage` in that directory will run the module's test suite
42# (`make check`) and build a code coverage report detailing the code which
43# was touched, then print the URI for the report.
44#
45# This code was derived from Makefile.decl in GLib, originally licenced
46# under LGPLv2.1+.
47#
48# LICENSE
49#
50# Copyright (c) 2012 Philip Withnall
51# Copyright (c) 2012 Xan Lopez
52# Copyright (c) 2012 Christian Persch
53# Copyright (c) 2012 Paolo Borelli
54# Copyright (c) 2012 Dan Winship
55# Copyright (c) 2015 Bastien ROUCARIES
56#
57# This library is free software; you can redistribute it and/or modify it
58# under the terms of the GNU Lesser General Public License as published by
59# the Free Software Foundation; either version 2.1 of the License, or (at
60# your option) any later version.
61#
62# This library is distributed in the hope that it will be useful, but
63# WITHOUT ANY WARRANTY; without even the implied warranty of
64# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
65# General Public License for more details.
66#
67# You should have received a copy of the GNU Lesser General Public License
68# along with this program. If not, see <http://www.gnu.org/licenses/>.
69
70#serial 5
71
72AC_DEFUN([AX_CODE_COVERAGE],[
73 dnl Check for --enable-code-coverage
74 AC_REQUIRE([AC_PROG_SED])
75
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
82 AC_MSG_CHECKING([whether to build with code coverage support])
83 AC_ARG_ENABLE([code-coverage],
84 AS_HELP_STRING([--enable-code-coverage],
85 [Whether to enable code coverage support]),,
86 enable_code_coverage=no)
87
88 AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test x$enable_code_coverage = xyes])
89 AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
90 AC_MSG_RESULT($enable_code_coverage)
91
92 AS_IF([ test "$enable_code_coverage" = "yes" ], [
93 # check for gcov
94 AC_CHECK_TOOL([GCOV],
95 [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],
96 [:])
97 AS_IF([test "X$GCOV" = "X:"],
98 [AC_MSG_ERROR([gcov is needed to do coverage])])
99 AC_SUBST([GCOV])
100
101 dnl Check if gcc is being used
102 AS_IF([ test "$GCC" = "no" ], [
103 AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
104 ])
105
106 # List of supported lcov versions.
107 lcov_version_list="1.6 1.7 1.8 1.9 1.10 1.11"
108
109 AC_CHECK_PROG([LCOV], [lcov], [lcov])
110 AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
111
112 AS_IF([ test "$LCOV" ], [
113 AC_CACHE_CHECK([for lcov version], ax_cv_lcov_version, [
114 ax_cv_lcov_version=invalid
115 lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'`
116 for lcov_check_version in $lcov_version_list; do
117 if test "$lcov_version" = "$lcov_check_version"; then
118 ax_cv_lcov_version="$lcov_check_version (ok)"
119 fi
120 done
121 ])
122 ], [
123 lcov_msg="To enable code coverage reporting you must have one of the following lcov versions installed: $lcov_version_list"
124 AC_MSG_ERROR([$lcov_msg])
125 ])
126
127 case $ax_cv_lcov_version in
128 ""|invalid[)]
129 lcov_msg="You must have one of the following versions of lcov: $lcov_version_list (found: $lcov_version)."
130 AC_MSG_ERROR([$lcov_msg])
131 LCOV="exit 0;"
132 ;;
133 esac
134
135 AS_IF([ test -z "$GENHTML" ], [
136 AC_MSG_ERROR([Could not find genhtml from the lcov package])
137 ])
138
139 dnl Build the code coverage flags
140 CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
141 CODE_COVERAGE_LDFLAGS="-lgcov"
142
143 AC_SUBST([CODE_COVERAGE_CFLAGS])
144 AC_SUBST([CODE_COVERAGE_LDFLAGS])
145
146CODE_COVERAGE_RULES='
147# Code coverage
148#
149# Optional:
150# - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
151# (Default: $(top_builddir))
152# - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
153# by lcov for code coverage. (Default:
154# $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info)
155# - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
156# reports to be created. (Default:
157# $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage)
158# - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov
159# - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the lcov instance.
160# (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
161# - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the lcov instance.
162# (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
163# - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
164# instance. (Default: empty)
165# - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
166#
167# The generated report will be titled using the $(PACKAGE_NAME) and
168# $(PACKAGE_VERSION). In order to add the current git hash to the title,
169# use the git-version-gen script, available online.
170
171# Optional variables
172CODE_COVERAGE_DIRECTORY ?= $(top_builddir)
173CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info
174CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage
175CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool "$(GCOV)"
176CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= $(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)
177CODE_COVERAGE_LCOV_OPTIONS ?= $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
178CODE_COVERAGE_GENHTML_OPTIONS ?=
179CODE_COVERAGE_IGNORE_PATTERN ?=
180
181code_coverage_quiet = $(code_coverage_quiet_$(V))
182code_coverage_quiet_ =
183code_coverage_quiet_0 = --quiet
184
185# Use recursive makes in order to ignore errors during check
186check-code-coverage:
187 -$(MAKE) $(AM_MAKEFLAGS) -k check
188 $(MAKE) $(AM_MAKEFLAGS) code-coverage-capture
189
190# Capture code coverage data
191code-coverage-capture: code-coverage-capture-hook
192 $(LCOV) $(code_coverage_quiet) --directory $(CODE_COVERAGE_DIRECTORY) --capture --output-file "$(CODE_COVERAGE_OUTPUT_FILE).tmp" --test-name "$(PACKAGE_NAME)-$(PACKAGE_VERSION)" --no-checksum --compat-libtool $(CODE_COVERAGE_LCOV_OPTIONS)
193 $(LCOV) $(code_coverage_quiet) --directory $(CODE_COVERAGE_DIRECTORY) --remove "$(CODE_COVERAGE_OUTPUT_FILE).tmp" "/tmp/*" $(CODE_COVERAGE_IGNORE_PATTERN) --output-file "$(CODE_COVERAGE_OUTPUT_FILE)"
194 -@rm -f $(CODE_COVERAGE_OUTPUT_FILE).tmp
195 LANG=C $(GENHTML) $(code_coverage_quiet) --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)
196 @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html"
197
198# Hook rule executed before code-coverage-capture, overridable by the user
199code-coverage-capture-hook:
200
201clean: code-coverage-clean
202code-coverage-clean:
203 -$(LCOV) --directory $(top_builddir) -z
204 -rm -rf $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_FILE).tmp $(CODE_COVERAGE_OUTPUT_DIRECTORY)
205 -find . -name "*.gcda" -o -name "*.gcov" -delete
206
207GITIGNOREFILES ?=
208GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY)
209
210DISTCHECK_CONFIGURE_FLAGS ?=
211DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage
212
213.PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean
214'
215 AC_SUBST([CODE_COVERAGE_RULES])
216 ])
217
218 m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([CODE_COVERAGE_RULES])])
219])